*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35
36 (eval-when-compile (require 'cl))
37
38 ;; Site dependent variables.  These variables should be defined in
39 ;; paths.el.
40
41 (defvar gnus-default-nntp-server nil
42   "Specify a default NNTP server.
43 This variable should be defined in paths.el, and should never be set
44 by the user.
45 If you want to change servers, you should use `gnus-select-method'.
46 See the documentation to that variable.")
47
48 (defvar gnus-backup-default-subscribed-newsgroups
49   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
50   "Default default new newsgroups the first time Gnus is run.
51 Should be set in paths.el, and shouldn't be touched by the user.")
52
53 (defvar gnus-local-domain nil
54   "Local domain name without a host name.
55 The DOMAINNAME environment variable is used instead if it is defined.
56 If the `system-name' function returns the full Internet name, there is
57 no need to set this variable.")
58
59 (defvar gnus-local-organization nil
60   "String with a description of what organization (if any) the user belongs to.
61 The ORGANIZATION environment variable is used instead if it is defined.
62 If this variable contains a function, this function will be called
63 with the current newsgroup name as the argument.  The function should
64 return a string.
65
66 In any case, if the string (either in the variable, in the environment
67 variable, or returned by the function) is a file name, the contents of
68 this file will be used as the organization.")
69
70 ;; Customization variables
71
72 ;; Don't touch this variable.
73 (defvar gnus-nntp-service "nntp"
74   "*NNTP service name (\"nntp\" or 119).
75 This is an obsolete variable, which is scarcely used.  If you use an
76 nntp server for your newsgroup and want to change the port number
77 used to 899, you would say something along these lines:
78
79  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
80
81 (defvar gnus-nntpserver-file "/etc/nntpserver"
82   "*A file with only the name of the nntp server in it.")
83
84 ;; This function is used to check both the environment variable
85 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
86 ;; an nntp server name default.
87 (defun gnus-getenv-nntpserver ()
88   (or (getenv "NNTPSERVER")
89       (and (file-readable-p gnus-nntpserver-file)
90            (save-excursion
91              (set-buffer (get-buffer-create " *gnus nntp*"))
92              (buffer-disable-undo (current-buffer))
93              (insert-file-contents gnus-nntpserver-file)
94              (let ((name (buffer-string)))
95                (prog1
96                    (if (string-match "^[ \t\n]*$" name)
97                        nil
98                      name)
99                  (kill-buffer (current-buffer))))))))
100
101 (defvar gnus-select-method
102   (nconc
103    (list 'nntp (or (condition-case ()
104                        (gnus-getenv-nntpserver)
105                      (error nil))
106                    (if (and gnus-default-nntp-server
107                             (not (string= gnus-default-nntp-server "")))
108                        gnus-default-nntp-server)
109                    (system-name)))
110    (if (or (null gnus-nntp-service)
111            (equal gnus-nntp-service "nntp"))
112        nil
113      (list gnus-nntp-service)))
114   "*Default method for selecting a newsgroup.
115 This variable should be a list, where the first element is how the
116 news is to be fetched, the second is the address.
117
118 For instance, if you want to get your news via NNTP from
119 \"flab.flab.edu\", you could say:
120
121 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
122
123 If you want to use your local spool, say:
124
125 (setq gnus-select-method (list 'nnspool (system-name)))
126
127 If you use this variable, you must set `gnus-nntp-server' to nil.
128
129 There is a lot more to know about select methods and virtual servers -
130 see the manual for details.")
131
132 (defvar gnus-message-archive-method 
133   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
134              (nnfolder-active-file "~/Mail/archive/active")
135              (nnfolder-get-new-mail nil)
136              (nnfolder-inhibit-expiry t))
137   "*Method used for archiving messages you've sent.
138 This should be a mail method.")
139
140 (defvar gnus-refer-article-method nil
141   "*Preferred method for fetching an article by Message-ID.
142 If you are reading news from the local spool (with nnspool), fetching
143 articles by Message-ID is painfully slow.  By setting this method to an
144 nntp method, you might get acceptable results.
145
146 The value of this variable must be a valid select method as discussed
147 in the documentation of `gnus-select-method'.")
148
149 (defvar gnus-secondary-select-methods nil
150   "*A list of secondary methods that will be used for reading news.
151 This is a list where each element is a complete select method (see
152 `gnus-select-method').
153
154 If, for instance, you want to read your mail with the nnml backend,
155 you could set this variable:
156
157 (setq gnus-secondary-select-methods '((nnml \"\")))")
158
159 (defvar gnus-secondary-servers nil
160   "*List of NNTP servers that the user can choose between interactively.
161 To make Gnus query you for a server, you have to give `gnus' a
162 non-numeric prefix - `C-u M-x gnus', in short.")
163
164 (defvar gnus-nntp-server nil
165   "*The name of the host running the NNTP server.
166 This variable is semi-obsolete.  Use the `gnus-select-method'
167 variable instead.")
168
169 (defvar gnus-startup-file "~/.newsrc"
170   "*Your `.newsrc' file.
171 `.newsrc-SERVER' will be used instead if that exists.")
172
173 (defvar gnus-init-file "~/.gnus"
174   "*Your Gnus elisp startup file.
175 If a file with the .el or .elc suffixes exist, it will be read
176 instead.")
177
178 (defvar gnus-group-faq-directory
179   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
180     "/ftp@sunsite.auc.dk:/pub/usenet/"
181     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
182     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
183     "/ftp@rtfm.mit.edu:/pub/usenet/"
184     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
185     "/ftp@ftp.sunet.se:/pub/usenet/"
186     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
187     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
188     "/ftp@ftp.hk.super.net:/mirror/faqs/")
189   "*Directory where the group FAQs are stored.
190 This will most commonly be on a remote machine, and the file will be
191 fetched by ange-ftp.
192
193 This variable can also be a list of directories.  In that case, the
194 first element in the list will be used by default, and the others will
195 be used as backup sites.
196
197 Note that Gnus uses an aol machine as the default directory.  If this
198 feels fundamentally unclean, just think of it as a way to finally get
199 something of value back from them.
200
201 If the default site is too slow, try one of these:
202
203    North America: mirrors.aol.com                /pub/rtfm/usenet
204                   ftp.seas.gwu.edu               /pub/rtfm
205                   rtfm.mit.edu                   /pub/usenet
206    Europe:        ftp.uni-paderborn.de           /pub/FAQ
207                   src.doc.ic.ac.uk               /usenet/news-FAQS
208                   ftp.sunet.se                   /pub/usenet
209                   sunsite.auc.dk                 /pub/usenet
210    Asia:          nctuccca.edu.tw                /USENET/FAQ
211                   hwarang.postech.ac.kr          /pub/usenet
212                   ftp.hk.super.net               /mirror/faqs")
213
214 (defvar gnus-group-archive-directory
215   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
216   "*The address of the (ding) archives.")
217
218 (defvar gnus-group-recent-archive-directory
219   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
220   "*The address of the most recent (ding) articles.")
221
222 (defvar gnus-default-subscribed-newsgroups nil
223   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
224 It should be a list of strings.
225 If it is `t', Gnus will not do anything special the first time it is
226 started; it'll just use the normal newsgroups subscription methods.")
227
228 (defvar gnus-use-cross-reference t
229   "*Non-nil means that cross referenced articles will be marked as read.
230 If nil, ignore cross references.  If t, mark articles as read in
231 subscribed newsgroups.  If neither t nor nil, mark as read in all
232 newsgroups.")
233
234 (defvar gnus-single-article-buffer t
235   "*If non-nil, display all articles in the same buffer.
236 If nil, each group will get its own article buffer.")
237
238 (defvar gnus-use-dribble-file t
239   "*Non-nil means that Gnus will use a dribble file to store user updates.
240 If Emacs should crash without saving the .newsrc files, complete
241 information can be restored from the dribble file.")
242
243 (defvar gnus-dribble-directory nil
244   "*The directory where dribble files will be saved.
245 If this variable is nil, the directory where the .newsrc files are
246 saved will be used.")
247
248 (defvar gnus-asynchronous nil
249   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
250
251 (defvar gnus-kill-summary-on-exit t
252   "*If non-nil, kill the summary buffer when you exit from it.
253 If nil, the summary will become a \"*Dead Summary*\" buffer, and
254 it will be killed sometime later.")
255
256 (defvar gnus-large-newsgroup 200
257   "*The number of articles which indicates a large newsgroup.
258 If the number of articles in a newsgroup is greater than this value,
259 confirmation is required for selecting the newsgroup.")
260
261 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
262 (defvar gnus-no-groups-message "No news is horrible news"
263   "*Message displayed by Gnus when no groups are available.")
264
265 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
266   "*Non-nil means that the default name of a file to save articles in is the group name.
267 If it's nil, the directory form of the group name is used instead.
268
269 If this variable is a list, and the list contains the element
270 `not-score', long file names will not be used for score files; if it
271 contains the element `not-save', long file names will not be used for
272 saving; and if it contains the element `not-kill', long file names
273 will not be used for kill files.")
274
275 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
276   "*Name of the directory articles will be saved in (default \"~/News\").
277 Initialized from the SAVEDIR environment variable.")
278
279 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
280   "*Name of the directory where kill files will be stored (default \"~/News\").
281 Initialized from the SAVEDIR environment variable.")
282
283 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
284   "*A function to save articles in your favorite format.
285 The function must be interactively callable (in other words, it must
286 be an Emacs command).
287
288 Gnus provides the following functions:
289
290 * gnus-summary-save-in-rmail (Rmail format)
291 * gnus-summary-save-in-mail (Unix mail format)
292 * gnus-summary-save-in-folder (MH folder)
293 * gnus-summary-save-in-file (article format).
294 * gnus-summary-save-in-vm (use VM's folder format).")
295
296 (defvar gnus-prompt-before-saving 'always
297   "*This variable says how much prompting is to be done when saving articles.
298 If it is nil, no prompting will be done, and the articles will be
299 saved to the default files.  If this variable is `always', each and
300 every article that is saved will be preceded by a prompt, even when
301 saving large batches of articles.  If this variable is neither nil not
302 `always', there the user will be prompted once for a file name for
303 each invocation of the saving commands.")
304
305 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
306   "*A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
308
309 (defvar gnus-mail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Unix mail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-folder-save-name (function gnus-folder-save-name)
314   "*A function generating a file name to save articles in MH folder.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
316
317 (defvar gnus-file-save-name (function gnus-numeric-save-name)
318   "*A function generating a file name to save articles in article format.
319 The function is called with NEWSGROUP, HEADERS, and optional
320 LAST-FILE.")
321
322 (defvar gnus-split-methods
323   '((gnus-article-archive-name))
324   "*Variable used to suggest where articles are to be saved.
325 For instance, if you would like to save articles related to Gnus in
326 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
327 you could set this variable to something like:
328
329  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
330    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
331
332 This variable is an alist where the where the key is the match and the
333 value is a list of possible files to save in if the match is non-nil.
334
335 If the match is a string, it is used as a regexp match on the
336 article.  If the match is a symbol, that symbol will be funcalled
337 from the buffer of the article to be saved with the newsgroup as the
338 parameter.  If it is a list, it will be evaled in the same buffer.
339
340 If this form or function returns a string, this string will be used as
341 a possible file name; and if it returns a non-nil list, that list will
342 be used as possible file names.")
343
344 (defvar gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable.")
347
348 (defvar gnus-save-score nil
349   "*If non-nil, save group scoring info.")
350
351 (defvar gnus-use-adaptive-scoring nil
352   "*If non-nil, use some adaptive scoring scheme.")
353
354 (defvar gnus-use-cache nil
355   "*If nil, Gnus will ignore the article cache.
356 If `passive', it will allow entering (and reading) articles
357 explicitly entered into the cache.  If anything else, use the
358 cache to the full extent of the law.")
359
360 (defvar gnus-use-trees nil
361   "*If non-nil, display a thread tree buffer.")
362
363 (defvar gnus-use-grouplens nil
364   "*If non-nil, use GroupLens ratings.")
365
366 (defvar gnus-keep-backlog nil
367   "*If non-nil, Gnus will keep read articles for later re-retrieval.
368 If it is a number N, then Gnus will only keep the last N articles
369 read.  If it is neither nil nor a number, Gnus will keep all read
370 articles.  This is not a good idea.")
371
372 (defvar gnus-use-nocem nil
373   "*If non-nil, Gnus will read NoCeM cancel messages.")
374
375 (defvar gnus-use-demon nil
376   "If non-nil, Gnus might use some demons.")
377
378 (defvar gnus-use-scoring t
379   "*If non-nil, enable scoring.")
380
381 (defvar gnus-use-picons nil
382   "*If non-nil, display picons.")
383
384 (defvar gnus-fetch-old-headers nil
385   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
386 If an unread article in the group refers to an older, already read (or
387 just marked as read) article, the old article will not normally be
388 displayed in the Summary buffer.  If this variable is non-nil, Gnus
389 will attempt to grab the headers to the old articles, and thereby
390 build complete threads.  If it has the value `some', only enough
391 headers to connect otherwise loose threads will be displayed.
392 This variable can also be a number.  In that case, no more than that
393 number of old headers will be fetched.
394
395 The server has to support NOV for any of this to work.")
396
397 ;see gnus-cus.el
398 ;(defvar gnus-visual t
399 ;  "*If non-nil, will do various highlighting.
400 ;If nil, no mouse highlights (or any other highlights) will be
401 ;performed.  This might speed up Gnus some when generating large group
402 ;and summary buffers.")
403
404 (defvar gnus-novice-user t
405   "*Non-nil means that you are a usenet novice.
406 If non-nil, verbose messages may be displayed and confirmations may be
407 required.")
408
409 (defvar gnus-expert-user nil
410   "*Non-nil means that you will never be asked for confirmation about anything.
411 And that means *anything*.")
412
413 (defvar gnus-verbose 7
414   "*Integer that says how verbose Gnus should be.
415 The higher the number, the more messages Gnus will flash to say what
416 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
417 display most important messages; and at ten, Gnus will keep on
418 jabbering all the time.")
419
420 (defvar gnus-keep-same-level nil
421   "*Non-nil means that the next newsgroup after the current will be on the same level.
422 When you type, for instance, `n' after reading the last article in the
423 current newsgroup, you will go to the next newsgroup.  If this variable
424 is nil, the next newsgroup will be the next from the group
425 buffer.
426 If this variable is non-nil, Gnus will either put you in the
427 next newsgroup with the same level, or, if no such newsgroup is
428 available, the next newsgroup with the lowest possible level higher
429 than the current level.
430 If this variable is `best', Gnus will make the next newsgroup the one
431 with the best level.")
432
433 (defvar gnus-summary-make-false-root 'adopt
434   "*nil means that Gnus won't gather loose threads.
435 If the root of a thread has expired or been read in a previous
436 session, the information necessary to build a complete thread has been
437 lost.  Instead of having many small sub-threads from this original thread
438 scattered all over the summary buffer, Gnus can gather them.
439
440 If non-nil, Gnus will try to gather all loose sub-threads from an
441 original thread into one large thread.
442
443 If this variable is non-nil, it should be one of `none', `adopt',
444 `dummy' or `empty'.
445
446 If this variable is `none', Gnus will not make a false root, but just
447 present the sub-threads after another.
448 If this variable is `dummy', Gnus will create a dummy root that will
449 have all the sub-threads as children.
450 If this variable is `adopt', Gnus will make one of the \"children\"
451 the parent and mark all the step-children as such.
452 If this variable is `empty', the \"children\" are printed with empty
453 subject fields.  (Or rather, they will be printed with a string
454 given by the `gnus-summary-same-subject' variable.)")
455
456 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
457   "*A regexp to match subjects to be excluded from loose thread gathering.
458 As loose thread gathering is done on subjects only, that means that
459 there can be many false gatherings performed.  By rooting out certain
460 common subjects, gathering might become saner.")
461
462 (defvar gnus-summary-gather-subject-limit nil
463   "*Maximum length of subject comparisons when gathering loose threads.
464 Use nil to compare full subjects.  Setting this variable to a low
465 number will help gather threads that have been corrupted by
466 newsreaders chopping off subject lines, but it might also mean that
467 unrelated articles that have subject that happen to begin with the
468 same few characters will be incorrectly gathered.
469
470 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
471 comparing subjects.")
472
473 (defvar gnus-simplify-ignored-prefixes nil
474   "*Regexp, matches for which are removed from subject lines when simplifying.")
475
476 (defvar gnus-build-sparse-threads nil
477   "*If non-nil, fill in the gaps in threads.
478 If `some', only fill in the gaps that are needed to tie loose threads
479 together.  If `more', fill in all leaf nodes that Gnus can find.  If
480 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
481
482 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
483   "Function used for gathering loose threads.
484 There are two pre-defined functions: `gnus-gather-threads-by-subject',
485 which only takes Subjects into consideration; and
486 `gnus-gather-threads-by-references', which compared the References
487 headers of the articles to find matches.")
488
489 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
490 (defvar gnus-summary-same-subject ""
491   "*String indicating that the current article has the same subject as the previous.
492 This variable will only be used if the value of
493 `gnus-summary-make-false-root' is `empty'.")
494
495 (defvar gnus-summary-goto-unread t
496   "*If non-nil, marking commands will go to the next unread article.
497 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
498 whether it is read or not.")
499
500 (defvar gnus-group-goto-unread t
501   "*If non-nil, movement commands will go to the next unread and subscribed group.")
502
503 (defvar gnus-goto-next-group-when-activating t
504   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
505
506 (defvar gnus-check-new-newsgroups t
507   "*Non-nil means that Gnus will add new newsgroups at startup.
508 If this variable is `ask-server', Gnus will ask the server for new
509 groups since the last time it checked.  This means that the killed list
510 is no longer necessary, so you could set `gnus-save-killed-list' to
511 nil.
512
513 A variant is to have this variable be a list of select methods.  Gnus
514 will then use the `ask-server' method on all these select methods to
515 query for new groups from all those servers.
516
517 Eg.
518   (setq gnus-check-new-newsgroups
519         '((nntp \"some.server\") (nntp \"other.server\")))
520
521 If this variable is nil, then you have to tell Gnus explicitly to
522 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
523
524 (defvar gnus-check-bogus-newsgroups nil
525   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
528
529 (defvar gnus-read-active-file t
530   "*Non-nil means that Gnus will read the entire active file at startup.
531 If this variable is nil, Gnus will only know about the groups in your
532 `.newsrc' file.
533
534 If this variable is `some', Gnus will try to only read the relevant
535 parts of the active file from the server.  Not all servers support
536 this, and it might be quite slow with other servers, but this should
537 generally be faster than both the t and nil value.
538
539 If you set this variable to nil or `some', you probably still want to
540 be told about new newsgroups that arrive.  To do that, set
541 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
542 properly with all servers.")
543
544 (defvar gnus-level-subscribed 5
545   "*Groups with levels less than or equal to this variable are subscribed.")
546
547 (defvar gnus-level-unsubscribed 7
548   "*Groups with levels less than or equal to this variable are unsubscribed.
549 Groups with levels less than `gnus-level-subscribed', which should be
550 less than this variable, are subscribed.")
551
552 (defvar gnus-level-zombie 8
553   "*Groups with this level are zombie groups.")
554
555 (defvar gnus-level-killed 9
556   "*Groups with this level are killed.")
557
558 (defvar gnus-level-default-subscribed 3
559   "*New subscribed groups will be subscribed at this level.")
560
561 (defvar gnus-level-default-unsubscribed 6
562   "*New unsubscribed groups will be unsubscribed at this level.")
563
564 (defvar gnus-activate-level (1+ gnus-level-subscribed)
565   "*Groups higher than this level won't be activated on startup.
566 Setting this variable to something log might save lots of time when
567 you have many groups that you aren't interested in.")
568
569 (defvar gnus-activate-foreign-newsgroups 4
570   "*If nil, Gnus will not check foreign newsgroups at startup.
571 If it is non-nil, it should be a number between one and nine.  Foreign
572 newsgroups that have a level lower or equal to this number will be
573 activated on startup.  For instance, if you want to active all
574 subscribed newsgroups, but not the rest, you'd set this variable to
575 `gnus-level-subscribed'.
576
577 If you subscribe to lots of newsgroups from different servers, startup
578 might take a while.  By setting this variable to nil, you'll save time,
579 but you won't be told how many unread articles there are in the
580 groups.")
581
582 (defvar gnus-save-newsrc-file t
583   "*Non-nil means that Gnus will save the `.newsrc' file.
584 Gnus always saves its own startup file, which is called
585 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
586 be readily understood by other newsreaders.  If you don't plan on
587 using other newsreaders, set this variable to nil to save some time on
588 exit.")
589
590 (defvar gnus-save-killed-list t
591   "*If non-nil, save the list of killed groups to the startup file.
592 If you set this variable to nil, you'll save both time (when starting
593 and quitting) and space (both memory and disk), but it will also mean
594 that Gnus has no record of which groups are new and which are old, so
595 the automatic new newsgroups subscription methods become meaningless.
596
597 You should always set `gnus-check-new-newsgroups' to `ask-server' or
598 nil if you set this variable to nil.")
599
600 (defvar gnus-interactive-catchup t
601   "*If non-nil, require your confirmation when catching up a group.")
602
603 (defvar gnus-interactive-post t
604   "*If non-nil, group name will be asked for when posting.")
605
606 (defvar gnus-interactive-exit t
607   "*If non-nil, require your confirmation when exiting Gnus.")
608
609 (defvar gnus-kill-killed t
610   "*If non-nil, Gnus will apply kill files to already killed articles.
611 If it is nil, Gnus will never apply kill files to articles that have
612 already been through the scoring process, which might very well save lots
613 of time.")
614
615 (defvar gnus-extract-address-components 'gnus-extract-address-components
616   "*Function for extracting address components from a From header.
617 Two pre-defined function exist: `gnus-extract-address-components',
618 which is the default, quite fast, and too simplistic solution, and
619 `mail-extract-address-components', which works much better, but is
620 slower.")
621
622 (defvar gnus-summary-default-score 0
623   "*Default article score level.
624 If this variable is nil, scoring will be disabled.")
625
626 (defvar gnus-summary-zcore-fuzz 0
627   "*Fuzziness factor for the zcore in the summary buffer.
628 Articles with scores closer than this to `gnus-summary-default-score'
629 will not be marked.")
630
631 (defvar gnus-simplify-subject-fuzzy-regexp nil
632   "*Strings to be removed when doing fuzzy matches.
633 This can either be a regular expression or list of regular expressions
634 that will be removed from subject strings if fuzzy subject
635 simplification is selected.")
636
637 (defvar gnus-permanently-visible-groups nil
638   "*Regexp to match groups that should always be listed in the group buffer.
639 This means that they will still be listed when there are no unread
640 articles in the groups.")
641
642 (defvar gnus-list-groups-with-ticked-articles t
643   "*If non-nil, list groups that have only ticked articles.
644 If nil, only list groups that have unread articles.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level.
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-group-list-inactive-groups t
654   "*If non-nil, inactive groups will be listed.")
655
656 (defvar gnus-show-mime nil
657   "*If non-nil, do mime processing of articles.
658 The articles will simply be fed to the function given by
659 `gnus-show-mime-method'.")
660
661 (defvar gnus-strict-mime t
662   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
663
664 (defvar gnus-show-mime-method 'metamail-buffer
665   "*Function to process a MIME message.
666 The function is called from the article buffer.")
667
668 (defvar gnus-decode-encoded-word-method (lambda ())
669   "*Function to decode a MIME encoded-words.
670 The function is called from the article buffer.")
671
672 (defvar gnus-show-threads t
673   "*If non-nil, display threads in summary mode.")
674
675 (defvar gnus-thread-hide-subtree nil
676   "*If non-nil, hide all threads initially.
677 If threads are hidden, you have to run the command
678 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
679 to expose hidden threads.")
680
681 (defvar gnus-thread-hide-killed t
682   "*If non-nil, hide killed threads automatically.")
683
684 (defvar gnus-thread-ignore-subject nil
685   "*If non-nil, ignore subjects and do all threading based on the Reference header.
686 If nil, which is the default, articles that have different subjects
687 from their parents will start separate threads.")
688
689 (defvar gnus-thread-operation-ignore-subject t
690   "*If non-nil, subjects will be ignored when doing thread commands.
691 This affects commands like `gnus-summary-kill-thread' and
692 `gnus-summary-lower-thread'.
693
694 If this variable is nil, articles in the same thread with different
695 subjects will not be included in the operation in question.  If this
696 variable is `fuzzy', only articles that have subjects that are fuzzily
697 equal will be included.")
698
699 (defvar gnus-thread-indent-level 4
700   "*Number that says how much each sub-thread should be indented.")
701
702 (defvar gnus-ignored-newsgroups
703   (purecopy (mapconcat 'identity
704                        '("^to\\."       ; not "real" groups
705                          "^[0-9. \t]+ " ; all digits in name
706                          "[][\"#'()]"   ; bogus characters
707                          )
708                        "\\|"))
709   "*A regexp to match uninteresting newsgroups in the active file.
710 Any lines in the active file matching this regular expression are
711 removed from the newsgroup list before anything else is done to it,
712 thus making them effectively non-existent.")
713
714 (defvar gnus-ignored-headers
715   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
716   "*All headers that match this regexp will be hidden.
717 This variable can also be a list of regexps of headers to be ignored.
718 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
719
720 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
721   "*All headers that do not match this regexp will be hidden.
722 This variable can also be a list of regexp of headers to remain visible.
723 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
724
725 (defvar gnus-sorted-header-list
726   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
727     "^Cc:" "^Date:" "^Organization:")
728   "*This variable is a list of regular expressions.
729 If it is non-nil, headers that match the regular expressions will
730 be placed first in the article buffer in the sequence specified by
731 this list.")
732
733 (defvar gnus-boring-article-headers
734   '(empty followup-to reply-to)
735   "*Headers that are only to be displayed if they have interesting data.
736 Possible values in this list are `empty', `newsgroups', `followup-to',
737 `reply-to', and `date'.")
738
739 (defvar gnus-show-all-headers nil
740   "*If non-nil, don't hide any headers.")
741
742 (defvar gnus-save-all-headers t
743   "*If non-nil, don't remove any headers before saving.")
744
745 (defvar gnus-saved-headers gnus-visible-headers
746   "*Headers to keep if `gnus-save-all-headers' is nil.
747 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
748 If that variable is nil, however, all headers that match this regexp
749 will be kept while the rest will be deleted before saving.")
750
751 (defvar gnus-inhibit-startup-message nil
752   "*If non-nil, the startup message will not be displayed.")
753
754 (defvar gnus-signature-separator "^-- *$"
755   "Regexp matching signature separator.")
756
757 (defvar gnus-signature-limit nil
758   "Provide a limit to what is considered a signature.
759 If it is a number, no signature may not be longer (in characters) than
760 that number.  If it is a function, the function will be called without
761 any parameters, and if it returns nil, there is no signature in the
762 buffer.  If it is a string, it will be used as a regexp.  If it
763 matches, the text in question is not a signature.")
764
765 (defvar gnus-auto-extend-newsgroup t
766   "*If non-nil, extend newsgroup forward and backward when requested.")
767
768 (defvar gnus-auto-select-first t
769   "*If nil, don't select the first unread article when entering a group.
770 If this variable is `best', select the highest-scored unread article
771 in the group.  If neither nil nor `best', select the first unread
772 article.
773
774 If you want to prevent automatic selection of the first unread article
775 in some newsgroups, set the variable to nil in
776 `gnus-select-group-hook'.")
777
778 (defvar gnus-auto-select-next t
779   "*If non-nil, offer to go to the next group from the end of the previous.
780 If the value is t and the next newsgroup is empty, Gnus will exit
781 summary mode and go back to group mode.  If the value is neither nil
782 nor t, Gnus will select the following unread newsgroup.  In
783 particular, if the value is the symbol `quietly', the next unread
784 newsgroup will be selected without any confirmation, and if it is
785 `almost-quietly', the next group will be selected without any
786 confirmation if you are located on the last article in the group.
787 Finally, if this variable is `slightly-quietly', the `Z n' command
788 will go to the next group without confirmation.")
789
790 (defvar gnus-auto-select-same nil
791   "*If non-nil, select the next article with the same subject.")
792
793 (defvar gnus-summary-check-current nil
794   "*If non-nil, consider the current article when moving.
795 The \"unread\" movement commands will stay on the same line if the
796 current article is unread.")
797
798 (defvar gnus-auto-center-summary t
799   "*If non-nil, always center the current summary buffer.
800 In particular, if `vertical' do only vertical recentering.  If non-nil
801 and non-`vertical', do both horizontal and vertical recentering.")
802
803 (defvar gnus-break-pages t
804   "*If non-nil, do page breaking on articles.
805 The page delimiter is specified by the `gnus-page-delimiter'
806 variable.")
807
808 (defvar gnus-page-delimiter "^\^L"
809   "*Regexp describing what to use as article page delimiters.
810 The default value is \"^\^L\", which is a form linefeed at the
811 beginning of a line.")
812
813 (defvar gnus-use-full-window t
814   "*If non-nil, use the entire Emacs screen.")
815
816 (defvar gnus-window-configuration nil
817   "Obsolete variable.  See `gnus-buffer-configuration'.")
818
819 (defvar gnus-window-min-width 2
820   "*Minimum width of Gnus buffers.")
821
822 (defvar gnus-window-min-height 1
823   "*Minimum height of Gnus buffers.")
824
825 (defvar gnus-buffer-configuration
826   '((group
827      (vertical 1.0
828                (group 1.0 point)
829                (if gnus-carpal '(group-carpal 4))))
830     (summary
831      (vertical 1.0
832                (summary 1.0 point)
833                (if gnus-carpal '(summary-carpal 4))))
834     (article
835      (cond 
836       (gnus-use-picons
837        '(frame 1.0
838                (vertical 1.0
839                          (summary 0.25 point)
840                          (if gnus-carpal '(summary-carpal 4))
841                          (article 1.0))
842                (vertical ((height . 5) (width . 15)
843                           (user-position . t)
844                           (left . -1) (top . 1))
845                          (picons 1.0))))
846       (gnus-use-trees
847        '(vertical 1.0
848                   (summary 0.25 point)
849                   (tree 0.25)
850                   (article 1.0)))
851       (t
852        '(vertical 1.0
853                  (summary 0.25 point)
854                  (if gnus-carpal '(summary-carpal 4))
855                  (if gnus-use-trees '(tree 0.25))
856                  (article 1.0)))))
857     (server
858      (vertical 1.0
859                (server 1.0 point)
860                (if gnus-carpal '(server-carpal 2))))
861     (browse
862      (vertical 1.0
863                (browse 1.0 point)
864                (if gnus-carpal '(browse-carpal 2))))
865     (message
866      (vertical 1.0
867                (message 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article-copy 0.5)
896                (message 1.0 point)))
897     (forward
898      (vertical 1.0
899                (message 1.0 point)))
900     (reply-yank
901      (vertical 1.0
902                (message 1.0 point)))
903     (mail-bounce
904      (vertical 1.0
905                (article 0.5)
906                (message 1.0 point)))
907     (draft
908      (vertical 1.0
909                (draft 1.0 point)))
910     (pipe
911      (vertical 1.0
912                (summary 0.25 point)
913                (if gnus-carpal '(summary-carpal 4))
914                ("*Shell Command Output*" 1.0)))
915     (bug
916      (vertical 1.0
917                ("*Gnus Help Bug*" 0.5)
918                ("*Gnus Bug*" 1.0 point)))
919     (compose-bounce
920      (vertical 1.0
921                (article 0.5)
922                (message 1.0 point))))
923   "Window configuration for all possible Gnus buffers.
924 This variable is a list of lists.  Each of these lists has a NAME and
925 a RULE.  The NAMEs are commonsense names like `group', which names a
926 rule used when displaying the group buffer; `summary', which names a
927 rule for what happens when you enter a group and do not display an
928 article buffer; and so on.  See the value of this variable for a
929 complete list of NAMEs.
930
931 Each RULE is a list of vectors.  The first element in this vector is
932 the name of the buffer to be displayed; the second element is the
933 percentage of the screen this buffer is to occupy (a number in the
934 0.0-0.99 range); the optional third element is `point', which should
935 be present to denote which buffer point is to go to after making this
936 buffer configuration.")
937
938 (defvar gnus-window-to-buffer
939   '((group . gnus-group-buffer)
940     (summary . gnus-summary-buffer)
941     (article . gnus-article-buffer)
942     (server . gnus-server-buffer)
943     (browse . "*Gnus Browse Server*")
944     (edit-group . gnus-group-edit-buffer)
945     (edit-server . gnus-server-edit-buffer)
946     (group-carpal . gnus-carpal-group-buffer)
947     (summary-carpal . gnus-carpal-summary-buffer)
948     (server-carpal . gnus-carpal-server-buffer)
949     (browse-carpal . gnus-carpal-browse-buffer)
950     (edit-score . gnus-score-edit-buffer)
951     (message . gnus-message-buffer)
952     (mail . gnus-message-buffer)
953     (post-news . gnus-message-buffer)
954     (faq . gnus-faq-buffer)
955     (picons . "*Picons*")
956     (tree . gnus-tree-buffer)
957     (info . gnus-info-buffer)
958     (article-copy . gnus-article-copy)
959     (draft . gnus-draft-buffer))
960   "Mapping from short symbols to buffer names or buffer variables.")
961
962 (defvar gnus-carpal nil
963   "*If non-nil, display clickable icons.")
964
965 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
966   "*Function called with a group name when new group is detected.
967 A few pre-made functions are supplied: `gnus-subscribe-randomly'
968 inserts new groups at the beginning of the list of groups;
969 `gnus-subscribe-alphabetically' inserts new groups in strict
970 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
971 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
972 for your decision; `gnus-subscribe-killed' kills all new groups.")
973
974 ;; Suggested by a bug report by Hallvard B Furuseth.
975 ;; <h.b.furuseth@usit.uio.no>.
976 (defvar gnus-subscribe-options-newsgroup-method
977   (function gnus-subscribe-alphabetically)
978   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
979 If, for instance, you want to subscribe to all newsgroups in the
980 \"no\" and \"alt\" hierarchies, you'd put the following in your
981 .newsrc file:
982
983 options -n no.all alt.all
984
985 Gnus will the subscribe all new newsgroups in these hierarchies with
986 the subscription method in this variable.")
987
988 (defvar gnus-subscribe-hierarchical-interactive nil
989   "*If non-nil, Gnus will offer to subscribe hierarchically.
990 When a new hierarchy appears, Gnus will ask the user:
991
992 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
993
994 If the user pressed `d', Gnus will descend the hierarchy, `y' will
995 subscribe to all newsgroups in the hierarchy and `s' will skip this
996 hierarchy in its entirety.")
997
998 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
999   "*Function used for sorting the group buffer.
1000 This function will be called with group info entries as the arguments
1001 for the groups to be sorted.  Pre-made functions include
1002 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1003 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1004 `gnus-group-sort-by-rank'.
1005
1006 This variable can also be a list of sorting functions.  In that case,
1007 the most significant sort function should be the last function in the
1008 list.")
1009
1010 ;; Mark variables suggested by Thomas Michanek
1011 ;; <Thomas.Michanek@telelogic.se>.
1012 (defvar gnus-unread-mark ? 
1013   "*Mark used for unread articles.")
1014 (defvar gnus-ticked-mark ?!
1015   "*Mark used for ticked articles.")
1016 (defvar gnus-dormant-mark ??
1017   "*Mark used for dormant articles.")
1018 (defvar gnus-del-mark ?r
1019   "*Mark used for del'd articles.")
1020 (defvar gnus-read-mark ?R
1021   "*Mark used for read articles.")
1022 (defvar gnus-expirable-mark ?E
1023   "*Mark used for expirable articles.")
1024 (defvar gnus-killed-mark ?K
1025   "*Mark used for killed articles.")
1026 (defvar gnus-souped-mark ?F
1027   "*Mark used for killed articles.")
1028 (defvar gnus-kill-file-mark ?X
1029   "*Mark used for articles killed by kill files.")
1030 (defvar gnus-low-score-mark ?Y
1031   "*Mark used for articles with a low score.")
1032 (defvar gnus-catchup-mark ?C
1033   "*Mark used for articles that are caught up.")
1034 (defvar gnus-replied-mark ?A
1035   "*Mark used for articles that have been replied to.")
1036 (defvar gnus-cached-mark ?*
1037   "*Mark used for articles that are in the cache.")
1038 (defvar gnus-saved-mark ?S
1039   "*Mark used for articles that have been saved to.")
1040 (defvar gnus-process-mark ?#
1041   "*Process mark.")
1042 (defvar gnus-ancient-mark ?O
1043   "*Mark used for ancient articles.")
1044 (defvar gnus-sparse-mark ?Q
1045   "*Mark used for sparsely reffed articles.")
1046 (defvar gnus-canceled-mark ?G
1047   "*Mark used for canceled articles.")
1048 (defvar gnus-score-over-mark ?+
1049   "*Score mark used for articles with high scores.")
1050 (defvar gnus-score-below-mark ?-
1051   "*Score mark used for articles with low scores.")
1052 (defvar gnus-empty-thread-mark ? 
1053   "*There is no thread under the article.")
1054 (defvar gnus-not-empty-thread-mark ?=
1055   "*There is a thread under the article.")
1056
1057 (defvar gnus-view-pseudo-asynchronously nil
1058   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1059
1060 (defvar gnus-view-pseudos nil
1061   "*If `automatic', pseudo-articles will be viewed automatically.
1062 If `not-confirm', pseudos will be viewed automatically, and the user
1063 will not be asked to confirm the command.")
1064
1065 (defvar gnus-view-pseudos-separately t
1066   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1067 If nil, all files that use the same viewing command will be given as a
1068 list of parameters to that command.")
1069
1070 (defvar gnus-insert-pseudo-articles t
1071   "*If non-nil, insert pseudo-articles when decoding articles.")
1072
1073 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1074   "*Format of group lines.
1075 It works along the same lines as a normal formatting string,
1076 with some simple extensions.
1077
1078 %M    Only marked articles (character, \"*\" or \" \")
1079 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1080 %L    Level of subscribedness (integer)
1081 %N    Number of unread articles (integer)
1082 %I    Number of dormant articles (integer)
1083 %i    Number of ticked and dormant (integer)
1084 %T    Number of ticked articles (integer)
1085 %R    Number of read articles (integer)
1086 %t    Total number of articles (integer)
1087 %y    Number of unread, unticked articles (integer)
1088 %G    Group name (string)
1089 %g    Qualified group name (string)
1090 %D    Group description (string)
1091 %s    Select method (string)
1092 %o    Moderated group (char, \"m\")
1093 %p    Process mark (char)
1094 %O    Moderated group (string, \"(m)\" or \"\")
1095 %P    Topic indentation (string)
1096 %l    Whether there are GroupLens predictions for this group (string)
1097 %n    Select from where (string)
1098 %z    A string that look like `<%s:%n>' if a foreign select method is used
1099 %u    User defined specifier.  The next character in the format string should
1100       be a letter.  Gnus will call the function gnus-user-format-function-X,
1101       where X is the letter following %u.  The function will be passed the
1102       current header as argument.  The function should return a string, which
1103       will be inserted into the buffer just like information from any other
1104       group specifier.
1105
1106 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1107 the mouse point move inside the area.  There can only be one such area.
1108
1109 Note that this format specification is not always respected.  For
1110 reasons of efficiency, when listing killed groups, this specification
1111 is ignored altogether.  If the spec is changed considerably, your
1112 output may end up looking strange when listing both alive and killed
1113 groups.
1114
1115 If you use %o or %O, reading the active file will be slower and quite
1116 a bit of extra memory will be used. %D will also worsen performance.
1117 Also note that if you change the format specification to include any
1118 of these specs, you must probably re-start Gnus to see them go into
1119 effect.")
1120
1121 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1122   "*The format specification of the lines in the summary buffer.
1123
1124 It works along the same lines as a normal formatting string,
1125 with some simple extensions.
1126
1127 %N   Article number, left padded with spaces (string)
1128 %S   Subject (string)
1129 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1130 %n   Name of the poster (string)
1131 %a   Extracted name of the poster (string)
1132 %A   Extracted address of the poster (string)
1133 %F   Contents of the From: header (string)
1134 %x   Contents of the Xref: header (string)
1135 %D   Date of the article (string)
1136 %d   Date of the article (string) in DD-MMM format
1137 %M   Message-id of the article (string)
1138 %r   References of the article (string)
1139 %c   Number of characters in the article (integer)
1140 %L   Number of lines in the article (integer)
1141 %I   Indentation based on thread level (a string of spaces)
1142 %T   A string with two possible values: 80 spaces if the article
1143      is on thread level two or larger and 0 spaces on level one
1144 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1145 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1146 %[   Opening bracket (character, \"[\" or \"<\")
1147 %]   Closing bracket (character, \"]\" or \">\")
1148 %>   Spaces of length thread-level (string)
1149 %<   Spaces of length (- 20 thread-level) (string)
1150 %i   Article score (number)
1151 %z   Article zcore (character)
1152 %t   Number of articles under the current thread (number).
1153 %e   Whether the thread is empty or not (character).
1154 %l   GroupLens score (string).
1155 %u   User defined specifier.  The next character in the format string should
1156      be a letter.  Gnus will call the function gnus-user-format-function-X,
1157      where X is the letter following %u.  The function will be passed the
1158      current header as argument.  The function should return a string, which
1159      will be inserted into the summary just like information from any other
1160      summary specifier.
1161
1162 Text between %( and %) will be highlighted with `gnus-mouse-face'
1163 when the mouse point is placed inside the area.  There can only be one
1164 such area.
1165
1166 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1167 with care.  For reasons of efficiency, Gnus will compute what column
1168 these characters will end up in, and \"hard-code\" that.  This means that
1169 it is illegal to have these specs after a variable-length spec.  Well,
1170 you might not be arrested, but your summary buffer will look strange,
1171 which is bad enough.
1172
1173 The smart choice is to have these specs as for to the left as
1174 possible.
1175
1176 This restriction may disappear in later versions of Gnus.")
1177
1178 (defvar gnus-summary-dummy-line-format
1179   "*  %(:                          :%) %S\n"
1180   "*The format specification for the dummy roots in the summary buffer.
1181 It works along the same lines as a normal formatting string,
1182 with some simple extensions.
1183
1184 %S  The subject")
1185
1186 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1187   "*The format specification for the summary mode line.
1188 It works along the same lines as a normal formatting string,
1189 with some simple extensions:
1190
1191 %G  Group name
1192 %p  Unprefixed group name
1193 %A  Current article number
1194 %V  Gnus version
1195 %U  Number of unread articles in the group
1196 %e  Number of unselected articles in the group
1197 %Z  A string with unread/unselected article counts
1198 %g  Shortish group name
1199 %S  Subject of the current article
1200 %u  User-defined spec
1201 %s  Current score file name
1202 %d  Number of dormant articles
1203 %r  Number of articles that have been marked as read in this session
1204 %E  Number of articles expunged by the score files")
1205
1206 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1207   "*The format specification for the article mode line.
1208 See `gnus-summary-mode-line-format' for a closer description.")
1209
1210 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1211   "*The format specification for the group mode line.
1212 It works along the same lines as a normal formatting string,
1213 with some simple extensions:
1214
1215 %S   The native news server.
1216 %M   The native select method.")
1217
1218 (defvar gnus-valid-select-methods
1219   '(("nntp" post address prompt-address)
1220     ("nnspool" post address)
1221     ("nnvirtual" post-mail virtual prompt-address)
1222     ("nnmbox" mail respool address)
1223     ("nnml" mail respool address)
1224     ("nnmh" mail respool address)
1225     ("nndir" post-mail prompt-address address)
1226     ("nneething" none address prompt-address)
1227     ("nndoc" none address prompt-address)
1228     ("nnbabyl" mail address respool)
1229     ("nnkiboze" post address virtual)
1230     ("nnsoup" post-mail address)
1231     ("nndraft" post-mail)
1232     ("nnfolder" mail respool address))
1233   "An alist of valid select methods.
1234 The first element of each list lists should be a string with the name
1235 of the select method.  The other elements may be be the category of
1236 this method (ie. `post', `mail', `none' or whatever) or other
1237 properties that this method has (like being respoolable).
1238 If you implement a new select method, all you should have to change is
1239 this variable.  I think.")
1240
1241 (defvar gnus-updated-mode-lines '(group article summary tree)
1242   "*List of buffers that should update their mode lines.
1243 The list may contain the symbols `group', `article' and `summary'.  If
1244 the corresponding symbol is present, Gnus will keep that mode line
1245 updated with information that may be pertinent.
1246 If this variable is nil, screen refresh may be quicker.")
1247
1248 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1249 (defvar gnus-mode-non-string-length nil
1250   "*Max length of mode-line non-string contents.
1251 If this is nil, Gnus will take space as is needed, leaving the rest
1252 of the modeline intact.")
1253
1254 ;see gnus-cus.el
1255 ;(defvar gnus-mouse-face 'highlight
1256 ;  "*Face used for mouse highlighting in Gnus.
1257 ;No mouse highlights will be done if `gnus-visual' is nil.")
1258
1259 (defvar gnus-summary-mark-below nil
1260   "*Mark all articles with a score below this variable as read.
1261 This variable is local to each summary buffer and usually set by the
1262 score file.")
1263
1264 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1265   "*List of functions used for sorting articles in the summary buffer.
1266 This variable is only used when not using a threaded display.")
1267
1268 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1269   "*List of functions used for sorting threads in the summary buffer.
1270 By default, threads are sorted by article number.
1271
1272 Each function takes two threads and return non-nil if the first thread
1273 should be sorted before the other.  If you use more than one function,
1274 the primary sort function should be the last.  You should probably
1275 always include `gnus-thread-sort-by-number' in the list of sorting
1276 functions -- preferably first.
1277
1278 Ready-mady functions include `gnus-thread-sort-by-number',
1279 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1280 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1281 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1282
1283 (defvar gnus-thread-score-function '+
1284   "*Function used for calculating the total score of a thread.
1285
1286 The function is called with the scores of the article and each
1287 subthread and should then return the score of the thread.
1288
1289 Some functions you can use are `+', `max', or `min'.")
1290
1291 (defvar gnus-summary-expunge-below nil
1292   "All articles that have a score less than this variable will be expunged.")
1293
1294 (defvar gnus-thread-expunge-below nil
1295   "All threads that have a total score less than this variable will be expunged.
1296 See `gnus-thread-score-function' for en explanation of what a
1297 \"thread score\" is.")
1298
1299 (defvar gnus-auto-subscribed-groups
1300   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1301   "*All new groups that match this regexp will be subscribed automatically.
1302 Note that this variable only deals with new groups.  It has no effect
1303 whatsoever on old groups.")
1304
1305 (defvar gnus-options-subscribe nil
1306   "*All new groups matching this regexp will be subscribed unconditionally.
1307 Note that this variable deals only with new newsgroups.  This variable
1308 does not affect old newsgroups.")
1309
1310 (defvar gnus-options-not-subscribe nil
1311   "*All new groups matching this regexp will be ignored.
1312 Note that this variable deals only with new newsgroups.  This variable
1313 does not affect old (already subscribed) newsgroups.")
1314
1315 (defvar gnus-auto-expirable-newsgroups nil
1316   "*Groups in which to automatically mark read articles as expirable.
1317 If non-nil, this should be a regexp that should match all groups in
1318 which to perform auto-expiry.  This only makes sense for mail groups.")
1319
1320 (defvar gnus-total-expirable-newsgroups nil
1321   "*Groups in which to perform expiry of all read articles.
1322 Use with extreme caution.  All groups that match this regexp will be
1323 expiring - which means that all read articles will be deleted after
1324 (say) one week.  (This only goes for mail groups and the like, of
1325 course.)")
1326
1327 (defvar gnus-group-uncollapsed-levels 1
1328   "Number of group name elements to leave alone when making a short group name.")
1329
1330 (defvar gnus-hidden-properties '(invisible t intangible t)
1331   "Property list to use for hiding text.")
1332
1333 (defvar gnus-modtime-botch nil
1334   "*Non-nil means .newsrc should be deleted prior to save.  
1335 Its use is due to the bogus appearance that .newsrc was modified on
1336 disc.")
1337
1338 ;; Hooks.
1339
1340 (defvar gnus-group-mode-hook nil
1341   "*A hook for Gnus group mode.")
1342
1343 (defvar gnus-summary-mode-hook nil
1344   "*A hook for Gnus summary mode.
1345 This hook is run before any variables are set in the summary buffer.")
1346
1347 (defvar gnus-article-mode-hook nil
1348   "*A hook for Gnus article mode.")
1349
1350 (defvar gnus-summary-prepare-exit-hook nil
1351   "*A hook called when preparing to exit from the summary buffer.
1352 It calls `gnus-summary-expire-articles' by default.")
1353 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1354
1355 (defvar gnus-summary-exit-hook nil
1356   "*A hook called on exit from the summary buffer.")
1357
1358 (defvar gnus-group-catchup-group-hook nil
1359   "*A hook run when catching up a group from the group buffer.")
1360
1361 (defvar gnus-open-server-hook nil
1362   "*A hook called just before opening connection to the news server.")
1363
1364 (defvar gnus-load-hook nil
1365   "*A hook run while Gnus is loaded.")
1366
1367 (defvar gnus-startup-hook nil
1368   "*A hook called at startup.
1369 This hook is called after Gnus is connected to the NNTP server.")
1370
1371 (defvar gnus-get-new-news-hook nil
1372   "*A hook run just before Gnus checks for new news.")
1373
1374 (defvar gnus-after-getting-new-news-hook nil
1375   "*A hook run after Gnus checks for new news.")
1376
1377 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1378   "*A function that is called to generate the group buffer.
1379 The function is called with three arguments: The first is a number;
1380 all group with a level less or equal to that number should be listed,
1381 if the second is non-nil, empty groups should also be displayed.  If
1382 the third is non-nil, it is a number.  No groups with a level lower
1383 than this number should be displayed.
1384
1385 The only current function implemented is `gnus-group-prepare-flat'.")
1386
1387 (defvar gnus-group-prepare-hook nil
1388   "*A hook called after the group buffer has been generated.
1389 If you want to modify the group buffer, you can use this hook.")
1390
1391 (defvar gnus-summary-prepare-hook nil
1392   "*A hook called after the summary buffer has been generated.
1393 If you want to modify the summary buffer, you can use this hook.")
1394
1395 (defvar gnus-summary-generate-hook nil
1396   "*A hook run just before generating the summary buffer.
1397 This hook is commonly used to customize threading variables and the
1398 like.")
1399
1400 (defvar gnus-article-prepare-hook nil
1401   "*A hook called after an article has been prepared in the article buffer.
1402 If you want to run a special decoding program like nkf, use this hook.")
1403
1404 ;(defvar gnus-article-display-hook nil
1405 ;  "*A hook called after the article is displayed in the article buffer.
1406 ;The hook is designed to change the contents of the article
1407 ;buffer.  Typical functions that this hook may contain are
1408 ;`gnus-article-hide-headers' (hide selected headers),
1409 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1410 ;`gnus-article-hide-signature' (hide signature) and
1411 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1413 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1414 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1415
1416 (defvar gnus-article-x-face-command
1417   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1418   "String or function to be executed to display an X-Face header.
1419 If it is a string, the command will be executed in a sub-shell
1420 asynchronously.  The compressed face will be piped to this command.")
1421
1422 (defvar gnus-article-x-face-too-ugly nil
1423   "Regexp matching posters whose face shouldn't be shown automatically.")
1424
1425 (defvar gnus-select-group-hook nil
1426   "*A hook called when a newsgroup is selected.
1427
1428 If you'd like to simplify subjects like the
1429 `gnus-summary-next-same-subject' command does, you can use the
1430 following hook:
1431
1432  (setq gnus-select-group-hook
1433       (list
1434         (lambda ()
1435           (mapcar (lambda (header)
1436                      (mail-header-set-subject
1437                       header
1438                       (gnus-simplify-subject
1439                        (mail-header-subject header) 're-only)))
1440                   gnus-newsgroup-headers))))")
1441
1442 (defvar gnus-select-article-hook nil
1443   "*A hook called when an article is selected.")
1444
1445 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1446   "*A hook called to apply kill files to a group.
1447 This hook is intended to apply a kill file to the selected newsgroup.
1448 The function `gnus-apply-kill-file' is called by default.
1449
1450 Since a general kill file is too heavy to use only for a few
1451 newsgroups, I recommend you to use a lighter hook function.  For
1452 example, if you'd like to apply a kill file to articles which contains
1453 a string `rmgroup' in subject in newsgroup `control', you can use the
1454 following hook:
1455
1456  (setq gnus-apply-kill-hook
1457       (list
1458         (lambda ()
1459           (cond ((string-match \"control\" gnus-newsgroup-name)
1460                  (gnus-kill \"Subject\" \"rmgroup\")
1461                  (gnus-expunge \"X\"))))))")
1462
1463 (defvar gnus-visual-mark-article-hook
1464   (list 'gnus-highlight-selected-summary)
1465   "*Hook run after selecting an article in the summary buffer.
1466 It is meant to be used for highlighting the article in some way.  It
1467 is not run if `gnus-visual' is nil.")
1468
1469 (defvar gnus-parse-headers-hook nil
1470   "*A hook called before parsing the headers.")
1471 (add-hook 'gnus-parse-headers-hook 'gnus-headers-decode-quoted-printable)
1472
1473 (defvar gnus-exit-group-hook nil
1474   "*A hook called when exiting (not quitting) summary mode.")
1475
1476 (defvar gnus-suspend-gnus-hook nil
1477   "*A hook called when suspending (not exiting) Gnus.")
1478
1479 (defvar gnus-exit-gnus-hook nil
1480   "*A hook called when exiting Gnus.")
1481
1482 (defvar gnus-after-exiting-gnus-hook nil
1483   "*A hook called after exiting Gnus.")
1484
1485 (defvar gnus-save-newsrc-hook nil
1486   "*A hook called before saving any of the newsrc files.")
1487
1488 (defvar gnus-save-quick-newsrc-hook nil
1489   "*A hook called just before saving the quick newsrc file.
1490 Can be used to turn version control on or off.")
1491
1492 (defvar gnus-save-standard-newsrc-hook nil
1493   "*A hook called just before saving the standard newsrc file.
1494 Can be used to turn version control on or off.")
1495
1496 (defvar gnus-summary-update-hook
1497   (list 'gnus-summary-highlight-line)
1498   "*A hook called when a summary line is changed.
1499 The hook will not be called if `gnus-visual' is nil.
1500
1501 The default function `gnus-summary-highlight-line' will
1502 highlight the line according to the `gnus-summary-highlight'
1503 variable.")
1504
1505 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1506   "*A hook called when a group line is changed.
1507 The hook will not be called if `gnus-visual' is nil.
1508
1509 The default function `gnus-group-highlight-line' will
1510 highlight the line according to the `gnus-group-highlight'
1511 variable.")
1512
1513 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1514   "*A hook called when an article is selected for the first time.
1515 The hook is intended to mark an article as read (or unread)
1516 automatically when it is selected.")
1517
1518 (defvar gnus-group-change-level-function nil
1519   "Function run when a group level is changed.
1520 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1521
1522 ;; Remove any hilit infestation.
1523 (add-hook 'gnus-startup-hook
1524           (lambda ()
1525             (remove-hook 'gnus-summary-prepare-hook
1526                          'hilit-rehighlight-buffer-quietly)
1527             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1528             (setq gnus-mark-article-hook
1529                   '(gnus-summary-mark-read-and-unread-as-read))
1530             (remove-hook 'gnus-article-prepare-hook
1531                          'hilit-rehighlight-buffer-quietly)))
1532
1533 \f
1534 ;; Internal variables
1535
1536 ;; Dummy variable.
1537 (defvar gnus-use-generic-from nil)
1538
1539 (defvar gnus-thread-indent-array nil)
1540 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1541
1542 (defvar gnus-newsrc-file-version nil)
1543
1544 (defvar gnus-method-history nil)
1545 ;; Variable holding the user answers to all method prompts.
1546
1547 (defvar gnus-group-history nil)
1548 ;; Variable holding the user answers to all group prompts.
1549
1550 (defvar gnus-server-alist nil
1551   "List of available servers.")
1552
1553 (defvar gnus-group-indentation-function nil)
1554
1555 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1556
1557 (defvar gnus-goto-missing-group-function nil)
1558
1559 (defvar gnus-override-subscribe-method nil)
1560
1561 (defvar gnus-group-goto-next-group-function nil
1562   "Function to override finding the next group after listing groups.")
1563
1564 (defconst gnus-article-mark-lists
1565   '((marked . tick) (replied . reply)
1566     (expirable . expire) (killed . killed)
1567     (bookmarks . bookmark) (dormant . dormant)
1568     (scored . score) (saved . save)
1569     (cached . cache)
1570     ))
1571
1572 ;; Avoid highlighting in kill files.
1573 (defvar gnus-summary-inhibit-highlight nil)
1574 (defvar gnus-newsgroup-selected-overlay nil)
1575
1576 (defvar gnus-inhibit-hiding nil)
1577 (defvar gnus-group-indentation "")
1578 (defvar gnus-inhibit-limiting nil)
1579 (defvar gnus-created-frames nil)
1580
1581 (defvar gnus-article-mode-map nil)
1582 (defvar gnus-dribble-buffer nil)
1583 (defvar gnus-headers-retrieved-by nil)
1584 (defvar gnus-article-reply nil)
1585 (defvar gnus-override-method nil)
1586 (defvar gnus-article-check-size nil)
1587
1588 (defvar gnus-current-score-file nil)
1589 (defvar gnus-newsgroup-adaptive-score-file nil)
1590 (defvar gnus-scores-exclude-files nil)
1591
1592 (defvar gnus-opened-servers nil)
1593
1594 (defvar gnus-current-move-group nil)
1595
1596 (defvar gnus-newsgroup-dependencies nil)
1597 (defvar gnus-newsgroup-async nil)
1598 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1599
1600 (defvar gnus-newsgroup-adaptive nil)
1601
1602 (defvar gnus-summary-display-table nil)
1603 (defvar gnus-summary-display-article-function nil)
1604
1605 (defvar gnus-summary-highlight-line-function nil
1606   "Function called after highlighting a summary line.")
1607
1608 (defvar gnus-group-line-format-alist
1609   `((?M gnus-tmp-marked-mark ?c)
1610     (?S gnus-tmp-subscribed ?c)
1611     (?L gnus-tmp-level ?d)
1612     (?N (cond ((eq number t) "*" )
1613               ((numberp number) 
1614                (int-to-string
1615                 (+ number
1616                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1617                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1618               (t number)) ?s)
1619     (?R gnus-tmp-number-of-read ?s)
1620     (?t gnus-tmp-number-total ?d)
1621     (?y gnus-tmp-number-of-unread ?s)
1622     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1623     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1624     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1625            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1626     (?g gnus-tmp-group ?s)
1627     (?G gnus-tmp-qualified-group ?s)
1628     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1629     (?D gnus-tmp-newsgroup-description ?s)
1630     (?o gnus-tmp-moderated ?c)
1631     (?O gnus-tmp-moderated-string ?s)
1632     (?p gnus-tmp-process-marked ?c)
1633     (?s gnus-tmp-news-server ?s)
1634     (?n gnus-tmp-news-method ?s)
1635     (?P gnus-group-indentation ?s)
1636     (?l gnus-tmp-grouplens ?s)
1637     (?z gnus-tmp-news-method-string ?s)
1638     (?u gnus-tmp-user-defined ?s)))
1639
1640 (defvar gnus-summary-line-format-alist
1641   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1642     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1643     (?s gnus-tmp-subject-or-nil ?s)
1644     (?n gnus-tmp-name ?s)
1645     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1646         ?s)
1647     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1648             gnus-tmp-from) ?s)
1649     (?F gnus-tmp-from ?s)
1650     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1651     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1652     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1653     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1654     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1655     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1656     (?L gnus-tmp-lines ?d)
1657     (?I gnus-tmp-indentation ?s)
1658     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1659     (?R gnus-tmp-replied ?c)
1660     (?\[ gnus-tmp-opening-bracket ?c)
1661     (?\] gnus-tmp-closing-bracket ?c)
1662     (?\> (make-string gnus-tmp-level ? ) ?s)
1663     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1664     (?i gnus-tmp-score ?d)
1665     (?z gnus-tmp-score-char ?c)
1666     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1667     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1668     (?U gnus-tmp-unread ?c)
1669     (?t (gnus-summary-number-of-articles-in-thread
1670          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1671         ?d)
1672     (?e (gnus-summary-number-of-articles-in-thread
1673          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1674         ?c)
1675     (?u gnus-tmp-user-defined ?s))
1676   "An alist of format specifications that can appear in summary lines,
1677 and what variables they correspond with, along with the type of the
1678 variable (string, integer, character, etc).")
1679
1680 (defvar gnus-summary-dummy-line-format-alist
1681   `((?S gnus-tmp-subject ?s)
1682     (?N gnus-tmp-number ?d)
1683     (?u gnus-tmp-user-defined ?s)))
1684
1685 (defvar gnus-summary-mode-line-format-alist
1686   `((?G gnus-tmp-group-name ?s)
1687     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1688     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1689     (?A gnus-tmp-article-number ?d)
1690     (?Z gnus-tmp-unread-and-unselected ?s)
1691     (?V gnus-version ?s)
1692     (?U gnus-tmp-unread ?d)
1693     (?S gnus-tmp-subject ?s)
1694     (?e gnus-tmp-unselected ?d)
1695     (?u gnus-tmp-user-defined ?s)
1696     (?d (length gnus-newsgroup-dormant) ?d)
1697     (?t (length gnus-newsgroup-marked) ?d)
1698     (?r (length gnus-newsgroup-reads) ?d)
1699     (?E gnus-newsgroup-expunged-tally ?d)
1700     (?s (gnus-current-score-file-nondirectory) ?s)))
1701
1702 (defvar gnus-article-mode-line-format-alist
1703   gnus-summary-mode-line-format-alist)
1704
1705 (defvar gnus-group-mode-line-format-alist
1706   `((?S gnus-tmp-news-server ?s)
1707     (?M gnus-tmp-news-method ?s)
1708     (?u gnus-tmp-user-defined ?s)))
1709
1710 (defvar gnus-have-read-active-file nil)
1711
1712 (defconst gnus-maintainer
1713   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1714   "The mail address of the Gnus maintainers.")
1715
1716 (defconst gnus-version "September Gnus v0.79"
1717   "Version number for this version of Gnus.")
1718
1719 (defvar gnus-info-nodes
1720   '((gnus-group-mode "(gnus)The Group Buffer")
1721     (gnus-summary-mode "(gnus)The Summary Buffer")
1722     (gnus-article-mode "(gnus)The Article Buffer"))
1723   "Assoc list of major modes and related Info nodes.")
1724
1725 (defvar gnus-group-buffer "*Group*")
1726 (defvar gnus-summary-buffer "*Summary*")
1727 (defvar gnus-article-buffer "*Article*")
1728 (defvar gnus-server-buffer "*Server*")
1729
1730 (defvar gnus-work-buffer " *gnus work*")
1731
1732 (defvar gnus-original-article-buffer " *Original Article*")
1733 (defvar gnus-original-article nil)
1734
1735 (defvar gnus-buffer-list nil
1736   "Gnus buffers that should be killed on exit.")
1737
1738 (defvar gnus-slave nil
1739   "Whether this Gnus is a slave or not.")
1740
1741 (defvar gnus-variable-list
1742   '(gnus-newsrc-options gnus-newsrc-options-n
1743     gnus-newsrc-last-checked-date
1744     gnus-newsrc-alist gnus-server-alist
1745     gnus-killed-list gnus-zombie-list
1746     gnus-topic-topology gnus-topic-alist
1747     gnus-format-specs)
1748   "Gnus variables saved in the quick startup file.")
1749
1750 (defvar gnus-newsrc-options nil
1751   "Options line in the .newsrc file.")
1752
1753 (defvar gnus-newsrc-options-n nil
1754   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1755
1756 (defvar gnus-newsrc-last-checked-date nil
1757   "Date Gnus last asked server for new newsgroups.")
1758
1759 (defvar gnus-topic-topology nil
1760   "The complete topic hierarchy.")
1761
1762 (defvar gnus-topic-alist nil
1763   "The complete topic-group alist.")
1764
1765 (defvar gnus-newsrc-alist nil
1766   "Assoc list of read articles.
1767 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1768
1769 (defvar gnus-newsrc-hashtb nil
1770   "Hashtable of gnus-newsrc-alist.")
1771
1772 (defvar gnus-killed-list nil
1773   "List of killed newsgroups.")
1774
1775 (defvar gnus-killed-hashtb nil
1776   "Hash table equivalent of gnus-killed-list.")
1777
1778 (defvar gnus-zombie-list nil
1779   "List of almost dead newsgroups.")
1780
1781 (defvar gnus-description-hashtb nil
1782   "Descriptions of newsgroups.")
1783
1784 (defvar gnus-list-of-killed-groups nil
1785   "List of newsgroups that have recently been killed by the user.")
1786
1787 (defvar gnus-active-hashtb nil
1788   "Hashtable of active articles.")
1789
1790 (defvar gnus-moderated-list nil
1791   "List of moderated newsgroups.")
1792
1793 (defvar gnus-group-marked nil)
1794
1795 (defvar gnus-current-startup-file nil
1796   "Startup file for the current host.")
1797
1798 (defvar gnus-last-search-regexp nil
1799   "Default regexp for article search command.")
1800
1801 (defvar gnus-last-shell-command nil
1802   "Default shell command on article.")
1803
1804 (defvar gnus-current-select-method nil
1805   "The current method for selecting a newsgroup.")
1806
1807 (defvar gnus-group-list-mode nil)
1808
1809 (defvar gnus-article-internal-prepare-hook nil)
1810
1811 (defvar gnus-newsgroup-name nil)
1812 (defvar gnus-newsgroup-begin nil)
1813 (defvar gnus-newsgroup-end nil)
1814 (defvar gnus-newsgroup-last-rmail nil)
1815 (defvar gnus-newsgroup-last-mail nil)
1816 (defvar gnus-newsgroup-last-folder nil)
1817 (defvar gnus-newsgroup-last-file nil)
1818 (defvar gnus-newsgroup-auto-expire nil)
1819 (defvar gnus-newsgroup-active nil)
1820
1821 (defvar gnus-newsgroup-data nil)
1822 (defvar gnus-newsgroup-data-reverse nil)
1823 (defvar gnus-newsgroup-limit nil)
1824 (defvar gnus-newsgroup-limits nil)
1825
1826 (defvar gnus-newsgroup-unreads nil
1827   "List of unread articles in the current newsgroup.")
1828
1829 (defvar gnus-newsgroup-unselected nil
1830   "List of unselected unread articles in the current newsgroup.")
1831
1832 (defvar gnus-newsgroup-reads nil
1833   "Alist of read articles and article marks in the current newsgroup.")
1834
1835 (defvar gnus-newsgroup-expunged-tally nil)
1836
1837 (defvar gnus-newsgroup-marked nil
1838   "List of ticked articles in the current newsgroup (a subset of unread art).")
1839
1840 (defvar gnus-newsgroup-killed nil
1841   "List of ranges of articles that have been through the scoring process.")
1842
1843 (defvar gnus-newsgroup-cached nil
1844   "List of articles that come from the article cache.")
1845
1846 (defvar gnus-newsgroup-saved nil
1847   "List of articles that have been saved.")
1848
1849 (defvar gnus-newsgroup-kill-headers nil)
1850
1851 (defvar gnus-newsgroup-replied nil
1852   "List of articles that have been replied to in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-expirable nil
1855   "List of articles in the current newsgroup that can be expired.")
1856
1857 (defvar gnus-newsgroup-processable nil
1858   "List of articles in the current newsgroup that can be processed.")
1859
1860 (defvar gnus-newsgroup-bookmarks nil
1861   "List of articles in the current newsgroup that have bookmarks.")
1862
1863 (defvar gnus-newsgroup-dormant nil
1864   "List of dormant articles in the current newsgroup.")
1865
1866 (defvar gnus-newsgroup-scored nil
1867   "List of scored articles in the current newsgroup.")
1868
1869 (defvar gnus-newsgroup-headers nil
1870   "List of article headers in the current newsgroup.")
1871
1872 (defvar gnus-newsgroup-threads nil)
1873
1874 (defvar gnus-newsgroup-prepared nil
1875   "Whether the current group has been prepared properly.")
1876
1877 (defvar gnus-newsgroup-ancient nil
1878   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1879
1880 (defvar gnus-newsgroup-sparse nil)
1881
1882 (defvar gnus-current-article nil)
1883 (defvar gnus-article-current nil)
1884 (defvar gnus-current-headers nil)
1885 (defvar gnus-have-all-headers nil)
1886 (defvar gnus-last-article nil)
1887 (defvar gnus-newsgroup-history nil)
1888 (defvar gnus-current-kill-article nil)
1889
1890 ;; Save window configuration.
1891 (defvar gnus-prev-winconf nil)
1892
1893 (defvar gnus-summary-mark-positions nil)
1894 (defvar gnus-group-mark-positions nil)
1895
1896 (defvar gnus-reffed-article-number nil)
1897
1898 ;;; Let the byte-compiler know that we know about this variable.
1899 (defvar rmail-default-rmail-file)
1900
1901 (defvar gnus-cache-removable-articles nil)
1902
1903 (defvar gnus-dead-summary nil)
1904
1905 (defconst gnus-summary-local-variables
1906   '(gnus-newsgroup-name
1907     gnus-newsgroup-begin gnus-newsgroup-end
1908     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1909     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1910     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1911     gnus-newsgroup-unselected gnus-newsgroup-marked
1912     gnus-newsgroup-reads gnus-newsgroup-saved
1913     gnus-newsgroup-replied gnus-newsgroup-expirable
1914     gnus-newsgroup-processable gnus-newsgroup-killed
1915     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1916     gnus-newsgroup-headers gnus-newsgroup-threads
1917     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1918     gnus-current-article gnus-current-headers gnus-have-all-headers
1919     gnus-last-article gnus-article-internal-prepare-hook
1920     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1921     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1922     gnus-newsgroup-async gnus-thread-expunge-below
1923     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1924     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1925     gnus-newsgroup-history gnus-newsgroup-ancient
1926     gnus-newsgroup-sparse
1927     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1928     gnus-newsgroup-adaptive-score-file
1929     (gnus-newsgroup-expunged-tally . 0)
1930     gnus-cache-removable-articles gnus-newsgroup-cached
1931     gnus-newsgroup-data gnus-newsgroup-data-reverse
1932     gnus-newsgroup-limit gnus-newsgroup-limits)
1933   "Variables that are buffer-local to the summary buffers.")
1934
1935 (defconst gnus-bug-message
1936   "Sending a bug report to the Gnus Towers.
1937 ========================================
1938
1939 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1940 be sent to the Gnus Bug Exterminators.
1941
1942 At the bottom of the buffer you'll see lots of variable settings.
1943 Please do not delete those.  They will tell the Bug People what your
1944 environment is, so that it will be easier to locate the bugs.
1945
1946 If you have found a bug that makes Emacs go \"beep\", set
1947 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1948 and include the backtrace in your bug report.
1949
1950 Please describe the bug in annoying, painstaking detail.
1951
1952 Thank you for your help in stamping out bugs.
1953 ")
1954
1955 ;;; End of variables.
1956
1957 ;; Define some autoload functions Gnus might use.
1958 (eval-and-compile
1959
1960   ;; This little mapcar goes through the list below and marks the
1961   ;; symbols in question as autoloaded functions.
1962   (mapcar
1963    (lambda (package)
1964      (let ((interactive (nth 1 (memq ':interactive package))))
1965        (mapcar
1966         (lambda (function)
1967           (let (keymap)
1968             (when (consp function)
1969               (setq keymap (car (memq 'keymap function)))
1970               (setq function (car function)))
1971             (autoload function (car package) nil interactive keymap)))
1972         (if (eq (nth 1 package) ':interactive)
1973             (cdddr package)
1974           (cdr package)))))
1975    '(("metamail" metamail-buffer)
1976      ("info" Info-goto-node)
1977      ("hexl" hexl-hex-string-to-integer)
1978      ("pp" pp pp-to-string pp-eval-expression)
1979      ("mail-extr" mail-extract-address-components)
1980      ("nnmail" nnmail-split-fancy nnmail-article-group)
1981      ("nnvirtual" nnvirtual-catchup-group)
1982      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1983       timezone-make-sortable-date timezone-make-time-string)
1984      ("sendmail" mail-position-on-field mail-setup)
1985      ("rmailout" rmail-output)
1986      ("rnewspost" news-mail-other-window news-reply-yank-original
1987       news-caesar-buffer-body)
1988      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1989       rmail-show-message)
1990      ("gnus-soup" :interactive t
1991       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1992       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1993      ("nnsoup" nnsoup-pack-replies)
1994      ("gnus-scomo" :interactive t gnus-score-mode)
1995      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1996       gnus-Folder-save-name gnus-folder-save-name)
1997      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1998      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1999       gnus-server-make-menu-bar gnus-article-make-menu-bar
2000       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2001       gnus-summary-highlight-line gnus-carpal-setup-buffer
2002       gnus-group-highlight-line
2003       gnus-article-add-button gnus-insert-next-page-button
2004       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2005      ("gnus-vis" :interactive t
2006       gnus-article-push-button gnus-article-press-button
2007       gnus-article-highlight gnus-article-highlight-some
2008       gnus-article-highlight-headers gnus-article-highlight-signature
2009       gnus-article-add-buttons gnus-article-add-buttons-to-head
2010       gnus-article-next-button gnus-article-prev-button)
2011      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2012       gnus-demon-add-disconnection gnus-demon-add-handler
2013       gnus-demon-remove-handler)
2014      ("gnus-demon" :interactive t
2015       gnus-demon-init gnus-demon-cancel)
2016      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2017       gnus-tree-open gnus-tree-close)
2018      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2019       gnus-nocem-unwanted-article-p)
2020      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2021      ("gnus-srvr" gnus-browse-foreign-server)
2022      ("gnus-cite" :interactive t
2023       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2024       gnus-article-hide-citation gnus-article-fill-cited-article)
2025      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2026       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2027       gnus-execute gnus-expunge)
2028      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2029       gnus-cache-possibly-remove-articles gnus-cache-request-article
2030       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2031       gnus-cache-enter-remove-article gnus-cached-article-p
2032       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2033      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2034       gnus-cache-remove-article)
2035      ("gnus-score" :interactive t
2036       gnus-summary-increase-score gnus-summary-lower-score
2037       gnus-score-flush-cache gnus-score-close
2038       gnus-score-raise-same-subject-and-select
2039       gnus-score-raise-same-subject gnus-score-default
2040       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2041       gnus-score-lower-same-subject gnus-score-lower-thread
2042       gnus-possibly-score-headers)
2043      ("gnus-score"
2044       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2045       gnus-current-score-file-nondirectory gnus-score-adaptive
2046       gnus-score-find-trace gnus-score-file-name)
2047      ("gnus-edit" :interactive t gnus-score-customize)
2048      ("gnus-topic" :interactive t gnus-topic-mode)
2049      ("gnus-topic" gnus-topic-remove-group)
2050      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2051      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2052      ("gnus-uu" :interactive t
2053       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2054       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2055       gnus-uu-mark-by-regexp gnus-uu-mark-all
2056       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2057       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2058       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2059       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2060       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2061       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2062       gnus-uu-decode-binhex-view)
2063      ("gnus-msg" (gnus-summary-send-map keymap)
2064       gnus-mail-yank-original gnus-mail-send-and-exit
2065       gnus-sendmail-setup-mail gnus-article-mail
2066       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2067      ("gnus-msg" :interactive t
2068       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2069       gnus-summary-followup gnus-summary-followup-with-original
2070       gnus-summary-followup-and-reply
2071       gnus-summary-followup-and-reply-with-original
2072       gnus-summary-cancel-article gnus-summary-supersede-article
2073       gnus-post-news gnus-inews-news gnus-cancel-news
2074       gnus-summary-reply gnus-summary-reply-with-original
2075       gnus-summary-mail-forward gnus-summary-mail-other-window
2076       gnus-bug)
2077      ("gnus-picon" :interactive t gnus-article-display-picons
2078       gnus-group-display-picons gnus-picons-article-display-x-face)
2079      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2080       gnus-grouplens-mode)
2081      ("gnus-vm" gnus-vm-mail-setup)
2082      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2083       gnus-summary-save-article-vm gnus-yank-article))))
2084
2085 \f
2086
2087 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2088 ;; If you want the cursor to go somewhere else, set these two
2089 ;; functions in some startup hook to whatever you want.
2090 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2091 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2092
2093 ;;; Various macros and substs.
2094
2095 (defun gnus-header-from (header)
2096   (mail-header-from header))
2097
2098 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2099   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2100   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2101     `(let ((,tempvar (selected-window)))
2102        (unwind-protect
2103            (progn
2104              (pop-to-buffer ,buffer)
2105              ,@forms)
2106          (select-window ,tempvar)))))
2107
2108 (defmacro gnus-gethash (string hashtable)
2109   "Get hash value of STRING in HASHTABLE."
2110   `(symbol-value (intern-soft ,string ,hashtable)))
2111
2112 (defmacro gnus-sethash (string value hashtable)
2113   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2114   `(set (intern ,string ,hashtable) ,value))
2115
2116 (defmacro gnus-intern-safe (string hashtable)
2117   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2118   `(let ((symbol (intern ,string ,hashtable)))
2119      (or (boundp symbol)
2120          (set symbol nil))
2121      symbol))
2122
2123 (defmacro gnus-group-unread (group)
2124   "Get the currently computed number of unread articles in GROUP."
2125   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2126
2127 (defmacro gnus-group-entry (group)
2128   "Get the newsrc entry for GROUP."
2129   `(gnus-gethash ,group gnus-newsrc-hashtb))
2130
2131 (defmacro gnus-active (group)
2132   "Get active info on GROUP."
2133   `(gnus-gethash ,group gnus-active-hashtb))
2134
2135 (defmacro gnus-set-active (group active)
2136   "Set GROUP's active info."
2137   `(gnus-sethash ,group ,active gnus-active-hashtb))
2138
2139 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2140 ;;   function `substring' might cut on a middle of multi-octet
2141 ;;   character.
2142 (defun gnus-truncate-string (str width)
2143   (substring str 0 width))
2144
2145 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2146 ;; to limit the length of a string.  This function is necessary since
2147 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2148 (defsubst gnus-limit-string (str width)
2149   (if (> (length str) width)
2150       (substring str 0 width)
2151     str))
2152
2153 (defsubst gnus-simplify-subject-re (subject)
2154   "Remove \"Re:\" from subject lines."
2155   (if (string-match "^[Rr][Ee]: *" subject)
2156       (substring subject (match-end 0))
2157     subject))
2158
2159 (defsubst gnus-functionp (form)
2160   "Return non-nil if FORM is funcallable."
2161   (or (and (symbolp form) (fboundp form))
2162       (and (listp form) (eq (car form) 'lambda))))
2163
2164 (defsubst gnus-goto-char (point)
2165   (and point (goto-char point)))
2166
2167 (defmacro gnus-buffer-exists-p (buffer)
2168   `(let ((buffer ,buffer))
2169      (and buffer
2170           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2171                    buffer))))
2172
2173 (defmacro gnus-kill-buffer (buffer)
2174   `(let ((buf ,buffer))
2175      (if (gnus-buffer-exists-p buf)
2176          (kill-buffer buf))))
2177
2178 (defsubst gnus-point-at-bol ()
2179   "Return point at the beginning of the line."
2180   (let ((p (point)))
2181     (beginning-of-line)
2182     (prog1
2183         (point)
2184       (goto-char p))))
2185
2186 (defsubst gnus-point-at-eol ()
2187   "Return point at the end of the line."
2188   (let ((p (point)))
2189     (end-of-line)
2190     (prog1
2191         (point)
2192       (goto-char p))))
2193
2194 (defun gnus-alive-p ()
2195   "Say whether Gnus is running or not."
2196   (and gnus-group-buffer
2197        (get-buffer gnus-group-buffer)))
2198
2199 ;; Delete the current line (and the next N lines.);
2200 (defmacro gnus-delete-line (&optional n)
2201   `(delete-region (progn (beginning-of-line) (point))
2202                   (progn (forward-line ,(or n 1)) (point))))
2203
2204 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2205 (defvar gnus-init-inhibit nil)
2206 (defun gnus-read-init-file (&optional inhibit-next)
2207   (if gnus-init-inhibit
2208       (setq gnus-init-inhibit nil)
2209     (setq gnus-init-inhibit inhibit-next)
2210     (and gnus-init-file
2211          (or (and (file-exists-p gnus-init-file)
2212                   ;; Don't try to load a directory.
2213                   (not (file-directory-p gnus-init-file)))
2214              (file-exists-p (concat gnus-init-file ".el"))
2215              (file-exists-p (concat gnus-init-file ".elc")))
2216          (condition-case var
2217              (load gnus-init-file nil t)
2218            (error
2219             (error "Error in %s: %s" gnus-init-file var))))))
2220
2221 ;; Info access macros.
2222
2223 (defmacro gnus-info-group (info)
2224   `(nth 0 ,info))
2225 (defmacro gnus-info-rank (info)
2226   `(nth 1 ,info))
2227 (defmacro gnus-info-read (info)
2228   `(nth 2 ,info))
2229 (defmacro gnus-info-marks (info)
2230   `(nth 3 ,info))
2231 (defmacro gnus-info-method (info)
2232   `(nth 4 ,info))
2233 (defmacro gnus-info-params (info)
2234   `(nth 5 ,info))
2235
2236 (defmacro gnus-info-level (info)
2237   `(let ((rank (gnus-info-rank ,info)))
2238      (if (consp rank)
2239          (car rank)
2240        rank)))
2241 (defmacro gnus-info-score (info)
2242   `(let ((rank (gnus-info-rank ,info)))
2243      (or (and (consp rank) (cdr rank)) 0)))
2244
2245 (defmacro gnus-info-set-group (info group)
2246   `(setcar ,info ,group))
2247 (defmacro gnus-info-set-rank (info rank)
2248   `(setcar (nthcdr 1 ,info) ,rank))
2249 (defmacro gnus-info-set-read (info read)
2250   `(setcar (nthcdr 2 ,info) ,read))
2251 (defmacro gnus-info-set-marks (info marks)
2252   `(setcar (nthcdr 3 ,info) ,marks))
2253 (defmacro gnus-info-set-method (info method)
2254   `(setcar (nthcdr 4 ,info) ,method))
2255 (defmacro gnus-info-set-params (info params)
2256   `(setcar (nthcdr 5 ,info) ,params))
2257
2258 (defmacro gnus-info-set-level (info level)
2259   `(let ((rank (cdr ,info)))
2260      (if (consp (car rank))
2261          (setcar (car rank) ,level)
2262        (setcar rank ,level))))
2263 (defmacro gnus-info-set-score (info score)
2264   `(let ((rank (cdr ,info)))
2265      (if (consp (car rank))
2266          (setcdr (car rank) ,score)
2267        (setcar rank (cons (car rank) ,score)))))
2268
2269 (defmacro gnus-get-info (group)
2270   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2271
2272 (defun gnus-byte-code (func)
2273   "Return a form that can be `eval'ed based on FUNC."
2274   (let ((fval (symbol-function func)))
2275     (if (byte-code-function-p fval)
2276         (let ((flist (append fval nil)))
2277           (setcar flist 'byte-code)
2278           flist)
2279       (cons 'progn (cddr fval)))))
2280
2281 ;;; Load the compatability functions.
2282
2283 (require 'gnus-cus)
2284 (require 'gnus-ems)
2285
2286 \f
2287 ;;;
2288 ;;; Shutdown
2289 ;;;
2290
2291 (defvar gnus-shutdown-alist nil)
2292
2293 (defun gnus-add-shutdown (function &rest symbols)
2294   "Run FUNCTION whenever one of SYMBOLS is shut down."
2295   (push (cons function symbols) gnus-shutdown-alist))
2296
2297 (defun gnus-shutdown (symbol)
2298   "Shut down everything that waits for SYMBOL."
2299   (let ((alist gnus-shutdown-alist)
2300         entry)
2301     (while (setq entry (pop alist))
2302       (when (memq symbol (cdr entry))
2303         (funcall (car entry))))))
2304
2305 \f
2306
2307 ;; Format specs.  The chunks below are the machine-generated forms
2308 ;; that are to be evaled as the result of the default format strings.
2309 ;; We write them in here to get them byte-compiled.  That way the
2310 ;; default actions will be quite fast, while still retaining the full
2311 ;; flexibility of the user-defined format specs.
2312
2313 ;; First we have lots of dummy defvars to let the compiler know these
2314 ;; are really dynamic variables.
2315
2316 (defvar gnus-tmp-unread)
2317 (defvar gnus-tmp-replied)
2318 (defvar gnus-tmp-score-char)
2319 (defvar gnus-tmp-indentation)
2320 (defvar gnus-tmp-opening-bracket)
2321 (defvar gnus-tmp-lines)
2322 (defvar gnus-tmp-name)
2323 (defvar gnus-tmp-closing-bracket)
2324 (defvar gnus-tmp-subject-or-nil)
2325 (defvar gnus-tmp-subject)
2326 (defvar gnus-tmp-marked)
2327 (defvar gnus-tmp-marked-mark)
2328 (defvar gnus-tmp-subscribed)
2329 (defvar gnus-tmp-process-marked)
2330 (defvar gnus-tmp-number-of-unread)
2331 (defvar gnus-tmp-group-name)
2332 (defvar gnus-tmp-group)
2333 (defvar gnus-tmp-article-number)
2334 (defvar gnus-tmp-unread-and-unselected)
2335 (defvar gnus-tmp-news-method)
2336 (defvar gnus-tmp-news-server)
2337 (defvar gnus-tmp-article-number)
2338 (defvar gnus-mouse-face)
2339 (defvar gnus-mouse-face-prop)
2340
2341 (defun gnus-summary-line-format-spec ()
2342   (insert gnus-tmp-unread gnus-tmp-replied
2343           gnus-tmp-score-char gnus-tmp-indentation)
2344   (put-text-property
2345    (point)
2346    (progn
2347      (insert
2348       gnus-tmp-opening-bracket
2349       (format "%4d: %-20s"
2350               gnus-tmp-lines
2351               (if (> (length gnus-tmp-name) 20)
2352                   (substring gnus-tmp-name 0 20)
2353                 gnus-tmp-name))
2354       gnus-tmp-closing-bracket)
2355      (point))
2356    gnus-mouse-face-prop gnus-mouse-face)
2357   (insert " " gnus-tmp-subject-or-nil "\n"))
2358
2359 (defvar gnus-summary-line-format-spec
2360   (gnus-byte-code 'gnus-summary-line-format-spec))
2361
2362 (defun gnus-summary-dummy-line-format-spec ()
2363   (insert "*  ")
2364   (put-text-property
2365    (point)
2366    (progn
2367      (insert ":                          :")
2368      (point))
2369    gnus-mouse-face-prop gnus-mouse-face)
2370   (insert " " gnus-tmp-subject "\n"))
2371
2372 (defvar gnus-summary-dummy-line-format-spec
2373   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2374
2375 (defun gnus-group-line-format-spec ()
2376   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2377           gnus-tmp-process-marked
2378           gnus-group-indentation
2379           (format "%5s: " gnus-tmp-number-of-unread))
2380   (put-text-property
2381    (point)
2382    (progn
2383      (insert gnus-tmp-group "\n")
2384      (1- (point)))
2385    gnus-mouse-face-prop gnus-mouse-face))
2386 (defvar gnus-group-line-format-spec
2387   (gnus-byte-code 'gnus-group-line-format-spec))
2388
2389 (defvar gnus-format-specs
2390   `((version . ,emacs-version)
2391     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2392     (summary-dummy ,gnus-summary-dummy-line-format
2393                    ,gnus-summary-dummy-line-format-spec)
2394     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2395
2396 (defvar gnus-article-mode-line-format-spec nil)
2397 (defvar gnus-summary-mode-line-format-spec nil)
2398 (defvar gnus-group-mode-line-format-spec nil)
2399
2400 ;;; Phew.  All that gruft is over, fortunately.
2401
2402 \f
2403 ;;;
2404 ;;; Gnus Utility Functions
2405 ;;;
2406
2407 (defun gnus-extract-address-components (from)
2408   (let (name address)
2409     ;; First find the address - the thing with the @ in it.  This may
2410     ;; not be accurate in mail addresses, but does the trick most of
2411     ;; the time in news messages.
2412     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2413         (setq address (substring from (match-beginning 0) (match-end 0))))
2414     ;; Then we check whether the "name <address>" format is used.
2415     (and address
2416          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2417          ;; Linear white space is not required.
2418          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2419          (and (setq name (substring from 0 (match-beginning 0)))
2420               ;; Strip any quotes from the name.
2421               (string-match "\".*\"" name)
2422               (setq name (substring name 1 (1- (match-end 0))))))
2423     ;; If not, then "address (name)" is used.
2424     (or name
2425         (and (string-match "(.+)" from)
2426              (setq name (substring from (1+ (match-beginning 0))
2427                                    (1- (match-end 0)))))
2428         (and (string-match "()" from)
2429              (setq name address))
2430         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2431         ;; XOVER might not support folded From headers.
2432         (and (string-match "(.*" from)
2433              (setq name (substring from (1+ (match-beginning 0))
2434                                    (match-end 0)))))
2435     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2436     (list (or name from) (or address from))))
2437
2438 (defun gnus-fetch-field (field)
2439   "Return the value of the header FIELD of current article."
2440   (save-excursion
2441     (save-restriction
2442       (let ((case-fold-search t)
2443             (inhibit-point-motion-hooks t))
2444         (nnheader-narrow-to-headers)
2445         (mail-fetch-field field)))))
2446
2447 (defun gnus-goto-colon ()
2448   (beginning-of-line)
2449   (search-forward ":" (gnus-point-at-eol) t))
2450
2451 ;;;###autoload
2452 (defun gnus-update-format (var)
2453   "Update the format specification near point."
2454   (interactive
2455    (list
2456     (save-excursion
2457       (eval-defun nil)
2458       ;; Find the end of the current word.
2459       (re-search-forward "[ \t\n]" nil t)
2460       ;; Search backward.
2461       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2462         (match-string 1)))))
2463   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2464                               (match-string 1 var))))
2465          (entry (assq type gnus-format-specs))
2466          value spec)
2467     (when entry
2468       (setq gnus-format-specs (delq entry gnus-format-specs)))
2469     (set
2470      (intern (format "%s-spec" var))
2471      (gnus-parse-format (setq value (symbol-value (intern var)))
2472                         (symbol-value (intern (format "%s-alist" var)))
2473                         (not (string-match "mode" var))))
2474     (setq spec (symbol-value (intern (format "%s-spec" var))))
2475     (push (list type value spec) gnus-format-specs)
2476
2477     (pop-to-buffer "*Gnus Format*")
2478     (erase-buffer)
2479     (lisp-interaction-mode)
2480     (insert (pp-to-string spec))))
2481
2482 (defun gnus-update-format-specifications (&optional force)
2483   "Update all (necessary) format specifications."
2484   ;; Make the indentation array.
2485   (gnus-make-thread-indent-array)
2486
2487   ;; See whether all the stored info needs to be flushed.
2488   (when (or force
2489             (not (equal emacs-version
2490                         (cdr (assq 'version gnus-format-specs)))))
2491     (setq gnus-format-specs nil))
2492
2493   ;; Go through all the formats and see whether they need updating.
2494   (let ((types '(summary summary-dummy group
2495                          summary-mode group-mode article-mode))
2496         new-format entry type val)
2497     (while (setq type (pop types))
2498       ;; Jump to the proper buffer to find out the value of
2499       ;; the variable, if possible.  (It may be buffer-local.)
2500       (save-excursion
2501         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2502               val)
2503           (when (and (boundp buffer)
2504                      (setq val (symbol-value buffer))
2505                      (get-buffer val)
2506                      (buffer-name (get-buffer val)))
2507             (set-buffer (get-buffer val)))
2508           (setq new-format (symbol-value
2509                             (intern (format "gnus-%s-line-format" type))))))
2510       (setq entry (cdr (assq type gnus-format-specs)))
2511       (if (and entry
2512                (equal (car entry) new-format))
2513           ;; Use the old format.
2514           (set (intern (format "gnus-%s-line-format-spec" type))
2515                (cadr entry))
2516         ;; This is a new format.
2517         (setq val
2518               (if (not (stringp new-format))
2519                   ;; This is a function call or something.
2520                   new-format
2521                 ;; This is a "real" format.
2522                 (gnus-parse-format
2523                  new-format
2524                  (symbol-value
2525                   (intern (format "gnus-%s-line-format-alist"
2526                                   (if (eq type 'article-mode)
2527                                       'summary-mode type))))
2528                  (not (string-match "mode$" (symbol-name type))))))
2529         ;; Enter the new format spec into the list.
2530         (if entry
2531             (progn
2532               (setcar (cdr entry) val)
2533               (setcar entry new-format))
2534           (push (list type new-format val) gnus-format-specs))
2535         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2536
2537   (unless (assq 'version gnus-format-specs)
2538     (push (cons 'version emacs-version) gnus-format-specs))
2539
2540   (gnus-update-group-mark-positions)
2541   (gnus-update-summary-mark-positions))
2542
2543 (defun gnus-update-summary-mark-positions ()
2544   "Compute where the summary marks are to go."
2545   (save-excursion
2546     (when (and gnus-summary-buffer
2547                (get-buffer gnus-summary-buffer)
2548                (buffer-name (get-buffer gnus-summary-buffer)))
2549       (set-buffer gnus-summary-buffer))
2550     (let ((gnus-replied-mark 129)
2551           (gnus-score-below-mark 130)
2552           (gnus-score-over-mark 130)
2553           (thread nil)
2554           (gnus-visual nil)
2555           (spec gnus-summary-line-format-spec)
2556           pos)
2557       (gnus-set-work-buffer)
2558       (let ((gnus-summary-line-format-spec spec))
2559         (gnus-summary-insert-line
2560          [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2561         (goto-char (point-min))
2562         (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2563                                            (- (point) 2)))))
2564         (goto-char (point-min))
2565         (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2566               pos)
2567         (goto-char (point-min))
2568         (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2569               pos)
2570         (setq gnus-summary-mark-positions pos)))))
2571
2572 (defun gnus-update-group-mark-positions ()
2573   (save-excursion
2574     (let ((gnus-process-mark 128)
2575           (gnus-group-marked '("dummy.group")))
2576       (gnus-set-active "dummy.group" '(0 . 0))
2577       (gnus-set-work-buffer)
2578       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2579       (goto-char (point-min))
2580       (setq gnus-group-mark-positions
2581             (list (cons 'process (and (search-forward "\200" nil t)
2582                                       (- (point) 2))))))))
2583
2584 (defvar gnus-mouse-face-0 'highlight)
2585 (defvar gnus-mouse-face-1 'highlight)
2586 (defvar gnus-mouse-face-2 'highlight)
2587 (defvar gnus-mouse-face-3 'highlight)
2588 (defvar gnus-mouse-face-4 'highlight)
2589
2590 (defun gnus-mouse-face-function (form type)
2591   `(put-text-property
2592     (point) (progn ,@form (point))
2593     gnus-mouse-face-prop
2594     ,(if (equal type 0)
2595          'gnus-mouse-face
2596        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2597
2598 (defvar gnus-face-0 'bold)
2599 (defvar gnus-face-1 'italic)
2600 (defvar gnus-face-2 'bold-italic)
2601 (defvar gnus-face-3 'bold)
2602 (defvar gnus-face-4 'bold)
2603
2604 (defun gnus-face-face-function (form type)
2605   `(put-text-property
2606     (point) (progn ,@form (point))
2607     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2608
2609 (defun gnus-max-width-function (el max-width)
2610   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2611   (if (symbolp el)
2612       `(if (> (length ,el) ,max-width)
2613            (substring ,el 0 ,max-width)
2614          ,el)
2615     `(let ((val (eval ,el)))
2616        (if (numberp val)
2617            (setq val (int-to-string val)))
2618        (if (> (length val) ,max-width)
2619            (substring val 0 ,max-width)
2620          val))))
2621
2622 (defun gnus-parse-format (format spec-alist &optional insert)
2623   ;; This function parses the FORMAT string with the help of the
2624   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2625   ;; string.  If the FORMAT string contains the specifiers %( and %)
2626   ;; the text between them will have the mouse-face text property.
2627   (if (string-match
2628        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2629        format)
2630       (gnus-parse-complex-format format spec-alist)
2631     ;; This is a simple format.
2632     (gnus-parse-simple-format format spec-alist insert)))
2633
2634 (defun gnus-parse-complex-format (format spec-alist)
2635   (save-excursion
2636     (gnus-set-work-buffer)
2637     (insert format)
2638     (goto-char (point-min))
2639     (while (re-search-forward "\"" nil t)
2640       (replace-match "\\\"" nil t))
2641     (goto-char (point-min))
2642     (insert "(\"")
2643     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2644       (let ((number (if (match-beginning 1)
2645                         (match-string 1) "0"))
2646             (delim (aref (match-string 2) 0)))
2647         (if (or (= delim ?\() (= delim ?\{))
2648             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2649                                    " " number " \""))
2650           (replace-match "\")\""))))
2651     (goto-char (point-max))
2652     (insert "\")")
2653     (goto-char (point-min))
2654     (let ((form (read (current-buffer))))
2655       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2656
2657 (defun gnus-complex-form-to-spec (form spec-alist)
2658   (delq nil
2659         (mapcar
2660          (lambda (sform)
2661            (if (stringp sform)
2662                (gnus-parse-simple-format sform spec-alist t)
2663              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2664                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2665                       (nth 1 sform))))
2666          form)))
2667
2668 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2669   ;; This function parses the FORMAT string with the help of the
2670   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2671   ;; string.
2672   (let ((max-width 0)
2673         spec flist fstring newspec elem beg result dontinsert)
2674     (save-excursion
2675       (gnus-set-work-buffer)
2676       (insert format)
2677       (goto-char (point-min))
2678       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2679                                 nil t)
2680         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2681               (setq newspec "%"
2682                     beg (1+ (match-beginning 0)))
2683           ;; First check if there are any specs that look anything like
2684           ;; "%12,12A", ie. with a "max width specification".  These have
2685           ;; to be treated specially.
2686           (if (setq beg (match-beginning 1))
2687               (setq max-width
2688                     (string-to-int
2689                      (buffer-substring
2690                       (1+ (match-beginning 1)) (match-end 1))))
2691             (setq max-width 0)
2692             (setq beg (match-beginning 2)))
2693           ;; Find the specification from `spec-alist'.
2694           (unless (setq elem (cdr (assq spec spec-alist)))
2695             (setq elem '("*" ?s)))
2696           ;; Treat user defined format specifiers specially.
2697           (when (eq (car elem) 'gnus-tmp-user-defined)
2698             (setq elem
2699                   (list
2700                    (list (intern (concat "gnus-user-format-function-"
2701                                          (match-string 3)))
2702                          'gnus-tmp-header) ?s))
2703             (delete-region (match-beginning 3) (match-end 3)))
2704           (if (not (zerop max-width))
2705               (let ((el (car elem)))
2706                 (cond ((= (cadr elem) ?c)
2707                        (setq el (list 'char-to-string el)))
2708                       ((= (cadr elem) ?d)
2709                        (setq el (list 'int-to-string el))))
2710                 (setq flist (cons (gnus-max-width-function el max-width)
2711                                   flist))
2712                 (setq newspec ?s))
2713             (progn
2714               (setq flist (cons (car elem) flist))
2715               (setq newspec (cadr elem)))))
2716         ;; Remove the old specification (and possibly a ",12" string).
2717         (delete-region beg (match-end 2))
2718         ;; Insert the new specification.
2719         (goto-char beg)
2720         (insert newspec))
2721       (setq fstring (buffer-substring 1 (point-max))))
2722     ;; Do some postprocessing to increase efficiency.
2723     (setq
2724      result
2725      (cond
2726       ;; Emptyness.
2727       ((string= fstring "")
2728        nil)
2729       ;; Not a format string.
2730       ((not (string-match "%" fstring))
2731        (list fstring))
2732       ;; A format string with just a single string spec.
2733       ((string= fstring "%s")
2734        (list (car flist)))
2735       ;; A single character.
2736       ((string= fstring "%c")
2737        (list (car flist)))
2738       ;; A single number.
2739       ((string= fstring "%d")
2740        (setq dontinsert)
2741        (if insert
2742            (list `(princ ,(car flist)))
2743          (list `(int-to-string ,(car flist)))))
2744       ;; Just lots of chars and strings.
2745       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2746        (nreverse flist))
2747       ;; A single string spec at the beginning of the spec.
2748       ((string-match "\\`%[sc][^%]+\\'" fstring)
2749        (list (car flist) (substring fstring 2)))
2750       ;; A single string spec in the middle of the spec.
2751       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2752        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2753       ;; A single string spec in the end of the spec.
2754       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2755        (list (match-string 1 fstring) (car flist)))
2756       ;; A more complex spec.
2757       (t
2758        (list (cons 'format (cons fstring (nreverse flist)))))))
2759
2760     (if insert
2761         (when result
2762           (if dontinsert
2763               result
2764             (cons 'insert result)))
2765       (cond ((stringp result)
2766              result)
2767             ((consp result)
2768              (cons 'concat result))
2769             (t "")))))
2770
2771 (defun gnus-eval-format (format &optional alist props)
2772   "Eval the format variable FORMAT, using ALIST.
2773 If PROPS, insert the result."
2774   (let ((form (gnus-parse-format format alist props)))
2775     (if props
2776         (add-text-properties (point) (progn (eval form) (point)) props)
2777       (eval form))))
2778
2779 (defun gnus-remove-text-with-property (prop)
2780   "Delete all text in the current buffer with text property PROP."
2781   (save-excursion
2782     (goto-char (point-min))
2783     (while (not (eobp))
2784       (while (get-text-property (point) prop)
2785         (delete-char 1))
2786       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2787
2788 (defun gnus-set-work-buffer ()
2789   (if (get-buffer gnus-work-buffer)
2790       (progn
2791         (set-buffer gnus-work-buffer)
2792         (erase-buffer))
2793     (set-buffer (get-buffer-create gnus-work-buffer))
2794     (kill-all-local-variables)
2795     (buffer-disable-undo (current-buffer))
2796     (gnus-add-current-to-buffer-list)))
2797
2798 ;; Article file names when saving.
2799
2800 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2801   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2802 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2803 Otherwise, it is like ~/News/news/group/num."
2804   (let ((default
2805           (expand-file-name
2806            (concat (if (gnus-use-long-file-name 'not-save)
2807                        (gnus-capitalize-newsgroup newsgroup)
2808                      (gnus-newsgroup-directory-form newsgroup))
2809                    "/" (int-to-string (mail-header-number headers)))
2810            (or gnus-article-save-directory "~/News"))))
2811     (if (and last-file
2812              (string-equal (file-name-directory default)
2813                            (file-name-directory last-file))
2814              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2815         default
2816       (or last-file default))))
2817
2818 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2819   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2820 If variable `gnus-use-long-file-name' is non-nil, it is
2821 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2822   (let ((default
2823           (expand-file-name
2824            (concat (if (gnus-use-long-file-name 'not-save)
2825                        newsgroup
2826                      (gnus-newsgroup-directory-form newsgroup))
2827                    "/" (int-to-string (mail-header-number headers)))
2828            (or gnus-article-save-directory "~/News"))))
2829     (if (and last-file
2830              (string-equal (file-name-directory default)
2831                            (file-name-directory last-file))
2832              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2833         default
2834       (or last-file default))))
2835
2836 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2837   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2838 If variable `gnus-use-long-file-name' is non-nil, it is
2839 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2840   (or last-file
2841       (expand-file-name
2842        (if (gnus-use-long-file-name 'not-save)
2843            (gnus-capitalize-newsgroup newsgroup)
2844          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2845        (or gnus-article-save-directory "~/News"))))
2846
2847 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2848   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2849 If variable `gnus-use-long-file-name' is non-nil, it is
2850 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2851   (or last-file
2852       (expand-file-name
2853        (if (gnus-use-long-file-name 'not-save)
2854            newsgroup
2855          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2856        (or gnus-article-save-directory "~/News"))))
2857
2858 ;; For subscribing new newsgroup
2859
2860 (defun gnus-subscribe-hierarchical-interactive (groups)
2861   (let ((groups (sort groups 'string<))
2862         prefixes prefix start ans group starts)
2863     (while groups
2864       (setq prefixes (list "^"))
2865       (while (and groups prefixes)
2866         (while (not (string-match (car prefixes) (car groups)))
2867           (setq prefixes (cdr prefixes)))
2868         (setq prefix (car prefixes))
2869         (setq start (1- (length prefix)))
2870         (if (and (string-match "[^\\.]\\." (car groups) start)
2871                  (cdr groups)
2872                  (setq prefix
2873                        (concat "^" (substring (car groups) 0 (match-end 0))))
2874                  (string-match prefix (cadr groups)))
2875             (progn
2876               (setq prefixes (cons prefix prefixes))
2877               (message "Descend hierarchy %s? ([y]nsq): "
2878                        (substring prefix 1 (1- (length prefix))))
2879               (setq ans (read-char))
2880               (cond ((= ans ?n)
2881                      (while (and groups
2882                                  (string-match prefix
2883                                                (setq group (car groups))))
2884                        (setq gnus-killed-list
2885                              (cons group gnus-killed-list))
2886                        (gnus-sethash group group gnus-killed-hashtb)
2887                        (setq groups (cdr groups)))
2888                      (setq starts (cdr starts)))
2889                     ((= ans ?s)
2890                      (while (and groups
2891                                  (string-match prefix
2892                                                (setq group (car groups))))
2893                        (gnus-sethash group group gnus-killed-hashtb)
2894                        (gnus-subscribe-alphabetically (car groups))
2895                        (setq groups (cdr groups)))
2896                      (setq starts (cdr starts)))
2897                     ((= ans ?q)
2898                      (while groups
2899                        (setq group (car groups))
2900                        (setq gnus-killed-list (cons group gnus-killed-list))
2901                        (gnus-sethash group group gnus-killed-hashtb)
2902                        (setq groups (cdr groups))))
2903                     (t nil)))
2904           (message "Subscribe %s? ([n]yq)" (car groups))
2905           (setq ans (read-char))
2906           (setq group (car groups))
2907           (cond ((= ans ?y)
2908                  (gnus-subscribe-alphabetically (car groups))
2909                  (gnus-sethash group group gnus-killed-hashtb))
2910                 ((= ans ?q)
2911                  (while groups
2912                    (setq group (car groups))
2913                    (setq gnus-killed-list (cons group gnus-killed-list))
2914                    (gnus-sethash group group gnus-killed-hashtb)
2915                    (setq groups (cdr groups))))
2916                 (t
2917                  (setq gnus-killed-list (cons group gnus-killed-list))
2918                  (gnus-sethash group group gnus-killed-hashtb)))
2919           (setq groups (cdr groups)))))))
2920
2921 (defun gnus-subscribe-randomly (newsgroup)
2922   "Subscribe new NEWSGROUP by making it the first newsgroup."
2923   (gnus-subscribe-newsgroup newsgroup))
2924
2925 (defun gnus-subscribe-alphabetically (newgroup)
2926   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2927   (let ((groups (cdr gnus-newsrc-alist))
2928         before)
2929     (while (and (not before) groups)
2930       (if (string< newgroup (caar groups))
2931           (setq before (caar groups))
2932         (setq groups (cdr groups))))
2933     (gnus-subscribe-newsgroup newgroup before)))
2934
2935 (defun gnus-subscribe-hierarchically (newgroup)
2936   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2937   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2938   (save-excursion
2939     (set-buffer (find-file-noselect gnus-current-startup-file))
2940     (let ((groupkey newgroup)
2941           before)
2942       (while (and (not before) groupkey)
2943         (goto-char (point-min))
2944         (let ((groupkey-re
2945                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2946           (while (and (re-search-forward groupkey-re nil t)
2947                       (progn
2948                         (setq before (match-string 1))
2949                         (string< before newgroup)))))
2950         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2951         (setq groupkey
2952               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2953                   (substring groupkey (match-beginning 1) (match-end 1)))))
2954       (gnus-subscribe-newsgroup newgroup before))))
2955
2956 (defun gnus-subscribe-interactively (group)
2957   "Subscribe the new GROUP interactively.
2958 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2959 it is killed."
2960   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2961       (gnus-subscribe-hierarchically group)
2962     (push group gnus-killed-list)))
2963
2964 (defun gnus-subscribe-zombies (group)
2965   "Make the new GROUP into a zombie group."
2966   (push group gnus-zombie-list))
2967
2968 (defun gnus-subscribe-killed (group)
2969   "Make the new GROUP a killed group."
2970   (push group gnus-killed-list))
2971
2972 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2973   "Subscribe new NEWSGROUP.
2974 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2975 the first newsgroup."
2976   ;; We subscribe the group by changing its level to `subscribed'.
2977   (gnus-group-change-level
2978    newsgroup gnus-level-default-subscribed
2979    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2980   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2981
2982 ;; For directories
2983
2984 (defun gnus-newsgroup-directory-form (newsgroup)
2985   "Make hierarchical directory name from NEWSGROUP name."
2986   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2987         (len (length newsgroup))
2988         idx)
2989     ;; If this is a foreign group, we don't want to translate the
2990     ;; entire name.
2991     (if (setq idx (string-match ":" newsgroup))
2992         (aset newsgroup idx ?/)
2993       (setq idx 0))
2994     ;; Replace all occurrences of `.' with `/'.
2995     (while (< idx len)
2996       (if (= (aref newsgroup idx) ?.)
2997           (aset newsgroup idx ?/))
2998       (setq idx (1+ idx)))
2999     newsgroup))
3000
3001 (defun gnus-newsgroup-savable-name (group)
3002   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3003   ;; with dots.
3004   (nnheader-replace-chars-in-string group ?/ ?.))
3005
3006 (defun gnus-make-directory (dir)
3007   "Make DIRECTORY recursively."
3008   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3009   ;; of the many mysteries of the universe.
3010   (let* ((dir (expand-file-name dir default-directory))
3011          dirs err)
3012     (if (string-match "/$" dir)
3013         (setq dir (substring dir 0 (match-beginning 0))))
3014     ;; First go down the path until we find a directory that exists.
3015     (while (not (file-exists-p dir))
3016       (setq dirs (cons dir dirs))
3017       (string-match "/[^/]+$" dir)
3018       (setq dir (substring dir 0 (match-beginning 0))))
3019     ;; Then create all the subdirs.
3020     (while (and dirs (not err))
3021       (condition-case ()
3022           (make-directory (car dirs))
3023         (error (setq err t)))
3024       (setq dirs (cdr dirs)))
3025     ;; We return whether we were successful or not.
3026     (not dirs)))
3027
3028 (defun gnus-capitalize-newsgroup (newsgroup)
3029   "Capitalize NEWSGROUP name."
3030   (and (not (zerop (length newsgroup)))
3031        (concat (char-to-string (upcase (aref newsgroup 0)))
3032                (substring newsgroup 1))))
3033
3034 ;; Various... things.
3035
3036 (defun gnus-simplify-subject (subject &optional re-only)
3037   "Remove `Re:' and words in parentheses.
3038 If RE-ONLY is non-nil, strip leading `Re:'s only."
3039   (let ((case-fold-search t))           ;Ignore case.
3040     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3041     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3042       (setq subject (substring subject (match-end 0))))
3043     ;; Remove uninteresting prefixes.
3044     (if (and (not re-only)
3045              gnus-simplify-ignored-prefixes
3046              (string-match gnus-simplify-ignored-prefixes subject))
3047         (setq subject (substring subject (match-end 0))))
3048     ;; Remove words in parentheses from end.
3049     (unless re-only
3050       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3051         (setq subject (substring subject 0 (match-beginning 0)))))
3052     ;; Return subject string.
3053     subject))
3054
3055 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3056 ;; all whitespace.
3057 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3058 (defun gnus-simplify-buffer-fuzzy ()
3059   (goto-char (point-min))
3060   (while (search-forward "\t" nil t)
3061     (replace-match " " t t))
3062   (goto-char (point-min))
3063   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3064   (goto-char (match-beginning 0))
3065   (while (or
3066           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3067           (looking-at "^[[].*: .*[]]$"))
3068     (goto-char (point-min))
3069     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3070                               nil t)
3071       (replace-match "" t t))
3072     (goto-char (point-min))
3073     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3074       (goto-char (match-end 0))
3075       (delete-char -1)
3076       (delete-region
3077        (progn (goto-char (match-beginning 0)))
3078        (re-search-forward ":"))))
3079   (goto-char (point-min))
3080   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3081     (replace-match "" t t))
3082   (goto-char (point-min))
3083   (while (re-search-forward "  +" nil t)
3084     (replace-match " " t t))
3085   (goto-char (point-min))
3086   (while (re-search-forward " $" nil t)
3087     (replace-match "" t t))
3088   (goto-char (point-min))
3089   (while (re-search-forward "^ +" nil t)
3090     (replace-match "" t t))
3091   (goto-char (point-min))
3092   (when gnus-simplify-subject-fuzzy-regexp
3093     (if (listp gnus-simplify-subject-fuzzy-regexp)
3094         (let ((list gnus-simplify-subject-fuzzy-regexp))
3095           (while list
3096             (goto-char (point-min))
3097             (while (re-search-forward (car list) nil t)
3098               (replace-match "" t t))
3099             (setq list (cdr list))))
3100       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3101         (replace-match "" t t)))))
3102
3103 (defun gnus-simplify-subject-fuzzy (subject)
3104   "Siplify a subject string fuzzily."
3105   (save-excursion
3106     (gnus-set-work-buffer)
3107     (let ((case-fold-search t))
3108       (insert subject)
3109       (inline (gnus-simplify-buffer-fuzzy))
3110       (buffer-string))))
3111
3112 ;; Add the current buffer to the list of buffers to be killed on exit.
3113 (defun gnus-add-current-to-buffer-list ()
3114   (or (memq (current-buffer) gnus-buffer-list)
3115       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3116
3117 (defun gnus-string> (s1 s2)
3118   (not (or (string< s1 s2)
3119            (string= s1 s2))))
3120
3121 ;;; General various misc type functions.
3122
3123 (defun gnus-clear-system ()
3124   "Clear all variables and buffers."
3125   ;; Clear Gnus variables.
3126   (let ((variables gnus-variable-list))
3127     (while variables
3128       (set (car variables) nil)
3129       (setq variables (cdr variables))))
3130   ;; Clear other internal variables.
3131   (setq gnus-list-of-killed-groups nil
3132         gnus-have-read-active-file nil
3133         gnus-newsrc-alist nil
3134         gnus-newsrc-hashtb nil
3135         gnus-killed-list nil
3136         gnus-zombie-list nil
3137         gnus-killed-hashtb nil
3138         gnus-active-hashtb nil
3139         gnus-moderated-list nil
3140         gnus-description-hashtb nil
3141         gnus-current-headers nil
3142         gnus-thread-indent-array nil
3143         gnus-newsgroup-headers nil
3144         gnus-newsgroup-name nil
3145         gnus-server-alist nil
3146         gnus-group-list-mode nil
3147         gnus-opened-servers nil
3148         gnus-current-select-method nil)
3149   (gnus-shutdown 'gnus)
3150   ;; Kill the startup file.
3151   (and gnus-current-startup-file
3152        (get-file-buffer gnus-current-startup-file)
3153        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3154   ;; Clear the dribble buffer.
3155   (gnus-dribble-clear)
3156   ;; Kill global KILL file buffer.
3157   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3158     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3159   (gnus-kill-buffer nntp-server-buffer)
3160   ;; Kill Gnus buffers.
3161   (while gnus-buffer-list
3162     (gnus-kill-buffer (pop gnus-buffer-list)))
3163   ;; Remove Gnus frames.
3164   (while gnus-created-frames
3165     (when (frame-live-p (car gnus-created-frames))
3166       ;; We slap a condition-case around this `delete-frame' to ensure 
3167       ;; agains errors if we try do delete the single frame that's left.
3168       (condition-case ()
3169           (delete-frame (car gnus-created-frames))
3170         (error nil)))
3171     (pop gnus-created-frames)))
3172
3173 (defun gnus-windows-old-to-new (setting)
3174   ;; First we take care of the really, really old Gnus 3 actions.
3175   (when (symbolp setting)
3176     (setq setting
3177           ;; Take care of ooold GNUS 3.x values.
3178           (cond ((eq setting 'SelectArticle) 'article)
3179                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3180                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3181                 (t setting))))
3182   (if (or (listp setting)
3183           (not (and gnus-window-configuration
3184                     (memq setting '(group summary article)))))
3185       setting
3186     (let* ((setting (if (eq setting 'group)
3187                         (if (assq 'newsgroup gnus-window-configuration)
3188                             'newsgroup
3189                           'newsgroups) setting))
3190            (elem (cadr (assq setting gnus-window-configuration)))
3191            (total (apply '+ elem))
3192            (types '(group summary article))
3193            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3194            (i 0)
3195            perc
3196            out)
3197       (while (< i 3)
3198         (or (not (numberp (nth i elem)))
3199             (zerop (nth i elem))
3200             (progn
3201               (setq perc  (/ (float (nth 0 elem)) total))
3202               (setq out (cons (if (eq pbuf (nth i types))
3203                                   (vector (nth i types) perc 'point)
3204                                 (vector (nth i types) perc))
3205                               out))))
3206         (setq i (1+ i)))
3207       (list (nreverse out)))))
3208
3209 ;;;###autoload
3210 (defun gnus-add-configuration (conf)
3211   "Add the window configuration CONF to `gnus-buffer-configuration'."
3212   (setq gnus-buffer-configuration
3213         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3214                          gnus-buffer-configuration))))
3215
3216 (defvar gnus-frame-list nil)
3217
3218 (defun gnus-configure-frame (split &optional window)
3219   "Split WINDOW according to SPLIT."
3220   (unless window
3221     (setq window (get-buffer-window (current-buffer))))
3222   (select-window window)
3223   ;; This might be an old-stylee buffer config.
3224   (when (vectorp split)
3225     (setq split (append split nil)))
3226   (when (or (consp (car split))
3227             (vectorp (car split)))
3228     (push 1.0 split)
3229     (push 'vertical split))
3230   ;; The SPLIT might be something that is to be evaled to
3231   ;; return a new SPLIT.
3232   (while (and (not (assq (car split) gnus-window-to-buffer))
3233               (gnus-functionp (car split)))
3234     (setq split (eval split)))
3235   (let* ((type (car split))
3236          (subs (cddr split))
3237          (len (if (eq type 'horizontal) (window-width) (window-height)))
3238          (total 0)
3239          (window-min-width (or gnus-window-min-width window-min-width))
3240          (window-min-height (or gnus-window-min-height window-min-height))
3241          s result new-win rest comp-subs size sub)
3242     (cond
3243      ;; Nothing to do here.
3244      ((null split))
3245      ;; Don't switch buffers.
3246      ((null type)
3247       (and (memq 'point split) window))
3248      ;; This is a buffer to be selected.
3249      ((not (memq type '(frame horizontal vertical)))
3250       (let ((buffer (cond ((stringp type) type)
3251                           (t (cdr (assq type gnus-window-to-buffer)))))
3252             buf)
3253         (unless buffer
3254           (error "Illegal buffer type: %s" type))
3255         (unless (setq buf (get-buffer (if (symbolp buffer)
3256                                           (symbol-value buffer) buffer)))
3257           (setq buf (get-buffer-create (if (symbolp buffer)
3258                                            (symbol-value buffer) buffer))))
3259         (switch-to-buffer buf)
3260         ;; We return the window if it has the `point' spec.
3261         (and (memq 'point split) window)))
3262      ;; This is a frame split.
3263      ((eq type 'frame)
3264       (unless gnus-frame-list
3265         (setq gnus-frame-list (list (window-frame
3266                                      (get-buffer-window (current-buffer))))))
3267       (let ((i 0)
3268             params frame fresult)
3269         (while (< i (length subs))
3270           ;; Frame parameter is gotten from the sub-split.
3271           (setq params (cadr (elt subs i)))
3272           ;; It should be a list.
3273           (unless (listp params)
3274             (setq params nil))
3275           ;; Create a new frame?
3276           (unless (setq frame (elt gnus-frame-list i))
3277             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3278             (push frame gnus-created-frames))
3279           ;; Is the old frame still alive?
3280           (unless (frame-live-p frame)
3281             (setcar (nthcdr i gnus-frame-list)
3282                     (setq frame (make-frame params))))
3283           ;; Select the frame in question and do more splits there.
3284           (select-frame frame)
3285           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3286           (incf i))
3287         ;; Select the frame that has the selected buffer.
3288         (when fresult
3289           (select-frame (window-frame fresult)))))
3290      ;; This is a normal split.
3291      (t
3292       (when (> (length subs) 0)
3293         ;; First we have to compute the sizes of all new windows.
3294         (while subs
3295           (setq sub (append (pop subs) nil))
3296           (while (and (not (assq (car sub) gnus-window-to-buffer))
3297                       (gnus-functionp (car sub)))
3298             (setq sub (eval sub)))
3299           (when sub
3300             (push sub comp-subs)
3301             (setq size (cadar comp-subs))
3302             (cond ((equal size 1.0)
3303                    (setq rest (car comp-subs))
3304                    (setq s 0))
3305                   ((floatp size)
3306                    (setq s (floor (* size len))))
3307                   ((integerp size)
3308                    (setq s size))
3309                   (t
3310                    (error "Illegal size: %s" size)))
3311             ;; Try to make sure that we are inside the safe limits.
3312             (cond ((zerop s))
3313                   ((eq type 'horizontal)
3314                    (setq s (max s window-min-width)))
3315                   ((eq type 'vertical)
3316                    (setq s (max s window-min-height))))
3317             (setcar (cdar comp-subs) s)
3318             (incf total s)))
3319         ;; Take care of the "1.0" spec.
3320         (if rest
3321             (setcar (cdr rest) (- len total))
3322           (error "No 1.0 specs in %s" split))
3323         ;; The we do the actual splitting in a nice recursive
3324         ;; fashion.
3325         (setq comp-subs (nreverse comp-subs))
3326         (while comp-subs
3327           (if (null (cdr comp-subs))
3328               (setq new-win window)
3329             (setq new-win
3330                   (split-window window (cadar comp-subs)
3331                                 (eq type 'horizontal))))
3332           (setq result (or (gnus-configure-frame
3333                             (car comp-subs) window) result))
3334           (select-window new-win)
3335           (setq window new-win)
3336           (setq comp-subs (cdr comp-subs))))
3337       ;; Return the proper window, if any.
3338       (when result
3339         (select-window result))))))
3340
3341 (defvar gnus-frame-split-p nil)
3342
3343 (defun gnus-configure-windows (setting &optional force)
3344   (setq setting (gnus-windows-old-to-new setting))
3345   (let ((split (if (symbolp setting)
3346                    (cadr (assq setting gnus-buffer-configuration))
3347                  setting))
3348         all-visible)
3349
3350     (setq gnus-frame-split-p nil)
3351
3352     (unless split
3353       (error "No such setting: %s" setting))
3354
3355     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3356              (not force))
3357         ;; All the windows mentioned are already visible, so we just
3358         ;; put point in the assigned buffer, and do not touch the
3359         ;; winconf.
3360         (select-window all-visible)
3361
3362       ;; Either remove all windows or just remove all Gnus windows.
3363       (let ((frame (selected-frame)))
3364         (unwind-protect
3365             (if gnus-use-full-window
3366                 ;; We want to remove all other windows.
3367                 (if (not gnus-frame-split-p)
3368                     ;; This is not a `frame' split, so we ignore the
3369                     ;; other frames.  
3370                     (delete-other-windows)
3371                   ;; This is a `frame' split, so we delete all windows
3372                   ;; on all frames.
3373                   (mapcar 
3374                    (lambda (frame)
3375                      (unless (eq (cdr (assq 'minibuffer
3376                                             (frame-parameters frame)))
3377                                  'only)
3378                        (select-frame frame)
3379                        (delete-other-windows)))
3380                    (frame-list)))
3381               ;; Just remove some windows.
3382               (gnus-remove-some-windows)
3383               (switch-to-buffer nntp-server-buffer))
3384           (select-frame frame)))
3385
3386       (switch-to-buffer nntp-server-buffer)
3387       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3388
3389 (defun gnus-all-windows-visible-p (split)
3390   "Say whether all buffers in SPLIT are currently visible.
3391 In particular, the value returned will be the window that
3392 should have point."
3393   (let ((stack (list split))
3394         (all-visible t)
3395         type buffer win buf)
3396     (while (and (setq split (pop stack))
3397                 all-visible)
3398       ;; Be backwards compatible.
3399       (when (vectorp split)
3400         (setq split (append split nil)))
3401       (when (or (consp (car split))
3402                 (vectorp (car split)))
3403         (push 1.0 split)
3404         (push 'vertical split))
3405       ;; The SPLIT might be something that is to be evaled to
3406       ;; return a new SPLIT.
3407       (while (and (not (assq (car split) gnus-window-to-buffer))
3408                   (gnus-functionp (car split)))
3409         (setq split (eval split)))
3410
3411       (setq type (elt split 0))
3412       (cond
3413        ;; Nothing here.
3414        ((null split) t)
3415        ;; A buffer.
3416        ((not (memq type '(horizontal vertical frame)))
3417         (setq buffer (cond ((stringp type) type)
3418                            (t (cdr (assq type gnus-window-to-buffer)))))
3419         (unless buffer
3420           (error "Illegal buffer type: %s" type))
3421         (when (setq buf (get-buffer (if (symbolp buffer)
3422                                         (symbol-value buffer)
3423                                       buffer)))
3424           (setq win (get-buffer-window buf t)))
3425         (if win
3426             (when (memq 'point split)
3427                 (setq all-visible win))
3428           (setq all-visible nil)))
3429        (t
3430         (when (eq type 'frame)
3431           (setq gnus-frame-split-p t))
3432         (setq stack (append (cddr split) stack)))))
3433     (unless (eq all-visible t)
3434       all-visible)))
3435
3436 (defun gnus-window-top-edge (&optional window)
3437   (nth 1 (window-edges window)))
3438
3439 (defun gnus-remove-some-windows ()
3440   (let ((buffers gnus-window-to-buffer)
3441         buf bufs lowest-buf lowest)
3442     (save-excursion
3443       ;; Remove windows on all known Gnus buffers.
3444       (while buffers
3445         (setq buf (cdar buffers))
3446         (if (symbolp buf)
3447             (setq buf (and (boundp buf) (symbol-value buf))))
3448         (and buf
3449              (get-buffer-window buf)
3450              (progn
3451                (setq bufs (cons buf bufs))
3452                (pop-to-buffer buf)
3453                (if (or (not lowest)
3454                        (< (gnus-window-top-edge) lowest))
3455                    (progn
3456                      (setq lowest (gnus-window-top-edge))
3457                      (setq lowest-buf buf)))))
3458         (setq buffers (cdr buffers)))
3459       ;; Remove windows on *all* summary buffers.
3460       (walk-windows
3461        (lambda (win)
3462          (let ((buf (window-buffer win)))
3463            (if (string-match    "^\\*Summary" (buffer-name buf))
3464                (progn
3465                  (setq bufs (cons buf bufs))
3466                  (pop-to-buffer buf)
3467                  (if (or (not lowest)
3468                          (< (gnus-window-top-edge) lowest))
3469                      (progn
3470                        (setq lowest-buf buf)
3471                        (setq lowest (gnus-window-top-edge)))))))))
3472       (and lowest-buf
3473            (progn
3474              (pop-to-buffer lowest-buf)
3475              (switch-to-buffer nntp-server-buffer)))
3476       (while bufs
3477         (and (not (eq (car bufs) lowest-buf))
3478              (delete-windows-on (car bufs)))
3479         (setq bufs (cdr bufs))))))
3480
3481 (defun gnus-version ()
3482   "Version numbers of this version of Gnus."
3483   (interactive)
3484   (let ((methods gnus-valid-select-methods)
3485         (mess gnus-version)
3486         meth)
3487     ;; Go through all the legal select methods and add their version
3488     ;; numbers to the total version string.  Only the backends that are
3489     ;; currently in use will have their message numbers taken into
3490     ;; consideration.
3491     (while methods
3492       (setq meth (intern (concat (caar methods) "-version")))
3493       (and (boundp meth)
3494            (stringp (symbol-value meth))
3495            (setq mess (concat mess "; " (symbol-value meth))))
3496       (setq methods (cdr methods)))
3497     (gnus-message 2 mess)))
3498
3499 (defun gnus-info-find-node ()
3500   "Find Info documentation of Gnus."
3501   (interactive)
3502   ;; Enlarge info window if needed.
3503   (let ((mode major-mode)
3504         gnus-info-buffer)
3505     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3506     (setq gnus-info-buffer (current-buffer))
3507     (gnus-configure-windows 'info)))
3508
3509 (defun gnus-days-between (date1 date2)
3510   ;; Return the number of days between date1 and date2.
3511   (- (gnus-day-number date1) (gnus-day-number date2)))
3512
3513 (defun gnus-day-number (date)
3514   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3515                      (timezone-parse-date date))))
3516     (timezone-absolute-from-gregorian
3517      (nth 1 dat) (nth 2 dat) (car dat))))
3518
3519 (defun gnus-encode-date (date)
3520   "Convert DATE to internal time."
3521   (let* ((parse (timezone-parse-date date))
3522          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3523          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3524     (encode-time (caddr time) (cadr time) (car time)
3525                  (caddr date) (cadr date) (car date) (nth 4 date))))
3526
3527 (defun gnus-time-minus (t1 t2)
3528   "Subtract two internal times."
3529   (let ((borrow (< (cadr t1) (cadr t2))))
3530     (list (- (car t1) (car t2) (if borrow 1 0))
3531           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3532
3533 (defun gnus-file-newer-than (file date)
3534   (let ((fdate (nth 5 (file-attributes file))))
3535     (or (> (car fdate) (car date))
3536         (and (= (car fdate) (car date))
3537              (> (nth 1 fdate) (nth 1 date))))))
3538
3539 (defmacro gnus-local-set-keys (&rest plist)
3540   "Set the keys in PLIST in the current keymap."
3541   `(gnus-define-keys-1 (current-local-map) ',plist))
3542
3543 (defmacro gnus-define-keys (keymap &rest plist)
3544   "Define all keys in PLIST in KEYMAP."
3545   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3546
3547 (put 'gnus-define-keys 'lisp-indent-function 1)
3548 (put 'gnus-define-keys 'lisp-indent-hook 1)
3549 (put 'gnus-define-keymap 'lisp-indent-function 1)
3550 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3551
3552 (defmacro gnus-define-keymap (keymap &rest plist)
3553   "Define all keys in PLIST in KEYMAP."
3554   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3555
3556 (defun gnus-define-keys-1 (keymap plist)
3557   (when (null keymap)
3558     (error "Can't set keys in a null keymap"))
3559   (cond ((symbolp keymap)
3560          (setq keymap (symbol-value keymap)))
3561         ((keymapp keymap))
3562         ((listp keymap)
3563          (set (car keymap) nil)
3564          (define-prefix-command (car keymap))
3565          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3566          (setq keymap (symbol-value (car keymap)))))
3567   (let (key)
3568     (while plist
3569       (when (symbolp (setq key (pop plist)))
3570         (setq key (symbol-value key)))
3571       (define-key keymap key (pop plist)))))
3572
3573 (defun gnus-group-read-only-p (&optional group)
3574   "Check whether GROUP supports editing or not.
3575 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3576 that that variable is buffer-local to the summary buffers."
3577   (let ((group (or group gnus-newsgroup-name)))
3578     (not (gnus-check-backend-function 'request-replace-article group))))
3579
3580 (defun gnus-group-total-expirable-p (group)
3581   "Check whether GROUP is total-expirable or not."
3582   (let ((params (gnus-info-params (gnus-get-info group))))
3583     (or (memq 'total-expire params)
3584         (cdr (assq 'total-expire params)) ; (total-expire . t)
3585         (and gnus-total-expirable-newsgroups ; Check var.
3586              (string-match gnus-total-expirable-newsgroups group)))))
3587
3588 (defun gnus-group-auto-expirable-p (group)
3589   "Check whether GROUP is total-expirable or not."
3590   (let ((params (gnus-info-params (gnus-get-info group))))
3591     (or (memq 'auto-expire params)
3592         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3593         (and gnus-auto-expirable-newsgroups ; Check var.
3594              (string-match gnus-auto-expirable-newsgroups group)))))
3595
3596 (defun gnus-virtual-group-p (group)
3597   "Say whether GROUP is virtual or not."
3598   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3599                         gnus-valid-select-methods)))
3600
3601 (defun gnus-news-group-p (group &optional article)
3602   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3603   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3604       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3605            (eq (gnus-request-type group article) 'news))))
3606
3607 (defsubst gnus-simplify-subject-fully (subject)
3608   "Simplify a subject string according to the user's wishes."
3609   (cond
3610    ((null gnus-summary-gather-subject-limit)
3611     (gnus-simplify-subject-re subject))
3612    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3613     (gnus-simplify-subject-fuzzy subject))
3614    ((numberp gnus-summary-gather-subject-limit)
3615     (gnus-limit-string (gnus-simplify-subject-re subject)
3616                        gnus-summary-gather-subject-limit))
3617    (t
3618     subject)))
3619
3620 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3621   "Check whether two subjects are equal.  If optional argument
3622 simple-first is t, first argument is already simplified."
3623   (cond
3624    ((null simple-first)
3625     (equal (gnus-simplify-subject-fully s1)
3626            (gnus-simplify-subject-fully s2)))
3627    (t
3628     (equal s1
3629            (gnus-simplify-subject-fully s2)))))
3630
3631 ;; Returns a list of writable groups.
3632 (defun gnus-writable-groups ()
3633   (let ((alist gnus-newsrc-alist)
3634         groups group)
3635     (while (setq group (car (pop alist)))
3636       (unless (gnus-group-read-only-p group)
3637         (push group groups)))
3638     (nreverse groups)))
3639
3640 (defun gnus-completing-read (default prompt &rest args)
3641   ;; Like `completing-read', except that DEFAULT is the default argument.
3642   (let* ((prompt (if default 
3643                      (concat prompt " (default " default ") ")
3644                    (concat prompt " ")))
3645          (answer (apply 'completing-read prompt args)))
3646     (if (or (null answer) (zerop (length answer)))
3647         default
3648       answer)))
3649
3650 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3651 ;; the echo area.
3652 (defun gnus-y-or-n-p (prompt)
3653   (prog1
3654       (y-or-n-p prompt)
3655     (message "")))
3656
3657 (defun gnus-yes-or-no-p (prompt)
3658   (prog1
3659       (yes-or-no-p prompt)
3660     (message "")))
3661
3662 ;; Check whether to use long file names.
3663 (defun gnus-use-long-file-name (symbol)
3664   ;; The variable has to be set...
3665   (and gnus-use-long-file-name
3666        ;; If it isn't a list, then we return t.
3667        (or (not (listp gnus-use-long-file-name))
3668            ;; If it is a list, and the list contains `symbol', we
3669            ;; return nil.
3670            (not (memq symbol gnus-use-long-file-name)))))
3671
3672 ;; I suspect there's a better way, but I haven't taken the time to do
3673 ;; it yet. -erik selberg@cs.washington.edu
3674 (defun gnus-dd-mmm (messy-date)
3675   "Return a string like DD-MMM from a big messy string"
3676   (let ((datevec (timezone-parse-date messy-date)))
3677     (format "%2s-%s"
3678             (condition-case ()
3679                 ;; Make sure leading zeroes are stripped.
3680                 (number-to-string (string-to-number (aref datevec 2)))
3681               (error "??"))
3682             (capitalize
3683              (or (car
3684                   (nth (1- (string-to-number (aref datevec 1)))
3685                        timezone-months-assoc))
3686                  "???")))))
3687
3688 ;; Make a hash table (default and minimum size is 255).
3689 ;; Optional argument HASHSIZE specifies the table size.
3690 (defun gnus-make-hashtable (&optional hashsize)
3691   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3692
3693 ;; Make a number that is suitable for hashing; bigger than MIN and one
3694 ;; less than 2^x.
3695 (defun gnus-create-hash-size (min)
3696   (let ((i 1))
3697     (while (< i min)
3698       (setq i (* 2 i)))
3699     (1- i)))
3700
3701 ;; Show message if message has a lower level than `gnus-verbose'.
3702 ;; Guideline for numbers:
3703 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3704 ;; for things that take a long time, 7 - not very important messages
3705 ;; on stuff, 9 - messages inside loops.
3706 (defun gnus-message (level &rest args)
3707   (if (<= level gnus-verbose)
3708       (apply 'message args)
3709     ;; We have to do this format thingy here even if the result isn't
3710     ;; shown - the return value has to be the same as the return value
3711     ;; from `message'.
3712     (apply 'format args)))
3713
3714 (defun gnus-error (level &rest args)
3715   "Beep an error if `gnus-verbose' is on LEVEL or less."
3716   (when (<= (floor level) gnus-verbose)
3717     (apply 'message args)
3718     (ding)
3719     (let (duration)
3720       (when (and (floatp level)
3721                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3722         (sit-for duration))))
3723   nil)
3724
3725 ;; Generate a unique new group name.
3726 (defun gnus-generate-new-group-name (leaf)
3727   (let ((name leaf)
3728         (num 0))
3729     (while (gnus-gethash name gnus-newsrc-hashtb)
3730       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3731     name))
3732
3733 (defsubst gnus-hide-text (b e props)
3734   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3735   (add-text-properties b e props)
3736   (when (memq 'intangible props)
3737     (put-text-property (max (1- b) (point-min))
3738                        b 'intangible (cddr (memq 'intangible props)))))
3739
3740 (defsubst gnus-unhide-text (b e)
3741   "Remove hidden text properties from region between B and E."
3742   (remove-text-properties b e gnus-hidden-properties)
3743   (when (memq 'intangible gnus-hidden-properties)
3744     (put-text-property (max (1- b) (point-min))
3745                        b 'intangible nil)))
3746
3747 (defun gnus-hide-text-type (b e type)
3748   "Hide text of TYPE between B and E."
3749   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3750
3751 ;; Find out whether the gnus-visual TYPE is wanted.
3752 (defun gnus-visual-p (&optional type class)
3753   (and gnus-visual                      ; Has to be non-nil, at least.
3754        (if (not type)                   ; We don't care about type.
3755            gnus-visual
3756          (if (listp gnus-visual)        ; It's a list, so we check it.
3757              (or (memq type gnus-visual)
3758                  (memq class gnus-visual))
3759            t))))
3760
3761 (defun gnus-parent-id (references)
3762   "Return the last Message-ID in REFERENCES."
3763   (when (and references
3764              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3765     (substring references (match-beginning 1) (match-end 1))))
3766
3767 (defun gnus-split-references (references)
3768   "Return a list of Message-IDs in REFERENCES."
3769   (let ((beg 0)
3770         ids)
3771     (while (string-match "<[^>]+>" references beg)
3772       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3773             ids))
3774     (nreverse ids)))
3775
3776 (defun gnus-buffer-live-p (buffer)
3777   "Say whether BUFFER is alive or not."
3778   (and buffer
3779        (get-buffer buffer)
3780        (buffer-name (get-buffer buffer))))
3781
3782 (defun gnus-ephemeral-group-p (group)
3783   "Say whether GROUP is ephemeral or not."
3784   (gnus-group-get-parameter group 'quit-config))
3785
3786 (defun gnus-group-quit-config (group)
3787   "Return the quit-config of GROUP."
3788   (gnus-group-get-parameter group 'quit-config))
3789
3790 (defun gnus-simplify-mode-line ()
3791   "Make mode lines a bit simpler."
3792   (setq mode-line-modified "-- ")
3793   (when (listp mode-line-format)
3794     (make-local-variable 'mode-line-format)
3795     (setq mode-line-format (copy-sequence mode-line-format))
3796     (when (equal (nth 3 mode-line-format) "   ")
3797       (setcar (nthcdr 3 mode-line-format) " "))))
3798
3799 ;;; List and range functions
3800
3801 (defun gnus-last-element (list)
3802   "Return last element of LIST."
3803   (while (cdr list)
3804     (setq list (cdr list)))
3805   (car list))
3806
3807 (defun gnus-copy-sequence (list)
3808   "Do a complete, total copy of a list."
3809   (if (and (consp list) (not (consp (cdr list))))
3810       (cons (car list) (cdr list))
3811     (mapcar (lambda (elem) (if (consp elem)
3812                                (if (consp (cdr elem))
3813                                    (gnus-copy-sequence elem)
3814                                  (cons (car elem) (cdr elem)))
3815                              elem))
3816             list)))
3817
3818 (defun gnus-set-difference (list1 list2)
3819   "Return a list of elements of LIST1 that do not appear in LIST2."
3820   (let ((list1 (copy-sequence list1)))
3821     (while list2
3822       (setq list1 (delq (car list2) list1))
3823       (setq list2 (cdr list2)))
3824     list1))
3825
3826 (defun gnus-sorted-complement (list1 list2)
3827   "Return a list of elements of LIST1 that do not appear in LIST2.
3828 Both lists have to be sorted over <."
3829   (let (out)
3830     (if (or (null list1) (null list2))
3831         (or list1 list2)
3832       (while (and list1 list2)
3833         (cond ((= (car list1) (car list2))
3834                (setq list1 (cdr list1)
3835                      list2 (cdr list2)))
3836               ((< (car list1) (car list2))
3837                (setq out (cons (car list1) out))
3838                (setq list1 (cdr list1)))
3839               (t
3840                (setq out (cons (car list2) out))
3841                (setq list2 (cdr list2)))))
3842       (nconc (nreverse out) (or list1 list2)))))
3843
3844 (defun gnus-intersection (list1 list2)
3845   (let ((result nil))
3846     (while list2
3847       (if (memq (car list2) list1)
3848           (setq result (cons (car list2) result)))
3849       (setq list2 (cdr list2)))
3850     result))
3851
3852 (defun gnus-sorted-intersection (list1 list2)
3853   ;; LIST1 and LIST2 have to be sorted over <.
3854   (let (out)
3855     (while (and list1 list2)
3856       (cond ((= (car list1) (car list2))
3857              (setq out (cons (car list1) out)
3858                    list1 (cdr list1)
3859                    list2 (cdr list2)))
3860             ((< (car list1) (car list2))
3861              (setq list1 (cdr list1)))
3862             (t
3863              (setq list2 (cdr list2)))))
3864     (nreverse out)))
3865
3866 (defun gnus-set-sorted-intersection (list1 list2)
3867   ;; LIST1 and LIST2 have to be sorted over <.
3868   ;; This function modifies LIST1.
3869   (let* ((top (cons nil list1))
3870          (prev top))
3871     (while (and list1 list2)
3872       (cond ((= (car list1) (car list2))
3873              (setq prev list1
3874                    list1 (cdr list1)
3875                    list2 (cdr list2)))
3876             ((< (car list1) (car list2))
3877              (setcdr prev (cdr list1))
3878              (setq list1 (cdr list1)))
3879             (t
3880              (setq list2 (cdr list2)))))
3881     (setcdr prev nil)
3882     (cdr top)))
3883
3884 (defun gnus-compress-sequence (numbers &optional always-list)
3885   "Convert list of numbers to a list of ranges or a single range.
3886 If ALWAYS-LIST is non-nil, this function will always release a list of
3887 ranges."
3888   (let* ((first (car numbers))
3889          (last (car numbers))
3890          result)
3891     (if (null numbers)
3892         nil
3893       (if (not (listp (cdr numbers)))
3894           numbers
3895         (while numbers
3896           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3897                 ((= (1+ last) (car numbers)) ;Still in sequence
3898                  (setq last (car numbers)))
3899                 (t                      ;End of one sequence
3900                  (setq result
3901                        (cons (if (= first last) first
3902                                (cons first last)) result))
3903                  (setq first (car numbers))
3904                  (setq last  (car numbers))))
3905           (setq numbers (cdr numbers)))
3906         (if (and (not always-list) (null result))
3907             (if (= first last) (list first) (cons first last))
3908           (nreverse (cons (if (= first last) first (cons first last))
3909                           result)))))))
3910
3911 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3912 (defun gnus-uncompress-range (ranges)
3913   "Expand a list of ranges into a list of numbers.
3914 RANGES is either a single range on the form `(num . num)' or a list of
3915 these ranges."
3916   (let (first last result)
3917     (cond
3918      ((null ranges)
3919       nil)
3920      ((not (listp (cdr ranges)))
3921       (setq first (car ranges))
3922       (setq last (cdr ranges))
3923       (while (<= first last)
3924         (setq result (cons first result))
3925         (setq first (1+ first)))
3926       (nreverse result))
3927      (t
3928       (while ranges
3929         (if (atom (car ranges))
3930             (if (numberp (car ranges))
3931                 (setq result (cons (car ranges) result)))
3932           (setq first (caar ranges))
3933           (setq last  (cdar ranges))
3934           (while (<= first last)
3935             (setq result (cons first result))
3936             (setq first (1+ first))))
3937         (setq ranges (cdr ranges)))
3938       (nreverse result)))))
3939
3940 (defun gnus-add-to-range (ranges list)
3941   "Return a list of ranges that has all articles from both RANGES and LIST.
3942 Note: LIST has to be sorted over `<'."
3943   (if (not ranges)
3944       (gnus-compress-sequence list t)
3945     (setq list (copy-sequence list))
3946     (or (listp (cdr ranges))
3947         (setq ranges (list ranges)))
3948     (let ((out ranges)
3949           ilist lowest highest temp)
3950       (while (and ranges list)
3951         (setq ilist list)
3952         (setq lowest (or (and (atom (car ranges)) (car ranges))
3953                          (caar ranges)))
3954         (while (and list (cdr list) (< (cadr list) lowest))
3955           (setq list (cdr list)))
3956         (if (< (car ilist) lowest)
3957             (progn
3958               (setq temp list)
3959               (setq list (cdr list))
3960               (setcdr temp nil)
3961               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3962         (setq highest (or (and (atom (car ranges)) (car ranges))
3963                           (cdar ranges)))
3964         (while (and list (<= (car list) highest))
3965           (setq list (cdr list)))
3966         (setq ranges (cdr ranges)))
3967       (if list
3968           (setq out (nconc (gnus-compress-sequence list t) out)))
3969       (setq out (sort out (lambda (r1 r2)
3970                             (< (or (and (atom r1) r1) (car r1))
3971                                (or (and (atom r2) r2) (car r2))))))
3972       (setq ranges out)
3973       (while ranges
3974         (if (atom (car ranges))
3975             (if (cdr ranges)
3976                 (if (atom (cadr ranges))
3977                     (if (= (1+ (car ranges)) (cadr ranges))
3978                         (progn
3979                           (setcar ranges (cons (car ranges)
3980                                                (cadr ranges)))
3981                           (setcdr ranges (cddr ranges))))
3982                   (if (= (1+ (car ranges)) (caadr ranges))
3983                       (progn
3984                         (setcar (cadr ranges) (car ranges))
3985                         (setcar ranges (cadr ranges))
3986                         (setcdr ranges (cddr ranges))))))
3987           (if (cdr ranges)
3988               (if (atom (cadr ranges))
3989                   (if (= (1+ (cdar ranges)) (cadr ranges))
3990                       (progn
3991                         (setcdr (car ranges) (cadr ranges))
3992                         (setcdr ranges (cddr ranges))))
3993                 (if (= (1+ (cdar ranges)) (caadr ranges))
3994                     (progn
3995                       (setcdr (car ranges) (cdadr ranges))
3996                       (setcdr ranges (cddr ranges)))))))
3997         (setq ranges (cdr ranges)))
3998       out)))
3999
4000 (defun gnus-remove-from-range (ranges list)
4001   "Return a list of ranges that has all articles from LIST removed from RANGES.
4002 Note: LIST has to be sorted over `<'."
4003   ;; !!! This function shouldn't look like this, but I've got a headache.
4004   (gnus-compress-sequence
4005    (gnus-sorted-complement
4006     (gnus-uncompress-range ranges) list)))
4007
4008 (defun gnus-member-of-range (number ranges)
4009   (if (not (listp (cdr ranges)))
4010       (and (>= number (car ranges))
4011            (<= number (cdr ranges)))
4012     (let ((not-stop t))
4013       (while (and ranges
4014                   (if (numberp (car ranges))
4015                       (>= number (car ranges))
4016                     (>= number (caar ranges)))
4017                   not-stop)
4018         (if (if (numberp (car ranges))
4019                 (= number (car ranges))
4020               (and (>= number (caar ranges))
4021                    (<= number (cdar ranges))))
4022             (setq not-stop nil))
4023         (setq ranges (cdr ranges)))
4024       (not not-stop))))
4025
4026 (defun gnus-range-length (range)
4027   "Return the length RANGE would have if uncompressed."
4028   (length (gnus-uncompress-range range)))
4029
4030 (defun gnus-sublist-p (list sublist)
4031   "Test whether all elements in SUBLIST are members of LIST."
4032   (let ((sublistp t))
4033     (while sublist
4034       (unless (memq (pop sublist) list)
4035         (setq sublistp nil
4036               sublist nil)))
4037     sublistp))
4038
4039 \f
4040 ;;;
4041 ;;; Gnus group mode
4042 ;;;
4043
4044 (defvar gnus-group-mode-map nil)
4045 (put 'gnus-group-mode 'mode-class 'special)
4046
4047 (unless gnus-group-mode-map
4048   (setq gnus-group-mode-map (make-keymap))
4049   (suppress-keymap gnus-group-mode-map)
4050
4051   (gnus-define-keys gnus-group-mode-map
4052     " " gnus-group-read-group
4053     "=" gnus-group-select-group
4054     "\r" gnus-group-select-group
4055     "\M-\r" gnus-group-quick-select-group
4056     "j" gnus-group-jump-to-group
4057     "n" gnus-group-next-unread-group
4058     "p" gnus-group-prev-unread-group
4059     "\177" gnus-group-prev-unread-group
4060     [delete] gnus-group-prev-unread-group
4061     "N" gnus-group-next-group
4062     "P" gnus-group-prev-group
4063     "\M-n" gnus-group-next-unread-group-same-level
4064     "\M-p" gnus-group-prev-unread-group-same-level
4065     "," gnus-group-best-unread-group
4066     "." gnus-group-first-unread-group
4067     "u" gnus-group-unsubscribe-current-group
4068     "U" gnus-group-unsubscribe-group
4069     "c" gnus-group-catchup-current
4070     "C" gnus-group-catchup-current-all
4071     "l" gnus-group-list-groups
4072     "L" gnus-group-list-all-groups
4073     "m" gnus-group-mail
4074     "g" gnus-group-get-new-news
4075     "\M-g" gnus-group-get-new-news-this-group
4076     "R" gnus-group-restart
4077     "r" gnus-group-read-init-file
4078     "B" gnus-group-browse-foreign-server
4079     "b" gnus-group-check-bogus-groups
4080     "F" gnus-find-new-newsgroups
4081     "\C-c\C-d" gnus-group-describe-group
4082     "\M-d" gnus-group-describe-all-groups
4083     "\C-c\C-a" gnus-group-apropos
4084     "\C-c\M-\C-a" gnus-group-description-apropos
4085     "a" gnus-group-post-news
4086     "\ek" gnus-group-edit-local-kill
4087     "\eK" gnus-group-edit-global-kill
4088     "\C-k" gnus-group-kill-group
4089     "\C-y" gnus-group-yank-group
4090     "\C-w" gnus-group-kill-region
4091     "\C-x\C-t" gnus-group-transpose-groups
4092     "\C-c\C-l" gnus-group-list-killed
4093     "\C-c\C-x" gnus-group-expire-articles
4094     "\C-c\M-\C-x" gnus-group-expire-all-groups
4095     "V" gnus-version
4096     "s" gnus-group-save-newsrc
4097     "z" gnus-group-suspend
4098     "Z" gnus-group-clear-dribble
4099     "q" gnus-group-exit
4100     "Q" gnus-group-quit
4101     "?" gnus-group-describe-briefly
4102     "\C-c\C-i" gnus-info-find-node
4103     "\M-e" gnus-group-edit-group-method
4104     "^" gnus-group-enter-server-mode
4105     gnus-mouse-2 gnus-mouse-pick-group
4106     "<" beginning-of-buffer
4107     ">" end-of-buffer
4108     "\C-c\C-b" gnus-bug
4109     "\C-c\C-s" gnus-group-sort-groups
4110     "t" gnus-topic-mode
4111     "\C-c\M-g" gnus-activate-all-groups
4112     "\M-&" gnus-group-universal-argument
4113     "#" gnus-group-mark-group
4114     "\M-#" gnus-group-unmark-group)
4115
4116   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4117     "m" gnus-group-mark-group
4118     "u" gnus-group-unmark-group
4119     "w" gnus-group-mark-region
4120     "m" gnus-group-mark-buffer
4121     "r" gnus-group-mark-regexp
4122     "U" gnus-group-unmark-all-groups)
4123
4124   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4125     "d" gnus-group-make-directory-group
4126     "h" gnus-group-make-help-group
4127     "a" gnus-group-make-archive-group
4128     "k" gnus-group-make-kiboze-group
4129     "m" gnus-group-make-group
4130     "E" gnus-group-edit-group
4131     "e" gnus-group-edit-group-method
4132     "p" gnus-group-edit-group-parameters
4133     "v" gnus-group-add-to-virtual
4134     "V" gnus-group-make-empty-virtual
4135     "D" gnus-group-enter-directory
4136     "f" gnus-group-make-doc-group
4137     "r" gnus-group-rename-group
4138     "\177" gnus-group-delete-group
4139     [delete] gnus-group-delete-group)
4140
4141    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4142      "b" gnus-group-brew-soup
4143      "w" gnus-soup-save-areas
4144      "s" gnus-soup-send-replies
4145      "p" gnus-soup-pack-packet
4146      "r" nnsoup-pack-replies)
4147
4148    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4149      "s" gnus-group-sort-groups
4150      "a" gnus-group-sort-groups-by-alphabet
4151      "u" gnus-group-sort-groups-by-unread
4152      "l" gnus-group-sort-groups-by-level
4153      "v" gnus-group-sort-groups-by-score
4154      "r" gnus-group-sort-groups-by-rank
4155      "m" gnus-group-sort-groups-by-method)
4156
4157    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4158      "k" gnus-group-list-killed
4159      "z" gnus-group-list-zombies
4160      "s" gnus-group-list-groups
4161      "u" gnus-group-list-all-groups
4162      "A" gnus-group-list-active
4163      "a" gnus-group-apropos
4164      "d" gnus-group-description-apropos
4165      "m" gnus-group-list-matching
4166      "M" gnus-group-list-all-matching
4167      "l" gnus-group-list-level)
4168
4169    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4170      "f" gnus-score-flush-cache)
4171
4172    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4173      "f" gnus-group-fetch-faq)
4174
4175    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4176      "l" gnus-group-set-current-level
4177      "t" gnus-group-unsubscribe-current-group
4178      "s" gnus-group-unsubscribe-group
4179      "k" gnus-group-kill-group
4180      "y" gnus-group-yank-group
4181      "w" gnus-group-kill-region
4182      "\C-k" gnus-group-kill-level
4183      "z" gnus-group-kill-all-zombies))
4184
4185 (defun gnus-group-mode ()
4186   "Major mode for reading news.
4187
4188 All normal editing commands are switched off.
4189 \\<gnus-group-mode-map>
4190 The group buffer lists (some of) the groups available.  For instance,
4191 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4192 lists all zombie groups.
4193
4194 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4195 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4196
4197 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4198
4199 The following commands are available:
4200
4201 \\{gnus-group-mode-map}"
4202   (interactive)
4203   (when (and menu-bar-mode
4204              (gnus-visual-p 'group-menu 'menu))
4205     (gnus-group-make-menu-bar))
4206   (kill-all-local-variables)
4207   (gnus-simplify-mode-line)
4208   (setq major-mode 'gnus-group-mode)
4209   (setq mode-name "Group")
4210   (gnus-group-set-mode-line)
4211   (setq mode-line-process nil)
4212   (use-local-map gnus-group-mode-map)
4213   (buffer-disable-undo (current-buffer))
4214   (setq truncate-lines t)
4215   (setq buffer-read-only t)
4216   (run-hooks 'gnus-group-mode-hook))
4217
4218 (defun gnus-mouse-pick-group (e)
4219   "Enter the group under the mouse pointer."
4220   (interactive "e")
4221   (mouse-set-point e)
4222   (gnus-group-read-group nil))
4223
4224 ;; Look at LEVEL and find out what the level is really supposed to be.
4225 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4226 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4227 (defun gnus-group-default-level (&optional level number-or-nil)
4228   (cond
4229    (gnus-group-use-permanent-levels
4230 ;    (setq gnus-group-default-list-level
4231 ;         (or level gnus-group-default-list-level))
4232     (or level gnus-group-default-list-level gnus-level-subscribed))
4233    (number-or-nil
4234     level)
4235    (t
4236     (or level gnus-group-default-list-level gnus-level-subscribed))))
4237
4238 ;;;###autoload
4239 (defun gnus-slave-no-server (&optional arg)
4240   "Read network news as a slave, without connecting to local server"
4241   (interactive "P")
4242   (gnus-no-server arg t))
4243
4244 ;;;###autoload
4245 (defun gnus-no-server (&optional arg slave)
4246   "Read network news.
4247 If ARG is a positive number, Gnus will use that as the
4248 startup level.  If ARG is nil, Gnus will be started at level 2.
4249 If ARG is non-nil and not a positive number, Gnus will
4250 prompt the user for the name of an NNTP server to use.
4251 As opposed to `gnus', this command will not connect to the local server."
4252   (interactive "P")
4253   (let ((gnus-group-use-permanent-levels t))
4254     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4255   (make-local-variable 'gnus-group-use-permanent-levels)
4256   (setq gnus-group-use-permanent-levels t))
4257
4258 ;;;###autoload
4259 (defun gnus-slave (&optional arg)
4260   "Read news as a slave."
4261   (interactive "P")
4262   (gnus arg nil 'slave))
4263
4264 ;;;###autoload
4265 (defun gnus-other-frame (&optional arg)
4266   "Pop up a frame to read news."
4267   (interactive "P")
4268   (if (get-buffer gnus-group-buffer)
4269       (let ((pop-up-frames t))
4270         (gnus arg))
4271     (select-frame (make-frame))
4272     (gnus arg)))
4273
4274 ;;;###autoload
4275 (defun gnus (&optional arg dont-connect slave)
4276   "Read network news.
4277 If ARG is non-nil and a positive number, Gnus will use that as the
4278 startup level.  If ARG is non-nil and not a positive number, Gnus will
4279 prompt the user for the name of an NNTP server to use."
4280   (interactive "P")
4281
4282   (if (get-buffer gnus-group-buffer)
4283       (progn
4284         (switch-to-buffer gnus-group-buffer)
4285         (gnus-group-get-new-news))
4286
4287     (gnus-clear-system)
4288     (nnheader-init-server-buffer)
4289     (gnus-read-init-file)
4290     (setq gnus-slave slave)
4291
4292     (gnus-group-setup-buffer)
4293     (let ((buffer-read-only nil))
4294       (erase-buffer)
4295       (if (not gnus-inhibit-startup-message)
4296           (progn
4297             (gnus-group-startup-message)
4298             (sit-for 0))))
4299
4300     (let ((level (and (numberp arg) (> arg 0) arg))
4301           did-connect)
4302       (unwind-protect
4303           (progn
4304             (or dont-connect
4305                 (setq did-connect
4306                       (gnus-start-news-server (and arg (not level))))))
4307         (if (and (not dont-connect)
4308                  (not did-connect))
4309             (gnus-group-quit)
4310           (run-hooks 'gnus-startup-hook)
4311           ;; NNTP server is successfully open.
4312
4313           ;; Find the current startup file name.
4314           (setq gnus-current-startup-file
4315                 (gnus-make-newsrc-file gnus-startup-file))
4316
4317           ;; Read the dribble file.
4318           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4319
4320           ;; Allow using GroupLens predictions.
4321           (when gnus-use-grouplens
4322             (bbb-login)
4323             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4324
4325           (gnus-summary-make-display-table)
4326           ;; Do the actual startup.
4327           (gnus-setup-news nil level dont-connect)
4328           ;; Generate the group buffer.
4329           (gnus-group-list-groups level)
4330           (gnus-group-first-unread-group)
4331           (gnus-configure-windows 'group)
4332           (gnus-group-set-mode-line))))))
4333
4334 (defun gnus-unload ()
4335   "Unload all Gnus features."
4336   (interactive)
4337   (or (boundp 'load-history)
4338       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4339   (let ((history load-history)
4340         feature)
4341     (while history
4342       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4343            (setq feature (cdr (assq 'provide (car history))))
4344            (unload-feature feature 'force))
4345       (setq history (cdr history)))))
4346
4347 (defun gnus-compile ()
4348   "Byte-compile the user-defined format specs."
4349   (interactive)
4350   (let ((entries gnus-format-specs)
4351         entry gnus-tmp-func)
4352     (save-excursion
4353       (gnus-message 7 "Compiling format specs...")
4354
4355       (while entries
4356         (setq entry (pop entries))
4357         (if (eq (car entry) 'version)
4358             (setq gnus-format-specs (delq entry gnus-format-specs))
4359           (when (and (listp (caddr entry))
4360                      (not (eq 'byte-code (caaddr entry))))
4361             (fset 'gnus-tmp-func
4362                   `(lambda () ,(caddr entry)))
4363             (byte-compile 'gnus-tmp-func)
4364             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4365
4366       (push (cons 'version emacs-version) gnus-format-specs)
4367
4368       (gnus-message 7 "Compiling user specs...done"))))
4369
4370 (defun gnus-indent-rigidly (start end arg)
4371   "Indent rigidly using only spaces and no tabs."
4372   (save-excursion
4373     (save-restriction
4374       (narrow-to-region start end)
4375       (indent-rigidly start end arg)
4376       (goto-char (point-min))
4377       (while (search-forward "\t" nil t)
4378         (replace-match "        " t t)))))
4379
4380 (defun gnus-group-startup-message (&optional x y)
4381   "Insert startup message in current buffer."
4382   ;; Insert the message.
4383   (erase-buffer)
4384   (insert
4385    (format "              %s
4386           _    ___ _             _
4387           _ ___ __ ___  __    _ ___
4388           __   _     ___    __  ___
4389               _           ___     _
4390              _  _ __             _
4391              ___   __            _
4392                    __           _
4393                     _      _   _
4394                    _      _    _
4395                       _  _    _
4396                   __  ___
4397                  _   _ _     _
4398                 _   _
4399               _    _
4400              _    _
4401             _
4402           __
4403
4404 "
4405            ""))
4406   ;; And then hack it.
4407   (gnus-indent-rigidly (point-min) (point-max)
4408                        (/ (max (- (window-width) (or x 46)) 0) 2))
4409   (goto-char (point-min))
4410   (forward-line 1)
4411   (let* ((pheight (count-lines (point-min) (point-max)))
4412          (wheight (window-height))
4413          (rest (- wheight pheight)))
4414     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4415   ;; Fontify some.
4416   (goto-char (point-min))
4417   (and (search-forward "Praxis" nil t)
4418        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4419   (goto-char (point-min))
4420   (let* ((mode-string (gnus-group-set-mode-line)))
4421     (setq mode-line-buffer-identification
4422           (list (concat gnus-version (substring (car mode-string) 4))))
4423     (set-buffer-modified-p t)))
4424
4425 (defun gnus-group-setup-buffer ()
4426   (or (get-buffer gnus-group-buffer)
4427       (progn
4428         (switch-to-buffer gnus-group-buffer)
4429         (gnus-add-current-to-buffer-list)
4430         (gnus-group-mode)
4431         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4432
4433 (defun gnus-group-list-groups (&optional level unread lowest)
4434   "List newsgroups with level LEVEL or lower that have unread articles.
4435 Default is all subscribed groups.
4436 If argument UNREAD is non-nil, groups with no unread articles are also
4437 listed."
4438   (interactive (list (if current-prefix-arg
4439                          (prefix-numeric-value current-prefix-arg)
4440                        (or
4441                         (gnus-group-default-level nil t)
4442                         gnus-group-default-list-level
4443                         gnus-level-subscribed))))
4444   (or level
4445       (setq level (car gnus-group-list-mode)
4446             unread (cdr gnus-group-list-mode)))
4447   (setq level (gnus-group-default-level level))
4448   (gnus-group-setup-buffer)             ;May call from out of group buffer
4449   (gnus-update-format-specifications)
4450   (let ((case-fold-search nil)
4451         (props (text-properties-at (gnus-point-at-bol)))
4452         (group (gnus-group-group-name)))
4453     (set-buffer gnus-group-buffer)
4454     (funcall gnus-group-prepare-function level unread lowest)
4455     (if (zerop (buffer-size))
4456         (gnus-message 5 gnus-no-groups-message)
4457       (goto-char (point-max))
4458       (when (or (not gnus-group-goto-next-group-function)
4459                 (not (funcall gnus-group-goto-next-group-function 
4460                               group props)))
4461         (if (not group)
4462             ;; Go to the first group with unread articles.
4463             (gnus-group-search-forward t)
4464           ;; Find the right group to put point on.  If the current group
4465           ;; has disappeared in the new listing, try to find the next
4466           ;; one.        If no next one can be found, just leave point at the
4467           ;; first newsgroup in the buffer.
4468           (if (not (gnus-goto-char
4469                     (text-property-any
4470                      (point-min) (point-max)
4471                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4472               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4473                 (while (and newsrc
4474                             (not (gnus-goto-char
4475                                   (text-property-any
4476                                    (point-min) (point-max) 'gnus-group
4477                                    (gnus-intern-safe
4478                                     (caar newsrc) gnus-active-hashtb)))))
4479                   (setq newsrc (cdr newsrc)))
4480                 (or newsrc (progn (goto-char (point-max))
4481                                   (forward-line -1)))))))
4482       ;; Adjust cursor point.
4483       (gnus-group-position-point))))
4484
4485 (defun gnus-group-list-level (level &optional all)
4486   "List groups on LEVEL.
4487 If ALL (the prefix), also list groups that have no unread articles."
4488   (interactive "nList groups on level: \nP")
4489   (gnus-group-list-groups level all level))
4490
4491 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4492   "List all newsgroups with unread articles of level LEVEL or lower.
4493 If ALL is non-nil, list groups that have no unread articles.
4494 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4495 If REGEXP, only list groups matching REGEXP."
4496   (set-buffer gnus-group-buffer)
4497   (let ((buffer-read-only nil)
4498         (newsrc (cdr gnus-newsrc-alist))
4499         (lowest (or lowest 1))
4500         info clevel unread group params)
4501     (erase-buffer)
4502     (if (< lowest gnus-level-zombie)
4503         ;; List living groups.
4504         (while newsrc
4505           (setq info (car newsrc)
4506                 group (gnus-info-group info)
4507                 params (gnus-info-params info)
4508                 newsrc (cdr newsrc)
4509                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4510           (and unread                   ; This group might be bogus
4511                (or (not regexp)
4512                    (string-match regexp group))
4513                (<= (setq clevel (gnus-info-level info)) level)
4514                (>= clevel lowest)
4515                (or all                  ; We list all groups?
4516                    (if (eq unread t)    ; Unactivated?
4517                        gnus-group-list-inactive-groups ; We list unactivated 
4518                      (> unread 0))      ; We list groups with unread articles
4519                    (and gnus-list-groups-with-ticked-articles
4520                         (cdr (assq 'tick (gnus-info-marks info))))
4521                                         ; And groups with tickeds
4522                    ;; Check for permanent visibility.
4523                    (and gnus-permanently-visible-groups
4524                         (string-match gnus-permanently-visible-groups
4525                                       group))
4526                    (memq 'visible params)
4527                    (cdr (assq 'visible params)))
4528                (gnus-group-insert-group-line
4529                 group (gnus-info-level info)
4530                 (gnus-info-marks info) unread (gnus-info-method info)))))
4531
4532     ;; List dead groups.
4533     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4534          (gnus-group-prepare-flat-list-dead
4535           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4536           gnus-level-zombie ?Z
4537           regexp))
4538     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4539          (gnus-group-prepare-flat-list-dead
4540           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4541           gnus-level-killed ?K regexp))
4542
4543     (gnus-group-set-mode-line)
4544     (setq gnus-group-list-mode (cons level all))
4545     (run-hooks 'gnus-group-prepare-hook)))
4546
4547 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4548   ;; List zombies and killed lists somewhat faster, which was
4549   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4550   ;; this by ignoring the group format specification altogether.
4551   (let (group)
4552     (if regexp
4553         ;; This loop is used when listing groups that match some
4554         ;; regexp.
4555         (while groups
4556           (setq group (pop groups))
4557           (when (string-match regexp group)
4558             (add-text-properties
4559              (point) (prog1 (1+ (point))
4560                        (insert " " mark "     *: " group "\n"))
4561              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4562                    'gnus-unread t
4563                    'gnus-level level))))
4564       ;; This loop is used when listing all groups.
4565       (while groups
4566         (add-text-properties
4567          (point) (prog1 (1+ (point))
4568                    (insert " " mark "     *: "
4569                            (setq group (pop groups)) "\n"))
4570          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4571                'gnus-unread t
4572                'gnus-level level))))))
4573
4574 (defmacro gnus-group-real-name (group)
4575   "Find the real name of a foreign newsgroup."
4576   `(let ((gname ,group))
4577      (if (string-match ":[^:]+$" gname)
4578          (substring gname (1+ (match-beginning 0)))
4579        gname)))
4580
4581 (defsubst gnus-server-add-address (method)
4582   (let ((method-name (symbol-name (car method))))
4583     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4584              (not (assq (intern (concat method-name "-address")) method)))
4585         (append method (list (list (intern (concat method-name "-address"))
4586                                    (nth 1 method))))
4587       method)))
4588
4589 (defsubst gnus-server-get-method (group method)
4590   ;; Input either a server name, and extended server name, or a
4591   ;; select method, and return a select method.
4592   (cond ((stringp method)
4593          (gnus-server-to-method method))
4594         ((equal method gnus-select-method)
4595          gnus-select-method)
4596         ((and (stringp (car method)) group)
4597          (gnus-server-extend-method group method))
4598         ((and method (not group)
4599               (equal (cadr method) ""))
4600          method)
4601         (t
4602          (gnus-server-add-address method))))
4603
4604 (defun gnus-server-to-method (server)
4605   "Map virtual server names to select methods."
4606   (or 
4607    ;; Perhaps this is the native server?
4608    (and (equal server "native") gnus-select-method)
4609    ;; It should be in the server alist.
4610    (cdr (assoc server gnus-server-alist))
4611    ;; If not, we look through all the opened server
4612    ;; to see whether we can find it there.
4613    (let ((opened gnus-opened-servers))
4614      (while (and opened
4615                  (not (equal server (format "%s:%s" (caaar opened)
4616                                             (cadaar opened)))))
4617        (pop opened))
4618      (caar opened))))
4619
4620 (defmacro gnus-method-equal (ss1 ss2)
4621   "Say whether two servers are equal."
4622   `(let ((s1 ,ss1)
4623          (s2 ,ss2))
4624      (or (equal s1 s2)
4625          (and (= (length s1) (length s2))
4626               (progn
4627                 (while (and s1 (member (car s1) s2))
4628                   (setq s1 (cdr s1)))
4629                 (null s1))))))
4630
4631 (defun gnus-server-equal (m1 m2)
4632   "Say whether two methods are equal."
4633   (let ((m1 (cond ((null m1) gnus-select-method)
4634                   ((stringp m1) (gnus-server-to-method m1))
4635                   (t m1)))
4636         (m2 (cond ((null m2) gnus-select-method)
4637                   ((stringp m2) (gnus-server-to-method m2))
4638                   (t m2))))
4639     (gnus-method-equal m1 m2)))
4640
4641 (defun gnus-servers-using-backend (backend)
4642   "Return a list of known servers using BACKEND."
4643   (let ((opened gnus-opened-servers)
4644         out)
4645     (while opened
4646       (when (eq backend (caaar opened))
4647         (push (caar opened) out))
4648       (pop opened))
4649     out))
4650
4651 (defun gnus-group-prefixed-name (group method)
4652   "Return the whole name from GROUP and METHOD."
4653   (and (stringp method) (setq method (gnus-server-to-method method)))
4654   (concat (format "%s" (car method))
4655           (if (and
4656                (or (assoc (format "%s" (car method)) 
4657                           (gnus-methods-using 'address))
4658                    (gnus-server-equal method gnus-message-archive-method))
4659                (nth 1 method)
4660                (not (string= (nth 1 method) "")))
4661               (concat "+" (nth 1 method)))
4662           ":" group))
4663
4664 (defun gnus-group-real-prefix (group)
4665   "Return the prefix of the current group name."
4666   (if (string-match "^[^:]+:" group)
4667       (substring group 0 (match-end 0))
4668     ""))
4669
4670 (defun gnus-group-method (group)
4671   "Return the server or method used for selecting GROUP."
4672   (let ((prefix (gnus-group-real-prefix group)))
4673     (if (equal prefix "")
4674         gnus-select-method
4675       (let ((servers gnus-opened-servers)
4676             (server "")
4677             backend possible found)
4678         (if (string-match "^[^\\+]+\\+" prefix)
4679             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4680                   server (substring prefix (match-end 0) (1- (length prefix))))
4681           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4682         (while servers
4683           (when (eq (caaar servers) backend)
4684             (setq possible (caar servers))
4685             (when (equal (cadaar servers) server)
4686               (setq found (caar servers))))
4687           (pop servers))
4688         (or (car (rassoc found gnus-server-alist))
4689             found
4690             (car (rassoc possible gnus-server-alist))
4691             possible
4692             (list backend server))))))
4693
4694 (defsubst gnus-secondary-method-p (method)
4695   "Return whether METHOD is a secondary select method."
4696   (let ((methods gnus-secondary-select-methods)
4697         (gmethod (gnus-server-get-method nil method)))
4698     (while (and methods
4699                 (not (equal (gnus-server-get-method nil (car methods))
4700                             gmethod)))
4701       (setq methods (cdr methods)))
4702     methods))
4703
4704 (defun gnus-group-foreign-p (group)
4705   "Say whether a group is foreign or not."
4706   (and (not (gnus-group-native-p group))
4707        (not (gnus-group-secondary-p group))))
4708
4709 (defun gnus-group-native-p (group)
4710   "Say whether the group is native or not."
4711   (not (string-match ":" group)))
4712
4713 (defun gnus-group-secondary-p (group)
4714   "Say whether the group is secondary or not."
4715   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4716
4717 (defun gnus-group-get-parameter (group &optional symbol)
4718   "Returns the group parameters for GROUP.
4719 If SYMBOL, return the value of that symbol in the group parameters."
4720   (let ((params (gnus-info-params (gnus-get-info group))))
4721     (if symbol
4722         (gnus-group-parameter-value params symbol)
4723       params)))
4724
4725 (defun gnus-group-parameter-value (params symbol)
4726   "Return the value of SYMBOL in group PARAMS."
4727   (or (car (memq symbol params))        ; It's either a simple symbol
4728       (cdr (assq symbol params))))      ; or a cons.
4729
4730 (defun gnus-group-add-parameter (group param)
4731   "Add parameter PARAM to GROUP."
4732   (let ((info (gnus-get-info group)))
4733     (if (not info)
4734         () ; This is a dead group.  We just ignore it.
4735       ;; Cons the new param to the old one and update.
4736       (gnus-group-set-info (cons param (gnus-info-params info))
4737                            group 'params))))
4738
4739 (defun gnus-group-set-parameter (group name value)
4740   "Set parameter NAME to VALUE in GROUP."
4741   (let ((info (gnus-get-info group)))
4742     (if (not info)
4743         () ; This is a dead group.  We just ignore it.
4744       (let ((old-params (gnus-info-params info))
4745             (new-params (list (cons name value))))
4746         (while old-params
4747           (if (or (not (listp (car old-params)))
4748                   (not (eq (caar old-params) name)))
4749               (setq new-params (append new-params (list (car old-params)))))
4750           (setq old-params (cdr old-params)))
4751         (gnus-group-set-info new-params group 'params)))))
4752
4753 (defun gnus-group-add-score (group &optional score)
4754   "Add SCORE to the GROUP score.
4755 If SCORE is nil, add 1 to the score of GROUP."
4756   (let ((info (gnus-get-info group)))
4757     (when info
4758       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4759
4760 (defun gnus-summary-bubble-group ()
4761   "Increase the score of the current group.
4762 This is a handy function to add to `gnus-summary-exit-hook' to
4763 increase the score of each group you read."
4764   (gnus-group-add-score gnus-newsgroup-name))
4765
4766 (defun gnus-group-set-info (info &optional method-only-group part)
4767   (let* ((entry (gnus-gethash
4768                  (or method-only-group (gnus-info-group info))
4769                  gnus-newsrc-hashtb))
4770          (part-info info)
4771          (info (if method-only-group (nth 2 entry) info))
4772          method)
4773     (when method-only-group
4774       (unless entry
4775         (error "Trying to change non-existent group %s" method-only-group))
4776       ;; We have received parts of the actual group info - either the
4777       ;; select method or the group parameters.  We first check
4778       ;; whether we have to extend the info, and if so, do that.
4779       (let ((len (length info))
4780             (total (if (eq part 'method) 5 6)))
4781         (when (< len total)
4782           (setcdr (nthcdr (1- len) info)
4783                   (make-list (- total len) nil)))
4784         ;; Then we enter the new info.
4785         (setcar (nthcdr (1- total) info) part-info)))
4786     (unless entry
4787       ;; This is a new group, so we just create it.
4788       (save-excursion
4789         (set-buffer gnus-group-buffer)
4790         (setq method (gnus-info-method info))
4791         (when (gnus-server-equal method "native")
4792           (setq method nil))
4793         (save-excursion
4794           (set-buffer gnus-group-buffer)
4795           (if method
4796               ;; It's a foreign group...
4797               (gnus-group-make-group
4798                (gnus-group-real-name (gnus-info-group info))
4799                (if (stringp method) method
4800                  (prin1-to-string (car method)))
4801                (and (consp method)
4802                     (nth 1 (gnus-info-method info))))
4803             ;; It's a native group.
4804             (gnus-group-make-group (gnus-info-group info))))
4805         (gnus-message 6 "Note: New group created")
4806         (setq entry
4807               (gnus-gethash (gnus-group-prefixed-name
4808                              (gnus-group-real-name (gnus-info-group info))
4809                              (or (gnus-info-method info) gnus-select-method))
4810                             gnus-newsrc-hashtb))))
4811     ;; Whether it was a new group or not, we now have the entry, so we
4812     ;; can do the update.
4813     (if entry
4814         (progn
4815           (setcar (nthcdr 2 entry) info)
4816           (when (and (not (eq (car entry) t))
4817                      (gnus-active (gnus-info-group info)))
4818             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4819       (error "No such group: %s" (gnus-info-group info)))))
4820
4821 (defun gnus-group-set-method-info (group select-method)
4822   (gnus-group-set-info select-method group 'method))
4823
4824 (defun gnus-group-set-params-info (group params)
4825   (gnus-group-set-info params group 'params))
4826
4827 (defun gnus-group-update-group-line ()
4828   "Update the current line in the group buffer."
4829   (let* ((buffer-read-only nil)
4830          (group (gnus-group-group-name))
4831          (gnus-group-indentation (gnus-group-group-indentation))
4832          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4833     (and entry
4834          (not (gnus-ephemeral-group-p group))
4835          (gnus-dribble-enter
4836           (concat "(gnus-group-set-info '"
4837                   (prin1-to-string (nth 2 entry)) ")")))
4838     (gnus-delete-line)
4839     (gnus-group-insert-group-line-info group)
4840     (forward-line -1)
4841     (gnus-group-position-point)))
4842
4843 (defun gnus-group-insert-group-line-info (group)
4844   "Insert GROUP on the current line."
4845   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4846         active info)
4847     (setq gnus-group-indentation (gnus-group-group-indentation))
4848     (if entry
4849         (progn
4850           ;; (Un)subscribed group.
4851           (setq info (nth 2 entry))
4852           (gnus-group-insert-group-line
4853            group (gnus-info-level info) (gnus-info-marks info)
4854            (or (car entry) t) (gnus-info-method info)))
4855       ;; This group is dead.
4856       (gnus-group-insert-group-line
4857        group
4858        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4859        nil
4860        (if (setq active (gnus-active group))
4861            (- (1+ (cdr active)) (car active)) 0)
4862        nil))))
4863
4864 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4865                                                     gnus-tmp-marked number
4866                                                     gnus-tmp-method)
4867   "Insert a group line in the group buffer."
4868   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4869          (gnus-tmp-number-total
4870           (if gnus-tmp-active
4871               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4872             0))
4873          (gnus-tmp-number-of-unread
4874           (if (numberp number) (int-to-string (max 0 number))
4875             "*"))
4876          (gnus-tmp-number-of-read
4877           (if (numberp number)
4878               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4879             "*"))
4880          (gnus-tmp-subscribed
4881           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4882                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4883                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4884                 (t ?K)))
4885          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4886          (gnus-tmp-newsgroup-description
4887           (if gnus-description-hashtb
4888               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4889             ""))
4890          (gnus-tmp-moderated
4891           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4892          (gnus-tmp-moderated-string
4893           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4894          (gnus-tmp-method
4895           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4896          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4897          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4898          (gnus-tmp-news-method-string
4899           (if gnus-tmp-method
4900               (format "(%s:%s)" (car gnus-tmp-method)
4901                       (cadr gnus-tmp-method)) ""))
4902          (gnus-tmp-marked-mark
4903           (if (and (numberp number)
4904                    (zerop number)
4905                    (cdr (assq 'tick gnus-tmp-marked)))
4906               ?* ? ))
4907          (gnus-tmp-process-marked
4908           (if (member gnus-tmp-group gnus-group-marked)
4909               gnus-process-mark ? ))
4910          (gnus-tmp-grouplens
4911           (or (and gnus-use-grouplens
4912                    (bbb-grouplens-group-p gnus-tmp-group))
4913               ""))
4914          (buffer-read-only nil)
4915          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4916     (beginning-of-line)
4917     (add-text-properties
4918      (point)
4919      (prog1 (1+ (point))
4920        ;; Insert the text.
4921        (eval gnus-group-line-format-spec))
4922      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4923        gnus-unread ,(if (numberp number)
4924                         (string-to-int gnus-tmp-number-of-unread)
4925                       t)
4926        gnus-marked ,gnus-tmp-marked-mark
4927        gnus-indentation ,gnus-group-indentation
4928        gnus-level ,gnus-tmp-level))
4929     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4930       (forward-line -1)
4931       (run-hooks 'gnus-group-update-hook)
4932       (forward-line))
4933     ;; Allow XEmacs to remove front-sticky text properties.
4934     (gnus-group-remove-excess-properties)))
4935
4936 (defun gnus-group-update-group (group &optional visible-only)
4937   "Update all lines where GROUP appear.
4938 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4939 already."
4940   (save-excursion
4941     (set-buffer gnus-group-buffer)
4942     ;; The buffer may be narrowed.
4943     (save-restriction
4944       (widen)
4945       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4946             (loc (point-min))
4947             found buffer-read-only)
4948         ;; Enter the current status into the dribble buffer.
4949         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4950           (if (and entry (not (gnus-ephemeral-group-p group)))
4951               (gnus-dribble-enter
4952                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4953                        ")"))))
4954         ;; Find all group instances.  If topics are in use, each group
4955         ;; may be listed in more than once.
4956         (while (setq loc (text-property-any
4957                           loc (point-max) 'gnus-group ident))
4958           (setq found t)
4959           (goto-char loc)
4960           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4961             (gnus-delete-line)
4962             (gnus-group-insert-group-line-info group))
4963           (setq loc (1+ loc)))
4964         (unless (or found visible-only)
4965           ;; No such line in the buffer, find out where it's supposed to
4966           ;; go, and insert it there (or at the end of the buffer).
4967           (if gnus-goto-missing-group-function
4968               (funcall gnus-goto-missing-group-function group)
4969             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4970               (while (and entry (car entry)
4971                           (not
4972                            (gnus-goto-char
4973                             (text-property-any
4974                              (point-min) (point-max)
4975                              'gnus-group (gnus-intern-safe
4976                                           (caar entry) gnus-active-hashtb)))))
4977                 (setq entry (cdr entry)))
4978               (or entry (goto-char (point-max)))))
4979           ;; Finally insert the line.
4980           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4981             (gnus-group-insert-group-line-info group)))
4982         (gnus-group-set-mode-line)))))
4983
4984 (defun gnus-group-set-mode-line ()
4985   (when (memq 'group gnus-updated-mode-lines)
4986     (let* ((gformat (or gnus-group-mode-line-format-spec
4987                         (setq gnus-group-mode-line-format-spec
4988                               (gnus-parse-format
4989                                gnus-group-mode-line-format
4990                                gnus-group-mode-line-format-alist))))
4991            (gnus-tmp-news-server (cadr gnus-select-method))
4992            (gnus-tmp-news-method (car gnus-select-method))
4993            (max-len 60)
4994            gnus-tmp-header                      ;Dummy binding for user-defined formats
4995            ;; Get the resulting string.
4996            (mode-string (eval gformat)))
4997       ;; If the line is too long, we chop it off.
4998       (when (> (length mode-string) max-len)
4999         (setq mode-string (substring mode-string 0 (- max-len 4))))
5000       (prog1
5001           (setq mode-line-buffer-identification (list mode-string))
5002         (set-buffer-modified-p t)))))
5003
5004 (defun gnus-group-group-name ()
5005   "Get the name of the newsgroup on the current line."
5006   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5007     (and group (symbol-name group))))
5008
5009 (defun gnus-group-group-level ()
5010   "Get the level of the newsgroup on the current line."
5011   (get-text-property (gnus-point-at-bol) 'gnus-level))
5012
5013 (defun gnus-group-group-indentation ()
5014   "Get the indentation of the newsgroup on the current line."
5015   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5016       (and gnus-group-indentation-function
5017            (funcall gnus-group-indentation-function))
5018       ""))
5019
5020 (defun gnus-group-group-unread ()
5021   "Get the number of unread articles of the newsgroup on the current line."
5022   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5023
5024 (defun gnus-group-search-forward (&optional backward all level first-too)
5025   "Find the next newsgroup with unread articles.
5026 If BACKWARD is non-nil, find the previous newsgroup instead.
5027 If ALL is non-nil, just find any newsgroup.
5028 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5029 group exists.
5030 If FIRST-TOO, the current line is also eligible as a target."
5031   (let ((way (if backward -1 1))
5032         (low gnus-level-killed)
5033         (beg (point))
5034         pos found lev)
5035     (if (and backward (progn (beginning-of-line)) (bobp))
5036         nil
5037       (or first-too (forward-line way))
5038       (while (and
5039               (not (eobp))
5040               (not (setq
5041                     found
5042                     (and (or all
5043                              (and
5044                               (let ((unread
5045                                      (get-text-property (point) 'gnus-unread)))
5046                                 (and (numberp unread) (> unread 0)))
5047                               (setq lev (get-text-property (point)
5048                                                            'gnus-level))
5049                               (<= lev gnus-level-subscribed)))
5050                          (or (not level)
5051                              (and (setq lev (get-text-property (point)
5052                                                                'gnus-level))
5053                                   (or (= lev level)
5054                                       (and (< lev low)
5055                                            (< level lev)
5056                                            (progn
5057                                              (setq low lev)
5058                                              (setq pos (point))
5059                                              nil))))))))
5060               (zerop (forward-line way)))))
5061     (if found
5062         (progn (gnus-group-position-point) t)
5063       (goto-char (or pos beg))
5064       (and pos t))))
5065
5066 ;;; Gnus group mode commands
5067
5068 ;; Group marking.
5069
5070 (defun gnus-group-mark-group (n &optional unmark no-advance)
5071   "Mark the current group."
5072   (interactive "p")
5073   (let ((buffer-read-only nil)
5074         group)
5075     (while
5076         (and (> n 0)
5077              (setq group (gnus-group-group-name))
5078              (progn
5079                (beginning-of-line)
5080                (forward-char
5081                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5082                (delete-char 1)
5083                (if unmark
5084                    (progn
5085                      (insert " ")
5086                      (setq gnus-group-marked (delete group gnus-group-marked)))
5087                  (insert "#")
5088                  (setq gnus-group-marked
5089                        (cons group (delete group gnus-group-marked))))
5090                t)
5091              (or no-advance (zerop (gnus-group-next-group 1))))
5092       (setq n (1- n)))
5093     (gnus-summary-position-point)
5094     n))
5095
5096 (defun gnus-group-unmark-group (n)
5097   "Remove the mark from the current group."
5098   (interactive "p")
5099   (gnus-group-mark-group n 'unmark)
5100   (gnus-group-position-point))
5101
5102 (defun gnus-group-unmark-all-groups ()
5103   "Unmark all groups."
5104   (interactive)
5105   (let ((groups gnus-group-marked))
5106     (save-excursion
5107       (while groups
5108         (gnus-group-remove-mark (pop groups)))))
5109   (gnus-group-position-point))
5110
5111 (defun gnus-group-mark-region (unmark beg end)
5112   "Mark all groups between point and mark.
5113 If UNMARK, remove the mark instead."
5114   (interactive "P\nr")
5115   (let ((num (count-lines beg end)))
5116     (save-excursion
5117       (goto-char beg)
5118       (- num (gnus-group-mark-group num unmark)))))
5119
5120 (defun gnus-group-mark-buffer (&optional unmark)
5121   "Mark all groups in the buffer.
5122 If UNMARK, remove the mark instead."
5123   (interactive "P")
5124   (gnus-group-mark-region unmark (point-min) (point-max)))
5125
5126 (defun gnus-group-mark-regexp (regexp)
5127   "Mark all groups that match some regexp."
5128   (interactive "sMark (regexp): ")
5129   (let ((alist (cdr gnus-newsrc-alist))
5130         group)
5131     (while alist
5132       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5133         (gnus-group-set-mark group))))
5134   (gnus-group-position-point))
5135
5136 (defun gnus-group-remove-mark (group)
5137   "Remove the process mark from GROUP and move point there.
5138 Return nil if the group isn't displayed."
5139   (if (gnus-group-goto-group group)
5140       (save-excursion
5141         (gnus-group-mark-group 1 'unmark t)
5142         t)
5143     (setq gnus-group-marked
5144           (delete group gnus-group-marked))
5145     nil))
5146
5147 (defun gnus-group-set-mark (group)
5148   "Set the process mark on GROUP."
5149   (if (gnus-group-goto-group group) 
5150       (save-excursion
5151         (gnus-group-mark-group 1 nil t))
5152     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5153
5154 (defun gnus-group-universal-argument (arg &optional groups func)
5155   "Perform any command on all groups accoring to the process/prefix convention."
5156   (interactive "P")
5157   (let ((groups (or groups (gnus-group-process-prefix arg)))
5158         group func)
5159     (if (eq (setq func (or func
5160                            (key-binding
5161                             (read-key-sequence
5162                              (substitute-command-keys
5163                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5164             'undefined)
5165         (gnus-error 1 "Undefined key")
5166       (while groups
5167         (gnus-group-remove-mark (setq group (pop groups)))
5168         (command-execute func))))
5169   (gnus-group-position-point))
5170
5171 (defun gnus-group-process-prefix (n)
5172   "Return a list of groups to work on.
5173 Take into consideration N (the prefix) and the list of marked groups."
5174   (cond
5175    (n
5176     (setq n (prefix-numeric-value n))
5177     ;; There is a prefix, so we return a list of the N next
5178     ;; groups.
5179     (let ((way (if (< n 0) -1 1))
5180           (n (abs n))
5181           group groups)
5182       (save-excursion
5183         (while (and (> n 0)
5184                     (setq group (gnus-group-group-name)))
5185           (setq groups (cons group groups))
5186           (setq n (1- n))
5187           (gnus-group-next-group way)))
5188       (nreverse groups)))
5189    ((and (boundp 'transient-mark-mode)
5190          transient-mark-mode
5191          (boundp 'mark-active)
5192          mark-active)
5193     ;; Work on the region between point and mark.
5194     (let ((max (max (point) (mark)))
5195           groups)
5196       (save-excursion
5197         (goto-char (min (point) (mark)))
5198         (while
5199             (and
5200              (push (gnus-group-group-name) groups)
5201              (zerop (gnus-group-next-group 1))
5202              (< (point) max)))
5203         (nreverse groups))))
5204    (gnus-group-marked
5205     ;; No prefix, but a list of marked articles.
5206     (reverse gnus-group-marked))
5207    (t
5208     ;; Neither marked articles or a prefix, so we return the
5209     ;; current group.
5210     (let ((group (gnus-group-group-name)))
5211       (and group (list group))))))
5212
5213 ;; Selecting groups.
5214
5215 (defun gnus-group-read-group (&optional all no-article group)
5216   "Read news in this newsgroup.
5217 If the prefix argument ALL is non-nil, already read articles become
5218 readable.  IF ALL is a number, fetch this number of articles.  If the
5219 optional argument NO-ARTICLE is non-nil, no article will be
5220 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5221 group."
5222   (interactive "P")
5223   (let ((group (or group (gnus-group-group-name)))
5224         number active marked entry)
5225     (or group (error "No group on current line"))
5226     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5227                                             group gnus-newsrc-hashtb)))))
5228     ;; This group might be a dead group.  In that case we have to get
5229     ;; the number of unread articles from `gnus-active-hashtb'.
5230     (setq number
5231           (cond ((numberp all) all)
5232                 (entry (car entry))
5233                 ((setq active (gnus-active group))
5234                  (- (1+ (cdr active)) (car active)))))
5235     (gnus-summary-read-group
5236      group (or all (and (numberp number)
5237                         (zerop (+ number (length (cdr (assq 'tick marked)))
5238                                   (length (cdr (assq 'dormant marked)))))))
5239      no-article)))
5240
5241 (defun gnus-group-select-group (&optional all)
5242   "Select this newsgroup.
5243 No article is selected automatically.
5244 If ALL is non-nil, already read articles become readable.
5245 If ALL is a number, fetch this number of articles."
5246   (interactive "P")
5247   (gnus-group-read-group all t))
5248
5249 (defun gnus-group-quick-select-group (&optional all)
5250   "Select the current group \"quickly\".
5251 This means that no highlighting or scoring will be performed."
5252   (interactive "P")
5253   (let (gnus-visual
5254         gnus-score-find-score-files-function
5255         gnus-apply-kill-hook
5256         gnus-summary-expunge-below)
5257     (gnus-group-read-group all t)))
5258
5259 (defun gnus-group-visible-select-group (&optional all)
5260   "Select the current group without hiding any articles."
5261   (interactive "P")
5262   (let ((gnus-inhibit-limiting t))
5263     (gnus-group-read-group all t)))
5264
5265 ;;;###autoload
5266 (defun gnus-fetch-group (group)
5267   "Start Gnus if necessary and enter GROUP.
5268 Returns whether the fetching was successful or not."
5269   (interactive "sGroup name: ")
5270   (or (get-buffer gnus-group-buffer)
5271       (gnus))
5272   (gnus-group-read-group nil nil group))
5273
5274 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5275 ;; if selection was successful.
5276 (defun gnus-group-read-ephemeral-group
5277   (group method &optional activate quit-config)
5278   (let ((group (if (gnus-group-foreign-p group) group
5279                  (gnus-group-prefixed-name group method))))
5280     (gnus-sethash
5281      group
5282      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5283                      ((quit-config . ,(if quit-config quit-config
5284                                         (cons (current-buffer) 'summary))))))
5285      gnus-newsrc-hashtb)
5286     (set-buffer gnus-group-buffer)
5287     (or (gnus-check-server method)
5288         (error "Unable to contact server: %s" (gnus-status-message method)))
5289     (if activate (or (gnus-request-group group)
5290                      (error "Couldn't request group")))
5291     (condition-case ()
5292         (gnus-group-read-group t t group)
5293       (error nil)
5294       (quit nil))))
5295
5296 (defun gnus-group-jump-to-group (group)
5297   "Jump to newsgroup GROUP."
5298   (interactive
5299    (list (completing-read
5300           "Group: " gnus-active-hashtb nil
5301           (memq gnus-select-method gnus-have-read-active-file)
5302           nil
5303           'gnus-group-history)))
5304
5305   (when (equal group "")
5306     (error "Empty group name"))
5307
5308   (when (string-match "[\000-\032]" group)
5309     (error "Control characters in group: %s" group))
5310
5311   (let ((b (text-property-any
5312             (point-min) (point-max)
5313             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5314     (unless (gnus-ephemeral-group-p group)
5315       (if b
5316           ;; Either go to the line in the group buffer...
5317           (goto-char b)
5318         ;; ... or insert the line.
5319         (or
5320          (gnus-active group)
5321          (gnus-activate-group group)
5322          (error "%s error: %s" group (gnus-status-message group)))
5323
5324         (gnus-group-update-group group)
5325         (goto-char (text-property-any
5326                     (point-min) (point-max)
5327                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5328     ;; Adjust cursor point.
5329     (gnus-group-position-point)))
5330
5331 (defun gnus-group-goto-group (group)
5332   "Goto to newsgroup GROUP."
5333   (when group
5334     (let ((b (text-property-any (point-min) (point-max)
5335                                 'gnus-group (gnus-intern-safe
5336                                              group gnus-active-hashtb))))
5337       (and b (goto-char b)))))
5338
5339 (defun gnus-group-next-group (n)
5340   "Go to next N'th newsgroup.
5341 If N is negative, search backward instead.
5342 Returns the difference between N and the number of skips actually
5343 done."
5344   (interactive "p")
5345   (gnus-group-next-unread-group n t))
5346
5347 (defun gnus-group-next-unread-group (n &optional all level)
5348   "Go to next N'th unread newsgroup.
5349 If N is negative, search backward instead.
5350 If ALL is non-nil, choose any newsgroup, unread or not.
5351 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5352 such group can be found, the next group with a level higher than
5353 LEVEL.
5354 Returns the difference between N and the number of skips actually
5355 made."
5356   (interactive "p")
5357   (let ((backward (< n 0))
5358         (n (abs n)))
5359     (while (and (> n 0)
5360                 (gnus-group-search-forward
5361                  backward (or (not gnus-group-goto-unread) all) level))
5362       (setq n (1- n)))
5363     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5364                                (if level " on this level or higher" "")))
5365     n))
5366
5367 (defun gnus-group-prev-group (n)
5368   "Go to previous N'th newsgroup.
5369 Returns the difference between N and the number of skips actually
5370 done."
5371   (interactive "p")
5372   (gnus-group-next-unread-group (- n) t))
5373
5374 (defun gnus-group-prev-unread-group (n)
5375   "Go to previous N'th unread newsgroup.
5376 Returns the difference between N and the number of skips actually
5377 done."
5378   (interactive "p")
5379   (gnus-group-next-unread-group (- n)))
5380
5381 (defun gnus-group-next-unread-group-same-level (n)
5382   "Go to next N'th unread newsgroup on the same level.
5383 If N is negative, search backward instead.
5384 Returns the difference between N and the number of skips actually
5385 done."
5386   (interactive "p")
5387   (gnus-group-next-unread-group n t (gnus-group-group-level))
5388   (gnus-group-position-point))
5389
5390 (defun gnus-group-prev-unread-group-same-level (n)
5391   "Go to next N'th unread newsgroup on the same level.
5392 Returns the difference between N and the number of skips actually
5393 done."
5394   (interactive "p")
5395   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5396   (gnus-group-position-point))
5397
5398 (defun gnus-group-best-unread-group (&optional exclude-group)
5399   "Go to the group with the highest level.
5400 If EXCLUDE-GROUP, do not go to that group."
5401   (interactive)
5402   (goto-char (point-min))
5403   (let ((best 100000)
5404         unread best-point)
5405     (while (not (eobp))
5406       (setq unread (get-text-property (point) 'gnus-unread))
5407       (if (and (numberp unread) (> unread 0))
5408           (progn
5409             (if (and (get-text-property (point) 'gnus-level)
5410                      (< (get-text-property (point) 'gnus-level) best)
5411                      (or (not exclude-group)
5412                          (not (equal exclude-group (gnus-group-group-name)))))
5413                 (progn
5414                   (setq best (get-text-property (point) 'gnus-level))
5415                   (setq best-point (point))))))
5416       (forward-line 1))
5417     (if best-point (goto-char best-point))
5418     (gnus-summary-position-point)
5419     (and best-point (gnus-group-group-name))))
5420
5421 (defun gnus-group-first-unread-group ()
5422   "Go to the first group with unread articles."
5423   (interactive)
5424   (prog1
5425       (let ((opoint (point))
5426             unread)
5427         (goto-char (point-min))
5428         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5429                 (and (numberp unread)   ; Not a topic.
5430                      (not (zerop unread))) ; Has unread articles.
5431                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5432             (point)                     ; Success.
5433           (goto-char opoint)
5434           nil))                         ; Not success.
5435     (gnus-group-position-point)))
5436
5437 (defun gnus-group-enter-server-mode ()
5438   "Jump to the server buffer."
5439   (interactive)
5440   (gnus-enter-server-buffer))
5441
5442 (defun gnus-group-make-group (name &optional method address)
5443   "Add a new newsgroup.
5444 The user will be prompted for a NAME, for a select METHOD, and an
5445 ADDRESS."
5446   (interactive
5447    (cons
5448     (read-string "Group name: ")
5449     (let ((method
5450            (completing-read
5451             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5452             nil t nil 'gnus-method-history)))
5453       (cond ((assoc method gnus-valid-select-methods)
5454              (list method
5455                    (if (memq 'prompt-address
5456                              (assoc method gnus-valid-select-methods))
5457                        (read-string "Address: ")
5458                      "")))
5459             ((assoc method gnus-server-alist)
5460              (list method))
5461             (t
5462              (list method ""))))))
5463
5464   (let* ((meth (and method (if address (list (intern method) address)
5465                              method)))
5466          (nname (if method (gnus-group-prefixed-name name meth) name))
5467          backend info)
5468     (when (gnus-gethash nname gnus-newsrc-hashtb)
5469       (error "Group %s already exists" nname))
5470     ;; Subscribe to the new group.
5471     (gnus-group-change-level
5472      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5473      gnus-level-default-subscribed gnus-level-killed
5474      (and (gnus-group-group-name)
5475           (gnus-gethash (gnus-group-group-name)
5476                         gnus-newsrc-hashtb))
5477      t)
5478     ;; Make it active.
5479     (gnus-set-active nname (cons 1 0))
5480     (or (gnus-ephemeral-group-p name)
5481         (gnus-dribble-enter
5482          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5483     ;; Insert the line.
5484     (gnus-group-insert-group-line-info nname)
5485     (forward-line -1)
5486     (gnus-group-position-point)
5487
5488     ;; Load the backend and try to make the backend create
5489     ;; the group as well.
5490     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5491                                                   nil meth))))
5492                  gnus-valid-select-methods)
5493       (require backend))
5494     (gnus-check-server meth)
5495     (and (gnus-check-backend-function 'request-create-group nname)
5496          (gnus-request-create-group nname))
5497     t))
5498
5499 (defun gnus-group-delete-group (group &optional force)
5500   "Delete the current group.
5501 If FORCE (the prefix) is non-nil, all the articles in the group will
5502 be deleted.  This is \"deleted\" as in \"removed forever from the face
5503 of the Earth\".  There is no undo."
5504   (interactive
5505    (list (gnus-group-group-name)
5506          current-prefix-arg))
5507   (or group (error "No group to rename"))
5508   (or (gnus-check-backend-function 'request-delete-group group)
5509       (error "This backend does not support group deletion"))
5510   (prog1
5511       (if (not (gnus-yes-or-no-p
5512                 (format
5513                  "Do you really want to delete %s%s? "
5514                  group (if force " and all its contents" ""))))
5515           () ; Whew!
5516         (gnus-message 6 "Deleting group %s..." group)
5517         (if (not (gnus-request-delete-group group force))
5518             (gnus-error 3 "Couldn't delete group %s" group)
5519           (gnus-message 6 "Deleting group %s...done" group)
5520           (gnus-group-goto-group group)
5521           (gnus-group-kill-group 1 t)
5522           (gnus-sethash group nil gnus-active-hashtb)
5523           t))
5524     (gnus-group-position-point)))
5525
5526 (defun gnus-group-rename-group (group new-name)
5527   (interactive
5528    (list
5529     (gnus-group-group-name)
5530     (progn
5531       (or (gnus-check-backend-function
5532            'request-rename-group (gnus-group-group-name))
5533           (error "This backend does not support renaming groups"))
5534       (read-string "New group name: "))))
5535
5536   (or (gnus-check-backend-function 'request-rename-group group)
5537       (error "This backend does not support renaming groups"))
5538
5539   (or group (error "No group to rename"))
5540   (and (string-match "^[ \t]*$" new-name)
5541        (error "Not a valid group name"))
5542
5543   ;; We find the proper prefixed name.
5544   (setq new-name
5545         (gnus-group-prefixed-name
5546          (gnus-group-real-name new-name)
5547          (gnus-info-method (gnus-get-info group))))
5548
5549   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5550   (prog1
5551       (if (not (gnus-request-rename-group group new-name))
5552           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5553         ;; We rename the group internally by killing it...
5554         (gnus-group-goto-group group)
5555         (gnus-group-kill-group)
5556         ;; ... changing its name ...
5557         (setcar (cdar gnus-list-of-killed-groups) new-name)
5558         ;; ... and then yanking it.  Magic!
5559         (gnus-group-yank-group)
5560         (gnus-set-active new-name (gnus-active group))
5561         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5562         new-name)
5563     (gnus-group-position-point)))
5564
5565 (defun gnus-group-edit-group (group &optional part)
5566   "Edit the group on the current line."
5567   (interactive (list (gnus-group-group-name)))
5568   (let* ((part (or part 'info))
5569          (done-func `(lambda ()
5570                        "Exit editing mode and update the information."
5571                        (interactive)
5572                        (gnus-group-edit-group-done ',part ,group)))
5573          (winconf (current-window-configuration))
5574          info)
5575     (or group (error "No group on current line"))
5576     (or (setq info (gnus-get-info group))
5577         (error "Killed group; can't be edited"))
5578     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5579     (gnus-configure-windows 'edit-group)
5580     (gnus-add-current-to-buffer-list)
5581     (emacs-lisp-mode)
5582     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5583     (use-local-map (copy-keymap emacs-lisp-mode-map))
5584     (local-set-key "\C-c\C-c" done-func)
5585     (make-local-variable 'gnus-prev-winconf)
5586     (setq gnus-prev-winconf winconf)
5587     (erase-buffer)
5588     (insert
5589      (cond
5590       ((eq part 'method)
5591        ";; Type `C-c C-c' after editing the select method.\n\n")
5592       ((eq part 'params)
5593        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5594       ((eq part 'info)
5595        ";; Type `C-c C-c' after editing the group info.\n\n")))
5596     (insert
5597      (pp-to-string
5598       (cond ((eq part 'method)
5599              (or (gnus-info-method info) "native"))
5600             ((eq part 'params)
5601              (gnus-info-params info))
5602             (t info)))
5603      "\n")))
5604
5605 (defun gnus-group-edit-group-method (group)
5606   "Edit the select method of GROUP."
5607   (interactive (list (gnus-group-group-name)))
5608   (gnus-group-edit-group group 'method))
5609
5610 (defun gnus-group-edit-group-parameters (group)
5611   "Edit the group parameters of GROUP."
5612   (interactive (list (gnus-group-group-name)))
5613   (gnus-group-edit-group group 'params))
5614
5615 (defun gnus-group-edit-group-done (part group)
5616   "Get info from buffer, update variables and jump to the group buffer."
5617   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5618   (goto-char (point-min))
5619   (let* ((form (read (current-buffer)))
5620          (winconf gnus-prev-winconf)
5621          (method (cond ((eq part 'info) (nth 4 form))
5622                        ((eq part 'method) form)
5623                        (t nil)))
5624          (info (cond ((eq part 'info) form)
5625                      ((eq part 'method) (gnus-get-info group))
5626                      (t nil)))
5627          (new-group (if info
5628                       (if (or (not method)
5629                               (gnus-server-equal
5630                                gnus-select-method method))
5631                           (gnus-group-real-name (car info))
5632                         (gnus-group-prefixed-name
5633                          (gnus-group-real-name (car info)) method))
5634                       nil)))
5635     (when (and new-group
5636                (not (equal new-group group)))
5637       (when (gnus-group-goto-group group)
5638         (gnus-group-kill-group 1))
5639       (gnus-activate-group new-group))
5640     ;; Set the info.
5641     (if (and info new-group)
5642         (progn
5643           (setq info (gnus-copy-sequence info))
5644           (setcar info new-group)
5645           (unless (gnus-server-equal method "native")
5646             (unless (nthcdr 3 info)
5647               (nconc info (list nil nil)))
5648             (unless (nthcdr 4 info)
5649               (nconc info (list nil)))
5650             (gnus-info-set-method info method))
5651           (gnus-group-set-info info))
5652       (gnus-group-set-info form (or new-group group) part))
5653     (kill-buffer (current-buffer))
5654     (and winconf (set-window-configuration winconf))
5655     (set-buffer gnus-group-buffer)
5656     (gnus-group-update-group (or new-group group))
5657     (gnus-group-position-point)))
5658
5659 (defun gnus-group-make-help-group ()
5660   "Create the Gnus documentation group."
5661   (interactive)
5662   (let ((path load-path)
5663         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5664         file dir)
5665     (and (gnus-gethash name gnus-newsrc-hashtb)
5666          (error "Documentation group already exists"))
5667     (while path
5668       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5669             file nil)
5670       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5671                 (file-exists-p
5672                  (setq file (concat (file-name-directory
5673                                      (directory-file-name dir))
5674                                     "etc/gnus-tut.txt"))))
5675         (setq path nil)))
5676     (if (not file)
5677         (gnus-message 1 "Couldn't find doc group")
5678       (gnus-group-make-group
5679        (gnus-group-real-name name)
5680        (list 'nndoc "gnus-help"
5681              (list 'nndoc-address file)
5682              (list 'nndoc-article-type 'mbox)))))
5683   (gnus-group-position-point))
5684
5685 (defun gnus-group-make-doc-group (file type)
5686   "Create a group that uses a single file as the source."
5687   (interactive
5688    (list (read-file-name "File name: ")
5689          (and current-prefix-arg 'ask)))
5690   (when (eq type 'ask)
5691     (let ((err "")
5692           char found)
5693       (while (not found)
5694         (message
5695          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5696          err)
5697         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5698                           ((= char ?b) 'babyl)
5699                           ((= char ?d) 'digest)
5700                           ((= char ?f) 'forward)
5701                           ((= char ?a) 'mmfd)
5702                           (t (setq err (format "%c unknown. " char))
5703                              nil))))
5704       (setq type found)))
5705   (let* ((file (expand-file-name file))
5706          (name (gnus-generate-new-group-name
5707                 (gnus-group-prefixed-name
5708                  (file-name-nondirectory file) '(nndoc "")))))
5709     (gnus-group-make-group
5710      (gnus-group-real-name name)
5711      (list 'nndoc (file-name-nondirectory file)
5712            (list 'nndoc-address file)
5713            (list 'nndoc-article-type (or type 'guess))))))
5714
5715 (defun gnus-group-make-archive-group (&optional all)
5716   "Create the (ding) Gnus archive group of the most recent articles.
5717 Given a prefix, create a full group."
5718   (interactive "P")
5719   (let ((group (gnus-group-prefixed-name
5720                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5721     (and (gnus-gethash group gnus-newsrc-hashtb)
5722          (error "Archive group already exists"))
5723     (gnus-group-make-group
5724      (gnus-group-real-name group)
5725      (list 'nndir (if all "hpc" "edu")
5726            (list 'nndir-directory
5727                  (if all gnus-group-archive-directory
5728                    gnus-group-recent-archive-directory))))))
5729
5730 (defun gnus-group-make-directory-group (dir)
5731   "Create an nndir group.
5732 The user will be prompted for a directory.  The contents of this
5733 directory will be used as a newsgroup.  The directory should contain
5734 mail messages or news articles in files that have numeric names."
5735   (interactive
5736    (list (read-file-name "Create group from directory: ")))
5737   (or (file-exists-p dir) (error "No such directory"))
5738   (or (file-directory-p dir) (error "Not a directory"))
5739   (let ((ext "")
5740         (i 0)
5741         group)
5742     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5743       (setq group
5744             (gnus-group-prefixed-name
5745              (concat (file-name-as-directory (directory-file-name dir))
5746                      ext)
5747              '(nndir "")))
5748       (setq ext (format "<%d>" (setq i (1+ i)))))
5749     (gnus-group-make-group
5750      (gnus-group-real-name group)
5751      (list 'nndir group (list 'nndir-directory dir)))))
5752
5753 (defun gnus-group-make-kiboze-group (group address scores)
5754   "Create an nnkiboze group.
5755 The user will be prompted for a name, a regexp to match groups, and
5756 score file entries for articles to include in the group."
5757   (interactive
5758    (list
5759     (read-string "nnkiboze group name: ")
5760     (read-string "Source groups (regexp): ")
5761     (let ((headers (mapcar (lambda (group) (list group))
5762                            '("subject" "from" "number" "date" "message-id"
5763                              "references" "chars" "lines" "xref"
5764                              "followup" "all" "body" "head")))
5765           scores header regexp regexps)
5766       (while (not (equal "" (setq header (completing-read
5767                                           "Match on header: " headers nil t))))
5768         (setq regexps nil)
5769         (while (not (equal "" (setq regexp (read-string
5770                                             (format "Match on %s (string): "
5771                                                     header)))))
5772           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5773         (setq scores (cons (cons header regexps) scores)))
5774       scores)))
5775   (gnus-group-make-group group "nnkiboze" address)
5776   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5777     (let (emacs-lisp-mode-hook)
5778       (pp scores (current-buffer)))))
5779
5780 (defun gnus-group-add-to-virtual (n vgroup)
5781   "Add the current group to a virtual group."
5782   (interactive
5783    (list current-prefix-arg
5784          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5785                           "nnvirtual:")))
5786   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5787       (error "%s is not an nnvirtual group" vgroup))
5788   (let* ((groups (gnus-group-process-prefix n))
5789          (method (gnus-info-method (gnus-get-info vgroup))))
5790     (setcar (cdr method)
5791             (concat
5792              (nth 1 method) "\\|"
5793              (mapconcat
5794               (lambda (s)
5795                 (gnus-group-remove-mark s)
5796                 (concat "\\(^" (regexp-quote s) "$\\)"))
5797               groups "\\|"))))
5798   (gnus-group-position-point))
5799
5800 (defun gnus-group-make-empty-virtual (group)
5801   "Create a new, fresh, empty virtual group."
5802   (interactive "sCreate new, empty virtual group: ")
5803   (let* ((method (list 'nnvirtual "^$"))
5804          (pgroup (gnus-group-prefixed-name group method)))
5805     ;; Check whether it exists already.
5806     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5807          (error "Group %s already exists." pgroup))
5808     ;; Subscribe the new group after the group on the current line.
5809     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5810     (gnus-group-update-group pgroup)
5811     (forward-line -1)
5812     (gnus-group-position-point)))
5813
5814 (defun gnus-group-enter-directory (dir)
5815   "Enter an ephemeral nneething group."
5816   (interactive "DDirectory to read: ")
5817   (let* ((method (list 'nneething dir))
5818          (leaf (gnus-group-prefixed-name
5819                 (file-name-nondirectory (directory-file-name dir))
5820                 method))
5821          (name (gnus-generate-new-group-name leaf)))
5822     (let ((nneething-read-only t))
5823       (or (gnus-group-read-ephemeral-group
5824            name method t
5825            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5826                                       'summary 'group)))
5827           (error "Couldn't enter %s" dir)))))
5828
5829 ;; Group sorting commands
5830 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5831
5832 (defun gnus-group-sort-groups (func &optional reverse)
5833   "Sort the group buffer according to FUNC.
5834 If REVERSE, reverse the sorting order."
5835   (interactive (list gnus-group-sort-function
5836                      current-prefix-arg))
5837   (let ((func (cond 
5838                ((not (listp func)) func)
5839                ((null func) func)
5840                ((= 1 (length func)) (car func))
5841                (t `(lambda (t1 t2)
5842                      ,(gnus-make-sort-function 
5843                        (reverse func)))))))
5844     ;; We peel off the dummy group from the alist.
5845     (when func
5846       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5847         (pop gnus-newsrc-alist))
5848       ;; Do the sorting.
5849       (setq gnus-newsrc-alist
5850             (sort gnus-newsrc-alist func))
5851       (when reverse
5852         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5853       ;; Regenerate the hash table.
5854       (gnus-make-hashtable-from-newsrc-alist)
5855       (gnus-group-list-groups))))
5856
5857 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5858   "Sort the group buffer alphabetically by group name.
5859 If REVERSE, sort in reverse order."
5860   (interactive "P")
5861   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5862
5863 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5864   "Sort the group buffer by number of unread articles.
5865 If REVERSE, sort in reverse order."
5866   (interactive "P")
5867   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5868
5869 (defun gnus-group-sort-groups-by-level (&optional reverse)
5870   "Sort the group buffer by group level.
5871 If REVERSE, sort in reverse order."
5872   (interactive "P")
5873   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5874
5875 (defun gnus-group-sort-groups-by-score (&optional reverse)
5876   "Sort the group buffer by group score.
5877 If REVERSE, sort in reverse order."
5878   (interactive "P")
5879   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5880
5881 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5882   "Sort the group buffer by group rank.
5883 If REVERSE, sort in reverse order."
5884   (interactive "P")
5885   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5886
5887 (defun gnus-group-sort-groups-by-method (&optional reverse)
5888   "Sort the group buffer alphabetically by backend name.
5889 If REVERSE, sort in reverse order."
5890   (interactive "P")
5891   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5892
5893 (defun gnus-group-sort-by-alphabet (info1 info2)
5894   "Sort alphabetically."
5895   (string< (gnus-info-group info1) (gnus-info-group info2)))
5896
5897 (defun gnus-group-sort-by-unread (info1 info2)
5898   "Sort by number of unread articles."
5899   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5900         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5901     (< (or (and (numberp n1) n1) 0)
5902        (or (and (numberp n2) n2) 0))))
5903
5904 (defun gnus-group-sort-by-level (info1 info2)
5905   "Sort by level."
5906   (< (gnus-info-level info1) (gnus-info-level info2)))
5907
5908 (defun gnus-group-sort-by-method (info1 info2)
5909   "Sort alphabetically by backend name."
5910   (string< (symbol-name (car (gnus-find-method-for-group
5911                               (gnus-info-group info1) info1)))
5912            (symbol-name (car (gnus-find-method-for-group
5913                               (gnus-info-group info2) info2)))))
5914
5915 (defun gnus-group-sort-by-score (info1 info2)
5916   "Sort by group score."
5917   (< (gnus-info-score info1) (gnus-info-score info2)))
5918
5919 (defun gnus-group-sort-by-rank (info1 info2)
5920   "Sort by level and score."
5921   (let ((level1 (gnus-info-level info1))
5922         (level2 (gnus-info-level info2)))
5923     (or (< level1 level2)
5924         (and (= level1 level2)
5925              (< (gnus-info-score info1) (gnus-info-score info2))))))
5926
5927 ;; Group catching up.
5928
5929 (defun gnus-group-clear-data (n)
5930   "Clear all marks and read ranges from the current group."
5931   (interactive "P")
5932   (let ((groups (gnus-group-process-prefix n))
5933         group info)
5934     (while (setq group (pop groups))
5935       (setq info (gnus-get-info group))
5936       (gnus-info-set-read info nil)
5937       (when (gnus-info-marks info)
5938         (gnus-info-set-marks info nil))
5939       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5940       (when (gnus-group-goto-group group)
5941         (gnus-group-remove-mark group)
5942         (gnus-group-update-group-line)))))
5943
5944 (defun gnus-group-catchup-current (&optional n all)
5945   "Mark all articles not marked as unread in current newsgroup as read.
5946 If prefix argument N is numeric, the ARG next newsgroups will be
5947 caught up.  If ALL is non-nil, marked articles will also be marked as
5948 read.  Cross references (Xref: header) of articles are ignored.
5949 The difference between N and actual number of newsgroups that were
5950 caught up is returned."
5951   (interactive "P")
5952   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5953                gnus-expert-user
5954                (gnus-y-or-n-p
5955                 (if all
5956                     "Do you really want to mark all articles as read? "
5957                   "Mark all unread articles as read? "))))
5958       n
5959     (let ((groups (gnus-group-process-prefix n))
5960           (ret 0))
5961       (while groups
5962         ;; Virtual groups have to be given special treatment.
5963         (let ((method (gnus-find-method-for-group (car groups))))
5964           (if (eq 'nnvirtual (car method))
5965               (nnvirtual-catchup-group
5966                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5967         (gnus-group-remove-mark (car groups))
5968         (if (>= (gnus-group-group-level) gnus-level-zombie)
5969             (gnus-message 2 "Dead groups can't be caught up")
5970           (if (prog1
5971                   (gnus-group-goto-group (car groups))
5972                 (gnus-group-catchup (car groups) all))
5973               (gnus-group-update-group-line)
5974             (setq ret (1+ ret))))
5975         (setq groups (cdr groups)))
5976       (gnus-group-next-unread-group 1)
5977       ret)))
5978
5979 (defun gnus-group-catchup-current-all (&optional n)
5980   "Mark all articles in current newsgroup as read.
5981 Cross references (Xref: header) of articles are ignored."
5982   (interactive "P")
5983   (gnus-group-catchup-current n 'all))
5984
5985 (defun gnus-group-catchup (group &optional all)
5986   "Mark all articles in GROUP as read.
5987 If ALL is non-nil, all articles are marked as read.
5988 The return value is the number of articles that were marked as read,
5989 or nil if no action could be taken."
5990   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5991          (num (car entry)))
5992     ;; Do the updating only if the newsgroup isn't killed.
5993     (if (not (numberp (car entry)))
5994         (gnus-message 1 "Can't catch up; non-active group")
5995       ;; Do auto-expirable marks if that's required.
5996       (when (gnus-group-auto-expirable-p group)
5997         (gnus-add-marked-articles
5998          group 'expire (gnus-list-of-unread-articles group))
5999         (when all
6000           (let ((marks (nth 3 (nth 2 entry))))
6001             (gnus-add-marked-articles
6002              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6003             (gnus-add-marked-articles
6004              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6005       (when entry
6006         (gnus-update-read-articles group nil)
6007         ;; Also nix out the lists of marks and dormants.
6008         (when all
6009           (gnus-add-marked-articles group 'tick nil nil 'force)
6010           (gnus-add-marked-articles group 'dormant nil nil 'force))
6011         (run-hooks 'gnus-group-catchup-group-hook)
6012         num))))
6013
6014 (defun gnus-group-expire-articles (&optional n)
6015   "Expire all expirable articles in the current newsgroup."
6016   (interactive "P")
6017   (let ((groups (gnus-group-process-prefix n))
6018         group)
6019     (unless groups
6020       (error "No groups to expire"))
6021     (while (setq group (pop groups))
6022       (gnus-group-remove-mark group)
6023       (when (gnus-check-backend-function 'request-expire-articles group)
6024         (gnus-message 6 "Expiring articles in %s..." group)
6025         (let* ((info (gnus-get-info group))
6026                (expirable (if (gnus-group-total-expirable-p group)
6027                               (cons nil (gnus-list-of-read-articles group))
6028                             (assq 'expire (gnus-info-marks info))))
6029                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6030           (when expirable
6031             (setcdr
6032              expirable
6033              (gnus-compress-sequence
6034               (if expiry-wait
6035                   ;; We set the expiry variables to the groupp
6036                   ;; parameter. 
6037                   (let ((nnmail-expiry-wait-function nil)
6038                         (nnmail-expiry-wait expiry-wait))
6039                     (gnus-request-expire-articles
6040                      (gnus-uncompress-sequence (cdr expirable)) group))
6041                 ;; Just expire using the normal expiry values.
6042                 (gnus-request-expire-articles
6043                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6044           (gnus-message 6 "Expiring articles in %s...done" group)))
6045       (gnus-group-position-point))))
6046
6047 (defun gnus-group-expire-all-groups ()
6048   "Expire all expirable articles in all newsgroups."
6049   (interactive)
6050   (save-excursion
6051     (gnus-message 5 "Expiring...")
6052     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6053                                      (cdr gnus-newsrc-alist))))
6054       (gnus-group-expire-articles nil)))
6055   (gnus-group-position-point)
6056   (gnus-message 5 "Expiring...done"))
6057
6058 (defun gnus-group-set-current-level (n level)
6059   "Set the level of the next N groups to LEVEL."
6060   (interactive
6061    (list
6062     current-prefix-arg
6063     (string-to-int
6064      (let ((s (read-string
6065                (format "Level (default %s): "
6066                        (or (gnus-group-group-level) 
6067                            gnus-level-default-subscribed)))))
6068        (if (string-match "^\\s-*$" s)
6069            (int-to-string (or (gnus-group-group-level) 
6070                               gnus-level-default-subscribed))
6071          s)))))
6072   (or (and (>= level 1) (<= level gnus-level-killed))
6073       (error "Illegal level: %d" level))
6074   (let ((groups (gnus-group-process-prefix n))
6075         group)
6076     (while (setq group (pop groups))
6077       (gnus-group-remove-mark group)
6078       (gnus-message 6 "Changed level of %s from %d to %d"
6079                     group (or (gnus-group-group-level) gnus-level-killed)
6080                     level)
6081       (gnus-group-change-level
6082        group level (or (gnus-group-group-level) gnus-level-killed))
6083       (gnus-group-update-group-line)))
6084   (gnus-group-position-point))
6085
6086 (defun gnus-group-unsubscribe-current-group (&optional n)
6087   "Toggle subscription of the current group.
6088 If given numerical prefix, toggle the N next groups."
6089   (interactive "P")
6090   (let ((groups (gnus-group-process-prefix n))
6091         group)
6092     (while groups
6093       (setq group (car groups)
6094             groups (cdr groups))
6095       (gnus-group-remove-mark group)
6096       (gnus-group-unsubscribe-group
6097        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6098                  gnus-level-default-unsubscribed
6099                gnus-level-default-subscribed) t)
6100       (gnus-group-update-group-line))
6101     (gnus-group-next-group 1)))
6102
6103 (defun gnus-group-unsubscribe-group (group &optional level silent)
6104   "Toggle subscription to GROUP.
6105 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6106 group line."
6107   (interactive
6108    (list (completing-read
6109           "Group: " gnus-active-hashtb nil
6110           (memq gnus-select-method gnus-have-read-active-file)
6111           nil 
6112           'gnus-group-history)))
6113   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6114     (cond
6115      ((string-match "^[ \t]$" group)
6116       (error "Empty group name"))
6117      (newsrc
6118       ;; Toggle subscription flag.
6119       (gnus-group-change-level
6120        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6121                                       gnus-level-subscribed)
6122                                   (1+ gnus-level-subscribed)
6123                                 gnus-level-default-subscribed)))
6124       (unless silent
6125         (gnus-group-update-group group)))
6126      ((and (stringp group)
6127            (or (not (memq gnus-select-method gnus-have-read-active-file))
6128                (gnus-active group)))
6129       ;; Add new newsgroup.
6130       (gnus-group-change-level
6131        group
6132        (if level level gnus-level-default-subscribed)
6133        (or (and (member group gnus-zombie-list)
6134                 gnus-level-zombie)
6135            gnus-level-killed)
6136        (and (gnus-group-group-name)
6137             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6138       (unless silent
6139         (gnus-group-update-group group)))
6140      (t (error "No such newsgroup: %s" group)))
6141     (gnus-group-position-point)))
6142
6143 (defun gnus-group-transpose-groups (n)
6144   "Move the current newsgroup up N places.
6145 If given a negative prefix, move down instead.  The difference between
6146 N and the number of steps taken is returned."
6147   (interactive "p")
6148   (or (gnus-group-group-name)
6149       (error "No group on current line"))
6150   (gnus-group-kill-group 1)
6151   (prog1
6152       (forward-line (- n))
6153     (gnus-group-yank-group)
6154     (gnus-group-position-point)))
6155
6156 (defun gnus-group-kill-all-zombies ()
6157   "Kill all zombie newsgroups."
6158   (interactive)
6159   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6160   (setq gnus-zombie-list nil)
6161   (gnus-group-list-groups))
6162
6163 (defun gnus-group-kill-region (begin end)
6164   "Kill newsgroups in current region (excluding current point).
6165 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6166   (interactive "r")
6167   (let ((lines
6168          ;; Count lines.
6169          (save-excursion
6170            (count-lines
6171             (progn
6172               (goto-char begin)
6173               (beginning-of-line)
6174               (point))
6175             (progn
6176               (goto-char end)
6177               (beginning-of-line)
6178               (point))))))
6179     (goto-char begin)
6180     (beginning-of-line)                 ;Important when LINES < 1
6181     (gnus-group-kill-group lines)))
6182
6183 (defun gnus-group-kill-group (&optional n discard)
6184   "Kill the next N groups.
6185 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6186 However, only groups that were alive can be yanked; already killed
6187 groups or zombie groups can't be yanked.
6188 The return value is the name of the group that was killed, or a list
6189 of groups killed."
6190   (interactive "P")
6191   (let ((buffer-read-only nil)
6192         (groups (gnus-group-process-prefix n))
6193         group entry level out)
6194     (if (< (length groups) 10)
6195         ;; This is faster when there are few groups.
6196         (while groups
6197           (push (setq group (pop groups)) out)
6198           (gnus-group-remove-mark group)
6199           (setq level (gnus-group-group-level))
6200           (gnus-delete-line)
6201           (when (and (not discard)
6202                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6203             (push (cons (car entry) (nth 2 entry))
6204                   gnus-list-of-killed-groups))
6205           (gnus-group-change-level
6206            (if entry entry group) gnus-level-killed (if entry nil level)))
6207       ;; If there are lots and lots of groups to be killed, we use
6208       ;; this thing instead.
6209       (let (entry)
6210         (setq groups (nreverse groups))
6211         (while groups
6212           (gnus-group-remove-mark (setq group (pop groups)))
6213           (gnus-delete-line)
6214           (cond
6215            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6216             (push (cons (car entry) (nth 2 entry))
6217                   gnus-list-of-killed-groups)
6218             (setcdr (cdr entry) (cdddr entry)))
6219            ((member group gnus-zombie-list)
6220             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6221         (gnus-make-hashtable-from-newsrc-alist)))
6222
6223     (gnus-group-position-point)
6224     (if (< (length out) 2) (car out) (nreverse out))))
6225
6226 (defun gnus-group-yank-group (&optional arg)
6227   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6228 inserting it before the current newsgroup.  The numeric ARG specifies
6229 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6230 is returned, or (if several groups are yanked) a list of yanked groups
6231 is returned."
6232   (interactive "p")
6233   (setq arg (or arg 1))
6234   (let (info group prev out)
6235     (while (>= (decf arg) 0)
6236       (if (not (setq info (pop gnus-list-of-killed-groups)))
6237           (error "No more newsgroups to yank"))
6238       (push (setq group (nth 1 info)) out)
6239       ;; Find which newsgroup to insert this one before - search
6240       ;; backward until something suitable is found.  If there are no
6241       ;; other newsgroups in this buffer, just make this newsgroup the
6242       ;; first newsgroup.
6243       (setq prev (gnus-group-group-name))
6244       (gnus-group-change-level
6245        info (gnus-info-level (cdr info)) gnus-level-killed
6246        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6247        t)
6248       (gnus-group-insert-group-line-info group))
6249     (forward-line -1)
6250     (gnus-group-position-point)
6251     (if (< (length out) 2) (car out) (nreverse out))))
6252
6253 (defun gnus-group-kill-level (level)
6254   "Kill all groups that is on a certain LEVEL."
6255   (interactive "nKill all groups on level: ")
6256   (cond
6257    ((= level gnus-level-zombie)
6258     (setq gnus-killed-list
6259           (nconc gnus-zombie-list gnus-killed-list))
6260     (setq gnus-zombie-list nil))
6261    ((and (< level gnus-level-zombie)
6262          (> level 0)
6263          (or gnus-expert-user
6264              (gnus-yes-or-no-p
6265               (format
6266                "Do you really want to kill all groups on level %d? "
6267                level))))
6268     (let* ((prev gnus-newsrc-alist)
6269            (alist (cdr prev)))
6270       (while alist
6271         (if (= (gnus-info-level level) level)
6272             (setcdr prev (cdr alist))
6273           (setq prev alist))
6274         (setq alist (cdr alist)))
6275       (gnus-make-hashtable-from-newsrc-alist)
6276       (gnus-group-list-groups)))
6277    (t
6278     (error "Can't kill; illegal level: %d" level))))
6279
6280 (defun gnus-group-list-all-groups (&optional arg)
6281   "List all newsgroups with level ARG or lower.
6282 Default is gnus-level-unsubscribed, which lists all subscribed and most
6283 unsubscribed groups."
6284   (interactive "P")
6285   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6286
6287 ;; Redefine this to list ALL killed groups if prefix arg used.
6288 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6289 (defun gnus-group-list-killed (&optional arg)
6290   "List all killed newsgroups in the group buffer.
6291 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6292 entail asking the server for the groups."
6293   (interactive "P")
6294   ;; Find all possible killed newsgroups if arg.
6295   (when arg
6296     ;; First make sure active file has been read.
6297     (unless gnus-have-read-active-file
6298       (let ((gnus-read-active-file t))
6299         (gnus-read-active-file)))
6300     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6301     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6302     (mapatoms
6303      (lambda (sym)
6304        (let ((groups 0)
6305              (group (symbol-name sym)))
6306          (if (or (null group)
6307                  (gnus-gethash group gnus-killed-hashtb)
6308                  (gnus-gethash group gnus-newsrc-hashtb))
6309              ()
6310            (let ((do-sub (gnus-matches-options-n group)))
6311              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6312                  ()
6313                (setq groups (1+ groups))
6314                (setq gnus-killed-list
6315                      (cons group gnus-killed-list))
6316                (gnus-sethash group group gnus-killed-hashtb))))))
6317      gnus-active-hashtb))
6318   (if (not gnus-killed-list)
6319       (gnus-message 6 "No killed groups")
6320     (let (gnus-group-list-mode)
6321       (funcall gnus-group-prepare-function
6322                gnus-level-killed t gnus-level-killed))
6323     (goto-char (point-min)))
6324   (gnus-group-position-point))
6325
6326 (defun gnus-group-list-zombies ()
6327   "List all zombie newsgroups in the group buffer."
6328   (interactive)
6329   (if (not gnus-zombie-list)
6330       (gnus-message 6 "No zombie groups")
6331     (let (gnus-group-list-mode)
6332       (funcall gnus-group-prepare-function
6333                gnus-level-zombie t gnus-level-zombie))
6334     (goto-char (point-min)))
6335   (gnus-group-position-point))
6336
6337 (defun gnus-group-list-active ()
6338   "List all groups that are available from the server(s)."
6339   (interactive)
6340   ;; First we make sure that we have really read the active file.
6341   (unless gnus-have-read-active-file
6342     (let ((gnus-read-active-file t))
6343       (gnus-read-active-file)))
6344   ;; Find all groups and sort them.
6345   (let ((groups
6346          (sort
6347           (let (list)
6348             (mapatoms
6349              (lambda (sym)
6350                (and (symbol-value sym)
6351                     (setq list (cons (symbol-name sym) list))))
6352              gnus-active-hashtb)
6353             list)
6354           'string<))
6355         (buffer-read-only nil))
6356     (erase-buffer)
6357     (while groups
6358       (gnus-group-insert-group-line-info (pop groups)))
6359     (goto-char (point-min))))
6360
6361 (defun gnus-activate-all-groups (level)
6362   "Activate absolutely all groups."
6363   (interactive (list 7))
6364   (let ((gnus-activate-level level)
6365         (gnus-activate-foreign-newsgroups level))
6366     (gnus-group-get-new-news)))
6367
6368 (defun gnus-group-get-new-news (&optional arg)
6369   "Get newly arrived articles.
6370 If ARG is a number, it specifies which levels you are interested in
6371 re-scanning.  If ARG is non-nil and not a number, this will force
6372 \"hard\" re-reading of the active files from all servers."
6373   (interactive "P")
6374   (run-hooks 'gnus-get-new-news-hook)
6375   ;; We might read in new NoCeM messages here.
6376   (when (and gnus-use-nocem 
6377              (null arg))
6378     (gnus-nocem-scan-groups))
6379   ;; If ARG is not a number, then we read the active file.
6380   (when (and arg (not (numberp arg)))
6381     (let ((gnus-read-active-file t))
6382       (gnus-read-active-file))
6383     (setq arg nil))
6384
6385   (setq arg (gnus-group-default-level arg t))
6386   (if (and gnus-read-active-file (not arg))
6387       (progn
6388         (gnus-read-active-file)
6389         (gnus-get-unread-articles arg))
6390     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6391       (gnus-get-unread-articles arg)))
6392   (run-hooks 'gnus-after-getting-new-news-hook)
6393   (gnus-group-list-groups))
6394
6395 (defun gnus-group-get-new-news-this-group (&optional n)
6396   "Check for newly arrived news in the current group (and the N-1 next groups).
6397 The difference between N and the number of newsgroup checked is returned.
6398 If N is negative, this group and the N-1 previous groups will be checked."
6399   (interactive "P")
6400   (let* ((groups (gnus-group-process-prefix n))
6401          (ret (if (numberp n) (- n (length groups)) 0))
6402          (beg (unless n (point)))
6403          group)
6404     (while (setq group (pop groups))
6405       (gnus-group-remove-mark group)
6406       (if (gnus-activate-group group 'scan)
6407           (progn
6408             (gnus-get-unread-articles-in-group
6409              (gnus-get-info group) (gnus-active group) t)
6410             (unless (gnus-virtual-group-p group)
6411               (gnus-close-group group))
6412             (gnus-group-update-group group))
6413         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6414     (when beg (goto-char beg))
6415     (when gnus-goto-next-group-when-activating
6416       (gnus-group-next-unread-group 1 t))
6417     (gnus-summary-position-point)
6418     ret))
6419
6420 (defun gnus-group-fetch-faq (group &optional faq-dir)
6421   "Fetch the FAQ for the current group."
6422   (interactive
6423    (list
6424     (gnus-group-real-name (gnus-group-group-name))
6425     (cond (current-prefix-arg
6426            (completing-read
6427             "Faq dir: " (and (listp gnus-group-faq-directory)
6428                              gnus-group-faq-directory))))))
6429   (or faq-dir
6430       (setq faq-dir (if (listp gnus-group-faq-directory)
6431                         (car gnus-group-faq-directory)
6432                       gnus-group-faq-directory)))
6433   (or group (error "No group name given"))
6434   (let ((file (concat (file-name-as-directory faq-dir)
6435                       (gnus-group-real-name group))))
6436     (if (not (file-exists-p file))
6437         (error "No such file: %s" file)
6438       (find-file file))))
6439
6440 (defun gnus-group-describe-group (force &optional group)
6441   "Display a description of the current newsgroup."
6442   (interactive (list current-prefix-arg (gnus-group-group-name)))
6443   (and force (setq gnus-description-hashtb nil))
6444   (let ((method (gnus-find-method-for-group group))
6445         desc)
6446     (or group (error "No group name given"))
6447     (and (or (and gnus-description-hashtb
6448                   ;; We check whether this group's method has been
6449                   ;; queried for a description file.
6450                   (gnus-gethash
6451                    (gnus-group-prefixed-name "" method)
6452                    gnus-description-hashtb))
6453              (setq desc (gnus-group-get-description group))
6454              (gnus-read-descriptions-file method))
6455          (gnus-message 1
6456           (or desc (gnus-gethash group gnus-description-hashtb)
6457               "No description available")))))
6458
6459 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6460 (defun gnus-group-describe-all-groups (&optional force)
6461   "Pop up a buffer with descriptions of all newsgroups."
6462   (interactive "P")
6463   (and force (setq gnus-description-hashtb nil))
6464   (if (not (or gnus-description-hashtb
6465                (gnus-read-all-descriptions-files)))
6466       (error "Couldn't request descriptions file"))
6467   (let ((buffer-read-only nil)
6468         b)
6469     (erase-buffer)
6470     (mapatoms
6471      (lambda (group)
6472        (setq b (point))
6473        (insert (format "      *: %-20s %s\n" (symbol-name group)
6474                        (symbol-value group)))
6475        (add-text-properties
6476         b (1+ b) (list 'gnus-group group
6477                        'gnus-unread t 'gnus-marked nil
6478                        'gnus-level (1+ gnus-level-subscribed))))
6479      gnus-description-hashtb)
6480     (goto-char (point-min))
6481     (gnus-group-position-point)))
6482
6483 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6484 (defun gnus-group-apropos (regexp &optional search-description)
6485   "List all newsgroups that have names that match a regexp."
6486   (interactive "sGnus apropos (regexp): ")
6487   (let ((prev "")
6488         (obuf (current-buffer))
6489         groups des)
6490     ;; Go through all newsgroups that are known to Gnus.
6491     (mapatoms
6492      (lambda (group)
6493        (and (symbol-name group)
6494             (string-match regexp (symbol-name group))
6495             (setq groups (cons (symbol-name group) groups))))
6496      gnus-active-hashtb)
6497     ;; Also go through all descriptions that are known to Gnus.
6498     (when search-description
6499       (mapatoms
6500        (lambda (group)
6501          (and (string-match regexp (symbol-value group))
6502               (gnus-active (symbol-name group))
6503               (setq groups (cons (symbol-name group) groups))))
6504        gnus-description-hashtb))
6505     (if (not groups)
6506         (gnus-message 3 "No groups matched \"%s\"." regexp)
6507       ;; Print out all the groups.
6508       (save-excursion
6509         (pop-to-buffer "*Gnus Help*")
6510         (buffer-disable-undo (current-buffer))
6511         (erase-buffer)
6512         (setq groups (sort groups 'string<))
6513         (while groups
6514           ;; Groups may be entered twice into the list of groups.
6515           (if (not (string= (car groups) prev))
6516               (progn
6517                 (insert (setq prev (car groups)) "\n")
6518                 (if (and gnus-description-hashtb
6519                          (setq des (gnus-gethash (car groups)
6520                                                  gnus-description-hashtb)))
6521                     (insert "  " des "\n"))))
6522           (setq groups (cdr groups)))
6523         (goto-char (point-min))))
6524     (pop-to-buffer obuf)))
6525
6526 (defun gnus-group-description-apropos (regexp)
6527   "List all newsgroups that have names or descriptions that match a regexp."
6528   (interactive "sGnus description apropos (regexp): ")
6529   (if (not (or gnus-description-hashtb
6530                (gnus-read-all-descriptions-files)))
6531       (error "Couldn't request descriptions file"))
6532   (gnus-group-apropos regexp t))
6533
6534 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6535 (defun gnus-group-list-matching (level regexp &optional all lowest)
6536   "List all groups with unread articles that match REGEXP.
6537 If the prefix LEVEL is non-nil, it should be a number that says which
6538 level to cut off listing groups.
6539 If ALL, also list groups with no unread articles.
6540 If LOWEST, don't list groups with level lower than LOWEST."
6541   (interactive "P\nsList newsgroups matching: ")
6542   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6543                            all (or lowest 1) regexp)
6544   (goto-char (point-min))
6545   (gnus-group-position-point))
6546
6547 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6548   "List all groups that match REGEXP.
6549 If the prefix LEVEL is non-nil, it should be a number that says which
6550 level to cut off listing groups.
6551 If LOWEST, don't list groups with level lower than LOWEST."
6552   (interactive "P\nsList newsgroups matching: ")
6553   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6554
6555 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6556 (defun gnus-group-save-newsrc (&optional force)
6557   "Save the Gnus startup files.
6558 If FORCE, force saving whether it is necessary or not."
6559   (interactive "P")
6560   (gnus-save-newsrc-file force))
6561
6562 (defun gnus-group-restart (&optional arg)
6563   "Force Gnus to read the .newsrc file."
6564   (interactive "P")
6565   (when (gnus-yes-or-no-p
6566          (format "Are you sure you want to read %s? "
6567                  gnus-current-startup-file))
6568     (gnus-save-newsrc-file)
6569     (gnus-setup-news 'force)
6570     (gnus-group-list-groups arg)))
6571
6572 (defun gnus-group-read-init-file ()
6573   "Read the Gnus elisp init file."
6574   (interactive)
6575   (gnus-read-init-file))
6576
6577 (defun gnus-group-check-bogus-groups (&optional silent)
6578   "Check bogus newsgroups.
6579 If given a prefix, don't ask for confirmation before removing a bogus
6580 group."
6581   (interactive "P")
6582   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6583   (gnus-group-list-groups))
6584
6585 (defun gnus-group-edit-global-kill (&optional article group)
6586   "Edit the global kill file.
6587 If GROUP, edit that local kill file instead."
6588   (interactive "P")
6589   (setq gnus-current-kill-article article)
6590   (gnus-kill-file-edit-file group)
6591   (gnus-message
6592    6
6593    (substitute-command-keys
6594     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6595             (if group "local" "global")))))
6596
6597 (defun gnus-group-edit-local-kill (article group)
6598   "Edit a local kill file."
6599   (interactive (list nil (gnus-group-group-name)))
6600   (gnus-group-edit-global-kill article group))
6601
6602 (defun gnus-group-force-update ()
6603   "Update `.newsrc' file."
6604   (interactive)
6605   (gnus-save-newsrc-file))
6606
6607 (defun gnus-group-suspend ()
6608   "Suspend the current Gnus session.
6609 In fact, cleanup buffers except for group mode buffer.
6610 The hook gnus-suspend-gnus-hook is called before actually suspending."
6611   (interactive)
6612   (run-hooks 'gnus-suspend-gnus-hook)
6613   ;; Kill Gnus buffers except for group mode buffer.
6614   (let ((group-buf (get-buffer gnus-group-buffer)))
6615     ;; Do this on a separate list in case the user does a ^G before we finish
6616     (let ((gnus-buffer-list
6617            (delq group-buf (delq gnus-dribble-buffer
6618                                  (append gnus-buffer-list nil)))))
6619       (while gnus-buffer-list
6620         (gnus-kill-buffer (car gnus-buffer-list))
6621         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6622     (if group-buf
6623         (progn
6624           (setq gnus-buffer-list (list group-buf))
6625           (bury-buffer group-buf)
6626           (delete-windows-on group-buf t)))))
6627
6628 (defun gnus-group-clear-dribble ()
6629   "Clear all information from the dribble buffer."
6630   (interactive)
6631   (gnus-dribble-clear)
6632   (gnus-message 7 "Cleared dribble buffer"))
6633
6634 (defun gnus-group-exit ()
6635   "Quit reading news after updating .newsrc.eld and .newsrc.
6636 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6637   (interactive)
6638   (when 
6639       (or noninteractive                ;For gnus-batch-kill
6640           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6641           (not gnus-interactive-exit)   ;Without confirmation
6642           gnus-expert-user
6643           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6644     (run-hooks 'gnus-exit-gnus-hook)
6645     ;; Offer to save data from non-quitted summary buffers.
6646     (gnus-offer-save-summaries)
6647     ;; Save the newsrc file(s).
6648     (gnus-save-newsrc-file)
6649     ;; Kill-em-all.
6650     (gnus-close-backends)
6651     ;; Reset everything.
6652     (gnus-clear-system)
6653     ;; Allow the user to do things after cleaning up.
6654     (run-hooks 'gnus-after-exiting-gnus-hook)))
6655
6656 (defun gnus-close-backends ()
6657   ;; Send a close request to all backends that support such a request.
6658   (let ((methods gnus-valid-select-methods)
6659         func)
6660     (while methods
6661       (if (fboundp (setq func (intern (concat (caar methods)
6662                                               "-request-close"))))
6663           (funcall func))
6664       (setq methods (cdr methods)))))
6665
6666 (defun gnus-group-quit ()
6667   "Quit reading news without updating .newsrc.eld or .newsrc.
6668 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6669   (interactive)
6670   (when (or noninteractive              ;For gnus-batch-kill
6671             (zerop (buffer-size))
6672             (not (gnus-server-opened gnus-select-method))
6673             gnus-expert-user
6674             (not gnus-current-startup-file)
6675             (gnus-yes-or-no-p
6676              (format "Quit reading news without saving %s? "
6677                      (file-name-nondirectory gnus-current-startup-file))))
6678     (run-hooks 'gnus-exit-gnus-hook)
6679     (if gnus-use-full-window
6680         (delete-other-windows)
6681       (gnus-remove-some-windows))
6682     (gnus-dribble-save)
6683     (gnus-close-backends)
6684     (gnus-clear-system)
6685     ;; Allow the user to do things after cleaning up.
6686     (run-hooks 'gnus-after-exiting-gnus-hook)))
6687
6688 (defun gnus-offer-save-summaries ()
6689   "Offer to save all active summary buffers."
6690   (save-excursion
6691     (let ((buflist (buffer-list))
6692           buffers bufname)
6693       ;; Go through all buffers and find all summaries.
6694       (while buflist
6695         (and (setq bufname (buffer-name (car buflist)))
6696              (string-match "Summary" bufname)
6697              (save-excursion
6698                (set-buffer bufname)
6699                ;; We check that this is, indeed, a summary buffer.
6700                (and (eq major-mode 'gnus-summary-mode)
6701                     ;; Also make sure this isn't bogus.
6702                     gnus-newsgroup-prepared))
6703              (push bufname buffers))
6704         (setq buflist (cdr buflist)))
6705       ;; Go through all these summary buffers and offer to save them.
6706       (when buffers
6707         (map-y-or-n-p
6708          "Update summary buffer %s? "
6709          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6710          buffers)))))
6711
6712 (defun gnus-group-describe-briefly ()
6713   "Give a one line description of the group mode commands."
6714   (interactive)
6715   (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")))
6716
6717 (defun gnus-group-browse-foreign-server (method)
6718   "Browse a foreign news server.
6719 If called interactively, this function will ask for a select method
6720  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6721 If not, METHOD should be a list where the first element is the method
6722 and the second element is the address."
6723   (interactive
6724    (list (let ((how (completing-read
6725                      "Which backend: "
6726                      (append gnus-valid-select-methods gnus-server-alist)
6727                      nil t (cons "nntp" 0) 'gnus-method-history)))
6728            ;; We either got a backend name or a virtual server name.
6729            ;; If the first, we also need an address.
6730            (if (assoc how gnus-valid-select-methods)
6731                (list (intern how)
6732                      ;; Suggested by mapjph@bath.ac.uk.
6733                      (completing-read
6734                       "Address: "
6735                       (mapcar (lambda (server) (list server))
6736                               gnus-secondary-servers)))
6737              ;; We got a server name, so we find the method.
6738              (gnus-server-to-method how)))))
6739   (gnus-browse-foreign-server method))
6740
6741 \f
6742 ;;;
6743 ;;; Gnus summary mode
6744 ;;;
6745
6746 (defvar gnus-summary-mode-map nil)
6747
6748 (put 'gnus-summary-mode 'mode-class 'special)
6749
6750 (unless gnus-summary-mode-map
6751   (setq gnus-summary-mode-map (make-keymap))
6752   (suppress-keymap gnus-summary-mode-map)
6753
6754   ;; Non-orthogonal keys
6755
6756   (gnus-define-keys gnus-summary-mode-map
6757     " " gnus-summary-next-page
6758     "\177" gnus-summary-prev-page
6759     [delete] gnus-summary-prev-page
6760     "\r" gnus-summary-scroll-up
6761     "n" gnus-summary-next-unread-article
6762     "p" gnus-summary-prev-unread-article
6763     "N" gnus-summary-next-article
6764     "P" gnus-summary-prev-article
6765     "\M-\C-n" gnus-summary-next-same-subject
6766     "\M-\C-p" gnus-summary-prev-same-subject
6767     "\M-n" gnus-summary-next-unread-subject
6768     "\M-p" gnus-summary-prev-unread-subject
6769     "." gnus-summary-first-unread-article
6770     "," gnus-summary-best-unread-article
6771     "\M-s" gnus-summary-search-article-forward
6772     "\M-r" gnus-summary-search-article-backward
6773     "<" gnus-summary-beginning-of-article
6774     ">" gnus-summary-end-of-article
6775     "j" gnus-summary-goto-article
6776     "^" gnus-summary-refer-parent-article
6777     "\M-^" gnus-summary-refer-article
6778     "u" gnus-summary-tick-article-forward
6779     "!" gnus-summary-tick-article-forward
6780     "U" gnus-summary-tick-article-backward
6781     "d" gnus-summary-mark-as-read-forward
6782     "D" gnus-summary-mark-as-read-backward
6783     "E" gnus-summary-mark-as-expirable
6784     "\M-u" gnus-summary-clear-mark-forward
6785     "\M-U" gnus-summary-clear-mark-backward
6786     "k" gnus-summary-kill-same-subject-and-select
6787     "\C-k" gnus-summary-kill-same-subject
6788     "\M-\C-k" gnus-summary-kill-thread
6789     "\M-\C-l" gnus-summary-lower-thread
6790     "e" gnus-summary-edit-article
6791     "#" gnus-summary-mark-as-processable
6792     "\M-#" gnus-summary-unmark-as-processable
6793     "\M-\C-t" gnus-summary-toggle-threads
6794     "\M-\C-s" gnus-summary-show-thread
6795     "\M-\C-h" gnus-summary-hide-thread
6796     "\M-\C-f" gnus-summary-next-thread
6797     "\M-\C-b" gnus-summary-prev-thread
6798     "\M-\C-u" gnus-summary-up-thread
6799     "\M-\C-d" gnus-summary-down-thread
6800     "&" gnus-summary-execute-command
6801     "c" gnus-summary-catchup-and-exit
6802     "\C-w" gnus-summary-mark-region-as-read
6803     "\C-t" gnus-summary-toggle-truncation
6804     "?" gnus-summary-mark-as-dormant
6805     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6806     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6807     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6808     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6809     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6810     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6811     "=" gnus-summary-expand-window
6812     "\C-x\C-s" gnus-summary-reselect-current-group
6813     "\M-g" gnus-summary-rescan-group
6814     "w" gnus-summary-stop-page-breaking
6815     "\C-c\C-r" gnus-summary-caesar-message
6816     "\M-t" gnus-summary-toggle-mime
6817     "f" gnus-summary-followup
6818     "F" gnus-summary-followup-with-original
6819     "C" gnus-summary-cancel-article
6820     "r" gnus-summary-reply
6821     "R" gnus-summary-reply-with-original
6822     "\C-c\C-f" gnus-summary-mail-forward
6823     "o" gnus-summary-save-article
6824     "\C-o" gnus-summary-save-article-mail
6825     "|" gnus-summary-pipe-output
6826     "\M-k" gnus-summary-edit-local-kill
6827     "\M-K" gnus-summary-edit-global-kill
6828     "V" gnus-version
6829     "\C-c\C-d" gnus-summary-describe-group
6830     "q" gnus-summary-exit
6831     "Q" gnus-summary-exit-no-update
6832     "\C-c\C-i" gnus-info-find-node
6833     gnus-mouse-2 gnus-mouse-pick-article
6834     "m" gnus-summary-mail-other-window
6835     "a" gnus-summary-post-news
6836     "x" gnus-summary-limit-to-unread
6837     "s" gnus-summary-isearch-article
6838     "t" gnus-article-hide-headers
6839     "g" gnus-summary-show-article
6840     "l" gnus-summary-goto-last-article
6841     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6842     "\C-d" gnus-summary-enter-digest-group
6843     "\C-c\C-b" gnus-bug
6844     "*" gnus-cache-enter-article
6845     "\M-*" gnus-cache-remove-article
6846     "\M-&" gnus-summary-universal-argument
6847     "\C-l" gnus-recenter
6848     "I" gnus-summary-increase-score
6849     "L" gnus-summary-lower-score
6850
6851     "V" gnus-summary-score-map
6852     "X" gnus-uu-extract-map
6853     "S" gnus-summary-send-map)
6854
6855   ;; Sort of orthogonal keymap
6856   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6857     "t" gnus-summary-tick-article-forward
6858     "!" gnus-summary-tick-article-forward
6859     "d" gnus-summary-mark-as-read-forward
6860     "r" gnus-summary-mark-as-read-forward
6861     "c" gnus-summary-clear-mark-forward
6862     " " gnus-summary-clear-mark-forward
6863     "e" gnus-summary-mark-as-expirable
6864     "x" gnus-summary-mark-as-expirable
6865     "?" gnus-summary-mark-as-dormant
6866     "b" gnus-summary-set-bookmark
6867     "B" gnus-summary-remove-bookmark
6868     "#" gnus-summary-mark-as-processable
6869     "\M-#" gnus-summary-unmark-as-processable
6870     "S" gnus-summary-limit-include-expunged
6871     "C" gnus-summary-catchup
6872     "H" gnus-summary-catchup-to-here
6873     "\C-c" gnus-summary-catchup-all
6874     "k" gnus-summary-kill-same-subject-and-select
6875     "K" gnus-summary-kill-same-subject
6876     "P" gnus-uu-mark-map)
6877
6878   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6879     "c" gnus-summary-clear-above
6880     "u" gnus-summary-tick-above
6881     "m" gnus-summary-mark-above
6882     "k" gnus-summary-kill-below)
6883
6884   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6885     "/" gnus-summary-limit-to-subject
6886     "n" gnus-summary-limit-to-articles
6887     "w" gnus-summary-pop-limit
6888     "s" gnus-summary-limit-to-subject
6889     "a" gnus-summary-limit-to-author
6890     "u" gnus-summary-limit-to-unread
6891     "m" gnus-summary-limit-to-marks
6892     "v" gnus-summary-limit-to-score
6893     "D" gnus-summary-limit-include-dormant
6894     "d" gnus-summary-limit-exclude-dormant
6895     ;;  "t" gnus-summary-limit-exclude-thread
6896     "E" gnus-summary-limit-include-expunged
6897     "c" gnus-summary-limit-exclude-childless-dormant
6898     "C" gnus-summary-limit-mark-excluded-as-read)
6899
6900   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6901     "n" gnus-summary-next-unread-article
6902     "p" gnus-summary-prev-unread-article
6903     "N" gnus-summary-next-article
6904     "P" gnus-summary-prev-article
6905     "\C-n" gnus-summary-next-same-subject
6906     "\C-p" gnus-summary-prev-same-subject
6907     "\M-n" gnus-summary-next-unread-subject
6908     "\M-p" gnus-summary-prev-unread-subject
6909     "f" gnus-summary-first-unread-article
6910     "b" gnus-summary-best-unread-article
6911     "j" gnus-summary-goto-article
6912     "g" gnus-summary-goto-subject
6913     "l" gnus-summary-goto-last-article
6914     "p" gnus-summary-pop-article)
6915
6916   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6917     "k" gnus-summary-kill-thread
6918     "l" gnus-summary-lower-thread
6919     "i" gnus-summary-raise-thread
6920     "T" gnus-summary-toggle-threads
6921     "t" gnus-summary-rethread-current
6922     "^" gnus-summary-reparent-thread
6923     "s" gnus-summary-show-thread
6924     "S" gnus-summary-show-all-threads
6925     "h" gnus-summary-hide-thread
6926     "H" gnus-summary-hide-all-threads
6927     "n" gnus-summary-next-thread
6928     "p" gnus-summary-prev-thread
6929     "u" gnus-summary-up-thread
6930     "o" gnus-summary-top-thread
6931     "d" gnus-summary-down-thread
6932     "#" gnus-uu-mark-thread
6933     "\M-#" gnus-uu-unmark-thread)
6934
6935   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6936     "c" gnus-summary-catchup-and-exit
6937     "C" gnus-summary-catchup-all-and-exit
6938     "E" gnus-summary-exit-no-update
6939     "Q" gnus-summary-exit
6940     "Z" gnus-summary-exit
6941     "n" gnus-summary-catchup-and-goto-next-group
6942     "R" gnus-summary-reselect-current-group
6943     "G" gnus-summary-rescan-group
6944     "N" gnus-summary-next-group
6945     "P" gnus-summary-prev-group)
6946
6947   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6948     " " gnus-summary-next-page
6949     "n" gnus-summary-next-page
6950     "\177" gnus-summary-prev-page
6951     [delete] gnus-summary-prev-page
6952     "p" gnus-summary-prev-page
6953     "\r" gnus-summary-scroll-up
6954     "<" gnus-summary-beginning-of-article
6955     ">" gnus-summary-end-of-article
6956     "b" gnus-summary-beginning-of-article
6957     "e" gnus-summary-end-of-article
6958     "^" gnus-summary-refer-parent-article
6959     "r" gnus-summary-refer-parent-article
6960     "R" gnus-summary-refer-references
6961     "g" gnus-summary-show-article
6962     "s" gnus-summary-isearch-article)
6963
6964   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6965     "b" gnus-article-add-buttons
6966     "B" gnus-article-add-buttons-to-head
6967     "o" gnus-article-treat-overstrike
6968     ;;  "w" gnus-article-word-wrap
6969     "w" gnus-article-fill-cited-article
6970     "c" gnus-article-remove-cr
6971     "L" gnus-article-remove-trailing-blank-lines
6972     "q" gnus-article-de-quoted-unreadable
6973     "f" gnus-article-display-x-face
6974     "l" gnus-summary-stop-page-breaking
6975     "r" gnus-summary-caesar-message
6976     "t" gnus-article-hide-headers
6977     "v" gnus-summary-verbose-headers
6978     "m" gnus-summary-toggle-mime)
6979
6980   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6981     "a" gnus-article-hide
6982     "h" gnus-article-hide-headers
6983     "b" gnus-article-hide-boring-headers
6984     "s" gnus-article-hide-signature
6985     "c" gnus-article-hide-citation
6986     "p" gnus-article-hide-pgp
6987     "\C-c" gnus-article-hide-citation-maybe)
6988
6989   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6990     "a" gnus-article-highlight
6991     "h" gnus-article-highlight-headers
6992     "c" gnus-article-highlight-citation
6993     "s" gnus-article-highlight-signature)
6994
6995   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6996     "z" gnus-article-date-ut
6997     "u" gnus-article-date-ut
6998     "l" gnus-article-date-local
6999     "e" gnus-article-date-lapsed
7000     "o" gnus-article-date-original)
7001
7002   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7003     "v" gnus-version
7004     "f" gnus-summary-fetch-faq
7005     "d" gnus-summary-describe-group
7006     "h" gnus-summary-describe-briefly
7007     "i" gnus-info-find-node)
7008
7009   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7010     "e" gnus-summary-expire-articles
7011     "\M-\C-e" gnus-summary-expire-articles-now
7012     "\177" gnus-summary-delete-article
7013     [delete] gnus-summary-delete-article
7014     "m" gnus-summary-move-article
7015     "r" gnus-summary-respool-article
7016     "w" gnus-summary-edit-article
7017     "c" gnus-summary-copy-article
7018     "B" gnus-summary-crosspost-article
7019     "q" gnus-summary-respool-query
7020     "i" gnus-summary-import-article)
7021
7022   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7023     "o" gnus-summary-save-article
7024     "m" gnus-summary-save-article-mail
7025     "r" gnus-summary-save-article-rmail
7026     "f" gnus-summary-save-article-file
7027     "b" gnus-summary-save-article-body-file
7028     "h" gnus-summary-save-article-folder
7029     "v" gnus-summary-save-article-vm
7030     "p" gnus-summary-pipe-output
7031     "s" gnus-soup-add-article)
7032   )
7033
7034 \f
7035
7036 (defun gnus-summary-mode (&optional group)
7037   "Major mode for reading articles.
7038
7039 All normal editing commands are switched off.
7040 \\<gnus-summary-mode-map>
7041 Each line in this buffer represents one article.  To read an
7042 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7043 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7044 respectively.
7045
7046 You can also post articles and send mail from this buffer.  To
7047 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7048 of an article, type `\\[gnus-summary-reply]'.
7049
7050 There are approx. one gazillion commands you can execute in this
7051 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7052
7053 The following commands are available:
7054
7055 \\{gnus-summary-mode-map}"
7056   (interactive)
7057   (when (and menu-bar-mode
7058              (gnus-visual-p 'summary-menu 'menu))
7059     (gnus-summary-make-menu-bar))
7060   (kill-all-local-variables)
7061   (let ((locals gnus-summary-local-variables))
7062     (while locals
7063       (if (consp (car locals))
7064           (progn
7065             (make-local-variable (caar locals))
7066             (set (caar locals) (eval (cdar locals))))
7067         (make-local-variable (car locals))
7068         (set (car locals) nil))
7069       (setq locals (cdr locals))))
7070   (gnus-make-thread-indent-array)
7071   (gnus-simplify-mode-line)
7072   (setq major-mode 'gnus-summary-mode)
7073   (setq mode-name "Summary")
7074   (make-local-variable 'minor-mode-alist)
7075   (use-local-map gnus-summary-mode-map)
7076   (buffer-disable-undo (current-buffer))
7077   (setq buffer-read-only t)             ;Disable modification
7078   (setq truncate-lines t)
7079   (setq selective-display t)
7080   (setq selective-display-ellipses t)   ;Display `...'
7081   (setq buffer-display-table gnus-summary-display-table)
7082   (setq gnus-newsgroup-name group)
7083   (make-local-variable 'gnus-summary-line-format)
7084   (make-local-variable 'gnus-summary-line-format-spec)
7085   (run-hooks 'gnus-summary-mode-hook))
7086
7087 (defun gnus-summary-make-display-table ()
7088   ;; Change the display table.  Odd characters have a tendency to mess
7089   ;; up nicely formatted displays - we make all possible glyphs
7090   ;; display only a single character.
7091
7092   ;; We start from the standard display table, if any.
7093   (setq gnus-summary-display-table
7094         (or (copy-sequence standard-display-table)
7095             (make-display-table)))
7096   ;; Nix out all the control chars...
7097   (let ((i 32))
7098     (while (>= (setq i (1- i)) 0)
7099       (aset gnus-summary-display-table i [??])))
7100   ;; ... but not newline and cr, of course. (cr is necessary for the
7101   ;; selective display).
7102   (aset gnus-summary-display-table ?\n nil)
7103   (aset gnus-summary-display-table ?\r nil)
7104   ;; We nix out any glyphs over 126 that are not set already.
7105   (let ((i 256))
7106     (while (>= (setq i (1- i)) 127)
7107       ;; Only modify if the entry is nil.
7108       (or (aref gnus-summary-display-table i)
7109           (aset gnus-summary-display-table i [??])))))
7110
7111 (defun gnus-summary-clear-local-variables ()
7112   (let ((locals gnus-summary-local-variables))
7113     (while locals
7114       (if (consp (car locals))
7115           (and (vectorp (caar locals))
7116                (set (caar locals) nil))
7117         (and (vectorp (car locals))
7118              (set (car locals) nil)))
7119       (setq locals (cdr locals)))))
7120
7121 ;; Summary data functions.
7122
7123 (defmacro gnus-data-number (data)
7124   `(car ,data))
7125
7126 (defmacro gnus-data-set-number (data number)
7127   `(setcar ,data ,number))
7128
7129 (defmacro gnus-data-mark (data)
7130   `(nth 1 ,data))
7131
7132 (defmacro gnus-data-set-mark (data mark)
7133   `(setcar (nthcdr 1 ,data) ,mark))
7134
7135 (defmacro gnus-data-pos (data)
7136   `(nth 2 ,data))
7137
7138 (defmacro gnus-data-set-pos (data pos)
7139   `(setcar (nthcdr 2 ,data) ,pos))
7140
7141 (defmacro gnus-data-header (data)
7142   `(nth 3 ,data))
7143
7144 (defmacro gnus-data-level (data)
7145   `(nth 4 ,data))
7146
7147 (defmacro gnus-data-unread-p (data)
7148   `(= (nth 1 ,data) gnus-unread-mark))
7149
7150 (defmacro gnus-data-pseudo-p (data)
7151   `(consp (nth 3 ,data)))
7152
7153 (defmacro gnus-data-find (number)
7154   `(assq ,number gnus-newsgroup-data))
7155
7156 (defmacro gnus-data-find-list (number &optional data)
7157   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7158      (memq (assq ,number bdata)
7159            bdata)))
7160
7161 (defmacro gnus-data-make (number mark pos header level)
7162   `(list ,number ,mark ,pos ,header ,level))
7163
7164 (defun gnus-data-enter (after-article number mark pos header level offset)
7165   (let ((data (gnus-data-find-list after-article)))
7166     (or data (error "No such article: %d" after-article))
7167     (setcdr data (cons (gnus-data-make number mark pos header level)
7168                        (cdr data)))
7169     (setq gnus-newsgroup-data-reverse nil)
7170     (gnus-data-update-list (cddr data) offset)))
7171
7172 (defun gnus-data-enter-list (after-article list &optional offset)
7173   (when list
7174     (let ((data (and after-article (gnus-data-find-list after-article)))
7175           (ilist list))
7176       (or data (not after-article) (error "No such article: %d" after-article))
7177       ;; Find the last element in the list to be spliced into the main
7178       ;; list.
7179       (while (cdr list)
7180         (setq list (cdr list)))
7181       (if (not data)
7182           (progn
7183             (setcdr list gnus-newsgroup-data)
7184             (setq gnus-newsgroup-data ilist)
7185             (and offset (gnus-data-update-list (cdr list) offset)))
7186         (setcdr list (cdr data))
7187         (setcdr data ilist)
7188         (and offset (gnus-data-update-list (cdr data) offset)))
7189       (setq gnus-newsgroup-data-reverse nil))))
7190
7191 (defun gnus-data-remove (article &optional offset)
7192   (let ((data gnus-newsgroup-data))
7193     (if (= (gnus-data-number (car data)) article)
7194         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7195               gnus-newsgroup-data-reverse nil)
7196       (while (cdr data)
7197         (and (= (gnus-data-number (cadr data)) article)
7198              (progn
7199                (setcdr data (cddr data))
7200                (and offset (gnus-data-update-list (cdr data) offset))
7201                (setq data nil
7202                      gnus-newsgroup-data-reverse nil)))
7203         (setq data (cdr data))))))
7204
7205 (defmacro gnus-data-list (backward)
7206   `(if ,backward
7207        (or gnus-newsgroup-data-reverse
7208            (setq gnus-newsgroup-data-reverse
7209                  (reverse gnus-newsgroup-data)))
7210      gnus-newsgroup-data))
7211
7212 (defun gnus-data-update-list (data offset)
7213   "Add OFFSET to the POS of all data entries in DATA."
7214   (while data
7215     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7216     (setq data (cdr data))))
7217
7218 (defun gnus-data-compute-positions ()
7219   "Compute the positions of all articles."
7220   (let ((data gnus-newsgroup-data)
7221         pos)
7222     (while data
7223       (when (setq pos (text-property-any
7224                        (point-min) (point-max)
7225                        'gnus-number (gnus-data-number (car data))))
7226         (gnus-data-set-pos (car data) (+ pos 3)))
7227       (setq data (cdr data)))))
7228
7229 (defun gnus-summary-article-pseudo-p (article)
7230   "Say whether this article is a pseudo article or not."
7231   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7232
7233 (defun gnus-article-parent-p (number)
7234   "Say whether this article is a parent or not."
7235   (let ((data (gnus-data-find-list number)))
7236     (and (cdr data)                     ; There has to be an article after...
7237          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7238             (gnus-data-level (nth 1 data))))))
7239
7240 (defun gnus-article-children (number)
7241   "Return a list of all children to NUMBER."
7242   (let* ((data (gnus-data-find-list number))
7243          (level (gnus-data-level (car data)))
7244          children)
7245     (setq data (cdr data))
7246     (while (and data            
7247                 (= (gnus-data-level (car data)) (1+ level)))
7248       (push (gnus-data-number (car data)) children)
7249       (setq data (cdr data)))
7250     children))
7251
7252 (defmacro gnus-summary-skip-intangible ()
7253   "If the current article is intangible, then jump to a different article."
7254   '(let ((to (get-text-property (point) 'gnus-intangible)))
7255     (and to (gnus-summary-goto-subject to))))
7256
7257 (defmacro gnus-summary-article-intangible-p ()
7258   "Say whether this article is intangible or not."
7259   '(get-text-property (point) 'gnus-intangible))
7260
7261 ;; Some summary mode macros.
7262
7263 (defmacro gnus-summary-article-number ()
7264   "The article number of the article on the current line.
7265 If there isn's an article number here, then we return the current
7266 article number."
7267   '(progn
7268      (gnus-summary-skip-intangible)
7269      (or (get-text-property (point) 'gnus-number)
7270          (gnus-summary-last-subject))))
7271
7272 (defmacro gnus-summary-article-header (&optional number)
7273   `(gnus-data-header (gnus-data-find
7274                       ,(or number '(gnus-summary-article-number)))))
7275
7276 (defmacro gnus-summary-thread-level (&optional number)
7277   `(if (and (eq gnus-summary-make-false-root 'dummy)
7278             (get-text-property (point) 'gnus-intangible))
7279        0
7280      (gnus-data-level (gnus-data-find
7281                        ,(or number '(gnus-summary-article-number))))))
7282
7283 (defmacro gnus-summary-article-mark (&optional number)
7284   `(gnus-data-mark (gnus-data-find
7285                     ,(or number '(gnus-summary-article-number)))))
7286
7287 (defmacro gnus-summary-article-pos (&optional number)
7288   `(gnus-data-pos (gnus-data-find
7289                    ,(or number '(gnus-summary-article-number)))))
7290
7291 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7292 (defmacro gnus-summary-article-subject (&optional number)
7293   "Return current subject string or nil if nothing."
7294   `(let ((headers
7295           ,(if number
7296                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7297              '(gnus-data-header (assq (gnus-summary-article-number)
7298                                       gnus-newsgroup-data)))))
7299      (and headers
7300           (vectorp headers)
7301           (mail-header-subject headers))))
7302
7303 (defmacro gnus-summary-article-score (&optional number)
7304   "Return current article score."
7305   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7306                   gnus-newsgroup-scored))
7307        gnus-summary-default-score 0))
7308
7309 (defun gnus-summary-article-children (&optional number)
7310   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7311          (level (gnus-data-level (car data)))
7312          l children)
7313     (while (and (setq data (cdr data))
7314                 (> (setq l (gnus-data-level (car data))) level))
7315       (and (= (1+ level) l)
7316            (setq children (cons (gnus-data-number (car data))
7317                                 children))))
7318     (nreverse children)))
7319
7320 (defun gnus-summary-article-parent (&optional number)
7321   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7322                                     (gnus-data-list t)))
7323          (level (gnus-data-level (car data))))
7324     (if (zerop level)
7325         () ; This is a root.
7326       ;; We search until we find an article with a level less than
7327       ;; this one.  That function has to be the parent.
7328       (while (and (setq data (cdr data))
7329                   (not (< (gnus-data-level (car data)) level))))
7330       (and data (gnus-data-number (car data))))))
7331
7332 (defun gnus-unread-mark-p (mark)
7333   "Say whether MARK is the unread mark."
7334   (= mark gnus-unread-mark))
7335
7336 (defun gnus-read-mark-p (mark)
7337   "Say whether MARK is one of the marks that mark as read.
7338 This is all marks except unread, ticked, dormant, and expirable."
7339   (not (or (= mark gnus-unread-mark)
7340            (= mark gnus-ticked-mark)
7341            (= mark gnus-dormant-mark)
7342            (= mark gnus-expirable-mark))))
7343
7344 ;; Various summary mode internalish functions.
7345
7346 (defun gnus-mouse-pick-article (e)
7347   (interactive "e")
7348   (mouse-set-point e)
7349   (gnus-summary-next-page nil t))
7350
7351 (defun gnus-summary-setup-buffer (group)
7352   "Initialize summary buffer."
7353   (let ((buffer (concat "*Summary " group "*")))
7354     (if (get-buffer buffer)
7355         (progn
7356           (set-buffer buffer)
7357           (setq gnus-summary-buffer (current-buffer))
7358           (not gnus-newsgroup-prepared))
7359       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7360       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7361       (gnus-add-current-to-buffer-list)
7362       (gnus-summary-mode group)
7363       (when gnus-carpal
7364         (gnus-carpal-setup-buffer 'summary))
7365       (unless gnus-single-article-buffer
7366         (make-local-variable 'gnus-article-buffer)
7367         (make-local-variable 'gnus-article-current)
7368         (make-local-variable 'gnus-original-article-buffer))
7369       (setq gnus-newsgroup-name group)
7370       t)))
7371
7372 (defun gnus-set-global-variables ()
7373   ;; Set the global equivalents of the summary buffer-local variables
7374   ;; to the latest values they had.  These reflect the summary buffer
7375   ;; that was in action when the last article was fetched.
7376   (when (eq major-mode 'gnus-summary-mode)
7377     (setq gnus-summary-buffer (current-buffer))
7378     (let ((name gnus-newsgroup-name)
7379           (marked gnus-newsgroup-marked)
7380           (unread gnus-newsgroup-unreads)
7381           (headers gnus-current-headers)
7382           (data gnus-newsgroup-data)
7383           (summary gnus-summary-buffer)
7384           (article-buffer gnus-article-buffer)
7385           (original gnus-original-article-buffer)
7386           (gac gnus-article-current)
7387           (score-file gnus-current-score-file))
7388       (save-excursion
7389         (set-buffer gnus-group-buffer)
7390         (setq gnus-newsgroup-name name)
7391         (setq gnus-newsgroup-marked marked)
7392         (setq gnus-newsgroup-unreads unread)
7393         (setq gnus-current-headers headers)
7394         (setq gnus-newsgroup-data data)
7395         (setq gnus-article-current gac)
7396         (setq gnus-summary-buffer summary)
7397         (setq gnus-article-buffer article-buffer)
7398         (setq gnus-original-article-buffer original)
7399         (setq gnus-current-score-file score-file)))))
7400
7401 (defun gnus-summary-last-article-p (&optional article)
7402   "Return whether ARTICLE is the last article in the buffer."
7403   (if (not (setq article (or article (gnus-summary-article-number))))
7404       t ; All non-existant numbers are the last article. :-)
7405     (not (cdr (gnus-data-find-list article)))))
7406
7407 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7408   "Insert a dummy root in the summary buffer."
7409   (beginning-of-line)
7410   (add-text-properties
7411    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7412    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7413
7414 (defun gnus-make-thread-indent-array ()
7415   (let ((n 200))
7416     (unless (and gnus-thread-indent-array
7417                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7418       (setq gnus-thread-indent-array (make-vector 201 "")
7419             gnus-thread-indent-array-level gnus-thread-indent-level)
7420       (while (>= n 0)
7421         (aset gnus-thread-indent-array n
7422               (make-string (* n gnus-thread-indent-level) ? ))
7423         (setq n (1- n))))))
7424
7425 (defun gnus-summary-insert-line
7426   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7427                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7428                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7429   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7430          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7431          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7432          (gnus-tmp-score-char
7433           (if (or (null gnus-summary-default-score)
7434                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7435                       gnus-summary-zcore-fuzz)) ? 
7436             (if (< gnus-tmp-score gnus-summary-default-score)
7437                 gnus-score-below-mark gnus-score-over-mark)))
7438          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7439                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7440                                   gnus-cached-mark)
7441                                  (gnus-tmp-replied gnus-replied-mark)
7442                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7443                                   gnus-saved-mark)
7444                                  (t gnus-unread-mark)))
7445          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7446          (gnus-tmp-name
7447           (cond
7448            ((string-match "(.+)" gnus-tmp-from)
7449             (substring gnus-tmp-from
7450                        (1+ (match-beginning 0)) (1- (match-end 0))))
7451            ((string-match "<[^>]+> *$" gnus-tmp-from)
7452             (let ((beg (match-beginning 0)))
7453               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7454                        (substring gnus-tmp-from (1+ (match-beginning 0))
7455                                   (1- (match-end 0))))
7456                   (substring gnus-tmp-from 0 beg))))
7457            (t gnus-tmp-from)))
7458          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7459          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7460          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7461          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7462          (buffer-read-only nil))
7463     (when (string= gnus-tmp-name "")
7464       (setq gnus-tmp-name gnus-tmp-from))
7465     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7466     (put-text-property
7467      (point)
7468      (progn (eval gnus-summary-line-format-spec) (point))
7469      'gnus-number gnus-tmp-number)
7470     (when (gnus-visual-p 'summary-highlight 'highlight)
7471       (forward-line -1)
7472       (run-hooks 'gnus-summary-update-hook)
7473       (forward-line 1))))
7474
7475 (defun gnus-summary-update-line (&optional dont-update)
7476   ;; Update summary line after change.
7477   (when (and gnus-summary-default-score
7478              (not gnus-summary-inhibit-highlight))
7479     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7480            (article (gnus-summary-article-number))
7481            (score (gnus-summary-article-score article)))
7482       (unless dont-update
7483         (if (and gnus-summary-mark-below
7484                  (< (gnus-summary-article-score)
7485                     gnus-summary-mark-below))
7486             ;; This article has a low score, so we mark it as read.
7487             (when (memq article gnus-newsgroup-unreads)
7488               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7489           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7490             ;; This article was previously marked as read on account
7491             ;; of a low score, but now it has risen, so we mark it as
7492             ;; unread.
7493             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7494         (gnus-summary-update-mark
7495          (if (or (null gnus-summary-default-score)
7496                  (<= (abs (- score gnus-summary-default-score))
7497                      gnus-summary-zcore-fuzz)) ? 
7498            (if (< score gnus-summary-default-score)
7499                gnus-score-below-mark gnus-score-over-mark)) 'score))
7500       ;; Do visual highlighting.
7501       (when (gnus-visual-p 'summary-highlight 'highlight)
7502         (run-hooks 'gnus-summary-update-hook)))))
7503
7504 (defvar gnus-tmp-new-adopts nil)
7505
7506 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7507   ;; Sum up all elements (and sub-elements) in a list.
7508   (let* ((number
7509           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7510           (cond
7511            ((and (consp thread) (cdr thread))
7512             (apply
7513              '+ 1 (mapcar
7514                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7515            ((null thread)
7516             1)
7517            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7518             1)
7519            (t 1))))
7520     (when (and level (zerop level) gnus-tmp-new-adopts)
7521       (incf number
7522             (apply '+ (mapcar
7523                        'gnus-summary-number-of-articles-in-thread
7524                        gnus-tmp-new-adopts))))
7525     (if char
7526         (if (> number 1) gnus-not-empty-thread-mark
7527           gnus-empty-thread-mark)
7528       number)))
7529
7530 (defun gnus-summary-set-local-parameters (group)
7531  "Go through the local params of GROUP and set all variable specs in that list."
7532   (let ((params (gnus-info-params (gnus-get-info group)))
7533         elem)
7534     (while params
7535       (setq elem (car params)
7536             params (cdr params))
7537       (and (consp elem)                 ; Has to be a cons.
7538            (consp (cdr elem))           ; The cdr has to be a list.
7539            (symbolp (car elem))         ; Has to be a symbol in there.
7540            (not (memq (car elem) 
7541                       '(quit-config to-address to-list to-group)))
7542            (progn                       ; So we set it.
7543              (make-local-variable (car elem))
7544              (set (car elem) (eval (nth 1 elem))))))))
7545
7546 (defun gnus-summary-read-group (group &optional show-all no-article
7547                                       kill-buffer no-display)
7548   "Start reading news in newsgroup GROUP.
7549 If SHOW-ALL is non-nil, already read articles are also listed.
7550 If NO-ARTICLE is non-nil, no article is selected initially.
7551 If NO-DISPLAY, don't generate a summary buffer."
7552   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7553   (let* ((new-group (gnus-summary-setup-buffer group))
7554          (quit-config (gnus-group-quit-config group))
7555          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7556     (cond
7557      ;; This summary buffer exists already, so we just select it.
7558      ((not new-group)
7559       (gnus-set-global-variables)
7560       (when kill-buffer
7561         (gnus-kill-or-deaden-summary kill-buffer))
7562       (gnus-configure-windows 'summary 'force)
7563       (gnus-set-mode-line 'summary)
7564       (gnus-summary-position-point)
7565       (message "")
7566       t)
7567      ;; We couldn't select this group.
7568      ((null did-select)
7569       (when (and (eq major-mode 'gnus-summary-mode)
7570                  (not (equal (current-buffer) kill-buffer)))
7571         (kill-buffer (current-buffer))
7572         (if (not quit-config)
7573             (progn
7574               (set-buffer gnus-group-buffer)
7575               (gnus-group-jump-to-group group)
7576               (gnus-group-next-unread-group 1))
7577           (if (not (buffer-name (car quit-config)))
7578               (gnus-configure-windows 'group 'force)
7579             (set-buffer (car quit-config))
7580             (and (eq major-mode 'gnus-summary-mode)
7581                  (gnus-set-global-variables))
7582             (gnus-configure-windows (cdr quit-config)))))
7583       (gnus-message 3 "Can't select group")
7584       nil)
7585      ;; The user did a `C-g' while prompting for number of articles,
7586      ;; so we exit this group.
7587      ((eq did-select 'quit)
7588       (and (eq major-mode 'gnus-summary-mode)
7589            (not (equal (current-buffer) kill-buffer))
7590            (kill-buffer (current-buffer)))
7591       (when kill-buffer
7592         (gnus-kill-or-deaden-summary kill-buffer))
7593       (if (not quit-config)
7594           (progn
7595             (set-buffer gnus-group-buffer)
7596             (gnus-group-jump-to-group group)
7597             (gnus-group-next-unread-group 1)
7598             (gnus-configure-windows 'group 'force))
7599         (if (not (buffer-name (car quit-config)))
7600             (gnus-configure-windows 'group 'force)
7601           (set-buffer (car quit-config))
7602           (and (eq major-mode 'gnus-summary-mode)
7603                (gnus-set-global-variables))
7604           (gnus-configure-windows (cdr quit-config))))
7605       ;; Finally signal the quit.
7606       (signal 'quit nil))
7607      ;; The group was successfully selected.
7608      (t
7609       (gnus-set-global-variables)
7610       ;; Save the active value in effect when the group was entered.
7611       (setq gnus-newsgroup-active
7612             (gnus-copy-sequence
7613              (gnus-active gnus-newsgroup-name)))
7614       ;; You can change the summary buffer in some way with this hook.
7615       (run-hooks 'gnus-select-group-hook)
7616       ;; Set any local variables in the group parameters.
7617       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7618       (gnus-update-format-specifications)
7619       ;; Do score processing.
7620       (when gnus-use-scoring
7621         (gnus-possibly-score-headers))
7622       ;; Check whether to fill in the gaps in the threads.
7623       (when gnus-build-sparse-threads
7624         (gnus-build-sparse-threads))
7625       ;; Find the initial limit.
7626       (if gnus-show-threads
7627           (if show-all
7628               (let ((gnus-newsgroup-dormant nil))
7629                 (gnus-summary-initial-limit show-all))
7630             (gnus-summary-initial-limit show-all))
7631         (setq gnus-newsgroup-limit 
7632               (mapcar 
7633                (lambda (header) (mail-header-number header))
7634                gnus-newsgroup-headers)))
7635       ;; Generate the summary buffer.
7636       (unless no-display
7637         (gnus-summary-prepare))
7638       (when gnus-use-trees
7639         (gnus-tree-open group)
7640         (setq gnus-summary-highlight-line-function
7641               'gnus-tree-highlight-article))
7642       ;; If the summary buffer is empty, but there are some low-scored
7643       ;; articles or some excluded dormants, we include these in the
7644       ;; buffer.
7645       (when (and (zerop (buffer-size))
7646                  (not no-display))
7647         (cond (gnus-newsgroup-dormant
7648                (gnus-summary-limit-include-dormant))
7649               ((and gnus-newsgroup-scored show-all)
7650                (gnus-summary-limit-include-expunged))))
7651       ;; Function `gnus-apply-kill-file' must be called in this hook.
7652       (run-hooks 'gnus-apply-kill-hook)
7653       (if (and (zerop (buffer-size))
7654                (not no-display))
7655           (progn
7656             ;; This newsgroup is empty.
7657             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7658             (gnus-message 6 "No unread news")
7659             (when kill-buffer
7660               (gnus-kill-or-deaden-summary kill-buffer))
7661             ;; Return nil from this function.
7662             nil)
7663         ;; Hide conversation thread subtrees.  We cannot do this in
7664         ;; gnus-summary-prepare-hook since kill processing may not
7665         ;; work with hidden articles.
7666         (and gnus-show-threads
7667              gnus-thread-hide-subtree
7668              (gnus-summary-hide-all-threads))
7669         ;; Show first unread article if requested.
7670         (if (and (not no-article)
7671                  (not no-display)
7672                  gnus-newsgroup-unreads
7673                  gnus-auto-select-first)
7674             (unless (if (eq gnus-auto-select-first 'best)
7675                         (gnus-summary-best-unread-article)
7676                       (gnus-summary-first-unread-article))
7677               (gnus-configure-windows 'summary))
7678           ;; Don't select any articles, just move point to the first
7679           ;; article in the group.
7680           (goto-char (point-min))
7681           (gnus-summary-position-point)
7682           (gnus-set-mode-line 'summary)
7683           (gnus-configure-windows 'summary 'force))
7684         ;; If we are in async mode, we send some info to the backend.
7685         (when gnus-newsgroup-async
7686           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7687         (when kill-buffer
7688           (gnus-kill-or-deaden-summary kill-buffer))
7689         (when (get-buffer-window gnus-group-buffer t)
7690           ;; Gotta use windows, because recenter does wierd stuff if
7691           ;; the current buffer ain't the displayed window.
7692           (let ((owin (selected-window)))
7693             (select-window (get-buffer-window gnus-group-buffer t))
7694             (when (gnus-group-goto-group group)
7695               (recenter))
7696             (select-window owin))))
7697       ;; Mark this buffer as "prepared".
7698       (setq gnus-newsgroup-prepared t)
7699       t))))
7700
7701 (defun gnus-summary-prepare ()
7702   "Generate the summary buffer."
7703   (let ((buffer-read-only nil))
7704     (erase-buffer)
7705     (setq gnus-newsgroup-data nil
7706           gnus-newsgroup-data-reverse nil)
7707     (run-hooks 'gnus-summary-generate-hook)
7708     ;; Generate the buffer, either with threads or without.
7709     (when gnus-newsgroup-headers
7710       (gnus-summary-prepare-threads
7711        (if gnus-show-threads
7712            (gnus-sort-gathered-threads
7713             (funcall gnus-summary-thread-gathering-function
7714                      (gnus-sort-threads
7715                       (gnus-cut-threads (gnus-make-threads)))))
7716          ;; Unthreaded display.
7717          (gnus-sort-articles gnus-newsgroup-headers))))
7718     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7719     ;; Call hooks for modifying summary buffer.
7720     (goto-char (point-min))
7721     (run-hooks 'gnus-summary-prepare-hook)))
7722
7723 (defun gnus-gather-threads-by-subject (threads)
7724   "Gather threads by looking at Subject headers."
7725   (if (not gnus-summary-make-false-root)
7726       threads
7727     (let ((hashtb (gnus-make-hashtable 1023))
7728           (prev threads)
7729           (result threads)
7730           subject hthread whole-subject)
7731       (while threads
7732         (setq whole-subject (mail-header-subject (caar threads)))
7733         (setq subject
7734               (cond
7735                ;; Truncate the subject.
7736                ((numberp gnus-summary-gather-subject-limit)
7737                 (setq subject (gnus-simplify-subject-re whole-subject))
7738                 (if (> (length subject) gnus-summary-gather-subject-limit)
7739                     (substring subject 0 gnus-summary-gather-subject-limit)
7740                   subject))
7741                ;; Fuzzily simplify it.
7742                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7743                 (gnus-simplify-subject-fuzzy whole-subject))
7744                ;; Just remove the leading "Re:".
7745                (t
7746                 (gnus-simplify-subject-re whole-subject))))
7747
7748         (if (and gnus-summary-gather-exclude-subject
7749                  (string-match gnus-summary-gather-exclude-subject
7750                                subject))
7751             ()          ; We don't want to do anything with this article.
7752           ;; We simplify the subject before looking it up in the
7753           ;; hash table.
7754
7755           (if (setq hthread (gnus-gethash subject hashtb))
7756               (progn
7757                 ;; We enter a dummy root into the thread, if we
7758                 ;; haven't done that already.
7759                 (unless (stringp (caar hthread))
7760                   (setcar hthread (list whole-subject (car hthread))))
7761                 ;; We add this new gathered thread to this gathered
7762                 ;; thread.
7763                 (setcdr (car hthread)
7764                         (nconc (cdar hthread) (list (car threads))))
7765                 ;; Remove it from the list of threads.
7766                 (setcdr prev (cdr threads))
7767                 (setq threads prev))
7768             ;; Enter this thread into the hash table.
7769             (gnus-sethash subject threads hashtb)))
7770         (setq prev threads)
7771         (setq threads (cdr threads)))
7772       result)))
7773
7774 (defun gnus-gather-threads-by-references (threads)
7775   "Gather threads by looking at References headers."
7776   (let ((idhashtb (gnus-make-hashtable 1023))
7777         (thhashtb (gnus-make-hashtable 1023))
7778         (prev threads)
7779         (result threads)
7780         ids references id gthread gid entered)
7781     (while threads
7782       (when (setq references (mail-header-references (caar threads)))
7783         (setq id (mail-header-id (caar threads)))
7784         (setq ids (gnus-split-references references))
7785         (setq entered nil)
7786         (while ids
7787           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7788               (progn
7789                 (gnus-sethash (car ids) id idhashtb)
7790                 (gnus-sethash id threads thhashtb))
7791             (setq gthread (gnus-gethash gid thhashtb))
7792             (unless entered
7793               ;; We enter a dummy root into the thread, if we
7794               ;; haven't done that already.
7795               (unless (stringp (caar gthread))
7796                 (setcar gthread (list (mail-header-subject (caar gthread))
7797                                       (car gthread))))
7798               ;; We add this new gathered thread to this gathered
7799               ;; thread.
7800               (setcdr (car gthread)
7801                       (nconc (cdar gthread) (list (car threads)))))
7802             ;; Add it into the thread hash table.
7803             (gnus-sethash id gthread thhashtb)
7804             (setq entered t)
7805             ;; Remove it from the list of threads.
7806             (setcdr prev (cdr threads))
7807             (setq threads prev))
7808           (setq ids (cdr ids))))
7809       (setq prev threads)
7810       (setq threads (cdr threads)))
7811     result))
7812
7813 (defun gnus-sort-gathered-threads (threads)
7814   "Sort subtreads inside each gathered thread by article number."
7815   (let ((result threads))
7816     (while threads
7817       (when (stringp (caar threads))
7818         (setcdr (car threads)
7819                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7820       (setq threads (cdr threads)))
7821     result))
7822
7823 (defun gnus-make-threads ()
7824   "Go through the dependency hashtb and find the roots.  Return all threads."
7825   (let (threads)
7826     (mapatoms
7827      (lambda (refs)
7828        (unless (car (symbol-value refs))
7829          ;; These threads do not refer back to any other articles,
7830          ;; so they're roots.
7831          (setq threads (append (cdr (symbol-value refs)) threads))))
7832      gnus-newsgroup-dependencies)
7833     threads))
7834
7835 (defun gnus-build-sparse-threads ()
7836   (let ((headers gnus-newsgroup-headers)
7837         (deps gnus-newsgroup-dependencies)
7838         header references generation relations 
7839         cthread subject child end pthread relation)
7840     ;; First we create an alist of generations/relations, where 
7841     ;; generations is how much we trust the ralation, and the relation
7842     ;; is parent/child.
7843     (gnus-message 7 "Making sparse threads...")
7844     (save-excursion
7845       (nnheader-set-temp-buffer " *gnus sparse threads*")
7846       (while (setq header (pop headers))
7847         (when (and (setq references (mail-header-references header))
7848                    (not (string= references "")))
7849           (insert references)
7850           (setq child (mail-header-id header)
7851                 subject (mail-header-subject header))
7852           (setq generation 0)
7853           (while (search-backward ">" nil t)
7854             (setq end (1+ (point)))
7855             (when (search-backward "<" nil t)
7856               (push (list (incf generation) 
7857                           child (setq child (buffer-substring (point) end))
7858                           subject)
7859                     relations)))
7860           (push (list (1+ generation) child nil subject) relations)
7861           (erase-buffer)))
7862       (kill-buffer (current-buffer)))
7863     ;; Sort over trustworthiness.
7864     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7865     (while (setq relation (pop relations))
7866       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7867                 (unless (car (symbol-value cthread))
7868                   ;; Make this article the parent of these threads.
7869                   (setcar (symbol-value cthread)
7870                           (vector gnus-reffed-article-number 
7871                                   (cadddr relation) 
7872                                   "" ""
7873                                   (cadr relation) 
7874                                   (or (caddr relation) "") 0 0 "")))
7875               (set cthread (list (vector gnus-reffed-article-number
7876                                          (cadddr relation) 
7877                                          "" "" (cadr relation) 
7878                                          (or (caddr relation) "") 0 0 ""))))
7879         (push gnus-reffed-article-number gnus-newsgroup-limit)
7880         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7881         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7882               gnus-newsgroup-reads)
7883         (decf gnus-reffed-article-number)
7884         ;; Make this new thread the child of its parent.
7885         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7886             (setcdr (symbol-value pthread)
7887                     (nconc (cdr (symbol-value pthread))
7888                            (list (symbol-value cthread))))
7889           (set pthread (list nil (symbol-value cthread))))))
7890     (gnus-message 7 "Making sparse threads...done")))
7891
7892 (defun gnus-build-old-threads ()
7893   ;; Look at all the articles that refer back to old articles, and
7894   ;; fetch the headers for the articles that aren't there.  This will
7895   ;; build complete threads - if the roots haven't been expired by the
7896   ;; server, that is.
7897   (let (id heads)
7898     (mapatoms
7899      (lambda (refs)
7900        (when (not (car (symbol-value refs)))
7901          (setq heads (cdr (symbol-value refs)))
7902          (while heads
7903            (if (memq (mail-header-number (caar heads))
7904                      gnus-newsgroup-dormant)
7905                (setq heads (cdr heads))
7906              (setq id (symbol-name refs))
7907              (while (and (setq id (gnus-build-get-header id))
7908                          (not (car (gnus-gethash
7909                                     id gnus-newsgroup-dependencies)))))
7910              (setq heads nil)))))
7911      gnus-newsgroup-dependencies)))
7912
7913 (defun gnus-build-get-header (id)
7914   ;; Look through the buffer of NOV lines and find the header to
7915   ;; ID.  Enter this line into the dependencies hash table, and return
7916   ;; the id of the parent article (if any).
7917   (let ((deps gnus-newsgroup-dependencies)
7918         found header)
7919     (prog1
7920         (save-excursion
7921           (set-buffer nntp-server-buffer)
7922           (goto-char (point-min))
7923           (while (and (not found) (search-forward id nil t))
7924             (beginning-of-line)
7925             (setq found (looking-at
7926                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7927                                  (regexp-quote id))))
7928             (or found (beginning-of-line 2)))
7929           (when found
7930             (beginning-of-line)
7931             (and
7932              (setq header (gnus-nov-parse-line
7933                            (read (current-buffer)) deps))
7934              (gnus-parent-id (mail-header-references header)))))
7935       (when header
7936         (let ((number (mail-header-number header)))
7937           (push number gnus-newsgroup-limit)
7938           (push header gnus-newsgroup-headers)
7939           (if (memq number gnus-newsgroup-unselected)
7940               (progn
7941                 (push number gnus-newsgroup-unreads)
7942                 (setq gnus-newsgroup-unselected
7943                       (delq number gnus-newsgroup-unselected)))
7944             (push number gnus-newsgroup-ancient)))))))
7945
7946 (defun gnus-summary-update-article (article &optional header)
7947   "Update ARTICLE in the summary buffer."
7948   (set-buffer gnus-summary-buffer)
7949   (let* ((header (or header (gnus-summary-article-header article)))
7950          (id (mail-header-id header))
7951          (data (gnus-data-find article))
7952          (thread (gnus-id-to-thread id))
7953          (parent
7954           (gnus-id-to-thread (or (gnus-parent-id 
7955                                   (mail-header-references header))
7956                                  "tull")))
7957          (buffer-read-only nil)
7958          (old (car thread))
7959          (number (mail-header-number header))
7960          pos)
7961     (when thread
7962       (setcar thread nil)
7963       (when parent
7964         (delq thread parent))
7965       (if (gnus-summary-insert-subject id header)
7966           ;; Set the (possibly) new article number in the data structure.
7967           (gnus-data-set-number data (gnus-id-to-article id))
7968         (setcar thread old)
7969         nil))))
7970
7971 (defun gnus-rebuild-thread (id)
7972   "Rebuild the thread containing ID."
7973   (let ((buffer-read-only nil)
7974         current thread data)
7975     (if (not gnus-show-threads)
7976         (setq thread (list (car (gnus-id-to-thread id))))
7977       ;; Get the thread this article is part of.
7978       (setq thread (gnus-remove-thread id)))
7979     (setq current (save-excursion
7980                     (and (zerop (forward-line -1))
7981                          (gnus-summary-article-number))))
7982     ;; If this is a gathered thread, we have to go some re-gathering.
7983     (when (stringp (car thread))
7984       (let ((subject (car thread))
7985             roots thr)
7986         (setq thread (cdr thread))
7987         (while thread
7988           (unless (memq (setq thr (gnus-id-to-thread
7989                                       (gnus-root-id
7990                                        (mail-header-id (caar thread)))))
7991                         roots)
7992             (push thr roots))
7993           (setq thread (cdr thread)))
7994         ;; We now have all (unique) roots.
7995         (if (= (length roots) 1)
7996             ;; All the loose roots are now one solid root.
7997             (setq thread (car roots))
7998           (setq thread (cons subject (gnus-sort-threads roots))))))
7999     (let (threads)
8000       ;; We then insert this thread into the summary buffer.
8001       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8002         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8003         (setq data (nreverse gnus-newsgroup-data))
8004         (setq threads gnus-newsgroup-threads))
8005       ;; We splice the new data into the data structure.
8006       (gnus-data-enter-list current data)
8007       (gnus-data-compute-positions)
8008       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8009
8010 (defun gnus-id-to-thread (id)
8011   "Return the (sub-)thread where ID appears."
8012   (gnus-gethash id gnus-newsgroup-dependencies))
8013
8014 (defun gnus-id-to-article (id)
8015   "Return the article number of ID."
8016   (let ((thread (gnus-id-to-thread id)))
8017     (when thread
8018       (mail-header-number (car thread)))))
8019
8020 (defun gnus-id-to-header (id)
8021   "Return the article headers of ID."
8022   (car (gnus-id-to-thread id)))
8023
8024 (defun gnus-article-displayed-root-p (article)
8025   "Say whether ARTICLE is a root(ish) article."
8026   (let ((level (gnus-summary-thread-level article))
8027         particle)
8028     (cond 
8029      ((null level) nil)
8030      ((zerop level) t)
8031      ((and (= 1 level)
8032            (null (setq particle (gnus-id-to-article
8033                                  (gnus-parent-id 
8034                                   (mail-header-references 
8035                                    (gnus-summary-article-header article))))))
8036            (null (gnus-summary-thread-level particle)))))))
8037
8038 (defun gnus-root-id (id)
8039   "Return the id of the root of the thread where ID appears."
8040   (let (last-id prev)
8041     (while (and id (setq prev (car (gnus-gethash 
8042                                     id gnus-newsgroup-dependencies))))
8043       (setq last-id id
8044             id (gnus-parent-id (mail-header-references prev))))
8045     last-id))
8046
8047 (defun gnus-remove-thread (id &optional dont-remove)
8048   "Remove the thread that has ID in it."
8049   (let ((dep gnus-newsgroup-dependencies)
8050         headers thread last-id)
8051     ;; First go up in this thread until we find the root.
8052     (setq last-id (gnus-root-id id))
8053     (setq headers (list (car (gnus-id-to-thread last-id))
8054                         (caadr (gnus-id-to-thread last-id))))
8055     ;; We have now found the real root of this thread.  It might have
8056     ;; been gathered into some loose thread, so we have to search
8057     ;; through the threads to find the thread we wanted.
8058     (let ((threads gnus-newsgroup-threads)
8059           sub)
8060       (while threads
8061         (setq sub (car threads))
8062         (if (stringp (car sub))
8063             ;; This is a gathered threads, so we look at the roots
8064             ;; below it to find whether this article in in this
8065             ;; gathered root.
8066             (progn
8067               (setq sub (cdr sub))
8068               (while sub
8069                 (when (member (caar sub) headers)
8070                   (setq thread (car threads)
8071                         threads nil
8072                         sub nil))
8073                 (setq sub (cdr sub))))
8074           ;; It's an ordinary thread, so we check it.
8075           (when (eq (car sub) (car headers))
8076             (setq thread sub
8077                   threads nil)))
8078         (setq threads (cdr threads)))
8079       ;; If this article is in no thread, then it's a root.
8080       (if thread
8081           (unless dont-remove
8082             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8083         (setq thread (gnus-gethash last-id dep)))
8084       (when thread
8085         (prog1
8086             thread ; We return this thread.
8087           (unless dont-remove
8088             (if (stringp (car thread))
8089                 (progn
8090                   ;; If we use dummy roots, then we have to remove the
8091                   ;; dummy root as well.
8092                   (when (eq gnus-summary-make-false-root 'dummy)
8093                     ;; Uhm.
8094                     )
8095                   (setq thread (cdr thread))
8096                   (while thread
8097                     (gnus-remove-thread-1 (car thread))
8098                     (setq thread (cdr thread))))
8099               (gnus-remove-thread-1 thread))))))))
8100
8101 (defun gnus-remove-thread-1 (thread)
8102   "Remove the thread THREAD recursively."
8103   (let ((number (mail-header-number (car thread)))
8104         pos)
8105     (when (setq pos (text-property-any
8106                      (point-min) (point-max) 'gnus-number number))
8107       (goto-char pos)
8108       (gnus-delete-line)
8109       (gnus-data-remove number))
8110     (setq thread (cdr thread))
8111     (while thread
8112       (gnus-remove-thread-1 (pop thread)))))
8113
8114 (defun gnus-sort-threads (threads)
8115   "Sort THREADS."
8116   (if (not gnus-thread-sort-functions)
8117       threads
8118     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8119                     (car gnus-thread-sort-functions)
8120                   `(lambda (t1 t2)
8121                      ,(gnus-make-sort-function 
8122                        (reverse gnus-thread-sort-functions))))))
8123       (gnus-message 7 "Sorting threads...")
8124       (prog1
8125           (sort threads func)
8126         (gnus-message 7 "Sorting threads...done")))))
8127
8128 (defun gnus-sort-articles (articles)
8129   "Sort ARTICLES."
8130   (when gnus-article-sort-functions
8131     (let ((func (if (= 1 (length gnus-article-sort-functions))
8132                     (car gnus-article-sort-functions)
8133                   `(lambda (t1 t2)
8134                      ,(gnus-make-sort-function 
8135                        (reverse gnus-article-sort-functions))))))
8136       (gnus-message 7 "Sorting articles...")
8137       (prog1
8138           (setq gnus-newsgroup-headers (sort articles func))
8139         (gnus-message 7 "Sorting articles...done")))))
8140
8141 (defun gnus-make-sort-function (funs)
8142   "Return a composite sort condition based on the functions in FUNC."
8143   (if (cdr funs)
8144       `(or (,(car funs) t1 t2)
8145            (and (not (,(car funs) t2 t1))
8146                 ,(gnus-make-sort-function (cdr funs))))
8147     `(,(car funs) t1 t2)))
8148                  
8149 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8150 (defmacro gnus-thread-header (thread)
8151   ;; Return header of first article in THREAD.
8152   ;; Note that THREAD must never, ever be anything else than a variable -
8153   ;; using some other form will lead to serious barfage.
8154   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8155   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8156   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8157         (vector thread) 2))
8158
8159 (defsubst gnus-article-sort-by-number (h1 h2)
8160   "Sort articles by article number."
8161   (< (mail-header-number h1)
8162      (mail-header-number h2)))
8163
8164 (defun gnus-thread-sort-by-number (h1 h2)
8165   "Sort threads by root article number."
8166   (gnus-article-sort-by-number
8167    (gnus-thread-header h1) (gnus-thread-header h2)))
8168
8169 (defsubst gnus-article-sort-by-author (h1 h2)
8170   "Sort articles by root author."
8171   (string-lessp
8172    (let ((extract (funcall
8173                    gnus-extract-address-components
8174                    (mail-header-from h1))))
8175      (or (car extract) (cdr extract)))
8176    (let ((extract (funcall
8177                    gnus-extract-address-components
8178                    (mail-header-from h2))))
8179      (or (car extract) (cdr extract)))))
8180
8181 (defun gnus-thread-sort-by-author (h1 h2)
8182   "Sort threads by root author."
8183   (gnus-article-sort-by-author
8184    (gnus-thread-header h1)  (gnus-thread-header h2)))
8185
8186 (defsubst gnus-article-sort-by-subject (h1 h2)
8187   "Sort articles by root subject."
8188   (string-lessp
8189    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8190    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8191
8192 (defun gnus-thread-sort-by-subject (h1 h2)
8193   "Sort threads by root subject."
8194   (gnus-article-sort-by-subject
8195    (gnus-thread-header h1) (gnus-thread-header h2)))
8196
8197 (defsubst gnus-article-sort-by-date (h1 h2)
8198   "Sort articles by root article date."
8199   (string-lessp
8200    (gnus-sortable-date (mail-header-date h1))
8201    (gnus-sortable-date (mail-header-date h2))))
8202
8203 (defun gnus-thread-sort-by-date (h1 h2)
8204   "Sort threads by root article date."
8205   (gnus-article-sort-by-date
8206    (gnus-thread-header h1) (gnus-thread-header h2)))
8207
8208 (defsubst gnus-article-sort-by-score (h1 h2)
8209   "Sort articles by root article score.
8210 Unscored articles will be counted as having a score of zero."
8211   (> (or (cdr (assq (mail-header-number h1)
8212                     gnus-newsgroup-scored))
8213          gnus-summary-default-score 0)
8214      (or (cdr (assq (mail-header-number h2)
8215                     gnus-newsgroup-scored))
8216          gnus-summary-default-score 0)))
8217
8218 (defun gnus-thread-sort-by-score (h1 h2)
8219   "Sort threads by root article score."
8220   (gnus-article-sort-by-score
8221    (gnus-thread-header h1) (gnus-thread-header h2)))
8222
8223 (defun gnus-thread-sort-by-total-score (h1 h2)
8224   "Sort threads by the sum of all scores in the thread.
8225 Unscored articles will be counted as having a score of zero."
8226   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8227
8228 (defun gnus-thread-total-score (thread)
8229   ;;  This function find the total score of THREAD.
8230   (if (consp thread)
8231       (if (stringp (car thread))
8232           (apply gnus-thread-score-function 0
8233                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8234         (gnus-thread-total-score-1 thread))
8235     (gnus-thread-total-score-1 (list thread))))
8236
8237 (defun gnus-thread-total-score-1 (root)
8238   ;; This function find the total score of the thread below ROOT.
8239   (setq root (car root))
8240   (apply gnus-thread-score-function
8241          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8242              gnus-summary-default-score 0)
8243          (mapcar 'gnus-thread-total-score
8244                  (cdr (gnus-gethash (mail-header-id root)
8245                                     gnus-newsgroup-dependencies)))))
8246
8247 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8248 (defvar gnus-tmp-prev-subject nil)
8249 (defvar gnus-tmp-false-parent nil)
8250 (defvar gnus-tmp-root-expunged nil)
8251 (defvar gnus-tmp-dummy-line nil)
8252
8253 (defun gnus-summary-prepare-threads (threads)
8254   "Prepare summary buffer from THREADS and indentation LEVEL.
8255 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8256 or a straight list of headers."
8257   (gnus-message 7 "Generating summary...")
8258
8259   (setq gnus-newsgroup-threads threads)
8260   (beginning-of-line)
8261
8262   (let ((gnus-tmp-level 0)
8263         (default-score (or gnus-summary-default-score 0))
8264         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8265         thread number subject stack state gnus-tmp-gathered beg-match
8266         new-roots gnus-tmp-new-adopts thread-end
8267         gnus-tmp-header gnus-tmp-unread
8268         gnus-tmp-replied gnus-tmp-subject-or-nil
8269         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8270         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8271         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8272
8273     (setq gnus-tmp-prev-subject nil)
8274
8275     (if (vectorp (car threads))
8276         ;; If this is a straight (sic) list of headers, then a
8277         ;; threaded summary display isn't required, so we just create
8278         ;; an unthreaded one.
8279         (gnus-summary-prepare-unthreaded threads)
8280
8281       ;; Do the threaded display.
8282
8283       (while (or threads stack gnus-tmp-new-adopts new-roots)
8284
8285         (if (and (= gnus-tmp-level 0)
8286                  (not (setq gnus-tmp-dummy-line nil))
8287                  (or (not stack)
8288                      (= (caar stack) 0))
8289                  (not gnus-tmp-false-parent)
8290                  (or gnus-tmp-new-adopts new-roots))
8291             (if gnus-tmp-new-adopts
8292                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8293                       thread (list (car gnus-tmp-new-adopts))
8294                       gnus-tmp-header (caar thread)
8295                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8296               (if new-roots
8297                   (setq thread (list (car new-roots))
8298                         gnus-tmp-header (caar thread)
8299                         new-roots (cdr new-roots))))
8300
8301           (if threads
8302               ;; If there are some threads, we do them before the
8303               ;; threads on the stack.
8304               (setq thread threads
8305                     gnus-tmp-header (caar thread))
8306             ;; There were no current threads, so we pop something off
8307             ;; the stack.
8308             (setq state (car stack)
8309                   gnus-tmp-level (car state)
8310                   thread (cdr state)
8311                   stack (cdr stack)
8312                   gnus-tmp-header (caar thread))))
8313
8314         (setq gnus-tmp-false-parent nil)
8315         (setq gnus-tmp-root-expunged nil)
8316         (setq thread-end nil)
8317
8318         (if (stringp gnus-tmp-header)
8319             ;; The header is a dummy root.
8320             (cond
8321              ((eq gnus-summary-make-false-root 'adopt)
8322               ;; We let the first article adopt the rest.
8323               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8324                                                (cddar thread)))
8325               (setq gnus-tmp-gathered
8326                     (nconc (mapcar
8327                             (lambda (h) (mail-header-number (car h)))
8328                             (cddar thread))
8329                            gnus-tmp-gathered))
8330               (setq thread (cons (list (caar thread)
8331                                        (cadar thread))
8332                                  (cdr thread)))
8333               (setq gnus-tmp-level -1
8334                     gnus-tmp-false-parent t))
8335              ((eq gnus-summary-make-false-root 'empty)
8336               ;; We print adopted articles with empty subject fields.
8337               (setq gnus-tmp-gathered
8338                     (nconc (mapcar
8339                             (lambda (h) (mail-header-number (car h)))
8340                             (cddar thread))
8341                            gnus-tmp-gathered))
8342               (setq gnus-tmp-level -1))
8343              ((eq gnus-summary-make-false-root 'dummy)
8344               ;; We remember that we probably want to output a dummy
8345               ;; root.
8346               (setq gnus-tmp-dummy-line gnus-tmp-header)
8347               (setq gnus-tmp-prev-subject gnus-tmp-header))
8348              (t
8349               ;; We do not make a root for the gathered
8350               ;; sub-threads at all.
8351               (setq gnus-tmp-level -1)))
8352
8353           (setq number (mail-header-number gnus-tmp-header)
8354                 subject (mail-header-subject gnus-tmp-header))
8355
8356           (cond
8357            ;; If the thread has changed subject, we might want to make
8358            ;; this subthread into a root.
8359            ((and (null gnus-thread-ignore-subject)
8360                  (not (zerop gnus-tmp-level))
8361                  gnus-tmp-prev-subject
8362                  (not (inline
8363                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8364             (setq new-roots (nconc new-roots (list (car thread)))
8365                   thread-end t
8366                   gnus-tmp-header nil))
8367            ;; If the article lies outside the current limit,
8368            ;; then we do not display it.
8369            ((and (not (memq number gnus-newsgroup-limit))
8370                  (not gnus-tmp-dummy-line))
8371             (setq gnus-tmp-gathered
8372                   (nconc (mapcar
8373                           (lambda (h) (mail-header-number (car h)))
8374                           (cdar thread))
8375                          gnus-tmp-gathered))
8376             (setq gnus-tmp-new-adopts (if (cdar thread)
8377                                           (append gnus-tmp-new-adopts
8378                                                   (cdar thread))
8379                                         gnus-tmp-new-adopts)
8380                   thread-end t
8381                   gnus-tmp-header nil)
8382             (when (zerop gnus-tmp-level)
8383               (setq gnus-tmp-root-expunged t)))
8384            ;; Perhaps this article is to be marked as read?
8385            ((and gnus-summary-mark-below
8386                  (< (or (cdr (assq number gnus-newsgroup-scored))
8387                         default-score)
8388                     gnus-summary-mark-below)
8389                  ;; Don't touch sparse articles.
8390                  (not (memq number gnus-newsgroup-sparse)))
8391             (setq gnus-newsgroup-unreads
8392                   (delq number gnus-newsgroup-unreads))
8393             (if gnus-newsgroup-auto-expire
8394                 (push number gnus-newsgroup-expirable)
8395               (push (cons number gnus-low-score-mark)
8396                     gnus-newsgroup-reads))))
8397
8398           (when gnus-tmp-header
8399             ;; We may have an old dummy line to output before this
8400             ;; article.
8401             (when gnus-tmp-dummy-line
8402               (gnus-summary-insert-dummy-line
8403                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8404               (setq gnus-tmp-dummy-line nil))
8405
8406             ;; Compute the mark.
8407             (setq
8408              gnus-tmp-unread
8409              (cond
8410               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8411               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8412               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8413               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8414               (t (or (cdr (assq number gnus-newsgroup-reads))
8415                      gnus-ancient-mark))))
8416
8417             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8418                                   gnus-tmp-header gnus-tmp-level)
8419                   gnus-newsgroup-data)
8420
8421             ;; Actually insert the line.
8422             (setq
8423              gnus-tmp-subject-or-nil
8424              (cond
8425               ((and gnus-thread-ignore-subject
8426                     gnus-tmp-prev-subject
8427                     (not (inline (gnus-subject-equal
8428                                   gnus-tmp-prev-subject subject))))
8429                subject)
8430               ((zerop gnus-tmp-level)
8431                (if (and (eq gnus-summary-make-false-root 'empty)
8432                         (memq number gnus-tmp-gathered)
8433                         gnus-tmp-prev-subject
8434                         (inline (gnus-subject-equal
8435                                  gnus-tmp-prev-subject subject)))
8436                    gnus-summary-same-subject
8437                  subject))
8438               (t gnus-summary-same-subject)))
8439             (if (and (eq gnus-summary-make-false-root 'adopt)
8440                      (= gnus-tmp-level 1)
8441                      (memq number gnus-tmp-gathered))
8442                 (setq gnus-tmp-opening-bracket ?\<
8443                       gnus-tmp-closing-bracket ?\>)
8444               (setq gnus-tmp-opening-bracket ?\[
8445                     gnus-tmp-closing-bracket ?\]))
8446             (setq
8447              gnus-tmp-indentation
8448              (aref gnus-thread-indent-array gnus-tmp-level)
8449              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8450              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8451                                 gnus-summary-default-score 0)
8452              gnus-tmp-score-char
8453              (if (or (null gnus-summary-default-score)
8454                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8455                          gnus-summary-zcore-fuzz)) ? 
8456                (if (< gnus-tmp-score gnus-summary-default-score)
8457                    gnus-score-below-mark gnus-score-over-mark))
8458              gnus-tmp-replied
8459              (cond ((memq number gnus-newsgroup-processable)
8460                     gnus-process-mark)
8461                    ((memq number gnus-newsgroup-cached)
8462                     gnus-cached-mark)
8463                    ((memq number gnus-newsgroup-replied)
8464                     gnus-replied-mark)
8465                    (t gnus-unread-mark))
8466              gnus-tmp-from (mail-header-from gnus-tmp-header)
8467              gnus-tmp-name
8468              (cond
8469               ((string-match "(.+)" gnus-tmp-from)
8470                (substring gnus-tmp-from
8471                           (1+ (match-beginning 0)) (1- (match-end 0))))
8472               ((string-match "<[^>]+> *$" gnus-tmp-from)
8473                (setq beg-match (match-beginning 0))
8474                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8475                         (substring gnus-tmp-from (1+ (match-beginning 0))
8476                                    (1- (match-end 0))))
8477                    (substring gnus-tmp-from 0 beg-match)))
8478               (t gnus-tmp-from)))
8479             (when (string= gnus-tmp-name "")
8480               (setq gnus-tmp-name gnus-tmp-from))
8481             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8482             (put-text-property
8483              (point)
8484              (progn (eval gnus-summary-line-format-spec) (point))
8485              'gnus-number number)
8486             (when gnus-visual-p
8487               (forward-line -1)
8488               (run-hooks 'gnus-summary-update-hook)
8489               (forward-line 1))
8490
8491             (setq gnus-tmp-prev-subject subject)))
8492
8493         (when (nth 1 thread)
8494           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8495         (incf gnus-tmp-level)
8496         (setq threads (if thread-end nil (cdar thread)))
8497         (unless threads
8498           (setq gnus-tmp-level 0)))))
8499   (gnus-message 7 "Generating summary...done"))
8500
8501 (defun gnus-summary-prepare-unthreaded (headers)
8502   "Generate an unthreaded summary buffer based on HEADERS."
8503   (let (header number mark)
8504
8505     (while headers
8506       ;; We may have to root out some bad articles...
8507       (when (memq (setq number (mail-header-number
8508                                 (setq header (pop headers))))
8509                   gnus-newsgroup-limit)
8510         ;; Mark article as read when it has a low score.
8511         (when (and gnus-summary-mark-below
8512                    (< (or (cdr (assq number gnus-newsgroup-scored))
8513                           gnus-summary-default-score 0)
8514                       gnus-summary-mark-below))
8515           (setq gnus-newsgroup-unreads
8516                 (delq number gnus-newsgroup-unreads))
8517           (if gnus-newsgroup-auto-expire
8518               (push number gnus-newsgroup-expirable)
8519             (push (cons number gnus-low-score-mark)
8520                   gnus-newsgroup-reads)))
8521
8522         (setq mark
8523               (cond
8524                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8525                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8526                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8527                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8528                (t (or (cdr (assq number gnus-newsgroup-reads))
8529                       gnus-ancient-mark))))
8530         (setq gnus-newsgroup-data
8531               (cons (gnus-data-make number mark (1+ (point)) header 0)
8532                     gnus-newsgroup-data))
8533         (gnus-summary-insert-line
8534          header 0 nil mark (memq number gnus-newsgroup-replied)
8535          (memq number gnus-newsgroup-expirable)
8536          (mail-header-subject header) nil
8537          (cdr (assq number gnus-newsgroup-scored))
8538          (memq number gnus-newsgroup-processable))))))
8539
8540 (defun gnus-select-newsgroup (group &optional read-all)
8541   "Select newsgroup GROUP.
8542 If READ-ALL is non-nil, all articles in the group are selected."
8543   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8544          (info (nth 2 entry))
8545          articles fetched-articles cached)
8546
8547     (or (gnus-check-server
8548          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8549         (error "Couldn't open server"))
8550
8551     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8552         (gnus-activate-group group) ; Or we can activate it...
8553         (progn ; Or we bug out.
8554           (when (equal major-mode 'gnus-summary-mode)
8555             (kill-buffer (current-buffer)))
8556           (error "Couldn't request group %s: %s"
8557                  group (gnus-status-message group))))
8558
8559     (setq gnus-newsgroup-name group)
8560     (setq gnus-newsgroup-unselected nil)
8561     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8562
8563     (and gnus-asynchronous
8564          (gnus-check-backend-function
8565           'request-asynchronous gnus-newsgroup-name)
8566          (setq gnus-newsgroup-async
8567                (gnus-request-asynchronous gnus-newsgroup-name)))
8568
8569     ;; Adjust and set lists of article marks.
8570     (when info
8571       (gnus-adjust-marked-articles info))
8572
8573     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8574     (when (gnus-virtual-group-p group)
8575       (setq cached gnus-newsgroup-cached))
8576
8577     (setq gnus-newsgroup-unreads
8578           (gnus-set-difference
8579            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8580            gnus-newsgroup-dormant))
8581
8582     (setq gnus-newsgroup-processable nil)
8583
8584     (setq articles (gnus-articles-to-read group read-all))
8585
8586     (cond
8587      ((null articles)
8588       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8589       'quit)
8590      ((eq articles 0) nil)
8591      (t
8592       ;; Init the dependencies hash table.
8593       (setq gnus-newsgroup-dependencies
8594             (gnus-make-hashtable (length articles)))
8595       ;; Retrieve the headers and read them in.
8596       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8597       (setq gnus-newsgroup-headers
8598             (if (eq 'nov
8599                     (setq gnus-headers-retrieved-by
8600                           (gnus-retrieve-headers
8601                            articles gnus-newsgroup-name
8602                            ;; We might want to fetch old headers, but
8603                            ;; not if there is only 1 article.
8604                            (and gnus-fetch-old-headers
8605                                 (or (and
8606                                      (not (eq gnus-fetch-old-headers 'some))
8607                                      (not (numberp gnus-fetch-old-headers)))
8608                                     (> (length articles) 1))))))
8609                 (gnus-get-newsgroup-headers-xover articles)
8610               (gnus-get-newsgroup-headers)))
8611       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8612
8613       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8614       (when cached
8615         (setq gnus-newsgroup-cached cached))
8616
8617       ;; Set the initial limit.
8618       (setq gnus-newsgroup-limit (copy-sequence articles))
8619       ;; Remove canceled articles from the list of unread articles.
8620       (setq gnus-newsgroup-unreads
8621             (gnus-set-sorted-intersection
8622              gnus-newsgroup-unreads
8623              (setq fetched-articles
8624                    (mapcar (lambda (headers) (mail-header-number headers))
8625                            gnus-newsgroup-headers))))
8626       ;; Removed marked articles that do not exist.
8627       (gnus-update-missing-marks
8628        (gnus-sorted-complement fetched-articles articles))
8629       ;; We might want to build some more threads first.
8630       (and gnus-fetch-old-headers
8631            (eq gnus-headers-retrieved-by 'nov)
8632            (gnus-build-old-threads))
8633       ;; Check whether auto-expire is to be done in this group.
8634       (setq gnus-newsgroup-auto-expire
8635             (gnus-group-auto-expirable-p group))
8636       ;; Set up the article buffer now, if necessary.
8637       (unless gnus-single-article-buffer
8638         (gnus-article-setup-buffer))
8639       ;; First and last article in this newsgroup.
8640       (and gnus-newsgroup-headers
8641            (setq gnus-newsgroup-begin
8642                  (mail-header-number (car gnus-newsgroup-headers)))
8643            (setq gnus-newsgroup-end
8644                  (mail-header-number
8645                   (gnus-last-element gnus-newsgroup-headers))))
8646       (setq gnus-reffed-article-number -1)
8647       ;; GROUP is successfully selected.
8648       (or gnus-newsgroup-headers t)))))
8649
8650 (defun gnus-articles-to-read (group read-all)
8651   ;; Find out what articles the user wants to read.
8652   (let* ((articles
8653           ;; Select all articles if `read-all' is non-nil, or if there
8654           ;; are no unread articles.
8655           (if (or read-all
8656                   (and (zerop (length gnus-newsgroup-marked))
8657                        (zerop (length gnus-newsgroup-unreads))))
8658               (gnus-uncompress-range (gnus-active group))
8659             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8660                           (copy-sequence gnus-newsgroup-unreads))
8661                   '<)))
8662          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8663          (scored (length scored-list))
8664          (number (length articles))
8665          (marked (+ (length gnus-newsgroup-marked)
8666                     (length gnus-newsgroup-dormant)))
8667          (select
8668           (cond
8669            ((numberp read-all)
8670             read-all)
8671            (t
8672             (condition-case ()
8673                 (cond
8674                  ((and (or (<= scored marked) (= scored number))
8675                        (numberp gnus-large-newsgroup)
8676                        (> number gnus-large-newsgroup))
8677                   (let ((input
8678                          (read-string
8679                           (format
8680                            "How many articles from %s (default %d): "
8681                            gnus-newsgroup-name number))))
8682                     (if (string-match "^[ \t]*$" input) number input)))
8683                  ((and (> scored marked) (< scored number))
8684                   (let ((input
8685                          (read-string
8686                           (format "%s %s (%d scored, %d total): "
8687                                   "How many articles from"
8688                                   group scored number))))
8689                     (if (string-match "^[ \t]*$" input)
8690                         number input)))
8691                  (t number))
8692               (quit nil))))))
8693     (setq select (if (stringp select) (string-to-number select) select))
8694     (if (or (null select) (zerop select))
8695         select
8696       (if (and (not (zerop scored)) (<= (abs select) scored))
8697           (progn
8698             (setq articles (sort scored-list '<))
8699             (setq number (length articles)))
8700         (setq articles (copy-sequence articles)))
8701
8702       (if (< (abs select) number)
8703           (if (< select 0)
8704               ;; Select the N oldest articles.
8705               (setcdr (nthcdr (1- (abs select)) articles) nil)
8706             ;; Select the N most recent articles.
8707             (setq articles (nthcdr (- number select) articles))))
8708       (setq gnus-newsgroup-unselected
8709             (gnus-sorted-intersection
8710              gnus-newsgroup-unreads
8711              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8712       articles)))
8713
8714 (defun gnus-killed-articles (killed articles)
8715   (let (out)
8716     (while articles
8717       (if (inline (gnus-member-of-range (car articles) killed))
8718           (setq out (cons (car articles) out)))
8719       (setq articles (cdr articles)))
8720     out))
8721
8722 (defun gnus-uncompress-marks (marks)
8723   "Uncompress the mark ranges in MARKS."
8724   (let ((uncompressed '(score bookmark))
8725         out)
8726     (while marks
8727       (if (memq (caar marks) uncompressed)
8728           (push (car marks) out)
8729         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8730       (setq marks (cdr marks)))
8731     out))
8732
8733 (defun gnus-adjust-marked-articles (info)
8734   "Set all article lists and remove all marks that are no longer legal."
8735   (let* ((marked-lists (gnus-info-marks info))
8736          (active (gnus-active (gnus-info-group info)))
8737          (min (car active))
8738          (max (cdr active))
8739          (types gnus-article-mark-lists)
8740          (uncompressed '(score bookmark))
8741          marks var articles article mark)
8742
8743     (while marked-lists
8744       (setq marks (pop marked-lists))
8745       (set (setq var (intern (format "gnus-newsgroup-%s"
8746                                      (car (rassq (setq mark (car marks))
8747                                                  types)))))
8748            (if (memq (car marks) uncompressed) (cdr marks)
8749              (gnus-uncompress-range (cdr marks))))
8750
8751       (setq articles (symbol-value var))
8752
8753       ;; All articles have to be subsets of the active articles.
8754       (cond
8755        ;; Adjust "simple" lists.
8756        ((memq mark '(tick dormant expirable reply killed save))
8757         (while articles
8758           (when (or (< (setq article (pop articles)) min) (> article max))
8759             (set var (delq article (symbol-value var))))))
8760        ;; Adjust assocs.
8761        ((memq mark '(score bookmark))
8762         (while articles
8763           (when (or (< (car (setq article (pop articles))) min)
8764                     (> (car article) max))
8765             (set var (delq article (symbol-value var))))))))))
8766
8767 (defun gnus-update-missing-marks (missing)
8768   "Go through the list of MISSING articles and remove them mark lists."
8769   (when missing
8770     (let ((types gnus-article-mark-lists)
8771           var m)
8772       ;; Go through all types.
8773       (while types
8774         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8775         (when (symbol-value var)
8776           ;; This list has articles.  So we delete all missing articles
8777           ;; from it.
8778           (setq m missing)
8779           (while m
8780             (set var (delq (pop m) (symbol-value var)))))))))
8781
8782 (defun gnus-update-marks ()
8783   "Enter the various lists of marked articles into the newsgroup info list."
8784   (let ((types gnus-article-mark-lists)
8785         (info (gnus-get-info gnus-newsgroup-name))
8786         (uncompressed '(score bookmark killed))
8787         type list newmarked symbol)
8788     (when info
8789       ;; Add all marks lists that are non-nil to the list of marks lists.
8790       (while types
8791         (setq type (pop types))
8792         (when (setq list (symbol-value
8793                           (setq symbol
8794                                 (intern (format "gnus-newsgroup-%s"
8795                                                 (car type))))))
8796           (push (cons (cdr type)
8797                       (if (memq (cdr type) uncompressed) list
8798                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8799                 newmarked)))
8800
8801       ;; Enter these new marks into the info of the group.
8802       (if (nthcdr 3 info)
8803           (setcar (nthcdr 3 info) newmarked)
8804         ;; Add the marks lists to the end of the info.
8805         (when newmarked
8806           (setcdr (nthcdr 2 info) (list newmarked))))
8807
8808       ;; Cut off the end of the info if there's nothing else there.
8809       (let ((i 5))
8810         (while (and (> i 2)
8811                     (not (nth i info)))
8812           (when (nthcdr (decf i) info)
8813             (setcdr (nthcdr i info) nil)))))))
8814
8815 (defun gnus-add-marked-articles (group type articles &optional info force)
8816   ;; Add ARTICLES of TYPE to the info of GROUP.
8817   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8818   ;; add, but replace marked articles of TYPE with ARTICLES.
8819   (let ((info (or info (gnus-get-info group)))
8820         (uncompressed '(score bookmark killed))
8821         marked m)
8822     (or (not info)
8823         (and (not (setq marked (nthcdr 3 info)))
8824              (or (null articles)
8825                  (setcdr (nthcdr 2 info)
8826                          (list (list (cons type (gnus-compress-sequence
8827                                                  articles t)))))))
8828         (and (not (setq m (assq type (car marked))))
8829              (or (null articles)
8830                  (setcar marked
8831                          (cons (cons type (gnus-compress-sequence articles t) )
8832                                (car marked)))))
8833         (if force
8834             (if (null articles)
8835                 (setcar (nthcdr 3 info)
8836                         (delq (assq type (car marked)) (car marked)))
8837               (setcdr m (gnus-compress-sequence articles t)))
8838           (setcdr m (gnus-compress-sequence
8839                      (sort (nconc (gnus-uncompress-range (cdr m))
8840                                   (copy-sequence articles)) '<) t))))))
8841
8842 (defun gnus-set-mode-line (where)
8843   "This function sets the mode line of the article or summary buffers.
8844 If WHERE is `summary', the summary mode line format will be used."
8845   ;; Is this mode line one we keep updated?
8846   (when (memq where gnus-updated-mode-lines)
8847     (let (mode-string)
8848       (save-excursion
8849         ;; We evaluate this in the summary buffer since these
8850         ;; variables are buffer-local to that buffer.
8851         (set-buffer gnus-summary-buffer)
8852         ;; We bind all these variables that are used in the `eval' form
8853         ;; below.
8854         (let* ((mformat (symbol-value
8855                          (intern
8856                           (format "gnus-%s-mode-line-format-spec" where))))
8857                (gnus-tmp-group-name gnus-newsgroup-name)
8858                (gnus-tmp-article-number (or gnus-current-article 0))
8859                (gnus-tmp-unread gnus-newsgroup-unreads)
8860                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8861                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8862                (gnus-tmp-unread-and-unselected
8863                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8864                             (zerop gnus-tmp-unselected)) "")
8865                       ((zerop gnus-tmp-unselected)
8866                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8867                       (t (format "{%d(+%d) more}"
8868                                  gnus-tmp-unread-and-unticked
8869                                  gnus-tmp-unselected))))
8870                (gnus-tmp-subject
8871                 (if (and gnus-current-headers
8872                          (vectorp gnus-current-headers))
8873                     (mail-header-subject gnus-current-headers) ""))
8874                max-len
8875                gnus-tmp-header);; passed as argument to any user-format-funcs
8876           (setq mode-string (eval mformat))
8877           (setq max-len (max 4 (if gnus-mode-non-string-length
8878                                    (- (frame-width)
8879                                       gnus-mode-non-string-length)
8880                                  (length mode-string))))
8881           ;; We might have to chop a bit of the string off...
8882           (when (> (length mode-string) max-len)
8883             (setq mode-string
8884                   (concat (gnus-truncate-string mode-string (- max-len 3))
8885                           "...")))
8886           ;; Pad the mode string a bit.
8887           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8888       ;; Update the mode line.
8889       (setq mode-line-buffer-identification (list mode-string))
8890       (set-buffer-modified-p t))))
8891
8892 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8893   "Go through the HEADERS list and add all Xrefs to a hash table.
8894 The resulting hash table is returned, or nil if no Xrefs were found."
8895   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8896          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8897          (xref-hashtb (make-vector 63 0))
8898          start group entry number xrefs header)
8899     (while headers
8900       (setq header (pop headers))
8901       (when (and (setq xrefs (mail-header-xref header))
8902                  (not (memq (setq number (mail-header-number header))
8903                             unreads)))
8904         (setq start 0)
8905         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8906           (setq start (match-end 0))
8907           (setq group (if prefix
8908                           (concat prefix (substring xrefs (match-beginning 1)
8909                                                     (match-end 1)))
8910                         (substring xrefs (match-beginning 1) (match-end 1))))
8911           (setq number
8912                 (string-to-int (substring xrefs (match-beginning 2)
8913                                           (match-end 2))))
8914           (if (setq entry (gnus-gethash group xref-hashtb))
8915               (setcdr entry (cons number (cdr entry)))
8916             (gnus-sethash group (cons number nil) xref-hashtb)))))
8917     (and start xref-hashtb)))
8918
8919 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8920   "Look through all the headers and mark the Xrefs as read."
8921   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8922         name entry info xref-hashtb idlist method nth4)
8923     (save-excursion
8924       (set-buffer gnus-group-buffer)
8925       (when (setq xref-hashtb
8926                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8927         (mapatoms
8928          (lambda (group)
8929            (unless (string= from-newsgroup (setq name (symbol-name group)))
8930              (setq idlist (symbol-value group))
8931              ;; Dead groups are not updated.
8932              (and (prog1
8933                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8934                             info (nth 2 entry))
8935                     (if (stringp (setq nth4 (gnus-info-method info)))
8936                         (setq nth4 (gnus-server-to-method nth4))))
8937                   ;; Only do the xrefs if the group has the same
8938                   ;; select method as the group we have just read.
8939                   (or (gnus-methods-equal-p
8940                        nth4 (gnus-find-method-for-group from-newsgroup))
8941                       virtual
8942                       (equal nth4 (setq method (gnus-find-method-for-group
8943                                                 from-newsgroup)))
8944                       (and (equal (car nth4) (car method))
8945                            (equal (nth 1 nth4) (nth 1 method))))
8946                   gnus-use-cross-reference
8947                   (or (not (eq gnus-use-cross-reference t))
8948                       virtual
8949                       ;; Only do cross-references on subscribed
8950                       ;; groups, if that is what is wanted.
8951                       (<= (gnus-info-level info) gnus-level-subscribed))
8952                   (gnus-group-make-articles-read name idlist))))
8953          xref-hashtb)))))
8954
8955 (defun gnus-group-make-articles-read (group articles)
8956   (let* ((num 0)
8957          (entry (gnus-gethash group gnus-newsrc-hashtb))
8958          (info (nth 2 entry))
8959          (active (gnus-active group))
8960          range)
8961     ;; First peel off all illegal article numbers.
8962     (if active
8963         (let ((ids articles)
8964               id first)
8965           (while ids
8966             (setq id (car ids))
8967             (if (and first (> id (cdr active)))
8968                 (progn
8969                   ;; We'll end up in this situation in one particular
8970                   ;; obscure situation.  If you re-scan a group and get
8971                   ;; a new article that is cross-posted to a different
8972                   ;; group that has not been re-scanned, you might get
8973                   ;; crossposted article that has a higher number than
8974                   ;; Gnus believes possible.  So we re-activate this
8975                   ;; group as well.  This might mean doing the
8976                   ;; crossposting thingy will *increase* the number
8977                   ;; of articles in some groups.  Tsk, tsk.
8978                   (setq active (or (gnus-activate-group group) active))))
8979             (if (or (> id (cdr active))
8980                     (< id (car active)))
8981                 (setq articles (delq id articles)))
8982             (setq ids (cdr ids)))))
8983     ;; If the read list is nil, we init it.
8984     (and active
8985          (null (gnus-info-read info))
8986          (> (car active) 1)
8987          (gnus-info-set-read info (cons 1 (1- (car active)))))
8988     ;; Then we add the read articles to the range.
8989     (gnus-info-set-read
8990      info
8991      (setq range
8992            (gnus-add-to-range
8993             (gnus-info-read info) (setq articles (sort articles '<)))))
8994     ;; Then we have to re-compute how many unread
8995     ;; articles there are in this group.
8996     (if active
8997         (progn
8998           (cond
8999            ((not range)
9000             (setq num (- (1+ (cdr active)) (car active))))
9001            ((not (listp (cdr range)))
9002             (setq num (- (cdr active) (- (1+ (cdr range))
9003                                          (car range)))))
9004            (t
9005             (while range
9006               (if (numberp (car range))
9007                   (setq num (1+ num))
9008                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9009               (setq range (cdr range)))
9010             (setq num (- (cdr active) num))))
9011           ;; Update the number of unread articles.
9012           (setcar entry num)
9013           ;; Update the group buffer.
9014           (gnus-group-update-group group t)))))
9015
9016 (defun gnus-methods-equal-p (m1 m2)
9017   (let ((m1 (or m1 gnus-select-method))
9018         (m2 (or m2 gnus-select-method)))
9019     (or (equal m1 m2)
9020         (and (eq (car m1) (car m2))
9021              (or (not (memq 'address (assoc (symbol-name (car m1))
9022                                             gnus-valid-select-methods)))
9023                  (equal (nth 1 m1) (nth 1 m2)))))))
9024
9025 (defsubst gnus-header-value ()
9026   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9027
9028 (defvar gnus-newsgroup-none-id 0)
9029
9030 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9031   (let ((cur nntp-server-buffer)
9032         (dependencies
9033          (or dependencies
9034              (save-excursion (set-buffer gnus-summary-buffer)
9035                              gnus-newsgroup-dependencies)))
9036         headers id id-dep ref-dep end ref)
9037     (save-excursion
9038       (set-buffer nntp-server-buffer)
9039       (run-hooks 'gnus-parse-headers-hook)
9040       (let ((case-fold-search t)
9041             in-reply-to header p lines)
9042         (goto-char (point-min))
9043         ;; Search to the beginning of the next header.  Error messages
9044         ;; do not begin with 2 or 3.
9045         (while (re-search-forward "^[23][0-9]+ " nil t)
9046           (setq id nil
9047                 ref nil)
9048           ;; This implementation of this function, with nine
9049           ;; search-forwards instead of the one re-search-forward and
9050           ;; a case (which basically was the old function) is actually
9051           ;; about twice as fast, even though it looks messier.  You
9052           ;; can't have everything, I guess.  Speed and elegance
9053           ;; doesn't always go hand in hand.
9054           (setq
9055            header
9056            (vector
9057             ;; Number.
9058             (prog1
9059                 (read cur)
9060               (end-of-line)
9061               (setq p (point))
9062               (narrow-to-region (point)
9063                                 (or (and (search-forward "\n.\n" nil t)
9064                                          (- (point) 2))
9065                                     (point))))
9066             ;; Subject.
9067             (progn
9068               (goto-char p)
9069               (if (search-forward "\nsubject: " nil t)
9070                   (gnus-header-value) "(none)"))
9071             ;; From.
9072             (progn
9073               (goto-char p)
9074               (if (search-forward "\nfrom: " nil t)
9075                   (gnus-header-value) "(nobody)"))
9076             ;; Date.
9077             (progn
9078               (goto-char p)
9079               (if (search-forward "\ndate: " nil t)
9080                   (gnus-header-value) ""))
9081             ;; Message-ID.
9082             (progn
9083               (goto-char p)
9084               (if (search-forward "\nmessage-id: " nil t)
9085                   (setq id (gnus-header-value))
9086                 ;; If there was no message-id, we just fake one to make
9087                 ;; subsequent routines simpler.
9088                 (setq id (concat "none+"
9089                                  (int-to-string
9090                                   (setq gnus-newsgroup-none-id
9091                                         (1+ gnus-newsgroup-none-id)))))))
9092             ;; References.
9093             (progn
9094               (goto-char p)
9095               (if (search-forward "\nreferences: " nil t)
9096                   (prog1
9097                       (gnus-header-value)
9098                     (setq end (match-end 0))
9099                     (save-excursion
9100                       (setq ref
9101                             (buffer-substring
9102                              (progn
9103                                (end-of-line)
9104                                (search-backward ">" end t)
9105                                (1+ (point)))
9106                              (progn
9107                                (search-backward "<" end t)
9108                                (point))))))
9109                 ;; Get the references from the in-reply-to header if there
9110                 ;; were no references and the in-reply-to header looks
9111                 ;; promising.
9112                 (if (and (search-forward "\nin-reply-to: " nil t)
9113                          (setq in-reply-to (gnus-header-value))
9114                          (string-match "<[^>]+>" in-reply-to))
9115                     (setq ref (substring in-reply-to (match-beginning 0)
9116                                          (match-end 0)))
9117                   (setq ref ""))))
9118             ;; Chars.
9119             0
9120             ;; Lines.
9121             (progn
9122               (goto-char p)
9123               (if (search-forward "\nlines: " nil t)
9124                   (if (numberp (setq lines (read cur)))
9125                       lines 0)
9126                 0))
9127             ;; Xref.
9128             (progn
9129               (goto-char p)
9130               (and (search-forward "\nxref: " nil t)
9131                    (gnus-header-value)))))
9132           ;; We do the threading while we read the headers.  The
9133           ;; message-id and the last reference are both entered into
9134           ;; the same hash table.  Some tippy-toeing around has to be
9135           ;; done in case an article has arrived before the article
9136           ;; which it refers to.
9137           (if (boundp (setq id-dep (intern id dependencies)))
9138               (if (and (car (symbol-value id-dep))
9139                        (not force-new))
9140                   ;; An article with this Message-ID has already
9141                   ;; been seen, so we ignore this one, except we add
9142                   ;; any additional Xrefs (in case the two articles
9143                   ;; came from different servers).
9144                   (progn
9145                     (mail-header-set-xref
9146                      (car (symbol-value id-dep))
9147                      (concat (or (mail-header-xref
9148                                   (car (symbol-value id-dep))) "")
9149                              (or (mail-header-xref header) "")))
9150                     (setq header nil))
9151                 (setcar (symbol-value id-dep) header))
9152             (set id-dep (list header)))
9153           (when header
9154             (if (boundp (setq ref-dep (intern ref dependencies)))
9155                 (setcdr (symbol-value ref-dep)
9156                         (nconc (cdr (symbol-value ref-dep))
9157                                (list (symbol-value id-dep))))
9158               (set ref-dep (list nil (symbol-value id-dep))))
9159             (setq headers (cons header headers)))
9160           (goto-char (point-max))
9161           (widen))
9162         (nreverse headers)))))
9163
9164 ;; The following macros and functions were written by Felix Lee
9165 ;; <flee@cse.psu.edu>.
9166
9167 (defmacro gnus-nov-read-integer ()
9168   '(prog1
9169        (if (= (following-char) ?\t)
9170            0
9171          (let ((num (condition-case nil (read buffer) (error nil))))
9172            (if (numberp num) num 0)))
9173      (or (eobp) (forward-char 1))))
9174
9175 (defmacro gnus-nov-skip-field ()
9176   '(search-forward "\t" eol 'move))
9177
9178 (defmacro gnus-nov-field ()
9179   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9180
9181 ;; Goes through the xover lines and returns a list of vectors
9182 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9183                                                   force-new dependencies)
9184   "Parse the news overview data in the server buffer, and return a
9185 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9186   ;; Get the Xref when the users reads the articles since most/some
9187   ;; NNTP servers do not include Xrefs when using XOVER.
9188   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9189   (let ((cur nntp-server-buffer)
9190         (dependencies (or dependencies gnus-newsgroup-dependencies))
9191         number headers header)
9192     (save-excursion
9193       (set-buffer nntp-server-buffer)
9194       ;; Allow the user to mangle the headers before parsing them.
9195       (run-hooks 'gnus-parse-headers-hook)
9196       (goto-char (point-min))
9197       (while (and sequence (not (eobp)))
9198         (setq number (read cur))
9199         (while (and sequence (< (car sequence) number))
9200           (setq sequence (cdr sequence)))
9201         (and sequence
9202              (eq number (car sequence))
9203              (progn
9204                (setq sequence (cdr sequence))
9205                (if (setq header
9206                          (inline (gnus-nov-parse-line
9207                                   number dependencies force-new)))
9208                    (setq headers (cons header headers)))))
9209         (forward-line 1))
9210       (setq headers (nreverse headers)))
9211     headers))
9212
9213 ;; This function has to be called with point after the article number
9214 ;; on the beginning of the line.
9215 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9216   (let ((none 0)
9217         (eol (gnus-point-at-eol))
9218         (buffer (current-buffer))
9219         header ref id id-dep ref-dep)
9220
9221     ;; overview: [num subject from date id refs chars lines misc]
9222     (narrow-to-region (point) eol)
9223     (or (eobp) (forward-char))
9224
9225     (condition-case nil
9226         (setq header
9227               (vector
9228                number                   ; number
9229                (gnus-nov-field)         ; subject
9230                (gnus-nov-field)         ; from
9231                (gnus-nov-field)         ; date
9232                (setq id (or (gnus-nov-field)
9233                             (concat "none+"
9234                                     (int-to-string
9235                                      (setq none (1+ none)))))) ; id
9236                (progn
9237                  (save-excursion
9238                    (let ((beg (point)))
9239                      (search-forward "\t" eol)
9240                      (if (search-backward ">" beg t)
9241                          (setq ref
9242                                (buffer-substring
9243                                 (1+ (point))
9244                                 (search-backward "<" beg t)))
9245                        (setq ref nil))))
9246                  (gnus-nov-field))      ; refs
9247                (gnus-nov-read-integer)  ; chars
9248                (gnus-nov-read-integer)  ; lines
9249                (if (= (following-char) ?\n)
9250                    nil
9251                  (gnus-nov-field))      ; misc
9252                ))
9253       (error (progn
9254                (gnus-error 4 "Strange nov line")
9255                (setq header nil)
9256                (goto-char eol))))
9257
9258     (widen)
9259
9260     ;; We build the thread tree.
9261     (when header
9262       (if (boundp (setq id-dep (intern id dependencies)))
9263           (if (and (car (symbol-value id-dep))
9264                    (not force-new))
9265               ;; An article with this Message-ID has already been seen,
9266               ;; so we ignore this one, except we add any additional
9267               ;; Xrefs (in case the two articles came from different
9268               ;; servers.
9269               (progn
9270                 (mail-header-set-xref
9271                  (car (symbol-value id-dep))
9272                  (concat (or (mail-header-xref
9273                               (car (symbol-value id-dep))) "")
9274                          (or (mail-header-xref header) "")))
9275                 (setq header nil))
9276             (setcar (symbol-value id-dep) header))
9277         (set id-dep (list header))))
9278     (if header
9279         (progn
9280           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9281               (setcdr (symbol-value ref-dep)
9282                       (nconc (cdr (symbol-value ref-dep))
9283                              (list (symbol-value id-dep))))
9284             (set ref-dep (list nil (symbol-value id-dep))))))
9285     header))
9286
9287 (defun gnus-article-get-xrefs ()
9288   "Fill in the Xref value in `gnus-current-headers', if necessary.
9289 This is meant to be called in `gnus-article-internal-prepare-hook'."
9290   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9291                                  gnus-current-headers)))
9292     (or (not gnus-use-cross-reference)
9293         (not headers)
9294         (and (mail-header-xref headers)
9295              (not (string= (mail-header-xref headers) "")))
9296         (let ((case-fold-search t)
9297               xref)
9298           (save-restriction
9299             (nnheader-narrow-to-headers)
9300             (goto-char (point-min))
9301             (if (or (and (eq (downcase (following-char)) ?x)
9302                          (looking-at "Xref:"))
9303                     (search-forward "\nXref:" nil t))
9304                 (progn
9305                   (goto-char (1+ (match-end 0)))
9306                   (setq xref (buffer-substring (point)
9307                                                (progn (end-of-line) (point))))
9308                   (mail-header-set-xref headers xref))))))))
9309
9310 (defun gnus-summary-insert-subject (id &optional old-header)
9311   "Find article ID and insert the summary line for that article."
9312   (let ((header (gnus-read-header id))
9313         (number (and (numberp id) id))
9314         pos)
9315     (when header
9316       ;; Rebuild the thread that this article is part of and go to the
9317       ;; article we have fetched.
9318       (when old-header
9319         (when (setq pos (text-property-any
9320                          (point-min) (point-max) 'gnus-number 
9321                          (mail-header-number old-header)))
9322           (goto-char pos)
9323           (gnus-delete-line)
9324           (gnus-data-remove (mail-header-number old-header))))
9325       (gnus-rebuild-thread (mail-header-id header))
9326       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9327     (when (and (numberp number)
9328                (> number 0))
9329       ;; We have to update the boundaries even if we can't fetch the
9330       ;; article if ID is a number -- so that the next `P' or `N'
9331       ;; command will fetch the previous (or next) article even
9332       ;; if the one we tried to fetch this time has been canceled.
9333       (and (> number gnus-newsgroup-end)
9334            (setq gnus-newsgroup-end number))
9335       (and (< number gnus-newsgroup-begin)
9336            (setq gnus-newsgroup-begin number))
9337       (setq gnus-newsgroup-unselected
9338             (delq number gnus-newsgroup-unselected)))
9339     ;; Report back a success?
9340     (and header (mail-header-number header))))
9341
9342 (defun gnus-summary-work-articles (n)
9343   "Return a list of articles to be worked upon.  The prefix argument,
9344 the list of process marked articles, and the current article will be
9345 taken into consideration."
9346   (cond
9347    ((and n (numberp n))
9348     ;; A numerical prefix has been given.
9349     (let ((backward (< n 0))
9350           (n (abs n))
9351           articles article)
9352       (save-excursion
9353         (while
9354             (and (> n 0)
9355                  (push (setq article (gnus-summary-article-number))
9356                        articles)
9357                  (if backward
9358                      (gnus-summary-find-prev nil article)
9359                    (gnus-summary-find-next nil article)))
9360           (decf n)))
9361       (nreverse articles)))
9362    ((and (boundp 'transient-mark-mode)
9363          transient-mark-mode
9364          mark-active)
9365     ;; Work on the region between point and mark.
9366     (let ((max (max (point) (mark)))
9367           articles article)
9368       (save-excursion
9369         (goto-char (min (point) (mark)))
9370         (while
9371             (and
9372              (push (setq article (gnus-summary-article-number)) articles)
9373              (gnus-summary-find-next nil article)
9374              (< (point) max)))
9375         (nreverse articles))))
9376    (gnus-newsgroup-processable
9377     ;; There are process-marked articles present.
9378     (reverse gnus-newsgroup-processable))
9379    (t
9380     ;; Just return the current article.
9381     (list (gnus-summary-article-number)))))
9382
9383 (defun gnus-summary-search-group (&optional backward use-level)
9384   "Search for next unread newsgroup.
9385 If optional argument BACKWARD is non-nil, search backward instead."
9386   (save-excursion
9387     (set-buffer gnus-group-buffer)
9388     (if (gnus-group-search-forward
9389          backward nil (if use-level (gnus-group-group-level) nil))
9390         (gnus-group-group-name))))
9391
9392 (defun gnus-summary-best-group (&optional exclude-group)
9393   "Find the name of the best unread group.
9394 If EXCLUDE-GROUP, do not go to this group."
9395   (save-excursion
9396     (set-buffer gnus-group-buffer)
9397     (save-excursion
9398       (gnus-group-best-unread-group exclude-group))))
9399
9400 (defun gnus-summary-find-next (&optional unread article backward)
9401   (if backward (gnus-summary-find-prev)
9402     (let* ((dummy (gnus-summary-article-intangible-p))
9403            (article (or article (gnus-summary-article-number)))
9404            (arts (gnus-data-find-list article))
9405            result)
9406       (when (and (not dummy)
9407                  (or (not gnus-summary-check-current)
9408                      (not unread)
9409                      (not (gnus-data-unread-p (car arts)))))
9410         (setq arts (cdr arts)))
9411       (when (setq result
9412                   (if unread
9413                       (progn
9414                         (while arts
9415                           (when (gnus-data-unread-p (car arts))
9416                             (setq result (car arts)
9417                                   arts nil))
9418                           (setq arts (cdr arts)))
9419                         result)
9420                     (car arts)))
9421         (goto-char (gnus-data-pos result))
9422         (gnus-data-number result)))))
9423
9424 (defun gnus-summary-find-prev (&optional unread article)
9425   (let* ((article (or article (gnus-summary-article-number)))
9426          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9427          result)
9428     (when (or (not gnus-summary-check-current)
9429               (not unread)
9430               (not (gnus-data-unread-p (car arts))))
9431       (setq arts (cdr arts)))
9432     (if (setq result
9433               (if unread
9434                   (progn
9435                     (while arts
9436                       (and (gnus-data-unread-p (car arts))
9437                            (setq result (car arts)
9438                                  arts nil))
9439                       (setq arts (cdr arts)))
9440                     result)
9441                 (car arts)))
9442         (progn
9443           (goto-char (gnus-data-pos result))
9444           (gnus-data-number result)))))
9445
9446 (defun gnus-summary-find-subject (subject &optional unread backward article)
9447   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9448          (article (or article (gnus-summary-article-number)))
9449          (articles (gnus-data-list backward))
9450          (arts (gnus-data-find-list article articles))
9451          result)
9452     (when (or (not gnus-summary-check-current)
9453               (not unread)
9454               (not (gnus-data-unread-p (car arts))))
9455       (setq arts (cdr arts)))
9456     (while arts
9457       (and (or (not unread)
9458                (gnus-data-unread-p (car arts)))
9459            (vectorp (gnus-data-header (car arts)))
9460            (gnus-subject-equal
9461             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9462            (setq result (car arts)
9463                  arts nil))
9464       (setq arts (cdr arts)))
9465     (and result
9466          (goto-char (gnus-data-pos result))
9467          (gnus-data-number result))))
9468
9469 (defun gnus-summary-search-forward (&optional unread subject backward)
9470   "Search forward for an article.
9471 If UNREAD, look for unread articles.  If SUBJECT, look for
9472 articles with that subject.  If BACKWARD, search backward instead."
9473   (cond (subject (gnus-summary-find-subject subject unread backward))
9474         (backward (gnus-summary-find-prev unread))
9475         (t (gnus-summary-find-next unread))))
9476
9477 (defun gnus-recenter (&optional n)
9478   "Center point in window and redisplay frame.
9479 Also do horizontal recentering."
9480   (interactive "P")
9481   (when (and gnus-auto-center-summary
9482              (not (eq gnus-auto-center-summary 'vertical)))
9483     (gnus-horizontal-recenter))
9484   (recenter n))
9485
9486 (defun gnus-summary-recenter ()
9487   "Center point in the summary window.
9488 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9489 displayed, no centering will be performed."
9490   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9491   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9492   (let* ((top (cond ((< (window-height) 4) 0)
9493                     ((< (window-height) 7) 1)
9494                     (t 2)))
9495          (height (1- (window-height)))
9496          (bottom (save-excursion (goto-char (point-max))
9497                                  (forward-line (- height))
9498                                  (point)))
9499          (window (get-buffer-window (current-buffer))))
9500     ;; The user has to want it.
9501     (when gnus-auto-center-summary
9502       (when (get-buffer-window gnus-article-buffer)
9503        ;; Only do recentering when the article buffer is displayed,
9504        ;; Set the window start to either `bottom', which is the biggest
9505        ;; possible valid number, or the second line from the top,
9506        ;; whichever is the least.
9507        (set-window-start
9508         window (min bottom (save-excursion 
9509                              (forward-line (- top)) (point)))))
9510       ;; Do horizontal recentering while we're at it.
9511       (when (and (get-buffer-window (current-buffer) t)
9512                  (not (eq gnus-auto-center-summary 'vertical)))
9513         (let ((selected (selected-window)))
9514           (select-window (get-buffer-window (current-buffer) t))
9515           (gnus-summary-position-point)
9516           (gnus-horizontal-recenter)
9517           (select-window selected))))))
9518
9519 (defun gnus-horizontal-recenter ()
9520   "Recenter the current buffer horizontally."
9521   (if (< (current-column) (/ (window-width) 2))
9522       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9523     (let* ((orig (point))
9524            (end (window-end (get-buffer-window (current-buffer) t)))
9525            (max 0))
9526       ;; Find the longest line currently displayed in the window.
9527       (goto-char (window-start))
9528       (while (and (not (eobp)) 
9529                   (< (point) end))
9530         (end-of-line)
9531         (setq max (max max (current-column)))
9532         (forward-line 1))
9533       (goto-char orig)
9534       ;; Scroll horizontally to center (sort of) the point.
9535       (if (> max (window-width))
9536           (set-window-hscroll 
9537            (get-buffer-window (current-buffer) t)
9538            (min (- (current-column) (/ (window-width) 3))
9539                 (+ 2 (- max (window-width)))))
9540         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9541       max)))
9542
9543 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9544 (defun gnus-short-group-name (group &optional levels)
9545   "Collapse GROUP name LEVELS."
9546   (let* ((name "") 
9547          (foreign "")
9548          (depth 0) 
9549          (skip 1)
9550          (levels (or levels
9551                      (progn
9552                        (while (string-match "\\." group skip)
9553                          (setq skip (match-end 0)
9554                                depth (+ depth 1)))
9555                        depth))))
9556     (if (string-match ":" group)
9557         (setq foreign (substring group 0 (match-end 0))
9558               group (substring group (match-end 0))))
9559     (while group
9560       (if (and (string-match "\\." group)
9561                (> levels (- gnus-group-uncollapsed-levels 1)))
9562           (setq name (concat name (substring group 0 1))
9563                 group (substring group (match-end 0))
9564                 levels (- levels 1)
9565                 name (concat name "."))
9566         (setq name (concat foreign name group)
9567               group nil)))
9568     name))
9569
9570 (defun gnus-summary-jump-to-group (newsgroup)
9571   "Move point to NEWSGROUP in group mode buffer."
9572   ;; Keep update point of group mode buffer if visible.
9573   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9574       (save-window-excursion
9575         ;; Take care of tree window mode.
9576         (if (get-buffer-window gnus-group-buffer)
9577             (pop-to-buffer gnus-group-buffer))
9578         (gnus-group-jump-to-group newsgroup))
9579     (save-excursion
9580       ;; Take care of tree window mode.
9581       (if (get-buffer-window gnus-group-buffer)
9582           (pop-to-buffer gnus-group-buffer)
9583         (set-buffer gnus-group-buffer))
9584       (gnus-group-jump-to-group newsgroup))))
9585
9586 ;; This function returns a list of article numbers based on the
9587 ;; difference between the ranges of read articles in this group and
9588 ;; the range of active articles.
9589 (defun gnus-list-of-unread-articles (group)
9590   (let* ((read (gnus-info-read (gnus-get-info group)))
9591          (active (gnus-active group))
9592          (last (cdr active))
9593          first nlast unread)
9594     ;; If none are read, then all are unread.
9595     (if (not read)
9596         (setq first (car active))
9597       ;; If the range of read articles is a single range, then the
9598       ;; first unread article is the article after the last read
9599       ;; article.  Sounds logical, doesn't it?
9600       (if (not (listp (cdr read)))
9601           (setq first (1+ (cdr read)))
9602         ;; `read' is a list of ranges.
9603         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9604                                 (caar read))) 1)
9605             (setq first 1))
9606         (while read
9607           (if first
9608               (while (< first nlast)
9609                 (setq unread (cons first unread))
9610                 (setq first (1+ first))))
9611           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9612           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9613           (setq read (cdr read)))))
9614     ;; And add the last unread articles.
9615     (while (<= first last)
9616       (setq unread (cons first unread))
9617       (setq first (1+ first)))
9618     ;; Return the list of unread articles.
9619     (nreverse unread)))
9620
9621 (defun gnus-list-of-read-articles (group)
9622   "Return a list of unread, unticked and non-dormant articles."
9623   (let* ((info (gnus-get-info group))
9624          (marked (gnus-info-marks info))
9625          (active (gnus-active group)))
9626     (and info active
9627          (gnus-set-difference
9628           (gnus-sorted-complement
9629            (gnus-uncompress-range active)
9630            (gnus-list-of-unread-articles group))
9631           (append
9632            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9633            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9634
9635 ;; Various summary commands
9636
9637 (defun gnus-summary-universal-argument (arg)
9638   "Perform any operation on all articles that are process/prefixed."
9639   (interactive "P")
9640   (gnus-set-global-variables)
9641   (let ((articles (gnus-summary-work-articles arg))
9642         func article)
9643     (if (eq
9644          (setq
9645           func
9646           (key-binding
9647            (read-key-sequence
9648             (substitute-command-keys
9649              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9650              ))))
9651          'undefined)
9652         (gnus-error 1 "Undefined key")
9653       (save-excursion
9654         (while articles
9655           (gnus-summary-goto-subject (setq article (pop articles)))
9656           (command-execute func)
9657           (gnus-summary-remove-process-mark article)))))
9658   (gnus-summary-position-point))
9659
9660 (defun gnus-summary-toggle-truncation (&optional arg)
9661   "Toggle truncation of summary lines.
9662 With arg, turn line truncation on iff arg is positive."
9663   (interactive "P")
9664   (setq truncate-lines
9665         (if (null arg) (not truncate-lines)
9666           (> (prefix-numeric-value arg) 0)))
9667   (redraw-display))
9668
9669 (defun gnus-summary-reselect-current-group (&optional all rescan)
9670   "Exit and then reselect the current newsgroup.
9671 The prefix argument ALL means to select all articles."
9672   (interactive "P")
9673   (gnus-set-global-variables)
9674   (let ((current-subject (gnus-summary-article-number))
9675         (group gnus-newsgroup-name))
9676     (setq gnus-newsgroup-begin nil)
9677     (gnus-summary-exit)
9678     ;; We have to adjust the point of group mode buffer because the
9679     ;; current point was moved to the next unread newsgroup by
9680     ;; exiting.
9681     (gnus-summary-jump-to-group group)
9682     (when rescan
9683       (save-excursion
9684         (gnus-group-get-new-news-this-group 1)))
9685     (gnus-group-read-group all t)
9686     (gnus-summary-goto-subject current-subject)))
9687
9688 (defun gnus-summary-rescan-group (&optional all)
9689   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9690   (interactive "P")
9691   (gnus-summary-reselect-current-group all t))
9692
9693 (defun gnus-summary-update-info ()
9694   (let* ((group gnus-newsgroup-name))
9695     (when gnus-newsgroup-kill-headers
9696       (setq gnus-newsgroup-killed
9697             (gnus-compress-sequence
9698              (nconc
9699               (gnus-set-sorted-intersection
9700                (gnus-uncompress-range gnus-newsgroup-killed)
9701                (setq gnus-newsgroup-unselected
9702                      (sort gnus-newsgroup-unselected '<)))
9703               (setq gnus-newsgroup-unreads
9704                     (sort gnus-newsgroup-unreads '<))) t)))
9705     (unless (listp (cdr gnus-newsgroup-killed))
9706       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9707     (let ((headers gnus-newsgroup-headers))
9708       (run-hooks 'gnus-exit-group-hook)
9709       (unless gnus-save-score
9710         (setq gnus-newsgroup-scored nil))
9711       ;; Set the new ranges of read articles.
9712       (gnus-update-read-articles
9713        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9714       ;; Set the current article marks.
9715       (gnus-update-marks)
9716       ;; Do the cross-ref thing.
9717       (when gnus-use-cross-reference
9718         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9719       ;; Do adaptive scoring, and possibly save score files.
9720       (when gnus-newsgroup-adaptive
9721         (gnus-score-adaptive))
9722       (when gnus-use-scoring
9723         (gnus-score-save))
9724       ;; Do not switch windows but change the buffer to work.
9725       (set-buffer gnus-group-buffer)
9726       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9727           (gnus-group-update-group group)))))
9728
9729 (defun gnus-summary-exit (&optional temporary)
9730   "Exit reading current newsgroup, and then return to group selection mode.
9731 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9732   (interactive)
9733   (gnus-set-global-variables)
9734   (gnus-kill-save-kill-buffer)
9735   (let* ((group gnus-newsgroup-name)
9736          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9737          (mode major-mode)
9738          (buf (current-buffer)))
9739     (run-hooks 'gnus-summary-prepare-exit-hook)
9740     ;; If we have several article buffers, we kill them at exit.
9741     (unless gnus-single-article-buffer
9742       (gnus-kill-buffer gnus-article-buffer)
9743       (gnus-kill-buffer gnus-original-article-buffer)
9744       (setq gnus-article-current nil))
9745     (when gnus-use-cache
9746       (gnus-cache-possibly-remove-articles)
9747       (gnus-cache-save-buffers))
9748     (when gnus-use-trees
9749       (gnus-tree-close group))
9750     ;; Make all changes in this group permanent.
9751     (unless quit-config
9752       (gnus-summary-update-info))
9753     (gnus-close-group group)
9754     ;; Make sure where I was, and go to next newsgroup.
9755     (set-buffer gnus-group-buffer)
9756     (unless quit-config
9757       (gnus-group-jump-to-group group)
9758       (gnus-group-next-unread-group 1))
9759     (run-hooks 'gnus-summary-exit-hook)
9760     (unless gnus-single-article-buffer
9761       (setq gnus-article-current nil))
9762     (if temporary
9763         nil                             ;Nothing to do.
9764       ;; If we have several article buffers, we kill them at exit.
9765       (unless gnus-single-article-buffer
9766         (gnus-kill-buffer gnus-article-buffer)
9767         (gnus-kill-buffer gnus-original-article-buffer)
9768         (setq gnus-article-current nil))
9769       (set-buffer buf)
9770       (if (not gnus-kill-summary-on-exit)
9771           (gnus-deaden-summary)
9772         ;; We set all buffer-local variables to nil.  It is unclear why
9773         ;; this is needed, but if we don't, buffer-local variables are
9774         ;; not garbage-collected, it seems.  This would the lead to en
9775         ;; ever-growing Emacs.
9776         (gnus-summary-clear-local-variables)
9777         (when (get-buffer gnus-article-buffer)
9778           (bury-buffer gnus-article-buffer))
9779         ;; We clear the global counterparts of the buffer-local
9780         ;; variables as well, just to be on the safe side.
9781         (gnus-configure-windows 'group 'force)
9782         (gnus-summary-clear-local-variables)
9783         ;; Return to group mode buffer.
9784         (if (eq mode 'gnus-summary-mode)
9785             (gnus-kill-buffer buf)))
9786       (setq gnus-current-select-method gnus-select-method)
9787       (pop-to-buffer gnus-group-buffer)
9788       ;; Clear the current group name.
9789       (if (not quit-config)
9790           (progn
9791             (gnus-group-jump-to-group group)
9792             (gnus-group-next-unread-group 1)
9793             (gnus-configure-windows 'group 'force))
9794         (if (not (buffer-name (car quit-config)))
9795             (gnus-configure-windows 'group 'force)
9796           (set-buffer (car quit-config))
9797           (and (eq major-mode 'gnus-summary-mode)
9798                (gnus-set-global-variables))
9799           (gnus-configure-windows (cdr quit-config))))
9800       (unless quit-config
9801         (setq gnus-newsgroup-name nil)))))
9802
9803 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9804 (defun gnus-summary-exit-no-update (&optional no-questions)
9805   "Quit reading current newsgroup without updating read article info."
9806   (interactive)
9807   (gnus-set-global-variables)
9808   (let* ((group gnus-newsgroup-name)
9809          (quit-config (gnus-group-quit-config group)))
9810     (when (or no-questions
9811               gnus-expert-user
9812               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9813       ;; If we have several article buffers, we kill them at exit.
9814       (unless gnus-single-article-buffer
9815         (gnus-kill-buffer gnus-article-buffer)
9816         (gnus-kill-buffer gnus-original-article-buffer)
9817         (setq gnus-article-current nil))
9818       (if (not gnus-kill-summary-on-exit)
9819           (gnus-deaden-summary)
9820         (gnus-close-group group)
9821         (gnus-summary-clear-local-variables)
9822         (set-buffer gnus-group-buffer)
9823         (gnus-summary-clear-local-variables)
9824         (when (get-buffer gnus-summary-buffer)
9825           (kill-buffer gnus-summary-buffer)))
9826       (unless gnus-single-article-buffer
9827         (setq gnus-article-current nil))
9828       (when gnus-use-trees
9829         (gnus-tree-close group))
9830       (when (get-buffer gnus-article-buffer)
9831         (bury-buffer gnus-article-buffer))
9832       ;; Return to the group buffer.
9833       (gnus-configure-windows 'group 'force)
9834       ;; Clear the current group name.
9835       (setq gnus-newsgroup-name nil)
9836       (when (equal (gnus-group-group-name) group)
9837         (gnus-group-next-unread-group 1))
9838       (when quit-config
9839         (if (not (buffer-name (car quit-config)))
9840             (gnus-configure-windows 'group 'force)
9841           (set-buffer (car quit-config))
9842           (when (eq major-mode 'gnus-summary-mode)
9843             (gnus-set-global-variables))
9844           (gnus-configure-windows (cdr quit-config)))))))
9845
9846 ;;; Dead summaries.
9847
9848 (defvar gnus-dead-summary-mode-map nil)
9849
9850 (if gnus-dead-summary-mode-map
9851     nil
9852   (setq gnus-dead-summary-mode-map (make-keymap))
9853   (suppress-keymap gnus-dead-summary-mode-map)
9854   (substitute-key-definition
9855    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9856   (let ((keys '("\C-d" "\r" "\177")))
9857     (while keys
9858       (define-key gnus-dead-summary-mode-map
9859         (pop keys) 'gnus-summary-wake-up-the-dead))))
9860
9861 (defvar gnus-dead-summary-mode nil
9862   "Minor mode for Gnus summary buffers.")
9863
9864 (defun gnus-dead-summary-mode (&optional arg)
9865   "Minor mode for Gnus summary buffers."
9866   (interactive "P")
9867   (when (eq major-mode 'gnus-summary-mode)
9868     (make-local-variable 'gnus-dead-summary-mode)
9869     (setq gnus-dead-summary-mode
9870           (if (null arg) (not gnus-dead-summary-mode)
9871             (> (prefix-numeric-value arg) 0)))
9872     (when gnus-dead-summary-mode
9873       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9874         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9875       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9876         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9877               minor-mode-map-alist)))))
9878
9879 (defun gnus-deaden-summary ()
9880   "Make the current summary buffer into a dead summary buffer."
9881   ;; Kill any previous dead summary buffer.
9882   (when (and gnus-dead-summary
9883              (buffer-name gnus-dead-summary))
9884     (save-excursion
9885       (set-buffer gnus-dead-summary)
9886       (when gnus-dead-summary-mode
9887         (kill-buffer (current-buffer)))))
9888   ;; Make this the current dead summary.
9889   (setq gnus-dead-summary (current-buffer))
9890   (gnus-dead-summary-mode 1)
9891   (let ((name (buffer-name)))
9892     (when (string-match "Summary" name)
9893       (rename-buffer
9894        (concat (substring name 0 (match-beginning 0)) "Dead "
9895                (substring name (match-beginning 0))) t))))
9896
9897 (defun gnus-kill-or-deaden-summary (buffer)
9898   "Kill or deaden the summary BUFFER."
9899   (cond (gnus-kill-summary-on-exit
9900          (when (and gnus-use-trees
9901                     (and (get-buffer buffer)
9902                          (buffer-name (get-buffer buffer))))
9903            (save-excursion
9904              (set-buffer (get-buffer buffer))
9905              (gnus-tree-close gnus-newsgroup-name)))
9906          (gnus-kill-buffer buffer))
9907         ((and (get-buffer buffer)
9908               (buffer-name (get-buffer buffer)))
9909          (save-excursion
9910            (set-buffer buffer)
9911            (gnus-deaden-summary)))))
9912
9913 (defun gnus-summary-wake-up-the-dead (&rest args)
9914   "Wake up the dead summary buffer."
9915   (interactive)
9916   (gnus-dead-summary-mode -1)
9917   (let ((name (buffer-name)))
9918     (when (string-match "Dead " name)
9919       (rename-buffer
9920        (concat (substring name 0 (match-beginning 0))
9921                (substring name (match-end 0))) t)))
9922   (gnus-message 3 "This dead summary is now alive again"))
9923
9924 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9925 (defun gnus-summary-fetch-faq (&optional faq-dir)
9926   "Fetch the FAQ for the current group.
9927 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9928 in."
9929   (interactive
9930    (list
9931     (if current-prefix-arg
9932         (completing-read
9933          "Faq dir: " (and (listp gnus-group-faq-directory)
9934                           gnus-group-faq-directory)))))
9935   (let (gnus-faq-buffer)
9936     (and (setq gnus-faq-buffer
9937                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9938          (gnus-configure-windows 'summary-faq))))
9939
9940 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9941 (defun gnus-summary-describe-group (&optional force)
9942   "Describe the current newsgroup."
9943   (interactive "P")
9944   (gnus-group-describe-group force gnus-newsgroup-name))
9945
9946 (defun gnus-summary-describe-briefly ()
9947   "Describe summary mode commands briefly."
9948   (interactive)
9949   (gnus-message 6
9950                 (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")))
9951
9952 ;; Walking around group mode buffer from summary mode.
9953
9954 (defun gnus-summary-next-group (&optional no-article target-group backward)
9955   "Exit current newsgroup and then select next unread newsgroup.
9956 If prefix argument NO-ARTICLE is non-nil, no article is selected
9957 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9958 previous group instead."
9959   (interactive "P")
9960   (gnus-set-global-variables)
9961   (let ((current-group gnus-newsgroup-name)
9962         (current-buffer (current-buffer))
9963         entered)
9964     ;; First we semi-exit this group to update Xrefs and all variables.
9965     ;; We can't do a real exit, because the window conf must remain
9966     ;; the same in case the user is prompted for info, and we don't
9967     ;; want the window conf to change before that...
9968     (gnus-summary-exit t)
9969     (while (not entered)
9970       ;; Then we find what group we are supposed to enter.
9971       (set-buffer gnus-group-buffer)
9972       (gnus-group-jump-to-group current-group)
9973       (setq target-group
9974             (or target-group
9975                 (if (eq gnus-keep-same-level 'best)
9976                     (gnus-summary-best-group gnus-newsgroup-name)
9977                   (gnus-summary-search-group backward gnus-keep-same-level))))
9978       (if (not target-group)
9979           ;; There are no further groups, so we return to the group
9980           ;; buffer.
9981           (progn
9982             (gnus-message 5 "Returning to the group buffer")
9983             (setq entered t)
9984             (set-buffer current-buffer)
9985             (gnus-summary-exit))
9986         ;; We try to enter the target group.
9987         (gnus-group-jump-to-group target-group)
9988         (let ((unreads (gnus-group-group-unread)))
9989           (if (and (or (eq t unreads)
9990                        (and unreads (not (zerop unreads))))
9991                    (gnus-summary-read-group
9992                     target-group nil no-article current-buffer))
9993               (setq entered t)
9994             (setq current-group target-group
9995                   target-group nil)))))))
9996
9997 (defun gnus-summary-prev-group (&optional no-article)
9998   "Exit current newsgroup and then select previous unread newsgroup.
9999 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10000   (interactive "P")
10001   (gnus-summary-next-group no-article nil t))
10002
10003 ;; Walking around summary lines.
10004
10005 (defun gnus-summary-first-subject (&optional unread)
10006   "Go to the first unread subject.
10007 If UNREAD is non-nil, go to the first unread article.
10008 Returns the article selected or nil if there are no unread articles."
10009   (interactive "P")
10010   (prog1
10011       (cond
10012        ;; Empty summary.
10013        ((null gnus-newsgroup-data)
10014         (gnus-message 3 "No articles in the group")
10015         nil)
10016        ;; Pick the first article.
10017        ((not unread)
10018         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10019         (gnus-data-number (car gnus-newsgroup-data)))
10020        ;; No unread articles.
10021        ((null gnus-newsgroup-unreads)
10022         (gnus-message 3 "No more unread articles")
10023         nil)
10024        ;; Find the first unread article.
10025        (t
10026         (let ((data gnus-newsgroup-data))
10027           (while (and data
10028                       (not (gnus-data-unread-p (car data))))
10029             (setq data (cdr data)))
10030           (if data
10031               (progn
10032                 (goto-char (gnus-data-pos (car data)))
10033                 (gnus-data-number (car data)))))))
10034     (gnus-summary-position-point)))
10035
10036 (defun gnus-summary-next-subject (n &optional unread dont-display)
10037   "Go to next N'th summary line.
10038 If N is negative, go to the previous N'th subject line.
10039 If UNREAD is non-nil, only unread articles are selected.
10040 The difference between N and the actual number of steps taken is
10041 returned."
10042   (interactive "p")
10043   (let ((backward (< n 0))
10044         (n (abs n)))
10045     (while (and (> n 0)
10046                 (if backward
10047                     (gnus-summary-find-prev unread)
10048                   (gnus-summary-find-next unread)))
10049       (setq n (1- n)))
10050     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10051                                (if unread " unread" "")))
10052     (unless dont-display
10053       (gnus-summary-recenter)
10054       (gnus-summary-position-point))
10055     n))
10056
10057 (defun gnus-summary-next-unread-subject (n)
10058   "Go to next N'th unread summary line."
10059   (interactive "p")
10060   (gnus-summary-next-subject n t))
10061
10062 (defun gnus-summary-prev-subject (n &optional unread)
10063   "Go to previous N'th summary line.
10064 If optional argument UNREAD is non-nil, only unread article is selected."
10065   (interactive "p")
10066   (gnus-summary-next-subject (- n) unread))
10067
10068 (defun gnus-summary-prev-unread-subject (n)
10069   "Go to previous N'th unread summary line."
10070   (interactive "p")
10071   (gnus-summary-next-subject (- n) t))
10072
10073 (defun gnus-summary-goto-subject (article &optional force silent)
10074   "Go the subject line of ARTICLE.
10075 If FORCE, also allow jumping to articles not currently shown."
10076   (let ((b (point))
10077         (data (gnus-data-find article)))
10078     ;; We read in the article if we have to.
10079     (and (not data)
10080          force
10081          (gnus-summary-insert-subject article)
10082          (setq data (gnus-data-find article)))
10083     (goto-char b)
10084     (if (not data)
10085         (progn
10086           (unless silent
10087             (gnus-message 3 "Can't find article %d" article))
10088           nil)
10089       (goto-char (gnus-data-pos data))
10090       article)))
10091
10092 ;; Walking around summary lines with displaying articles.
10093
10094 (defun gnus-summary-expand-window (&optional arg)
10095   "Make the summary buffer take up the entire Emacs frame.
10096 Given a prefix, will force an `article' buffer configuration."
10097   (interactive "P")
10098   (gnus-set-global-variables)
10099   (if arg
10100       (gnus-configure-windows 'article 'force)
10101     (gnus-configure-windows 'summary 'force)))
10102
10103 (defun gnus-summary-display-article (article &optional all-header)
10104   "Display ARTICLE in article buffer."
10105   (gnus-set-global-variables)
10106   (if (null article)
10107       nil
10108     (prog1
10109         (if gnus-summary-display-article-function
10110             (funcall gnus-summary-display-article-function article all-header)
10111           (gnus-article-prepare article all-header))
10112       (run-hooks 'gnus-select-article-hook)
10113       (gnus-summary-recenter)
10114       (gnus-summary-goto-subject article)
10115       (when gnus-use-trees
10116         (gnus-possibly-generate-tree article)
10117         (gnus-highlight-selected-tree article))
10118       ;; Successfully display article.
10119       (gnus-article-set-window-start
10120        (cdr (assq article gnus-newsgroup-bookmarks)))
10121       t)))
10122
10123 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10124   "Select the current article.
10125 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10126 non-nil, the article will be re-fetched even if it already present in
10127 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10128 be displayed."
10129   (let ((article (or article (gnus-summary-article-number)))
10130         (all-headers (not (not all-headers))) ;Must be T or NIL.
10131         gnus-summary-display-article-function
10132         did)
10133     (and (not pseudo)
10134          (gnus-summary-article-pseudo-p article)
10135          (error "This is a pseudo-article."))
10136     (prog1
10137         (save-excursion
10138           (set-buffer gnus-summary-buffer)
10139           (if (or (and gnus-single-article-buffer
10140                        (or (null gnus-current-article)
10141                            (null gnus-article-current)
10142                            (null (get-buffer gnus-article-buffer))
10143                            (not (eq article (cdr gnus-article-current)))
10144                            (not (equal (car gnus-article-current)
10145                                        gnus-newsgroup-name))))
10146                   (and (not gnus-single-article-buffer)
10147                        (or (null gnus-current-article)
10148                            (not (eq gnus-current-article article))))
10149                   force)
10150               ;; The requested article is different from the current article.
10151               (prog1
10152                   (gnus-summary-display-article article all-headers)
10153                 (setq did article))
10154             (if (or all-headers gnus-show-all-headers)
10155                 (gnus-article-show-all-headers))
10156             'old))
10157       (if did
10158           (gnus-article-set-window-start
10159            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10160
10161 (defun gnus-summary-set-current-mark (&optional current-mark)
10162   "Obsolete function."
10163   nil)
10164
10165 (defun gnus-summary-next-article (&optional unread subject backward push)
10166   "Select the next article.
10167 If UNREAD, only unread articles are selected.
10168 If SUBJECT, only articles with SUBJECT are selected.
10169 If BACKWARD, the previous article is selected instead of the next."
10170   (interactive "P")
10171   (gnus-set-global-variables)
10172   (cond
10173    ;; Is there such an article?
10174    ((and (gnus-summary-search-forward unread subject backward)
10175          (or (gnus-summary-display-article (gnus-summary-article-number))
10176              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10177     (gnus-summary-position-point))
10178    ;; If not, we try the first unread, if that is wanted.
10179    ((and subject
10180          gnus-auto-select-same
10181          (or (gnus-summary-first-unread-article)
10182              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10183     (gnus-summary-position-point)
10184     (gnus-message 6 "Wrapped"))
10185    ;; Try to get next/previous article not displayed in this group.
10186    ((and gnus-auto-extend-newsgroup
10187          (not unread) (not subject))
10188     (gnus-summary-goto-article
10189      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10190      nil t))
10191    ;; Go to next/previous group.
10192    (t
10193     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10194         (gnus-summary-jump-to-group gnus-newsgroup-name))
10195     (let ((cmd last-command-char)
10196           (group
10197            (if (eq gnus-keep-same-level 'best)
10198                (gnus-summary-best-group gnus-newsgroup-name)
10199              (gnus-summary-search-group backward gnus-keep-same-level))))
10200       ;; For some reason, the group window gets selected.  We change
10201       ;; it back.
10202       (select-window (get-buffer-window (current-buffer)))
10203       ;; Select next unread newsgroup automagically.
10204       (cond
10205        ((not gnus-auto-select-next)
10206         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10207        ((or (eq gnus-auto-select-next 'quietly)
10208             (and (eq gnus-auto-select-next 'slightly-quietly)
10209                  push)
10210             (and (eq gnus-auto-select-next 'almost-quietly)
10211                  (gnus-summary-last-article-p)))
10212         ;; Select quietly.
10213         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10214             (gnus-summary-exit)
10215           (gnus-message 7 "No more%s articles (%s)..."
10216                         (if unread " unread" "")
10217                         (if group (concat "selecting " group)
10218                           "exiting"))
10219           (gnus-summary-next-group nil group backward)))
10220        (t
10221         (gnus-summary-walk-group-buffer
10222          gnus-newsgroup-name cmd unread backward)))))))
10223
10224 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10225   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10226                       (?\C-p (gnus-group-prev-unread-group 1))))
10227         keve key group ended)
10228     (save-excursion
10229       (set-buffer gnus-group-buffer)
10230       (gnus-summary-jump-to-group from-group)
10231       (setq group
10232             (if (eq gnus-keep-same-level 'best)
10233                 (gnus-summary-best-group gnus-newsgroup-name)
10234               (gnus-summary-search-group backward gnus-keep-same-level))))
10235     (while (not ended)
10236       (gnus-message
10237        5 "No more%s articles%s" (if unread " unread" "")
10238        (if (and group
10239                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10240            (format " (Type %s for %s [%s])"
10241                    (single-key-description cmd) group
10242                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10243          (format " (Type %s to exit %s)"
10244                  (single-key-description cmd)
10245                  gnus-newsgroup-name)))
10246       ;; Confirm auto selection.
10247       (setq key (car (setq keve (gnus-read-event-char))))
10248       (setq ended t)
10249       (cond
10250        ((assq key keystrokes)
10251         (let ((obuf (current-buffer)))
10252           (switch-to-buffer gnus-group-buffer)
10253           (and group
10254                (gnus-group-jump-to-group group))
10255           (eval (cadr (assq key keystrokes)))
10256           (setq group (gnus-group-group-name))
10257           (switch-to-buffer obuf))
10258         (setq ended nil))
10259        ((equal key cmd)
10260         (if (or (not group)
10261                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10262             (gnus-summary-exit)
10263           (gnus-summary-next-group nil group backward)))
10264        (t
10265         (push (cdr keve) unread-command-events))))))
10266
10267 (defun gnus-read-event-char ()
10268   "Get the next event."
10269   (let ((event (read-event)))
10270     (cons (and (numberp event) event) event)))
10271
10272 (defun gnus-summary-next-unread-article ()
10273   "Select unread article after current one."
10274   (interactive)
10275   (gnus-summary-next-article t (and gnus-auto-select-same
10276                                     (gnus-summary-article-subject))))
10277
10278 (defun gnus-summary-prev-article (&optional unread subject)
10279   "Select the article after the current one.
10280 If UNREAD is non-nil, only unread articles are selected."
10281   (interactive "P")
10282   (gnus-summary-next-article unread subject t))
10283
10284 (defun gnus-summary-prev-unread-article ()
10285   "Select unred article before current one."
10286   (interactive)
10287   (gnus-summary-prev-article t (and gnus-auto-select-same
10288                                     (gnus-summary-article-subject))))
10289
10290 (defun gnus-summary-next-page (&optional lines circular)
10291   "Show next page of the selected article.
10292 If at the end of the current article, select the next article.
10293 LINES says how many lines should be scrolled up.
10294
10295 If CIRCULAR is non-nil, go to the start of the article instead of
10296 selecting the next article when reaching the end of the current
10297 article."
10298   (interactive "P")
10299   (setq gnus-summary-buffer (current-buffer))
10300   (gnus-set-global-variables)
10301   (let ((article (gnus-summary-article-number))
10302         (endp nil))
10303     (gnus-configure-windows 'article)
10304     (if (or (null gnus-current-article)
10305             (null gnus-article-current)
10306             (/= article (cdr gnus-article-current))
10307             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10308         ;; Selected subject is different from current article's.
10309         (gnus-summary-display-article article)
10310       (gnus-eval-in-buffer-window
10311        gnus-article-buffer
10312        (setq endp (gnus-article-next-page lines)))
10313       (if endp
10314           (cond (circular
10315                  (gnus-summary-beginning-of-article))
10316                 (lines
10317                  (gnus-message 3 "End of message"))
10318                 ((null lines)
10319                  (if (and (eq gnus-summary-goto-unread 'never)
10320                           (not (gnus-summary-last-article-p article)))
10321                      (gnus-summary-next-article)
10322                    (gnus-summary-next-unread-article))))))
10323     (gnus-summary-recenter)
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-prev-page (&optional lines)
10327   "Show previous page of selected article.
10328 Argument LINES specifies lines to be scrolled down."
10329   (interactive "P")
10330   (gnus-set-global-variables)
10331   (let ((article (gnus-summary-article-number)))
10332     (gnus-configure-windows 'article)
10333     (if (or (null gnus-current-article)
10334             (null gnus-article-current)
10335             (/= article (cdr gnus-article-current))
10336             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10337         ;; Selected subject is different from current article's.
10338         (gnus-summary-display-article article)
10339       (gnus-summary-recenter)
10340       (gnus-eval-in-buffer-window gnus-article-buffer
10341                                   (gnus-article-prev-page lines))))
10342   (gnus-summary-position-point))
10343
10344 (defun gnus-summary-scroll-up (lines)
10345   "Scroll up (or down) one line current article.
10346 Argument LINES specifies lines to be scrolled up (or down if negative)."
10347   (interactive "p")
10348   (gnus-set-global-variables)
10349   (gnus-configure-windows 'article)
10350   (gnus-summary-show-thread)
10351   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10352     (gnus-eval-in-buffer-window
10353      gnus-article-buffer
10354      (cond ((> lines 0)
10355             (if (gnus-article-next-page lines)
10356                 (gnus-message 3 "End of message")))
10357            ((< lines 0)
10358             (gnus-article-prev-page (- lines))))))
10359   (gnus-summary-recenter)
10360   (gnus-summary-position-point))
10361
10362 (defun gnus-summary-next-same-subject ()
10363   "Select next article which has the same subject as current one."
10364   (interactive)
10365   (gnus-set-global-variables)
10366   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10367
10368 (defun gnus-summary-prev-same-subject ()
10369   "Select previous article which has the same subject as current one."
10370   (interactive)
10371   (gnus-set-global-variables)
10372   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10373
10374 (defun gnus-summary-next-unread-same-subject ()
10375   "Select next unread article which has the same subject as current one."
10376   (interactive)
10377   (gnus-set-global-variables)
10378   (gnus-summary-next-article t (gnus-summary-article-subject)))
10379
10380 (defun gnus-summary-prev-unread-same-subject ()
10381   "Select previous unread article which has the same subject as current one."
10382   (interactive)
10383   (gnus-set-global-variables)
10384   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10385
10386 (defun gnus-summary-first-unread-article ()
10387   "Select the first unread article.
10388 Return nil if there are no unread articles."
10389   (interactive)
10390   (gnus-set-global-variables)
10391   (prog1
10392       (if (gnus-summary-first-subject t)
10393           (progn
10394             (gnus-summary-show-thread)
10395             (gnus-summary-first-subject t)
10396             (gnus-summary-display-article (gnus-summary-article-number))))
10397     (gnus-summary-position-point)))
10398
10399 (defun gnus-summary-best-unread-article ()
10400   "Select the unread article with the highest score."
10401   (interactive)
10402   (gnus-set-global-variables)
10403   (let ((best -1000000)
10404         (data gnus-newsgroup-data)
10405         article score)
10406     (while data
10407       (and (gnus-data-unread-p (car data))
10408            (> (setq score
10409                     (gnus-summary-article-score (gnus-data-number (car data))))
10410               best)
10411            (setq best score
10412                  article (gnus-data-number (car data))))
10413       (setq data (cdr data)))
10414     (prog1
10415         (if article
10416             (gnus-summary-goto-article article)
10417           (error "No unread articles"))
10418       (gnus-summary-position-point))))
10419
10420 (defun gnus-summary-last-subject ()
10421   "Go to the last displayed subject line in the group."
10422   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10423     (when article
10424       (gnus-summary-goto-subject article))))
10425
10426 (defun gnus-summary-goto-article (article &optional all-headers force)
10427   "Fetch ARTICLE and display it if it exists.
10428 If ALL-HEADERS is non-nil, no header lines are hidden."
10429   (interactive
10430    (list
10431     (string-to-int
10432      (completing-read
10433       "Article number: "
10434       (mapcar (lambda (number) (list (int-to-string number)))
10435               gnus-newsgroup-limit)))
10436     current-prefix-arg
10437     t))
10438   (prog1
10439       (if (gnus-summary-goto-subject article force)
10440           (gnus-summary-display-article article all-headers)
10441         (gnus-message 4 "Couldn't go to article %s" article) nil)
10442     (gnus-summary-position-point)))
10443
10444 (defun gnus-summary-goto-last-article ()
10445   "Go to the previously read article."
10446   (interactive)
10447   (prog1
10448       (and gnus-last-article
10449            (gnus-summary-goto-article gnus-last-article))
10450     (gnus-summary-position-point)))
10451
10452 (defun gnus-summary-pop-article (number)
10453   "Pop one article off the history and go to the previous.
10454 NUMBER articles will be popped off."
10455   (interactive "p")
10456   (let (to)
10457     (setq gnus-newsgroup-history
10458           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10459     (if to
10460         (gnus-summary-goto-article (car to))
10461       (error "Article history empty")))
10462   (gnus-summary-position-point))
10463
10464 ;; Summary commands and functions for limiting the summary buffer.
10465
10466 (defun gnus-summary-limit-to-articles (n)
10467   "Limit the summary buffer to the next N articles.
10468 If not given a prefix, use the process marked articles instead."
10469   (interactive "P")
10470   (gnus-set-global-variables)
10471   (prog1
10472       (let ((articles (gnus-summary-work-articles n)))
10473         (setq gnus-newsgroup-processable nil)
10474         (gnus-summary-limit articles))
10475     (gnus-summary-position-point)))
10476
10477 (defun gnus-summary-pop-limit (&optional total)
10478   "Restore the previous limit.
10479 If given a prefix, remove all limits."
10480   (interactive "P")
10481   (gnus-set-global-variables)
10482   (when total 
10483     (setq gnus-newsgroup-limits
10484           (list (mapcar (lambda (h) (mail-header-number h))
10485                         gnus-newsgroup-headers))))
10486   (unless gnus-newsgroup-limits
10487     (error "No limit to pop"))
10488   (prog1
10489       (gnus-summary-limit nil 'pop)
10490     (gnus-summary-position-point)))
10491
10492 (defun gnus-summary-limit-to-subject (subject &optional header)
10493   "Limit the summary buffer to articles that have subjects that match a regexp."
10494   (interactive "sRegexp: ")
10495   (unless header
10496     (setq header "subject"))
10497   (when (not (equal "" subject))
10498     (prog1
10499         (let ((articles (gnus-summary-find-matching
10500                          (or header "subject") subject 'all)))
10501           (or articles (error "Found no matches for \"%s\"" subject))
10502           (gnus-summary-limit articles))
10503       (gnus-summary-position-point))))
10504
10505 (defun gnus-summary-limit-to-author (from)
10506   "Limit the summary buffer to articles that have authors that match a regexp."
10507   (interactive "sRegexp: ")
10508   (gnus-summary-limit-to-subject from "from"))
10509
10510 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10511 (make-obsolete
10512  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10513
10514 (defun gnus-summary-limit-to-unread (&optional all)
10515   "Limit the summary buffer to articles that are not marked as read.
10516 If ALL is non-nil, limit strictly to unread articles."
10517   (interactive "P")
10518   (if all
10519       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10520     (gnus-summary-limit-to-marks
10521      ;; Concat all the marks that say that an article is read and have
10522      ;; those removed.
10523      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10524            gnus-killed-mark gnus-kill-file-mark
10525            gnus-low-score-mark gnus-expirable-mark
10526            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10527      'reverse)))
10528
10529 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10530 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10531
10532 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10533   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10534 If REVERSE, limit the summary buffer to articles that are not marked
10535 with MARKS.  MARKS can either be a string of marks or a list of marks.
10536 Returns how many articles were removed."
10537   (interactive "sMarks: ")
10538   (gnus-set-global-variables)
10539   (prog1
10540       (let ((data gnus-newsgroup-data)
10541             (marks (if (listp marks) marks
10542                      (append marks nil))) ; Transform to list.
10543             articles)
10544         (while data
10545           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10546                  (memq (gnus-data-mark (car data)) marks))
10547                (setq articles (cons (gnus-data-number (car data)) articles)))
10548           (setq data (cdr data)))
10549         (gnus-summary-limit articles))
10550     (gnus-summary-position-point)))
10551
10552 (defun gnus-summary-limit-to-score (&optional score)
10553   "Limit to articles with score at or above SCORE."
10554   (interactive "P")
10555   (gnus-set-global-variables)
10556   (setq score (if score
10557                   (prefix-numeric-value score)
10558                 (or gnus-summary-default-score 0)))
10559   (let ((data gnus-newsgroup-data)
10560         articles)
10561     (while data
10562       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10563                 score)
10564         (push (gnus-data-number (car data)) articles))
10565       (setq data (cdr data)))
10566     (prog1
10567         (gnus-summary-limit articles)
10568       (gnus-summary-position-point))))
10569
10570 (defun gnus-summary-limit-include-dormant ()
10571   "Display all the hidden articles that are marked as dormant."
10572   (interactive)
10573   (gnus-set-global-variables)
10574   (or gnus-newsgroup-dormant
10575       (error "There are no dormant articles in this group"))
10576   (prog1
10577       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10578     (gnus-summary-position-point)))
10579
10580 (defun gnus-summary-limit-exclude-dormant ()
10581   "Hide all dormant articles."
10582   (interactive)
10583   (gnus-set-global-variables)
10584   (prog1
10585       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10586     (gnus-summary-position-point)))
10587
10588 (defun gnus-summary-limit-exclude-childless-dormant ()
10589   "Hide all dormant articles that have no children."
10590   (interactive)
10591   (gnus-set-global-variables)
10592   (let ((data (gnus-data-list t))
10593         articles d children)
10594     ;; Find all articles that are either not dormant or have
10595     ;; children.
10596     (while (setq d (pop data))
10597       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10598                 (and (setq children 
10599                            (gnus-article-children (gnus-data-number d)))
10600                      (let (found)
10601                        (while children
10602                          (when (memq (car children) articles)
10603                            (setq children nil
10604                                  found t))
10605                          (pop children))
10606                        found)))
10607         (push (gnus-data-number d) articles)))
10608     ;; Do the limiting.
10609     (prog1
10610         (gnus-summary-limit articles)
10611       (gnus-summary-position-point))))
10612
10613 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10614   "Mark all unread excluded articles as read.
10615 If ALL, mark even excluded ticked and dormants as read."
10616   (interactive "P")
10617   (let ((articles (gnus-sorted-complement
10618                    (sort
10619                     (mapcar (lambda (h) (mail-header-number h))
10620                             gnus-newsgroup-headers)
10621                     '<)
10622                    (sort gnus-newsgroup-limit '<)))
10623         article)
10624     (setq gnus-newsgroup-unreads nil)
10625     (if all
10626         (setq gnus-newsgroup-dormant nil
10627               gnus-newsgroup-marked nil
10628               gnus-newsgroup-reads
10629               (nconc
10630                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10631                gnus-newsgroup-reads))
10632       (while (setq article (pop articles))
10633         (unless (or (memq article gnus-newsgroup-dormant)
10634                     (memq article gnus-newsgroup-marked))
10635           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10636
10637 (defun gnus-summary-limit (articles &optional pop)
10638   (if pop
10639       ;; We pop the previous limit off the stack and use that.
10640       (setq articles (car gnus-newsgroup-limits)
10641             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10642     ;; We use the new limit, so we push the old limit on the stack.
10643     (setq gnus-newsgroup-limits
10644           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10645   ;; Set the limit.
10646   (setq gnus-newsgroup-limit articles)
10647   (let ((total (length gnus-newsgroup-data))
10648         (data (gnus-data-find-list (gnus-summary-article-number)))
10649         found)
10650     ;; This will do all the work of generating the new summary buffer
10651     ;; according to the new limit.
10652     (gnus-summary-prepare)
10653     ;; Hide any threads, possibly.
10654     (and gnus-show-threads
10655          gnus-thread-hide-subtree
10656          (gnus-summary-hide-all-threads))
10657     ;; Try to return to the article you were at, or one in the
10658     ;; neighborhood.
10659     (if data
10660         ;; We try to find some article after the current one.
10661         (while data
10662           (and (gnus-summary-goto-subject
10663                 (gnus-data-number (car data)) nil t)
10664                (setq data nil
10665                      found t))
10666           (setq data (cdr data))))
10667     (or found
10668         ;; If there is no data, that means that we were after the last
10669         ;; article.  The same goes when we can't find any articles
10670         ;; after the current one.
10671         (progn
10672           (goto-char (point-max))
10673           (gnus-summary-find-prev)))
10674     ;; We return how many articles were removed from the summary
10675     ;; buffer as a result of the new limit.
10676     (- total (length gnus-newsgroup-data))))
10677
10678 (defsubst gnus-cut-thread (thread)
10679   "Go forwards in the thread until we find an article that we want to display."
10680   (when (eq gnus-fetch-old-headers 'some)
10681     ;; Deal with old-fetched headers.
10682     (while (and thread
10683                 (memq (mail-header-number (car thread)) 
10684                       gnus-newsgroup-ancient)
10685                 (<= (length (cdr thread)) 1))
10686       (setq thread (cadr thread))))
10687   ;; Deal with sparse threads.
10688   (when (or (eq gnus-build-sparse-threads 'some)
10689             (eq gnus-build-sparse-threads 'more))
10690     (while (and thread
10691                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10692                 (= (length (cdr thread)) 1))
10693       (setq thread (cadr thread))))
10694   thread)
10695
10696 (defun gnus-cut-threads (threads)
10697   "Cut off all uninteresting articles from the beginning of threads."
10698   (when (or (eq gnus-fetch-old-headers 'some)
10699             (eq gnus-build-sparse-threads 'some)
10700             (eq gnus-build-sparse-threads 'more))
10701     (let ((th threads))
10702       (while th
10703         (setcar th (gnus-cut-thread (car th)))
10704         (setq th (cdr th)))))
10705   ;; Remove nixed out threads.
10706   (delq nil threads))
10707
10708 (defun gnus-summary-initial-limit (&optional show-if-empty)
10709   "Figure out what the initial limit is supposed to be on group entry.
10710 This entails weeding out unwanted dormants, low-scored articles,
10711 fetch-old-headers verbiage, and so on."
10712   ;; Most groups have nothing to remove.
10713   (if (or gnus-inhibit-limiting
10714           (and (null gnus-newsgroup-dormant)
10715                (not (eq gnus-fetch-old-headers 'some))
10716                (null gnus-summary-expunge-below)
10717                (not (eq gnus-build-sparse-threads 'some))
10718                (not (eq gnus-build-sparse-threads 'more))
10719                (null gnus-thread-expunge-below)
10720                (not gnus-use-nocem)))
10721       () ; Do nothing.
10722     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10723     (setq gnus-newsgroup-limit nil)
10724     (mapatoms
10725      (lambda (node)
10726        (unless (car (symbol-value node))
10727          ;; These threads have no parents -- they are roots.
10728          (let ((nodes (cdr (symbol-value node)))
10729                thread)
10730            (while nodes
10731              (if (and gnus-thread-expunge-below
10732                       (< (gnus-thread-total-score (car nodes))
10733                          gnus-thread-expunge-below))
10734                  (gnus-expunge-thread (pop nodes))
10735                (setq thread (pop nodes))
10736                (gnus-summary-limit-children thread))))))
10737      gnus-newsgroup-dependencies)
10738     ;; If this limitation resulted in an empty group, we might
10739     ;; pop the previous limit and use it instead.
10740     (when (and (not gnus-newsgroup-limit)
10741                show-if-empty)
10742       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10743     gnus-newsgroup-limit))
10744
10745 (defun gnus-summary-limit-children (thread)
10746   "Return 1 if this subthread is visible and 0 if it is not."
10747   ;; First we get the number of visible children to this thread.  This
10748   ;; is done by recursing down the thread using this function, so this
10749   ;; will really go down to a leaf article first, before slowly
10750   ;; working its way up towards the root.
10751   (when thread
10752     (let ((children
10753            (if (cdr thread)
10754                (apply '+ (mapcar 'gnus-summary-limit-children
10755                                  (cdr thread)))
10756              0))
10757           (number (mail-header-number (car thread)))
10758           score)
10759       (if (or
10760            ;; If this article is dormant and has absolutely no visible
10761            ;; children, then this article isn't visible.
10762            (and (memq number gnus-newsgroup-dormant)
10763                 (= children 0))
10764            ;; If this is a "fetch-old-headered" and there is only one
10765            ;; visible child (or less), then we don't want this article.
10766            (and (eq gnus-fetch-old-headers 'some)
10767                 (memq number gnus-newsgroup-ancient)
10768                 (zerop children))
10769            ;; If this is a sparsely inserted article with no children,
10770            ;; we don't want it.
10771            (and (eq gnus-build-sparse-threads 'some)
10772                 (memq number gnus-newsgroup-sparse)
10773                 (zerop children))
10774            ;; If we use expunging, and this article is really
10775            ;; low-scored, then we don't want this article.
10776            (when (and gnus-summary-expunge-below
10777                       (< (setq score
10778                                (or (cdr (assq number gnus-newsgroup-scored))
10779                                    gnus-summary-default-score))
10780                          gnus-summary-expunge-below))
10781              ;; We increase the expunge-tally here, but that has
10782              ;; nothing to do with the limits, really.
10783              (incf gnus-newsgroup-expunged-tally)
10784              ;; We also mark as read here, if that's wanted.
10785              (when (and gnus-summary-mark-below
10786                         (< score gnus-summary-mark-below))
10787                (setq gnus-newsgroup-unreads
10788                      (delq number gnus-newsgroup-unreads))
10789                (if gnus-newsgroup-auto-expire
10790                    (push number gnus-newsgroup-expirable)
10791                  (push (cons number gnus-low-score-mark)
10792                        gnus-newsgroup-reads)))
10793              t)
10794            (and gnus-use-nocem
10795                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10796           ;; Nope, invisible article.
10797           0
10798         ;; Ok, this article is to be visible, so we add it to the limit
10799         ;; and return 1.
10800         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10801         1))))
10802
10803 (defun gnus-expunge-thread (thread)
10804   "Mark all articles in THREAD as read."
10805   (let* ((number (mail-header-number (car thread))))
10806     (incf gnus-newsgroup-expunged-tally)
10807     ;; We also mark as read here, if that's wanted.
10808     (setq gnus-newsgroup-unreads
10809           (delq number gnus-newsgroup-unreads))
10810     (if gnus-newsgroup-auto-expire
10811         (push number gnus-newsgroup-expirable)
10812       (push (cons number gnus-low-score-mark)
10813             gnus-newsgroup-reads)))
10814   ;; Go recursively through all subthreads.
10815   (mapcar 'gnus-expunge-thread (cdr thread)))
10816
10817 ;; Summary article oriented commands
10818
10819 (defun gnus-summary-refer-parent-article (n)
10820   "Refer parent article N times.
10821 The difference between N and the number of articles fetched is returned."
10822   (interactive "p")
10823   (gnus-set-global-variables)
10824   (while
10825       (and
10826        (> n 0)
10827        (let* ((header (gnus-summary-article-header))
10828               (ref
10829                ;; If we try to find the parent of the currently
10830                ;; displayed article, then we take a look at the actual
10831                ;; References header, since this is slightly more
10832                ;; reliable than the References field we got from the
10833                ;; server.
10834                (if (and (eq (mail-header-number header)
10835                             (cdr gnus-article-current))
10836                         (equal gnus-newsgroup-name
10837                                (car gnus-article-current)))
10838                    (save-excursion
10839                      (set-buffer gnus-original-article-buffer)
10840                      (nnheader-narrow-to-headers)
10841                      (prog1
10842                          (mail-fetch-field "references")
10843                        (widen)))
10844                  ;; It's not the current article, so we take a bet on
10845                  ;; the value we got from the server.
10846                  (mail-header-references header))))
10847          (if (setq ref (or ref (mail-header-references header)))
10848              (or (gnus-summary-refer-article (gnus-parent-id ref))
10849                  (gnus-message 1 "Couldn't find parent"))
10850            (gnus-message 1 "No references in article %d"
10851                          (gnus-summary-article-number))
10852            nil)))
10853     (setq n (1- n)))
10854   (gnus-summary-position-point)
10855   n)
10856
10857 (defun gnus-summary-refer-references ()
10858   "Fetch all articles mentioned in the References header.
10859 Return how many articles were fetched."
10860   (interactive)
10861   (gnus-set-global-variables)
10862   (let ((ref (mail-header-references (gnus-summary-article-header)))
10863         (current (gnus-summary-article-number))
10864         (n 0))
10865     ;; For each Message-ID in the References header...
10866     (while (string-match "<[^>]*>" ref)
10867       (incf n)
10868       ;; ... fetch that article.
10869       (gnus-summary-refer-article
10870        (prog1 (match-string 0 ref)
10871          (setq ref (substring ref (match-end 0))))))
10872     (gnus-summary-goto-subject current)
10873     (gnus-summary-position-point)
10874     n))
10875
10876 (defun gnus-summary-refer-article (message-id)
10877   "Fetch an article specified by MESSAGE-ID."
10878   (interactive "sMessage-ID: ")
10879   (when (and (stringp message-id)
10880              (not (zerop (length message-id))))
10881     ;; Construct the correct Message-ID if necessary.
10882     ;; Suggested by tale@pawl.rpi.edu.
10883     (unless (string-match "^<" message-id)
10884       (setq message-id (concat "<" message-id)))
10885     (unless (string-match ">$" message-id)
10886       (setq message-id (concat message-id ">")))
10887     (let ((header (car (gnus-gethash message-id
10888                                      gnus-newsgroup-dependencies))))
10889       (if header
10890           ;; The article is present in the buffer, to we just go to it.
10891           (gnus-summary-goto-article (mail-header-number header) nil t)
10892         ;; We fetch the article
10893         (let ((gnus-override-method 
10894                (and (gnus-news-group-p gnus-newsgroup-name)
10895                     gnus-refer-article-method))
10896               number)
10897           ;; Start the special refer-article method, if necessary.
10898           (when gnus-refer-article-method
10899             (gnus-check-server gnus-refer-article-method))
10900           ;; Fetch the header, and display the article.
10901           (if (setq number (gnus-summary-insert-subject message-id))
10902               (gnus-summary-select-article nil nil nil number)
10903             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10904
10905 (defun gnus-summary-enter-digest-group (&optional force)
10906   "Enter a digest group based on the current article."
10907   (interactive "P")
10908   (gnus-set-global-variables)
10909   (gnus-summary-select-article)
10910   (let ((name (format "%s-%d"
10911                       (gnus-group-prefixed-name
10912                        gnus-newsgroup-name (list 'nndoc ""))
10913                       gnus-current-article))
10914         (ogroup gnus-newsgroup-name)
10915         (case-fold-search t)
10916         (buf (current-buffer))
10917         dig)
10918     (save-excursion
10919       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10920       (insert-buffer-substring gnus-original-article-buffer)
10921       (narrow-to-region
10922        (goto-char (point-min))
10923        (or (search-forward "\n\n" nil t) (point)))
10924       (goto-char (point-min))
10925       (delete-matching-lines "^\\(Path\\):\\|^From ")
10926       (widen))
10927     (unwind-protect
10928         (if (gnus-group-read-ephemeral-group
10929              name `(nndoc ,name (nndoc-address
10930                                  ,(get-buffer dig))
10931                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10932             ;; Make all postings to this group go to the parent group.
10933             (nconc (gnus-info-params (gnus-get-info name))
10934                    (list (cons 'to-group ogroup)))
10935           ;; Couldn't select this doc group.
10936           (switch-to-buffer buf)
10937           (gnus-set-global-variables)
10938           (gnus-configure-windows 'summary)
10939           (gnus-message 3 "Article couldn't be entered?"))
10940       (kill-buffer dig))))
10941
10942 (defun gnus-summary-isearch-article (&optional regexp-p)
10943   "Do incremental search forward on the current article.
10944 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10945   (interactive "P")
10946   (gnus-set-global-variables)
10947   (gnus-summary-select-article)
10948   (gnus-configure-windows 'article)
10949   (gnus-eval-in-buffer-window
10950    gnus-article-buffer
10951    (goto-char (point-min))
10952    (isearch-forward regexp-p)))
10953
10954 (defun gnus-summary-search-article-forward (regexp &optional backward)
10955   "Search for an article containing REGEXP forward.
10956 If BACKWARD, search backward instead."
10957   (interactive
10958    (list (read-string
10959           (format "Search article %s (regexp%s): "
10960                   (if current-prefix-arg "backward" "forward")
10961                   (if gnus-last-search-regexp
10962                       (concat ", default " gnus-last-search-regexp)
10963                     "")))
10964          current-prefix-arg))
10965   (gnus-set-global-variables)
10966   (if (string-equal regexp "")
10967       (setq regexp (or gnus-last-search-regexp ""))
10968     (setq gnus-last-search-regexp regexp))
10969   (if (gnus-summary-search-article regexp backward)
10970       (gnus-article-set-window-start
10971        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10972     (error "Search failed: \"%s\"" regexp)))
10973
10974 (defun gnus-summary-search-article-backward (regexp)
10975   "Search for an article containing REGEXP backward."
10976   (interactive
10977    (list (read-string
10978           (format "Search article backward (regexp%s): "
10979                   (if gnus-last-search-regexp
10980                       (concat ", default " gnus-last-search-regexp)
10981                     "")))))
10982   (gnus-summary-search-article-forward regexp 'backward))
10983
10984 (defun gnus-summary-search-article (regexp &optional backward)
10985   "Search for an article containing REGEXP.
10986 Optional argument BACKWARD means do search for backward.
10987 gnus-select-article-hook is not called during the search."
10988   (let ((gnus-select-article-hook nil)  ;Disable hook.
10989         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10990         (re-search
10991          (if backward
10992              (function re-search-backward) (function re-search-forward)))
10993         (found nil)
10994         (last nil))
10995     ;; Hidden thread subtrees must be searched for ,too.
10996     (gnus-summary-show-all-threads)
10997     ;; First of all, search current article.
10998     ;; We don't want to read article again from NNTP server nor reset
10999     ;; current point.
11000     (gnus-summary-select-article)
11001     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11002     (setq last gnus-current-article)
11003     (gnus-eval-in-buffer-window
11004      gnus-article-buffer
11005      (save-restriction
11006        (widen)
11007        ;; Begin search from current point.
11008        (setq found (funcall re-search regexp nil t))))
11009     ;; Then search next articles.
11010     (while (and (not found)
11011                 (gnus-summary-display-article
11012                  (if backward (gnus-summary-find-prev)
11013                    (gnus-summary-find-next))))
11014       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11015       (gnus-eval-in-buffer-window
11016        gnus-article-buffer
11017        (save-restriction
11018          (widen)
11019          (goto-char (if backward (point-max) (point-min)))
11020          (setq found (funcall re-search regexp nil t)))))
11021     (message "")
11022     ;; Adjust article pointer.
11023     (or (eq last gnus-current-article)
11024         (setq gnus-last-article last))
11025     ;; Return T if found such article.
11026     found))
11027
11028 (defun gnus-summary-find-matching (header regexp &optional backward unread
11029                                           not-case-fold)
11030   "Return a list of all articles that match REGEXP on HEADER.
11031 The search stars on the current article and goes forwards unless
11032 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11033 If UNREAD is non-nil, only unread articles will
11034 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11035 in the comparisons."
11036   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11037                 (gnus-data-find-list
11038                  (gnus-summary-article-number) (gnus-data-list backward))))
11039         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11040         (case-fold-search (not not-case-fold))
11041         articles d)
11042     (or (fboundp (intern (concat "mail-header-" header)))
11043         (error "%s is not a valid header" header))
11044     (while data
11045       (setq d (car data))
11046       (and (or (not unread)             ; We want all articles...
11047                (gnus-data-unread-p d))  ; Or just unreads.
11048            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11049            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11050            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11051       (setq data (cdr data)))
11052     (nreverse articles)))
11053
11054 (defun gnus-summary-execute-command (header regexp command &optional backward)
11055   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11056 If HEADER is an empty string (or nil), the match is done on the entire
11057 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11058   (interactive
11059    (list (let ((completion-ignore-case t))
11060            (completing-read
11061             "Header name: "
11062             (mapcar (lambda (string) (list string))
11063                     '("Number" "Subject" "From" "Lines" "Date"
11064                       "Message-ID" "Xref" "References" "Body"))
11065             nil 'require-match))
11066          (read-string "Regexp: ")
11067          (read-key-sequence "Command: ")
11068          current-prefix-arg))
11069   (when (equal header "Body")
11070     (setq header ""))
11071   (gnus-set-global-variables)
11072   ;; Hidden thread subtrees must be searched as well.
11073   (gnus-summary-show-all-threads)
11074   ;; We don't want to change current point nor window configuration.
11075   (save-excursion
11076     (save-window-excursion
11077       (gnus-message 6 "Executing %s..." (key-description command))
11078       ;; We'd like to execute COMMAND interactively so as to give arguments.
11079       (gnus-execute header regexp
11080                     `(lambda () (call-interactively ',(key-binding command)))
11081                     backward)
11082       (gnus-message 6 "Executing %s...done" (key-description command)))))
11083
11084 (defun gnus-summary-beginning-of-article ()
11085   "Scroll the article back to the beginning."
11086   (interactive)
11087   (gnus-set-global-variables)
11088   (gnus-summary-select-article)
11089   (gnus-configure-windows 'article)
11090   (gnus-eval-in-buffer-window
11091    gnus-article-buffer
11092    (widen)
11093    (goto-char (point-min))
11094    (and gnus-break-pages (gnus-narrow-to-page))))
11095
11096 (defun gnus-summary-end-of-article ()
11097   "Scroll to the end of the article."
11098   (interactive)
11099   (gnus-set-global-variables)
11100   (gnus-summary-select-article)
11101   (gnus-configure-windows 'article)
11102   (gnus-eval-in-buffer-window
11103    gnus-article-buffer
11104    (widen)
11105    (goto-char (point-max))
11106    (recenter -3)
11107    (and gnus-break-pages (gnus-narrow-to-page))))
11108
11109 (defun gnus-summary-show-article (&optional arg)
11110   "Force re-fetching of the current article.
11111 If ARG (the prefix) is non-nil, show the raw article without any
11112 article massaging functions being run."
11113   (interactive "P")
11114   (gnus-set-global-variables)
11115   (if (not arg)
11116       ;; Select the article the normal way.
11117       (gnus-summary-select-article nil 'force)
11118     ;; Bind the article treatment functions to nil.
11119     (let ((gnus-have-all-headers t)
11120           gnus-article-display-hook
11121           gnus-article-prepare-hook
11122           gnus-break-pages
11123           gnus-visual)
11124       (gnus-summary-select-article nil 'force)))
11125 ;  (gnus-configure-windows 'article)
11126   (gnus-summary-position-point))
11127
11128 (defun gnus-summary-verbose-headers (&optional arg)
11129   "Toggle permanent full header display.
11130 If ARG is a positive number, turn header display on.
11131 If ARG is a negative number, turn header display off."
11132   (interactive "P")
11133   (gnus-set-global-variables)
11134   (gnus-summary-toggle-header arg)
11135   (setq gnus-show-all-headers
11136         (cond ((or (not (numberp arg))
11137                    (zerop arg))
11138                (not gnus-show-all-headers))
11139               ((natnump arg)
11140                t))))
11141
11142 (defun gnus-summary-toggle-header (&optional arg)
11143   "Show the headers if they are hidden, or hide them if they are shown.
11144 If ARG is a positive number, show the entire header.
11145 If ARG is a negative number, hide the unwanted header lines."
11146   (interactive "P")
11147   (gnus-set-global-variables)
11148   (save-excursion
11149     (set-buffer gnus-article-buffer)
11150     (let* ((buffer-read-only nil)
11151            (inhibit-point-motion-hooks t)
11152            (hidden (text-property-any
11153                     (goto-char (point-min)) (search-forward "\n\n")
11154                     'invisible t))
11155            e)
11156       (goto-char (point-min))
11157       (when (search-forward "\n\n" nil t)
11158         (delete-region (point-min) (1- (point))))
11159       (goto-char (point-min))
11160       (save-excursion
11161         (set-buffer gnus-original-article-buffer)
11162         (goto-char (point-min))
11163         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11164       (insert-buffer-substring gnus-original-article-buffer 1 e)
11165       (let ((gnus-inhibit-hiding t))
11166         (run-hooks 'gnus-article-display-hook))
11167       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11168           (gnus-article-hide-headers)))))
11169
11170 (defun gnus-summary-show-all-headers ()
11171   "Make all header lines visible."
11172   (interactive)
11173   (gnus-set-global-variables)
11174   (gnus-article-show-all-headers))
11175
11176 (defun gnus-summary-toggle-mime (&optional arg)
11177   "Toggle MIME processing.
11178 If ARG is a positive number, turn MIME processing on."
11179   (interactive "P")
11180   (gnus-set-global-variables)
11181   (setq gnus-show-mime
11182         (if (null arg) (not gnus-show-mime)
11183           (> (prefix-numeric-value arg) 0)))
11184   (gnus-summary-select-article t 'force))
11185
11186 (defun gnus-summary-caesar-message (&optional arg)
11187   "Caesar rotate the current article by 13.
11188 The numerical prefix specifies how manu places to rotate each letter
11189 forward."
11190   (interactive "P")
11191   (gnus-set-global-variables)
11192   (gnus-summary-select-article)
11193   (let ((mail-header-separator ""))
11194     (gnus-eval-in-buffer-window
11195      gnus-article-buffer
11196      (save-restriction
11197        (widen)
11198        (let ((start (window-start)))
11199          (news-caesar-buffer-body arg)
11200          (set-window-start (get-buffer-window (current-buffer)) start))))))
11201
11202 (defun gnus-summary-stop-page-breaking ()
11203   "Stop page breaking in the current article."
11204   (interactive)
11205   (gnus-set-global-variables)
11206   (gnus-summary-select-article)
11207   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11208
11209 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11210   "Move the current article to a different newsgroup.
11211 If N is a positive number, move the N next articles.
11212 If N is a negative number, move the N previous articles.
11213 If N is nil and any articles have been marked with the process mark,
11214 move those articles instead.
11215 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11216 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11217 re-spool using this method.
11218
11219 For this function to work, both the current newsgroup and the
11220 newsgroup that you want to move to have to support the `request-move'
11221 and `request-accept' functions."
11222   (interactive "P")
11223   (unless action (setq action 'move))
11224   (gnus-set-global-variables)
11225   ;; Check whether the source group supports the required functions.
11226   (cond ((and (eq action 'move)
11227               (not (gnus-check-backend-function
11228                     'request-move-article gnus-newsgroup-name)))
11229          (error "The current group does not support article moving"))
11230         ((and (eq action 'crosspost)
11231               (not (gnus-check-backend-function
11232                     'request-replace-article gnus-newsgroup-name)))
11233          (error "The current group does not support article editing")))
11234   (let ((articles (gnus-summary-work-articles n))
11235         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11236         (names '((move "move" "Moving")
11237                  (copy "copy" "Copying")
11238                  (crosspost "crosspost" "Crossposting")))
11239         (copy-buf (save-excursion
11240                     (nnheader-set-temp-buffer " *copy article*")))
11241         art-group to-method new-xref article to-groups)
11242     (unless (assq action names)
11243       (error "Unknown action %s" action))
11244     ;; Read the newsgroup name.
11245     (when (and (not to-newsgroup)
11246                (not select-method))
11247       (setq to-newsgroup
11248             (gnus-read-move-group-name
11249              (cadr (assq action names))
11250              gnus-current-move-group articles prefix))
11251       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11252     (setq to-method (or select-method 
11253                         (gnus-find-method-for-group to-newsgroup)))
11254     ;; Check the method we are to move this article to...
11255     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11256         (error "%s does not support article copying" (car to-method)))
11257     (or (gnus-check-server to-method)
11258         (error "Can't open server %s" (car to-method)))
11259     (gnus-message 6 "%s to %s: %s..."
11260                   (caddr (assq action names))
11261                   (or (car select-method) to-newsgroup) articles)
11262     (while articles
11263       (setq article (pop articles))
11264       (setq
11265        art-group
11266        (cond
11267         ;; Move the article.
11268         ((eq action 'move)
11269          (gnus-request-move-article
11270           article                       ; Article to move
11271           gnus-newsgroup-name           ; From newsgrouo
11272           (nth 1 (gnus-find-method-for-group
11273                   gnus-newsgroup-name)) ; Server
11274           (list 'gnus-request-accept-article
11275                 to-newsgroup (list 'quote select-method)
11276                 (not articles))         ; Accept form
11277           (not articles)))              ; Only save nov last time
11278         ;; Copy the article.
11279         ((eq action 'copy)
11280          (save-excursion
11281            (set-buffer copy-buf)
11282            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11283            (gnus-request-accept-article
11284             to-newsgroup select-method (not articles))))
11285         ;; Crosspost the article.
11286         ((eq action 'crosspost)
11287          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11288            (setq new-xref (concat gnus-newsgroup-name ":" article))
11289            (if (and xref (not (string= xref "")))
11290                (progn
11291                  (when (string-match "^Xref: " xref)
11292                    (setq xref (substring xref (match-end 0))))
11293                  (setq new-xref (concat xref " " new-xref)))
11294              (setq new-xref (concat (system-name) " " new-xref)))
11295            (save-excursion
11296              (set-buffer copy-buf)
11297              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11298              (nnheader-replace-header "xref" new-xref)
11299              (gnus-request-accept-article
11300               to-newsgroup select-method (not articles)))))))
11301       (if (not art-group)
11302           (gnus-message 1 "Couldn't %s article %s"
11303                         (cadr (assq action names)) article)
11304         (let* ((entry
11305                 (or
11306                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11307                  (gnus-gethash
11308                   (gnus-group-prefixed-name
11309                    (car art-group)
11310                    (or select-method 
11311                        (gnus-find-method-for-group to-newsgroup)))
11312                   gnus-newsrc-hashtb)))
11313                (info (nth 2 entry))
11314                (to-group (gnus-info-group info)))
11315           ;; Update the group that has been moved to.
11316           (when (and info
11317                      (memq action '(move copy)))
11318             (unless (member to-group to-groups)
11319               (push to-group to-groups))
11320
11321             (unless (memq article gnus-newsgroup-unreads)
11322               (gnus-info-set-read
11323                info (gnus-add-to-range (gnus-info-read info)
11324                                        (list (cdr art-group)))))
11325
11326             ;; Copy any marks over to the new group.
11327             (let ((marks gnus-article-mark-lists)
11328                   (to-article (cdr art-group)))
11329
11330               ;; See whether the article is to be put in the cache.
11331               (when gnus-use-cache
11332                 (gnus-cache-possibly-enter-article
11333                  to-group to-article
11334                  (let ((header (copy-sequence
11335                                 (gnus-summary-article-header article))))
11336                    (mail-header-set-number header to-article)
11337                    header)
11338                  (memq article gnus-newsgroup-marked)
11339                  (memq article gnus-newsgroup-dormant)
11340                  (memq article gnus-newsgroup-unreads)))
11341
11342               (while marks
11343                 (when (memq article (symbol-value
11344                                      (intern (format "gnus-newsgroup-%s"
11345                                                      (caar marks)))))
11346                   ;; If the other group is the same as this group,
11347                   ;; then we have to add the mark to the list.
11348                   (when (equal to-group gnus-newsgroup-name)
11349                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11350                          (cons to-article
11351                                (symbol-value
11352                                 (intern (format "gnus-newsgroup-%s"
11353                                                 (caar marks)))))))
11354                   ;; Copy mark to other group.
11355                   (gnus-add-marked-articles
11356                    to-group (cdar marks) (list to-article) info))
11357                 (setq marks (cdr marks)))))
11358
11359           ;; Update the Xref header in this article to point to
11360           ;; the new crossposted article we have just created.
11361           (when (eq action 'crosspost)
11362             (save-excursion
11363               (set-buffer copy-buf)
11364               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11365               (nnheader-replace-header
11366                "xref" (concat new-xref " " (gnus-group-prefixed-name
11367                                             (car art-group) to-method)
11368                               ":" (cdr art-group)))
11369               (gnus-request-replace-article
11370                article gnus-newsgroup-name (current-buffer)))))
11371
11372         (gnus-summary-goto-subject article)
11373         (when (eq action 'move)
11374           (gnus-summary-mark-article article gnus-canceled-mark)))
11375       (gnus-summary-remove-process-mark article))
11376     ;; Re-activate all groups that have been moved to.
11377     (while to-groups
11378       (gnus-activate-group (pop to-groups)))
11379     
11380     (gnus-kill-buffer copy-buf)
11381     (gnus-summary-position-point)
11382     (gnus-set-mode-line 'summary)))
11383
11384 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11385   "Move the current article to a different newsgroup.
11386 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11387 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11388 re-spool using this method."
11389   (interactive "P")
11390   (gnus-summary-move-article n nil select-method 'copy))
11391
11392 (defun gnus-summary-crosspost-article (&optional n)
11393   "Crosspost the current article to some other group."
11394   (interactive "P")
11395   (gnus-summary-move-article n nil nil 'crosspost))
11396
11397 (defvar gnus-summary-respool-default-method nil
11398   "Default method for respooling an article.  
11399 If nil, use to the current newsgroup method.")
11400
11401 (defun gnus-summary-respool-article (&optional n method)
11402   "Respool the current article.
11403 The article will be squeezed through the mail spooling process again,
11404 which means that it will be put in some mail newsgroup or other
11405 depending on `nnmail-split-methods'.
11406 If N is a positive number, respool the N next articles.
11407 If N is a negative number, respool the N previous articles.
11408 If N is nil and any articles have been marked with the process mark,
11409 respool those articles instead.
11410
11411 Respooling can be done both from mail groups and \"real\" newsgroups.
11412 In the former case, the articles in question will be moved from the
11413 current group into whatever groups they are destined to.  In the
11414 latter case, they will be copied into the relevant groups."
11415   (interactive 
11416    (list current-prefix-arg
11417          (let* ((methods (gnus-methods-using 'respool))
11418                 (methname
11419                  (symbol-name (or gnus-summary-respool-default-method
11420                                   (car (gnus-find-method-for-group
11421                                         gnus-newsgroup-name)))))
11422                 (method
11423                  (gnus-completing-read 
11424                   methname "What backend do you want to use when? "
11425                   methods nil t nil 'gnus-method-history))
11426                 ms)
11427            (cond
11428             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11429              (list (intern method) ""))
11430             ((= 1 (length ms))
11431              (car ms))
11432             (t
11433              (cdr (completing-read 
11434                    "Server name: "
11435                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11436   (gnus-set-global-variables)
11437   (unless method
11438     (error "No method given for respooling"))
11439   (if (assoc (symbol-name
11440               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11441              (gnus-methods-using 'respool))
11442       (gnus-summary-move-article n nil method)
11443     (gnus-summary-copy-article n nil method)))
11444
11445 (defun gnus-summary-import-article (file)
11446   "Import a random file into a mail newsgroup."
11447   (interactive "fImport file: ")
11448   (gnus-set-global-variables)
11449   (let ((group gnus-newsgroup-name)
11450         (now (current-time))
11451         atts lines)
11452     (or (gnus-check-backend-function 'request-accept-article group)
11453         (error "%s does not support article importing" group))
11454     (or (file-readable-p file)
11455         (not (file-regular-p file))
11456         (error "Can't read %s" file))
11457     (save-excursion
11458       (set-buffer (get-buffer-create " *import file*"))
11459       (buffer-disable-undo (current-buffer))
11460       (erase-buffer)
11461       (insert-file-contents file)
11462       (goto-char (point-min))
11463       (unless (nnheader-article-p)
11464         ;; This doesn't look like an article, so we fudge some headers.
11465         (setq atts (file-attributes file)
11466               lines (count-lines (point-min) (point-max)))
11467         (insert "From: " (read-string "From: ") "\n"
11468                 "Subject: " (read-string "Subject: ") "\n"
11469                 "Date: " (timezone-make-date-arpa-standard
11470                           (current-time-string (nth 5 atts))
11471                           (current-time-zone now)
11472                           (current-time-zone now)) "\n"
11473                 "Message-ID: " (gnus-inews-message-id) "\n"
11474                 "Lines: " (int-to-string lines) "\n"
11475                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11476       (gnus-request-accept-article group nil t)
11477       (kill-buffer (current-buffer)))))
11478
11479 (defun gnus-summary-expire-articles ()
11480   "Expire all articles that are marked as expirable in the current group."
11481   (interactive)
11482   (gnus-set-global-variables)
11483   (when (gnus-check-backend-function
11484          'request-expire-articles gnus-newsgroup-name)
11485     ;; This backend supports expiry.
11486     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11487            (expirable (if total
11488                           (gnus-list-of-read-articles gnus-newsgroup-name)
11489                         (setq gnus-newsgroup-expirable
11490                               (sort gnus-newsgroup-expirable '<))))
11491            (expiry-wait (gnus-group-get-parameter
11492                          gnus-newsgroup-name 'expiry-wait))
11493            es)
11494       (when expirable
11495         ;; There are expirable articles in this group, so we run them
11496         ;; through the expiry process.
11497         (gnus-message 6 "Expiring articles...")
11498         ;; The list of articles that weren't expired is returned.
11499         (if expiry-wait
11500             (let ((nnmail-expiry-wait-function nil)
11501                   (nnmail-expiry-wait expiry-wait))
11502               (setq es (gnus-request-expire-articles
11503                         expirable gnus-newsgroup-name)))
11504           (setq es (gnus-request-expire-articles
11505                     expirable gnus-newsgroup-name)))
11506         (or total (setq gnus-newsgroup-expirable es))
11507         ;; We go through the old list of expirable, and mark all
11508         ;; really expired articles as nonexistent.
11509         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11510           (let ((gnus-use-cache nil))
11511             (while expirable
11512               (unless (memq (car expirable) es)
11513                 (when (gnus-data-find (car expirable))
11514                   (gnus-summary-mark-article
11515                    (car expirable) gnus-canceled-mark)))
11516               (setq expirable (cdr expirable)))))
11517         (gnus-message 6 "Expiring articles...done")))))
11518
11519 (defun gnus-summary-expire-articles-now ()
11520   "Expunge all expirable articles in the current group.
11521 This means that *all* articles that are marked as expirable will be
11522 deleted forever, right now."
11523   (interactive)
11524   (gnus-set-global-variables)
11525   (or gnus-expert-user
11526       (gnus-y-or-n-p
11527        "Are you really, really, really sure you want to expunge? ")
11528       (error "Phew!"))
11529   (let ((nnmail-expiry-wait 'immediate)
11530         (nnmail-expiry-wait-function nil))
11531     (gnus-summary-expire-articles)))
11532
11533 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11534 (defun gnus-summary-delete-article (&optional n)
11535   "Delete the N next (mail) articles.
11536 This command actually deletes articles.  This is not a marking
11537 command.  The article will disappear forever from your life, never to
11538 return.
11539 If N is negative, delete backwards.
11540 If N is nil and articles have been marked with the process mark,
11541 delete these instead."
11542   (interactive "P")
11543   (gnus-set-global-variables)
11544   (or (gnus-check-backend-function 'request-expire-articles
11545                                    gnus-newsgroup-name)
11546       (error "The current newsgroup does not support article deletion."))
11547   ;; Compute the list of articles to delete.
11548   (let ((articles (gnus-summary-work-articles n))
11549         not-deleted)
11550     (if (and gnus-novice-user
11551              (not (gnus-y-or-n-p
11552                    (format "Do you really want to delete %s forever? "
11553                            (if (> (length articles) 1) "these articles"
11554                              "this article")))))
11555         ()
11556       ;; Delete the articles.
11557       (setq not-deleted (gnus-request-expire-articles
11558                          articles gnus-newsgroup-name 'force))
11559       (while articles
11560         (gnus-summary-remove-process-mark (car articles))
11561         ;; The backend might not have been able to delete the article
11562         ;; after all.
11563         (or (memq (car articles) not-deleted)
11564             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11565         (setq articles (cdr articles))))
11566     (gnus-summary-position-point)
11567     (gnus-set-mode-line 'summary)
11568     not-deleted))
11569
11570 (defun gnus-summary-edit-article (&optional force)
11571   "Enter into a buffer and edit the current article.
11572 This will have permanent effect only in mail groups.
11573 If FORCE is non-nil, allow editing of articles even in read-only
11574 groups."
11575   (interactive "P")
11576   (save-excursion
11577     (set-buffer gnus-summary-buffer)
11578     (gnus-set-global-variables)
11579     (when (and (not force)
11580                (gnus-group-read-only-p))
11581       (error "The current newsgroup does not support article editing."))
11582     (gnus-summary-select-article t nil t)
11583     (gnus-configure-windows 'article)
11584     (select-window (get-buffer-window gnus-article-buffer))
11585     (gnus-message 6 "C-c C-c to end edits")
11586     (setq buffer-read-only nil)
11587     (text-mode)
11588     (use-local-map (copy-keymap (current-local-map)))
11589     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11590     (buffer-enable-undo)
11591     (widen)
11592     (goto-char (point-min))
11593     (search-forward "\n\n" nil t)))
11594
11595 (defun gnus-summary-edit-article-done ()
11596   "Make edits to the current article permanent."
11597   (interactive)
11598   (if (gnus-group-read-only-p)
11599       (progn
11600         (gnus-summary-edit-article-postpone)
11601         (gnus-error
11602          1 "The current newsgroup does not support article editing."))
11603     (let ((buf (format "%s" (buffer-string))))
11604       (erase-buffer)
11605       (insert buf)
11606       (if (not (gnus-request-replace-article
11607                 (cdr gnus-article-current) (car gnus-article-current)
11608                 (current-buffer)))
11609           (error "Couldn't replace article.")
11610         (gnus-article-mode)
11611         (use-local-map gnus-article-mode-map)
11612         (setq buffer-read-only t)
11613         (buffer-disable-undo (current-buffer))
11614         (gnus-configure-windows 'summary)
11615         (gnus-summary-update-article (cdr gnus-article-current))
11616         (when gnus-use-cache
11617           (gnus-cache-update-article 
11618            (cdr gnus-article-current) (car gnus-article-current))))
11619       (save-excursion
11620         (when (get-buffer gnus-original-article-buffer)
11621           (set-buffer gnus-original-article-buffer)
11622           (setq gnus-original-article nil)))
11623       (setq gnus-article-current nil
11624             gnus-current-article nil)
11625       (run-hooks 'gnus-article-display-hook)
11626       (and (gnus-visual-p 'summary-highlight 'highlight)
11627            (run-hooks 'gnus-visual-mark-article-hook)))))
11628
11629 (defun gnus-summary-edit-article-postpone ()
11630   "Postpone changes to the current article."
11631   (interactive)
11632   (gnus-article-mode)
11633   (use-local-map gnus-article-mode-map)
11634   (setq buffer-read-only t)
11635   (buffer-disable-undo (current-buffer))
11636   (gnus-configure-windows 'summary)
11637   (and (gnus-visual-p 'summary-highlight 'highlight)
11638        (run-hooks 'gnus-visual-mark-article-hook)))
11639
11640 (defun gnus-summary-respool-query ()
11641   "Query where the respool algorithm would put this article."
11642   (interactive)
11643   (gnus-set-global-variables)
11644   (gnus-summary-select-article)
11645   (save-excursion
11646     (set-buffer gnus-article-buffer)
11647     (save-restriction
11648       (goto-char (point-min))
11649       (search-forward "\n\n")
11650       (narrow-to-region (point-min) (point))
11651       (pp-eval-expression
11652        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11653
11654 ;; Summary score commands.
11655
11656 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11657
11658 (defun gnus-summary-raise-score (n)
11659   "Raise the score of the current article by N."
11660   (interactive "p")
11661   (gnus-set-global-variables)
11662   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11663
11664 (defun gnus-summary-set-score (n)
11665   "Set the score of the current article to N."
11666   (interactive "p")
11667   (gnus-set-global-variables)
11668   (save-excursion
11669     (gnus-summary-show-thread)
11670     (let ((buffer-read-only nil))
11671       ;; Set score.
11672       (gnus-summary-update-mark
11673        (if (= n (or gnus-summary-default-score 0)) ? 
11674          (if (< n (or gnus-summary-default-score 0))
11675              gnus-score-below-mark gnus-score-over-mark)) 'score))
11676     (let* ((article (gnus-summary-article-number))
11677            (score (assq article gnus-newsgroup-scored)))
11678       (if score (setcdr score n)
11679         (setq gnus-newsgroup-scored
11680               (cons (cons article n) gnus-newsgroup-scored))))
11681     (gnus-summary-update-line)))
11682
11683 (defun gnus-summary-current-score ()
11684   "Return the score of the current article."
11685   (interactive)
11686   (gnus-set-global-variables)
11687   (gnus-message 1 "%s" (gnus-summary-article-score)))
11688
11689 ;; Summary marking commands.
11690
11691 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11692   "Mark articles which has the same subject as read, and then select the next.
11693 If UNMARK is positive, remove any kind of mark.
11694 If UNMARK is negative, tick articles."
11695   (interactive "P")
11696   (gnus-set-global-variables)
11697   (if unmark
11698       (setq unmark (prefix-numeric-value unmark)))
11699   (let ((count
11700          (gnus-summary-mark-same-subject
11701           (gnus-summary-article-subject) unmark)))
11702     ;; Select next unread article.  If auto-select-same mode, should
11703     ;; select the first unread article.
11704     (gnus-summary-next-article t (and gnus-auto-select-same
11705                                       (gnus-summary-article-subject)))
11706     (gnus-message 7 "%d article%s marked as %s"
11707                   count (if (= count 1) " is" "s are")
11708                   (if unmark "unread" "read"))))
11709
11710 (defun gnus-summary-kill-same-subject (&optional unmark)
11711   "Mark articles which has the same subject as read.
11712 If UNMARK is positive, remove any kind of mark.
11713 If UNMARK is negative, tick articles."
11714   (interactive "P")
11715   (gnus-set-global-variables)
11716   (if unmark
11717       (setq unmark (prefix-numeric-value unmark)))
11718   (let ((count
11719          (gnus-summary-mark-same-subject
11720           (gnus-summary-article-subject) unmark)))
11721     ;; If marked as read, go to next unread subject.
11722     (if (null unmark)
11723         ;; Go to next unread subject.
11724         (gnus-summary-next-subject 1 t))
11725     (gnus-message 7 "%d articles are marked as %s"
11726                   count (if unmark "unread" "read"))))
11727
11728 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11729   "Mark articles with same SUBJECT as read, and return marked number.
11730 If optional argument UNMARK is positive, remove any kinds of marks.
11731 If optional argument UNMARK is negative, mark articles as unread instead."
11732   (let ((count 1))
11733     (save-excursion
11734       (cond
11735        ((null unmark)                   ; Mark as read.
11736         (while (and
11737                 (progn
11738                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11739                   (gnus-summary-show-thread) t)
11740                 (gnus-summary-find-subject subject))
11741           (setq count (1+ count))))
11742        ((> unmark 0)                    ; Tick.
11743         (while (and
11744                 (progn
11745                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11746                   (gnus-summary-show-thread) t)
11747                 (gnus-summary-find-subject subject))
11748           (setq count (1+ count))))
11749        (t                               ; Mark as unread.
11750         (while (and
11751                 (progn
11752                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11753                   (gnus-summary-show-thread) t)
11754                 (gnus-summary-find-subject subject))
11755           (setq count (1+ count)))))
11756       (gnus-set-mode-line 'summary)
11757       ;; Return the number of marked articles.
11758       count)))
11759
11760 (defun gnus-summary-mark-as-processable (n &optional unmark)
11761   "Set the process mark on the next N articles.
11762 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11763 the process mark instead.  The difference between N and the actual
11764 number of articles marked is returned."
11765   (interactive "p")
11766   (gnus-set-global-variables)
11767   (let ((backward (< n 0))
11768         (n (abs n)))
11769     (while (and
11770             (> n 0)
11771             (if unmark
11772                 (gnus-summary-remove-process-mark
11773                  (gnus-summary-article-number))
11774               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11775             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11776       (setq n (1- n)))
11777     (if (/= 0 n) (gnus-message 7 "No more articles"))
11778     (gnus-summary-recenter)
11779     (gnus-summary-position-point)
11780     n))
11781
11782 (defun gnus-summary-unmark-as-processable (n)
11783   "Remove the process mark from the next N articles.
11784 If N is negative, mark backward instead.  The difference between N and
11785 the actual number of articles marked is returned."
11786   (interactive "p")
11787   (gnus-set-global-variables)
11788   (gnus-summary-mark-as-processable n t))
11789
11790 (defun gnus-summary-unmark-all-processable ()
11791   "Remove the process mark from all articles."
11792   (interactive)
11793   (gnus-set-global-variables)
11794   (save-excursion
11795     (while gnus-newsgroup-processable
11796       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11797   (gnus-summary-position-point))
11798
11799 (defun gnus-summary-mark-as-expirable (n)
11800   "Mark N articles forward as expirable.
11801 If N is negative, mark backward instead.  The difference between N and
11802 the actual number of articles marked is returned."
11803   (interactive "p")
11804   (gnus-set-global-variables)
11805   (gnus-summary-mark-forward n gnus-expirable-mark))
11806
11807 (defun gnus-summary-mark-article-as-replied (article)
11808   "Mark ARTICLE replied and update the summary line."
11809   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11810   (let ((buffer-read-only nil))
11811     (when (gnus-summary-goto-subject article)
11812       (gnus-summary-update-secondary-mark article))))
11813
11814 (defun gnus-summary-set-bookmark (article)
11815   "Set a bookmark in current article."
11816   (interactive (list (gnus-summary-article-number)))
11817   (gnus-set-global-variables)
11818   (if (or (not (get-buffer gnus-article-buffer))
11819           (not gnus-current-article)
11820           (not gnus-article-current)
11821           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11822       (error "No current article selected"))
11823   ;; Remove old bookmark, if one exists.
11824   (let ((old (assq article gnus-newsgroup-bookmarks)))
11825     (if old (setq gnus-newsgroup-bookmarks
11826                   (delq old gnus-newsgroup-bookmarks))))
11827   ;; Set the new bookmark, which is on the form
11828   ;; (article-number . line-number-in-body).
11829   (setq gnus-newsgroup-bookmarks
11830         (cons
11831          (cons article
11832                (save-excursion
11833                  (set-buffer gnus-article-buffer)
11834                  (count-lines
11835                   (min (point)
11836                        (save-excursion
11837                          (goto-char (point-min))
11838                          (search-forward "\n\n" nil t)
11839                          (point)))
11840                   (point))))
11841          gnus-newsgroup-bookmarks))
11842   (gnus-message 6 "A bookmark has been added to the current article."))
11843
11844 (defun gnus-summary-remove-bookmark (article)
11845   "Remove the bookmark from the current article."
11846   (interactive (list (gnus-summary-article-number)))
11847   (gnus-set-global-variables)
11848   ;; Remove old bookmark, if one exists.
11849   (let ((old (assq article gnus-newsgroup-bookmarks)))
11850     (if old
11851         (progn
11852           (setq gnus-newsgroup-bookmarks
11853                 (delq old gnus-newsgroup-bookmarks))
11854           (gnus-message 6 "Removed bookmark."))
11855       (gnus-message 6 "No bookmark in current article."))))
11856
11857 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11858 (defun gnus-summary-mark-as-dormant (n)
11859   "Mark N articles forward as dormant.
11860 If N is negative, mark backward instead.  The difference between N and
11861 the actual number of articles marked is returned."
11862   (interactive "p")
11863   (gnus-set-global-variables)
11864   (gnus-summary-mark-forward n gnus-dormant-mark))
11865
11866 (defun gnus-summary-set-process-mark (article)
11867   "Set the process mark on ARTICLE and update the summary line."
11868   (setq gnus-newsgroup-processable
11869         (cons article
11870               (delq article gnus-newsgroup-processable)))
11871   (when (gnus-summary-goto-subject article)
11872     (gnus-summary-show-thread)
11873     (gnus-summary-update-secondary-mark article)))
11874
11875 (defun gnus-summary-remove-process-mark (article)
11876   "Remove the process mark from ARTICLE and update the summary line."
11877   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11878   (when (gnus-summary-goto-subject article)
11879     (gnus-summary-show-thread)
11880     (gnus-summary-update-secondary-mark article)))
11881
11882 (defun gnus-summary-set-saved-mark (article)
11883   "Set the process mark on ARTICLE and update the summary line."
11884   (push article gnus-newsgroup-saved)
11885   (when (gnus-summary-goto-subject article)
11886     (gnus-summary-update-secondary-mark article)))
11887
11888 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11889   "Mark N articles as read forwards.
11890 If N is negative, mark backwards instead.
11891 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11892 marked as unread.
11893 The difference between N and the actual number of articles marked is
11894 returned."
11895   (interactive "p")
11896   (gnus-set-global-variables)
11897   (let ((backward (< n 0))
11898         (gnus-summary-goto-unread
11899          (and gnus-summary-goto-unread
11900               (not (eq gnus-summary-goto-unread 'never))
11901               (not (memq mark (list gnus-unread-mark
11902                                     gnus-ticked-mark gnus-dormant-mark)))))
11903         (n (abs n))
11904         (mark (or mark gnus-del-mark)))
11905     (while (and (> n 0)
11906                 (gnus-summary-mark-article nil mark no-expire)
11907                 (zerop (gnus-summary-next-subject
11908                         (if backward -1 1)
11909                         (and gnus-summary-goto-unread
11910                              (not (eq gnus-summary-goto-unread 'never)))
11911                         t)))
11912       (setq n (1- n)))
11913     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11914     (gnus-summary-recenter)
11915     (gnus-summary-position-point)
11916     (gnus-set-mode-line 'summary)
11917     n))
11918
11919 (defun gnus-summary-mark-article-as-read (mark)
11920   "Mark the current article quickly as read with MARK."
11921   (let ((article (gnus-summary-article-number)))
11922     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11923     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11924     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11925     (setq gnus-newsgroup-reads
11926           (cons (cons article mark) gnus-newsgroup-reads))
11927     ;; Possibly remove from cache, if that is used.
11928     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11929     ;; Allow the backend to change the mark.
11930     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11931     ;; Check for auto-expiry.
11932     (when (and gnus-newsgroup-auto-expire
11933                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11934                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11935                    (= mark gnus-ancient-mark)
11936                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11937       (setq mark gnus-expirable-mark)
11938       (push article gnus-newsgroup-expirable))
11939     ;; Set the mark in the buffer.
11940     (gnus-summary-update-mark mark 'unread)
11941     t))
11942
11943 (defun gnus-summary-mark-article-as-unread (mark)
11944   "Mark the current article quickly as unread with MARK."
11945   (let ((article (gnus-summary-article-number)))
11946     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11947     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11948     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11949     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11950     (cond ((= mark gnus-ticked-mark)
11951            (push article gnus-newsgroup-marked))
11952           ((= mark gnus-dormant-mark)
11953            (push article gnus-newsgroup-dormant))
11954           (t
11955            (push article gnus-newsgroup-unreads)))
11956     (setq gnus-newsgroup-reads
11957           (delq (assq article gnus-newsgroup-reads)
11958                 gnus-newsgroup-reads))
11959
11960     ;; See whether the article is to be put in the cache.
11961     (and gnus-use-cache
11962          (vectorp (gnus-summary-article-header article))
11963          (save-excursion
11964            (gnus-cache-possibly-enter-article
11965             gnus-newsgroup-name article
11966             (gnus-summary-article-header article)
11967             (= mark gnus-ticked-mark)
11968             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11969
11970     ;; Fix the mark.
11971     (gnus-summary-update-mark mark 'unread)
11972     t))
11973
11974 (defun gnus-summary-mark-article (&optional article mark no-expire)
11975   "Mark ARTICLE with MARK.  MARK can be any character.
11976 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11977 `??' (dormant) and `?E' (expirable).
11978 If MARK is nil, then the default character `?D' is used.
11979 If ARTICLE is nil, then the article on the current line will be
11980 marked."
11981   ;; The mark might be a string.
11982   (and (stringp mark)
11983        (setq mark (aref mark 0)))
11984   ;; If no mark is given, then we check auto-expiring.
11985   (and (not no-expire)
11986        gnus-newsgroup-auto-expire
11987        (or (not mark)
11988            (and (numberp mark)
11989                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11990                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11991                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11992        (setq mark gnus-expirable-mark))
11993   (let* ((mark (or mark gnus-del-mark))
11994          (article (or article (gnus-summary-article-number))))
11995     (or article (error "No article on current line"))
11996     (if (or (= mark gnus-unread-mark)
11997             (= mark gnus-ticked-mark)
11998             (= mark gnus-dormant-mark))
11999         (gnus-mark-article-as-unread article mark)
12000       (gnus-mark-article-as-read article mark))
12001
12002     ;; See whether the article is to be put in the cache.
12003     (and gnus-use-cache
12004          (not (= mark gnus-canceled-mark))
12005          (vectorp (gnus-summary-article-header article))
12006          (save-excursion
12007            (gnus-cache-possibly-enter-article
12008             gnus-newsgroup-name article
12009             (gnus-summary-article-header article)
12010             (= mark gnus-ticked-mark)
12011             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12012
12013     (if (gnus-summary-goto-subject article nil t)
12014         (let ((buffer-read-only nil))
12015           (gnus-summary-show-thread)
12016           ;; Fix the mark.
12017           (gnus-summary-update-mark mark 'unread)
12018           t))))
12019
12020 (defun gnus-summary-update-secondary-mark (article)
12021   "Update the secondary (read, process, cache) mark."
12022   (gnus-summary-update-mark
12023    (cond ((memq article gnus-newsgroup-processable)
12024           gnus-process-mark)
12025          ((memq article gnus-newsgroup-cached)
12026           gnus-cached-mark)
12027          ((memq article gnus-newsgroup-replied)
12028           gnus-replied-mark)
12029          ((memq article gnus-newsgroup-saved)
12030           gnus-saved-mark)
12031          (t gnus-unread-mark))
12032    'replied)
12033   (when (gnus-visual-p 'summary-highlight 'highlight)
12034     (run-hooks 'gnus-summary-update-hook))
12035   t)
12036
12037 (defun gnus-summary-update-mark (mark type)
12038   (beginning-of-line)
12039   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12040         (buffer-read-only nil))
12041     (when (and forward
12042                (<= (+ forward (point)) (point-max)))
12043       ;; Go to the right position on the line.
12044       (goto-char (+ forward (point)))
12045       ;; Replace the old mark with the new mark.
12046       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12047       ;; Optionally update the marks by some user rule.
12048       (when (eq type 'unread)
12049         (gnus-data-set-mark
12050          (gnus-data-find (gnus-summary-article-number)) mark)
12051         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12052
12053 (defun gnus-mark-article-as-read (article &optional mark)
12054   "Enter ARTICLE in the pertinent lists and remove it from others."
12055   ;; Make the article expirable.
12056   (let ((mark (or mark gnus-del-mark)))
12057     (if (= mark gnus-expirable-mark)
12058         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12059       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12060     ;; Remove from unread and marked lists.
12061     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12062     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12063     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12064     (push (cons article mark) gnus-newsgroup-reads)
12065     ;; Possibly remove from cache, if that is used.
12066     (when gnus-use-cache
12067       (gnus-cache-enter-remove-article article))))
12068
12069 (defun gnus-mark-article-as-unread (article &optional mark)
12070   "Enter ARTICLE in the pertinent lists and remove it from others."
12071   (let ((mark (or mark gnus-ticked-mark)))
12072     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12073     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12074     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12075     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12076     (cond ((= mark gnus-ticked-mark)
12077            (push article gnus-newsgroup-marked))
12078           ((= mark gnus-dormant-mark)
12079            (push article gnus-newsgroup-dormant))
12080           (t
12081            (push article gnus-newsgroup-unreads)))
12082     (setq gnus-newsgroup-reads
12083           (delq (assq article gnus-newsgroup-reads)
12084                 gnus-newsgroup-reads))))
12085
12086 (defalias 'gnus-summary-mark-as-unread-forward
12087   'gnus-summary-tick-article-forward)
12088 (make-obsolete 'gnus-summary-mark-as-unread-forward
12089                'gnus-summary-tick-article-forward)
12090 (defun gnus-summary-tick-article-forward (n)
12091   "Tick N articles forwards.
12092 If N is negative, tick backwards instead.
12093 The difference between N and the number of articles ticked is returned."
12094   (interactive "p")
12095   (gnus-summary-mark-forward n gnus-ticked-mark))
12096
12097 (defalias 'gnus-summary-mark-as-unread-backward
12098   'gnus-summary-tick-article-backward)
12099 (make-obsolete 'gnus-summary-mark-as-unread-backward
12100                'gnus-summary-tick-article-backward)
12101 (defun gnus-summary-tick-article-backward (n)
12102   "Tick N articles backwards.
12103 The difference between N and the number of articles ticked is returned."
12104   (interactive "p")
12105   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12106
12107 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12108 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12109 (defun gnus-summary-tick-article (&optional article clear-mark)
12110   "Mark current article as unread.
12111 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12112 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12113   (interactive)
12114   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12115                                        gnus-ticked-mark)))
12116
12117 (defun gnus-summary-mark-as-read-forward (n)
12118   "Mark N articles as read forwards.
12119 If N is negative, mark backwards instead.
12120 The difference between N and the actual number of articles marked is
12121 returned."
12122   (interactive "p")
12123   (gnus-summary-mark-forward n gnus-del-mark t))
12124
12125 (defun gnus-summary-mark-as-read-backward (n)
12126   "Mark the N articles as read backwards.
12127 The difference between N and the actual number of articles marked is
12128 returned."
12129   (interactive "p")
12130   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12131
12132 (defun gnus-summary-mark-as-read (&optional article mark)
12133   "Mark current article as read.
12134 ARTICLE specifies the article to be marked as read.
12135 MARK specifies a string to be inserted at the beginning of the line."
12136   (gnus-summary-mark-article article mark))
12137
12138 (defun gnus-summary-clear-mark-forward (n)
12139   "Clear marks from N articles forward.
12140 If N is negative, clear backward instead.
12141 The difference between N and the number of marks cleared is returned."
12142   (interactive "p")
12143   (gnus-summary-mark-forward n gnus-unread-mark))
12144
12145 (defun gnus-summary-clear-mark-backward (n)
12146   "Clear marks from N articles backward.
12147 The difference between N and the number of marks cleared is returned."
12148   (interactive "p")
12149   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12150
12151 (defun gnus-summary-mark-unread-as-read ()
12152   "Intended to be used by `gnus-summary-mark-article-hook'."
12153   (when (memq gnus-current-article gnus-newsgroup-unreads)
12154     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12155
12156 (defun gnus-summary-mark-read-and-unread-as-read ()
12157   "Intended to be used by `gnus-summary-mark-article-hook'."
12158   (let ((mark (gnus-summary-article-mark)))
12159     (when (or (gnus-unread-mark-p mark)
12160               (gnus-read-mark-p mark))
12161       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12162
12163 (defun gnus-summary-mark-region-as-read (point mark all)
12164   "Mark all unread articles between point and mark as read.
12165 If given a prefix, mark all articles between point and mark as read,
12166 even ticked and dormant ones."
12167   (interactive "r\nP")
12168   (save-excursion
12169     (let (article)
12170       (goto-char point)
12171       (beginning-of-line)
12172       (while (and
12173               (< (point) mark)
12174               (progn
12175                 (when (or all
12176                           (memq (setq article (gnus-summary-article-number))
12177                                 gnus-newsgroup-unreads))
12178                   (gnus-summary-mark-article article gnus-del-mark))
12179                 t)
12180               (gnus-summary-find-next))))))
12181
12182 (defun gnus-summary-mark-below (score mark)
12183   "Mark articles with score less than SCORE with MARK."
12184   (interactive "P\ncMark: ")
12185   (gnus-set-global-variables)
12186   (setq score (if score
12187                   (prefix-numeric-value score)
12188                 (or gnus-summary-default-score 0)))
12189   (save-excursion
12190     (set-buffer gnus-summary-buffer)
12191     (goto-char (point-min))
12192     (while 
12193         (progn
12194           (and (< (gnus-summary-article-score) score)
12195                (gnus-summary-mark-article nil mark))
12196           (gnus-summary-find-next)))))
12197
12198 (defun gnus-summary-kill-below (&optional score)
12199   "Mark articles with score below SCORE as read."
12200   (interactive "P")
12201   (gnus-set-global-variables)
12202   (gnus-summary-mark-below score gnus-killed-mark))
12203
12204 (defun gnus-summary-clear-above (&optional score)
12205   "Clear all marks from articles with score above SCORE."
12206   (interactive "P")
12207   (gnus-set-global-variables)
12208   (gnus-summary-mark-above score gnus-unread-mark))
12209
12210 (defun gnus-summary-tick-above (&optional score)
12211   "Tick all articles with score above SCORE."
12212   (interactive "P")
12213   (gnus-set-global-variables)
12214   (gnus-summary-mark-above score gnus-ticked-mark))
12215
12216 (defun gnus-summary-mark-above (score mark)
12217   "Mark articles with score over SCORE with MARK."
12218   (interactive "P\ncMark: ")
12219   (gnus-set-global-variables)
12220   (setq score (if score
12221                   (prefix-numeric-value score)
12222                 (or gnus-summary-default-score 0)))
12223   (save-excursion
12224     (set-buffer gnus-summary-buffer)
12225     (goto-char (point-min))
12226     (while (and (progn
12227                   (if (> (gnus-summary-article-score) score)
12228                       (gnus-summary-mark-article nil mark))
12229                   t)
12230                 (gnus-summary-find-next)))))
12231
12232 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12233 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12234 (defun gnus-summary-limit-include-expunged ()
12235   "Display all the hidden articles that were expunged for low scores."
12236   (interactive)
12237   (gnus-set-global-variables)
12238   (let ((buffer-read-only nil))
12239     (let ((scored gnus-newsgroup-scored)
12240           headers h)
12241       (while scored
12242         (or (gnus-summary-goto-subject (caar scored))
12243             (and (setq h (gnus-summary-article-header (caar scored)))
12244                  (< (cdar scored) gnus-summary-expunge-below)
12245                  (setq headers (cons h headers))))
12246         (setq scored (cdr scored)))
12247       (or headers (error "No expunged articles hidden."))
12248       (goto-char (point-min))
12249       (gnus-summary-prepare-unthreaded (nreverse headers)))
12250     (goto-char (point-min))
12251     (gnus-summary-position-point)))
12252
12253 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12254   "Mark all articles not marked as unread in this newsgroup as read.
12255 If prefix argument ALL is non-nil, all articles are marked as read.
12256 If QUIETLY is non-nil, no questions will be asked.
12257 If TO-HERE is non-nil, it should be a point in the buffer.  All
12258 articles before this point will be marked as read.
12259 The number of articles marked as read is returned."
12260   (interactive "P")
12261   (gnus-set-global-variables)
12262   (prog1
12263       (if (or quietly
12264               (not gnus-interactive-catchup) ;Without confirmation?
12265               gnus-expert-user
12266               (gnus-y-or-n-p
12267                (if all
12268                    "Mark absolutely all articles as read? "
12269                  "Mark all unread articles as read? ")))
12270           (if (and not-mark
12271                    (not gnus-newsgroup-adaptive)
12272                    (not gnus-newsgroup-auto-expire))
12273               (progn
12274                 (when all
12275                   (setq gnus-newsgroup-marked nil
12276                         gnus-newsgroup-dormant nil))
12277                 (setq gnus-newsgroup-unreads nil))
12278             ;; We actually mark all articles as canceled, which we
12279             ;; have to do when using auto-expiry or adaptive scoring.
12280             (gnus-summary-show-all-threads)
12281             (if (gnus-summary-first-subject (not all))
12282                 (while (and
12283                         (if to-here (< (point) to-here) t)
12284                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12285                         (gnus-summary-find-next (not all)))))
12286             (unless to-here
12287               (setq gnus-newsgroup-unreads nil))
12288             (gnus-set-mode-line 'summary)))
12289     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12290       (if (and (not to-here) (eq 'nnvirtual (car method)))
12291           (nnvirtual-catchup-group
12292            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12293     (gnus-summary-position-point)))
12294
12295 (defun gnus-summary-catchup-to-here (&optional all)
12296   "Mark all unticked articles before the current one as read.
12297 If ALL is non-nil, also mark ticked and dormant articles as read."
12298   (interactive "P")
12299   (gnus-set-global-variables)
12300   (save-excursion
12301     (let ((beg (point)))
12302       ;; We check that there are unread articles.
12303       (when (or all (gnus-summary-find-prev))
12304         (gnus-summary-catchup all t beg))))
12305   (gnus-summary-position-point))
12306
12307 (defun gnus-summary-catchup-all (&optional quietly)
12308   "Mark all articles in this newsgroup as read."
12309   (interactive "P")
12310   (gnus-set-global-variables)
12311   (gnus-summary-catchup t quietly))
12312
12313 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12314   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12315 If prefix argument ALL is non-nil, all articles are marked as read."
12316   (interactive "P")
12317   (gnus-set-global-variables)
12318   (gnus-summary-catchup all quietly nil 'fast)
12319   ;; Select next newsgroup or exit.
12320   (if (eq gnus-auto-select-next 'quietly)
12321       (gnus-summary-next-group nil)
12322     (gnus-summary-exit)))
12323
12324 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12325   "Mark all articles in this newsgroup as read, and then exit."
12326   (interactive "P")
12327   (gnus-set-global-variables)
12328   (gnus-summary-catchup-and-exit t quietly))
12329
12330 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12331 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12332   "Mark all articles in this group as read and select the next group.
12333 If given a prefix, mark all articles, unread as well as ticked, as
12334 read."
12335   (interactive "P")
12336   (gnus-set-global-variables)
12337   (save-excursion
12338     (gnus-summary-catchup all))
12339   (gnus-summary-next-article t nil nil t))
12340
12341 ;; Thread-based commands.
12342
12343 (defun gnus-summary-articles-in-thread (&optional article)
12344   "Return a list of all articles in the current thread.
12345 If ARTICLE is non-nil, return all articles in the thread that starts
12346 with that article."
12347   (let* ((article (or article (gnus-summary-article-number)))
12348          (data (gnus-data-find-list article))
12349          (top-level (gnus-data-level (car data)))
12350          (top-subject
12351           (cond ((null gnus-thread-operation-ignore-subject)
12352                  (gnus-simplify-subject-re
12353                   (mail-header-subject (gnus-data-header (car data)))))
12354                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12355                  (gnus-simplify-subject-fuzzy
12356                   (mail-header-subject (gnus-data-header (car data)))))
12357                 (t nil)))
12358          (end-point (save-excursion
12359                       (gnus-summary-go-to-next-thread) (point)))
12360          articles)
12361     (while (and data
12362                 (< (gnus-data-pos (car data)) end-point))
12363       (and (or (not top-subject)
12364                (string= top-subject
12365                         (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12366                             (gnus-simplify-subject-fuzzy
12367                              (mail-header-subject
12368                               (gnus-data-header (car data))))
12369                           (gnus-simplify-subject-re
12370                            (mail-header-subject
12371                             (gnus-data-header (car data)))))))
12372            (setq articles (cons (gnus-data-number (car data)) articles)))
12373       (unless (and (setq data (cdr data))
12374                    (> (gnus-data-level (car data)) top-level))
12375         (setq data nil)))
12376     ;; Return the list of articles.
12377     (nreverse articles)))
12378
12379 (defun gnus-summary-rethread-current ()
12380   "Rethread the thread the current article is part of."
12381   (interactive)
12382   (gnus-set-global-variables)
12383   (let* ((gnus-show-threads t)
12384          (article (gnus-summary-article-number))
12385          (id (mail-header-id (gnus-summary-article-header)))
12386          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12387     (unless id
12388       (error "No article on the current line"))
12389     (gnus-rebuild-thread id)
12390     (gnus-summary-goto-subject article)))
12391
12392 (defun gnus-summary-reparent-thread ()
12393   "Make current article child of the marked (or previous) article.
12394
12395 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12396 is non-nil or the Subject: of both articles are the same."
12397   (interactive)
12398   (or (not (gnus-group-read-only-p))
12399       (error "The current newsgroup does not support article editing."))
12400   (or (<= (length gnus-newsgroup-processable) 1)
12401       (error "No more than one article may be marked."))
12402   (save-window-excursion
12403     (let ((gnus-article-buffer " *reparent*")
12404           (current-article (gnus-summary-article-number))
12405           ; first grab the marked article, otherwise one line up.
12406           (parent-article (if (not (null gnus-newsgroup-processable))
12407                               (car gnus-newsgroup-processable)
12408                             (save-excursion
12409                               (if (eq (forward-line -1) 0)
12410                                   (gnus-summary-article-number)
12411                                 (error "Beginning of summary buffer."))))))
12412       (or (not (eq current-article parent-article))
12413           (error "An article may not be self-referential."))
12414       (let ((message-id (mail-header-id 
12415                          (gnus-summary-article-header parent-article))))
12416         (or (and message-id (not (equal message-id "")))
12417             (error "No message-id in desired parent."))
12418         (gnus-summary-select-article t t nil current-article)
12419         (set-buffer gnus-article-buffer)
12420         (setq buffer-read-only nil)
12421         (let ((buf (format "%s" (buffer-string))))
12422           (erase-buffer)
12423           (insert buf))
12424         (goto-char (point-min))
12425         (if (search-forward-regexp "^References: " nil t)
12426             (insert message-id " " )
12427           (insert "References: " message-id "\n"))
12428         (or (gnus-request-replace-article current-article
12429                                           (car gnus-article-current)
12430                                           gnus-article-buffer)
12431             (error "Couldn't replace article."))
12432         (set-buffer gnus-summary-buffer)
12433         (gnus-summary-unmark-all-processable)
12434         (gnus-summary-rethread-current)
12435         (gnus-message 3 "Article %d is now the child of article %d."
12436                       current-article parent-article)))))
12437
12438 (defun gnus-summary-toggle-threads (&optional arg)
12439   "Toggle showing conversation threads.
12440 If ARG is positive number, turn showing conversation threads on."
12441   (interactive "P")
12442   (gnus-set-global-variables)
12443   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12444     (setq gnus-show-threads
12445           (if (null arg) (not gnus-show-threads)
12446             (> (prefix-numeric-value arg) 0)))
12447     (gnus-summary-prepare)
12448     (gnus-summary-goto-subject current)
12449     (gnus-summary-position-point)))
12450
12451 (defun gnus-summary-show-all-threads ()
12452   "Show all threads."
12453   (interactive)
12454   (gnus-set-global-variables)
12455   (save-excursion
12456     (let ((buffer-read-only nil))
12457       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12458   (gnus-summary-position-point))
12459
12460 (defun gnus-summary-show-thread ()
12461   "Show thread subtrees.
12462 Returns nil if no thread was there to be shown."
12463   (interactive)
12464   (gnus-set-global-variables)
12465   (let ((buffer-read-only nil)
12466         (orig (point))
12467         ;; first goto end then to beg, to have point at beg after let
12468         (end (progn (end-of-line) (point)))
12469         (beg (progn (beginning-of-line) (point))))
12470     (prog1
12471         ;; Any hidden lines here?
12472         (search-forward "\r" end t)
12473       (subst-char-in-region beg end ?\^M ?\n t)
12474       (goto-char orig)
12475       (gnus-summary-position-point))))
12476
12477 (defun gnus-summary-hide-all-threads ()
12478   "Hide all thread subtrees."
12479   (interactive)
12480   (gnus-set-global-variables)
12481   (save-excursion
12482     (goto-char (point-min))
12483     (gnus-summary-hide-thread)
12484     (while (zerop (gnus-summary-next-thread 1 t))
12485       (gnus-summary-hide-thread)))
12486   (gnus-summary-position-point))
12487
12488 (defun gnus-summary-hide-thread ()
12489   "Hide thread subtrees.
12490 Returns nil if no threads were there to be hidden."
12491   (interactive)
12492   (gnus-set-global-variables)
12493   (let ((buffer-read-only nil)
12494         (start (point))
12495         (article (gnus-summary-article-number)))
12496     (goto-char start)
12497     ;; Go forward until either the buffer ends or the subthread
12498     ;; ends.
12499     (when (and (not (eobp))
12500                (or (zerop (gnus-summary-next-thread 1 t))
12501                    (goto-char (point-max))))
12502       (prog1
12503           (if (and (> (point) start)
12504                    (search-backward "\n" start t))
12505               (progn
12506                 (subst-char-in-region start (point) ?\n ?\^M)
12507                 (gnus-summary-goto-subject article))
12508             (goto-char start)
12509             nil)
12510         ;;(gnus-summary-position-point)
12511         ))))
12512
12513 (defun gnus-summary-go-to-next-thread (&optional previous)
12514   "Go to the same level (or less) next thread.
12515 If PREVIOUS is non-nil, go to previous thread instead.
12516 Return the article number moved to, or nil if moving was impossible."
12517   (let ((level (gnus-summary-thread-level))
12518         (way (if previous -1 1))
12519         (beg (point)))
12520     (forward-line way)
12521     (while (and (not (eobp))
12522                 (< level (gnus-summary-thread-level)))
12523       (forward-line way))
12524     (if (eobp)
12525         (progn
12526           (goto-char beg)
12527           nil)
12528       (setq beg (point))
12529       (prog1
12530           (gnus-summary-article-number)
12531         (goto-char beg)))))
12532
12533 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12534   "Go to the same level (or less) next thread.
12535 If PREVIOUS is non-nil, go to previous thread instead.
12536 Return the article number moved to, or nil if moving was impossible."
12537   (if (and (eq gnus-summary-make-false-root 'dummy)
12538            (gnus-summary-article-intangible-p))
12539       (let ((beg (point)))
12540         (while (and (zerop (forward-line 1))
12541                     (not (gnus-summary-article-intangible-p))
12542                     (not (zerop (save-excursion 
12543                                   (gnus-summary-thread-level))))))
12544         (if (eobp)
12545             (progn
12546               (goto-char beg)
12547               nil)
12548           (point)))
12549     (let* ((level (gnus-summary-thread-level))
12550            (article (gnus-summary-article-number))
12551            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12552            oart)
12553       (while data
12554         (if (<= (gnus-data-level (car data)) level)
12555             (setq oart (gnus-data-number (car data))
12556                   data nil)
12557           (setq data (cdr data))))
12558       (and oart
12559            (gnus-summary-goto-subject oart)))))
12560
12561 (defun gnus-summary-next-thread (n &optional silent)
12562   "Go to the same level next N'th thread.
12563 If N is negative, search backward instead.
12564 Returns the difference between N and the number of skips actually
12565 done.
12566
12567 If SILENT, don't output messages."
12568   (interactive "p")
12569   (gnus-set-global-variables)
12570   (let ((backward (< n 0))
12571         (n (abs n))
12572         old dum int)
12573     (while (and (> n 0)
12574                 (gnus-summary-go-to-next-thread backward))
12575       (decf n))
12576     (unless silent 
12577       (gnus-summary-position-point))
12578     (when (and (not silent) (/= 0 n))
12579       (gnus-message 7 "No more threads"))
12580     n))
12581
12582 (defun gnus-summary-prev-thread (n)
12583   "Go to the same level previous N'th thread.
12584 Returns the difference between N and the number of skips actually
12585 done."
12586   (interactive "p")
12587   (gnus-set-global-variables)
12588   (gnus-summary-next-thread (- n)))
12589
12590 (defun gnus-summary-go-down-thread ()
12591   "Go down one level in the current thread."
12592   (let ((children (gnus-summary-article-children)))
12593     (and children
12594          (gnus-summary-goto-subject (car children)))))
12595
12596 (defun gnus-summary-go-up-thread ()
12597   "Go up one level in the current thread."
12598   (let ((parent (gnus-summary-article-parent)))
12599     (and parent
12600          (gnus-summary-goto-subject parent))))
12601
12602 (defun gnus-summary-down-thread (n)
12603   "Go down thread N steps.
12604 If N is negative, go up instead.
12605 Returns the difference between N and how many steps down that were
12606 taken."
12607   (interactive "p")
12608   (gnus-set-global-variables)
12609   (let ((up (< n 0))
12610         (n (abs n)))
12611     (while (and (> n 0)
12612                 (if up (gnus-summary-go-up-thread)
12613                   (gnus-summary-go-down-thread)))
12614       (setq n (1- n)))
12615     (gnus-summary-position-point)
12616     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12617     n))
12618
12619 (defun gnus-summary-up-thread (n)
12620   "Go up thread N steps.
12621 If N is negative, go up instead.
12622 Returns the difference between N and how many steps down that were
12623 taken."
12624   (interactive "p")
12625   (gnus-set-global-variables)
12626   (gnus-summary-down-thread (- n)))
12627
12628 (defun gnus-summary-top-thread ()
12629   "Go to the top of the thread."
12630   (interactive)
12631   (gnus-set-global-variables)
12632   (while (gnus-summary-go-up-thread))
12633   (gnus-summary-article-number))
12634
12635 (defun gnus-summary-kill-thread (&optional unmark)
12636   "Mark articles under current thread as read.
12637 If the prefix argument is positive, remove any kinds of marks.
12638 If the prefix argument is negative, tick articles instead."
12639   (interactive "P")
12640   (gnus-set-global-variables)
12641   (if unmark
12642       (setq unmark (prefix-numeric-value unmark)))
12643   (let ((articles (gnus-summary-articles-in-thread)))
12644     (save-excursion
12645       ;; Expand the thread.
12646       (gnus-summary-show-thread)
12647       ;; Mark all the articles.
12648       (while articles
12649         (gnus-summary-goto-subject (car articles))
12650         (cond ((null unmark)
12651                (gnus-summary-mark-article-as-read gnus-killed-mark))
12652               ((> unmark 0)
12653                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12654               (t
12655                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12656         (setq articles (cdr articles))))
12657     ;; Hide killed subtrees.
12658     (and (null unmark)
12659          gnus-thread-hide-killed
12660          (gnus-summary-hide-thread))
12661     ;; If marked as read, go to next unread subject.
12662     (if (null unmark)
12663         ;; Go to next unread subject.
12664         (gnus-summary-next-subject 1 t)))
12665   (gnus-set-mode-line 'summary))
12666
12667 ;; Summary sorting commands
12668
12669 (defun gnus-summary-sort-by-number (&optional reverse)
12670   "Sort summary buffer by article number.
12671 Argument REVERSE means reverse order."
12672   (interactive "P")
12673   (gnus-summary-sort 'number reverse))
12674
12675 (defun gnus-summary-sort-by-author (&optional reverse)
12676   "Sort summary buffer by author name alphabetically.
12677 If case-fold-search is non-nil, case of letters is ignored.
12678 Argument REVERSE means reverse order."
12679   (interactive "P")
12680   (gnus-summary-sort 'author reverse))
12681
12682 (defun gnus-summary-sort-by-subject (&optional reverse)
12683   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12684 If case-fold-search is non-nil, case of letters is ignored.
12685 Argument REVERSE means reverse order."
12686   (interactive "P")
12687   (gnus-summary-sort 'subject reverse))
12688
12689 (defun gnus-summary-sort-by-date (&optional reverse)
12690   "Sort summary buffer by date.
12691 Argument REVERSE means reverse order."
12692   (interactive "P")
12693   (gnus-summary-sort 'date reverse))
12694
12695 (defun gnus-summary-sort-by-score (&optional reverse)
12696   "Sort summary buffer by score.
12697 Argument REVERSE means reverse order."
12698   (interactive "P")
12699   (gnus-summary-sort 'score reverse))
12700
12701 (defun gnus-summary-sort (predicate reverse)
12702   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12703   (gnus-set-global-variables)
12704   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12705          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12706          (gnus-thread-sort-functions
12707           (list
12708            (if (not reverse)
12709                thread
12710              `(lambda (t1 t2)
12711                 (,thread t2 t1)))))
12712          (gnus-article-sort-functions
12713           (list
12714            (if (not reverse)
12715                article
12716              `(lambda (t1 t2)
12717                 (,article t2 t1)))))
12718          (buffer-read-only)
12719          (gnus-summary-prepare-hook nil))
12720     ;; We do the sorting by regenerating the threads.
12721     (gnus-summary-prepare)
12722     ;; Hide subthreads if needed.
12723     (when (and gnus-show-threads gnus-thread-hide-subtree)
12724       (gnus-summary-hide-all-threads)))
12725   ;; If in async mode, we send some info to the backend.
12726   (when gnus-newsgroup-async
12727     (gnus-request-asynchronous
12728      gnus-newsgroup-name gnus-newsgroup-data)))
12729
12730 (defun gnus-sortable-date (date)
12731   "Make sortable string by string-lessp from DATE.
12732 Timezone package is used."
12733   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12734          (year (aref date 0))
12735          (month (aref date 1))
12736          (day (aref date 2)))
12737     (timezone-make-sortable-date
12738      year month day
12739      (timezone-make-time-string
12740       (aref date 3) (aref date 4) (aref date 5)))))
12741
12742 ;; Summary saving commands.
12743
12744 (defun gnus-summary-save-article (&optional n not-saved)
12745   "Save the current article using the default saver function.
12746 If N is a positive number, save the N next articles.
12747 If N is a negative number, save the N previous articles.
12748 If N is nil and any articles have been marked with the process mark,
12749 save those articles instead.
12750 The variable `gnus-default-article-saver' specifies the saver function."
12751   (interactive "P")
12752   (gnus-set-global-variables)
12753   (let ((articles (gnus-summary-work-articles n))
12754         file header article)
12755     (while articles
12756       (setq header (gnus-summary-article-header
12757                     (setq article (pop articles))))
12758       (if (not (vectorp header))
12759           ;; This is a pseudo-article.
12760           (if (assq 'name header)
12761               (gnus-copy-file (cdr (assq 'name header)))
12762             (gnus-message 1 "Article %d is unsaveable" article))
12763         ;; This is a real article.
12764         (save-window-excursion
12765           (gnus-summary-select-article t nil nil article))
12766         (unless gnus-save-all-headers
12767           ;; Remove headers accoring to `gnus-saved-headers'.
12768           (let ((gnus-visible-headers
12769                  (or gnus-saved-headers gnus-visible-headers)))
12770             (gnus-article-hide-headers nil t)))
12771         ;; Remove any X-Gnus lines.
12772         (save-excursion
12773           (set-buffer gnus-article-buffer)
12774           (save-restriction
12775             (let ((buffer-read-only nil))
12776               (nnheader-narrow-to-headers)
12777               (while (re-search-forward "^X-Gnus" nil t)
12778                 (gnus-delete-line)))))
12779         (save-window-excursion
12780           (if (not gnus-default-article-saver)
12781               (error "No default saver is defined.")
12782             (setq file (funcall
12783                         gnus-default-article-saver
12784                         (cond
12785                          ((not gnus-prompt-before-saving)
12786                           'default)
12787                          ((eq gnus-prompt-before-saving 'always)
12788                           nil)
12789                          (t file))))))
12790         (gnus-summary-remove-process-mark article)
12791         (unless not-saved
12792           (gnus-summary-set-saved-mark article))))
12793     (gnus-summary-position-point)
12794     n))
12795
12796 (defun gnus-summary-pipe-output (&optional arg)
12797   "Pipe the current article to a subprocess.
12798 If N is a positive number, pipe the N next articles.
12799 If N is a negative number, pipe the N previous articles.
12800 If N is nil and any articles have been marked with the process mark,
12801 pipe those articles instead."
12802   (interactive "P")
12803   (gnus-set-global-variables)
12804   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12805     (gnus-summary-save-article arg t))
12806   (gnus-configure-windows 'pipe))
12807
12808 (defun gnus-summary-save-article-mail (&optional arg)
12809   "Append the current article to an mail file.
12810 If N is a positive number, save the N next articles.
12811 If N is a negative number, save the N previous articles.
12812 If N is nil and any articles have been marked with the process mark,
12813 save those articles instead."
12814   (interactive "P")
12815   (gnus-set-global-variables)
12816   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12817     (gnus-summary-save-article arg)))
12818
12819 (defun gnus-summary-save-article-rmail (&optional arg)
12820   "Append the current article to an rmail file.
12821 If N is a positive number, save the N next articles.
12822 If N is a negative number, save the N previous articles.
12823 If N is nil and any articles have been marked with the process mark,
12824 save those articles instead."
12825   (interactive "P")
12826   (gnus-set-global-variables)
12827   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12828     (gnus-summary-save-article arg)))
12829
12830 (defun gnus-summary-save-article-file (&optional arg)
12831   "Append the current article to a file.
12832 If N is a positive number, save the N next articles.
12833 If N is a negative number, save the N previous articles.
12834 If N is nil and any articles have been marked with the process mark,
12835 save those articles instead."
12836   (interactive "P")
12837   (gnus-set-global-variables)
12838   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12839     (gnus-summary-save-article arg)))
12840
12841 (defun gnus-summary-save-article-body-file (&optional arg)
12842   "Append the current article body to a file.
12843 If N is a positive number, save the N next articles.
12844 If N is a negative number, save the N previous articles.
12845 If N is nil and any articles have been marked with the process mark,
12846 save those articles instead."
12847   (interactive "P")
12848   (gnus-set-global-variables)
12849   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12850     (gnus-summary-save-article arg)))
12851
12852 (defun gnus-get-split-value (methods)
12853   "Return a value based on the split METHODS."
12854   (let (split-name method result match)
12855     (when methods
12856       (save-excursion
12857         (set-buffer gnus-original-article-buffer)
12858         (save-restriction
12859           (nnheader-narrow-to-headers)
12860           (while methods
12861             (goto-char (point-min))
12862             (setq method (pop methods))
12863             (setq match (car method))
12864             (when (cond
12865                    ((stringp match)
12866                     ;; Regular expression.
12867                     (condition-case ()
12868                         (re-search-forward match nil t)
12869                       (error nil)))
12870                    ((gnus-functionp match)
12871                     ;; Function.
12872                     (save-restriction
12873                       (widen)
12874                       (setq result (funcall match gnus-newsgroup-name))))
12875                    ((consp match)
12876                     ;; Form.
12877                     (save-restriction
12878                       (widen)
12879                       (setq result (eval match)))))
12880               (setq split-name (append (cdr method) split-name))
12881               (cond ((stringp result)
12882                      (push result split-name))
12883                     ((consp result)
12884                      (setq split-name (append result split-name)))))))))
12885     split-name))
12886
12887 (defun gnus-read-move-group-name (prompt default articles prefix)
12888   "Read a group name."
12889   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12890          (prom
12891           (format "Where do you want to %s %s?"
12892                   prompt
12893                   (if (> (length articles) 1)
12894                       (format "these %d articles" (length articles))
12895                     "this article")))
12896          (to-newsgroup
12897           (cond
12898            ((null split-name)
12899             (gnus-completing-read default prom
12900                                   gnus-active-hashtb nil nil prefix
12901                                   'gnus-group-history))
12902            ((= 1 (length split-name))
12903             (gnus-completing-read (car split-name) prom gnus-active-hashtb
12904                                   nil nil nil
12905                                   'gnus-group-history))
12906            (t
12907             (gnus-completing-read nil prom 
12908                                   (mapcar (lambda (el) (list el))
12909                                           (nreverse split-name))
12910                                   nil nil nil
12911                                   'gnus-group-history)))))
12912     (when to-newsgroup
12913       (if (or (string= to-newsgroup "")
12914               (string= to-newsgroup prefix))
12915           (setq to-newsgroup (or default "")))
12916       (or (gnus-active to-newsgroup)
12917           (gnus-activate-group to-newsgroup)
12918           (error "No such group: %s" to-newsgroup)))
12919     to-newsgroup))
12920
12921 (defun gnus-read-save-file-name (prompt default-name)
12922   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12923          (file
12924           ;; Let the split methods have their say.
12925           (cond
12926            ;; No split name was found.
12927            ((null split-name)
12928             (read-file-name
12929              (concat prompt " (default "
12930                      (file-name-nondirectory default-name) ") ")
12931              (file-name-directory default-name)
12932              default-name))
12933            ;; A single split name was found
12934            ((= 1 (length split-name))
12935             (read-file-name
12936              (concat prompt " (default " (car split-name) ") ")
12937              gnus-article-save-directory
12938              (concat gnus-article-save-directory (car split-name))))
12939            ;; A list of splits was found.
12940            (t
12941             (setq split-name (mapcar (lambda (el) (list el))
12942                                      (nreverse split-name)))
12943             (let ((result (completing-read
12944                            (concat prompt " ") split-name nil nil)))
12945               (concat gnus-article-save-directory
12946                       (if (string= result "")
12947                           (caar split-name)
12948                         result)))))))
12949     ;; If we have read a directory, we append the default file name.
12950     (when (file-directory-p file)
12951       (setq file (concat (file-name-as-directory file)
12952                          (file-name-nondirectory default-name))))
12953     ;; Possibly translate some charaters.
12954     (nnheader-translate-file-chars file)))
12955
12956 (defun gnus-article-archive-name (group)
12957   "Return the first instance of an \"Archive-name\" in the current buffer."
12958   (let ((case-fold-search t))
12959     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12960       (match-string 1))))
12961
12962 (defun gnus-summary-save-in-rmail (&optional filename)
12963   "Append this article to Rmail file.
12964 Optional argument FILENAME specifies file name.
12965 Directory to save to is default to `gnus-article-save-directory' which
12966 is initialized from the SAVEDIR environment variable."
12967   (interactive)
12968   (gnus-set-global-variables)
12969   (let ((default-name
12970           (funcall gnus-rmail-save-name gnus-newsgroup-name
12971                    gnus-current-headers gnus-newsgroup-last-rmail)))
12972     (setq filename
12973           (cond ((eq filename 'default)
12974                  default-name)
12975                 (filename filename)
12976                 (t (gnus-read-save-file-name
12977                     "Save in rmail file:" default-name))))
12978     (gnus-make-directory (file-name-directory filename))
12979     (gnus-eval-in-buffer-window
12980      gnus-original-article-buffer
12981      (save-excursion
12982        (save-restriction
12983          (widen)
12984          (gnus-output-to-rmail filename))))
12985     ;; Remember the directory name to save articles
12986     (setq gnus-newsgroup-last-rmail filename)))
12987
12988 (defun gnus-summary-save-in-mail (&optional filename)
12989   "Append this article to Unix mail file.
12990 Optional argument FILENAME specifies file name.
12991 Directory to save to is default to `gnus-article-save-directory' which
12992 is initialized from the SAVEDIR environment variable."
12993   (interactive)
12994   (gnus-set-global-variables)
12995   (let ((default-name
12996           (funcall gnus-mail-save-name gnus-newsgroup-name
12997                    gnus-current-headers gnus-newsgroup-last-mail)))
12998     (setq filename
12999           (cond ((eq filename 'default)
13000                  default-name)
13001                 (filename filename)
13002                 (t (gnus-read-save-file-name
13003                     "Save in Unix mail file:" default-name))))
13004     (setq filename
13005           (expand-file-name filename
13006                             (and default-name
13007                                  (file-name-directory default-name))))
13008     (gnus-make-directory (file-name-directory filename))
13009     (gnus-eval-in-buffer-window
13010      gnus-original-article-buffer
13011      (save-excursion
13012        (save-restriction
13013          (widen)
13014          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13015              (gnus-output-to-rmail filename)
13016            (let ((mail-use-rfc822 t))
13017              (rmail-output filename 1 t t))))))
13018     ;; Remember the directory name to save articles.
13019     (setq gnus-newsgroup-last-mail filename)))
13020
13021 (defun gnus-summary-save-in-file (&optional filename)
13022   "Append this article to file.
13023 Optional argument FILENAME specifies file name.
13024 Directory to save to is default to `gnus-article-save-directory' which
13025 is initialized from the SAVEDIR environment variable."
13026   (interactive)
13027   (gnus-set-global-variables)
13028   (let ((default-name
13029           (funcall gnus-file-save-name gnus-newsgroup-name
13030                    gnus-current-headers gnus-newsgroup-last-file)))
13031     (setq filename
13032           (cond ((eq filename 'default)
13033                  default-name)
13034                 (filename filename)
13035                 (t (gnus-read-save-file-name
13036                     "Save in file:" default-name))))
13037     (gnus-make-directory (file-name-directory filename))
13038     (gnus-eval-in-buffer-window
13039      gnus-original-article-buffer
13040      (save-excursion
13041        (save-restriction
13042          (widen)
13043          (gnus-output-to-file filename))))
13044     ;; Remember the directory name to save articles.
13045     (setq gnus-newsgroup-last-file filename)))
13046
13047 (defun gnus-summary-save-body-in-file (&optional filename)
13048   "Append this article body to a file.
13049 Optional argument FILENAME specifies file name.
13050 The directory to save in defaults to `gnus-article-save-directory' which
13051 is initialized from the SAVEDIR environment variable."
13052   (interactive)
13053   (gnus-set-global-variables)
13054   (let ((default-name
13055           (funcall gnus-file-save-name gnus-newsgroup-name
13056                    gnus-current-headers gnus-newsgroup-last-file)))
13057     (setq filename
13058           (cond ((eq filename 'default)
13059                  default-name)
13060                 (filename filename)
13061                 (t (gnus-read-save-file-name
13062                     "Save body in file:" default-name))))
13063     (gnus-make-directory (file-name-directory filename))
13064     (gnus-eval-in-buffer-window
13065      gnus-article-buffer
13066      (save-excursion
13067        (save-restriction
13068          (widen)
13069          (goto-char (point-min))
13070          (and (search-forward "\n\n" nil t)
13071               (narrow-to-region (point) (point-max)))
13072          (gnus-output-to-file filename))))
13073     ;; Remember the directory name to save articles.
13074     (setq gnus-newsgroup-last-file filename)))
13075
13076 (defun gnus-summary-save-in-pipe (&optional command)
13077   "Pipe this article to subprocess."
13078   (interactive)
13079   (gnus-set-global-variables)
13080   (setq command
13081         (cond ((eq command 'default)
13082                gnus-last-shell-command)
13083               (command command)
13084               (t (read-string "Shell command on article: "
13085                               gnus-last-shell-command))))
13086   (if (string-equal command "")
13087       (setq command gnus-last-shell-command))
13088   (gnus-eval-in-buffer-window
13089    gnus-article-buffer
13090    (save-restriction
13091      (widen)
13092      (shell-command-on-region (point-min) (point-max) command nil)))
13093   (setq gnus-last-shell-command command))
13094
13095 ;; Summary extract commands
13096
13097 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13098   (let ((buffer-read-only nil)
13099         (article (gnus-summary-article-number))
13100         after-article b e)
13101     (or (gnus-summary-goto-subject article)
13102         (error (format "No such article: %d" article)))
13103     (gnus-summary-position-point)
13104     ;; If all commands are to be bunched up on one line, we collect
13105     ;; them here.
13106     (if gnus-view-pseudos-separately
13107         ()
13108       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13109             files action)
13110         (while ps
13111           (setq action (cdr (assq 'action (car ps))))
13112           (setq files (list (cdr (assq 'name (car ps)))))
13113           (while (and ps (cdr ps)
13114                       (string= (or action "1")
13115                                (or (cdr (assq 'action (cadr ps))) "2")))
13116             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13117             (setcdr ps (cddr ps)))
13118           (if (not files)
13119               ()
13120             (if (not (string-match "%s" action))
13121                 (setq files (cons " " files)))
13122             (setq files (cons " " files))
13123             (and (assq 'execute (car ps))
13124                  (setcdr (assq 'execute (car ps))
13125                          (funcall (if (string-match "%s" action)
13126                                       'format 'concat)
13127                                   action
13128                                   (mapconcat (lambda (f) f) files " ")))))
13129           (setq ps (cdr ps)))))
13130     (if (and gnus-view-pseudos (not not-view))
13131         (while pslist
13132           (and (assq 'execute (car pslist))
13133                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13134                                      (eq gnus-view-pseudos 'not-confirm)))
13135           (setq pslist (cdr pslist)))
13136       (save-excursion
13137         (while pslist
13138           (setq after-article (or (cdr (assq 'article (car pslist)))
13139                                   (gnus-summary-article-number)))
13140           (gnus-summary-goto-subject after-article)
13141           (forward-line 1)
13142           (setq b (point))
13143           (insert "    " (file-name-nondirectory
13144                                 (cdr (assq 'name (car pslist))))
13145                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13146           (setq e (point))
13147           (forward-line -1)             ; back to `b'
13148           (add-text-properties
13149            b e (list 'gnus-number gnus-reffed-article-number
13150                      gnus-mouse-face-prop gnus-mouse-face))
13151           (gnus-data-enter
13152            after-article gnus-reffed-article-number
13153            gnus-unread-mark b (car pslist) 0 (- e b))
13154           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13155           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13156           (setq pslist (cdr pslist)))))))
13157
13158 (defun gnus-pseudos< (p1 p2)
13159   (let ((c1 (cdr (assq 'action p1)))
13160         (c2 (cdr (assq 'action p2))))
13161     (and c1 c2 (string< c1 c2))))
13162
13163 (defun gnus-request-pseudo-article (props)
13164   (cond ((assq 'execute props)
13165          (gnus-execute-command (cdr (assq 'execute props)))))
13166   (let ((gnus-current-article (gnus-summary-article-number)))
13167     (run-hooks 'gnus-mark-article-hook)))
13168
13169 (defun gnus-execute-command (command &optional automatic)
13170   (save-excursion
13171     (gnus-article-setup-buffer)
13172     (set-buffer gnus-article-buffer)
13173     (let ((command (if automatic command (read-string "Command: " command)))
13174           (buffer-read-only nil))
13175       (erase-buffer)
13176       (insert "$ " command "\n\n")
13177       (if gnus-view-pseudo-asynchronously
13178           (start-process "gnus-execute" nil "sh" "-c" command)
13179         (call-process "sh" nil t nil "-c" command)))))
13180
13181 (defun gnus-copy-file (file &optional to)
13182   "Copy FILE to TO."
13183   (interactive
13184    (list (read-file-name "Copy file: " default-directory)
13185          (read-file-name "Copy file to: " default-directory)))
13186   (gnus-set-global-variables)
13187   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13188   (and (file-directory-p to)
13189        (setq to (concat (file-name-as-directory to)
13190                         (file-name-nondirectory file))))
13191   (copy-file file to))
13192
13193 ;; Summary kill commands.
13194
13195 (defun gnus-summary-edit-global-kill (article)
13196   "Edit the \"global\" kill file."
13197   (interactive (list (gnus-summary-article-number)))
13198   (gnus-set-global-variables)
13199   (gnus-group-edit-global-kill article))
13200
13201 (defun gnus-summary-edit-local-kill ()
13202   "Edit a local kill file applied to the current newsgroup."
13203   (interactive)
13204   (gnus-set-global-variables)
13205   (setq gnus-current-headers (gnus-summary-article-header))
13206   (gnus-set-global-variables)
13207   (gnus-group-edit-local-kill
13208    (gnus-summary-article-number) gnus-newsgroup-name))
13209
13210 \f
13211 ;;;
13212 ;;; Gnus article mode
13213 ;;;
13214
13215 (put 'gnus-article-mode 'mode-class 'special)
13216
13217 (if gnus-article-mode-map
13218     nil
13219   (setq gnus-article-mode-map (make-keymap))
13220   (suppress-keymap gnus-article-mode-map)
13221
13222   (gnus-define-keys gnus-article-mode-map
13223     " " gnus-article-goto-next-page
13224     "\177" gnus-article-goto-prev-page
13225     [delete] gnus-article-goto-prev-page
13226     "\C-c^" gnus-article-refer-article
13227     "h" gnus-article-show-summary
13228     "s" gnus-article-show-summary
13229     "\C-c\C-m" gnus-article-mail
13230     "?" gnus-article-describe-briefly
13231     gnus-mouse-2 gnus-article-push-button
13232     "\r" gnus-article-press-button
13233     "\t" gnus-article-next-button
13234     "\M-\t" gnus-article-prev-button
13235     "\C-c\C-b" gnus-bug)
13236
13237   (substitute-key-definition
13238    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13239
13240 (defun gnus-article-mode ()
13241   "Major mode for displaying an article.
13242
13243 All normal editing commands are switched off.
13244
13245 The following commands are available:
13246
13247 \\<gnus-article-mode-map>
13248 \\[gnus-article-next-page]\t Scroll the article one page forwards
13249 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13250 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13251 \\[gnus-article-show-summary]\t Display the summary buffer
13252 \\[gnus-article-mail]\t Send a reply to the address near point
13253 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13254 \\[gnus-info-find-node]\t Go to the Gnus info node"
13255   (interactive)
13256   (when (and menu-bar-mode
13257              (gnus-visual-p 'article-menu 'menu))
13258     (gnus-article-make-menu-bar))
13259   (kill-all-local-variables)
13260   (gnus-simplify-mode-line)
13261   (setq mode-name "Article")
13262   (setq major-mode 'gnus-article-mode)
13263   (make-local-variable 'minor-mode-alist)
13264   (or (assq 'gnus-show-mime minor-mode-alist)
13265       (setq minor-mode-alist
13266             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13267   (use-local-map gnus-article-mode-map)
13268   (make-local-variable 'page-delimiter)
13269   (setq page-delimiter gnus-page-delimiter)
13270   (buffer-disable-undo (current-buffer))
13271   (setq buffer-read-only t)             ;Disable modification
13272   (run-hooks 'gnus-article-mode-hook))
13273
13274 (defun gnus-article-setup-buffer ()
13275   "Initialize the article buffer."
13276   (let* ((name (if gnus-single-article-buffer "*Article*"
13277                  (concat "*Article " gnus-newsgroup-name "*")))
13278          (original
13279           (progn (string-match "\\*Article" name)
13280                  (concat " *Original Article"
13281                          (substring name (match-end 0))))))
13282     (setq gnus-article-buffer name)
13283     (setq gnus-original-article-buffer original)
13284     ;; This might be a variable local to the summary buffer.
13285     (unless gnus-single-article-buffer
13286       (save-excursion
13287         (set-buffer gnus-summary-buffer)
13288         (setq gnus-article-buffer name)
13289         (setq gnus-original-article-buffer original)
13290         (gnus-set-global-variables))
13291       (make-local-variable 'gnus-summary-buffer))
13292     ;; Init original article buffer.
13293     (save-excursion
13294       (set-buffer (get-buffer-create gnus-original-article-buffer))
13295       (buffer-disable-undo (current-buffer))
13296       (setq major-mode 'gnus-original-article-mode)
13297       (make-local-variable 'gnus-original-article))
13298     (if (get-buffer name)
13299         (save-excursion
13300           (set-buffer name)
13301           (buffer-disable-undo (current-buffer))
13302           (setq buffer-read-only t)
13303           (gnus-add-current-to-buffer-list)
13304           (or (eq major-mode 'gnus-article-mode)
13305               (gnus-article-mode))
13306           (current-buffer))
13307       (save-excursion
13308         (set-buffer (get-buffer-create name))
13309         (gnus-add-current-to-buffer-list)
13310         (gnus-article-mode)
13311         (current-buffer)))))
13312
13313 ;; Set article window start at LINE, where LINE is the number of lines
13314 ;; from the head of the article.
13315 (defun gnus-article-set-window-start (&optional line)
13316   (set-window-start
13317    (get-buffer-window gnus-article-buffer)
13318    (save-excursion
13319      (set-buffer gnus-article-buffer)
13320      (goto-char (point-min))
13321      (if (not line)
13322          (point-min)
13323        (gnus-message 6 "Moved to bookmark")
13324        (search-forward "\n\n" nil t)
13325        (forward-line line)
13326        (point)))))
13327
13328 (defun gnus-kill-all-overlays ()
13329   "Delete all overlays in the current buffer."
13330   (when (fboundp 'overlay-lists)
13331     (let* ((overlayss (overlay-lists))
13332            (buffer-read-only nil)
13333            (overlays (nconc (car overlayss) (cdr overlayss))))
13334       (while overlays
13335         (delete-overlay (pop overlays))))))
13336
13337 (defun gnus-request-article-this-buffer (article group)
13338   "Get an article and insert it into this buffer."
13339   (let (do-update-line)
13340     (prog1
13341         (save-excursion
13342           (erase-buffer)
13343           (gnus-kill-all-overlays)
13344           (setq group (or group gnus-newsgroup-name))
13345
13346           ;; Open server if it has closed.
13347           (gnus-check-server (gnus-find-method-for-group group))
13348
13349           ;; Using `gnus-request-article' directly will insert the article into
13350           ;; `nntp-server-buffer' - so we'll save some time by not having to
13351           ;; copy it from the server buffer into the article buffer.
13352
13353           ;; We only request an article by message-id when we do not have the
13354           ;; headers for it, so we'll have to get those.
13355           (when (stringp article)
13356             (let ((gnus-override-method gnus-refer-article-method))
13357               (gnus-read-header article)))
13358
13359           ;; If the article number is negative, that means that this article
13360           ;; doesn't belong in this newsgroup (possibly), so we find its
13361           ;; message-id and request it by id instead of number.
13362           (when (and (numberp article)
13363                      gnus-summary-buffer
13364                      (get-buffer gnus-summary-buffer)
13365                      (buffer-name (get-buffer gnus-summary-buffer)))
13366             (save-excursion
13367               (set-buffer gnus-summary-buffer)
13368               (let ((header (gnus-summary-article-header article)))
13369                 (if (< article 0)
13370                     (cond 
13371                      ((memq article gnus-newsgroup-sparse)
13372                       ;; This is a sparse gap article.
13373                       (setq do-update-line article)
13374                       (setq article (mail-header-id header))
13375                       (let ((gnus-override-method gnus-refer-article-method))
13376                         (gnus-read-header article)))
13377                      ((vectorp header)
13378                       ;; It's a real article.
13379                       (setq article (mail-header-id header)))
13380                      (t
13381                       ;; It is an extracted pseudo-article.
13382                       (setq article 'pseudo)
13383                       (gnus-request-pseudo-article header))))
13384                 
13385                 (let ((method (gnus-find-method-for-group 
13386                                gnus-newsgroup-name)))
13387                   (if (not (eq (car method) 'nneething))
13388                       ()
13389                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13390                                        (mail-header-subject header))))
13391                       (if (file-directory-p dir)
13392                           (progn
13393                             (setq article 'nneething)
13394                             (gnus-group-enter-directory dir)))))))))
13395
13396           (cond
13397            ;; We first check `gnus-original-article-buffer'.
13398            ((and (get-buffer gnus-original-article-buffer)
13399                  (save-excursion
13400                    (set-buffer gnus-original-article-buffer)
13401                    (and (equal (car gnus-original-article) group)
13402                         (eq (cdr gnus-original-article) article))))
13403             (insert-buffer-substring gnus-original-article-buffer)
13404             'article)
13405            ;; Check the backlog.
13406            ((and gnus-keep-backlog
13407                  (gnus-backlog-request-article group article (current-buffer)))
13408             'article)
13409            ;; Check the cache.
13410            ((and gnus-use-cache
13411                  (numberp article)
13412                  (gnus-cache-request-article article group))
13413             'article)
13414            ;; Get the article and put into the article buffer.
13415            ((or (stringp article) (numberp article))
13416             (let ((gnus-override-method
13417                    (and (stringp article) gnus-refer-article-method))
13418                   (buffer-read-only nil))
13419               (erase-buffer)
13420               (gnus-kill-all-overlays)
13421               (if (gnus-request-article article group (current-buffer))
13422                   (progn
13423                     (and gnus-keep-backlog
13424                          (numberp article)
13425                          (gnus-backlog-enter-article
13426                           group article (current-buffer)))
13427                     'article))))
13428            ;; It was a pseudo.
13429            (t article)))
13430
13431       ;; Take the article from the original article buffer
13432       ;; and place it in the buffer it's supposed to be in.
13433       (when (and (get-buffer gnus-article-buffer)
13434                  (equal (buffer-name (current-buffer))
13435                         (buffer-name (get-buffer gnus-article-buffer))))
13436         (save-excursion
13437           (if (get-buffer gnus-original-article-buffer)
13438               (set-buffer (get-buffer gnus-original-article-buffer))
13439             (set-buffer (get-buffer-create gnus-original-article-buffer))
13440             (buffer-disable-undo (current-buffer))
13441             (setq major-mode 'gnus-original-article-mode)
13442             (setq buffer-read-only t)
13443             (gnus-add-current-to-buffer-list))
13444           (let (buffer-read-only)
13445             (erase-buffer)
13446             (insert-buffer-substring gnus-article-buffer))
13447           (setq gnus-original-article (cons group article))))
13448     
13449       ;; Update sparse articles.
13450       (when do-update-line
13451         (save-excursion
13452           (set-buffer gnus-summary-buffer)
13453           (gnus-summary-update-article do-update-line)
13454           (gnus-summary-goto-subject do-update-line)
13455           (set-window-point (get-buffer-window (current-buffer) t)
13456                             (point)))))))
13457
13458 (defun gnus-read-header (id &optional header)
13459   "Read the headers of article ID and enter them into the Gnus system."
13460   (let ((group gnus-newsgroup-name)
13461         (gnus-override-method 
13462          (and (gnus-news-group-p gnus-newsgroup-name)
13463               gnus-refer-article-method))       
13464         where)
13465     ;; First we check to see whether the header in question is already
13466     ;; fetched.
13467     (if (stringp id)
13468         ;; This is a Message-ID.
13469         (setq header (or header (gnus-id-to-header id)))
13470       ;; This is an article number.
13471       (setq header (or header (gnus-summary-article-header id))))
13472     (if (and header
13473              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13474         ;; We have found the header.
13475         header
13476       ;; We have to really fetch the header to this article.
13477       (when (setq where
13478                   (if (gnus-check-backend-function 'request-head group)
13479                       (gnus-request-head id group)
13480                     (gnus-request-article id group)))
13481         (save-excursion
13482           (set-buffer nntp-server-buffer)
13483           (and (search-forward "\n\n" nil t)
13484                (delete-region (1- (point)) (point-max)))
13485           (goto-char (point-max))
13486           (insert ".\n")
13487           (goto-char (point-min))
13488           (insert "211 ")
13489           (princ (cond
13490                   ((numberp id) id)
13491                   ((cdr where) (cdr where))
13492                   (header (mail-header-number header))
13493                   (t gnus-reffed-article-number))
13494                  (current-buffer))
13495           (insert " Article retrieved.\n"))
13496         ;(when (and header
13497         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13498         ;  (setcar (gnus-id-to-thread id) nil))
13499         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13500             ()                          ; Malformed head.
13501           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13502             (if (and (stringp id)
13503                      (not (string= (gnus-group-real-name group)
13504                                    (car where))))
13505                 ;; If we fetched by Message-ID and the article came
13506                 ;; from a different group, we fudge some bogus article
13507                 ;; numbers for this article.
13508                 (mail-header-set-number header gnus-reffed-article-number))
13509             (decf gnus-reffed-article-number)
13510             (push header gnus-newsgroup-headers)
13511             (setq gnus-current-headers header)
13512             (push (mail-header-number header) gnus-newsgroup-limit))
13513           header)))))
13514
13515 (defun gnus-article-prepare (article &optional all-headers header)
13516   "Prepare ARTICLE in article mode buffer.
13517 ARTICLE should either be an article number or a Message-ID.
13518 If ARTICLE is an id, HEADER should be the article headers.
13519 If ALL-HEADERS is non-nil, no headers are hidden."
13520   (save-excursion
13521     ;; Make sure we start in a summary buffer.
13522     (unless (eq major-mode 'gnus-summary-mode)
13523       (set-buffer gnus-summary-buffer))
13524     (setq gnus-summary-buffer (current-buffer))
13525     ;; Make sure the connection to the server is alive.
13526     (unless (gnus-server-opened
13527              (gnus-find-method-for-group gnus-newsgroup-name))
13528       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13529       (gnus-request-group gnus-newsgroup-name t))
13530     (let* ((article (if header (mail-header-number header) article))
13531            (summary-buffer (current-buffer))
13532            (internal-hook gnus-article-internal-prepare-hook)
13533            (group gnus-newsgroup-name)
13534            result)
13535       (save-excursion
13536         (gnus-article-setup-buffer)
13537         (set-buffer gnus-article-buffer)
13538         ;; Deactivate active regions.
13539         (when (and (boundp 'transient-mark-mode)
13540                    transient-mark-mode)
13541           (setq mark-active nil))
13542         (if (not (setq result (let ((buffer-read-only nil))
13543                                 (gnus-request-article-this-buffer
13544                                  article group))))
13545             ;; There is no such article.
13546             (save-excursion
13547               (when (and (numberp article)
13548                          (not (memq article gnus-newsgroup-sparse)))
13549                 (setq gnus-article-current
13550                       (cons gnus-newsgroup-name article))
13551                 (set-buffer gnus-summary-buffer)
13552                 (setq gnus-current-article article)
13553                 (gnus-summary-mark-article article gnus-canceled-mark))
13554               (unless (memq article gnus-newsgroup-sparse)
13555                 (gnus-error
13556                  1 "No such article (may have expired or been canceled)")))
13557           (if (or (eq result 'pseudo) (eq result 'nneething))
13558               (progn
13559                 (save-excursion
13560                   (set-buffer summary-buffer)
13561                   (setq gnus-last-article gnus-current-article
13562                         gnus-newsgroup-history (cons gnus-current-article
13563                                                      gnus-newsgroup-history)
13564                         gnus-current-article 0
13565                         gnus-current-headers nil
13566                         gnus-article-current nil)
13567                   (if (eq result 'nneething)
13568                       (gnus-configure-windows 'summary)
13569                     (gnus-configure-windows 'article))
13570                   (gnus-set-global-variables))
13571                 (gnus-set-mode-line 'article))
13572             ;; The result from the `request' was an actual article -
13573             ;; or at least some text that is now displayed in the
13574             ;; article buffer.
13575             (if (and (numberp article)
13576                      (not (eq article gnus-current-article)))
13577                 ;; Seems like a new article has been selected.
13578                 ;; `gnus-current-article' must be an article number.
13579                 (save-excursion
13580                   (set-buffer summary-buffer)
13581                   (setq gnus-last-article gnus-current-article
13582                         gnus-newsgroup-history (cons gnus-current-article
13583                                                      gnus-newsgroup-history)
13584                         gnus-current-article article
13585                         gnus-current-headers
13586                         (gnus-summary-article-header gnus-current-article)
13587                         gnus-article-current
13588                         (cons gnus-newsgroup-name gnus-current-article))
13589                   (unless (vectorp gnus-current-headers)
13590                     (setq gnus-current-headers nil))
13591                   (gnus-summary-show-thread)
13592                   (run-hooks 'gnus-mark-article-hook)
13593                   (gnus-set-mode-line 'summary)
13594                   (and (gnus-visual-p 'article-highlight 'highlight)
13595                        (run-hooks 'gnus-visual-mark-article-hook))
13596                   ;; Set the global newsgroup variables here.
13597                   ;; Suggested by Jim Sisolak
13598                   ;; <sisolak@trans4.neep.wisc.edu>.
13599                   (gnus-set-global-variables)
13600                   (setq gnus-have-all-headers
13601                         (or all-headers gnus-show-all-headers))
13602                   (and gnus-use-cache
13603                        (vectorp (gnus-summary-article-header article))
13604                        (gnus-cache-possibly-enter-article
13605                         group article
13606                         (gnus-summary-article-header article)
13607                         (memq article gnus-newsgroup-marked)
13608                         (memq article gnus-newsgroup-dormant)
13609                         (memq article gnus-newsgroup-unreads)))))
13610             ;; Hooks for getting information from the article.
13611             ;; This hook must be called before being narrowed.
13612             (let (buffer-read-only)
13613               (run-hooks 'internal-hook)
13614               (run-hooks 'gnus-article-prepare-hook)
13615               ;; Decode MIME message.
13616               (if gnus-show-mime
13617                   (if (or (not gnus-strict-mime)
13618                           (gnus-fetch-field "Mime-Version"))
13619                       (funcall gnus-show-mime-method)
13620                     (funcall gnus-decode-encoded-word-method)))
13621               ;; Perform the article display hooks.
13622               (run-hooks 'gnus-article-display-hook))
13623             ;; Do page break.
13624             (goto-char (point-min))
13625             (and gnus-break-pages (gnus-narrow-to-page))
13626             (gnus-set-mode-line 'article)
13627             (gnus-configure-windows 'article)
13628             (goto-char (point-min))
13629             t))))))
13630
13631 (defun gnus-article-show-all-headers ()
13632   "Show all article headers in article mode buffer."
13633   (save-excursion
13634     (gnus-article-setup-buffer)
13635     (set-buffer gnus-article-buffer)
13636     (let ((buffer-read-only nil))
13637       (gnus-unhide-text (point-min) (point-max)))))
13638
13639 (defun gnus-article-hide-headers-if-wanted ()
13640   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13641 Provided for backwards compatibility."
13642   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13643       gnus-inhibit-hiding
13644       (gnus-article-hide-headers)))
13645
13646 (defsubst gnus-article-header-rank ()
13647   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13648   (let ((list gnus-sorted-header-list)
13649         (i 0))
13650     (while list
13651       (when (looking-at (car list))
13652         (setq list nil))
13653       (setq list (cdr list))
13654       (incf i))
13655     i))
13656
13657 (defun gnus-article-hide-headers (&optional arg delete)
13658   "Toggle whether to hide unwanted headers and possibly sort them as well.
13659 If given a negative prefix, always show; if given a positive prefix,
13660 always hide."
13661   (interactive "P")
13662   (unless (gnus-article-check-hidden-text 'headers arg)
13663     ;; This function might be inhibited.
13664     (unless gnus-inhibit-hiding
13665       (save-excursion
13666         (set-buffer gnus-article-buffer)
13667         (save-restriction
13668           (let ((buffer-read-only nil)
13669                 (props (nconc (list 'gnus-type 'headers)
13670                               gnus-hidden-properties))
13671                 (max (1+ (length gnus-sorted-header-list)))
13672                 (ignored (when (not (stringp gnus-visible-headers))
13673                            (cond ((stringp gnus-ignored-headers)
13674                                   gnus-ignored-headers)
13675                                  ((listp gnus-ignored-headers)
13676                                   (mapconcat 'identity gnus-ignored-headers
13677                                              "\\|")))))
13678                 (visible
13679                  (cond ((stringp gnus-visible-headers)
13680                         gnus-visible-headers)
13681                        ((and gnus-visible-headers
13682                              (listp gnus-visible-headers))
13683                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13684                 want-list beg)
13685             ;; First we narrow to just the headers.
13686             (widen)
13687             (goto-char (point-min))
13688             ;; Hide any "From " lines at the beginning of (mail) articles.
13689             (while (looking-at "From ")
13690               (forward-line 1))
13691             (unless (bobp)
13692               (gnus-hide-text (point-min) (point) props))
13693             ;; Then treat the rest of the header lines.
13694             (narrow-to-region
13695              (point)
13696              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13697             ;; Then we use the two regular expressions
13698             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13699             ;; select which header lines is to remain visible in the
13700             ;; article buffer.
13701             (goto-char (point-min))
13702             (while (re-search-forward "^[^ \t]*:" nil t)
13703               (beginning-of-line)
13704               ;; We add the headers we want to keep to a list and delete
13705               ;; them from the buffer.
13706               (put-text-property 
13707                (point) (1+ (point)) 'message-rank
13708                (if (or (and visible (looking-at visible))
13709                        (and ignored
13710                             (not (looking-at ignored))))
13711                    (gnus-article-header-rank) 
13712                  (+ 2 max)))
13713               (forward-line 1))
13714             (message-sort-headers-1)
13715             (when (setq beg (text-property-any 
13716                              (point-min) (point-max) 'message-rank (+ 2 max)))
13717               ;; We make the unwanted headers invisible.
13718               (if delete
13719                   (delete-region beg (point-max))
13720                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13721                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13722
13723 (defun gnus-article-hide-boring-headers (&optional arg)
13724   "Toggle hiding of headers that aren't very interesting.
13725 If given a negative prefix, always show; if given a positive prefix,
13726 always hide."
13727   (interactive "P")
13728   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13729     (save-excursion
13730       (set-buffer gnus-article-buffer)
13731       (save-restriction
13732         (let ((buffer-read-only nil)
13733               (list gnus-boring-article-headers)
13734               (inhibit-point-motion-hooks t)
13735               elem)
13736           (nnheader-narrow-to-headers)
13737           (while list
13738             (setq elem (pop list))
13739             (goto-char (point-min))
13740             (cond
13741              ;; Hide empty headers.
13742              ((eq elem 'empty)
13743               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13744                 (forward-line -1)
13745                 (gnus-hide-text-type
13746                  (progn (beginning-of-line) (point))
13747                  (progn 
13748                    (end-of-line)
13749                    (if (re-search-forward "^[^ \t]" nil t)
13750                        (match-beginning 0)
13751                      (point-max)))
13752                  'boring-headers)))
13753              ;; Hide boring Newsgroups header.
13754              ((eq elem 'newsgroups)
13755               (when (equal (mail-fetch-field "newsgroups")
13756                            (gnus-group-real-name gnus-newsgroup-name))
13757                 (gnus-article-hide-header "newsgroups")))
13758              ((eq elem 'followup-to)
13759               (when (equal (mail-fetch-field "followup-to")
13760                            (mail-fetch-field "newsgroups"))
13761                 (gnus-article-hide-header "followup-to")))
13762              ((eq elem 'reply-to)
13763               (let ((from (mail-fetch-field "from"))
13764                     (reply-to (mail-fetch-field "reply-to")))
13765                 (when (and
13766                        from reply-to
13767                        (equal 
13768                         (nth 1 (funcall gnus-extract-address-components from))
13769                         (nth 1 (funcall gnus-extract-address-components
13770                                         reply-to))))
13771                   (gnus-article-hide-header "reply-to"))))
13772              ((eq elem 'date)
13773               (let ((date (mail-fetch-field "date")))
13774                 (when (and date
13775                            (< (gnus-days-between date (current-time-string))
13776                               4))
13777                   (gnus-article-hide-header "date")))))))))))
13778
13779 (defun gnus-article-hide-header (header)
13780   (save-excursion
13781     (goto-char (point-min))
13782     (when (re-search-forward (concat "^" header ":") nil t)
13783       (gnus-hide-text-type
13784        (progn (beginning-of-line) (point))
13785        (progn 
13786          (end-of-line)
13787          (if (re-search-forward "^[^ \t]" nil t)
13788              (match-beginning 0)
13789            (point-max)))
13790        'boring-headers))))
13791
13792 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13793 (defun gnus-article-treat-overstrike ()
13794   "Translate overstrikes into bold text."
13795   (interactive)
13796   (save-excursion
13797     (set-buffer gnus-article-buffer)
13798     (let ((buffer-read-only nil))
13799       (while (search-forward "\b" nil t)
13800         (let ((next (following-char))
13801               (previous (char-after (- (point) 2))))
13802           (cond ((eq next previous)
13803                  (put-text-property (- (point) 2) (point) 'invisible t)
13804                  (put-text-property (point) (1+ (point)) 'face 'bold))
13805                 ((eq next ?_)
13806                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13807                  (put-text-property
13808                   (- (point) 2) (1- (point)) 'face 'underline))
13809                 ((eq previous ?_)
13810                  (put-text-property (- (point) 2) (point) 'invisible t)
13811                  (put-text-property
13812                   (point) (1+ (point))  'face 'underline))))))))
13813
13814 (defun gnus-article-word-wrap ()
13815   "Format too long lines."
13816   (interactive)
13817   (save-excursion
13818     (set-buffer gnus-article-buffer)
13819     (let ((buffer-read-only nil))
13820       (widen)
13821       (goto-char (point-min))
13822       (search-forward "\n\n" nil t)
13823       (end-of-line 1)
13824       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13825             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13826             (adaptive-fill-mode t))
13827         (while (not (eobp))
13828           (and (>= (current-column) (min fill-column (window-width)))
13829                (/= (preceding-char) ?:)
13830                (fill-paragraph nil))
13831           (end-of-line 2))))))
13832
13833 (defun gnus-article-remove-cr ()
13834   "Remove carriage returns from an article."
13835   (interactive)
13836   (save-excursion
13837     (set-buffer gnus-article-buffer)
13838     (let ((buffer-read-only nil))
13839       (goto-char (point-min))
13840       (while (search-forward "\r" nil t)
13841         (replace-match "" t t)))))
13842
13843 (defun gnus-article-remove-trailing-blank-lines ()
13844   "Remove all trailing blank lines from the article."
13845   (interactive)
13846   (save-excursion
13847     (set-buffer gnus-article-buffer)
13848     (let ((buffer-read-only nil))
13849       (goto-char (point-max))
13850       (delete-region
13851        (point)
13852        (progn
13853          (while (looking-at "^[ \t]*$")
13854            (forward-line -1))
13855          (forward-line 1)
13856          (point))))))
13857
13858 (defun gnus-article-display-x-face (&optional force)
13859   "Look for an X-Face header and display it if present."
13860   (interactive (list 'force))
13861   (save-excursion
13862     (set-buffer gnus-article-buffer)
13863     ;; Delete the old process, if any.
13864     (when (process-status "gnus-x-face")
13865       (delete-process "gnus-x-face"))
13866     (let ((inhibit-point-motion-hooks t)
13867           (case-fold-search nil)
13868           from)
13869       (save-restriction
13870         (nnheader-narrow-to-headers)
13871         (setq from (mail-fetch-field "from"))
13872         (goto-char (point-min))
13873         (when (and gnus-article-x-face-command
13874                    (or force
13875                        ;; Check whether this face is censored.
13876                        (not gnus-article-x-face-too-ugly)
13877                        (and gnus-article-x-face-too-ugly from
13878                             (not (string-match gnus-article-x-face-too-ugly
13879                                                from))))
13880                    ;; Has to be present.
13881                    (re-search-forward "^X-Face: " nil t))
13882           ;; We now have the area of the buffer where the X-Face is stored.
13883           (let ((beg (point))
13884                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13885             ;; We display the face.
13886             (if (symbolp gnus-article-x-face-command)
13887                 ;; The command is a lisp function, so we call it.
13888                 (if (gnus-functionp gnus-article-x-face-command)
13889                     (funcall gnus-article-x-face-command beg end)
13890                   (error "%s is not a function" gnus-article-x-face-command))
13891               ;; The command is a string, so we interpret the command
13892               ;; as a, well, command, and fork it off.
13893               (let ((process-connection-type nil))
13894                 (process-kill-without-query
13895                  (start-process
13896                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13897                 (process-send-region "gnus-x-face" beg end)
13898                 (process-send-eof "gnus-x-face")))))))))
13899
13900 (defun gnus-headers-decode-quoted-printable ()
13901   "Hack to remove QP encoding from headers."
13902   (let ((case-fold-search t)
13903         (inhibit-point-motion-hooks t)
13904         string)
13905     (goto-char (point-min))
13906     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13907       (setq string (match-string 1))
13908       (narrow-to-region (match-beginning 0) (match-end 0))
13909       (delete-region (point-min) (point-max))
13910       (insert string)
13911       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13912       (subst-char-in-region (point-min) (point-max) ?_ ? )
13913       (widen)
13914       (goto-char (point-min)))))
13915
13916 (defun gnus-article-de-quoted-unreadable (&optional force)
13917   "Do a naive translation of a quoted-printable-encoded article.
13918 This is in no way, shape or form meant as a replacement for real MIME
13919 processing, but is simply a stop-gap measure until MIME support is
13920 written.
13921 If FORCE, decode the article whether it is marked as quoted-printable
13922 or not."
13923   (interactive (list 'force))
13924   (save-excursion
13925     (set-buffer gnus-article-buffer)
13926     (let ((case-fold-search t)
13927           (buffer-read-only nil)
13928           (type (gnus-fetch-field "content-transfer-encoding")))
13929       (when (or force
13930                 (and type (string-match "quoted-printable" (downcase type))))
13931         (gnus-headers-decode-quoted-printable)
13932         (goto-char (point-min))
13933         (search-forward "\n\n" nil 'move)
13934         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13935
13936 (defun gnus-mime-decode-quoted-printable (from to)
13937   "Decode Quoted-Printable in the region between FROM and TO."
13938   (interactive "r")
13939   (goto-char from)
13940   (while (search-forward "=" to t)
13941     (cond ((eq (following-char) ?\n)
13942            (delete-char -1)
13943            (delete-char 1))
13944           ((looking-at "[0-9A-F][0-9A-F]")
13945            (delete-char -1)
13946            (insert (hexl-hex-string-to-integer
13947                     (buffer-substring (point) (+ 2 (point)))))
13948            (delete-char 2))
13949           ((looking-at "=")
13950            (delete-char 1))
13951           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13952
13953 (defun gnus-article-hide-pgp (&optional arg)
13954   "Toggle hiding of any PGP headers and signatures in the current article.
13955 If given a negative prefix, always show; if given a positive prefix,
13956 always hide."
13957   (interactive "P")
13958   (unless (gnus-article-check-hidden-text 'pgp arg)
13959     (save-excursion
13960       (set-buffer gnus-article-buffer)
13961       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13962             buffer-read-only beg end)
13963         (widen)
13964         (goto-char (point-min))
13965         ;; Hide the "header".
13966         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13967              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13968         (setq beg (point))
13969         ;; Hide the actual signature.
13970         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13971              (setq end (1+ (match-beginning 0)))
13972              (gnus-hide-text
13973               end
13974               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13975                   (match-end 0)
13976                 ;; Perhaps we shouldn't hide to the end of the buffer
13977                 ;; if there is no end to the signature?
13978                 (point-max))
13979               props))
13980         ;; Hide "- " PGP quotation markers.
13981         (when (and beg end)
13982           (narrow-to-region beg end)
13983           (goto-char (point-min))
13984           (while (re-search-forward "^- " nil t)
13985             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13986           (widen))))))
13987
13988 (defun gnus-article-hide-signature (&optional arg)
13989   "Hide the signature in the current article.
13990 If given a negative prefix, always show; if given a positive prefix,
13991 always hide."
13992   (interactive "P")
13993   (unless (gnus-article-check-hidden-text 'signature arg)
13994     (save-excursion
13995       (set-buffer gnus-article-buffer)
13996       (save-restriction
13997         (let ((buffer-read-only nil))
13998           (when (gnus-narrow-to-signature)
13999             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14000
14001 (defun gnus-article-strip-leading-blank-lines ()
14002   "Remove all blank lines from the beginning of the article."
14003   (interactive)
14004   (save-excursion
14005     (set-buffer gnus-article-buffer)
14006     (let (buffer-read-only)
14007       (goto-char (point-min))
14008       (when (search-forward "\n\n" nil t)
14009         (while (looking-at "[ \t]$")
14010           (gnus-delete-line))))))
14011
14012 (defun gnus-narrow-to-signature ()
14013   "Narrow to the signature."
14014   (widen)
14015   (goto-char (point-max))
14016   (when (re-search-backward gnus-signature-separator nil t)
14017     (forward-line 1)
14018     (when (or (null gnus-signature-limit)
14019               (and (numberp gnus-signature-limit)
14020                    (< (- (point-max) (point)) gnus-signature-limit))
14021               (and (gnus-functionp gnus-signature-limit)
14022                    (funcall gnus-signature-limit))
14023               (and (stringp gnus-signature-limit)
14024                    (not (re-search-forward gnus-signature-limit nil t))))
14025       (narrow-to-region (point) (point-max))
14026       t)))
14027
14028 (defun gnus-article-check-hidden-text (type arg)
14029   "Return nil if hiding is necessary."
14030   (save-excursion
14031     (set-buffer gnus-article-buffer)
14032     (let ((hide (gnus-article-hidden-text-p type)))
14033       (cond ((or (and (null arg) (eq hide 'hidden))
14034                  (and arg (< 0 (prefix-numeric-value arg))))
14035              (gnus-article-show-hidden-text type))
14036             ((eq hide 'shown)
14037              (gnus-article-show-hidden-text type t))
14038             (t nil)))))
14039
14040 (defun gnus-article-hidden-text-p (type)
14041   "Say whether the current buffer contains hidden text of type TYPE."
14042   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14043     (when pos
14044       (if (get-text-property pos 'invisible)
14045           'hidden
14046         'shown))))
14047
14048 (defun gnus-article-hide (&optional arg force)
14049   "Hide all the gruft in the current article.
14050 This means that PGP stuff, signatures, cited text and (some)
14051 headers will be hidden.
14052 If given a prefix, show the hidden text instead."
14053   (interactive (list current-prefix-arg 'force))
14054   (gnus-article-hide-headers arg)
14055   (gnus-article-hide-pgp arg)
14056   (gnus-article-hide-citation-maybe arg force)
14057   (gnus-article-hide-signature arg))
14058
14059 (defun gnus-article-show-hidden-text (type &optional hide)
14060   "Show all hidden text of type TYPE.
14061 If HIDE, hide the text instead."
14062   (save-excursion
14063     (set-buffer gnus-article-buffer)
14064     (let ((buffer-read-only nil)
14065           (inhibit-point-motion-hooks t)
14066           (beg (point-min)))
14067       (while (gnus-goto-char (text-property-any
14068                               beg (point-max) 'gnus-type type))
14069         (setq beg (point))
14070         (forward-char)
14071         (if hide
14072             (gnus-hide-text beg (point) gnus-hidden-properties)
14073           (gnus-unhide-text beg (point)))
14074         (setq beg (point)))
14075       t)))
14076
14077 (defvar gnus-article-time-units
14078   `((year . ,(* 365.25 24 60 60))
14079     (week . ,(* 7 24 60 60))
14080     (day . ,(* 24 60 60))
14081     (hour . ,(* 60 60))
14082     (minute . 60)
14083     (second . 1))
14084   "Mapping from time units to seconds.")
14085
14086 (defun gnus-article-date-ut (&optional type highlight)
14087   "Convert DATE date to universal time in the current article.
14088 If TYPE is `local', convert to local time; if it is `lapsed', output
14089 how much time has lapsed since DATE."
14090   (interactive (list 'ut t))
14091   (let* ((header (or gnus-current-headers
14092                      (gnus-summary-article-header) ""))
14093          (date (and (vectorp header) (mail-header-date header)))
14094          (date-regexp "^Date: \\|^X-Sent: ")
14095          (now (current-time))
14096          (inhibit-point-motion-hooks t)
14097          bface eface)
14098     (when (and date (not (string= date "")))
14099       (save-excursion
14100         (set-buffer gnus-article-buffer)
14101         (save-restriction
14102           (nnheader-narrow-to-headers)
14103           (let ((buffer-read-only nil))
14104             ;; Delete any old Date headers.
14105             (if (re-search-forward date-regexp nil t)
14106                 (progn
14107                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14108                         eface (get-text-property (gnus-point-at-eol) 'face))
14109                   (message-remove-header date-regexp t)
14110                   (beginning-of-line))
14111               (goto-char (point-max)))
14112             (insert
14113              (cond
14114               ;; Convert to the local timezone.  We have to slap a
14115               ;; `condition-case' round the calls to the timezone
14116               ;; functions since they aren't particularly resistant to
14117               ;; buggy dates.
14118               ((eq type 'local)
14119                (concat "Date: " (condition-case ()
14120                                     (timezone-make-date-arpa-standard date)
14121                                   (error date))
14122                        "\n"))
14123               ;; Convert to Universal Time.
14124               ((eq type 'ut)
14125                (concat "Date: "
14126                        (condition-case ()
14127                            (timezone-make-date-arpa-standard date nil "UT")
14128                          (error date))
14129                        "\n"))
14130               ;; Get the original date from the article.
14131               ((eq type 'original)
14132                (concat "Date: " date "\n"))
14133               ;; Do an X-Sent lapsed format.
14134               ((eq type 'lapsed)
14135                ;; If the date is seriously mangled, the timezone
14136                ;; functions are liable to bug out, so we condition-case
14137                ;; the entire thing.
14138                (let* ((real-time
14139                        (condition-case ()
14140                            (gnus-time-minus
14141                             (gnus-encode-date
14142                              (timezone-make-date-arpa-standard
14143                               (current-time-string now)
14144                               (current-time-zone now) "UT"))
14145                             (gnus-encode-date
14146                              (timezone-make-date-arpa-standard
14147                               date nil "UT")))
14148                          (error '(0 0))))
14149                       (real-sec (+ (* (float (car real-time)) 65536)
14150                                    (cadr real-time)))
14151                       (sec (abs real-sec))
14152                       num prev)
14153                  (if (zerop sec)
14154                      "X-Sent: Now\n"
14155                    (concat
14156                     "X-Sent: "
14157                     ;; This is a bit convoluted, but basically we go
14158                     ;; through the time units for years, weeks, etc,
14159                     ;; and divide things to see whether that results
14160                     ;; in positive answers.
14161                     (mapconcat
14162                      (lambda (unit)
14163                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14164                            ;; The (remaining) seconds are too few to
14165                            ;; be divided into this time unit.
14166                            ""
14167                          ;; It's big enough, so we output it.
14168                          (setq sec (- sec (* num (cdr unit))))
14169                          (prog1
14170                              (concat (if prev ", " "") (int-to-string
14171                                                         (floor num))
14172                                      " " (symbol-name (car unit))
14173                                      (if (> num 1) "s" ""))
14174                            (setq prev t))))
14175                      gnus-article-time-units "")
14176                     ;; If dates are odd, then it might appear like the
14177                     ;; article was sent in the future.
14178                     (if (> real-sec 0)
14179                         " ago\n"
14180                       " in the future\n")))))
14181               (t
14182                (error "Unknown conversion type: %s" type)))))
14183           ;; Do highlighting.
14184           (beginning-of-line)
14185           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14186                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14187             (put-text-property (match-beginning 1) (match-end 1)
14188                                'face bface)
14189             (put-text-property (match-beginning 2) (match-end 2)
14190                                'face eface)))))))
14191
14192 (defun gnus-article-date-local (&optional highlight)
14193   "Convert the current article date to the local timezone."
14194   (interactive (list t))
14195   (gnus-article-date-ut 'local highlight))
14196
14197 (defun gnus-article-date-original (&optional highlight)
14198   "Convert the current article date to what it was originally.
14199 This is only useful if you have used some other date conversion
14200 function and want to see what the date was before converting."
14201   (interactive (list t))
14202   (gnus-article-date-ut 'original highlight))
14203
14204 (defun gnus-article-date-lapsed (&optional highlight)
14205   "Convert the current article date to time lapsed since it was sent."
14206   (interactive (list t))
14207   (gnus-article-date-ut 'lapsed highlight))
14208
14209 (defun gnus-article-maybe-highlight ()
14210   "Do some article highlighting if `gnus-visual' is non-nil."
14211   (if (gnus-visual-p 'article-highlight 'highlight)
14212       (gnus-article-highlight-some)))
14213
14214 ;; Article savers.
14215
14216 (defun gnus-output-to-rmail (file-name)
14217   "Append the current article to an Rmail file named FILE-NAME."
14218   (require 'rmail)
14219   ;; Most of these codes are borrowed from rmailout.el.
14220   (setq file-name (expand-file-name file-name))
14221   (setq rmail-default-rmail-file file-name)
14222   (let ((artbuf (current-buffer))
14223         (tmpbuf (get-buffer-create " *Gnus-output*")))
14224     (save-excursion
14225       (or (get-file-buffer file-name)
14226           (file-exists-p file-name)
14227           (if (gnus-yes-or-no-p
14228                (concat "\"" file-name "\" does not exist, create it? "))
14229               (let ((file-buffer (create-file-buffer file-name)))
14230                 (save-excursion
14231                   (set-buffer file-buffer)
14232                   (rmail-insert-rmail-file-header)
14233                   (let ((require-final-newline nil))
14234                     (write-region (point-min) (point-max) file-name t 1)))
14235                 (kill-buffer file-buffer))
14236             (error "Output file does not exist")))
14237       (set-buffer tmpbuf)
14238       (buffer-disable-undo (current-buffer))
14239       (erase-buffer)
14240       (insert-buffer-substring artbuf)
14241       (gnus-convert-article-to-rmail)
14242       ;; Decide whether to append to a file or to an Emacs buffer.
14243       (let ((outbuf (get-file-buffer file-name)))
14244         (if (not outbuf)
14245             (append-to-file (point-min) (point-max) file-name)
14246           ;; File has been visited, in buffer OUTBUF.
14247           (set-buffer outbuf)
14248           (let ((buffer-read-only nil)
14249                 (msg (and (boundp 'rmail-current-message)
14250                           (symbol-value 'rmail-current-message))))
14251             ;; If MSG is non-nil, buffer is in RMAIL mode.
14252             (if msg
14253                 (progn (widen)
14254                        (narrow-to-region (point-max) (point-max))))
14255             (insert-buffer-substring tmpbuf)
14256             (if msg
14257                 (progn
14258                   (goto-char (point-min))
14259                   (widen)
14260                   (search-backward "\^_")
14261                   (narrow-to-region (point) (point-max))
14262                   (goto-char (1+ (point-min)))
14263                   (rmail-count-new-messages t)
14264                   (rmail-show-message msg)))))))
14265     (kill-buffer tmpbuf)))
14266
14267 (defun gnus-output-to-file (file-name)
14268   "Append the current article to a file named FILE-NAME."
14269   (setq file-name (expand-file-name file-name))
14270   (let ((artbuf (current-buffer))
14271         (tmpbuf (get-buffer-create " *Gnus-output*")))
14272     (save-excursion
14273       (set-buffer tmpbuf)
14274       (buffer-disable-undo (current-buffer))
14275       (erase-buffer)
14276       (insert-buffer-substring artbuf)
14277       ;; Append newline at end of the buffer as separator, and then
14278       ;; save it to file.
14279       (goto-char (point-max))
14280       (insert "\n")
14281       (append-to-file (point-min) (point-max) file-name))
14282     (kill-buffer tmpbuf)))
14283
14284 (defun gnus-convert-article-to-rmail ()
14285   "Convert article in current buffer to Rmail message format."
14286   (let ((buffer-read-only nil))
14287     ;; Convert article directly into Babyl format.
14288     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14289     (goto-char (point-min))
14290     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14291     (while (search-forward "\n\^_" nil t) ;single char
14292       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14293     (goto-char (point-max))
14294     (insert "\^_")))
14295
14296 (defun gnus-narrow-to-page (&optional arg)
14297   "Narrow the article buffer to a page.
14298 If given a numerical ARG, move forward ARG pages."
14299   (interactive "P")
14300   (setq arg (if arg (prefix-numeric-value arg) 0))
14301   (save-excursion
14302     (set-buffer gnus-article-buffer)
14303     (goto-char (point-min))
14304     (widen)
14305     (when (gnus-visual-p 'page-marker)
14306       (let ((buffer-read-only nil))
14307         (gnus-remove-text-with-property 'gnus-prev)
14308         (gnus-remove-text-with-property 'gnus-next)))
14309     (when
14310         (cond ((< arg 0)
14311                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14312               ((> arg 0)
14313                (re-search-forward page-delimiter nil 'move arg)))
14314       (goto-char (match-end 0)))
14315     (narrow-to-region
14316      (point)
14317      (if (re-search-forward page-delimiter nil 'move)
14318          (match-beginning 0)
14319        (point)))
14320     (when (and (gnus-visual-p 'page-marker)
14321                (not (= (point-min) 1)))
14322       (save-excursion
14323         (goto-char (point-min))
14324         (gnus-insert-prev-page-button)))
14325     (when (and (gnus-visual-p 'page-marker)
14326                (not (= (1- (point-max)) (buffer-size))))
14327       (save-excursion
14328         (goto-char (point-max))
14329         (gnus-insert-next-page-button)))))
14330
14331 ;; Article mode commands
14332
14333 (defun gnus-article-goto-next-page ()
14334   "Show the next page of the article."
14335   (interactive)
14336   (when (gnus-article-next-page)
14337     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14338
14339 (defun gnus-article-goto-prev-page ()
14340   "Show the next page of the article."
14341   (interactive)
14342   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14343     (gnus-article-prev-page nil)))
14344
14345 (defun gnus-article-next-page (&optional lines)
14346   "Show the next page of the current article.
14347 If end of article, return non-nil.  Otherwise return nil.
14348 Argument LINES specifies lines to be scrolled up."
14349   (interactive "p")
14350   (move-to-window-line -1)
14351   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14352   (if (save-excursion
14353         (end-of-line)
14354         (and (pos-visible-in-window-p)  ;Not continuation line.
14355              (eobp)))
14356       ;; Nothing in this page.
14357       (if (or (not gnus-break-pages)
14358               (save-excursion
14359                 (save-restriction
14360                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14361           t                             ;Nothing more.
14362         (gnus-narrow-to-page 1)         ;Go to next page.
14363         nil)
14364     ;; More in this page.
14365     (condition-case ()
14366         (scroll-up lines)
14367       (end-of-buffer
14368        ;; Long lines may cause an end-of-buffer error.
14369        (goto-char (point-max))))
14370     (move-to-window-line 0)
14371     nil))
14372
14373 (defun gnus-article-prev-page (&optional lines)
14374   "Show previous page of current article.
14375 Argument LINES specifies lines to be scrolled down."
14376   (interactive "p")
14377   (move-to-window-line 0)
14378   (if (and gnus-break-pages
14379            (bobp)
14380            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14381       (progn
14382         (gnus-narrow-to-page -1)        ;Go to previous page.
14383         (goto-char (point-max))
14384         (recenter -1))
14385     (prog1
14386         (condition-case ()
14387             (scroll-down lines)
14388           (error nil))
14389       (move-to-window-line 0))))
14390
14391 (defun gnus-article-refer-article ()
14392   "Read article specified by message-id around point."
14393   (interactive)
14394   (let ((point (point)))
14395     (search-forward ">" nil t)          ;Move point to end of "<....>".
14396     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14397         (let ((message-id (match-string 1)))
14398           (goto-char point)
14399           (set-buffer gnus-summary-buffer)
14400           (gnus-summary-refer-article message-id))
14401       (goto-char (point))
14402       (error "No references around point"))))
14403
14404 (defun gnus-article-show-summary ()
14405   "Reconfigure windows to show summary buffer."
14406   (interactive)
14407   (gnus-configure-windows 'article)
14408   (gnus-summary-goto-subject gnus-current-article))
14409
14410 (defun gnus-article-describe-briefly ()
14411   "Describe article mode commands briefly."
14412   (interactive)
14413   (gnus-message 6
14414                 (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")))
14415
14416 (defun gnus-article-summary-command ()
14417   "Execute the last keystroke in the summary buffer."
14418   (interactive)
14419   (let ((obuf (current-buffer))
14420         (owin (current-window-configuration))
14421         func)
14422     (switch-to-buffer gnus-summary-buffer 'norecord)
14423     (setq func (lookup-key (current-local-map) (this-command-keys)))
14424     (call-interactively func)
14425     (set-buffer obuf)
14426     (set-window-configuration owin)
14427     (set-window-point (get-buffer-window (current-buffer)) (point))))
14428
14429 (defun gnus-article-summary-command-nosave ()
14430   "Execute the last keystroke in the summary buffer."
14431   (interactive)
14432   (let (func)
14433     (pop-to-buffer gnus-summary-buffer 'norecord)
14434     (setq func (lookup-key (current-local-map) (this-command-keys)))
14435     (call-interactively func)))
14436
14437 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14438   "Read a summary buffer key sequence and execute it from the article buffer."
14439   (interactive "P")
14440   (let ((nosaves
14441          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14442            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14443            "=" "^" "\M-^"))
14444         keys)
14445     (save-excursion
14446       (set-buffer gnus-summary-buffer)
14447       (push (or key last-command-event) unread-command-events)
14448       (setq keys (read-key-sequence nil)))
14449     (message "")
14450
14451     (if (member keys nosaves)
14452         (let (func)
14453           (pop-to-buffer gnus-summary-buffer 'norecord)
14454           (if (setq func (lookup-key (current-local-map) keys))
14455               (call-interactively func)
14456             (ding)))
14457       (let ((obuf (current-buffer))
14458             (owin (current-window-configuration))
14459             (opoint (point))
14460             func in-buffer)
14461         (if not-restore-window
14462             (pop-to-buffer gnus-summary-buffer 'norecord)
14463           (switch-to-buffer gnus-summary-buffer 'norecord))
14464         (setq in-buffer (current-buffer))
14465         (if (setq func (lookup-key (current-local-map) keys))
14466             (call-interactively func)
14467           (ding))
14468         (when (eq in-buffer (current-buffer))
14469           (set-buffer obuf)
14470           (unless not-restore-window
14471             (set-window-configuration owin))
14472           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14473
14474 \f
14475 ;;;
14476 ;;; Kill file handling.
14477 ;;;
14478
14479 ;;;###autoload
14480 (defalias 'gnus-batch-kill 'gnus-batch-score)
14481 ;;;###autoload
14482 (defun gnus-batch-score ()
14483   "Run batched scoring.
14484 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14485 Newsgroups is a list of strings in Bnews format.  If you want to score
14486 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14487 score the alt hierarchy, you'd say \"!alt.all\"."
14488   (interactive)
14489   (let* ((yes-and-no
14490           (gnus-newsrc-parse-options
14491            (apply (function concat)
14492                   (mapcar (lambda (g) (concat g " "))
14493                           command-line-args-left))))
14494          (gnus-expert-user t)
14495          (nnmail-spool-file nil)
14496          (gnus-use-dribble-file nil)
14497          (yes (car yes-and-no))
14498          (no (cdr yes-and-no))
14499          group newsrc entry
14500          ;; Disable verbose message.
14501          gnus-novice-user gnus-large-newsgroup)
14502     ;; Eat all arguments.
14503     (setq command-line-args-left nil)
14504     ;; Start Gnus.
14505     (gnus)
14506     ;; Apply kills to specified newsgroups in command line arguments.
14507     (setq newsrc (cdr gnus-newsrc-alist))
14508     (while newsrc
14509       (setq group (caar newsrc))
14510       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14511       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14512                (and (car entry)
14513                     (or (eq (car entry) t)
14514                         (not (zerop (car entry)))))
14515                (if yes (string-match yes group) t)
14516                (or (null no) (not (string-match no group))))
14517           (progn
14518             (gnus-summary-read-group group nil t nil t)
14519             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14520                  (gnus-summary-exit))))
14521       (setq newsrc (cdr newsrc)))
14522     ;; Exit Emacs.
14523     (switch-to-buffer gnus-group-buffer)
14524     (gnus-group-save-newsrc)))
14525
14526 (defun gnus-apply-kill-file ()
14527   "Apply a kill file to the current newsgroup.
14528 Returns the number of articles marked as read."
14529   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14530           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14531       (gnus-apply-kill-file-internal)
14532     0))
14533
14534 (defun gnus-kill-save-kill-buffer ()
14535   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14536     (when (get-file-buffer file)
14537       (save-excursion
14538         (set-buffer (get-file-buffer file))
14539         (and (buffer-modified-p) (save-buffer))
14540         (kill-buffer (current-buffer))))))
14541
14542 (defvar gnus-kill-file-name "KILL"
14543   "Suffix of the kill files.")
14544
14545 (defun gnus-newsgroup-kill-file (newsgroup)
14546   "Return the name of a kill file name for NEWSGROUP.
14547 If NEWSGROUP is nil, return the global kill file name instead."
14548   (cond 
14549    ;; The global KILL file is placed at top of the directory.
14550    ((or (null newsgroup)
14551         (string-equal newsgroup ""))
14552     (expand-file-name gnus-kill-file-name
14553                       (or gnus-kill-files-directory "~/News")))
14554    ;; Append ".KILL" to newsgroup name.
14555    ((gnus-use-long-file-name 'not-kill)
14556     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14557                               "." gnus-kill-file-name)
14558                       (or gnus-kill-files-directory "~/News")))
14559    ;; Place "KILL" under the hierarchical directory.
14560    (t
14561     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14562                               "/" gnus-kill-file-name)
14563                       (or gnus-kill-files-directory "~/News")))))
14564
14565 \f
14566 ;;;
14567 ;;; Dribble file
14568 ;;;
14569
14570 (defvar gnus-dribble-ignore nil)
14571 (defvar gnus-dribble-eval-file nil)
14572
14573 (defun gnus-dribble-file-name ()
14574   "Return the dribble file for the current .newsrc."
14575   (concat
14576    (if gnus-dribble-directory
14577        (concat (file-name-as-directory gnus-dribble-directory)
14578                (file-name-nondirectory gnus-current-startup-file))
14579      gnus-current-startup-file)
14580    "-dribble"))
14581
14582 (defun gnus-dribble-enter (string)
14583   "Enter STRING into the dribble buffer."
14584   (if (and (not gnus-dribble-ignore)
14585            gnus-dribble-buffer
14586            (buffer-name gnus-dribble-buffer))
14587       (let ((obuf (current-buffer)))
14588         (set-buffer gnus-dribble-buffer)
14589         (insert string "\n")
14590         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14591         (set-buffer obuf))))
14592
14593 (defun gnus-dribble-read-file ()
14594   "Read the dribble file from disk."
14595   (let ((dribble-file (gnus-dribble-file-name)))
14596     (save-excursion
14597       (set-buffer (setq gnus-dribble-buffer
14598                         (get-buffer-create
14599                          (file-name-nondirectory dribble-file))))
14600       (gnus-add-current-to-buffer-list)
14601       (erase-buffer)
14602       (setq buffer-file-name dribble-file)
14603       (auto-save-mode t)
14604       (buffer-disable-undo (current-buffer))
14605       (bury-buffer (current-buffer))
14606       (set-buffer-modified-p nil)
14607       (let ((auto (make-auto-save-file-name))
14608             (gnus-dribble-ignore t))
14609         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14610           ;; Load whichever file is newest -- the auto save file
14611           ;; or the "real" file.
14612           (if (file-newer-than-file-p auto dribble-file)
14613               (insert-file-contents auto)
14614             (insert-file-contents dribble-file))
14615           (unless (zerop (buffer-size))
14616             (set-buffer-modified-p t))
14617           ;; Set the file modes to reflect the .newsrc file modes.
14618           (save-buffer)
14619           (when (file-exists-p gnus-current-startup-file)
14620             (set-file-modes dribble-file
14621                             (file-modes gnus-current-startup-file)))
14622           ;; Possibly eval the file later.
14623           (when (gnus-y-or-n-p
14624                  "Auto-save file exists.  Do you want to read it? ")
14625             (setq gnus-dribble-eval-file t)))))))
14626
14627 (defun gnus-dribble-eval-file ()
14628   (if (not gnus-dribble-eval-file)
14629       ()
14630     (setq gnus-dribble-eval-file nil)
14631     (save-excursion
14632       (let ((gnus-dribble-ignore t))
14633         (set-buffer gnus-dribble-buffer)
14634         (eval-buffer (current-buffer))))))
14635
14636 (defun gnus-dribble-delete-file ()
14637   (if (file-exists-p (gnus-dribble-file-name))
14638       (delete-file (gnus-dribble-file-name)))
14639   (if gnus-dribble-buffer
14640       (save-excursion
14641         (set-buffer gnus-dribble-buffer)
14642         (let ((auto (make-auto-save-file-name)))
14643           (if (file-exists-p auto)
14644               (delete-file auto))
14645           (erase-buffer)
14646           (set-buffer-modified-p nil)))))
14647
14648 (defun gnus-dribble-save ()
14649   (if (and gnus-dribble-buffer
14650            (buffer-name gnus-dribble-buffer))
14651       (save-excursion
14652         (set-buffer gnus-dribble-buffer)
14653         (save-buffer))))
14654
14655 (defun gnus-dribble-clear ()
14656   (save-excursion
14657     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14658         (progn
14659           (set-buffer gnus-dribble-buffer)
14660           (erase-buffer)
14661           (set-buffer-modified-p nil)
14662           (setq buffer-saved-size (buffer-size))))))
14663
14664 \f
14665 ;;;
14666 ;;; Server Communication
14667 ;;;
14668
14669 (defun gnus-start-news-server (&optional confirm)
14670   "Open a method for getting news.
14671 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14672   (let (how)
14673     (if gnus-current-select-method
14674         ;; Stream is already opened.
14675         nil
14676       ;; Open NNTP server.
14677       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14678       (if confirm
14679           (progn
14680             ;; Read server name with completion.
14681             (setq gnus-nntp-server
14682                   (completing-read "NNTP server: "
14683                                    (mapcar (lambda (server) (list server))
14684                                            (cons (list gnus-nntp-server)
14685                                                  gnus-secondary-servers))
14686                                    nil nil gnus-nntp-server))))
14687
14688       (if (and gnus-nntp-server
14689                (stringp gnus-nntp-server)
14690                (not (string= gnus-nntp-server "")))
14691           (setq gnus-select-method
14692                 (cond ((or (string= gnus-nntp-server "")
14693                            (string= gnus-nntp-server "::"))
14694                        (list 'nnspool (system-name)))
14695                       ((string-match "^:" gnus-nntp-server)
14696                        (list 'nnmh gnus-nntp-server
14697                              (list 'nnmh-directory
14698                                    (file-name-as-directory
14699                                     (expand-file-name
14700                                      (concat "~/" (substring
14701                                                    gnus-nntp-server 1)))))
14702                              (list 'nnmh-get-new-mail nil)))
14703                       (t
14704                        (list 'nntp gnus-nntp-server)))))
14705
14706       (setq how (car gnus-select-method))
14707       (cond ((eq how 'nnspool)
14708              (require 'nnspool)
14709              (gnus-message 5 "Looking up local news spool..."))
14710             ((eq how 'nnmh)
14711              (require 'nnmh)
14712              (gnus-message 5 "Looking up mh spool..."))
14713             (t
14714              (require 'nntp)))
14715       (setq gnus-current-select-method gnus-select-method)
14716       (run-hooks 'gnus-open-server-hook)
14717       (or
14718        ;; gnus-open-server-hook might have opened it
14719        (gnus-server-opened gnus-select-method)
14720        (gnus-open-server gnus-select-method)
14721        (gnus-y-or-n-p
14722         (format
14723          "%s (%s) open error: '%s'.     Continue? "
14724          (car gnus-select-method) (cadr gnus-select-method)
14725          (gnus-status-message gnus-select-method)))
14726        (gnus-error 1 "Couldn't open server on %s"
14727                    (nth 1 gnus-select-method))))))
14728
14729 (defun gnus-check-group (group)
14730   "Try to make sure that the server where GROUP exists is alive."
14731   (let ((method (gnus-find-method-for-group group)))
14732     (or (gnus-server-opened method)
14733         (gnus-open-server method))))
14734
14735 (defun gnus-check-server (&optional method silent)
14736   "Check whether the connection to METHOD is down.
14737 If METHOD is nil, use `gnus-select-method'.
14738 If it is down, start it up (again)."
14739   (let ((method (or method gnus-select-method)))
14740     ;; Transform virtual server names into select methods.
14741     (when (stringp method)
14742       (setq method (gnus-server-to-method method)))
14743     (if (gnus-server-opened method)
14744         ;; The stream is already opened.
14745         t
14746       ;; Open the server.
14747       (unless silent
14748         (gnus-message 5 "Opening %s server%s..." (car method)
14749                       (if (equal (nth 1 method) "") ""
14750                         (format " on %s" (nth 1 method)))))
14751       (run-hooks 'gnus-open-server-hook)
14752       (prog1
14753           (gnus-open-server method)
14754         (unless silent
14755           (message ""))))))
14756
14757 (defun gnus-get-function (method function)
14758   "Return a function symbol based on METHOD and FUNCTION."
14759   ;; Translate server names into methods.
14760   (unless method
14761     (error "Attempted use of a nil select method"))
14762   (when (stringp method)
14763     (setq method (gnus-server-to-method method)))
14764   (let ((func (intern (format "%s-%s" (car method) function))))
14765     ;; If the functions isn't bound, we require the backend in
14766     ;; question.
14767     (unless (fboundp func)
14768       (require (car method))
14769       (unless (fboundp func)
14770         ;; This backend doesn't implement this function.
14771         (error "No such function: %s" func)))
14772     func))
14773
14774 \f
14775 ;;;
14776 ;;; Interface functions to the backends.
14777 ;;;
14778
14779 (defun gnus-open-server (method)
14780   "Open a connection to METHOD."
14781   (when (stringp method)
14782     (setq method (gnus-server-to-method method)))
14783   (let ((elem (assoc method gnus-opened-servers)))
14784     ;; If this method was previously denied, we just return nil.
14785     (if (eq (nth 1 elem) 'denied)
14786         (progn
14787           (gnus-message 1 "Denied server")
14788           nil)
14789       ;; Open the server.
14790       (let ((result
14791              (funcall (gnus-get-function method 'open-server)
14792                       (nth 1 method) (nthcdr 2 method))))
14793         ;; If this hasn't been opened before, we add it to the list.
14794         (unless elem
14795           (setq elem (list method nil)
14796                 gnus-opened-servers (cons elem gnus-opened-servers)))
14797         ;; Set the status of this server.
14798         (setcar (cdr elem) (if result 'ok 'denied))
14799         ;; Return the result from the "open" call.
14800         result))))
14801
14802 (defun gnus-close-server (method)
14803   "Close the connection to METHOD."
14804   (when (stringp method)
14805     (setq method (gnus-server-to-method method)))
14806   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14807
14808 (defun gnus-request-list (method)
14809   "Request the active file from METHOD."
14810   (when (stringp method)
14811     (setq method (gnus-server-to-method method)))
14812   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14813
14814 (defun gnus-request-list-newsgroups (method)
14815   "Request the newsgroups file from METHOD."
14816   (when (stringp method)
14817     (setq method (gnus-server-to-method method)))
14818   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14819
14820 (defun gnus-request-newgroups (date method)
14821   "Request all new groups since DATE from METHOD."
14822   (when (stringp method)
14823     (setq method (gnus-server-to-method method)))
14824   (funcall (gnus-get-function method 'request-newgroups)
14825            date (nth 1 method)))
14826
14827 (defun gnus-server-opened (method)
14828   "Check whether a connection to METHOD has been opened."
14829   (when (stringp method)
14830     (setq method (gnus-server-to-method method)))
14831   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14832
14833 (defun gnus-status-message (method)
14834   "Return the status message from METHOD.
14835 If METHOD is a string, it is interpreted as a group name.   The method
14836 this group uses will be queried."
14837   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14838                   method)))
14839     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14840
14841 (defun gnus-request-group (group &optional dont-check method)
14842   "Request GROUP.  If DONT-CHECK, no information is required."
14843   (let ((method (or method (gnus-find-method-for-group group))))
14844     (when (stringp method)
14845       (setq method (gnus-server-to-method method)))
14846     (funcall (gnus-get-function method 'request-group)
14847              (gnus-group-real-name group) (nth 1 method) dont-check)))
14848
14849 (defun gnus-request-asynchronous (group &optional articles)
14850   "Request that GROUP behave asynchronously.
14851 ARTICLES is the `data' of the group."
14852   (let ((method (gnus-find-method-for-group group)))
14853     (funcall (gnus-get-function method 'request-asynchronous)
14854              (gnus-group-real-name group) (nth 1 method) articles)))
14855
14856 (defun gnus-list-active-group (group)
14857   "Request active information on GROUP."
14858   (let ((method (gnus-find-method-for-group group))
14859         (func 'list-active-group))
14860     (when (gnus-check-backend-function func group)
14861       (funcall (gnus-get-function method func)
14862                (gnus-group-real-name group) (nth 1 method)))))
14863
14864 (defun gnus-request-group-description (group)
14865   "Request a description of GROUP."
14866   (let ((method (gnus-find-method-for-group group))
14867         (func 'request-group-description))
14868     (when (gnus-check-backend-function func group)
14869       (funcall (gnus-get-function method func)
14870                (gnus-group-real-name group) (nth 1 method)))))
14871
14872 (defun gnus-close-group (group)
14873   "Request the GROUP be closed."
14874   (let ((method (gnus-find-method-for-group group)))
14875     (funcall (gnus-get-function method 'close-group)
14876              (gnus-group-real-name group) (nth 1 method))))
14877
14878 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14879   "Request headers for ARTICLES in GROUP.
14880 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14881   (let ((method (gnus-find-method-for-group group)))
14882     (if (and gnus-use-cache (numberp (car articles)))
14883         (gnus-cache-retrieve-headers articles group fetch-old)
14884       (funcall (gnus-get-function method 'retrieve-headers)
14885                articles (gnus-group-real-name group) (nth 1 method)
14886                fetch-old))))
14887
14888 (defun gnus-retrieve-groups (groups method)
14889   "Request active information on GROUPS from METHOD."
14890   (when (stringp method)
14891     (setq method (gnus-server-to-method method)))
14892   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14893
14894 (defun gnus-request-type (group &optional article)
14895   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14896   (let ((method (gnus-find-method-for-group group)))
14897     (if (not (gnus-check-backend-function 'request-type (car method)))
14898         'unknown
14899       (funcall (gnus-get-function method 'request-type)
14900                (gnus-group-real-name group) article))))
14901
14902 (defun gnus-request-update-mark (group article mark)
14903   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14904   (let ((method (gnus-find-method-for-group group)))
14905     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14906         mark
14907       (funcall (gnus-get-function method 'request-update-mark)
14908                (gnus-group-real-name group) article mark))))
14909
14910 (defun gnus-request-article (article group &optional buffer)
14911   "Request the ARTICLE in GROUP.
14912 ARTICLE can either be an article number or an article Message-ID.
14913 If BUFFER, insert the article in that group."
14914   (let ((method (gnus-find-method-for-group group)))
14915     (funcall (gnus-get-function method 'request-article)
14916              article (gnus-group-real-name group) (nth 1 method) buffer)))
14917
14918 (defun gnus-request-head (article group)
14919   "Request the head of ARTICLE in GROUP."
14920   (let ((method (gnus-find-method-for-group group)))
14921     (funcall (gnus-get-function method 'request-head)
14922              article (gnus-group-real-name group) (nth 1 method))))
14923
14924 (defun gnus-request-body (article group)
14925   "Request the body of ARTICLE in GROUP."
14926   (let ((method (gnus-find-method-for-group group)))
14927     (funcall (gnus-get-function method 'request-body)
14928              article (gnus-group-real-name group) (nth 1 method))))
14929
14930 (defun gnus-request-post (method)
14931   "Post the current buffer using METHOD."
14932   (when (stringp method)
14933     (setq method (gnus-server-to-method method)))
14934   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14935
14936 (defun gnus-request-scan (group method)
14937   "Request a SCAN being performed in GROUP from METHOD.
14938 If GROUP is nil, all groups on METHOD are scanned."
14939   (let ((method (if group (gnus-find-method-for-group group) method)))
14940     (funcall (gnus-get-function method 'request-scan)
14941              (and group (gnus-group-real-name group)) (nth 1 method))))
14942
14943 (defsubst gnus-request-update-info (info method)
14944   "Request that METHOD update INFO."
14945   (when (stringp method)
14946     (setq method (gnus-server-to-method method)))
14947   (when (gnus-check-backend-function 'request-update-info (car method))
14948     (funcall (gnus-get-function method 'request-update-info)
14949              (gnus-group-real-name (gnus-info-group info))
14950              info (nth 1 method))))
14951
14952 (defun gnus-request-expire-articles (articles group &optional force)
14953   (let ((method (gnus-find-method-for-group group)))
14954     (funcall (gnus-get-function method 'request-expire-articles)
14955              articles (gnus-group-real-name group) (nth 1 method)
14956              force)))
14957
14958 (defun gnus-request-move-article
14959   (article group server accept-function &optional last)
14960   (let ((method (gnus-find-method-for-group group)))
14961     (funcall (gnus-get-function method 'request-move-article)
14962              article (gnus-group-real-name group)
14963              (nth 1 method) accept-function last)))
14964
14965 (defun gnus-request-accept-article (group method &optional last)
14966   ;; Make sure there's a newline at the end of the article.
14967   (when (stringp method)
14968     (setq method (gnus-server-to-method method)))
14969   (when (and (not method)
14970              (stringp group))
14971     (setq method (gnus-find-method-for-group group)))
14972   (goto-char (point-max))
14973   (unless (bolp)
14974     (insert "\n"))
14975   (let ((func (car (or method (gnus-find-method-for-group group)))))
14976     (funcall (intern (format "%s-request-accept-article" func))
14977              (if (stringp group) (gnus-group-real-name group) group)
14978              (cadr method)
14979              last)))
14980
14981 (defun gnus-request-replace-article (article group buffer)
14982   (let ((func (car (gnus-find-method-for-group group))))
14983     (funcall (intern (format "%s-request-replace-article" func))
14984              article (gnus-group-real-name group) buffer)))
14985
14986 (defun gnus-request-associate-buffer (group)
14987   (let ((method (gnus-find-method-for-group group)))
14988     (funcall (gnus-get-function method 'request-associate-buffer)
14989              (gnus-group-real-name group))))
14990
14991 (defun gnus-request-restore-buffer (article group)
14992   "Request a new buffer restored to the state of ARTICLE."
14993   (let ((method (gnus-find-method-for-group group)))
14994     (funcall (gnus-get-function method 'request-restore-buffer)
14995              article (gnus-group-real-name group) (nth 1 method))))
14996
14997 (defun gnus-request-create-group (group &optional method)
14998   (when (stringp method)
14999     (setq method (gnus-server-to-method method)))
15000   (let ((method (or method (gnus-find-method-for-group group))))
15001     (funcall (gnus-get-function method 'request-create-group)
15002              (gnus-group-real-name group) (nth 1 method))))
15003
15004 (defun gnus-request-delete-group (group &optional force)
15005   (let ((method (gnus-find-method-for-group group)))
15006     (funcall (gnus-get-function method 'request-delete-group)
15007              (gnus-group-real-name group) force (nth 1 method))))
15008
15009 (defun gnus-request-rename-group (group new-name)
15010   (let ((method (gnus-find-method-for-group group)))
15011     (funcall (gnus-get-function method 'request-rename-group)
15012              (gnus-group-real-name group)
15013              (gnus-group-real-name new-name) (nth 1 method))))
15014
15015 (defun gnus-member-of-valid (symbol group)
15016   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15017   (memq symbol (assoc
15018                 (symbol-name (car (gnus-find-method-for-group group)))
15019                 gnus-valid-select-methods)))
15020
15021 (defun gnus-method-option-p (method option)
15022   "Return non-nil if select METHOD has OPTION as a parameter."
15023   (when (stringp method)
15024     (setq method (gnus-server-to-method method)))
15025   (memq option (assoc (format "%s" (car method))
15026                       gnus-valid-select-methods)))
15027
15028 (defun gnus-server-extend-method (group method)
15029   ;; This function "extends" a virtual server.  If the server is
15030   ;; "hello", and the select method is ("hello" (my-var "something"))
15031   ;; in the group "alt.alt", this will result in a new virtual server
15032   ;; called "hello+alt.alt".
15033   (let ((entry
15034          (gnus-copy-sequence
15035           (if (equal (car method) "native") gnus-select-method
15036             (cdr (assoc (car method) gnus-server-alist))))))
15037     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15038     (nconc entry (cdr method))))
15039
15040 (defun gnus-find-method-for-group (group &optional info)
15041   "Find the select method that GROUP uses."
15042   (or gnus-override-method
15043       (and (not group)
15044            gnus-select-method)
15045       (let ((info (or info (gnus-get-info group)))
15046             method)
15047         (if (or (not info)
15048                 (not (setq method (gnus-info-method info)))
15049                 (equal method "native"))
15050             gnus-select-method
15051           (setq method
15052                 (cond ((stringp method)
15053                        (gnus-server-to-method method))
15054                       ((stringp (car method))
15055                        (gnus-server-extend-method group method))
15056                       (t
15057                        method)))
15058           (cond ((equal (cadr method) "")
15059                  method)
15060                 ((null (cadr method))
15061                  (list (car method) ""))
15062                 (t
15063                  (gnus-server-add-address method)))))))
15064
15065 (defun gnus-check-backend-function (func group)
15066   "Check whether GROUP supports function FUNC."
15067   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15068                   group)))
15069     (fboundp (intern (format "%s-%s" method func)))))
15070
15071 (defun gnus-methods-using (feature)
15072   "Find all methods that have FEATURE."
15073   (let ((valids gnus-valid-select-methods)
15074         outs)
15075     (while valids
15076       (if (memq feature (car valids))
15077           (setq outs (cons (car valids) outs)))
15078       (setq valids (cdr valids)))
15079     outs))
15080
15081 \f
15082 ;;;
15083 ;;; Active & Newsrc File Handling
15084 ;;;
15085
15086 (defun gnus-setup-news (&optional rawfile level dont-connect)
15087   "Setup news information.
15088 If RAWFILE is non-nil, the .newsrc file will also be read.
15089 If LEVEL is non-nil, the news will be set up at level LEVEL."
15090   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15091
15092     (when init 
15093       ;; Clear some variables to re-initialize news information.
15094       (setq gnus-newsrc-alist nil
15095             gnus-active-hashtb nil)
15096       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15097       (gnus-read-newsrc-file rawfile))
15098
15099     (when (and (not (assoc "archive" gnus-server-alist))
15100                gnus-message-archive-method)
15101       (push (cons "archive" gnus-message-archive-method)
15102             gnus-server-alist))
15103
15104     ;; If we don't read the complete active file, we fill in the
15105     ;; hashtb here.
15106     (if (or (null gnus-read-active-file)
15107             (eq gnus-read-active-file 'some))
15108         (gnus-update-active-hashtb-from-killed))
15109
15110     ;; Read the active file and create `gnus-active-hashtb'.
15111     ;; If `gnus-read-active-file' is nil, then we just create an empty
15112     ;; hash table.  The partial filling out of the hash table will be
15113     ;; done in `gnus-get-unread-articles'.
15114     (and gnus-read-active-file
15115          (not level)
15116          (gnus-read-active-file))
15117
15118     (or gnus-active-hashtb
15119         (setq gnus-active-hashtb (make-vector 4095 0)))
15120
15121     ;; Initialize the cache.
15122     (when gnus-use-cache
15123       (gnus-cache-open))
15124
15125     ;; Possibly eval the dribble file.
15126     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15127
15128     (gnus-update-format-specifications)
15129
15130     ;; See whether we need to read the description file.
15131     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15132              (not gnus-description-hashtb)
15133              (not dont-connect)
15134              gnus-read-active-file)
15135         (gnus-read-all-descriptions-files))
15136
15137     ;; Find new newsgroups and treat them.
15138     (if (and init gnus-check-new-newsgroups (not level)
15139              (gnus-check-server gnus-select-method))
15140         (gnus-find-new-newsgroups))
15141
15142     ;; We might read in new NoCeM messages here.
15143     (when (and gnus-use-nocem 
15144                (not level)
15145                (not dont-connect))
15146       (gnus-nocem-scan-groups))
15147
15148     ;; Find the number of unread articles in each non-dead group.
15149     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15150       (gnus-get-unread-articles level))
15151
15152     (if (and init gnus-check-bogus-newsgroups
15153              gnus-read-active-file (not level)
15154              (gnus-server-opened gnus-select-method))
15155         (gnus-check-bogus-newsgroups))))
15156
15157 (defun gnus-find-new-newsgroups (&optional arg)
15158   "Search for new newsgroups and add them.
15159 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15160 The `-n' option line from .newsrc is respected.
15161 If ARG (the prefix), use the `ask-server' method to query
15162 the server for new groups."
15163   (interactive "P")
15164   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15165                        (null gnus-read-active-file)
15166                        (eq gnus-read-active-file 'some))
15167                    'ask-server gnus-check-new-newsgroups)))
15168     (unless (gnus-check-first-time-used)
15169       (if (or (consp check)
15170               (eq check 'ask-server))
15171           ;; Ask the server for new groups.
15172           (gnus-ask-server-for-new-groups)
15173         ;; Go through the active hashtb and look for new groups.
15174         (let ((groups 0)
15175               group new-newsgroups)
15176           (gnus-message 5 "Looking for new newsgroups...")
15177           (unless gnus-have-read-active-file
15178             (gnus-read-active-file))
15179           (setq gnus-newsrc-last-checked-date (current-time-string))
15180           (unless gnus-killed-hashtb
15181             (gnus-make-hashtable-from-killed))
15182           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15183           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15184           (mapatoms
15185            (lambda (sym)
15186              (if (or (null (setq group (symbol-name sym)))
15187                      (not (boundp sym))
15188                      (null (symbol-value sym))
15189                      (gnus-gethash group gnus-killed-hashtb)
15190                      (gnus-gethash group gnus-newsrc-hashtb))
15191                  ()
15192                (let ((do-sub (gnus-matches-options-n group)))
15193                  (cond
15194                   ((eq do-sub 'subscribe)
15195                    (setq groups (1+ groups))
15196                    (gnus-sethash group group gnus-killed-hashtb)
15197                    (funcall gnus-subscribe-options-newsgroup-method group))
15198                   ((eq do-sub 'ignore)
15199                    nil)
15200                   (t
15201                    (setq groups (1+ groups))
15202                    (gnus-sethash group group gnus-killed-hashtb)
15203                    (if gnus-subscribe-hierarchical-interactive
15204                        (setq new-newsgroups (cons group new-newsgroups))
15205                      (funcall gnus-subscribe-newsgroup-method group)))))))
15206            gnus-active-hashtb)
15207           (when new-newsgroups
15208             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15209           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15210           (if (> groups 0)
15211               (gnus-message 6 "%d new newsgroup%s arrived."
15212                             groups (if (> groups 1) "s have" " has"))
15213             (gnus-message 6 "No new newsgroups.")))))))
15214
15215 (defun gnus-matches-options-n (group)
15216   ;; Returns `subscribe' if the group is to be unconditionally
15217   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15218   ;; no match for the group.
15219
15220   ;; First we check the two user variables.
15221   (cond
15222    ((and gnus-options-subscribe
15223          (string-match gnus-options-subscribe group))
15224     'subscribe)
15225    ((and gnus-auto-subscribed-groups
15226          (string-match gnus-auto-subscribed-groups group))
15227     'subscribe)
15228    ((and gnus-options-not-subscribe
15229          (string-match gnus-options-not-subscribe group))
15230     'ignore)
15231    ;; Then we go through the list that was retrieved from the .newsrc
15232    ;; file.  This list has elements on the form
15233    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15234    ;; is in the reverse order of the options line) is returned.
15235    (t
15236     (let ((regs gnus-newsrc-options-n))
15237       (while (and regs
15238                   (not (string-match (caar regs) group)))
15239         (setq regs (cdr regs)))
15240       (and regs (cdar regs))))))
15241
15242 (defun gnus-ask-server-for-new-groups ()
15243   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15244          (methods (cons gnus-select-method
15245                         (nconc
15246                          (when gnus-message-archive-method
15247                            (list "archive"))
15248                          (append
15249                           (and (consp gnus-check-new-newsgroups)
15250                                gnus-check-new-newsgroups)
15251                           gnus-secondary-select-methods))))
15252          (groups 0)
15253          (new-date (current-time-string))
15254          group new-newsgroups got-new method hashtb
15255          gnus-override-subscribe-method)
15256     ;; Go through both primary and secondary select methods and
15257     ;; request new newsgroups.
15258     (while (setq method (gnus-server-get-method nil (pop methods)))
15259       (setq new-newsgroups nil)
15260       (setq gnus-override-subscribe-method method)
15261       (when (and (gnus-check-server method)
15262                  (gnus-request-newgroups date method))
15263         (save-excursion
15264           (setq got-new t)
15265           (setq hashtb (gnus-make-hashtable 100))
15266           (set-buffer nntp-server-buffer)
15267           ;; Enter all the new groups into a hashtable.
15268           (gnus-active-to-gnus-format method hashtb 'ignore))
15269         ;; Now all new groups from `method' are in `hashtb'.
15270         (mapatoms
15271          (lambda (group-sym)
15272            (if (or (null (setq group (symbol-name group-sym)))
15273                    (not (boundp group-sym))
15274                    (null (symbol-value group-sym))
15275                    (gnus-gethash group gnus-newsrc-hashtb)
15276                    (member group gnus-zombie-list)
15277                    (member group gnus-killed-list))
15278                ;; The group is already known.
15279                ()
15280              ;; Make this group active.
15281              (when (symbol-value group-sym)
15282                (gnus-set-active group (symbol-value group-sym)))
15283              ;; Check whether we want it or not.
15284              (let ((do-sub (gnus-matches-options-n group)))
15285                (cond
15286                 ((eq do-sub 'subscribe)
15287                  (incf groups)
15288                  (gnus-sethash group group gnus-killed-hashtb)
15289                  (funcall gnus-subscribe-options-newsgroup-method group))
15290                 ((eq do-sub 'ignore)
15291                  nil)
15292                 (t
15293                  (incf groups)
15294                  (gnus-sethash group group gnus-killed-hashtb)
15295                  (if gnus-subscribe-hierarchical-interactive
15296                      (push group new-newsgroups)
15297                    (funcall gnus-subscribe-newsgroup-method group)))))))
15298          hashtb))
15299       (when new-newsgroups
15300         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15301     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15302     (when (> groups 0)
15303       (gnus-message 6 "%d new newsgroup%s arrived."
15304                     groups (if (> groups 1) "s have" " has")))
15305     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15306     got-new))
15307
15308 (defun gnus-check-first-time-used ()
15309   (if (or (> (length gnus-newsrc-alist) 1)
15310           (file-exists-p gnus-startup-file)
15311           (file-exists-p (concat gnus-startup-file ".el"))
15312           (file-exists-p (concat gnus-startup-file ".eld")))
15313       nil
15314     (gnus-message 6 "First time user; subscribing you to default groups")
15315     (unless gnus-have-read-active-file
15316       (gnus-read-active-file))
15317     (setq gnus-newsrc-last-checked-date (current-time-string))
15318     (let ((groups gnus-default-subscribed-newsgroups)
15319           group)
15320       (if (eq groups t)
15321           nil
15322         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15323         (mapatoms
15324          (lambda (sym)
15325            (if (null (setq group (symbol-name sym)))
15326                ()
15327              (let ((do-sub (gnus-matches-options-n group)))
15328                (cond
15329                 ((eq do-sub 'subscribe)
15330                  (gnus-sethash group group gnus-killed-hashtb)
15331                  (funcall gnus-subscribe-options-newsgroup-method group))
15332                 ((eq do-sub 'ignore)
15333                  nil)
15334                 (t
15335                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15336          gnus-active-hashtb)
15337         (while groups
15338           (if (gnus-active (car groups))
15339               (gnus-group-change-level
15340                (car groups) gnus-level-default-subscribed gnus-level-killed))
15341           (setq groups (cdr groups)))
15342         (gnus-group-make-help-group)
15343         (and gnus-novice-user
15344              (gnus-message 7 "`A k' to list killed groups"))))))
15345
15346 (defun gnus-subscribe-group (group previous &optional method)
15347   (gnus-group-change-level
15348    (if method
15349        (list t group gnus-level-default-subscribed nil nil method)
15350      group)
15351    gnus-level-default-subscribed gnus-level-killed previous t))
15352
15353 ;; `gnus-group-change-level' is the fundamental function for changing
15354 ;; subscription levels of newsgroups.  This might mean just changing
15355 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15356 ;; again, which subscribes/unsubscribes a group, which is equally
15357 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15358 ;; from 8-9 to 1-7 means that you remove the group from the list of
15359 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15360 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15361 ;; which is trivial.
15362 ;; ENTRY can either be a string (newsgroup name) or a list (if
15363 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15364 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15365 ;; entries.
15366 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15367 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15368 ;; after.
15369 (defun gnus-group-change-level (entry level &optional oldlevel
15370                                       previous fromkilled)
15371   (let (group info active num)
15372     ;; Glean what info we can from the arguments
15373     (if (consp entry)
15374         (if fromkilled (setq group (nth 1 entry))
15375           (setq group (car (nth 2 entry))))
15376       (setq group entry))
15377     (if (and (stringp entry)
15378              oldlevel
15379              (< oldlevel gnus-level-zombie))
15380         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15381     (if (and (not oldlevel)
15382              (consp entry))
15383         (setq oldlevel (gnus-info-level (nth 2 entry)))
15384       (setq oldlevel (or oldlevel 9)))
15385     (if (stringp previous)
15386         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15387
15388     (if (and (>= oldlevel gnus-level-zombie)
15389              (gnus-gethash group gnus-newsrc-hashtb))
15390         ;; We are trying to subscribe a group that is already
15391         ;; subscribed.
15392         ()                              ; Do nothing.
15393
15394       (or (gnus-ephemeral-group-p group)
15395           (gnus-dribble-enter
15396            (format "(gnus-group-change-level %S %S %S %S %S)"
15397                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15398
15399       ;; Then we remove the newgroup from any old structures, if needed.
15400       ;; If the group was killed, we remove it from the killed or zombie
15401       ;; list.  If not, and it is in fact going to be killed, we remove
15402       ;; it from the newsrc hash table and assoc.
15403       (cond
15404        ((>= oldlevel gnus-level-zombie)
15405         (if (= oldlevel gnus-level-zombie)
15406             (setq gnus-zombie-list (delete group gnus-zombie-list))
15407           (setq gnus-killed-list (delete group gnus-killed-list))))
15408        (t
15409         (if (and (>= level gnus-level-zombie)
15410                  entry)
15411             (progn
15412               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15413               (if (nth 3 entry)
15414                   (setcdr (gnus-gethash (car (nth 3 entry))
15415                                         gnus-newsrc-hashtb)
15416                           (cdr entry)))
15417               (setcdr (cdr entry) (cdddr entry))))))
15418
15419       ;; Finally we enter (if needed) the list where it is supposed to
15420       ;; go, and change the subscription level.  If it is to be killed,
15421       ;; we enter it into the killed or zombie list.
15422       (cond 
15423        ((>= level gnus-level-zombie)
15424         ;; Remove from the hash table.
15425         (gnus-sethash group nil gnus-newsrc-hashtb)
15426         ;; We do not enter foreign groups into the list of dead
15427         ;; groups.
15428         (unless (gnus-group-foreign-p group)
15429           (if (= level gnus-level-zombie)
15430               (setq gnus-zombie-list (cons group gnus-zombie-list))
15431             (setq gnus-killed-list (cons group gnus-killed-list)))))
15432        (t
15433         ;; If the list is to be entered into the newsrc assoc, and
15434         ;; it was killed, we have to create an entry in the newsrc
15435         ;; hashtb format and fix the pointers in the newsrc assoc.
15436         (if (< oldlevel gnus-level-zombie)
15437             ;; It was alive, and it is going to stay alive, so we
15438             ;; just change the level and don't change any pointers or
15439             ;; hash table entries.
15440             (setcar (cdaddr entry) level)
15441           (if (listp entry)
15442               (setq info (cdr entry)
15443                     num (car entry))
15444             (setq active (gnus-active group))
15445             (setq num
15446                   (if active (- (1+ (cdr active)) (car active)) t))
15447             ;; Check whether the group is foreign.  If so, the
15448             ;; foreign select method has to be entered into the
15449             ;; info.
15450             (let ((method (or gnus-override-subscribe-method
15451                               (gnus-group-method group))))
15452               (if (eq method gnus-select-method)
15453                   (setq info (list group level nil))
15454                 (setq info (list group level nil nil method)))))
15455           (unless previous
15456             (setq previous
15457                   (let ((p gnus-newsrc-alist))
15458                     (while (cddr p)
15459                       (setq p (cdr p)))
15460                     p)))
15461           (setq entry (cons info (cddr previous)))
15462           (if (cdr previous)
15463               (progn
15464                 (setcdr (cdr previous) entry)
15465                 (gnus-sethash group (cons num (cdr previous))
15466                               gnus-newsrc-hashtb))
15467             (setcdr previous entry)
15468             (gnus-sethash group (cons num previous)
15469                           gnus-newsrc-hashtb))
15470           (when (cdr entry)
15471             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15472       (when gnus-group-change-level-function
15473         (funcall gnus-group-change-level-function group level oldlevel)))))
15474
15475 (defun gnus-kill-newsgroup (newsgroup)
15476   "Obsolete function.  Kills a newsgroup."
15477   (gnus-group-change-level
15478    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15479
15480 (defun gnus-check-bogus-newsgroups (&optional confirm)
15481   "Remove bogus newsgroups.
15482 If CONFIRM is non-nil, the user has to confirm the deletion of every
15483 newsgroup."
15484   (let ((newsrc (cdr gnus-newsrc-alist))
15485         bogus group entry info)
15486     (gnus-message 5 "Checking bogus newsgroups...")
15487     (unless gnus-have-read-active-file
15488       (gnus-read-active-file))
15489     (when (member gnus-select-method gnus-have-read-active-file)
15490       ;; Find all bogus newsgroup that are subscribed.
15491       (while newsrc
15492         (setq info (pop newsrc)
15493               group (gnus-info-group info))
15494         (unless (or (gnus-active group) ; Active
15495                     (gnus-info-method info) ; Foreign
15496                     (and confirm
15497                          (not (gnus-y-or-n-p
15498                                (format "Remove bogus newsgroup: %s " group)))))
15499           ;; Found a bogus newsgroup.
15500           (push group bogus)))
15501       ;; Remove all bogus subscribed groups by first killing them, and
15502       ;; then removing them from the list of killed groups.
15503       (while bogus
15504         (when (setq entry (gnus-gethash (setq group (pop bogus))
15505                                         gnus-newsrc-hashtb))
15506           (gnus-group-change-level entry gnus-level-killed)
15507           (setq gnus-killed-list (delete group gnus-killed-list))))
15508       ;; Then we remove all bogus groups from the list of killed and
15509       ;; zombie groups.  They are are removed without confirmation.
15510       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15511             killed)
15512         (while dead-lists
15513           (setq killed (symbol-value (car dead-lists)))
15514           (while killed
15515             (unless (gnus-active (setq group (pop killed)))
15516               ;; The group is bogus.
15517               ;; !!!Slow as hell.
15518               (set (car dead-lists)
15519                    (delete group (symbol-value (car dead-lists))))))
15520           (setq dead-lists (cdr dead-lists))))
15521       (gnus-message 5 "Checking bogus newsgroups...done"))))
15522
15523 (defun gnus-check-duplicate-killed-groups ()
15524   "Remove duplicates from the list of killed groups."
15525   (interactive)
15526   (let ((killed gnus-killed-list))
15527     (while killed
15528       (gnus-message 9 "%d" (length killed))
15529       (setcdr killed (delete (car killed) (cdr killed)))
15530       (setq killed (cdr killed)))))
15531
15532 ;; We want to inline a function from gnus-cache, so we cheat here:
15533 (eval-when-compile
15534   (provide 'gnus)
15535   (require 'gnus-cache))
15536
15537 (defun gnus-get-unread-articles-in-group (info active &optional update)
15538   (when active
15539     ;; Allow the backend to update the info in the group.
15540     (when (and update 
15541                (gnus-request-update-info
15542                 info (gnus-find-method-for-group (gnus-info-group info))))
15543       (gnus-activate-group (gnus-info-group info) nil t))
15544     (let* ((range (gnus-info-read info))
15545            (num 0))
15546       ;; If a cache is present, we may have to alter the active info.
15547       (when (and gnus-use-cache info)
15548         (inline (gnus-cache-possibly-alter-active 
15549                  (gnus-info-group info) active)))
15550       ;; Modify the list of read articles according to what articles
15551       ;; are available; then tally the unread articles and add the
15552       ;; number to the group hash table entry.
15553       (cond
15554        ((zerop (cdr active))
15555         (setq num 0))
15556        ((not range)
15557         (setq num (- (1+ (cdr active)) (car active))))
15558        ((not (listp (cdr range)))
15559         ;; Fix a single (num . num) range according to the
15560         ;; active hash table.
15561         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15562         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15563         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15564         ;; Compute number of unread articles.
15565         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15566        (t
15567         ;; The read list is a list of ranges.  Fix them according to
15568         ;; the active hash table.
15569         ;; First peel off any elements that are below the lower
15570         ;; active limit.
15571         (while (and (cdr range)
15572                     (>= (car active)
15573                         (or (and (atom (cadr range)) (cadr range))
15574                             (caadr range))))
15575           (if (numberp (car range))
15576               (setcar range
15577                       (cons (car range)
15578                             (or (and (numberp (cadr range))
15579                                      (cadr range))
15580                                 (cdadr range))))
15581             (setcdr (car range)
15582                     (or (and (numberp (nth 1 range)) (nth 1 range))
15583                         (cdadr range))))
15584           (setcdr range (cddr range)))
15585         ;; Adjust the first element to be the same as the lower limit.
15586         (if (and (not (atom (car range)))
15587                  (< (cdar range) (car active)))
15588             (setcdr (car range) (1- (car active))))
15589         ;; Then we want to peel off any elements that are higher
15590         ;; than the upper active limit.
15591         (let ((srange range))
15592           ;; Go past all legal elements.
15593           (while (and (cdr srange)
15594                       (<= (or (and (atom (cadr srange))
15595                                    (cadr srange))
15596                               (caadr srange)) (cdr active)))
15597             (setq srange (cdr srange)))
15598           (if (cdr srange)
15599               ;; Nuke all remaining illegal elements.
15600               (setcdr srange nil))
15601
15602           ;; Adjust the final element.
15603           (if (and (not (atom (car srange)))
15604                    (> (cdar srange) (cdr active)))
15605               (setcdr (car srange) (cdr active))))
15606         ;; Compute the number of unread articles.
15607         (while range
15608           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15609                                       (cdar range)))
15610                               (or (and (atom (car range)) (car range))
15611                                   (caar range)))))
15612           (setq range (cdr range)))
15613         (setq num (max 0 (- (cdr active) num)))))
15614       ;; Set the number of unread articles.
15615       (when info
15616         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15617       num)))
15618
15619 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15620 ;; and compute how many unread articles there are in each group.
15621 (defun gnus-get-unread-articles (&optional level)
15622   (let* ((newsrc (cdr gnus-newsrc-alist))
15623          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15624          (foreign-level
15625           (min
15626            (cond ((and gnus-activate-foreign-newsgroups
15627                        (not (numberp gnus-activate-foreign-newsgroups)))
15628                   (1+ gnus-level-subscribed))
15629                  ((numberp gnus-activate-foreign-newsgroups)
15630                   gnus-activate-foreign-newsgroups)
15631                  (t 0))
15632            level))
15633          info group active method)
15634     (gnus-message 5 "Checking new news...")
15635
15636     (while newsrc
15637       (setq active (gnus-active (setq group (gnus-info-group
15638                                              (setq info (pop newsrc))))))
15639
15640       ;; Check newsgroups.  If the user doesn't want to check them, or
15641       ;; they can't be checked (for instance, if the news server can't
15642       ;; be reached) we just set the number of unread articles in this
15643       ;; newsgroup to t.  This means that Gnus thinks that there are
15644       ;; unread articles, but it has no idea how many.
15645       (if (and (setq method (gnus-info-method info))
15646                (not (gnus-server-equal
15647                      gnus-select-method
15648                      (setq method (gnus-server-get-method nil method))))
15649                (not (gnus-secondary-method-p method)))
15650           ;; These groups are foreign.  Check the level.
15651           (when (<= (gnus-info-level info) foreign-level)
15652             (setq active (gnus-activate-group group 'scan))
15653             (unless (inline (gnus-virtual-group-p group))
15654               (inline (gnus-close-group group))))
15655
15656         ;; These groups are native or secondary.
15657         (when (and (<= (gnus-info-level info) level)
15658                    (not gnus-read-active-file))
15659           (setq active (gnus-activate-group group 'scan))
15660           (inline (gnus-close-group group))))
15661
15662       (if active
15663           (inline (gnus-get-unread-articles-in-group 
15664                    info active
15665                    (and method
15666                         (fboundp (intern (concat (symbol-name (car method))
15667                                                  "-request-update-info"))))))
15668         ;; The group couldn't be reached, so we nix out the number of
15669         ;; unread articles and stuff.
15670         (gnus-set-active group nil)
15671         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15672
15673     (gnus-message 5 "Checking new news...done")))
15674
15675 ;; Create a hash table out of the newsrc alist.  The `car's of the
15676 ;; alist elements are used as keys.
15677 (defun gnus-make-hashtable-from-newsrc-alist ()
15678   (let ((alist gnus-newsrc-alist)
15679         (ohashtb gnus-newsrc-hashtb)
15680         prev)
15681     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15682     (setq alist
15683           (setq prev (setq gnus-newsrc-alist
15684                            (if (equal (caar gnus-newsrc-alist)
15685                                       "dummy.group")
15686                                gnus-newsrc-alist
15687                              (cons (list "dummy.group" 0 nil) alist)))))
15688     (while alist
15689       (gnus-sethash
15690        (caar alist)
15691        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15692              prev)
15693        gnus-newsrc-hashtb)
15694       (setq prev alist
15695             alist (cdr alist)))))
15696
15697 (defun gnus-make-hashtable-from-killed ()
15698   "Create a hash table from the killed and zombie lists."
15699   (let ((lists '(gnus-killed-list gnus-zombie-list))
15700         list)
15701     (setq gnus-killed-hashtb
15702           (gnus-make-hashtable
15703            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15704     (while (setq list (pop lists))
15705       (setq list (symbol-value list))
15706       (while list
15707         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15708
15709 (defun gnus-activate-group (group &optional scan dont-check)
15710   ;; Check whether a group has been activated or not.
15711   ;; If SCAN, request a scan of that group as well.
15712   (let ((method (gnus-find-method-for-group group))
15713         active)
15714     (and (gnus-check-server method)
15715          ;; We escape all bugs and quit here to make it possible to
15716          ;; continue if a group is so out-there that it reports bugs
15717          ;; and stuff.
15718          (progn
15719            (and scan
15720                 (gnus-check-backend-function 'request-scan (car method))
15721                 (gnus-request-scan group method))
15722            t)
15723          (condition-case ()
15724              (gnus-request-group group dont-check)
15725         ;   (error nil)
15726            (quit nil))
15727          (save-excursion
15728            (set-buffer nntp-server-buffer)
15729            (goto-char (point-min))
15730            ;; Parse the result we got from `gnus-request-group'.
15731            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15732                 (progn
15733                   (goto-char (match-beginning 1))
15734                   (gnus-set-active
15735                    group (setq active (cons (read (current-buffer))
15736                                             (read (current-buffer)))))
15737                   ;; Return the new active info.
15738                   active))))))
15739
15740 (defun gnus-update-read-articles (group unread)
15741   "Update the list of read and ticked articles in GROUP using the
15742 UNREAD and TICKED lists.
15743 Note: UNSELECTED has to be sorted over `<'.
15744 Returns whether the updating was successful."
15745   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15746          (entry (gnus-gethash group gnus-newsrc-hashtb))
15747          (info (nth 2 entry))
15748          (prev 1)
15749          (unread (sort (copy-sequence unread) '<))
15750          read)
15751     (if (or (not info) (not active))
15752         ;; There is no info on this group if it was, in fact,
15753         ;; killed.  Gnus stores no information on killed groups, so
15754         ;; there's nothing to be done.
15755         ;; One could store the information somewhere temporarily,
15756         ;; perhaps...  Hmmm...
15757         ()
15758       ;; Remove any negative articles numbers.
15759       (while (and unread (< (car unread) 0))
15760         (setq unread (cdr unread)))
15761       ;; Remove any expired article numbers
15762       (while (and unread (< (car unread) (car active)))
15763         (setq unread (cdr unread)))
15764       ;; Compute the ranges of read articles by looking at the list of
15765       ;; unread articles.
15766       (while unread
15767         (if (/= (car unread) prev)
15768             (setq read (cons (if (= prev (1- (car unread))) prev
15769                                (cons prev (1- (car unread)))) read)))
15770         (setq prev (1+ (car unread)))
15771         (setq unread (cdr unread)))
15772       (when (<= prev (cdr active))
15773         (setq read (cons (cons prev (cdr active)) read)))
15774       ;; Enter this list into the group info.
15775       (gnus-info-set-read
15776        info (if (> (length read) 1) (nreverse read) read))
15777       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15778       (gnus-get-unread-articles-in-group info (gnus-active group))
15779       t)))
15780
15781 (defun gnus-make-articles-unread (group articles)
15782   "Mark ARTICLES in GROUP as unread."
15783   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15784                           (gnus-gethash (gnus-group-real-name group)
15785                                         gnus-newsrc-hashtb))))
15786          (ranges (gnus-info-read info))
15787          news article)
15788     (while articles
15789       (when (gnus-member-of-range
15790              (setq article (pop articles)) ranges)
15791         (setq news (cons article news))))
15792     (when news
15793       (gnus-info-set-read
15794        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15795       (gnus-group-update-group group t))))
15796
15797 ;; Enter all dead groups into the hashtb.
15798 (defun gnus-update-active-hashtb-from-killed ()
15799   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15800         (lists (list gnus-killed-list gnus-zombie-list))
15801         killed)
15802     (while lists
15803       (setq killed (car lists))
15804       (while killed
15805         (gnus-sethash (car killed) nil hashtb)
15806         (setq killed (cdr killed)))
15807       (setq lists (cdr lists)))))
15808
15809 ;; Get the active file(s) from the backend(s).
15810 (defun gnus-read-active-file ()
15811   (gnus-group-set-mode-line)
15812   (let ((methods 
15813          (append
15814           (if (gnus-check-server gnus-select-method)
15815               ;; The native server is available.
15816               (cons gnus-select-method gnus-secondary-select-methods)
15817             ;; The native server is down, so we just do the
15818             ;; secondary ones.
15819             gnus-secondary-select-methods)
15820           ;; Also read from the archive server.
15821           (when gnus-message-archive-method
15822             (list "archive"))))
15823         list-type)
15824     (setq gnus-have-read-active-file nil)
15825     (save-excursion
15826       (set-buffer nntp-server-buffer)
15827       (while methods
15828         (let* ((method (if (stringp (car methods))
15829                            (gnus-server-get-method nil (car methods))
15830                          (car methods)))
15831                (where (nth 1 method))
15832                (mesg (format "Reading active file%s via %s..."
15833                              (if (and where (not (zerop (length where))))
15834                                  (concat " from " where) "")
15835                              (car method))))
15836           (gnus-message 5 mesg)
15837           (when (gnus-check-server method)
15838             ;; Request that the backend scan its incoming messages.
15839             (and (gnus-check-backend-function 'request-scan (car method))
15840                  (gnus-request-scan nil method))
15841             (cond
15842              ((and (eq gnus-read-active-file 'some)
15843                    (gnus-check-backend-function 'retrieve-groups (car method)))
15844               (let ((newsrc (cdr gnus-newsrc-alist))
15845                     (gmethod (gnus-server-get-method nil method))
15846                     groups info)
15847                 (while (setq info (pop newsrc))
15848                   (when (gnus-server-equal
15849                          (gnus-find-method-for-group 
15850                           (gnus-info-group info) info)
15851                          gmethod)
15852                     (push (gnus-group-real-name (gnus-info-group info)) 
15853                           groups)))
15854                 (when groups
15855                   (gnus-check-server method)
15856                   (setq list-type (gnus-retrieve-groups groups method))
15857                   (cond
15858                    ((not list-type)
15859                     (gnus-error
15860                      1.2 "Cannot read partial active file from %s server."
15861                      (car method)))
15862                    ((eq list-type 'active)
15863                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15864                    (t
15865                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15866              (t
15867               (if (not (gnus-request-list method))
15868                   (unless (equal method gnus-message-archive-method)
15869                     (gnus-error 1 "Cannot read active file from %s server."
15870                                 (car method)))
15871                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15872                 ;; We mark this active file as read.
15873                 (push method gnus-have-read-active-file)
15874                 (gnus-message 5 "%sdone" mesg))))))
15875         (setq methods (cdr methods))))))
15876
15877 ;; Read an active file and place the results in `gnus-active-hashtb'.
15878 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15879   (unless method
15880     (setq method gnus-select-method))
15881   (let ((cur (current-buffer))
15882         (hashtb (or hashtb
15883                     (if (and gnus-active-hashtb
15884                              (not (equal method gnus-select-method)))
15885                         gnus-active-hashtb
15886                       (setq gnus-active-hashtb
15887                             (if (equal method gnus-select-method)
15888                                 (gnus-make-hashtable
15889                                  (count-lines (point-min) (point-max)))
15890                               (gnus-make-hashtable 4096)))))))
15891     ;; Delete unnecessary lines.
15892     (goto-char (point-min))
15893     (while (search-forward "\nto." nil t)
15894       (delete-region (1+ (match-beginning 0))
15895                      (progn (forward-line 1) (point))))
15896     (or (string= gnus-ignored-newsgroups "")
15897         (progn
15898           (goto-char (point-min))
15899           (delete-matching-lines gnus-ignored-newsgroups)))
15900     ;; Make the group names readable as a lisp expression even if they
15901     ;; contain special characters.
15902     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15903     (goto-char (point-max))
15904     (while (re-search-backward "[][';?()#]" nil t)
15905       (insert ?\\))
15906     ;; If these are groups from a foreign select method, we insert the
15907     ;; group prefix in front of the group names.
15908     (and method (not (gnus-server-equal
15909                       (gnus-server-get-method nil method)
15910                       (gnus-server-get-method nil gnus-select-method)))
15911          (let ((prefix (gnus-group-prefixed-name "" method)))
15912            (goto-char (point-min))
15913            (while (and (not (eobp))
15914                        (progn (insert prefix)
15915                               (zerop (forward-line 1)))))))
15916     ;; Store the active file in a hash table.
15917     (goto-char (point-min))
15918     (if (string-match "%[oO]" gnus-group-line-format)
15919         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15920         ;; If we want information on moderated groups, we use this
15921         ;; loop...
15922         (let* ((mod-hashtb (make-vector 7 0))
15923                (m (intern "m" mod-hashtb))
15924                group max min)
15925           (while (not (eobp))
15926             (condition-case nil
15927                 (progn
15928                   (narrow-to-region (point) (gnus-point-at-eol))
15929                   (setq group (let ((obarray hashtb)) (read cur)))
15930                   (if (and (numberp (setq max (read cur)))
15931                            (numberp (setq min (read cur)))
15932                            (progn
15933                              (skip-chars-forward " \t")
15934                              (not
15935                               (or (= (following-char) ?=)
15936                                   (= (following-char) ?x)
15937                                   (= (following-char) ?j)))))
15938                       (set group (cons min max))
15939                     (set group nil))
15940                   ;; Enter moderated groups into a list.
15941                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15942                       (setq gnus-moderated-list
15943                             (cons (symbol-name group) gnus-moderated-list))))
15944               (error
15945                (and group
15946                     (symbolp group)
15947                     (set group nil))))
15948             (widen)
15949             (forward-line 1)))
15950       ;; And if we do not care about moderation, we use this loop,
15951       ;; which is faster.
15952       (let (group max min)
15953         (while (not (eobp))
15954           (condition-case ()
15955               (progn
15956                 (narrow-to-region (point) (gnus-point-at-eol))
15957                 ;; group gets set to a symbol interned in the hash table
15958                 ;; (what a hack!!) - jwz
15959                 (setq group (let ((obarray hashtb)) (read cur)))
15960                 (if (and (numberp (setq max (read cur)))
15961                          (numberp (setq min (read cur)))
15962                          (progn
15963                            (skip-chars-forward " \t")
15964                            (not
15965                             (or (= (following-char) ?=)
15966                                 (= (following-char) ?x)
15967                                 (= (following-char) ?j)))))
15968                     (set group (cons min max))
15969                   (set group nil)))
15970             (error
15971              (progn
15972                (and group
15973                     (symbolp group)
15974                     (set group nil))
15975                (or ignore-errors
15976                    (gnus-message 3 "Warning - illegal active: %s"
15977                                  (buffer-substring
15978                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15979           (widen)
15980           (forward-line 1))))))
15981
15982 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15983   ;; Parse a "groups" active file.
15984   (let ((cur (current-buffer))
15985         (hashtb (or hashtb
15986                     (if (and method gnus-active-hashtb)
15987                         gnus-active-hashtb
15988                       (setq gnus-active-hashtb
15989                             (gnus-make-hashtable
15990                              (count-lines (point-min) (point-max)))))))
15991         (prefix (and method
15992                      (not (gnus-server-equal
15993                            (gnus-server-get-method nil method)
15994                            (gnus-server-get-method nil gnus-select-method)))
15995                      (gnus-group-prefixed-name "" method))))
15996
15997     (goto-char (point-min))
15998     ;; We split this into to separate loops, one with the prefix
15999     ;; and one without to speed the reading up somewhat.
16000     (if prefix
16001         (let (min max opoint group)
16002           (while (not (eobp))
16003             (condition-case ()
16004                 (progn
16005                   (read cur) (read cur)
16006                   (setq min (read cur)
16007                         max (read cur)
16008                         opoint (point))
16009                   (skip-chars-forward " \t")
16010                   (insert prefix)
16011                   (goto-char opoint)
16012                   (set (let ((obarray hashtb)) (read cur))
16013                        (cons min max)))
16014               (error (and group (symbolp group) (set group nil))))
16015             (forward-line 1)))
16016       (let (min max group)
16017         (while (not (eobp))
16018           (condition-case ()
16019               (if (= (following-char) ?2)
16020                   (progn
16021                     (read cur) (read cur)
16022                     (setq min (read cur)
16023                           max (read cur))
16024                     (set (setq group (let ((obarray hashtb)) (read cur)))
16025                          (cons min max))))
16026             (error (and group (symbolp group) (set group nil))))
16027           (forward-line 1))))))
16028
16029 (defun gnus-read-newsrc-file (&optional force)
16030   "Read startup file.
16031 If FORCE is non-nil, the .newsrc file is read."
16032   ;; Reset variables that might be defined in the .newsrc.eld file.
16033   (let ((variables gnus-variable-list))
16034     (while variables
16035       (set (car variables) nil)
16036       (setq variables (cdr variables))))
16037   (let* ((newsrc-file gnus-current-startup-file)
16038          (quick-file (concat newsrc-file ".el")))
16039     (save-excursion
16040       ;; We always load the .newsrc.eld file.  If always contains
16041       ;; much information that can not be gotten from the .newsrc
16042       ;; file (ticked articles, killed groups, foreign methods, etc.)
16043       (gnus-read-newsrc-el-file quick-file)
16044
16045       (if (and (file-exists-p gnus-current-startup-file)
16046                (or force
16047                    (and (file-newer-than-file-p newsrc-file quick-file)
16048                         (file-newer-than-file-p newsrc-file
16049                                                 (concat quick-file "d")))
16050                    (not gnus-newsrc-alist)))
16051           ;; We read the .newsrc file.  Note that if there if a
16052           ;; .newsrc.eld file exists, it has already been read, and
16053           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16054           ;; the .newsrc file, Gnus will only use the information it
16055           ;; can find there for changing the data already read -
16056           ;; ie. reading the .newsrc file will not trash the data
16057           ;; already read (except for read articles).
16058           (save-excursion
16059             (gnus-message 5 "Reading %s..." newsrc-file)
16060             (set-buffer (find-file-noselect newsrc-file))
16061             (buffer-disable-undo (current-buffer))
16062             (gnus-newsrc-to-gnus-format)
16063             (kill-buffer (current-buffer))
16064             (gnus-message 5 "Reading %s...done" newsrc-file)))
16065
16066       ;; Read any slave files.
16067       (unless gnus-slave
16068         (gnus-master-read-slave-newsrc))
16069       
16070       ;; Convert old to new.
16071       (gnus-convert-old-newsrc))))
16072
16073 (defun gnus-continuum-version (version)
16074   "Return VERSION as a floating point number."
16075   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16076     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16077            (number (match-string 2 version))
16078            major minor least)
16079       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16080       (setq major (string-to-number (match-string 1 number)))
16081       (setq minor (string-to-number (match-string 2 number)))
16082       (setq least (if (match-beginning 3)
16083                       (string-to-number (match-string 3 number))
16084                     0))
16085       (string-to-number
16086        (if (zerop major)
16087            (format "%s00%02d%02d"
16088                    (cond 
16089                     ((string= alpha "(ding)") "4.99")
16090                     ((string= alpha "September") "5.01")
16091                     ((string= alpha "Red") "5.03"))
16092                    minor least)
16093          (format "%d.%02d%20d" major minor least))))))
16094
16095 (defun gnus-convert-old-newsrc ()
16096   "Convert old newsrc into the new format, if needed."
16097   (let ((fcv (gnus-continuum-version gnus-newsrc-file-version)))
16098     (cond
16099      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16100       (gnus-convert-old-ticks)))))
16101
16102 (defun gnus-convert-old-ticks ()
16103   (let ((newsrc (cdr gnus-newsrc-alist))
16104         marks info dormant ticked)
16105     (while (setq info (pop newsrc))
16106       (when (setq marks (gnus-info-marks info))
16107         (setq dormant (cdr (assq 'dormant marks))
16108               ticked (cdr (assq 'tick marks)))
16109         (when (or dormant ticked)
16110           (gnus-info-set-read
16111            info
16112            (gnus-add-to-range
16113             (gnus-info-read info)
16114             (nconc (gnus-uncompress-range dormant)
16115                    (gnus-uncompress-range ticked)))))))))
16116
16117 (defun gnus-read-newsrc-el-file (file)
16118   (let ((ding-file (concat file "d")))
16119     ;; We always, always read the .eld file.
16120     (gnus-message 5 "Reading %s..." ding-file)
16121     (let (gnus-newsrc-assoc)
16122       (condition-case nil
16123           (load ding-file t t t)
16124         (error
16125          (gnus-error 1 "Error in %s" ding-file)))
16126       (when gnus-newsrc-assoc
16127         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16128     (gnus-make-hashtable-from-newsrc-alist)
16129     (when (file-newer-than-file-p file ding-file)
16130       ;; Old format quick file
16131       (gnus-message 5 "Reading %s..." file)
16132       ;; The .el file is newer than the .eld file, so we read that one
16133       ;; as well.
16134       (gnus-read-old-newsrc-el-file file))))
16135
16136 ;; Parse the old-style quick startup file
16137 (defun gnus-read-old-newsrc-el-file (file)
16138   (let (newsrc killed marked group m info)
16139     (prog1
16140         (let ((gnus-killed-assoc nil)
16141               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16142           (prog1
16143               (condition-case nil
16144                   (load file t t t)
16145                 (error nil))
16146             (setq newsrc gnus-newsrc-assoc
16147                   killed gnus-killed-assoc
16148                   marked gnus-marked-assoc)))
16149       (setq gnus-newsrc-alist nil)
16150       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16151         (if info
16152             (progn
16153               (gnus-info-set-read info (cddr group))
16154               (gnus-info-set-level
16155                info (if (nth 1 group) gnus-level-default-subscribed
16156                       gnus-level-default-unsubscribed))
16157               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16158           (push (setq info
16159                       (list (car group)
16160                             (if (nth 1 group) gnus-level-default-subscribed
16161                               gnus-level-default-unsubscribed)
16162                             (cddr group)))
16163                 gnus-newsrc-alist))
16164         ;; Copy marks into info.
16165         (when (setq m (assoc (car group) marked))
16166           (unless (nthcdr 3 info)
16167             (nconc info (list nil)))
16168           (gnus-info-set-marks
16169            info (list (cons 'tick (gnus-compress-sequence 
16170                                    (sort (cdr m) '<) t))))))
16171       (setq newsrc killed)
16172       (while newsrc
16173         (setcar newsrc (caar newsrc))
16174         (setq newsrc (cdr newsrc)))
16175       (setq gnus-killed-list killed))
16176     ;; The .el file version of this variable does not begin with
16177     ;; "options", while the .eld version does, so we just add it if it
16178     ;; isn't there.
16179     (and
16180      gnus-newsrc-options
16181      (progn
16182        (and (not (string-match "^ *options" gnus-newsrc-options))
16183             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16184        (and (not (string-match "\n$" gnus-newsrc-options))
16185             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16186        ;; Finally, if we read some options lines, we parse them.
16187        (or (string= gnus-newsrc-options "")
16188            (gnus-newsrc-parse-options gnus-newsrc-options))))
16189
16190     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16191     (gnus-make-hashtable-from-newsrc-alist)))
16192
16193 (defun gnus-make-newsrc-file (file)
16194   "Make server dependent file name by catenating FILE and server host name."
16195   (let* ((file (expand-file-name file nil))
16196          (real-file (concat file "-" (nth 1 gnus-select-method))))
16197     (if (or (file-exists-p real-file)
16198             (file-exists-p (concat real-file ".el"))
16199             (file-exists-p (concat real-file ".eld")))
16200         real-file file)))
16201
16202 (defun gnus-newsrc-to-gnus-format ()
16203   (setq gnus-newsrc-options "")
16204   (setq gnus-newsrc-options-n nil)
16205
16206   (or gnus-active-hashtb
16207       (setq gnus-active-hashtb (make-vector 4095 0)))
16208   (let ((buf (current-buffer))
16209         (already-read (> (length gnus-newsrc-alist) 1))
16210         group subscribed options-symbol newsrc Options-symbol
16211         symbol reads num1)
16212     (goto-char (point-min))
16213     ;; We intern the symbol `options' in the active hashtb so that we
16214     ;; can `eq' against it later.
16215     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16216     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16217
16218     (while (not (eobp))
16219       ;; We first read the first word on the line by narrowing and
16220       ;; then reading into `gnus-active-hashtb'.  Most groups will
16221       ;; already exist in that hashtb, so this will save some string
16222       ;; space.
16223       (narrow-to-region
16224        (point)
16225        (progn (skip-chars-forward "^ \t!:\n") (point)))
16226       (goto-char (point-min))
16227       (setq symbol
16228             (and (/= (point-min) (point-max))
16229                  (let ((obarray gnus-active-hashtb)) (read buf))))
16230       (widen)
16231       ;; Now, the symbol we have read is either `options' or a group
16232       ;; name.  If it is an options line, we just add it to a string.
16233       (cond
16234        ((or (eq symbol options-symbol)
16235             (eq symbol Options-symbol))
16236         (setq gnus-newsrc-options
16237               ;; This concating is quite inefficient, but since our
16238               ;; thorough studies show that approx 99.37% of all
16239               ;; .newsrc files only contain a single options line, we
16240               ;; don't give a damn, frankly, my dear.
16241               (concat gnus-newsrc-options
16242                       (buffer-substring
16243                        (gnus-point-at-bol)
16244                        ;; Options may continue on the next line.
16245                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16246                                 (progn (beginning-of-line) (point)))
16247                            (point)))))
16248         (forward-line -1))
16249        (symbol
16250         ;; Group names can be just numbers.  
16251         (when (numberp symbol) 
16252           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16253         (or (boundp symbol) (set symbol nil))
16254         ;; It was a group name.
16255         (setq subscribed (= (following-char) ?:)
16256               group (symbol-name symbol)
16257               reads nil)
16258         (if (eolp)
16259             ;; If the line ends here, this is clearly a buggy line, so
16260             ;; we put point a the beginning of line and let the cond
16261             ;; below do the error handling.
16262             (beginning-of-line)
16263           ;; We skip to the beginning of the ranges.
16264           (skip-chars-forward "!: \t"))
16265         ;; We are now at the beginning of the list of read articles.
16266         ;; We read them range by range.
16267         (while
16268             (cond
16269              ((looking-at "[0-9]+")
16270               ;; We narrow and read a number instead of buffer-substring/
16271               ;; string-to-int because it's faster.  narrow/widen is
16272               ;; faster than save-restriction/narrow, and save-restriction
16273               ;; produces a garbage object.
16274               (setq num1 (progn
16275                            (narrow-to-region (match-beginning 0) (match-end 0))
16276                            (read buf)))
16277               (widen)
16278               ;; If the next character is a dash, then this is a range.
16279               (if (= (following-char) ?-)
16280                   (progn
16281                     ;; We read the upper bound of the range.
16282                     (forward-char 1)
16283                     (if (not (looking-at "[0-9]+"))
16284                         ;; This is a buggy line, by we pretend that
16285                         ;; it's kinda OK.  Perhaps the user should be
16286                         ;; dinged?
16287                         (setq reads (cons num1 reads))
16288                       (setq reads
16289                             (cons
16290                              (cons num1
16291                                    (progn
16292                                      (narrow-to-region (match-beginning 0)
16293                                                        (match-end 0))
16294                                      (read buf)))
16295                              reads))
16296                       (widen)))
16297                 ;; It was just a simple number, so we add it to the
16298                 ;; list of ranges.
16299                 (setq reads (cons num1 reads)))
16300               ;; If the next char in ?\n, then we have reached the end
16301               ;; of the line and return nil.
16302               (/= (following-char) ?\n))
16303              ((= (following-char) ?\n)
16304               ;; End of line, so we end.
16305               nil)
16306              (t
16307               ;; Not numbers and not eol, so this might be a buggy
16308               ;; line...
16309               (or (eobp)
16310                   ;; If it was eob instead of ?\n, we allow it.
16311                   (progn
16312                     ;; The line was buggy.
16313                     (setq group nil)
16314                     (gnus-error 3.1 "Mangled line: %s"
16315                                 (buffer-substring (gnus-point-at-bol)
16316                                                   (gnus-point-at-eol)))))
16317               nil))
16318           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16319           ;; we allow them, because it's a common mistake to put a
16320           ;; space after the comma.
16321           (skip-chars-forward ", "))
16322
16323         ;; We have already read .newsrc.eld, so we gently update the
16324         ;; data in the hash table with the information we have just
16325         ;; read.
16326         (when group
16327           (let ((info (gnus-get-info group))
16328                 level)
16329             (if info
16330                 ;; There is an entry for this file in the alist.
16331                 (progn
16332                   (gnus-info-set-read info (nreverse reads))
16333                   ;; We update the level very gently.  In fact, we
16334                   ;; only change it if there's been a status change
16335                   ;; from subscribed to unsubscribed, or vice versa.
16336                   (setq level (gnus-info-level info))
16337                   (cond ((and (<= level gnus-level-subscribed)
16338                               (not subscribed))
16339                          (setq level (if reads
16340                                          gnus-level-default-unsubscribed
16341                                        (1+ gnus-level-default-unsubscribed))))
16342                         ((and (> level gnus-level-subscribed) subscribed)
16343                          (setq level gnus-level-default-subscribed)))
16344                   (gnus-info-set-level info level))
16345               ;; This is a new group.
16346               (setq info (list group
16347                                (if subscribed
16348                                    gnus-level-default-subscribed
16349                                  (if reads
16350                                      (1+ gnus-level-subscribed)
16351                                    gnus-level-default-unsubscribed))
16352                                (nreverse reads))))
16353             (setq newsrc (cons info newsrc))))))
16354       (forward-line 1))
16355
16356     (setq newsrc (nreverse newsrc))
16357
16358     (if (not already-read)
16359         ()
16360       ;; We now have two newsrc lists - `newsrc', which is what we
16361       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16362       ;; what we've read from .newsrc.eld.  We have to merge these
16363       ;; lists.  We do this by "attaching" any (foreign) groups in the
16364       ;; gnus-newsrc-alist to the (native) group that precedes them.
16365       (let ((rc (cdr gnus-newsrc-alist))
16366             (prev gnus-newsrc-alist)
16367             entry mentry)
16368         (while rc
16369           (or (null (nth 4 (car rc)))   ; It's a native group.
16370               (assoc (caar rc) newsrc) ; It's already in the alist.
16371               (if (setq entry (assoc (caar prev) newsrc))
16372                   (setcdr (setq mentry (memq entry newsrc))
16373                           (cons (car rc) (cdr mentry)))
16374                 (setq newsrc (cons (car rc) newsrc))))
16375           (setq prev rc
16376                 rc (cdr rc)))))
16377
16378     (setq gnus-newsrc-alist newsrc)
16379     ;; We make the newsrc hashtb.
16380     (gnus-make-hashtable-from-newsrc-alist)
16381
16382     ;; Finally, if we read some options lines, we parse them.
16383     (or (string= gnus-newsrc-options "")
16384         (gnus-newsrc-parse-options gnus-newsrc-options))))
16385
16386 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16387 ;; The return value will be a list on the form
16388 ;; ((regexp1 . ignore)
16389 ;;  (regexp2 . subscribe)...)
16390 ;; When handling new newsgroups, groups that match a `ignore' regexp
16391 ;; will be ignored, and groups that match a `subscribe' regexp will be
16392 ;; subscribed.  A line like
16393 ;; options -n !all rec.all
16394 ;; will lead to a list that looks like
16395 ;; (("^rec\\..+" . subscribe)
16396 ;;  ("^.+" . ignore))
16397 ;; So all "rec.*" groups will be subscribed, while all the other
16398 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16399 ;; different from "options -n rec.all !all".
16400 (defun gnus-newsrc-parse-options (options)
16401   (let (out eol)
16402     (save-excursion
16403       (gnus-set-work-buffer)
16404       (insert (regexp-quote options))
16405       ;; First we treat all continuation lines.
16406       (goto-char (point-min))
16407       (while (re-search-forward "\n[ \t]+" nil t)
16408         (replace-match " " t t))
16409       ;; Then we transform all "all"s into ".+"s.
16410       (goto-char (point-min))
16411       (while (re-search-forward "\\ball\\b" nil t)
16412         (replace-match ".+" t t))
16413       (goto-char (point-min))
16414       ;; We remove all other options than the "-n" ones.
16415       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16416         (replace-match " ")
16417         (forward-char -1))
16418       (goto-char (point-min))
16419
16420       ;; We are only interested in "options -n" lines - we
16421       ;; ignore the other option lines.
16422       (while (re-search-forward "[ \t]-n" nil t)
16423         (setq eol
16424               (or (save-excursion
16425                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16426                          (- (point) 2)))
16427                   (gnus-point-at-eol)))
16428         ;; Search for all "words"...
16429         (while (re-search-forward "[^ \t,\n]+" eol t)
16430           (if (= (char-after (match-beginning 0)) ?!)
16431               ;; If the word begins with a bang (!), this is a "not"
16432               ;; spec.  We put this spec (minus the bang) and the
16433               ;; symbol `ignore' into the list.
16434               (setq out (cons (cons (concat
16435                                      "^" (buffer-substring
16436                                           (1+ (match-beginning 0))
16437                                           (match-end 0)))
16438                                     'ignore) out))
16439             ;; There was no bang, so this is a "yes" spec.
16440             (setq out (cons (cons (concat "^" (match-string 0))
16441                                   'subscribe) out)))))
16442
16443       (setq gnus-newsrc-options-n out))))
16444
16445 (defun gnus-save-newsrc-file (&optional force)
16446   "Save .newsrc file."
16447   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16448   ;; from the variable gnus-newsrc-alist.
16449   (when (and (or gnus-newsrc-alist gnus-killed-list)
16450              gnus-current-startup-file)
16451     (save-excursion
16452       (if (and (or gnus-use-dribble-file gnus-slave)
16453                (not force)
16454                (or (not gnus-dribble-buffer)
16455                    (not (buffer-name gnus-dribble-buffer))
16456                    (zerop (save-excursion
16457                             (set-buffer gnus-dribble-buffer)
16458                             (buffer-size)))))
16459           (gnus-message 4 "(No changes need to be saved)")
16460         (run-hooks 'gnus-save-newsrc-hook)
16461         (if gnus-slave
16462             (gnus-slave-save-newsrc)
16463           ;; Save .newsrc.
16464           (when gnus-save-newsrc-file
16465             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16466             (gnus-gnus-to-newsrc-format)
16467             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16468           ;; Save .newsrc.eld.
16469           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16470           (make-local-variable 'version-control)
16471           (setq version-control 'never)
16472           (setq buffer-file-name
16473                 (concat gnus-current-startup-file ".eld"))
16474           (gnus-add-current-to-buffer-list)
16475           (buffer-disable-undo (current-buffer))
16476           (erase-buffer)
16477           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16478           (gnus-gnus-to-quick-newsrc-format)
16479           (run-hooks 'gnus-save-quick-newsrc-hook)
16480           (save-buffer)
16481           (kill-buffer (current-buffer))
16482           (gnus-message
16483            5 "Saving %s.eld...done" gnus-current-startup-file))
16484         (gnus-dribble-delete-file)))))
16485
16486 (defun gnus-gnus-to-quick-newsrc-format ()
16487   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16488   (insert ";; Gnus startup file.\n")
16489   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16490   (insert ";; to read .newsrc.\n")
16491   (insert "(setq gnus-newsrc-file-version "
16492           (prin1-to-string gnus-version) ")\n")
16493   (let ((variables
16494          (if gnus-save-killed-list gnus-variable-list
16495            ;; Remove the `gnus-killed-list' from the list of variables
16496            ;; to be saved, if required.
16497            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16498         ;; Peel off the "dummy" group.
16499         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16500         variable)
16501     ;; Insert the variables into the file.
16502     (while variables
16503       (when (and (boundp (setq variable (pop variables)))
16504                  (symbol-value variable))
16505         (insert "(setq " (symbol-name variable) " '")
16506         (prin1 (symbol-value variable) (current-buffer))
16507         (insert ")\n")))))
16508
16509 (defun gnus-gnus-to-newsrc-format ()
16510   ;; Generate and save the .newsrc file.
16511   (save-excursion
16512     (set-buffer (create-file-buffer gnus-current-startup-file))
16513     (let ((newsrc (cdr gnus-newsrc-alist))
16514           (standard-output (current-buffer))
16515           info ranges range method)
16516       (setq buffer-file-name gnus-current-startup-file)
16517       (buffer-disable-undo (current-buffer))
16518       (erase-buffer)
16519       ;; Write options.
16520       (if gnus-newsrc-options (insert gnus-newsrc-options))
16521       ;; Write subscribed and unsubscribed.
16522       (while (setq info (pop newsrc))
16523         ;; Don't write foreign groups to .newsrc.
16524         (when (or (null (setq method (gnus-info-method info)))
16525                   (equal method "native")
16526                   (gnus-server-equal method gnus-select-method))
16527           (insert (gnus-info-group info)
16528                   (if (> (gnus-info-level info) gnus-level-subscribed)
16529                       "!" ":"))
16530           (when (setq ranges (gnus-info-read info))
16531             (insert " ")
16532             (if (not (listp (cdr ranges)))
16533                 (if (= (car ranges) (cdr ranges))
16534                     (princ (car ranges))
16535                   (princ (car ranges))
16536                   (insert "-")
16537                   (princ (cdr ranges)))
16538               (while (setq range (pop ranges))
16539                 (if (or (atom range) (= (car range) (cdr range)))
16540                     (princ (or (and (atom range) range) (car range)))
16541                   (princ (car range))
16542                   (insert "-")
16543                   (princ (cdr range)))
16544                 (if ranges (insert ",")))))
16545           (insert "\n")))
16546       (make-local-variable 'version-control)
16547       (setq version-control 'never)
16548       ;; It has been reported that sometime the modtime on the .newsrc
16549       ;; file seems to be off.  We really do want to overwrite it, so
16550       ;; we clear the modtime here before saving.  It's a bit odd,
16551       ;; though...
16552       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16553       ;; delete the silly thing entirely first.  but this fails to provide
16554       ;; such niceties as .newsrc~ creation.
16555       (if gnus-modtime-botch
16556           (delete-file gnus-startup-file)
16557         (clear-visited-file-modtime))
16558       (run-hooks 'gnus-save-standard-newsrc-hook)
16559       (save-buffer)
16560       (kill-buffer (current-buffer)))))
16561
16562 \f
16563 ;;;
16564 ;;; Slave functions.
16565 ;;;
16566
16567 (defun gnus-slave-save-newsrc ()
16568   (save-excursion
16569     (set-buffer gnus-dribble-buffer)
16570     (let ((slave-name
16571            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16572       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16573
16574 (defun gnus-master-read-slave-newsrc ()
16575   (let ((slave-files
16576          (directory-files
16577           (file-name-directory gnus-current-startup-file)
16578           t (concat
16579              "^" (regexp-quote
16580                   (concat
16581                    (file-name-nondirectory gnus-current-startup-file)
16582                    "-slave-")))
16583           t))
16584         file)
16585     (if (not slave-files)
16586         ()                              ; There are no slave files to read.
16587       (gnus-message 7 "Reading slave newsrcs...")
16588       (save-excursion
16589         (set-buffer (get-buffer-create " *gnus slave*"))
16590         (buffer-disable-undo (current-buffer))
16591         (setq slave-files
16592               (sort (mapcar (lambda (file)
16593                               (list (nth 5 (file-attributes file)) file))
16594                             slave-files)
16595                     (lambda (f1 f2)
16596                       (or (< (caar f1) (caar f2))
16597                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16598         (while slave-files
16599           (erase-buffer)
16600           (setq file (nth 1 (car slave-files)))
16601           (insert-file-contents file)
16602           (if (condition-case ()
16603                   (progn
16604                     (eval-buffer (current-buffer))
16605                     t)
16606                 (error
16607                  (gnus-error 3.2 "Possible error in %s" file)
16608                  nil))
16609               (or gnus-slave ; Slaves shouldn't delete these files.
16610                   (condition-case ()
16611                       (delete-file file)
16612                     (error nil))))
16613           (setq slave-files (cdr slave-files))))
16614       (gnus-message 7 "Reading slave newsrcs...done"))))
16615
16616 \f
16617 ;;;
16618 ;;; Group description.
16619 ;;;
16620
16621 (defun gnus-read-all-descriptions-files ()
16622   (let ((methods (cons gnus-select-method 
16623                        (nconc
16624                         (when gnus-message-archive-method
16625                           (list "archive"))
16626                         gnus-secondary-select-methods))))
16627     (while methods
16628       (gnus-read-descriptions-file (car methods))
16629       (setq methods (cdr methods)))
16630     t))
16631
16632 (defun gnus-read-descriptions-file (&optional method)
16633   (let ((method (or method gnus-select-method)))
16634     (when (stringp method)
16635       (setq method (gnus-server-to-method method)))
16636     ;; We create the hashtable whether we manage to read the desc file
16637     ;; to avoid trying to re-read after a failed read.
16638     (or gnus-description-hashtb
16639         (setq gnus-description-hashtb
16640               (gnus-make-hashtable (length gnus-active-hashtb))))
16641     ;; Mark this method's desc file as read.
16642     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16643                   gnus-description-hashtb)
16644
16645     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16646     (cond
16647      ((not (gnus-check-server method))
16648       (gnus-message 1 "Couldn't open server")
16649       nil)
16650      ((not (gnus-request-list-newsgroups method))
16651       (gnus-message 1 "Couldn't read newsgroups descriptions")
16652       nil)
16653      (t
16654       (let (group)
16655         (save-excursion
16656           (save-restriction
16657             (set-buffer nntp-server-buffer)
16658             (goto-char (point-min))
16659             (if (or (search-forward "\n.\n" nil t)
16660                     (goto-char (point-max)))
16661                 (progn
16662                   (beginning-of-line)
16663                   (narrow-to-region (point-min) (point))))
16664             (goto-char (point-min))
16665             (while (not (eobp))
16666               ;; If we get an error, we set group to 0, which is not a
16667               ;; symbol...
16668               (setq group
16669                     (condition-case ()
16670                         (let ((obarray gnus-description-hashtb))
16671                           ;; Group is set to a symbol interned in this
16672                           ;; hash table.
16673                           (read nntp-server-buffer))
16674                       (error 0)))
16675               (skip-chars-forward " \t")
16676               ;; ...  which leads to this line being effectively ignored.
16677               (and (symbolp group)
16678                    (set group (buffer-substring
16679                                (point) (progn (end-of-line) (point)))))
16680               (forward-line 1))))
16681         (gnus-message 5 "Reading descriptions file...done")
16682         t)))))
16683
16684 (defun gnus-group-get-description (group)
16685   "Get the description of a group by sending XGTITLE to the server."
16686   (when (gnus-request-group-description group)
16687     (save-excursion
16688       (set-buffer nntp-server-buffer)
16689       (goto-char (point-min))
16690       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16691         (match-string 1)))))
16692
16693 \f
16694 ;;;
16695 ;;; Buffering of read articles.
16696 ;;;
16697
16698 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16699 (defvar gnus-backlog-articles nil)
16700 (defvar gnus-backlog-hashtb nil)
16701
16702 (defun gnus-backlog-buffer ()
16703   "Return the backlog buffer."
16704   (or (get-buffer gnus-backlog-buffer)
16705       (save-excursion
16706         (set-buffer (get-buffer-create gnus-backlog-buffer))
16707         (buffer-disable-undo (current-buffer))
16708         (setq buffer-read-only t)
16709         (gnus-add-current-to-buffer-list)
16710         (get-buffer gnus-backlog-buffer))))
16711
16712 (defun gnus-backlog-setup ()
16713   "Initialize backlog variables."
16714   (unless gnus-backlog-hashtb
16715     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16716
16717 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16718
16719 (defun gnus-backlog-shutdown ()
16720   "Clear all backlog variables and buffers."
16721   (when (get-buffer gnus-backlog-buffer)
16722     (kill-buffer gnus-backlog-buffer))
16723   (setq gnus-backlog-hashtb nil
16724         gnus-backlog-articles nil))
16725
16726 (defun gnus-backlog-enter-article (group number buffer)
16727   (gnus-backlog-setup)
16728   (let ((ident (intern (concat group ":" (int-to-string number))
16729                        gnus-backlog-hashtb))
16730         b)
16731     (if (memq ident gnus-backlog-articles)
16732         () ; It's already kept.
16733       ;; Remove the oldest article, if necessary.
16734       (and (numberp gnus-keep-backlog)
16735            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16736            (gnus-backlog-remove-oldest-article))
16737       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16738       ;; Insert the new article.
16739       (save-excursion
16740         (set-buffer (gnus-backlog-buffer))
16741         (let (buffer-read-only)
16742           (goto-char (point-max))
16743           (or (bolp) (insert "\n"))
16744           (setq b (point))
16745           (insert-buffer-substring buffer)
16746           ;; Tag the beginning of the article with the ident.
16747           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16748
16749 (defun gnus-backlog-remove-oldest-article ()
16750   (save-excursion
16751     (set-buffer (gnus-backlog-buffer))
16752     (goto-char (point-min))
16753     (if (zerop (buffer-size))
16754         () ; The buffer is empty.
16755       (let ((ident (get-text-property (point) 'gnus-backlog))
16756             buffer-read-only)
16757         ;; Remove the ident from the list of articles.
16758         (when ident
16759           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16760         ;; Delete the article itself.
16761         (delete-region
16762          (point) (next-single-property-change
16763                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16764
16765 (defun gnus-backlog-request-article (group number buffer)
16766   (when (numberp number)
16767     (gnus-backlog-setup)
16768     (let ((ident (intern (concat group ":" (int-to-string number))
16769                          gnus-backlog-hashtb))
16770           beg end)
16771       (when (memq ident gnus-backlog-articles)
16772         ;; It was in the backlog.
16773         (save-excursion
16774           (set-buffer (gnus-backlog-buffer))
16775           (if (not (setq beg (text-property-any
16776                               (point-min) (point-max) 'gnus-backlog
16777                               ident)))
16778               ;; It wasn't in the backlog after all.
16779               (ignore
16780                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16781             ;; Find the end (i. e., the beginning of the next article).
16782             (setq end
16783                   (next-single-property-change
16784                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16785         (let ((buffer-read-only nil))
16786           (erase-buffer)
16787           (insert-buffer-substring gnus-backlog-buffer beg end)
16788           t)))))
16789
16790 ;; Allow redefinition of Gnus functions.
16791
16792 (gnus-ems-redefine)
16793
16794 (provide 'gnus)
16795
16796 ;;; gnus.el ends here