*** 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 (defvar gnus-method-history nil)
1537 ;; Variable holding the user answers to all method prompts.
1538
1539 (defvar gnus-server-alist nil
1540   "List of available servers.")
1541
1542 (defvar gnus-group-indentation-function nil)
1543
1544 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1545
1546 (defvar gnus-goto-missing-group-function nil)
1547
1548 (defvar gnus-override-subscribe-method nil)
1549
1550 (defvar gnus-group-goto-next-group-function nil
1551   "Function to override finding the next group after listing groups.")
1552
1553 (defconst gnus-article-mark-lists
1554   '((marked . tick) (replied . reply)
1555     (expirable . expire) (killed . killed)
1556     (bookmarks . bookmark) (dormant . dormant)
1557     (scored . score) (saved . save)
1558     (cached . cache)
1559     ))
1560
1561 ;; Avoid highlighting in kill files.
1562 (defvar gnus-summary-inhibit-highlight nil)
1563 (defvar gnus-newsgroup-selected-overlay nil)
1564
1565 (defvar gnus-inhibit-hiding nil)
1566 (defvar gnus-group-indentation "")
1567 (defvar gnus-inhibit-limiting nil)
1568 (defvar gnus-created-frames nil)
1569
1570 (defvar gnus-article-mode-map nil)
1571 (defvar gnus-dribble-buffer nil)
1572 (defvar gnus-headers-retrieved-by nil)
1573 (defvar gnus-article-reply nil)
1574 (defvar gnus-override-method nil)
1575 (defvar gnus-article-check-size nil)
1576
1577 (defvar gnus-current-score-file nil)
1578 (defvar gnus-newsgroup-adaptive-score-file nil)
1579 (defvar gnus-scores-exclude-files nil)
1580
1581 (defvar gnus-opened-servers nil)
1582
1583 (defvar gnus-current-move-group nil)
1584
1585 (defvar gnus-newsgroup-dependencies nil)
1586 (defvar gnus-newsgroup-async nil)
1587 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1588
1589 (defvar gnus-newsgroup-adaptive nil)
1590
1591 (defvar gnus-summary-display-table nil)
1592 (defvar gnus-summary-display-article-function nil)
1593
1594 (defvar gnus-summary-highlight-line-function nil
1595   "Function called after highlighting a summary line.")
1596
1597 (defvar gnus-group-line-format-alist
1598   `((?M gnus-tmp-marked-mark ?c)
1599     (?S gnus-tmp-subscribed ?c)
1600     (?L gnus-tmp-level ?d)
1601     (?N (cond ((eq number t) "*" )
1602               ((numberp number) 
1603                (int-to-string
1604                 (+ number
1605                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1606                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1607               (t number)) ?s)
1608     (?R gnus-tmp-number-of-read ?s)
1609     (?t gnus-tmp-number-total ?d)
1610     (?y gnus-tmp-number-of-unread ?s)
1611     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1612     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1613     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1614            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1615     (?g gnus-tmp-group ?s)
1616     (?G gnus-tmp-qualified-group ?s)
1617     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1618     (?D gnus-tmp-newsgroup-description ?s)
1619     (?o gnus-tmp-moderated ?c)
1620     (?O gnus-tmp-moderated-string ?s)
1621     (?p gnus-tmp-process-marked ?c)
1622     (?s gnus-tmp-news-server ?s)
1623     (?n gnus-tmp-news-method ?s)
1624     (?P gnus-group-indentation ?s)
1625     (?l gnus-tmp-grouplens ?s)
1626     (?z gnus-tmp-news-method-string ?s)
1627     (?u gnus-tmp-user-defined ?s)))
1628
1629 (defvar gnus-summary-line-format-alist
1630   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1631     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1632     (?s gnus-tmp-subject-or-nil ?s)
1633     (?n gnus-tmp-name ?s)
1634     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1635         ?s)
1636     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1637             gnus-tmp-from) ?s)
1638     (?F gnus-tmp-from ?s)
1639     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1640     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1641     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1642     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1643     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1644     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1645     (?L gnus-tmp-lines ?d)
1646     (?I gnus-tmp-indentation ?s)
1647     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1648     (?R gnus-tmp-replied ?c)
1649     (?\[ gnus-tmp-opening-bracket ?c)
1650     (?\] gnus-tmp-closing-bracket ?c)
1651     (?\> (make-string gnus-tmp-level ? ) ?s)
1652     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1653     (?i gnus-tmp-score ?d)
1654     (?z gnus-tmp-score-char ?c)
1655     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1656     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1657     (?U gnus-tmp-unread ?c)
1658     (?t (gnus-summary-number-of-articles-in-thread
1659          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1660         ?d)
1661     (?e (gnus-summary-number-of-articles-in-thread
1662          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1663         ?c)
1664     (?u gnus-tmp-user-defined ?s))
1665   "An alist of format specifications that can appear in summary lines,
1666 and what variables they correspond with, along with the type of the
1667 variable (string, integer, character, etc).")
1668
1669 (defvar gnus-summary-dummy-line-format-alist
1670   `((?S gnus-tmp-subject ?s)
1671     (?N gnus-tmp-number ?d)
1672     (?u gnus-tmp-user-defined ?s)))
1673
1674 (defvar gnus-summary-mode-line-format-alist
1675   `((?G gnus-tmp-group-name ?s)
1676     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1677     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1678     (?A gnus-tmp-article-number ?d)
1679     (?Z gnus-tmp-unread-and-unselected ?s)
1680     (?V gnus-version ?s)
1681     (?U gnus-tmp-unread ?d)
1682     (?S gnus-tmp-subject ?s)
1683     (?e gnus-tmp-unselected ?d)
1684     (?u gnus-tmp-user-defined ?s)
1685     (?d (length gnus-newsgroup-dormant) ?d)
1686     (?t (length gnus-newsgroup-marked) ?d)
1687     (?r (length gnus-newsgroup-reads) ?d)
1688     (?E gnus-newsgroup-expunged-tally ?d)
1689     (?s (gnus-current-score-file-nondirectory) ?s)))
1690
1691 (defvar gnus-article-mode-line-format-alist
1692   gnus-summary-mode-line-format-alist)
1693
1694 (defvar gnus-group-mode-line-format-alist
1695   `((?S gnus-tmp-news-server ?s)
1696     (?M gnus-tmp-news-method ?s)
1697     (?u gnus-tmp-user-defined ?s)))
1698
1699 (defvar gnus-have-read-active-file nil)
1700
1701 (defconst gnus-maintainer
1702   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1703   "The mail address of the Gnus maintainers.")
1704
1705 (defconst gnus-version "September Gnus v0.76"
1706   "Version number for this version of Gnus.")
1707
1708 (defvar gnus-info-nodes
1709   '((gnus-group-mode            "(gnus)The Group Buffer")
1710     (gnus-summary-mode          "(gnus)The Summary Buffer")
1711     (gnus-article-mode          "(gnus)The Article Buffer"))
1712   "Assoc list of major modes and related Info nodes.")
1713
1714 (defvar gnus-group-buffer "*Group*")
1715 (defvar gnus-summary-buffer "*Summary*")
1716 (defvar gnus-article-buffer "*Article*")
1717 (defvar gnus-server-buffer "*Server*")
1718
1719 (defvar gnus-work-buffer " *gnus work*")
1720
1721 (defvar gnus-original-article-buffer " *Original Article*")
1722 (defvar gnus-original-article nil)
1723
1724 (defvar gnus-buffer-list nil
1725   "Gnus buffers that should be killed on exit.")
1726
1727 (defvar gnus-slave nil
1728   "Whether this Gnus is a slave or not.")
1729
1730 (defvar gnus-variable-list
1731   '(gnus-newsrc-options gnus-newsrc-options-n
1732     gnus-newsrc-last-checked-date
1733     gnus-newsrc-alist gnus-server-alist
1734     gnus-killed-list gnus-zombie-list
1735     gnus-topic-topology gnus-topic-alist
1736     gnus-format-specs)
1737   "Gnus variables saved in the quick startup file.")
1738
1739 (defvar gnus-newsrc-options nil
1740   "Options line in the .newsrc file.")
1741
1742 (defvar gnus-newsrc-options-n nil
1743   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1744
1745 (defvar gnus-newsrc-last-checked-date nil
1746   "Date Gnus last asked server for new newsgroups.")
1747
1748 (defvar gnus-topic-topology nil
1749   "The complete topic hierarchy.")
1750
1751 (defvar gnus-topic-alist nil
1752   "The complete topic-group alist.")
1753
1754 (defvar gnus-newsrc-alist nil
1755   "Assoc list of read articles.
1756 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1757
1758 (defvar gnus-newsrc-hashtb nil
1759   "Hashtable of gnus-newsrc-alist.")
1760
1761 (defvar gnus-killed-list nil
1762   "List of killed newsgroups.")
1763
1764 (defvar gnus-killed-hashtb nil
1765   "Hash table equivalent of gnus-killed-list.")
1766
1767 (defvar gnus-zombie-list nil
1768   "List of almost dead newsgroups.")
1769
1770 (defvar gnus-description-hashtb nil
1771   "Descriptions of newsgroups.")
1772
1773 (defvar gnus-list-of-killed-groups nil
1774   "List of newsgroups that have recently been killed by the user.")
1775
1776 (defvar gnus-active-hashtb nil
1777   "Hashtable of active articles.")
1778
1779 (defvar gnus-moderated-list nil
1780   "List of moderated newsgroups.")
1781
1782 (defvar gnus-group-marked nil)
1783
1784 (defvar gnus-current-startup-file nil
1785   "Startup file for the current host.")
1786
1787 (defvar gnus-last-search-regexp nil
1788   "Default regexp for article search command.")
1789
1790 (defvar gnus-last-shell-command nil
1791   "Default shell command on article.")
1792
1793 (defvar gnus-current-select-method nil
1794   "The current method for selecting a newsgroup.")
1795
1796 (defvar gnus-group-list-mode nil)
1797
1798 (defvar gnus-article-internal-prepare-hook nil)
1799
1800 (defvar gnus-newsgroup-name nil)
1801 (defvar gnus-newsgroup-begin nil)
1802 (defvar gnus-newsgroup-end nil)
1803 (defvar gnus-newsgroup-last-rmail nil)
1804 (defvar gnus-newsgroup-last-mail nil)
1805 (defvar gnus-newsgroup-last-folder nil)
1806 (defvar gnus-newsgroup-last-file nil)
1807 (defvar gnus-newsgroup-auto-expire nil)
1808 (defvar gnus-newsgroup-active nil)
1809
1810 (defvar gnus-newsgroup-data nil)
1811 (defvar gnus-newsgroup-data-reverse nil)
1812 (defvar gnus-newsgroup-limit nil)
1813 (defvar gnus-newsgroup-limits nil)
1814
1815 (defvar gnus-newsgroup-unreads nil
1816   "List of unread articles in the current newsgroup.")
1817
1818 (defvar gnus-newsgroup-unselected nil
1819   "List of unselected unread articles in the current newsgroup.")
1820
1821 (defvar gnus-newsgroup-reads nil
1822   "Alist of read articles and article marks in the current newsgroup.")
1823
1824 (defvar gnus-newsgroup-expunged-tally nil)
1825
1826 (defvar gnus-newsgroup-marked nil
1827   "List of ticked articles in the current newsgroup (a subset of unread art).")
1828
1829 (defvar gnus-newsgroup-killed nil
1830   "List of ranges of articles that have been through the scoring process.")
1831
1832 (defvar gnus-newsgroup-cached nil
1833   "List of articles that come from the article cache.")
1834
1835 (defvar gnus-newsgroup-saved nil
1836   "List of articles that have been saved.")
1837
1838 (defvar gnus-newsgroup-kill-headers nil)
1839
1840 (defvar gnus-newsgroup-replied nil
1841   "List of articles that have been replied to in the current newsgroup.")
1842
1843 (defvar gnus-newsgroup-expirable nil
1844   "List of articles in the current newsgroup that can be expired.")
1845
1846 (defvar gnus-newsgroup-processable nil
1847   "List of articles in the current newsgroup that can be processed.")
1848
1849 (defvar gnus-newsgroup-bookmarks nil
1850   "List of articles in the current newsgroup that have bookmarks.")
1851
1852 (defvar gnus-newsgroup-dormant nil
1853   "List of dormant articles in the current newsgroup.")
1854
1855 (defvar gnus-newsgroup-scored nil
1856   "List of scored articles in the current newsgroup.")
1857
1858 (defvar gnus-newsgroup-headers nil
1859   "List of article headers in the current newsgroup.")
1860
1861 (defvar gnus-newsgroup-threads nil)
1862
1863 (defvar gnus-newsgroup-prepared nil
1864   "Whether the current group has been prepared properly.")
1865
1866 (defvar gnus-newsgroup-ancient nil
1867   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1868
1869 (defvar gnus-newsgroup-sparse nil)
1870
1871 (defvar gnus-current-article nil)
1872 (defvar gnus-article-current nil)
1873 (defvar gnus-current-headers nil)
1874 (defvar gnus-have-all-headers nil)
1875 (defvar gnus-last-article nil)
1876 (defvar gnus-newsgroup-history nil)
1877 (defvar gnus-current-kill-article nil)
1878
1879 ;; Save window configuration.
1880 (defvar gnus-prev-winconf nil)
1881
1882 (defvar gnus-summary-mark-positions nil)
1883 (defvar gnus-group-mark-positions nil)
1884
1885 (defvar gnus-reffed-article-number nil)
1886
1887 ;;; Let the byte-compiler know that we know about this variable.
1888 (defvar rmail-default-rmail-file)
1889
1890 (defvar gnus-cache-removable-articles nil)
1891
1892 (defvar gnus-dead-summary nil)
1893
1894 (defconst gnus-summary-local-variables
1895   '(gnus-newsgroup-name
1896     gnus-newsgroup-begin gnus-newsgroup-end
1897     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1898     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1899     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1900     gnus-newsgroup-unselected gnus-newsgroup-marked
1901     gnus-newsgroup-reads gnus-newsgroup-saved
1902     gnus-newsgroup-replied gnus-newsgroup-expirable
1903     gnus-newsgroup-processable gnus-newsgroup-killed
1904     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1905     gnus-newsgroup-headers gnus-newsgroup-threads
1906     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1907     gnus-current-article gnus-current-headers gnus-have-all-headers
1908     gnus-last-article gnus-article-internal-prepare-hook
1909     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1910     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1911     gnus-newsgroup-async gnus-thread-expunge-below
1912     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1913     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1914     gnus-newsgroup-history gnus-newsgroup-ancient
1915     gnus-newsgroup-sparse
1916     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1917     gnus-newsgroup-adaptive-score-file
1918     (gnus-newsgroup-expunged-tally . 0)
1919     gnus-cache-removable-articles gnus-newsgroup-cached
1920     gnus-newsgroup-data gnus-newsgroup-data-reverse
1921     gnus-newsgroup-limit gnus-newsgroup-limits)
1922   "Variables that are buffer-local to the summary buffers.")
1923
1924 (defconst gnus-bug-message
1925   "Sending a bug report to the Gnus Towers.
1926 ========================================
1927
1928 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1929 be sent to the Gnus Bug Exterminators.
1930
1931 At the bottom of the buffer you'll see lots of variable settings.
1932 Please do not delete those.  They will tell the Bug People what your
1933 environment is, so that it will be easier to locate the bugs.
1934
1935 If you have found a bug that makes Emacs go \"beep\", set
1936 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1937 and include the backtrace in your bug report.
1938
1939 Please describe the bug in annoying, painstaking detail.
1940
1941 Thank you for your help in stamping out bugs.
1942 ")
1943
1944 ;;; End of variables.
1945
1946 ;; Define some autoload functions Gnus might use.
1947 (eval-and-compile
1948
1949   ;; This little mapcar goes through the list below and marks the
1950   ;; symbols in question as autoloaded functions.
1951   (mapcar
1952    (lambda (package)
1953      (let ((interactive (nth 1 (memq ':interactive package))))
1954        (mapcar
1955         (lambda (function)
1956           (let (keymap)
1957             (when (consp function)
1958               (setq keymap (car (memq 'keymap function)))
1959               (setq function (car function)))
1960             (autoload function (car package) nil interactive keymap)))
1961         (if (eq (nth 1 package) ':interactive)
1962             (cdddr package)
1963           (cdr package)))))
1964    '(("metamail" metamail-buffer)
1965      ("info" Info-goto-node)
1966      ("hexl" hexl-hex-string-to-integer)
1967      ("pp" pp pp-to-string pp-eval-expression)
1968      ("mail-extr" mail-extract-address-components)
1969      ("nnmail" nnmail-split-fancy nnmail-article-group)
1970      ("nnvirtual" nnvirtual-catchup-group)
1971      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1972       timezone-make-sortable-date timezone-make-time-string)
1973      ("sendmail" mail-position-on-field mail-setup)
1974      ("rmailout" rmail-output)
1975      ("rnewspost" news-mail-other-window news-reply-yank-original
1976       news-caesar-buffer-body)
1977      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1978       rmail-show-message)
1979      ("gnus-soup" :interactive t
1980       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1981       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1982      ("nnsoup" nnsoup-pack-replies)
1983      ("gnus-scomo" :interactive t gnus-score-mode)
1984      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1985       gnus-Folder-save-name gnus-folder-save-name)
1986      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1987      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1988       gnus-server-make-menu-bar gnus-article-make-menu-bar
1989       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1990       gnus-summary-highlight-line gnus-carpal-setup-buffer
1991       gnus-group-highlight-line
1992       gnus-article-add-button gnus-insert-next-page-button
1993       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1994      ("gnus-vis" :interactive t
1995       gnus-article-push-button gnus-article-press-button
1996       gnus-article-highlight gnus-article-highlight-some
1997       gnus-article-highlight-headers gnus-article-highlight-signature
1998       gnus-article-add-buttons gnus-article-add-buttons-to-head
1999       gnus-article-next-button gnus-article-prev-button)
2000      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2001       gnus-demon-add-disconnection gnus-demon-add-handler
2002       gnus-demon-remove-handler)
2003      ("gnus-demon" :interactive t
2004       gnus-demon-init gnus-demon-cancel)
2005      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2006       gnus-tree-open gnus-tree-close)
2007      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2008       gnus-nocem-unwanted-article-p)
2009      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2010      ("gnus-srvr" gnus-browse-foreign-server)
2011      ("gnus-cite" :interactive t
2012       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2013       gnus-article-hide-citation gnus-article-fill-cited-article)
2014      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2015       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2016       gnus-execute gnus-expunge)
2017      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2018       gnus-cache-possibly-remove-articles gnus-cache-request-article
2019       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2020       gnus-cache-enter-remove-article gnus-cached-article-p
2021       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2022      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2023       gnus-cache-remove-article)
2024      ("gnus-score" :interactive t
2025       gnus-summary-increase-score gnus-summary-lower-score
2026       gnus-score-flush-cache gnus-score-close
2027       gnus-score-raise-same-subject-and-select
2028       gnus-score-raise-same-subject gnus-score-default
2029       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2030       gnus-score-lower-same-subject gnus-score-lower-thread
2031       gnus-possibly-score-headers)
2032      ("gnus-score"
2033       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2034       gnus-current-score-file-nondirectory gnus-score-adaptive
2035       gnus-score-find-trace gnus-score-file-name)
2036      ("gnus-edit" :interactive t gnus-score-customize)
2037      ("gnus-topic" :interactive t gnus-topic-mode)
2038      ("gnus-topic" gnus-topic-remove-group)
2039      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2040      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2041      ("gnus-uu" :interactive t
2042       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2043       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2044       gnus-uu-mark-by-regexp gnus-uu-mark-all
2045       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2046       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2047       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2048       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2049       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2050       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2051       gnus-uu-decode-binhex-view)
2052      ("gnus-msg" (gnus-summary-send-map keymap)
2053       gnus-mail-yank-original gnus-mail-send-and-exit
2054       gnus-sendmail-setup-mail gnus-article-mail
2055       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2056      ("gnus-msg" :interactive t
2057       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2058       gnus-summary-followup gnus-summary-followup-with-original
2059       gnus-summary-followup-and-reply
2060       gnus-summary-followup-and-reply-with-original
2061       gnus-summary-cancel-article gnus-summary-supersede-article
2062       gnus-post-news gnus-inews-news gnus-cancel-news
2063       gnus-summary-reply gnus-summary-reply-with-original
2064       gnus-summary-mail-forward gnus-summary-mail-other-window
2065       gnus-bug)
2066      ("gnus-picon" :interactive t gnus-article-display-picons
2067       gnus-group-display-picons gnus-picons-article-display-x-face)
2068      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2069       gnus-grouplens-mode)
2070      ("gnus-vm" gnus-vm-mail-setup)
2071      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2072       gnus-summary-save-article-vm gnus-yank-article))))
2073
2074 \f
2075
2076 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2077 ;; If you want the cursor to go somewhere else, set these two
2078 ;; functions in some startup hook to whatever you want.
2079 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2080 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2081
2082 ;;; Various macros and substs.
2083
2084 (defun gnus-header-from (header)
2085   (mail-header-from header))
2086
2087 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2088   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2089   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2090     `(let ((,tempvar (selected-window)))
2091        (unwind-protect
2092            (progn
2093              (pop-to-buffer ,buffer)
2094              ,@forms)
2095          (select-window ,tempvar)))))
2096
2097 (defmacro gnus-gethash (string hashtable)
2098   "Get hash value of STRING in HASHTABLE."
2099   `(symbol-value (intern-soft ,string ,hashtable)))
2100
2101 (defmacro gnus-sethash (string value hashtable)
2102   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2103   `(set (intern ,string ,hashtable) ,value))
2104
2105 (defmacro gnus-intern-safe (string hashtable)
2106   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2107   `(let ((symbol (intern ,string ,hashtable)))
2108      (or (boundp symbol)
2109          (set symbol nil))
2110      symbol))
2111
2112 (defmacro gnus-group-unread (group)
2113   "Get the currently computed number of unread articles in GROUP."
2114   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2115
2116 (defmacro gnus-group-entry (group)
2117   "Get the newsrc entry for GROUP."
2118   `(gnus-gethash ,group gnus-newsrc-hashtb))
2119
2120 (defmacro gnus-active (group)
2121   "Get active info on GROUP."
2122   `(gnus-gethash ,group gnus-active-hashtb))
2123
2124 (defmacro gnus-set-active (group active)
2125   "Set GROUP's active info."
2126   `(gnus-sethash ,group ,active gnus-active-hashtb))
2127
2128 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2129 ;;   function `substring' might cut on a middle of multi-octet
2130 ;;   character.
2131 (defun gnus-truncate-string (str width)
2132   (substring str 0 width))
2133
2134 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2135 ;; to limit the length of a string.  This function is necessary since
2136 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2137 (defsubst gnus-limit-string (str width)
2138   (if (> (length str) width)
2139       (substring str 0 width)
2140     str))
2141
2142 (defsubst gnus-simplify-subject-re (subject)
2143   "Remove \"Re:\" from subject lines."
2144   (if (string-match "^[Rr][Ee]: *" subject)
2145       (substring subject (match-end 0))
2146     subject))
2147
2148 (defsubst gnus-functionp (form)
2149   "Return non-nil if FORM is funcallable."
2150   (or (and (symbolp form) (fboundp form))
2151       (and (listp form) (eq (car form) 'lambda))))
2152
2153 (defsubst gnus-goto-char (point)
2154   (and point (goto-char point)))
2155
2156 (defmacro gnus-buffer-exists-p (buffer)
2157   `(let ((buffer ,buffer))
2158      (and buffer
2159           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2160                    buffer))))
2161
2162 (defmacro gnus-kill-buffer (buffer)
2163   `(let ((buf ,buffer))
2164      (if (gnus-buffer-exists-p buf)
2165          (kill-buffer buf))))
2166
2167 (defsubst gnus-point-at-bol ()
2168   "Return point at the beginning of the line."
2169   (let ((p (point)))
2170     (beginning-of-line)
2171     (prog1
2172         (point)
2173       (goto-char p))))
2174
2175 (defsubst gnus-point-at-eol ()
2176   "Return point at the end of the line."
2177   (let ((p (point)))
2178     (end-of-line)
2179     (prog1
2180         (point)
2181       (goto-char p))))
2182
2183 (defun gnus-alive-p ()
2184   "Say whether Gnus is running or not."
2185   (and gnus-group-buffer
2186        (get-buffer gnus-group-buffer)))
2187
2188 ;; Delete the current line (and the next N lines.);
2189 (defmacro gnus-delete-line (&optional n)
2190   `(delete-region (progn (beginning-of-line) (point))
2191                   (progn (forward-line ,(or n 1)) (point))))
2192
2193 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2194 (defvar gnus-init-inhibit nil)
2195 (defun gnus-read-init-file (&optional inhibit-next)
2196   (if gnus-init-inhibit
2197       (setq gnus-init-inhibit nil)
2198     (setq gnus-init-inhibit inhibit-next)
2199     (and gnus-init-file
2200          (or (and (file-exists-p gnus-init-file)
2201                   ;; Don't try to load a directory.
2202                   (not (file-directory-p gnus-init-file)))
2203              (file-exists-p (concat gnus-init-file ".el"))
2204              (file-exists-p (concat gnus-init-file ".elc")))
2205          (condition-case var
2206              (load gnus-init-file nil t)
2207            (error
2208             (error "Error in %s: %s" gnus-init-file var))))))
2209
2210 ;; Info access macros.
2211
2212 (defmacro gnus-info-group (info)
2213   `(nth 0 ,info))
2214 (defmacro gnus-info-rank (info)
2215   `(nth 1 ,info))
2216 (defmacro gnus-info-read (info)
2217   `(nth 2 ,info))
2218 (defmacro gnus-info-marks (info)
2219   `(nth 3 ,info))
2220 (defmacro gnus-info-method (info)
2221   `(nth 4 ,info))
2222 (defmacro gnus-info-params (info)
2223   `(nth 5 ,info))
2224
2225 (defmacro gnus-info-level (info)
2226   `(let ((rank (gnus-info-rank ,info)))
2227      (if (consp rank)
2228          (car rank)
2229        rank)))
2230 (defmacro gnus-info-score (info)
2231   `(let ((rank (gnus-info-rank ,info)))
2232      (or (and (consp rank) (cdr rank)) 0)))
2233
2234 (defmacro gnus-info-set-group (info group)
2235   `(setcar ,info ,group))
2236 (defmacro gnus-info-set-rank (info rank)
2237   `(setcar (nthcdr 1 ,info) ,rank))
2238 (defmacro gnus-info-set-read (info read)
2239   `(setcar (nthcdr 2 ,info) ,read))
2240 (defmacro gnus-info-set-marks (info marks)
2241   `(setcar (nthcdr 3 ,info) ,marks))
2242 (defmacro gnus-info-set-method (info method)
2243   `(setcar (nthcdr 4 ,info) ,method))
2244 (defmacro gnus-info-set-params (info params)
2245   `(setcar (nthcdr 5 ,info) ,params))
2246
2247 (defmacro gnus-info-set-level (info level)
2248   `(let ((rank (cdr ,info)))
2249      (if (consp (car rank))
2250          (setcar (car rank) ,level)
2251        (setcar rank ,level))))
2252 (defmacro gnus-info-set-score (info score)
2253   `(let ((rank (cdr ,info)))
2254      (if (consp (car rank))
2255          (setcdr (car rank) ,score)
2256        (setcar rank (cons (car rank) ,score)))))
2257
2258 (defmacro gnus-get-info (group)
2259   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2260
2261 (defun gnus-byte-code (func)
2262   "Return a form that can be `eval'ed based on FUNC."
2263   (let ((fval (symbol-function func)))
2264     (if (byte-code-function-p fval)
2265         (let ((flist (append fval nil)))
2266           (setcar flist 'byte-code)
2267           flist)
2268       (cons 'progn (cddr fval)))))
2269
2270 ;;; Load the compatability functions.
2271
2272 (require 'gnus-cus)
2273 (require 'gnus-ems)
2274
2275 \f
2276 ;;;
2277 ;;; Shutdown
2278 ;;;
2279
2280 (defvar gnus-shutdown-alist nil)
2281
2282 (defun gnus-add-shutdown (function &rest symbols)
2283   "Run FUNCTION whenever one of SYMBOLS is shut down."
2284   (push (cons function symbols) gnus-shutdown-alist))
2285
2286 (defun gnus-shutdown (symbol)
2287   "Shut down everything that waits for SYMBOL."
2288   (let ((alist gnus-shutdown-alist)
2289         entry)
2290     (while (setq entry (pop alist))
2291       (when (memq symbol (cdr entry))
2292         (funcall (car entry))))))
2293
2294 \f
2295
2296 ;; Format specs.  The chunks below are the machine-generated forms
2297 ;; that are to be evaled as the result of the default format strings.
2298 ;; We write them in here to get them byte-compiled.  That way the
2299 ;; default actions will be quite fast, while still retaining the full
2300 ;; flexibility of the user-defined format specs.
2301
2302 ;; First we have lots of dummy defvars to let the compiler know these
2303 ;; are really dynamic variables.
2304
2305 (defvar gnus-tmp-unread)
2306 (defvar gnus-tmp-replied)
2307 (defvar gnus-tmp-score-char)
2308 (defvar gnus-tmp-indentation)
2309 (defvar gnus-tmp-opening-bracket)
2310 (defvar gnus-tmp-lines)
2311 (defvar gnus-tmp-name)
2312 (defvar gnus-tmp-closing-bracket)
2313 (defvar gnus-tmp-subject-or-nil)
2314 (defvar gnus-tmp-subject)
2315 (defvar gnus-tmp-marked)
2316 (defvar gnus-tmp-marked-mark)
2317 (defvar gnus-tmp-subscribed)
2318 (defvar gnus-tmp-process-marked)
2319 (defvar gnus-tmp-number-of-unread)
2320 (defvar gnus-tmp-group-name)
2321 (defvar gnus-tmp-group)
2322 (defvar gnus-tmp-article-number)
2323 (defvar gnus-tmp-unread-and-unselected)
2324 (defvar gnus-tmp-news-method)
2325 (defvar gnus-tmp-news-server)
2326 (defvar gnus-tmp-article-number)
2327 (defvar gnus-mouse-face)
2328 (defvar gnus-mouse-face-prop)
2329
2330 (defun gnus-summary-line-format-spec ()
2331   (insert gnus-tmp-unread gnus-tmp-replied
2332           gnus-tmp-score-char gnus-tmp-indentation)
2333   (put-text-property
2334    (point)
2335    (progn
2336      (insert
2337       gnus-tmp-opening-bracket
2338       (format "%4d: %-20s"
2339               gnus-tmp-lines
2340               (if (> (length gnus-tmp-name) 20)
2341                   (substring gnus-tmp-name 0 20)
2342                 gnus-tmp-name))
2343       gnus-tmp-closing-bracket)
2344      (point))
2345    gnus-mouse-face-prop gnus-mouse-face)
2346   (insert " " gnus-tmp-subject-or-nil "\n"))
2347
2348 (defvar gnus-summary-line-format-spec
2349   (gnus-byte-code 'gnus-summary-line-format-spec))
2350
2351 (defun gnus-summary-dummy-line-format-spec ()
2352   (insert "*  ")
2353   (put-text-property
2354    (point)
2355    (progn
2356      (insert ":                          :")
2357      (point))
2358    gnus-mouse-face-prop gnus-mouse-face)
2359   (insert " " gnus-tmp-subject "\n"))
2360
2361 (defvar gnus-summary-dummy-line-format-spec
2362   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2363
2364 (defun gnus-group-line-format-spec ()
2365   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2366           gnus-tmp-process-marked
2367           gnus-group-indentation
2368           (format "%5s: " gnus-tmp-number-of-unread))
2369   (put-text-property
2370    (point)
2371    (progn
2372      (insert gnus-tmp-group "\n")
2373      (1- (point)))
2374    gnus-mouse-face-prop gnus-mouse-face))
2375 (defvar gnus-group-line-format-spec
2376   (gnus-byte-code 'gnus-group-line-format-spec))
2377
2378 (defvar gnus-format-specs
2379   `((version . ,emacs-version)
2380     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2381     (summary-dummy ,gnus-summary-dummy-line-format
2382                    ,gnus-summary-dummy-line-format-spec)
2383     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2384
2385 (defvar gnus-article-mode-line-format-spec nil)
2386 (defvar gnus-summary-mode-line-format-spec nil)
2387 (defvar gnus-group-mode-line-format-spec nil)
2388
2389 ;;; Phew.  All that gruft is over, fortunately.
2390
2391 \f
2392 ;;;
2393 ;;; Gnus Utility Functions
2394 ;;;
2395
2396 (defun gnus-extract-address-components (from)
2397   (let (name address)
2398     ;; First find the address - the thing with the @ in it.  This may
2399     ;; not be accurate in mail addresses, but does the trick most of
2400     ;; the time in news messages.
2401     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2402         (setq address (substring from (match-beginning 0) (match-end 0))))
2403     ;; Then we check whether the "name <address>" format is used.
2404     (and address
2405          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2406          ;; Linear white space is not required.
2407          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2408          (and (setq name (substring from 0 (match-beginning 0)))
2409               ;; Strip any quotes from the name.
2410               (string-match "\".*\"" name)
2411               (setq name (substring name 1 (1- (match-end 0))))))
2412     ;; If not, then "address (name)" is used.
2413     (or name
2414         (and (string-match "(.+)" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (1- (match-end 0)))))
2417         (and (string-match "()" from)
2418              (setq name address))
2419         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2420         ;; XOVER might not support folded From headers.
2421         (and (string-match "(.*" from)
2422              (setq name (substring from (1+ (match-beginning 0))
2423                                    (match-end 0)))))
2424     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2425     (list (or name from) (or address from))))
2426
2427 (defun gnus-fetch-field (field)
2428   "Return the value of the header FIELD of current article."
2429   (save-excursion
2430     (save-restriction
2431       (let ((case-fold-search t)
2432             (inhibit-point-motion-hooks t))
2433         (nnheader-narrow-to-headers)
2434         (mail-fetch-field field)))))
2435
2436 (defun gnus-goto-colon ()
2437   (beginning-of-line)
2438   (search-forward ":" (gnus-point-at-eol) t))
2439
2440 ;;;###autoload
2441 (defun gnus-update-format (var)
2442   "Update the format specification near point."
2443   (interactive
2444    (list
2445     (save-excursion
2446       (eval-defun nil)
2447       ;; Find the end of the current word.
2448       (re-search-forward "[ \t\n]" nil t)
2449       ;; Search backward.
2450       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2451         (match-string 1)))))
2452   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2453                               (match-string 1 var))))
2454          (entry (assq type gnus-format-specs))
2455          value spec)
2456     (when entry
2457       (setq gnus-format-specs (delq entry gnus-format-specs)))
2458     (set
2459      (intern (format "%s-spec" var))
2460      (gnus-parse-format (setq value (symbol-value (intern var)))
2461                         (symbol-value (intern (format "%s-alist" var)))
2462                         (not (string-match "mode" var))))
2463     (setq spec (symbol-value (intern (format "%s-spec" var))))
2464     (push (list type value spec) gnus-format-specs)
2465
2466     (pop-to-buffer "*Gnus Format*")
2467     (erase-buffer)
2468     (lisp-interaction-mode)
2469     (insert (pp-to-string spec))))
2470
2471 (defun gnus-update-format-specifications (&optional force)
2472   "Update all (necessary) format specifications."
2473   ;; Make the indentation array.
2474   (gnus-make-thread-indent-array)
2475
2476   ;; See whether all the stored info needs to be flushed.
2477   (when (or force
2478             (not (equal emacs-version
2479                         (cdr (assq 'version gnus-format-specs)))))
2480     (setq gnus-format-specs nil))
2481
2482   ;; Go through all the formats and see whether they need updating.
2483   (let ((types '(summary summary-dummy group
2484                          summary-mode group-mode article-mode))
2485         new-format entry type val)
2486     (while (setq type (pop types))
2487       ;; Jump to the proper buffer to find out the value of
2488       ;; the variable, if possible.  (It may be buffer-local.)
2489       (save-excursion
2490         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2491               val)
2492           (when (and (boundp buffer)
2493                      (setq val (symbol-value buffer))
2494                      (get-buffer val)
2495                      (buffer-name (get-buffer val)))
2496             (set-buffer (get-buffer val)))
2497           (setq new-format (symbol-value
2498                             (intern (format "gnus-%s-line-format" type))))))
2499       (setq entry (cdr (assq type gnus-format-specs)))
2500       (if (and entry
2501                (equal (car entry) new-format))
2502           ;; Use the old format.
2503           (set (intern (format "gnus-%s-line-format-spec" type))
2504                (cadr entry))
2505         ;; This is a new format.
2506         (setq val
2507               (if (not (stringp new-format))
2508                   ;; This is a function call or something.
2509                   new-format
2510                 ;; This is a "real" format.
2511                 (gnus-parse-format
2512                  new-format
2513                  (symbol-value
2514                   (intern (format "gnus-%s-line-format-alist"
2515                                   (if (eq type 'article-mode)
2516                                       'summary-mode type))))
2517                  (not (string-match "mode$" (symbol-name type))))))
2518         ;; Enter the new format spec into the list.
2519         (if entry
2520             (progn
2521               (setcar (cdr entry) val)
2522               (setcar entry new-format))
2523           (push (list type new-format val) gnus-format-specs))
2524         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2525
2526   (unless (assq 'version gnus-format-specs)
2527     (push (cons 'version emacs-version) gnus-format-specs))
2528
2529   (gnus-update-group-mark-positions)
2530   (gnus-update-summary-mark-positions))
2531
2532 (defun gnus-update-summary-mark-positions ()
2533   "Compute where the summary marks are to go."
2534   (save-excursion
2535     (let ((gnus-replied-mark 129)
2536           (gnus-score-below-mark 130)
2537           (gnus-score-over-mark 130)
2538           (thread nil)
2539           (gnus-visual nil)
2540           pos)
2541       (gnus-set-work-buffer)
2542       (gnus-summary-insert-line
2543        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2544       (goto-char (point-min))
2545       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2546                                          (- (point) 2)))))
2547       (goto-char (point-min))
2548       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2549             pos)
2550       (goto-char (point-min))
2551       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2552             pos)
2553       (setq gnus-summary-mark-positions pos))))
2554
2555 (defun gnus-update-group-mark-positions ()
2556   (save-excursion
2557     (let ((gnus-process-mark 128)
2558           (gnus-group-marked '("dummy.group")))
2559       (gnus-set-active "dummy.group" '(0 . 0))
2560       (gnus-set-work-buffer)
2561       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2562       (goto-char (point-min))
2563       (setq gnus-group-mark-positions
2564             (list (cons 'process (and (search-forward "\200" nil t)
2565                                       (- (point) 2))))))))
2566
2567 (defvar gnus-mouse-face-0 'highlight)
2568 (defvar gnus-mouse-face-1 'highlight)
2569 (defvar gnus-mouse-face-2 'highlight)
2570 (defvar gnus-mouse-face-3 'highlight)
2571 (defvar gnus-mouse-face-4 'highlight)
2572
2573 (defun gnus-mouse-face-function (form type)
2574   `(put-text-property
2575     (point) (progn ,@form (point))
2576     gnus-mouse-face-prop
2577     ,(if (equal type 0)
2578          'gnus-mouse-face
2579        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2580
2581 (defvar gnus-face-0 'bold)
2582 (defvar gnus-face-1 'italic)
2583 (defvar gnus-face-2 'bold-italic)
2584 (defvar gnus-face-3 'bold)
2585 (defvar gnus-face-4 'bold)
2586
2587 (defun gnus-face-face-function (form type)
2588   `(put-text-property
2589     (point) (progn ,@form (point))
2590     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2591
2592 (defun gnus-max-width-function (el max-width)
2593   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2594   (if (symbolp el)
2595       `(if (> (length ,el) ,max-width)
2596            (substring ,el 0 ,max-width)
2597          ,el)
2598     `(let ((val (eval ,el)))
2599        (if (numberp val)
2600            (setq val (int-to-string val)))
2601        (if (> (length val) ,max-width)
2602            (substring val 0 ,max-width)
2603          val))))
2604
2605 (defun gnus-parse-format (format spec-alist &optional insert)
2606   ;; This function parses the FORMAT string with the help of the
2607   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2608   ;; string.  If the FORMAT string contains the specifiers %( and %)
2609   ;; the text between them will have the mouse-face text property.
2610   (if (string-match
2611        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2612        format)
2613       (gnus-parse-complex-format format spec-alist)
2614     ;; This is a simple format.
2615     (gnus-parse-simple-format format spec-alist insert)))
2616
2617 (defun gnus-parse-complex-format (format spec-alist)
2618   (save-excursion
2619     (gnus-set-work-buffer)
2620     (insert format)
2621     (goto-char (point-min))
2622     (while (re-search-forward "\"" nil t)
2623       (replace-match "\\\"" nil t))
2624     (goto-char (point-min))
2625     (insert "(\"")
2626     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2627       (let ((number (if (match-beginning 1)
2628                         (match-string 1) "0"))
2629             (delim (aref (match-string 2) 0)))
2630         (if (or (= delim ?\() (= delim ?\{))
2631             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2632                                    " " number " \""))
2633           (replace-match "\")\""))))
2634     (goto-char (point-max))
2635     (insert "\")")
2636     (goto-char (point-min))
2637     (let ((form (read (current-buffer))))
2638       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2639
2640 (defun gnus-complex-form-to-spec (form spec-alist)
2641   (delq nil
2642         (mapcar
2643          (lambda (sform)
2644            (if (stringp sform)
2645                (gnus-parse-simple-format sform spec-alist t)
2646              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2647                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2648                       (nth 1 sform))))
2649          form)))
2650
2651 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2652   ;; This function parses the FORMAT string with the help of the
2653   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2654   ;; string.
2655   (let ((max-width 0)
2656         spec flist fstring newspec elem beg result dontinsert)
2657     (save-excursion
2658       (gnus-set-work-buffer)
2659       (insert format)
2660       (goto-char (point-min))
2661       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2662                                 nil t)
2663         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2664               (setq newspec "%"
2665                     beg (1+ (match-beginning 0)))
2666           ;; First check if there are any specs that look anything like
2667           ;; "%12,12A", ie. with a "max width specification".  These have
2668           ;; to be treated specially.
2669           (if (setq beg (match-beginning 1))
2670               (setq max-width
2671                     (string-to-int
2672                      (buffer-substring
2673                       (1+ (match-beginning 1)) (match-end 1))))
2674             (setq max-width 0)
2675             (setq beg (match-beginning 2)))
2676           ;; Find the specification from `spec-alist'.
2677           (unless (setq elem (cdr (assq spec spec-alist)))
2678             (setq elem '("*" ?s)))
2679           ;; Treat user defined format specifiers specially.
2680           (when (eq (car elem) 'gnus-tmp-user-defined)
2681             (setq elem
2682                   (list
2683                    (list (intern (concat "gnus-user-format-function-"
2684                                          (match-string 3)))
2685                          'gnus-tmp-header) ?s))
2686             (delete-region (match-beginning 3) (match-end 3)))
2687           (if (not (zerop max-width))
2688               (let ((el (car elem)))
2689                 (cond ((= (cadr elem) ?c)
2690                        (setq el (list 'char-to-string el)))
2691                       ((= (cadr elem) ?d)
2692                        (setq el (list 'int-to-string el))))
2693                 (setq flist (cons (gnus-max-width-function el max-width)
2694                                   flist))
2695                 (setq newspec ?s))
2696             (progn
2697               (setq flist (cons (car elem) flist))
2698               (setq newspec (cadr elem)))))
2699         ;; Remove the old specification (and possibly a ",12" string).
2700         (delete-region beg (match-end 2))
2701         ;; Insert the new specification.
2702         (goto-char beg)
2703         (insert newspec))
2704       (setq fstring (buffer-substring 1 (point-max))))
2705     ;; Do some postprocessing to increase efficiency.
2706     (setq
2707      result
2708      (cond
2709       ;; Emptyness.
2710       ((string= fstring "")
2711        nil)
2712       ;; Not a format string.
2713       ((not (string-match "%" fstring))
2714        (list fstring))
2715       ;; A format string with just a single string spec.
2716       ((string= fstring "%s")
2717        (list (car flist)))
2718       ;; A single character.
2719       ((string= fstring "%c")
2720        (list (car flist)))
2721       ;; A single number.
2722       ((string= fstring "%d")
2723        (setq dontinsert)
2724        (if insert
2725            (list `(princ ,(car flist)))
2726          (list `(int-to-string ,(car flist)))))
2727       ;; Just lots of chars and strings.
2728       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2729        (nreverse flist))
2730       ;; A single string spec at the beginning of the spec.
2731       ((string-match "\\`%[sc][^%]+\\'" fstring)
2732        (list (car flist) (substring fstring 2)))
2733       ;; A single string spec in the middle of the spec.
2734       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2735        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2736       ;; A single string spec in the end of the spec.
2737       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2738        (list (match-string 1 fstring) (car flist)))
2739       ;; A more complex spec.
2740       (t
2741        (list (cons 'format (cons fstring (nreverse flist)))))))
2742
2743     (if insert
2744         (when result
2745           (if dontinsert
2746               result
2747             (cons 'insert result)))
2748       (cond ((stringp result)
2749              result)
2750             ((consp result)
2751              (cons 'concat result))
2752             (t "")))))
2753
2754 (defun gnus-eval-format (format &optional alist props)
2755   "Eval the format variable FORMAT, using ALIST.
2756 If PROPS, insert the result."
2757   (let ((form (gnus-parse-format format alist props)))
2758     (if props
2759         (add-text-properties (point) (progn (eval form) (point)) props)
2760       (eval form))))
2761
2762 (defun gnus-remove-text-with-property (prop)
2763   "Delete all text in the current buffer with text property PROP."
2764   (save-excursion
2765     (goto-char (point-min))
2766     (while (not (eobp))
2767       (while (get-text-property (point) prop)
2768         (delete-char 1))
2769       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2770
2771 (defun gnus-set-work-buffer ()
2772   (if (get-buffer gnus-work-buffer)
2773       (progn
2774         (set-buffer gnus-work-buffer)
2775         (erase-buffer))
2776     (set-buffer (get-buffer-create gnus-work-buffer))
2777     (kill-all-local-variables)
2778     (buffer-disable-undo (current-buffer))
2779     (gnus-add-current-to-buffer-list)))
2780
2781 ;; Article file names when saving.
2782
2783 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2784   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2785 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2786 Otherwise, it is like ~/News/news/group/num."
2787   (let ((default
2788           (expand-file-name
2789            (concat (if (gnus-use-long-file-name 'not-save)
2790                        (gnus-capitalize-newsgroup newsgroup)
2791                      (gnus-newsgroup-directory-form newsgroup))
2792                    "/" (int-to-string (mail-header-number headers)))
2793            (or gnus-article-save-directory "~/News"))))
2794     (if (and last-file
2795              (string-equal (file-name-directory default)
2796                            (file-name-directory last-file))
2797              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2798         default
2799       (or last-file default))))
2800
2801 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2802   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2803 If variable `gnus-use-long-file-name' is non-nil, it is
2804 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2805   (let ((default
2806           (expand-file-name
2807            (concat (if (gnus-use-long-file-name 'not-save)
2808                        newsgroup
2809                      (gnus-newsgroup-directory-form newsgroup))
2810                    "/" (int-to-string (mail-header-number headers)))
2811            (or gnus-article-save-directory "~/News"))))
2812     (if (and last-file
2813              (string-equal (file-name-directory default)
2814                            (file-name-directory last-file))
2815              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2816         default
2817       (or last-file default))))
2818
2819 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2820   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2821 If variable `gnus-use-long-file-name' is non-nil, it is
2822 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2823   (or last-file
2824       (expand-file-name
2825        (if (gnus-use-long-file-name 'not-save)
2826            (gnus-capitalize-newsgroup newsgroup)
2827          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2828        (or gnus-article-save-directory "~/News"))))
2829
2830 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2831   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2832 If variable `gnus-use-long-file-name' is non-nil, it is
2833 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2834   (or last-file
2835       (expand-file-name
2836        (if (gnus-use-long-file-name 'not-save)
2837            newsgroup
2838          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2839        (or gnus-article-save-directory "~/News"))))
2840
2841 ;; For subscribing new newsgroup
2842
2843 (defun gnus-subscribe-hierarchical-interactive (groups)
2844   (let ((groups (sort groups 'string<))
2845         prefixes prefix start ans group starts)
2846     (while groups
2847       (setq prefixes (list "^"))
2848       (while (and groups prefixes)
2849         (while (not (string-match (car prefixes) (car groups)))
2850           (setq prefixes (cdr prefixes)))
2851         (setq prefix (car prefixes))
2852         (setq start (1- (length prefix)))
2853         (if (and (string-match "[^\\.]\\." (car groups) start)
2854                  (cdr groups)
2855                  (setq prefix
2856                        (concat "^" (substring (car groups) 0 (match-end 0))))
2857                  (string-match prefix (cadr groups)))
2858             (progn
2859               (setq prefixes (cons prefix prefixes))
2860               (message "Descend hierarchy %s? ([y]nsq): "
2861                        (substring prefix 1 (1- (length prefix))))
2862               (setq ans (read-char))
2863               (cond ((= ans ?n)
2864                      (while (and groups
2865                                  (string-match prefix
2866                                                (setq group (car groups))))
2867                        (setq gnus-killed-list
2868                              (cons group gnus-killed-list))
2869                        (gnus-sethash group group gnus-killed-hashtb)
2870                        (setq groups (cdr groups)))
2871                      (setq starts (cdr starts)))
2872                     ((= ans ?s)
2873                      (while (and groups
2874                                  (string-match prefix
2875                                                (setq group (car groups))))
2876                        (gnus-sethash group group gnus-killed-hashtb)
2877                        (gnus-subscribe-alphabetically (car groups))
2878                        (setq groups (cdr groups)))
2879                      (setq starts (cdr starts)))
2880                     ((= ans ?q)
2881                      (while groups
2882                        (setq group (car groups))
2883                        (setq gnus-killed-list (cons group gnus-killed-list))
2884                        (gnus-sethash group group gnus-killed-hashtb)
2885                        (setq groups (cdr groups))))
2886                     (t nil)))
2887           (message "Subscribe %s? ([n]yq)" (car groups))
2888           (setq ans (read-char))
2889           (setq group (car groups))
2890           (cond ((= ans ?y)
2891                  (gnus-subscribe-alphabetically (car groups))
2892                  (gnus-sethash group group gnus-killed-hashtb))
2893                 ((= ans ?q)
2894                  (while groups
2895                    (setq group (car groups))
2896                    (setq gnus-killed-list (cons group gnus-killed-list))
2897                    (gnus-sethash group group gnus-killed-hashtb)
2898                    (setq groups (cdr groups))))
2899                 (t
2900                  (setq gnus-killed-list (cons group gnus-killed-list))
2901                  (gnus-sethash group group gnus-killed-hashtb)))
2902           (setq groups (cdr groups)))))))
2903
2904 (defun gnus-subscribe-randomly (newsgroup)
2905   "Subscribe new NEWSGROUP by making it the first newsgroup."
2906   (gnus-subscribe-newsgroup newsgroup))
2907
2908 (defun gnus-subscribe-alphabetically (newgroup)
2909   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2910   (let ((groups (cdr gnus-newsrc-alist))
2911         before)
2912     (while (and (not before) groups)
2913       (if (string< newgroup (caar groups))
2914           (setq before (caar groups))
2915         (setq groups (cdr groups))))
2916     (gnus-subscribe-newsgroup newgroup before)))
2917
2918 (defun gnus-subscribe-hierarchically (newgroup)
2919   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2920   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2921   (save-excursion
2922     (set-buffer (find-file-noselect gnus-current-startup-file))
2923     (let ((groupkey newgroup)
2924           before)
2925       (while (and (not before) groupkey)
2926         (goto-char (point-min))
2927         (let ((groupkey-re
2928                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2929           (while (and (re-search-forward groupkey-re nil t)
2930                       (progn
2931                         (setq before (match-string 1))
2932                         (string< before newgroup)))))
2933         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2934         (setq groupkey
2935               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2936                   (substring groupkey (match-beginning 1) (match-end 1)))))
2937       (gnus-subscribe-newsgroup newgroup before))))
2938
2939 (defun gnus-subscribe-interactively (group)
2940   "Subscribe the new GROUP interactively.
2941 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2942 it is killed."
2943   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2944       (gnus-subscribe-hierarchically group)
2945     (push group gnus-killed-list)))
2946
2947 (defun gnus-subscribe-zombies (group)
2948   "Make the new GROUP into a zombie group."
2949   (push group gnus-zombie-list))
2950
2951 (defun gnus-subscribe-killed (group)
2952   "Make the new GROUP a killed group."
2953   (push group gnus-killed-list))
2954
2955 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2956   "Subscribe new NEWSGROUP.
2957 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2958 the first newsgroup."
2959   ;; We subscribe the group by changing its level to `subscribed'.
2960   (gnus-group-change-level
2961    newsgroup gnus-level-default-subscribed
2962    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2963   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2964
2965 ;; For directories
2966
2967 (defun gnus-newsgroup-directory-form (newsgroup)
2968   "Make hierarchical directory name from NEWSGROUP name."
2969   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2970         (len (length newsgroup))
2971         idx)
2972     ;; If this is a foreign group, we don't want to translate the
2973     ;; entire name.
2974     (if (setq idx (string-match ":" newsgroup))
2975         (aset newsgroup idx ?/)
2976       (setq idx 0))
2977     ;; Replace all occurrences of `.' with `/'.
2978     (while (< idx len)
2979       (if (= (aref newsgroup idx) ?.)
2980           (aset newsgroup idx ?/))
2981       (setq idx (1+ idx)))
2982     newsgroup))
2983
2984 (defun gnus-newsgroup-savable-name (group)
2985   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2986   ;; with dots.
2987   (nnheader-replace-chars-in-string group ?/ ?.))
2988
2989 (defun gnus-make-directory (dir)
2990   "Make DIRECTORY recursively."
2991   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2992   ;; of the many mysteries of the universe.
2993   (let* ((dir (expand-file-name dir default-directory))
2994          dirs err)
2995     (if (string-match "/$" dir)
2996         (setq dir (substring dir 0 (match-beginning 0))))
2997     ;; First go down the path until we find a directory that exists.
2998     (while (not (file-exists-p dir))
2999       (setq dirs (cons dir dirs))
3000       (string-match "/[^/]+$" dir)
3001       (setq dir (substring dir 0 (match-beginning 0))))
3002     ;; Then create all the subdirs.
3003     (while (and dirs (not err))
3004       (condition-case ()
3005           (make-directory (car dirs))
3006         (error (setq err t)))
3007       (setq dirs (cdr dirs)))
3008     ;; We return whether we were successful or not.
3009     (not dirs)))
3010
3011 (defun gnus-capitalize-newsgroup (newsgroup)
3012   "Capitalize NEWSGROUP name."
3013   (and (not (zerop (length newsgroup)))
3014        (concat (char-to-string (upcase (aref newsgroup 0)))
3015                (substring newsgroup 1))))
3016
3017 ;; Various... things.
3018
3019 (defun gnus-simplify-subject (subject &optional re-only)
3020   "Remove `Re:' and words in parentheses.
3021 If RE-ONLY is non-nil, strip leading `Re:'s only."
3022   (let ((case-fold-search t))           ;Ignore case.
3023     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3024     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3025       (setq subject (substring subject (match-end 0))))
3026     ;; Remove uninteresting prefixes.
3027     (if (and (not re-only)
3028              gnus-simplify-ignored-prefixes
3029              (string-match gnus-simplify-ignored-prefixes subject))
3030         (setq subject (substring subject (match-end 0))))
3031     ;; Remove words in parentheses from end.
3032     (unless re-only
3033       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3034         (setq subject (substring subject 0 (match-beginning 0)))))
3035     ;; Return subject string.
3036     subject))
3037
3038 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3039 ;; all whitespace.
3040 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3041 (defun gnus-simplify-buffer-fuzzy ()
3042   (goto-char (point-min))
3043   (while (search-forward "\t" nil t)
3044     (replace-match " " t t))
3045   (goto-char (point-min))
3046   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3047   (goto-char (match-beginning 0))
3048   (while (or
3049           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3050           (looking-at "^[[].*: .*[]]$"))
3051     (goto-char (point-min))
3052     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3053                               nil t)
3054       (replace-match "" t t))
3055     (goto-char (point-min))
3056     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3057       (goto-char (match-end 0))
3058       (delete-char -1)
3059       (delete-region
3060        (progn (goto-char (match-beginning 0)))
3061        (re-search-forward ":"))))
3062   (goto-char (point-min))
3063   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3064     (replace-match "" t t))
3065   (goto-char (point-min))
3066   (while (re-search-forward "  +" nil t)
3067     (replace-match " " t t))
3068   (goto-char (point-min))
3069   (while (re-search-forward " $" nil t)
3070     (replace-match "" t t))
3071   (goto-char (point-min))
3072   (while (re-search-forward "^ +" nil t)
3073     (replace-match "" t t))
3074   (goto-char (point-min))
3075   (when gnus-simplify-subject-fuzzy-regexp
3076     (if (listp gnus-simplify-subject-fuzzy-regexp)
3077         (let ((list gnus-simplify-subject-fuzzy-regexp))
3078           (while list
3079             (goto-char (point-min))
3080             (while (re-search-forward (car list) nil t)
3081               (replace-match "" t t))
3082             (setq list (cdr list))))
3083       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3084         (replace-match "" t t)))))
3085
3086 (defun gnus-simplify-subject-fuzzy (subject)
3087   "Siplify a subject string fuzzily."
3088   (save-excursion
3089     (gnus-set-work-buffer)
3090     (let ((case-fold-search t))
3091       (insert subject)
3092       (inline (gnus-simplify-buffer-fuzzy))
3093       (buffer-string))))
3094
3095 ;; Add the current buffer to the list of buffers to be killed on exit.
3096 (defun gnus-add-current-to-buffer-list ()
3097   (or (memq (current-buffer) gnus-buffer-list)
3098       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3099
3100 (defun gnus-string> (s1 s2)
3101   (not (or (string< s1 s2)
3102            (string= s1 s2))))
3103
3104 ;;; General various misc type functions.
3105
3106 (defun gnus-clear-system ()
3107   "Clear all variables and buffers."
3108   ;; Clear Gnus variables.
3109   (let ((variables gnus-variable-list))
3110     (while variables
3111       (set (car variables) nil)
3112       (setq variables (cdr variables))))
3113   ;; Clear other internal variables.
3114   (setq gnus-list-of-killed-groups nil
3115         gnus-have-read-active-file nil
3116         gnus-newsrc-alist nil
3117         gnus-newsrc-hashtb nil
3118         gnus-killed-list nil
3119         gnus-zombie-list nil
3120         gnus-killed-hashtb nil
3121         gnus-active-hashtb nil
3122         gnus-moderated-list nil
3123         gnus-description-hashtb nil
3124         gnus-current-headers nil
3125         gnus-thread-indent-array nil
3126         gnus-newsgroup-headers nil
3127         gnus-newsgroup-name nil
3128         gnus-server-alist nil
3129         gnus-group-list-mode nil
3130         gnus-opened-servers nil
3131         gnus-current-select-method nil)
3132   (gnus-shutdown 'gnus)
3133   ;; Kill the startup file.
3134   (and gnus-current-startup-file
3135        (get-file-buffer gnus-current-startup-file)
3136        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3137   ;; Clear the dribble buffer.
3138   (gnus-dribble-clear)
3139   ;; Kill global KILL file buffer.
3140   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3141     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3142   (gnus-kill-buffer nntp-server-buffer)
3143   ;; Kill Gnus buffers.
3144   (while gnus-buffer-list
3145     (gnus-kill-buffer (pop gnus-buffer-list)))
3146   ;; Remove Gnus frames.
3147   (while gnus-created-frames
3148     (when (frame-live-p (car gnus-created-frames))
3149       ;; We slap a condition-case around this `delete-frame' to ensure 
3150       ;; agains errors if we try do delete the single frame that's left.
3151       (condition-case ()
3152           (delete-frame (car gnus-created-frames))
3153         (error nil)))
3154     (pop gnus-created-frames)))
3155
3156 (defun gnus-windows-old-to-new (setting)
3157   ;; First we take care of the really, really old Gnus 3 actions.
3158   (when (symbolp setting)
3159     (setq setting
3160           ;; Take care of ooold GNUS 3.x values.
3161           (cond ((eq setting 'SelectArticle) 'article)
3162                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3163                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3164                 (t setting))))
3165   (if (or (listp setting)
3166           (not (and gnus-window-configuration
3167                     (memq setting '(group summary article)))))
3168       setting
3169     (let* ((setting (if (eq setting 'group)
3170                         (if (assq 'newsgroup gnus-window-configuration)
3171                             'newsgroup
3172                           'newsgroups) setting))
3173            (elem (cadr (assq setting gnus-window-configuration)))
3174            (total (apply '+ elem))
3175            (types '(group summary article))
3176            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3177            (i 0)
3178            perc
3179            out)
3180       (while (< i 3)
3181         (or (not (numberp (nth i elem)))
3182             (zerop (nth i elem))
3183             (progn
3184               (setq perc  (/ (float (nth 0 elem)) total))
3185               (setq out (cons (if (eq pbuf (nth i types))
3186                                   (vector (nth i types) perc 'point)
3187                                 (vector (nth i types) perc))
3188                               out))))
3189         (setq i (1+ i)))
3190       (list (nreverse out)))))
3191
3192 ;;;###autoload
3193 (defun gnus-add-configuration (conf)
3194   "Add the window configuration CONF to `gnus-buffer-configuration'."
3195   (setq gnus-buffer-configuration
3196         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3197                          gnus-buffer-configuration))))
3198
3199 (defvar gnus-frame-list nil)
3200
3201 (defun gnus-configure-frame (split &optional window)
3202   "Split WINDOW according to SPLIT."
3203   (unless window
3204     (setq window (get-buffer-window (current-buffer))))
3205   (select-window window)
3206   ;; This might be an old-stylee buffer config.
3207   (when (vectorp split)
3208     (setq split (append split nil)))
3209   (when (or (consp (car split))
3210             (vectorp (car split)))
3211     (push 1.0 split)
3212     (push 'vertical split))
3213   ;; The SPLIT might be something that is to be evaled to
3214   ;; return a new SPLIT.
3215   (while (and (not (assq (car split) gnus-window-to-buffer))
3216               (gnus-functionp (car split)))
3217     (setq split (eval split)))
3218   (let* ((type (car split))
3219          (subs (cddr split))
3220          (len (if (eq type 'horizontal) (window-width) (window-height)))
3221          (total 0)
3222          (window-min-width (or gnus-window-min-width window-min-width))
3223          (window-min-height (or gnus-window-min-height window-min-height))
3224          s result new-win rest comp-subs size sub)
3225     (cond
3226      ;; Nothing to do here.
3227      ((null split))
3228      ;; Don't switch buffers.
3229      ((null type)
3230       (and (memq 'point split) window))
3231      ;; This is a buffer to be selected.
3232      ((not (memq type '(frame horizontal vertical)))
3233       (let ((buffer (cond ((stringp type) type)
3234                           (t (cdr (assq type gnus-window-to-buffer)))))
3235             buf)
3236         (unless buffer
3237           (error "Illegal buffer type: %s" type))
3238         (unless (setq buf (get-buffer (if (symbolp buffer)
3239                                           (symbol-value buffer) buffer)))
3240           (setq buf (get-buffer-create (if (symbolp buffer)
3241                                            (symbol-value buffer) buffer))))
3242         (switch-to-buffer buf)
3243         ;; We return the window if it has the `point' spec.
3244         (and (memq 'point split) window)))
3245      ;; This is a frame split.
3246      ((eq type 'frame)
3247       (unless gnus-frame-list
3248         (setq gnus-frame-list (list (window-frame
3249                                      (get-buffer-window (current-buffer))))))
3250       (let ((i 0)
3251             params frame fresult)
3252         (while (< i (length subs))
3253           ;; Frame parameter is gotten from the sub-split.
3254           (setq params (cadr (elt subs i)))
3255           ;; It should be a list.
3256           (unless (listp params)
3257             (setq params nil))
3258           ;; Create a new frame?
3259           (unless (setq frame (elt gnus-frame-list i))
3260             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3261             (push frame gnus-created-frames))
3262           ;; Is the old frame still alive?
3263           (unless (frame-live-p frame)
3264             (setcar (nthcdr i gnus-frame-list)
3265                     (setq frame (make-frame params))))
3266           ;; Select the frame in question and do more splits there.
3267           (select-frame frame)
3268           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3269           (incf i))
3270         ;; Select the frame that has the selected buffer.
3271         (when fresult
3272           (select-frame (window-frame fresult)))))
3273      ;; This is a normal split.
3274      (t
3275       (when (> (length subs) 0)
3276         ;; First we have to compute the sizes of all new windows.
3277         (while subs
3278           (setq sub (append (pop subs) nil))
3279           (while (and (not (assq (car sub) gnus-window-to-buffer))
3280                       (gnus-functionp (car sub)))
3281             (setq sub (eval sub)))
3282           (when sub
3283             (push sub comp-subs)
3284             (setq size (cadar comp-subs))
3285             (cond ((equal size 1.0)
3286                    (setq rest (car comp-subs))
3287                    (setq s 0))
3288                   ((floatp size)
3289                    (setq s (floor (* size len))))
3290                   ((integerp size)
3291                    (setq s size))
3292                   (t
3293                    (error "Illegal size: %s" size)))
3294             ;; Try to make sure that we are inside the safe limits.
3295             (cond ((zerop s))
3296                   ((eq type 'horizontal)
3297                    (setq s (max s window-min-width)))
3298                   ((eq type 'vertical)
3299                    (setq s (max s window-min-height))))
3300             (setcar (cdar comp-subs) s)
3301             (incf total s)))
3302         ;; Take care of the "1.0" spec.
3303         (if rest
3304             (setcar (cdr rest) (- len total))
3305           (error "No 1.0 specs in %s" split))
3306         ;; The we do the actual splitting in a nice recursive
3307         ;; fashion.
3308         (setq comp-subs (nreverse comp-subs))
3309         (while comp-subs
3310           (if (null (cdr comp-subs))
3311               (setq new-win window)
3312             (setq new-win
3313                   (split-window window (cadar comp-subs)
3314                                 (eq type 'horizontal))))
3315           (setq result (or (gnus-configure-frame
3316                             (car comp-subs) window) result))
3317           (select-window new-win)
3318           (setq window new-win)
3319           (setq comp-subs (cdr comp-subs))))
3320       ;; Return the proper window, if any.
3321       (when result
3322         (select-window result))))))
3323
3324 (defvar gnus-frame-split-p nil)
3325
3326 (defun gnus-configure-windows (setting &optional force)
3327   (setq setting (gnus-windows-old-to-new setting))
3328   (let ((split (if (symbolp setting)
3329                    (cadr (assq setting gnus-buffer-configuration))
3330                  setting))
3331         all-visible)
3332
3333     (setq gnus-frame-split-p nil)
3334
3335     (unless split
3336       (error "No such setting: %s" setting))
3337
3338     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3339              (not force))
3340         ;; All the windows mentioned are already visible, so we just
3341         ;; put point in the assigned buffer, and do not touch the
3342         ;; winconf.
3343         (select-window all-visible)
3344
3345       ;; Either remove all windows or just remove all Gnus windows.
3346       (let ((frame (selected-frame)))
3347         (unwind-protect
3348             (if gnus-use-full-window
3349                 ;; We want to remove all other windows.
3350                 (if (not gnus-frame-split-p)
3351                     ;; This is not a `frame' split, so we ignore the
3352                     ;; other frames.  
3353                     (delete-other-windows)
3354                   ;; This is a `frame' split, so we delete all windows
3355                   ;; on all frames.
3356                   (mapcar 
3357                    (lambda (frame)
3358                      (unless (eq (cdr (assq 'minibuffer
3359                                             (frame-parameters frame)))
3360                                  'only)
3361                        (select-frame frame)
3362                        (delete-other-windows)))
3363                    (frame-list)))
3364               ;; Just remove some windows.
3365               (gnus-remove-some-windows)
3366               (switch-to-buffer nntp-server-buffer))
3367           (select-frame frame)))
3368
3369       (switch-to-buffer nntp-server-buffer)
3370       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3371
3372 (defun gnus-all-windows-visible-p (split)
3373   "Say whether all buffers in SPLIT are currently visible.
3374 In particular, the value returned will be the window that
3375 should have point."
3376   (let ((stack (list split))
3377         (all-visible t)
3378         type buffer win buf)
3379     (while (and (setq split (pop stack))
3380                 all-visible)
3381       ;; Be backwards compatible.
3382       (when (vectorp split)
3383         (setq split (append split nil)))
3384       (when (or (consp (car split))
3385                 (vectorp (car split)))
3386         (push 1.0 split)
3387         (push 'vertical split))
3388       ;; The SPLIT might be something that is to be evaled to
3389       ;; return a new SPLIT.
3390       (while (and (not (assq (car split) gnus-window-to-buffer))
3391                   (gnus-functionp (car split)))
3392         (setq split (eval split)))
3393
3394       (setq type (elt split 0))
3395       (cond
3396        ;; Nothing here.
3397        ((null split) t)
3398        ;; A buffer.
3399        ((not (memq type '(horizontal vertical frame)))
3400         (setq buffer (cond ((stringp type) type)
3401                            (t (cdr (assq type gnus-window-to-buffer)))))
3402         (unless buffer
3403           (error "Illegal buffer type: %s" type))
3404         (when (setq buf (get-buffer (if (symbolp buffer)
3405                                         (symbol-value buffer)
3406                                       buffer)))
3407           (setq win (get-buffer-window buf t)))
3408         (if win
3409             (when (memq 'point split)
3410                 (setq all-visible win))
3411           (setq all-visible nil)))
3412        (t
3413         (when (eq type 'frame)
3414           (setq gnus-frame-split-p t))
3415         (setq stack (append (cddr split) stack)))))
3416     (unless (eq all-visible t)
3417       all-visible)))
3418
3419 (defun gnus-window-top-edge (&optional window)
3420   (nth 1 (window-edges window)))
3421
3422 (defun gnus-remove-some-windows ()
3423   (let ((buffers gnus-window-to-buffer)
3424         buf bufs lowest-buf lowest)
3425     (save-excursion
3426       ;; Remove windows on all known Gnus buffers.
3427       (while buffers
3428         (setq buf (cdar buffers))
3429         (if (symbolp buf)
3430             (setq buf (and (boundp buf) (symbol-value buf))))
3431         (and buf
3432              (get-buffer-window buf)
3433              (progn
3434                (setq bufs (cons buf bufs))
3435                (pop-to-buffer buf)
3436                (if (or (not lowest)
3437                        (< (gnus-window-top-edge) lowest))
3438                    (progn
3439                      (setq lowest (gnus-window-top-edge))
3440                      (setq lowest-buf buf)))))
3441         (setq buffers (cdr buffers)))
3442       ;; Remove windows on *all* summary buffers.
3443       (walk-windows
3444        (lambda (win)
3445          (let ((buf (window-buffer win)))
3446            (if (string-match    "^\\*Summary" (buffer-name buf))
3447                (progn
3448                  (setq bufs (cons buf bufs))
3449                  (pop-to-buffer buf)
3450                  (if (or (not lowest)
3451                          (< (gnus-window-top-edge) lowest))
3452                      (progn
3453                        (setq lowest-buf buf)
3454                        (setq lowest (gnus-window-top-edge)))))))))
3455       (and lowest-buf
3456            (progn
3457              (pop-to-buffer lowest-buf)
3458              (switch-to-buffer nntp-server-buffer)))
3459       (while bufs
3460         (and (not (eq (car bufs) lowest-buf))
3461              (delete-windows-on (car bufs)))
3462         (setq bufs (cdr bufs))))))
3463
3464 (defun gnus-version ()
3465   "Version numbers of this version of Gnus."
3466   (interactive)
3467   (let ((methods gnus-valid-select-methods)
3468         (mess gnus-version)
3469         meth)
3470     ;; Go through all the legal select methods and add their version
3471     ;; numbers to the total version string.  Only the backends that are
3472     ;; currently in use will have their message numbers taken into
3473     ;; consideration.
3474     (while methods
3475       (setq meth (intern (concat (caar methods) "-version")))
3476       (and (boundp meth)
3477            (stringp (symbol-value meth))
3478            (setq mess (concat mess "; " (symbol-value meth))))
3479       (setq methods (cdr methods)))
3480     (gnus-message 2 mess)))
3481
3482 (defun gnus-info-find-node ()
3483   "Find Info documentation of Gnus."
3484   (interactive)
3485   ;; Enlarge info window if needed.
3486   (let ((mode major-mode)
3487         gnus-info-buffer)
3488     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3489     (setq gnus-info-buffer (current-buffer))
3490     (gnus-configure-windows 'info)))
3491
3492 (defun gnus-days-between (date1 date2)
3493   ;; Return the number of days between date1 and date2.
3494   (- (gnus-day-number date1) (gnus-day-number date2)))
3495
3496 (defun gnus-day-number (date)
3497   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3498                      (timezone-parse-date date))))
3499     (timezone-absolute-from-gregorian
3500      (nth 1 dat) (nth 2 dat) (car dat))))
3501
3502 (defun gnus-encode-date (date)
3503   "Convert DATE to internal time."
3504   (let* ((parse (timezone-parse-date date))
3505          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3506          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3507     (encode-time (caddr time) (cadr time) (car time)
3508                  (caddr date) (cadr date) (car date) (nth 4 date))))
3509
3510 (defun gnus-time-minus (t1 t2)
3511   "Subtract two internal times."
3512   (let ((borrow (< (cadr t1) (cadr t2))))
3513     (list (- (car t1) (car t2) (if borrow 1 0))
3514           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3515
3516 (defun gnus-file-newer-than (file date)
3517   (let ((fdate (nth 5 (file-attributes file))))
3518     (or (> (car fdate) (car date))
3519         (and (= (car fdate) (car date))
3520              (> (nth 1 fdate) (nth 1 date))))))
3521
3522 (defmacro gnus-local-set-keys (&rest plist)
3523   "Set the keys in PLIST in the current keymap."
3524   `(gnus-define-keys-1 (current-local-map) ',plist))
3525
3526 (defmacro gnus-define-keys (keymap &rest plist)
3527   "Define all keys in PLIST in KEYMAP."
3528   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3529
3530 (put 'gnus-define-keys 'lisp-indent-function 1)
3531 (put 'gnus-define-keys 'lisp-indent-hook 1)
3532 (put 'gnus-define-keymap 'lisp-indent-function 1)
3533 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3534
3535 (defmacro gnus-define-keymap (keymap &rest plist)
3536   "Define all keys in PLIST in KEYMAP."
3537   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3538
3539 (defun gnus-define-keys-1 (keymap plist)
3540   (when (null keymap)
3541     (error "Can't set keys in a null keymap"))
3542   (cond ((symbolp keymap)
3543          (setq keymap (symbol-value keymap)))
3544         ((keymapp keymap))
3545         ((listp keymap)
3546          (set (car keymap) nil)
3547          (define-prefix-command (car keymap))
3548          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3549          (setq keymap (symbol-value (car keymap)))))
3550   (let (key)
3551     (while plist
3552       (when (symbolp (setq key (pop plist)))
3553         (setq key (symbol-value key)))
3554       (define-key keymap key (pop plist)))))
3555
3556 (defun gnus-group-read-only-p (&optional group)
3557   "Check whether GROUP supports editing or not.
3558 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3559 that that variable is buffer-local to the summary buffers."
3560   (let ((group (or group gnus-newsgroup-name)))
3561     (not (gnus-check-backend-function 'request-replace-article group))))
3562
3563 (defun gnus-group-total-expirable-p (group)
3564   "Check whether GROUP is total-expirable or not."
3565   (let ((params (gnus-info-params (gnus-get-info group))))
3566     (or (memq 'total-expire params)
3567         (cdr (assq 'total-expire params)) ; (total-expire . t)
3568         (and gnus-total-expirable-newsgroups ; Check var.
3569              (string-match gnus-total-expirable-newsgroups group)))))
3570
3571 (defun gnus-group-auto-expirable-p (group)
3572   "Check whether GROUP is total-expirable or not."
3573   (let ((params (gnus-info-params (gnus-get-info group))))
3574     (or (memq 'auto-expire params)
3575         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3576         (and gnus-auto-expirable-newsgroups ; Check var.
3577              (string-match gnus-auto-expirable-newsgroups group)))))
3578
3579 (defun gnus-virtual-group-p (group)
3580   "Say whether GROUP is virtual or not."
3581   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3582                         gnus-valid-select-methods)))
3583
3584 (defun gnus-news-group-p (group &optional article)
3585   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3586   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3587       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3588            (eq (gnus-request-type group article) 'news))))
3589
3590 (defsubst gnus-simplify-subject-fully (subject)
3591   "Simplify a subject string according to the user's wishes."
3592   (cond
3593    ((null gnus-summary-gather-subject-limit)
3594     (gnus-simplify-subject-re subject))
3595    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3596     (gnus-simplify-subject-fuzzy subject))
3597    ((numberp gnus-summary-gather-subject-limit)
3598     (gnus-limit-string (gnus-simplify-subject-re subject)
3599                        gnus-summary-gather-subject-limit))
3600    (t
3601     subject)))
3602
3603 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3604   "Check whether two subjects are equal.  If optional argument
3605 simple-first is t, first argument is already simplified."
3606   (cond
3607    ((null simple-first)
3608     (equal (gnus-simplify-subject-fully s1)
3609            (gnus-simplify-subject-fully s2)))
3610    (t
3611     (equal s1
3612            (gnus-simplify-subject-fully s2)))))
3613
3614 ;; Returns a list of writable groups.
3615 (defun gnus-writable-groups ()
3616   (let ((alist gnus-newsrc-alist)
3617         groups group)
3618     (while (setq group (car (pop alist)))
3619       (unless (gnus-group-read-only-p group)
3620         (push group groups)))
3621     (nreverse groups)))
3622
3623 (defun gnus-completing-read (default prompt &rest args)
3624   ;; Like `completing-read', except that DEFAULT is the default argument.
3625   (let* ((prompt (concat prompt " (default " default ") "))
3626          (answer (apply 'completing-read prompt args)))
3627     (if (or (null answer) (zerop (length answer)))
3628         default
3629       answer)))
3630
3631 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3632 ;; the echo area.
3633 (defun gnus-y-or-n-p (prompt)
3634   (prog1
3635       (y-or-n-p prompt)
3636     (message "")))
3637
3638 (defun gnus-yes-or-no-p (prompt)
3639   (prog1
3640       (yes-or-no-p prompt)
3641     (message "")))
3642
3643 ;; Check whether to use long file names.
3644 (defun gnus-use-long-file-name (symbol)
3645   ;; The variable has to be set...
3646   (and gnus-use-long-file-name
3647        ;; If it isn't a list, then we return t.
3648        (or (not (listp gnus-use-long-file-name))
3649            ;; If it is a list, and the list contains `symbol', we
3650            ;; return nil.
3651            (not (memq symbol gnus-use-long-file-name)))))
3652
3653 ;; I suspect there's a better way, but I haven't taken the time to do
3654 ;; it yet. -erik selberg@cs.washington.edu
3655 (defun gnus-dd-mmm (messy-date)
3656   "Return a string like DD-MMM from a big messy string"
3657   (let ((datevec (timezone-parse-date messy-date)))
3658     (format "%2s-%s"
3659             (condition-case ()
3660                 ;; Make sure leading zeroes are stripped.
3661                 (number-to-string (string-to-number (aref datevec 2)))
3662               (error "??"))
3663             (capitalize
3664              (or (car
3665                   (nth (1- (string-to-number (aref datevec 1)))
3666                        timezone-months-assoc))
3667                  "???")))))
3668
3669 ;; Make a hash table (default and minimum size is 255).
3670 ;; Optional argument HASHSIZE specifies the table size.
3671 (defun gnus-make-hashtable (&optional hashsize)
3672   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3673
3674 ;; Make a number that is suitable for hashing; bigger than MIN and one
3675 ;; less than 2^x.
3676 (defun gnus-create-hash-size (min)
3677   (let ((i 1))
3678     (while (< i min)
3679       (setq i (* 2 i)))
3680     (1- i)))
3681
3682 ;; Show message if message has a lower level than `gnus-verbose'.
3683 ;; Guideline for numbers:
3684 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3685 ;; for things that take a long time, 7 - not very important messages
3686 ;; on stuff, 9 - messages inside loops.
3687 (defun gnus-message (level &rest args)
3688   (if (<= level gnus-verbose)
3689       (apply 'message args)
3690     ;; We have to do this format thingy here even if the result isn't
3691     ;; shown - the return value has to be the same as the return value
3692     ;; from `message'.
3693     (apply 'format args)))
3694
3695 (defun gnus-error (level &rest args)
3696   "Beep an error if `gnus-verbose' is on LEVEL or less."
3697   (when (<= (floor level) gnus-verbose)
3698     (apply 'message args)
3699     (ding)
3700     (let (duration)
3701       (when (and (floatp level)
3702                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3703         (sit-for duration))))
3704   nil)
3705
3706 ;; Generate a unique new group name.
3707 (defun gnus-generate-new-group-name (leaf)
3708   (let ((name leaf)
3709         (num 0))
3710     (while (gnus-gethash name gnus-newsrc-hashtb)
3711       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3712     name))
3713
3714 (defsubst gnus-hide-text (b e props)
3715   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3716   (add-text-properties b e props)
3717   (when (memq 'intangible props)
3718     (put-text-property (max (1- b) (point-min))
3719                        b 'intangible (cddr (memq 'intangible props)))))
3720
3721 (defsubst gnus-unhide-text (b e)
3722   "Remove hidden text properties from region between B and E."
3723   (remove-text-properties b e gnus-hidden-properties)
3724   (when (memq 'intangible gnus-hidden-properties)
3725     (put-text-property (max (1- b) (point-min))
3726                        b 'intangible nil)))
3727
3728 (defun gnus-hide-text-type (b e type)
3729   "Hide text of TYPE between B and E."
3730   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3731
3732 ;; Find out whether the gnus-visual TYPE is wanted.
3733 (defun gnus-visual-p (&optional type class)
3734   (and gnus-visual                      ; Has to be non-nil, at least.
3735        (if (not type)                   ; We don't care about type.
3736            gnus-visual
3737          (if (listp gnus-visual)        ; It's a list, so we check it.
3738              (or (memq type gnus-visual)
3739                  (memq class gnus-visual))
3740            t))))
3741
3742 (defun gnus-parent-id (references)
3743   "Return the last Message-ID in REFERENCES."
3744   (when (and references
3745              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3746     (substring references (match-beginning 1) (match-end 1))))
3747
3748 (defun gnus-split-references (references)
3749   "Return a list of Message-IDs in REFERENCES."
3750   (let ((beg 0)
3751         ids)
3752     (while (string-match "<[^>]+>" references beg)
3753       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3754             ids))
3755     (nreverse ids)))
3756
3757 (defun gnus-buffer-live-p (buffer)
3758   "Say whether BUFFER is alive or not."
3759   (and buffer
3760        (get-buffer buffer)
3761        (buffer-name (get-buffer buffer))))
3762
3763 (defun gnus-ephemeral-group-p (group)
3764   "Say whether GROUP is ephemeral or not."
3765   (gnus-group-get-parameter group 'quit-config))
3766
3767 (defun gnus-group-quit-config (group)
3768   "Return the quit-config of GROUP."
3769   (gnus-group-get-parameter group 'quit-config))
3770
3771 (defun gnus-simplify-mode-line ()
3772   "Make mode lines a bit simpler."
3773   (setq mode-line-modified "-- ")
3774   (when (listp mode-line-format)
3775     (make-local-variable 'mode-line-format)
3776     (setq mode-line-format (copy-sequence mode-line-format))
3777     (when (equal (nth 3 mode-line-format) "   ")
3778       (setcar (nthcdr 3 mode-line-format) " "))))
3779
3780 ;;; List and range functions
3781
3782 (defun gnus-last-element (list)
3783   "Return last element of LIST."
3784   (while (cdr list)
3785     (setq list (cdr list)))
3786   (car list))
3787
3788 (defun gnus-copy-sequence (list)
3789   "Do a complete, total copy of a list."
3790   (if (and (consp list) (not (consp (cdr list))))
3791       (cons (car list) (cdr list))
3792     (mapcar (lambda (elem) (if (consp elem)
3793                                (if (consp (cdr elem))
3794                                    (gnus-copy-sequence elem)
3795                                  (cons (car elem) (cdr elem)))
3796                              elem))
3797             list)))
3798
3799 (defun gnus-set-difference (list1 list2)
3800   "Return a list of elements of LIST1 that do not appear in LIST2."
3801   (let ((list1 (copy-sequence list1)))
3802     (while list2
3803       (setq list1 (delq (car list2) list1))
3804       (setq list2 (cdr list2)))
3805     list1))
3806
3807 (defun gnus-sorted-complement (list1 list2)
3808   "Return a list of elements of LIST1 that do not appear in LIST2.
3809 Both lists have to be sorted over <."
3810   (let (out)
3811     (if (or (null list1) (null list2))
3812         (or list1 list2)
3813       (while (and list1 list2)
3814         (cond ((= (car list1) (car list2))
3815                (setq list1 (cdr list1)
3816                      list2 (cdr list2)))
3817               ((< (car list1) (car list2))
3818                (setq out (cons (car list1) out))
3819                (setq list1 (cdr list1)))
3820               (t
3821                (setq out (cons (car list2) out))
3822                (setq list2 (cdr list2)))))
3823       (nconc (nreverse out) (or list1 list2)))))
3824
3825 (defun gnus-intersection (list1 list2)
3826   (let ((result nil))
3827     (while list2
3828       (if (memq (car list2) list1)
3829           (setq result (cons (car list2) result)))
3830       (setq list2 (cdr list2)))
3831     result))
3832
3833 (defun gnus-sorted-intersection (list1 list2)
3834   ;; LIST1 and LIST2 have to be sorted over <.
3835   (let (out)
3836     (while (and list1 list2)
3837       (cond ((= (car list1) (car list2))
3838              (setq out (cons (car list1) out)
3839                    list1 (cdr list1)
3840                    list2 (cdr list2)))
3841             ((< (car list1) (car list2))
3842              (setq list1 (cdr list1)))
3843             (t
3844              (setq list2 (cdr list2)))))
3845     (nreverse out)))
3846
3847 (defun gnus-set-sorted-intersection (list1 list2)
3848   ;; LIST1 and LIST2 have to be sorted over <.
3849   ;; This function modifies LIST1.
3850   (let* ((top (cons nil list1))
3851          (prev top))
3852     (while (and list1 list2)
3853       (cond ((= (car list1) (car list2))
3854              (setq prev list1
3855                    list1 (cdr list1)
3856                    list2 (cdr list2)))
3857             ((< (car list1) (car list2))
3858              (setcdr prev (cdr list1))
3859              (setq list1 (cdr list1)))
3860             (t
3861              (setq list2 (cdr list2)))))
3862     (setcdr prev nil)
3863     (cdr top)))
3864
3865 (defun gnus-compress-sequence (numbers &optional always-list)
3866   "Convert list of numbers to a list of ranges or a single range.
3867 If ALWAYS-LIST is non-nil, this function will always release a list of
3868 ranges."
3869   (let* ((first (car numbers))
3870          (last (car numbers))
3871          result)
3872     (if (null numbers)
3873         nil
3874       (if (not (listp (cdr numbers)))
3875           numbers
3876         (while numbers
3877           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3878                 ((= (1+ last) (car numbers)) ;Still in sequence
3879                  (setq last (car numbers)))
3880                 (t                      ;End of one sequence
3881                  (setq result
3882                        (cons (if (= first last) first
3883                                (cons first last)) result))
3884                  (setq first (car numbers))
3885                  (setq last  (car numbers))))
3886           (setq numbers (cdr numbers)))
3887         (if (and (not always-list) (null result))
3888             (if (= first last) (list first) (cons first last))
3889           (nreverse (cons (if (= first last) first (cons first last))
3890                           result)))))))
3891
3892 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3893 (defun gnus-uncompress-range (ranges)
3894   "Expand a list of ranges into a list of numbers.
3895 RANGES is either a single range on the form `(num . num)' or a list of
3896 these ranges."
3897   (let (first last result)
3898     (cond
3899      ((null ranges)
3900       nil)
3901      ((not (listp (cdr ranges)))
3902       (setq first (car ranges))
3903       (setq last (cdr ranges))
3904       (while (<= first last)
3905         (setq result (cons first result))
3906         (setq first (1+ first)))
3907       (nreverse result))
3908      (t
3909       (while ranges
3910         (if (atom (car ranges))
3911             (if (numberp (car ranges))
3912                 (setq result (cons (car ranges) result)))
3913           (setq first (caar ranges))
3914           (setq last  (cdar ranges))
3915           (while (<= first last)
3916             (setq result (cons first result))
3917             (setq first (1+ first))))
3918         (setq ranges (cdr ranges)))
3919       (nreverse result)))))
3920
3921 (defun gnus-add-to-range (ranges list)
3922   "Return a list of ranges that has all articles from both RANGES and LIST.
3923 Note: LIST has to be sorted over `<'."
3924   (if (not ranges)
3925       (gnus-compress-sequence list t)
3926     (setq list (copy-sequence list))
3927     (or (listp (cdr ranges))
3928         (setq ranges (list ranges)))
3929     (let ((out ranges)
3930           ilist lowest highest temp)
3931       (while (and ranges list)
3932         (setq ilist list)
3933         (setq lowest (or (and (atom (car ranges)) (car ranges))
3934                          (caar ranges)))
3935         (while (and list (cdr list) (< (cadr list) lowest))
3936           (setq list (cdr list)))
3937         (if (< (car ilist) lowest)
3938             (progn
3939               (setq temp list)
3940               (setq list (cdr list))
3941               (setcdr temp nil)
3942               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3943         (setq highest (or (and (atom (car ranges)) (car ranges))
3944                           (cdar ranges)))
3945         (while (and list (<= (car list) highest))
3946           (setq list (cdr list)))
3947         (setq ranges (cdr ranges)))
3948       (if list
3949           (setq out (nconc (gnus-compress-sequence list t) out)))
3950       (setq out (sort out (lambda (r1 r2)
3951                             (< (or (and (atom r1) r1) (car r1))
3952                                (or (and (atom r2) r2) (car r2))))))
3953       (setq ranges out)
3954       (while ranges
3955         (if (atom (car ranges))
3956             (if (cdr ranges)
3957                 (if (atom (cadr ranges))
3958                     (if (= (1+ (car ranges)) (cadr ranges))
3959                         (progn
3960                           (setcar ranges (cons (car ranges)
3961                                                (cadr ranges)))
3962                           (setcdr ranges (cddr ranges))))
3963                   (if (= (1+ (car ranges)) (caadr ranges))
3964                       (progn
3965                         (setcar (cadr ranges) (car ranges))
3966                         (setcar ranges (cadr ranges))
3967                         (setcdr ranges (cddr ranges))))))
3968           (if (cdr ranges)
3969               (if (atom (cadr ranges))
3970                   (if (= (1+ (cdar ranges)) (cadr ranges))
3971                       (progn
3972                         (setcdr (car ranges) (cadr ranges))
3973                         (setcdr ranges (cddr ranges))))
3974                 (if (= (1+ (cdar ranges)) (caadr ranges))
3975                     (progn
3976                       (setcdr (car ranges) (cdadr ranges))
3977                       (setcdr ranges (cddr ranges)))))))
3978         (setq ranges (cdr ranges)))
3979       out)))
3980
3981 (defun gnus-remove-from-range (ranges list)
3982   "Return a list of ranges that has all articles from LIST removed from RANGES.
3983 Note: LIST has to be sorted over `<'."
3984   ;; !!! This function shouldn't look like this, but I've got a headache.
3985   (gnus-compress-sequence
3986    (gnus-sorted-complement
3987     (gnus-uncompress-range ranges) list)))
3988
3989 (defun gnus-member-of-range (number ranges)
3990   (if (not (listp (cdr ranges)))
3991       (and (>= number (car ranges))
3992            (<= number (cdr ranges)))
3993     (let ((not-stop t))
3994       (while (and ranges
3995                   (if (numberp (car ranges))
3996                       (>= number (car ranges))
3997                     (>= number (caar ranges)))
3998                   not-stop)
3999         (if (if (numberp (car ranges))
4000                 (= number (car ranges))
4001               (and (>= number (caar ranges))
4002                    (<= number (cdar ranges))))
4003             (setq not-stop nil))
4004         (setq ranges (cdr ranges)))
4005       (not not-stop))))
4006
4007 (defun gnus-range-length (range)
4008   "Return the length RANGE would have if uncompressed."
4009   (length (gnus-uncompress-range range)))
4010
4011 (defun gnus-sublist-p (list sublist)
4012   "Test whether all elements in SUBLIST are members of LIST."
4013   (let ((sublistp t))
4014     (while sublist
4015       (unless (memq (pop sublist) list)
4016         (setq sublistp nil
4017               sublist nil)))
4018     sublistp))
4019
4020 \f
4021 ;;;
4022 ;;; Gnus group mode
4023 ;;;
4024
4025 (defvar gnus-group-mode-map nil)
4026 (put 'gnus-group-mode 'mode-class 'special)
4027
4028 (unless gnus-group-mode-map
4029   (setq gnus-group-mode-map (make-keymap))
4030   (suppress-keymap gnus-group-mode-map)
4031
4032   (gnus-define-keys gnus-group-mode-map
4033     " " gnus-group-read-group
4034     "=" gnus-group-select-group
4035     "\r" gnus-group-select-group
4036     "\M-\r" gnus-group-quick-select-group
4037     "j" gnus-group-jump-to-group
4038     "n" gnus-group-next-unread-group
4039     "p" gnus-group-prev-unread-group
4040     "\177" gnus-group-prev-unread-group
4041     [delete] gnus-group-prev-unread-group
4042     "N" gnus-group-next-group
4043     "P" gnus-group-prev-group
4044     "\M-n" gnus-group-next-unread-group-same-level
4045     "\M-p" gnus-group-prev-unread-group-same-level
4046     "," gnus-group-best-unread-group
4047     "." gnus-group-first-unread-group
4048     "u" gnus-group-unsubscribe-current-group
4049     "U" gnus-group-unsubscribe-group
4050     "c" gnus-group-catchup-current
4051     "C" gnus-group-catchup-current-all
4052     "l" gnus-group-list-groups
4053     "L" gnus-group-list-all-groups
4054     "m" gnus-group-mail
4055     "g" gnus-group-get-new-news
4056     "\M-g" gnus-group-get-new-news-this-group
4057     "R" gnus-group-restart
4058     "r" gnus-group-read-init-file
4059     "B" gnus-group-browse-foreign-server
4060     "b" gnus-group-check-bogus-groups
4061     "F" gnus-find-new-newsgroups
4062     "\C-c\C-d" gnus-group-describe-group
4063     "\M-d" gnus-group-describe-all-groups
4064     "\C-c\C-a" gnus-group-apropos
4065     "\C-c\M-\C-a" gnus-group-description-apropos
4066     "a" gnus-group-post-news
4067     "\ek" gnus-group-edit-local-kill
4068     "\eK" gnus-group-edit-global-kill
4069     "\C-k" gnus-group-kill-group
4070     "\C-y" gnus-group-yank-group
4071     "\C-w" gnus-group-kill-region
4072     "\C-x\C-t" gnus-group-transpose-groups
4073     "\C-c\C-l" gnus-group-list-killed
4074     "\C-c\C-x" gnus-group-expire-articles
4075     "\C-c\M-\C-x" gnus-group-expire-all-groups
4076     "V" gnus-version
4077     "s" gnus-group-save-newsrc
4078     "z" gnus-group-suspend
4079     "Z" gnus-group-clear-dribble
4080     "q" gnus-group-exit
4081     "Q" gnus-group-quit
4082     "?" gnus-group-describe-briefly
4083     "\C-c\C-i" gnus-info-find-node
4084     "\M-e" gnus-group-edit-group-method
4085     "^" gnus-group-enter-server-mode
4086     gnus-mouse-2 gnus-mouse-pick-group
4087     "<" beginning-of-buffer
4088     ">" end-of-buffer
4089     "\C-c\C-b" gnus-bug
4090     "\C-c\C-s" gnus-group-sort-groups
4091     "t" gnus-topic-mode
4092     "\C-c\M-g" gnus-activate-all-groups
4093     "\M-&" gnus-group-universal-argument
4094     "#" gnus-group-mark-group
4095     "\M-#" gnus-group-unmark-group)
4096
4097   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4098     "m" gnus-group-mark-group
4099     "u" gnus-group-unmark-group
4100     "w" gnus-group-mark-region
4101     "m" gnus-group-mark-buffer
4102     "r" gnus-group-mark-regexp
4103     "U" gnus-group-unmark-all-groups)
4104
4105   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4106     "d" gnus-group-make-directory-group
4107     "h" gnus-group-make-help-group
4108     "a" gnus-group-make-archive-group
4109     "k" gnus-group-make-kiboze-group
4110     "m" gnus-group-make-group
4111     "E" gnus-group-edit-group
4112     "e" gnus-group-edit-group-method
4113     "p" gnus-group-edit-group-parameters
4114     "v" gnus-group-add-to-virtual
4115     "V" gnus-group-make-empty-virtual
4116     "D" gnus-group-enter-directory
4117     "f" gnus-group-make-doc-group
4118     "r" gnus-group-rename-group
4119     "\177" gnus-group-delete-group
4120     [delete] gnus-group-delete-group)
4121
4122    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4123      "b" gnus-group-brew-soup
4124      "w" gnus-soup-save-areas
4125      "s" gnus-soup-send-replies
4126      "p" gnus-soup-pack-packet
4127      "r" nnsoup-pack-replies)
4128
4129    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4130      "s" gnus-group-sort-groups
4131      "a" gnus-group-sort-groups-by-alphabet
4132      "u" gnus-group-sort-groups-by-unread
4133      "l" gnus-group-sort-groups-by-level
4134      "v" gnus-group-sort-groups-by-score
4135      "r" gnus-group-sort-groups-by-rank
4136      "m" gnus-group-sort-groups-by-method)
4137
4138    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4139      "k" gnus-group-list-killed
4140      "z" gnus-group-list-zombies
4141      "s" gnus-group-list-groups
4142      "u" gnus-group-list-all-groups
4143      "A" gnus-group-list-active
4144      "a" gnus-group-apropos
4145      "d" gnus-group-description-apropos
4146      "m" gnus-group-list-matching
4147      "M" gnus-group-list-all-matching
4148      "l" gnus-group-list-level)
4149
4150    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4151      "f" gnus-score-flush-cache)
4152
4153    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4154      "f" gnus-group-fetch-faq)
4155
4156    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4157      "l" gnus-group-set-current-level
4158      "t" gnus-group-unsubscribe-current-group
4159      "s" gnus-group-unsubscribe-group
4160      "k" gnus-group-kill-group
4161      "y" gnus-group-yank-group
4162      "w" gnus-group-kill-region
4163      "\C-k" gnus-group-kill-level
4164      "z" gnus-group-kill-all-zombies))
4165
4166 (defun gnus-group-mode ()
4167   "Major mode for reading news.
4168
4169 All normal editing commands are switched off.
4170 \\<gnus-group-mode-map>
4171 The group buffer lists (some of) the groups available.  For instance,
4172 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4173 lists all zombie groups.
4174
4175 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4176 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4177
4178 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4179
4180 The following commands are available:
4181
4182 \\{gnus-group-mode-map}"
4183   (interactive)
4184   (when (and menu-bar-mode
4185              (gnus-visual-p 'group-menu 'menu))
4186     (gnus-group-make-menu-bar))
4187   (kill-all-local-variables)
4188   (gnus-simplify-mode-line)
4189   (setq major-mode 'gnus-group-mode)
4190   (setq mode-name "Group")
4191   (gnus-group-set-mode-line)
4192   (setq mode-line-process nil)
4193   (use-local-map gnus-group-mode-map)
4194   (buffer-disable-undo (current-buffer))
4195   (setq truncate-lines t)
4196   (setq buffer-read-only t)
4197   (run-hooks 'gnus-group-mode-hook))
4198
4199 (defun gnus-mouse-pick-group (e)
4200   "Enter the group under the mouse pointer."
4201   (interactive "e")
4202   (mouse-set-point e)
4203   (gnus-group-read-group nil))
4204
4205 ;; Look at LEVEL and find out what the level is really supposed to be.
4206 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4207 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4208 (defun gnus-group-default-level (&optional level number-or-nil)
4209   (cond
4210    (gnus-group-use-permanent-levels
4211 ;    (setq gnus-group-default-list-level
4212 ;         (or level gnus-group-default-list-level))
4213     (or level gnus-group-default-list-level gnus-level-subscribed))
4214    (number-or-nil
4215     level)
4216    (t
4217     (or level gnus-group-default-list-level gnus-level-subscribed))))
4218
4219 ;;;###autoload
4220 (defun gnus-slave-no-server (&optional arg)
4221   "Read network news as a slave, without connecting to local server"
4222   (interactive "P")
4223   (gnus-no-server arg t))
4224
4225 ;;;###autoload
4226 (defun gnus-no-server (&optional arg slave)
4227   "Read network news.
4228 If ARG is a positive number, Gnus will use that as the
4229 startup level.  If ARG is nil, Gnus will be started at level 2.
4230 If ARG is non-nil and not a positive number, Gnus will
4231 prompt the user for the name of an NNTP server to use.
4232 As opposed to `gnus', this command will not connect to the local server."
4233   (interactive "P")
4234   (let ((gnus-group-use-permanent-levels t))
4235     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4236   (make-local-variable 'gnus-group-use-permanent-levels)
4237   (setq gnus-group-use-permanent-levels t))
4238
4239 ;;;###autoload
4240 (defun gnus-slave (&optional arg)
4241   "Read news as a slave."
4242   (interactive "P")
4243   (gnus arg nil 'slave))
4244
4245 ;;;###autoload
4246 (defun gnus-other-frame (&optional arg)
4247   "Pop up a frame to read news."
4248   (interactive "P")
4249   (if (get-buffer gnus-group-buffer)
4250       (let ((pop-up-frames t))
4251         (gnus arg))
4252     (select-frame (make-frame))
4253     (gnus arg)))
4254
4255 ;;;###autoload
4256 (defun gnus (&optional arg dont-connect slave)
4257   "Read network news.
4258 If ARG is non-nil and a positive number, Gnus will use that as the
4259 startup level.  If ARG is non-nil and not a positive number, Gnus will
4260 prompt the user for the name of an NNTP server to use."
4261   (interactive "P")
4262
4263   (if (get-buffer gnus-group-buffer)
4264       (progn
4265         (switch-to-buffer gnus-group-buffer)
4266         (gnus-group-get-new-news))
4267
4268     (gnus-clear-system)
4269     (nnheader-init-server-buffer)
4270     (gnus-read-init-file)
4271     (setq gnus-slave slave)
4272
4273     (gnus-group-setup-buffer)
4274     (let ((buffer-read-only nil))
4275       (erase-buffer)
4276       (if (not gnus-inhibit-startup-message)
4277           (progn
4278             (gnus-group-startup-message)
4279             (sit-for 0))))
4280
4281     (let ((level (and (numberp arg) (> arg 0) arg))
4282           did-connect)
4283       (unwind-protect
4284           (progn
4285             (or dont-connect
4286                 (setq did-connect
4287                       (gnus-start-news-server (and arg (not level))))))
4288         (if (and (not dont-connect)
4289                  (not did-connect))
4290             (gnus-group-quit)
4291           (run-hooks 'gnus-startup-hook)
4292           ;; NNTP server is successfully open.
4293
4294           ;; Find the current startup file name.
4295           (setq gnus-current-startup-file
4296                 (gnus-make-newsrc-file gnus-startup-file))
4297
4298           ;; Read the dribble file.
4299           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4300
4301           ;; Allow using GroupLens predictions.
4302           (when gnus-use-grouplens
4303             (bbb-login)
4304             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4305
4306           (gnus-summary-make-display-table)
4307           ;; Do the actual startup.
4308           (gnus-setup-news nil level dont-connect)
4309           ;; Generate the group buffer.
4310           (gnus-group-list-groups level)
4311           (gnus-group-first-unread-group)
4312           (gnus-configure-windows 'group)
4313           (gnus-group-set-mode-line))))))
4314
4315 (defun gnus-unload ()
4316   "Unload all Gnus features."
4317   (interactive)
4318   (or (boundp 'load-history)
4319       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4320   (let ((history load-history)
4321         feature)
4322     (while history
4323       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4324            (setq feature (cdr (assq 'provide (car history))))
4325            (unload-feature feature 'force))
4326       (setq history (cdr history)))))
4327
4328 (defun gnus-compile ()
4329   "Byte-compile the user-defined format specs."
4330   (interactive)
4331   (let ((entries gnus-format-specs)
4332         entry gnus-tmp-func)
4333     (save-excursion
4334       (gnus-message 7 "Compiling format specs...")
4335
4336       (while entries
4337         (setq entry (pop entries))
4338         (if (eq (car entry) 'version)
4339             (setq gnus-format-specs (delq entry gnus-format-specs))
4340           (when (and (listp (caddr entry))
4341                      (not (eq 'byte-code (caaddr entry))))
4342             (fset 'gnus-tmp-func
4343                   `(lambda () ,(caddr entry)))
4344             (byte-compile 'gnus-tmp-func)
4345             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4346
4347       (push (cons 'version emacs-version) gnus-format-specs)
4348
4349       (gnus-message 7 "Compiling user specs...done"))))
4350
4351 (defun gnus-indent-rigidly (start end arg)
4352   "Indent rigidly using only spaces and no tabs."
4353   (save-excursion
4354     (save-restriction
4355       (narrow-to-region start end)
4356       (indent-rigidly start end arg)
4357       (goto-char (point-min))
4358       (while (search-forward "\t" nil t)
4359         (replace-match "        " t t)))))
4360
4361 (defun gnus-group-startup-message (&optional x y)
4362   "Insert startup message in current buffer."
4363   ;; Insert the message.
4364   (erase-buffer)
4365   (insert
4366    (format "              %s
4367           _    ___ _             _
4368           _ ___ __ ___  __    _ ___
4369           __   _     ___    __  ___
4370               _           ___     _
4371              _  _ __             _
4372              ___   __            _
4373                    __           _
4374                     _      _   _
4375                    _      _    _
4376                       _  _    _
4377                   __  ___
4378                  _   _ _     _
4379                 _   _
4380               _    _
4381              _    _
4382             _
4383           __
4384
4385 "
4386            ""))
4387   ;; And then hack it.
4388   (gnus-indent-rigidly (point-min) (point-max)
4389                        (/ (max (- (window-width) (or x 46)) 0) 2))
4390   (goto-char (point-min))
4391   (forward-line 1)
4392   (let* ((pheight (count-lines (point-min) (point-max)))
4393          (wheight (window-height))
4394          (rest (- wheight pheight)))
4395     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4396   ;; Fontify some.
4397   (goto-char (point-min))
4398   (and (search-forward "Praxis" nil t)
4399        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4400   (goto-char (point-min))
4401   (let* ((mode-string (gnus-group-set-mode-line)))
4402     (setq mode-line-buffer-identification
4403           (list (concat gnus-version (substring (car mode-string) 4))))
4404     (set-buffer-modified-p t)))
4405
4406 (defun gnus-group-setup-buffer ()
4407   (or (get-buffer gnus-group-buffer)
4408       (progn
4409         (switch-to-buffer gnus-group-buffer)
4410         (gnus-add-current-to-buffer-list)
4411         (gnus-group-mode)
4412         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4413
4414 (defun gnus-group-list-groups (&optional level unread lowest)
4415   "List newsgroups with level LEVEL or lower that have unread articles.
4416 Default is all subscribed groups.
4417 If argument UNREAD is non-nil, groups with no unread articles are also
4418 listed."
4419   (interactive (list (if current-prefix-arg
4420                          (prefix-numeric-value current-prefix-arg)
4421                        (or
4422                         (gnus-group-default-level nil t)
4423                         gnus-group-default-list-level
4424                         gnus-level-subscribed))))
4425   (or level
4426       (setq level (car gnus-group-list-mode)
4427             unread (cdr gnus-group-list-mode)))
4428   (setq level (gnus-group-default-level level))
4429   (gnus-group-setup-buffer)             ;May call from out of group buffer
4430   (gnus-update-format-specifications)
4431   (let ((case-fold-search nil)
4432         (props (text-properties-at (gnus-point-at-bol)))
4433         (group (gnus-group-group-name)))
4434     (set-buffer gnus-group-buffer)
4435     (funcall gnus-group-prepare-function level unread lowest)
4436     (if (zerop (buffer-size))
4437         (gnus-message 5 gnus-no-groups-message)
4438       (goto-char (point-max))
4439       (when (or (not gnus-group-goto-next-group-function)
4440                 (not (funcall gnus-group-goto-next-group-function 
4441                               group props)))
4442         (if (not group)
4443             ;; Go to the first group with unread articles.
4444             (gnus-group-search-forward t)
4445           ;; Find the right group to put point on.  If the current group
4446           ;; has disappeared in the new listing, try to find the next
4447           ;; one.        If no next one can be found, just leave point at the
4448           ;; first newsgroup in the buffer.
4449           (if (not (gnus-goto-char
4450                     (text-property-any
4451                      (point-min) (point-max)
4452                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4453               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4454                 (while (and newsrc
4455                             (not (gnus-goto-char
4456                                   (text-property-any
4457                                    (point-min) (point-max) 'gnus-group
4458                                    (gnus-intern-safe
4459                                     (caar newsrc) gnus-active-hashtb)))))
4460                   (setq newsrc (cdr newsrc)))
4461                 (or newsrc (progn (goto-char (point-max))
4462                                   (forward-line -1)))))))
4463       ;; Adjust cursor point.
4464       (gnus-group-position-point))))
4465
4466 (defun gnus-group-list-level (level &optional all)
4467   "List groups on LEVEL.
4468 If ALL (the prefix), also list groups that have no unread articles."
4469   (interactive "nList groups on level: \nP")
4470   (gnus-group-list-groups level all level))
4471
4472 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4473   "List all newsgroups with unread articles of level LEVEL or lower.
4474 If ALL is non-nil, list groups that have no unread articles.
4475 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4476 If REGEXP, only list groups matching REGEXP."
4477   (set-buffer gnus-group-buffer)
4478   (let ((buffer-read-only nil)
4479         (newsrc (cdr gnus-newsrc-alist))
4480         (lowest (or lowest 1))
4481         info clevel unread group params)
4482     (erase-buffer)
4483     (if (< lowest gnus-level-zombie)
4484         ;; List living groups.
4485         (while newsrc
4486           (setq info (car newsrc)
4487                 group (gnus-info-group info)
4488                 params (gnus-info-params info)
4489                 newsrc (cdr newsrc)
4490                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4491           (and unread                   ; This group might be bogus
4492                (or (not regexp)
4493                    (string-match regexp group))
4494                (<= (setq clevel (gnus-info-level info)) level)
4495                (>= clevel lowest)
4496                (or all                  ; We list all groups?
4497                    (if (eq unread t)    ; Unactivated?
4498                        gnus-group-list-inactive-groups ; We list unactivated 
4499                      (> unread 0))      ; We list groups with unread articles
4500                    (and gnus-list-groups-with-ticked-articles
4501                         (cdr (assq 'tick (gnus-info-marks info))))
4502                                         ; And groups with tickeds
4503                    ;; Check for permanent visibility.
4504                    (and gnus-permanently-visible-groups
4505                         (string-match gnus-permanently-visible-groups
4506                                       group))
4507                    (memq 'visible params)
4508                    (cdr (assq 'visible params)))
4509                (gnus-group-insert-group-line
4510                 group (gnus-info-level info)
4511                 (gnus-info-marks info) unread (gnus-info-method info)))))
4512
4513     ;; List dead groups.
4514     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4515          (gnus-group-prepare-flat-list-dead
4516           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4517           gnus-level-zombie ?Z
4518           regexp))
4519     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4520          (gnus-group-prepare-flat-list-dead
4521           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4522           gnus-level-killed ?K regexp))
4523
4524     (gnus-group-set-mode-line)
4525     (setq gnus-group-list-mode (cons level all))
4526     (run-hooks 'gnus-group-prepare-hook)))
4527
4528 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4529   ;; List zombies and killed lists somewhat faster, which was
4530   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4531   ;; this by ignoring the group format specification altogether.
4532   (let (group)
4533     (if regexp
4534         ;; This loop is used when listing groups that match some
4535         ;; regexp.
4536         (while groups
4537           (setq group (pop groups))
4538           (when (string-match regexp group)
4539             (add-text-properties
4540              (point) (prog1 (1+ (point))
4541                        (insert " " mark "     *: " group "\n"))
4542              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4543                    'gnus-unread t
4544                    'gnus-level level))))
4545       ;; This loop is used when listing all groups.
4546       (while groups
4547         (add-text-properties
4548          (point) (prog1 (1+ (point))
4549                    (insert " " mark "     *: "
4550                            (setq group (pop groups)) "\n"))
4551          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4552                'gnus-unread t
4553                'gnus-level level))))))
4554
4555 (defmacro gnus-group-real-name (group)
4556   "Find the real name of a foreign newsgroup."
4557   `(let ((gname ,group))
4558      (if (string-match ":[^:]+$" gname)
4559          (substring gname (1+ (match-beginning 0)))
4560        gname)))
4561
4562 (defsubst gnus-server-add-address (method)
4563   (let ((method-name (symbol-name (car method))))
4564     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4565              (not (assq (intern (concat method-name "-address")) method)))
4566         (append method (list (list (intern (concat method-name "-address"))
4567                                    (nth 1 method))))
4568       method)))
4569
4570 (defsubst gnus-server-get-method (group method)
4571   ;; Input either a server name, and extended server name, or a
4572   ;; select method, and return a select method.
4573   (cond ((stringp method)
4574          (gnus-server-to-method method))
4575         ((equal method gnus-select-method)
4576          gnus-select-method)
4577         ((and (stringp (car method)) group)
4578          (gnus-server-extend-method group method))
4579         ((and method (not group)
4580               (equal (cadr method) ""))
4581          method)
4582         (t
4583          (gnus-server-add-address method))))
4584
4585 (defun gnus-server-to-method (server)
4586   "Map virtual server names to select methods."
4587   (or 
4588    ;; Perhaps this is the native server?
4589    (and (equal server "native") gnus-select-method)
4590    ;; It should be in the server alist.
4591    (cdr (assoc server gnus-server-alist))
4592    ;; If not, we look through all the opened server
4593    ;; to see whether we can find it there.
4594    (let ((opened gnus-opened-servers))
4595      (while (and opened
4596                  (not (equal server (format "%s:%s" (caaar opened)
4597                                             (cadaar opened)))))
4598        (pop opened))
4599      (caar opened))))
4600
4601 (defmacro gnus-method-equal (ss1 ss2)
4602   "Say whether two servers are equal."
4603   `(let ((s1 ,ss1)
4604          (s2 ,ss2))
4605      (or (equal s1 s2)
4606          (and (= (length s1) (length s2))
4607               (progn
4608                 (while (and s1 (member (car s1) s2))
4609                   (setq s1 (cdr s1)))
4610                 (null s1))))))
4611
4612 (defun gnus-server-equal (m1 m2)
4613   "Say whether two methods are equal."
4614   (let ((m1 (cond ((null m1) gnus-select-method)
4615                   ((stringp m1) (gnus-server-to-method m1))
4616                   (t m1)))
4617         (m2 (cond ((null m2) gnus-select-method)
4618                   ((stringp m2) (gnus-server-to-method m2))
4619                   (t m2))))
4620     (gnus-method-equal m1 m2)))
4621
4622 (defun gnus-servers-using-backend (backend)
4623   "Return a list of known servers using BACKEND."
4624   (let ((opened gnus-opened-servers)
4625         out)
4626     (while opened
4627       (when (eq backend (caaar opened))
4628         (push (caar opened) out))
4629       (pop opened))
4630     out))
4631
4632 (defun gnus-group-prefixed-name (group method)
4633   "Return the whole name from GROUP and METHOD."
4634   (and (stringp method) (setq method (gnus-server-to-method method)))
4635   (concat (format "%s" (car method))
4636           (if (and
4637                (or (assoc (format "%s" (car method)) 
4638                           (gnus-methods-using 'address))
4639                    (gnus-server-equal method gnus-message-archive-method))
4640                (nth 1 method)
4641                (not (string= (nth 1 method) "")))
4642               (concat "+" (nth 1 method)))
4643           ":" group))
4644
4645 (defun gnus-group-real-prefix (group)
4646   "Return the prefix of the current group name."
4647   (if (string-match "^[^:]+:" group)
4648       (substring group 0 (match-end 0))
4649     ""))
4650
4651 (defun gnus-group-method (group)
4652   "Return the server or method used for selecting GROUP."
4653   (let ((prefix (gnus-group-real-prefix group)))
4654     (if (equal prefix "")
4655         gnus-select-method
4656       (let ((servers gnus-opened-servers)
4657             (server "")
4658             backend possible found)
4659         (if (string-match "^[^\\+]+\\+" prefix)
4660             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4661                   server (substring prefix (match-end 0) (1- (length prefix))))
4662           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4663         (while servers
4664           (when (eq (caaar servers) backend)
4665             (setq possible (caar servers))
4666             (when (equal (cadaar servers) server)
4667               (setq found (caar servers))))
4668           (pop servers))
4669         (or (car (rassoc found gnus-server-alist))
4670             found
4671             (car (rassoc possible gnus-server-alist))
4672             possible
4673             (list backend server))))))
4674
4675 (defsubst gnus-secondary-method-p (method)
4676   "Return whether METHOD is a secondary select method."
4677   (let ((methods gnus-secondary-select-methods)
4678         (gmethod (gnus-server-get-method nil method)))
4679     (while (and methods
4680                 (not (equal (gnus-server-get-method nil (car methods))
4681                             gmethod)))
4682       (setq methods (cdr methods)))
4683     methods))
4684
4685 (defun gnus-group-foreign-p (group)
4686   "Say whether a group is foreign or not."
4687   (and (not (gnus-group-native-p group))
4688        (not (gnus-group-secondary-p group))))
4689
4690 (defun gnus-group-native-p (group)
4691   "Say whether the group is native or not."
4692   (not (string-match ":" group)))
4693
4694 (defun gnus-group-secondary-p (group)
4695   "Say whether the group is secondary or not."
4696   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4697
4698 (defun gnus-group-get-parameter (group &optional symbol)
4699   "Returns the group parameters for GROUP.
4700 If SYMBOL, return the value of that symbol in the group parameters."
4701   (let ((params (gnus-info-params (gnus-get-info group))))
4702     (if symbol
4703         (gnus-group-parameter-value params symbol)
4704       params)))
4705
4706 (defun gnus-group-parameter-value (params symbol)
4707   "Return the value of SYMBOL in group PARAMS."
4708   (or (car (memq symbol params))        ; It's either a simple symbol
4709       (cdr (assq symbol params))))      ; or a cons.
4710
4711 (defun gnus-group-add-parameter (group param)
4712   "Add parameter PARAM to GROUP."
4713   (let ((info (gnus-get-info group)))
4714     (if (not info)
4715         () ; This is a dead group.  We just ignore it.
4716       ;; Cons the new param to the old one and update.
4717       (gnus-group-set-info (cons param (gnus-info-params info))
4718                            group 'params))))
4719
4720 (defun gnus-group-set-parameter (group name value)
4721   "Set parameter NAME to VALUE in GROUP."
4722   (let ((info (gnus-get-info group)))
4723     (if (not info)
4724         () ; This is a dead group.  We just ignore it.
4725       (let ((old-params (gnus-info-params info))
4726             (new-params (list (cons name value))))
4727         (while old-params
4728           (if (or (not (listp (car old-params)))
4729                   (not (eq (caar old-params) name)))
4730               (setq new-params (append new-params (list (car old-params)))))
4731           (setq old-params (cdr old-params)))
4732         (gnus-group-set-info new-params group 'params)))))
4733
4734 (defun gnus-group-add-score (group &optional score)
4735   "Add SCORE to the GROUP score.
4736 If SCORE is nil, add 1 to the score of GROUP."
4737   (let ((info (gnus-get-info group)))
4738     (when info
4739       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4740
4741 (defun gnus-summary-bubble-group ()
4742   "Increase the score of the current group.
4743 This is a handy function to add to `gnus-summary-exit-hook' to
4744 increase the score of each group you read."
4745   (gnus-group-add-score gnus-newsgroup-name))
4746
4747 (defun gnus-group-set-info (info &optional method-only-group part)
4748   (let* ((entry (gnus-gethash
4749                  (or method-only-group (gnus-info-group info))
4750                  gnus-newsrc-hashtb))
4751          (part-info info)
4752          (info (if method-only-group (nth 2 entry) info))
4753          method)
4754     (when method-only-group
4755       (unless entry
4756         (error "Trying to change non-existent group %s" method-only-group))
4757       ;; We have received parts of the actual group info - either the
4758       ;; select method or the group parameters.  We first check
4759       ;; whether we have to extend the info, and if so, do that.
4760       (let ((len (length info))
4761             (total (if (eq part 'method) 5 6)))
4762         (when (< len total)
4763           (setcdr (nthcdr (1- len) info)
4764                   (make-list (- total len) nil)))
4765         ;; Then we enter the new info.
4766         (setcar (nthcdr (1- total) info) part-info)))
4767     (unless entry
4768       ;; This is a new group, so we just create it.
4769       (save-excursion
4770         (set-buffer gnus-group-buffer)
4771         (setq method (gnus-info-method info))
4772         (when (gnus-server-equal method "native")
4773           (setq method nil))
4774         (if method
4775             ;; It's a foreign group...
4776             (gnus-group-make-group
4777              (gnus-group-real-name (gnus-info-group info))
4778              (if (stringp method) method
4779                (prin1-to-string (car method)))
4780              (and (consp method)
4781                   (nth 1 (gnus-info-method info))))
4782           ;; It's a native group.
4783           (gnus-group-make-group (gnus-info-group info)))
4784         (gnus-message 6 "Note: New group created")
4785         (setq entry
4786               (gnus-gethash (gnus-group-prefixed-name
4787                              (gnus-group-real-name (gnus-info-group info))
4788                              (or (gnus-info-method info) gnus-select-method))
4789                             gnus-newsrc-hashtb))))
4790     ;; Whether it was a new group or not, we now have the entry, so we
4791     ;; can do the update.
4792     (if entry
4793         (progn
4794           (setcar (nthcdr 2 entry) info)
4795           (when (and (not (eq (car entry) t))
4796                      (gnus-active (gnus-info-group info)))
4797             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4798       (error "No such group: %s" (gnus-info-group info)))))
4799
4800 (defun gnus-group-set-method-info (group select-method)
4801   (gnus-group-set-info select-method group 'method))
4802
4803 (defun gnus-group-set-params-info (group params)
4804   (gnus-group-set-info params group 'params))
4805
4806 (defun gnus-group-update-group-line ()
4807   "Update the current line in the group buffer."
4808   (let* ((buffer-read-only nil)
4809          (group (gnus-group-group-name))
4810          (gnus-group-indentation (gnus-group-group-indentation))
4811          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4812     (and entry
4813          (not (gnus-ephemeral-group-p group))
4814          (gnus-dribble-enter
4815           (concat "(gnus-group-set-info '"
4816                   (prin1-to-string (nth 2 entry)) ")")))
4817     (gnus-delete-line)
4818     (gnus-group-insert-group-line-info group)
4819     (forward-line -1)
4820     (gnus-group-position-point)))
4821
4822 (defun gnus-group-insert-group-line-info (group)
4823   "Insert GROUP on the current line."
4824   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4825         active info)
4826     (if entry
4827         (progn
4828           ;; (Un)subscribed group.
4829           (setq info (nth 2 entry))
4830           (gnus-group-insert-group-line
4831            group (gnus-info-level info) (gnus-info-marks info)
4832            (or (car entry) t) (gnus-info-method info)))
4833       ;; This group is dead.
4834       (gnus-group-insert-group-line
4835        group
4836        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4837        nil
4838        (if (setq active (gnus-active group))
4839            (- (1+ (cdr active)) (car active)) 0)
4840        nil))))
4841
4842 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4843                                                     gnus-tmp-marked number
4844                                                     gnus-tmp-method)
4845   "Insert a group line in the group buffer."
4846   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4847          (gnus-tmp-number-total
4848           (if gnus-tmp-active
4849               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4850             0))
4851          (gnus-tmp-number-of-unread
4852           (if (numberp number) (int-to-string (max 0 number))
4853             "*"))
4854          (gnus-tmp-number-of-read
4855           (if (numberp number)
4856               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4857             "*"))
4858          (gnus-tmp-subscribed
4859           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4860                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4861                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4862                 (t ?K)))
4863          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4864          (gnus-tmp-newsgroup-description
4865           (if gnus-description-hashtb
4866               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4867             ""))
4868          (gnus-tmp-moderated
4869           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4870          (gnus-tmp-moderated-string
4871           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4872          (gnus-tmp-method
4873           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4874          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4875          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4876          (gnus-tmp-news-method-string
4877           (if gnus-tmp-method
4878               (format "(%s:%s)" (car gnus-tmp-method)
4879                       (cadr gnus-tmp-method)) ""))
4880          (gnus-tmp-marked-mark
4881           (if (and (numberp number)
4882                    (zerop number)
4883                    (cdr (assq 'tick gnus-tmp-marked)))
4884               ?* ? ))
4885          (gnus-tmp-process-marked
4886           (if (member gnus-tmp-group gnus-group-marked)
4887               gnus-process-mark ? ))
4888          (gnus-tmp-grouplens
4889           (or (and gnus-use-grouplens
4890                    (bbb-grouplens-group-p gnus-tmp-group))
4891               ""))
4892          (buffer-read-only nil)
4893          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4894     (beginning-of-line)
4895     (add-text-properties
4896      (point)
4897      (prog1 (1+ (point))
4898        ;; Insert the text.
4899        (eval gnus-group-line-format-spec))
4900      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4901        gnus-unread ,(if (numberp number)
4902                         (string-to-int gnus-tmp-number-of-unread)
4903                       t)
4904        gnus-marked ,gnus-tmp-marked-mark
4905        gnus-indentation ,gnus-group-indentation
4906        gnus-level ,gnus-tmp-level))
4907     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4908       (forward-line -1)
4909       (run-hooks 'gnus-group-update-hook)
4910       (forward-line))
4911     ;; Allow XEmacs to remove front-sticky text properties.
4912     (gnus-group-remove-excess-properties)))
4913
4914 (defun gnus-group-update-group (group &optional visible-only)
4915   "Update all lines where GROUP appear.
4916 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4917 already."
4918   (save-excursion
4919     (set-buffer gnus-group-buffer)
4920     ;; The buffer may be narrowed.
4921     (save-restriction
4922       (widen)
4923       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4924             (loc (point-min))
4925             found buffer-read-only)
4926         ;; Enter the current status into the dribble buffer.
4927         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4928           (if (and entry (not (gnus-ephemeral-group-p group)))
4929               (gnus-dribble-enter
4930                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4931                        ")"))))
4932         ;; Find all group instances.  If topics are in use, each group
4933         ;; may be listed in more than once.
4934         (while (setq loc (text-property-any
4935                           loc (point-max) 'gnus-group ident))
4936           (setq found t)
4937           (goto-char loc)
4938           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4939             (gnus-delete-line)
4940             (gnus-group-insert-group-line-info group))
4941           (setq loc (1+ loc)))
4942         (unless (or found visible-only)
4943           ;; No such line in the buffer, find out where it's supposed to
4944           ;; go, and insert it there (or at the end of the buffer).
4945           (if gnus-goto-missing-group-function
4946               (funcall gnus-goto-missing-group-function group)
4947             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4948               (while (and entry (car entry)
4949                           (not
4950                            (gnus-goto-char
4951                             (text-property-any
4952                              (point-min) (point-max)
4953                              'gnus-group (gnus-intern-safe
4954                                           (caar entry) gnus-active-hashtb)))))
4955                 (setq entry (cdr entry)))
4956               (or entry (goto-char (point-max)))))
4957           ;; Finally insert the line.
4958           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4959             (gnus-group-insert-group-line-info group)))
4960         (gnus-group-set-mode-line)))))
4961
4962 (defun gnus-group-set-mode-line ()
4963   (when (memq 'group gnus-updated-mode-lines)
4964     (let* ((gformat (or gnus-group-mode-line-format-spec
4965                         (setq gnus-group-mode-line-format-spec
4966                               (gnus-parse-format
4967                                gnus-group-mode-line-format
4968                                gnus-group-mode-line-format-alist))))
4969            (gnus-tmp-news-server (cadr gnus-select-method))
4970            (gnus-tmp-news-method (car gnus-select-method))
4971            (max-len 60)
4972            gnus-tmp-header                      ;Dummy binding for user-defined formats
4973            ;; Get the resulting string.
4974            (mode-string (eval gformat)))
4975       ;; If the line is too long, we chop it off.
4976       (when (> (length mode-string) max-len)
4977         (setq mode-string (substring mode-string 0 (- max-len 4))))
4978       (prog1
4979           (setq mode-line-buffer-identification (list mode-string))
4980         (set-buffer-modified-p t)))))
4981
4982 (defun gnus-group-group-name ()
4983   "Get the name of the newsgroup on the current line."
4984   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4985     (and group (symbol-name group))))
4986
4987 (defun gnus-group-group-level ()
4988   "Get the level of the newsgroup on the current line."
4989   (get-text-property (gnus-point-at-bol) 'gnus-level))
4990
4991 (defun gnus-group-group-indentation ()
4992   "Get the indentation of the newsgroup on the current line."
4993   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4994       (and gnus-group-indentation-function
4995            (funcall gnus-group-indentation-function))
4996       ""))
4997
4998 (defun gnus-group-group-unread ()
4999   "Get the number of unread articles of the newsgroup on the current line."
5000   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5001
5002 (defun gnus-group-search-forward (&optional backward all level first-too)
5003   "Find the next newsgroup with unread articles.
5004 If BACKWARD is non-nil, find the previous newsgroup instead.
5005 If ALL is non-nil, just find any newsgroup.
5006 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5007 group exists.
5008 If FIRST-TOO, the current line is also eligible as a target."
5009   (let ((way (if backward -1 1))
5010         (low gnus-level-killed)
5011         (beg (point))
5012         pos found lev)
5013     (if (and backward (progn (beginning-of-line)) (bobp))
5014         nil
5015       (or first-too (forward-line way))
5016       (while (and
5017               (not (eobp))
5018               (not (setq
5019                     found
5020                     (and (or all
5021                              (and
5022                               (let ((unread
5023                                      (get-text-property (point) 'gnus-unread)))
5024                                 (and (numberp unread) (> unread 0)))
5025                               (setq lev (get-text-property (point)
5026                                                            'gnus-level))
5027                               (<= lev gnus-level-subscribed)))
5028                          (or (not level)
5029                              (and (setq lev (get-text-property (point)
5030                                                                'gnus-level))
5031                                   (or (= lev level)
5032                                       (and (< lev low)
5033                                            (< level lev)
5034                                            (progn
5035                                              (setq low lev)
5036                                              (setq pos (point))
5037                                              nil))))))))
5038               (zerop (forward-line way)))))
5039     (if found
5040         (progn (gnus-group-position-point) t)
5041       (goto-char (or pos beg))
5042       (and pos t))))
5043
5044 ;;; Gnus group mode commands
5045
5046 ;; Group marking.
5047
5048 (defun gnus-group-mark-group (n &optional unmark no-advance)
5049   "Mark the current group."
5050   (interactive "p")
5051   (let ((buffer-read-only nil)
5052         group)
5053     (while
5054         (and (> n 0)
5055              (setq group (gnus-group-group-name))
5056              (progn
5057                (beginning-of-line)
5058                (forward-char
5059                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5060                (delete-char 1)
5061                (if unmark
5062                    (progn
5063                      (insert " ")
5064                      (setq gnus-group-marked (delete group gnus-group-marked)))
5065                  (insert "#")
5066                  (setq gnus-group-marked
5067                        (cons group (delete group gnus-group-marked))))
5068                t)
5069              (or no-advance (zerop (gnus-group-next-group 1))))
5070       (setq n (1- n)))
5071     (gnus-summary-position-point)
5072     n))
5073
5074 (defun gnus-group-unmark-group (n)
5075   "Remove the mark from the current group."
5076   (interactive "p")
5077   (gnus-group-mark-group n 'unmark)
5078   (gnus-group-position-point))
5079
5080 (defun gnus-group-unmark-all-groups ()
5081   "Unmark all groups."
5082   (interactive)
5083   (let ((groups gnus-group-marked))
5084     (save-excursion
5085       (while groups
5086         (gnus-group-remove-mark (pop groups)))))
5087   (gnus-group-position-point))
5088
5089 (defun gnus-group-mark-region (unmark beg end)
5090   "Mark all groups between point and mark.
5091 If UNMARK, remove the mark instead."
5092   (interactive "P\nr")
5093   (let ((num (count-lines beg end)))
5094     (save-excursion
5095       (goto-char beg)
5096       (- num (gnus-group-mark-group num unmark)))))
5097
5098 (defun gnus-group-mark-buffer (&optional unmark)
5099   "Mark all groups in the buffer.
5100 If UNMARK, remove the mark instead."
5101   (interactive "P")
5102   (gnus-group-mark-region unmark (point-min) (point-max)))
5103
5104 (defun gnus-group-mark-regexp (regexp)
5105   "Mark all groups that match some regexp."
5106   (interactive "sMark (regexp): ")
5107   (let ((alist (cdr gnus-newsrc-alist))
5108         group)
5109     (while alist
5110       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5111         (gnus-group-set-mark group))))
5112   (gnus-group-position-point))
5113
5114 (defun gnus-group-remove-mark (group)
5115   "Remove the process mark from GROUP and move point there.
5116 Return nil if the group isn't displayed."
5117   (if (gnus-group-goto-group group)
5118       (save-excursion
5119         (gnus-group-mark-group 1 'unmark t)
5120         t)
5121     (setq gnus-group-marked
5122           (delete group gnus-group-marked))
5123     nil))
5124
5125 (defun gnus-group-set-mark (group)
5126   "Set the process mark on GROUP."
5127   (if (gnus-group-goto-group group) 
5128       (save-excursion
5129         (gnus-group-mark-group 1 nil t))
5130     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5131
5132 (defun gnus-group-universal-argument (arg &optional groups func)
5133   "Perform any command on all groups accoring to the process/prefix convention."
5134   (interactive "P")
5135   (let ((groups (or groups (gnus-group-process-prefix arg)))
5136         group func)
5137     (if (eq (setq func (or func
5138                            (key-binding
5139                             (read-key-sequence
5140                              (substitute-command-keys
5141                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5142             'undefined)
5143         (gnus-error 1 "Undefined key")
5144       (while groups
5145         (gnus-group-remove-mark (setq group (pop groups)))
5146         (command-execute func))))
5147   (gnus-group-position-point))
5148
5149 (defun gnus-group-process-prefix (n)
5150   "Return a list of groups to work on.
5151 Take into consideration N (the prefix) and the list of marked groups."
5152   (cond
5153    (n
5154     (setq n (prefix-numeric-value n))
5155     ;; There is a prefix, so we return a list of the N next
5156     ;; groups.
5157     (let ((way (if (< n 0) -1 1))
5158           (n (abs n))
5159           group groups)
5160       (save-excursion
5161         (while (and (> n 0)
5162                     (setq group (gnus-group-group-name)))
5163           (setq groups (cons group groups))
5164           (setq n (1- n))
5165           (gnus-group-next-group way)))
5166       (nreverse groups)))
5167    ((and (boundp 'transient-mark-mode)
5168          transient-mark-mode
5169          mark-active)
5170     ;; Work on the region between point and mark.
5171     (let ((max (max (point) (mark)))
5172           groups)
5173       (save-excursion
5174         (goto-char (min (point) (mark)))
5175         (while
5176             (and
5177              (push (gnus-group-group-name) groups)
5178              (zerop (gnus-group-next-group 1))
5179              (< (point) max)))
5180         (nreverse groups))))
5181    (gnus-group-marked
5182     ;; No prefix, but a list of marked articles.
5183     (reverse gnus-group-marked))
5184    (t
5185     ;; Neither marked articles or a prefix, so we return the
5186     ;; current group.
5187     (let ((group (gnus-group-group-name)))
5188       (and group (list group))))))
5189
5190 ;; Selecting groups.
5191
5192 (defun gnus-group-read-group (&optional all no-article group)
5193   "Read news in this newsgroup.
5194 If the prefix argument ALL is non-nil, already read articles become
5195 readable.  IF ALL is a number, fetch this number of articles.  If the
5196 optional argument NO-ARTICLE is non-nil, no article will be
5197 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5198 group."
5199   (interactive "P")
5200   (let ((group (or group (gnus-group-group-name)))
5201         number active marked entry)
5202     (or group (error "No group on current line"))
5203     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5204                                             group gnus-newsrc-hashtb)))))
5205     ;; This group might be a dead group.  In that case we have to get
5206     ;; the number of unread articles from `gnus-active-hashtb'.
5207     (setq number
5208           (cond ((numberp all) all)
5209                 (entry (car entry))
5210                 ((setq active (gnus-active group))
5211                  (- (1+ (cdr active)) (car active)))))
5212     (gnus-summary-read-group
5213      group (or all (and (numberp number)
5214                         (zerop (+ number (length (cdr (assq 'tick marked)))
5215                                   (length (cdr (assq 'dormant marked)))))))
5216      no-article)))
5217
5218 (defun gnus-group-select-group (&optional all)
5219   "Select this newsgroup.
5220 No article is selected automatically.
5221 If ALL is non-nil, already read articles become readable.
5222 If ALL is a number, fetch this number of articles."
5223   (interactive "P")
5224   (gnus-group-read-group all t))
5225
5226 (defun gnus-group-quick-select-group (&optional all)
5227   "Select the current group \"quickly\".
5228 This means that no highlighting or scoring will be performed."
5229   (interactive "P")
5230   (let (gnus-visual
5231         gnus-score-find-score-files-function
5232         gnus-apply-kill-hook
5233         gnus-summary-expunge-below)
5234     (gnus-group-read-group all t)))
5235
5236 (defun gnus-group-visible-select-group (&optional all)
5237   "Select the current group without hiding any articles."
5238   (interactive "P")
5239   (let ((gnus-inhibit-limiting t))
5240     (gnus-group-read-group all t)))
5241
5242 ;;;###autoload
5243 (defun gnus-fetch-group (group)
5244   "Start Gnus if necessary and enter GROUP.
5245 Returns whether the fetching was successful or not."
5246   (interactive "sGroup name: ")
5247   (or (get-buffer gnus-group-buffer)
5248       (gnus))
5249   (gnus-group-read-group nil nil group))
5250
5251 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5252 ;; if selection was successful.
5253 (defun gnus-group-read-ephemeral-group
5254   (group method &optional activate quit-config)
5255   (let ((group (if (gnus-group-foreign-p group) group
5256                  (gnus-group-prefixed-name group method))))
5257     (gnus-sethash
5258      group
5259      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5260                      ((quit-config . ,(if quit-config quit-config
5261                                         (cons (current-buffer) 'summary))))))
5262      gnus-newsrc-hashtb)
5263     (set-buffer gnus-group-buffer)
5264     (or (gnus-check-server method)
5265         (error "Unable to contact server: %s" (gnus-status-message method)))
5266     (if activate (or (gnus-request-group group)
5267                      (error "Couldn't request group")))
5268     (condition-case ()
5269         (gnus-group-read-group t t group)
5270       (error nil)
5271       (quit nil))))
5272
5273 (defun gnus-group-jump-to-group (group)
5274   "Jump to newsgroup GROUP."
5275   (interactive
5276    (list (completing-read
5277           "Group: " gnus-active-hashtb nil
5278           (memq gnus-select-method gnus-have-read-active-file))))
5279
5280   (when (equal group "")
5281     (error "Empty group name"))
5282
5283   (when (string-match "[\000-\032]" group)
5284     (error "Control characters in group: %s" group))
5285
5286   (let ((b (text-property-any
5287             (point-min) (point-max)
5288             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5289     (unless (gnus-ephemeral-group-p group)
5290       (if b
5291           ;; Either go to the line in the group buffer...
5292           (goto-char b)
5293         ;; ... or insert the line.
5294         (or
5295          (gnus-active group)
5296          (gnus-activate-group group)
5297          (error "%s error: %s" group (gnus-status-message group)))
5298
5299         (gnus-group-update-group group)
5300         (goto-char (text-property-any
5301                     (point-min) (point-max)
5302                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5303     ;; Adjust cursor point.
5304     (gnus-group-position-point)))
5305
5306 (defun gnus-group-goto-group (group)
5307   "Goto to newsgroup GROUP."
5308   (when group
5309     (let ((b (text-property-any (point-min) (point-max)
5310                                 'gnus-group (gnus-intern-safe
5311                                              group gnus-active-hashtb))))
5312       (and b (goto-char b)))))
5313
5314 (defun gnus-group-next-group (n)
5315   "Go to next N'th newsgroup.
5316 If N is negative, search backward instead.
5317 Returns the difference between N and the number of skips actually
5318 done."
5319   (interactive "p")
5320   (gnus-group-next-unread-group n t))
5321
5322 (defun gnus-group-next-unread-group (n &optional all level)
5323   "Go to next N'th unread newsgroup.
5324 If N is negative, search backward instead.
5325 If ALL is non-nil, choose any newsgroup, unread or not.
5326 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5327 such group can be found, the next group with a level higher than
5328 LEVEL.
5329 Returns the difference between N and the number of skips actually
5330 made."
5331   (interactive "p")
5332   (let ((backward (< n 0))
5333         (n (abs n)))
5334     (while (and (> n 0)
5335                 (gnus-group-search-forward
5336                  backward (or (not gnus-group-goto-unread) all) level))
5337       (setq n (1- n)))
5338     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5339                                (if level " on this level or higher" "")))
5340     n))
5341
5342 (defun gnus-group-prev-group (n)
5343   "Go to previous N'th newsgroup.
5344 Returns the difference between N and the number of skips actually
5345 done."
5346   (interactive "p")
5347   (gnus-group-next-unread-group (- n) t))
5348
5349 (defun gnus-group-prev-unread-group (n)
5350   "Go to previous N'th unread newsgroup.
5351 Returns the difference between N and the number of skips actually
5352 done."
5353   (interactive "p")
5354   (gnus-group-next-unread-group (- n)))
5355
5356 (defun gnus-group-next-unread-group-same-level (n)
5357   "Go to next N'th unread newsgroup on the same level.
5358 If N is negative, search backward instead.
5359 Returns the difference between N and the number of skips actually
5360 done."
5361   (interactive "p")
5362   (gnus-group-next-unread-group n t (gnus-group-group-level))
5363   (gnus-group-position-point))
5364
5365 (defun gnus-group-prev-unread-group-same-level (n)
5366   "Go to next N'th unread newsgroup on the same level.
5367 Returns the difference between N and the number of skips actually
5368 done."
5369   (interactive "p")
5370   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5371   (gnus-group-position-point))
5372
5373 (defun gnus-group-best-unread-group (&optional exclude-group)
5374   "Go to the group with the highest level.
5375 If EXCLUDE-GROUP, do not go to that group."
5376   (interactive)
5377   (goto-char (point-min))
5378   (let ((best 100000)
5379         unread best-point)
5380     (while (not (eobp))
5381       (setq unread (get-text-property (point) 'gnus-unread))
5382       (if (and (numberp unread) (> unread 0))
5383           (progn
5384             (if (and (get-text-property (point) 'gnus-level)
5385                      (< (get-text-property (point) 'gnus-level) best)
5386                      (or (not exclude-group)
5387                          (not (equal exclude-group (gnus-group-group-name)))))
5388                 (progn
5389                   (setq best (get-text-property (point) 'gnus-level))
5390                   (setq best-point (point))))))
5391       (forward-line 1))
5392     (if best-point (goto-char best-point))
5393     (gnus-summary-position-point)
5394     (and best-point (gnus-group-group-name))))
5395
5396 (defun gnus-group-first-unread-group ()
5397   "Go to the first group with unread articles."
5398   (interactive)
5399   (prog1
5400       (let ((opoint (point))
5401             unread)
5402         (goto-char (point-min))
5403         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5404                 (and (numberp unread)   ; Not a topic.
5405                      (not (zerop unread))) ; Has unread articles.
5406                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5407             (point)                     ; Success.
5408           (goto-char opoint)
5409           nil))                         ; Not success.
5410     (gnus-group-position-point)))
5411
5412 (defun gnus-group-enter-server-mode ()
5413   "Jump to the server buffer."
5414   (interactive)
5415   (gnus-enter-server-buffer))
5416
5417 (defun gnus-group-make-group (name &optional method address)
5418   "Add a new newsgroup.
5419 The user will be prompted for a NAME, for a select METHOD, and an
5420 ADDRESS."
5421   (interactive
5422    (cons
5423     (read-string "Group name: ")
5424     (let ((method
5425            (completing-read
5426             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5427             nil t nil 'gnus-method-history)))
5428       (cond ((assoc method gnus-valid-select-methods)
5429              (list method
5430                    (if (memq 'prompt-address
5431                              (assoc method gnus-valid-select-methods))
5432                        (read-string "Address: ")
5433                      "")))
5434             ((assoc method gnus-server-alist)
5435              (list method))
5436             (t
5437              (list method ""))))))
5438
5439   (save-excursion
5440     (set-buffer gnus-group-buffer)
5441     (let* ((meth (and method (if address (list (intern method) address)
5442                                method)))
5443            (nname (if method (gnus-group-prefixed-name name meth) name))
5444            backend info)
5445       (and (gnus-gethash nname gnus-newsrc-hashtb)
5446            (error "Group %s already exists" nname))
5447       (gnus-group-change-level
5448        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5449        gnus-level-default-subscribed gnus-level-killed
5450        (and (gnus-group-group-name)
5451             (gnus-gethash (gnus-group-group-name)
5452                           gnus-newsrc-hashtb))
5453        t)
5454       (gnus-set-active nname (cons 1 0))
5455       (or (gnus-ephemeral-group-p name)
5456           (gnus-dribble-enter
5457            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5458       (gnus-group-insert-group-line-info nname)
5459
5460       (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5461                                                     nil meth))))
5462                    gnus-valid-select-methods)
5463         (require backend))
5464       (gnus-check-server meth)
5465       (and (gnus-check-backend-function 'request-create-group nname)
5466            (gnus-request-create-group nname))
5467       t)))
5468
5469 (defun gnus-group-delete-group (group &optional force)
5470   "Delete the current group.
5471 If FORCE (the prefix) is non-nil, all the articles in the group will
5472 be deleted.  This is \"deleted\" as in \"removed forever from the face
5473 of the Earth\".  There is no undo."
5474   (interactive
5475    (list (gnus-group-group-name)
5476          current-prefix-arg))
5477   (or group (error "No group to rename"))
5478   (or (gnus-check-backend-function 'request-delete-group group)
5479       (error "This backend does not support group deletion"))
5480   (prog1
5481       (if (not (gnus-yes-or-no-p
5482                 (format
5483                  "Do you really want to delete %s%s? "
5484                  group (if force " and all its contents" ""))))
5485           () ; Whew!
5486         (gnus-message 6 "Deleting group %s..." group)
5487         (if (not (gnus-request-delete-group group force))
5488             (gnus-error 3 "Couldn't delete group %s" group)
5489           (gnus-message 6 "Deleting group %s...done" group)
5490           (gnus-group-goto-group group)
5491           (gnus-group-kill-group 1 t)
5492           (gnus-sethash group nil gnus-active-hashtb)
5493           t))
5494     (gnus-group-position-point)))
5495
5496 (defun gnus-group-rename-group (group new-name)
5497   (interactive
5498    (list
5499     (gnus-group-group-name)
5500     (progn
5501       (or (gnus-check-backend-function
5502            'request-rename-group (gnus-group-group-name))
5503           (error "This backend does not support renaming groups"))
5504       (read-string "New group name: "))))
5505
5506   (or (gnus-check-backend-function 'request-rename-group group)
5507       (error "This backend does not support renaming groups"))
5508
5509   (or group (error "No group to rename"))
5510   (and (string-match "^[ \t]*$" new-name)
5511        (error "Not a valid group name"))
5512
5513   ;; We find the proper prefixed name.
5514   (setq new-name
5515         (gnus-group-prefixed-name
5516          (gnus-group-real-name new-name)
5517          (gnus-info-method (gnus-get-info group))))
5518
5519   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5520   (prog1
5521       (if (not (gnus-request-rename-group group new-name))
5522           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5523         ;; We rename the group internally by killing it...
5524         (gnus-group-goto-group group)
5525         (gnus-group-kill-group)
5526         ;; ... changing its name ...
5527         (setcar (cdar gnus-list-of-killed-groups) new-name)
5528         ;; ... and then yanking it.  Magic!
5529         (gnus-group-yank-group)
5530         (gnus-set-active new-name (gnus-active group))
5531         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5532         new-name)
5533     (gnus-group-position-point)))
5534
5535 (defun gnus-group-edit-group (group &optional part)
5536   "Edit the group on the current line."
5537   (interactive (list (gnus-group-group-name)))
5538   (let* ((part (or part 'info))
5539          (done-func `(lambda ()
5540                        "Exit editing mode and update the information."
5541                        (interactive)
5542                        (gnus-group-edit-group-done ',part ,group)))
5543          (winconf (current-window-configuration))
5544          info)
5545     (or group (error "No group on current line"))
5546     (or (setq info (gnus-get-info group))
5547         (error "Killed group; can't be edited"))
5548     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5549     (gnus-configure-windows 'edit-group)
5550     (gnus-add-current-to-buffer-list)
5551     (emacs-lisp-mode)
5552     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5553     (use-local-map (copy-keymap emacs-lisp-mode-map))
5554     (local-set-key "\C-c\C-c" done-func)
5555     (make-local-variable 'gnus-prev-winconf)
5556     (setq gnus-prev-winconf winconf)
5557     (erase-buffer)
5558     (insert
5559      (cond
5560       ((eq part 'method)
5561        ";; Type `C-c C-c' after editing the select method.\n\n")
5562       ((eq part 'params)
5563        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5564       ((eq part 'info)
5565        ";; Type `C-c C-c' after editing the group info.\n\n")))
5566     (insert
5567      (pp-to-string
5568       (cond ((eq part 'method)
5569              (or (gnus-info-method info) "native"))
5570             ((eq part 'params)
5571              (gnus-info-params info))
5572             (t info)))
5573      "\n")))
5574
5575 (defun gnus-group-edit-group-method (group)
5576   "Edit the select method of GROUP."
5577   (interactive (list (gnus-group-group-name)))
5578   (gnus-group-edit-group group 'method))
5579
5580 (defun gnus-group-edit-group-parameters (group)
5581   "Edit the group parameters of GROUP."
5582   (interactive (list (gnus-group-group-name)))
5583   (gnus-group-edit-group group 'params))
5584
5585 (defun gnus-group-edit-group-done (part group)
5586   "Get info from buffer, update variables and jump to the group buffer."
5587   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5588   (goto-char (point-min))
5589   (let* ((form (read (current-buffer)))
5590          (winconf gnus-prev-winconf)
5591          (method (cond ((eq part 'info) (nth 4 form))
5592                        ((eq part 'method) form)
5593                        (t nil)))
5594          (info (cond ((eq part 'info) form)
5595                      ((eq part 'method) (gnus-get-info group))
5596                      (t nil)))
5597          (new-group (if info
5598                       (if (or (not method)
5599                               (gnus-server-equal
5600                                gnus-select-method method))
5601                           (gnus-group-real-name (car info))
5602                         (gnus-group-prefixed-name
5603                          (gnus-group-real-name (car info)) method))
5604                       nil)))
5605     (when (and new-group
5606                (not (equal new-group group)))
5607       (when (gnus-group-goto-group group)
5608         (gnus-group-kill-group 1))
5609       (gnus-activate-group new-group))
5610     ;; Set the info.
5611     (if (and info new-group)
5612         (progn
5613           (setq info (gnus-copy-sequence info))
5614           (setcar info new-group)
5615           (unless (gnus-server-equal method "native")
5616             (unless (nthcdr 3 info)
5617               (nconc info (list nil nil)))
5618             (unless (nthcdr 4 info)
5619               (nconc info (list nil)))
5620             (gnus-info-set-method info method))
5621           (gnus-group-set-info info))
5622       (gnus-group-set-info form (or new-group group) part))
5623     (kill-buffer (current-buffer))
5624     (and winconf (set-window-configuration winconf))
5625     (set-buffer gnus-group-buffer)
5626     (gnus-group-update-group (or new-group group))
5627     (gnus-group-position-point)))
5628
5629 (defun gnus-group-make-help-group ()
5630   "Create the Gnus documentation group."
5631   (interactive)
5632   (let ((path load-path)
5633         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5634         file dir)
5635     (and (gnus-gethash name gnus-newsrc-hashtb)
5636          (error "Documentation group already exists"))
5637     (while path
5638       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5639             file nil)
5640       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5641                 (file-exists-p
5642                  (setq file (concat (file-name-directory
5643                                      (directory-file-name dir))
5644                                     "etc/gnus-tut.txt"))))
5645         (setq path nil)))
5646     (if (not file)
5647         (gnus-message 1 "Couldn't find doc group")
5648       (gnus-group-make-group
5649        (gnus-group-real-name name)
5650        (list 'nndoc "gnus-help"
5651              (list 'nndoc-address file)
5652              (list 'nndoc-article-type 'mbox)))))
5653   (gnus-group-position-point))
5654
5655 (defun gnus-group-make-doc-group (file type)
5656   "Create a group that uses a single file as the source."
5657   (interactive
5658    (list (read-file-name "File name: ")
5659          (and current-prefix-arg 'ask)))
5660   (when (eq type 'ask)
5661     (let ((err "")
5662           char found)
5663       (while (not found)
5664         (message
5665          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5666          err)
5667         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5668                           ((= char ?b) 'babyl)
5669                           ((= char ?d) 'digest)
5670                           ((= char ?f) 'forward)
5671                           ((= char ?a) 'mmfd)
5672                           (t (setq err (format "%c unknown. " char))
5673                              nil))))
5674       (setq type found)))
5675   (let* ((file (expand-file-name file))
5676          (name (gnus-generate-new-group-name
5677                 (gnus-group-prefixed-name
5678                  (file-name-nondirectory file) '(nndoc "")))))
5679     (gnus-group-make-group
5680      (gnus-group-real-name name)
5681      (list 'nndoc (file-name-nondirectory file)
5682            (list 'nndoc-address file)
5683            (list 'nndoc-article-type (or type 'guess))))
5684     (forward-line -1)
5685     (gnus-group-position-point)))
5686
5687 (defun gnus-group-make-archive-group (&optional all)
5688   "Create the (ding) Gnus archive group of the most recent articles.
5689 Given a prefix, create a full group."
5690   (interactive "P")
5691   (let ((group (gnus-group-prefixed-name
5692                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5693     (and (gnus-gethash group gnus-newsrc-hashtb)
5694          (error "Archive group already exists"))
5695     (gnus-group-make-group
5696      (gnus-group-real-name group)
5697      (list 'nndir (if all "hpc" "edu")
5698            (list 'nndir-directory
5699                  (if all gnus-group-archive-directory
5700                    gnus-group-recent-archive-directory)))))
5701   (forward-line -1)
5702   (gnus-group-position-point))
5703
5704 (defun gnus-group-make-directory-group (dir)
5705   "Create an nndir group.
5706 The user will be prompted for a directory.  The contents of this
5707 directory will be used as a newsgroup.  The directory should contain
5708 mail messages or news articles in files that have numeric names."
5709   (interactive
5710    (list (read-file-name "Create group from directory: ")))
5711   (or (file-exists-p dir) (error "No such directory"))
5712   (or (file-directory-p dir) (error "Not a directory"))
5713   (let ((ext "")
5714         (i 0)
5715         group)
5716     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5717       (setq group
5718             (gnus-group-prefixed-name
5719              (concat (file-name-as-directory (directory-file-name dir))
5720                      ext)
5721              '(nndir "")))
5722       (setq ext (format "<%d>" (setq i (1+ i)))))
5723     (gnus-group-make-group
5724      (gnus-group-real-name group)
5725      (list 'nndir group (list 'nndir-directory dir))))
5726   (forward-line -1)
5727   (gnus-group-position-point))
5728
5729 (defun gnus-group-make-kiboze-group (group address scores)
5730   "Create an nnkiboze group.
5731 The user will be prompted for a name, a regexp to match groups, and
5732 score file entries for articles to include in the group."
5733   (interactive
5734    (list
5735     (read-string "nnkiboze group name: ")
5736     (read-string "Source groups (regexp): ")
5737     (let ((headers (mapcar (lambda (group) (list group))
5738                            '("subject" "from" "number" "date" "message-id"
5739                              "references" "chars" "lines" "xref"
5740                              "followup" "all" "body" "head")))
5741           scores header regexp regexps)
5742       (while (not (equal "" (setq header (completing-read
5743                                           "Match on header: " headers nil t))))
5744         (setq regexps nil)
5745         (while (not (equal "" (setq regexp (read-string
5746                                             (format "Match on %s (string): "
5747                                                     header)))))
5748           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5749         (setq scores (cons (cons header regexps) scores)))
5750       scores)))
5751   (gnus-group-make-group group "nnkiboze" address)
5752   (save-excursion
5753     (gnus-set-work-buffer)
5754     (let (emacs-lisp-mode-hook)
5755       (pp scores (current-buffer)))
5756     (write-region (point-min) (point-max)
5757                   (gnus-score-file-name (concat "nnkiboze:" group))))
5758   (forward-line -1)
5759   (gnus-group-position-point))
5760
5761 (defun gnus-group-add-to-virtual (n vgroup)
5762   "Add the current group to a virtual group."
5763   (interactive
5764    (list current-prefix-arg
5765          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5766                           "nnvirtual:")))
5767   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5768       (error "%s is not an nnvirtual group" vgroup))
5769   (let* ((groups (gnus-group-process-prefix n))
5770          (method (gnus-info-method (gnus-get-info vgroup))))
5771     (setcar (cdr method)
5772             (concat
5773              (nth 1 method) "\\|"
5774              (mapconcat
5775               (lambda (s)
5776                 (gnus-group-remove-mark s)
5777                 (concat "\\(^" (regexp-quote s) "$\\)"))
5778               groups "\\|"))))
5779   (gnus-group-position-point))
5780
5781 (defun gnus-group-make-empty-virtual (group)
5782   "Create a new, fresh, empty virtual group."
5783   (interactive "sCreate new, empty virtual group: ")
5784   (let* ((method (list 'nnvirtual "^$"))
5785          (pgroup (gnus-group-prefixed-name group method)))
5786     ;; Check whether it exists already.
5787     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5788          (error "Group %s already exists." pgroup))
5789     ;; Subscribe the new group after the group on the current line.
5790     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5791     (gnus-group-update-group pgroup)
5792     (forward-line -1)
5793     (gnus-group-position-point)))
5794
5795 (defun gnus-group-enter-directory (dir)
5796   "Enter an ephemeral nneething group."
5797   (interactive "DDirectory to read: ")
5798   (let* ((method (list 'nneething dir))
5799          (leaf (gnus-group-prefixed-name
5800                 (file-name-nondirectory (directory-file-name dir))
5801                 method))
5802          (name (gnus-generate-new-group-name leaf)))
5803     (let ((nneething-read-only t))
5804       (or (gnus-group-read-ephemeral-group
5805            name method t
5806            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5807                                       'summary 'group)))
5808           (error "Couldn't enter %s" dir)))))
5809
5810 ;; Group sorting commands
5811 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5812
5813 (defun gnus-group-sort-groups (func &optional reverse)
5814   "Sort the group buffer according to FUNC.
5815 If REVERSE, reverse the sorting order."
5816   (interactive (list gnus-group-sort-function
5817                      current-prefix-arg))
5818   (let ((func (cond 
5819                ((not (listp func)) func)
5820                ((null func) func)
5821                ((= 1 (length func)) (car func))
5822                (t `(lambda (t1 t2)
5823                      ,(gnus-make-sort-function 
5824                        (reverse func)))))))
5825     ;; We peel off the dummy group from the alist.
5826     (when func
5827       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5828         (pop gnus-newsrc-alist))
5829       ;; Do the sorting.
5830       (setq gnus-newsrc-alist
5831             (sort gnus-newsrc-alist func))
5832       (when reverse
5833         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5834       ;; Regenerate the hash table.
5835       (gnus-make-hashtable-from-newsrc-alist)
5836       (gnus-group-list-groups))))
5837
5838 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5839   "Sort the group buffer alphabetically by group name.
5840 If REVERSE, sort in reverse order."
5841   (interactive "P")
5842   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5843
5844 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5845   "Sort the group buffer by number of unread articles.
5846 If REVERSE, sort in reverse order."
5847   (interactive "P")
5848   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5849
5850 (defun gnus-group-sort-groups-by-level (&optional reverse)
5851   "Sort the group buffer by group level.
5852 If REVERSE, sort in reverse order."
5853   (interactive "P")
5854   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5855
5856 (defun gnus-group-sort-groups-by-score (&optional reverse)
5857   "Sort the group buffer by group score.
5858 If REVERSE, sort in reverse order."
5859   (interactive "P")
5860   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5861
5862 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5863   "Sort the group buffer by group rank.
5864 If REVERSE, sort in reverse order."
5865   (interactive "P")
5866   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5867
5868 (defun gnus-group-sort-groups-by-method (&optional reverse)
5869   "Sort the group buffer alphabetically by backend name.
5870 If REVERSE, sort in reverse order."
5871   (interactive "P")
5872   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5873
5874 (defun gnus-group-sort-by-alphabet (info1 info2)
5875   "Sort alphabetically."
5876   (string< (gnus-info-group info1) (gnus-info-group info2)))
5877
5878 (defun gnus-group-sort-by-unread (info1 info2)
5879   "Sort by number of unread articles."
5880   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5881         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5882     (< (or (and (numberp n1) n1) 0)
5883        (or (and (numberp n2) n2) 0))))
5884
5885 (defun gnus-group-sort-by-level (info1 info2)
5886   "Sort by level."
5887   (< (gnus-info-level info1) (gnus-info-level info2)))
5888
5889 (defun gnus-group-sort-by-method (info1 info2)
5890   "Sort alphabetically by backend name."
5891   (string< (symbol-name (car (gnus-find-method-for-group
5892                               (gnus-info-group info1) info1)))
5893            (symbol-name (car (gnus-find-method-for-group
5894                               (gnus-info-group info2) info2)))))
5895
5896 (defun gnus-group-sort-by-score (info1 info2)
5897   "Sort by group score."
5898   (< (gnus-info-score info1) (gnus-info-score info2)))
5899
5900 (defun gnus-group-sort-by-rank (info1 info2)
5901   "Sort by level and score."
5902   (let ((level1 (gnus-info-level info1))
5903         (level2 (gnus-info-level info2)))
5904     (or (< level1 level2)
5905         (and (= level1 level2)
5906              (< (gnus-info-score info1) (gnus-info-score info2))))))
5907
5908 ;; Group catching up.
5909
5910 (defun gnus-group-clear-data (n)
5911   "Clear all marks and read ranges from the current group."
5912   (interactive "P")
5913   (let ((groups (gnus-group-process-prefix n))
5914         group info)
5915     (while (setq group (pop groups))
5916       (setq info (gnus-get-info group))
5917       (gnus-info-set-read info nil)
5918       (when (gnus-info-marks info)
5919         (gnus-info-set-marks info nil))
5920       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5921       (when (gnus-group-goto-group group)
5922         (gnus-group-remove-mark group)
5923         (gnus-group-update-group-line)))))
5924
5925 (defun gnus-group-catchup-current (&optional n all)
5926   "Mark all articles not marked as unread in current newsgroup as read.
5927 If prefix argument N is numeric, the ARG next newsgroups will be
5928 caught up.  If ALL is non-nil, marked articles will also be marked as
5929 read.  Cross references (Xref: header) of articles are ignored.
5930 The difference between N and actual number of newsgroups that were
5931 caught up is returned."
5932   (interactive "P")
5933   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5934                gnus-expert-user
5935                (gnus-y-or-n-p
5936                 (if all
5937                     "Do you really want to mark all articles as read? "
5938                   "Mark all unread articles as read? "))))
5939       n
5940     (let ((groups (gnus-group-process-prefix n))
5941           (ret 0))
5942       (while groups
5943         ;; Virtual groups have to be given special treatment.
5944         (let ((method (gnus-find-method-for-group (car groups))))
5945           (if (eq 'nnvirtual (car method))
5946               (nnvirtual-catchup-group
5947                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5948         (gnus-group-remove-mark (car groups))
5949         (if (prog1
5950                 (gnus-group-goto-group (car groups))
5951               (gnus-group-catchup (car groups) all))
5952             (gnus-group-update-group-line)
5953           (setq ret (1+ ret)))
5954         (setq groups (cdr groups)))
5955       (gnus-group-next-unread-group 1)
5956       ret)))
5957
5958 (defun gnus-group-catchup-current-all (&optional n)
5959   "Mark all articles in current newsgroup as read.
5960 Cross references (Xref: header) of articles are ignored."
5961   (interactive "P")
5962   (gnus-group-catchup-current n 'all))
5963
5964 (defun gnus-group-catchup (group &optional all)
5965   "Mark all articles in GROUP as read.
5966 If ALL is non-nil, all articles are marked as read.
5967 The return value is the number of articles that were marked as read,
5968 or nil if no action could be taken."
5969   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5970          (num (car entry)))
5971     ;; Do the updating only if the newsgroup isn't killed.
5972     (if (not (numberp (car entry)))
5973         (gnus-message 1 "Can't catch up; non-active group")
5974       ;; Do auto-expirable marks if that's required.
5975       (when (gnus-group-auto-expirable-p group)
5976         (gnus-add-marked-articles
5977          group 'expire (gnus-list-of-unread-articles group))
5978         (when all
5979           (let ((marks (nth 3 (nth 2 entry))))
5980             (gnus-add-marked-articles
5981              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5982             (gnus-add-marked-articles
5983              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5984       (when entry
5985         (gnus-update-read-articles group nil)
5986         ;; Also nix out the lists of marks and dormants.
5987         (when all
5988           (gnus-add-marked-articles group 'tick nil nil 'force)
5989           (gnus-add-marked-articles group 'dormant nil nil 'force))
5990         (run-hooks 'gnus-group-catchup-group-hook)
5991         num))))
5992
5993 (defun gnus-group-expire-articles (&optional n)
5994   "Expire all expirable articles in the current newsgroup."
5995   (interactive "P")
5996   (let ((groups (gnus-group-process-prefix n))
5997         group)
5998     (unless groups
5999       (error "No groups to expire"))
6000     (while (setq group (pop groups))
6001       (gnus-group-remove-mark group)
6002       (when (gnus-check-backend-function 'request-expire-articles group)
6003         (gnus-message 6 "Expiring articles in %s..." group)
6004         (let* ((info (gnus-get-info group))
6005                (expirable (if (gnus-group-total-expirable-p group)
6006                               (cons nil (gnus-list-of-read-articles group))
6007                             (assq 'expire (gnus-info-marks info))))
6008                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6009           (when expirable
6010             (setcdr
6011              expirable
6012              (gnus-compress-sequence
6013               (if expiry-wait
6014                   ;; We set the expiry variables to the groupp
6015                   ;; parameter. 
6016                   (let ((nnmail-expiry-wait-function nil)
6017                         (nnmail-expiry-wait expiry-wait))
6018                     (gnus-request-expire-articles
6019                      (gnus-uncompress-sequence (cdr expirable)) group))
6020                 ;; Just expire using the normal expiry values.
6021                 (gnus-request-expire-articles
6022                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6023           (gnus-message 6 "Expiring articles in %s...done" group)))
6024       (gnus-group-position-point))))
6025
6026 (defun gnus-group-expire-all-groups ()
6027   "Expire all expirable articles in all newsgroups."
6028   (interactive)
6029   (save-excursion
6030     (gnus-message 5 "Expiring...")
6031     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6032                                      (cdr gnus-newsrc-alist))))
6033       (gnus-group-expire-articles nil)))
6034   (gnus-group-position-point)
6035   (gnus-message 5 "Expiring...done"))
6036
6037 (defun gnus-group-set-current-level (n level)
6038   "Set the level of the next N groups to LEVEL."
6039   (interactive
6040    (list
6041     current-prefix-arg
6042     (string-to-int
6043      (let ((s (read-string
6044                (format "Level (default %s): "
6045                        (or (gnus-group-group-level) 
6046                            gnus-level-default-subscribed)))))
6047        (if (string-match "^\\s-*$" s)
6048            (int-to-string (or (gnus-group-group-level) 
6049                               gnus-level-default-subscribed))
6050          s)))))
6051   (or (and (>= level 1) (<= level gnus-level-killed))
6052       (error "Illegal level: %d" level))
6053   (let ((groups (gnus-group-process-prefix n))
6054         group)
6055     (while (setq group (pop groups))
6056       (gnus-group-remove-mark group)
6057       (gnus-message 6 "Changed level of %s from %d to %d"
6058                     group (or (gnus-group-group-level) gnus-level-killed)
6059                     level)
6060       (gnus-group-change-level
6061        group level (or (gnus-group-group-level) gnus-level-killed))
6062       (gnus-group-update-group-line)))
6063   (gnus-group-position-point))
6064
6065 (defun gnus-group-unsubscribe-current-group (&optional n)
6066   "Toggle subscription of the current group.
6067 If given numerical prefix, toggle the N next groups."
6068   (interactive "P")
6069   (let ((groups (gnus-group-process-prefix n))
6070         group)
6071     (while groups
6072       (setq group (car groups)
6073             groups (cdr groups))
6074       (gnus-group-remove-mark group)
6075       (gnus-group-unsubscribe-group
6076        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6077                  gnus-level-default-unsubscribed
6078                gnus-level-default-subscribed) t)
6079       (gnus-group-update-group-line))
6080     (gnus-group-next-group 1)))
6081
6082 (defun gnus-group-unsubscribe-group (group &optional level silent)
6083   "Toggle subscription to GROUP.
6084 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6085 group line."
6086   (interactive
6087    (list (completing-read
6088           "Group: " gnus-active-hashtb nil
6089           (memq gnus-select-method gnus-have-read-active-file))))
6090   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6091     (cond
6092      ((string-match "^[ \t]$" group)
6093       (error "Empty group name"))
6094      (newsrc
6095       ;; Toggle subscription flag.
6096       (gnus-group-change-level
6097        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6098                                       gnus-level-subscribed)
6099                                   (1+ gnus-level-subscribed)
6100                                 gnus-level-default-subscribed)))
6101       (unless silent
6102         (gnus-group-update-group group)))
6103      ((and (stringp group)
6104            (or (not (memq gnus-select-method gnus-have-read-active-file))
6105                (gnus-active group)))
6106       ;; Add new newsgroup.
6107       (gnus-group-change-level
6108        group
6109        (if level level gnus-level-default-subscribed)
6110        (or (and (member group gnus-zombie-list)
6111                 gnus-level-zombie)
6112            gnus-level-killed)
6113        (and (gnus-group-group-name)
6114             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6115       (unless silent
6116         (gnus-group-update-group group)))
6117      (t (error "No such newsgroup: %s" group)))
6118     (gnus-group-position-point)))
6119
6120 (defun gnus-group-transpose-groups (n)
6121   "Move the current newsgroup up N places.
6122 If given a negative prefix, move down instead.  The difference between
6123 N and the number of steps taken is returned."
6124   (interactive "p")
6125   (or (gnus-group-group-name)
6126       (error "No group on current line"))
6127   (gnus-group-kill-group 1)
6128   (prog1
6129       (forward-line (- n))
6130     (gnus-group-yank-group)
6131     (gnus-group-position-point)))
6132
6133 (defun gnus-group-kill-all-zombies ()
6134   "Kill all zombie newsgroups."
6135   (interactive)
6136   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6137   (setq gnus-zombie-list nil)
6138   (gnus-group-list-groups))
6139
6140 (defun gnus-group-kill-region (begin end)
6141   "Kill newsgroups in current region (excluding current point).
6142 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6143   (interactive "r")
6144   (let ((lines
6145          ;; Count lines.
6146          (save-excursion
6147            (count-lines
6148             (progn
6149               (goto-char begin)
6150               (beginning-of-line)
6151               (point))
6152             (progn
6153               (goto-char end)
6154               (beginning-of-line)
6155               (point))))))
6156     (goto-char begin)
6157     (beginning-of-line)                 ;Important when LINES < 1
6158     (gnus-group-kill-group lines)))
6159
6160 (defun gnus-group-kill-group (&optional n discard)
6161   "Kill the next N groups.
6162 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6163 However, only groups that were alive can be yanked; already killed
6164 groups or zombie groups can't be yanked.
6165 The return value is the name of the group that was killed, or a list
6166 of groups killed."
6167   (interactive "P")
6168   (let ((buffer-read-only nil)
6169         (groups (gnus-group-process-prefix n))
6170         group entry level out)
6171     (if (< (length groups) 10)
6172         ;; This is faster when there are few groups.
6173         (while groups
6174           (push (setq group (pop groups)) out)
6175           (gnus-group-remove-mark group)
6176           (setq level (gnus-group-group-level))
6177           (gnus-delete-line)
6178           (when (and (not discard)
6179                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6180             (push (cons (car entry) (nth 2 entry))
6181                   gnus-list-of-killed-groups))
6182           (gnus-group-change-level
6183            (if entry entry group) gnus-level-killed (if entry nil level)))
6184       ;; If there are lots and lots of groups to be killed, we use
6185       ;; this thing instead.
6186       (let (entry)
6187         (setq groups (nreverse groups))
6188         (while groups
6189           (gnus-group-remove-mark (setq group (pop groups)))
6190           (gnus-delete-line)
6191           (cond
6192            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6193             (push (cons (car entry) (nth 2 entry))
6194                   gnus-list-of-killed-groups)
6195             (setcdr (cdr entry) (cdddr entry)))
6196            ((member group gnus-zombie-list)
6197             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6198         (gnus-make-hashtable-from-newsrc-alist)))
6199
6200     (gnus-group-position-point)
6201     (if (< (length out) 2) (car out) (nreverse out))))
6202
6203 (defun gnus-group-yank-group (&optional arg)
6204   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6205 inserting it before the current newsgroup.  The numeric ARG specifies
6206 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6207 is returned, or (if several groups are yanked) a list of yanked groups
6208 is returned."
6209   (interactive "p")
6210   (setq arg (or arg 1))
6211   (let (info group prev out)
6212     (while (>= (decf arg) 0)
6213       (if (not (setq info (pop gnus-list-of-killed-groups)))
6214           (error "No more newsgroups to yank"))
6215       (push (setq group (nth 1 info)) out)
6216       ;; Find which newsgroup to insert this one before - search
6217       ;; backward until something suitable is found.  If there are no
6218       ;; other newsgroups in this buffer, just make this newsgroup the
6219       ;; first newsgroup.
6220       (setq prev (gnus-group-group-name))
6221       (gnus-group-change-level
6222        info (gnus-info-level (cdr info)) gnus-level-killed
6223        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6224        t)
6225       (gnus-group-insert-group-line-info group))
6226     (forward-line -1)
6227     (gnus-group-position-point)
6228     (if (< (length out) 2) (car out) (nreverse out))))
6229
6230 (defun gnus-group-kill-level (level)
6231   "Kill all groups that is on a certain LEVEL."
6232   (interactive "nKill all groups on level: ")
6233   (cond
6234    ((= level gnus-level-zombie)
6235     (setq gnus-killed-list
6236           (nconc gnus-zombie-list gnus-killed-list))
6237     (setq gnus-zombie-list nil))
6238    ((and (< level gnus-level-zombie)
6239          (> level 0)
6240          (or gnus-expert-user
6241              (gnus-yes-or-no-p
6242               (format
6243                "Do you really want to kill all groups on level %d? "
6244                level))))
6245     (let* ((prev gnus-newsrc-alist)
6246            (alist (cdr prev)))
6247       (while alist
6248         (if (= (gnus-info-level level) level)
6249             (setcdr prev (cdr alist))
6250           (setq prev alist))
6251         (setq alist (cdr alist)))
6252       (gnus-make-hashtable-from-newsrc-alist)
6253       (gnus-group-list-groups)))
6254    (t
6255     (error "Can't kill; illegal level: %d" level))))
6256
6257 (defun gnus-group-list-all-groups (&optional arg)
6258   "List all newsgroups with level ARG or lower.
6259 Default is gnus-level-unsubscribed, which lists all subscribed and most
6260 unsubscribed groups."
6261   (interactive "P")
6262   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6263
6264 ;; Redefine this to list ALL killed groups if prefix arg used.
6265 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6266 (defun gnus-group-list-killed (&optional arg)
6267   "List all killed newsgroups in the group buffer.
6268 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6269 entail asking the server for the groups."
6270   (interactive "P")
6271   ;; Find all possible killed newsgroups if arg.
6272   (when arg
6273     ;; First make sure active file has been read.
6274     (unless gnus-have-read-active-file
6275       (let ((gnus-read-active-file t))
6276         (gnus-read-active-file)))
6277     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6278     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6279     (mapatoms
6280      (lambda (sym)
6281        (let ((groups 0)
6282              (group (symbol-name sym)))
6283          (if (or (null group)
6284                  (gnus-gethash group gnus-killed-hashtb)
6285                  (gnus-gethash group gnus-newsrc-hashtb))
6286              ()
6287            (let ((do-sub (gnus-matches-options-n group)))
6288              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6289                  ()
6290                (setq groups (1+ groups))
6291                (setq gnus-killed-list
6292                      (cons group gnus-killed-list))
6293                (gnus-sethash group group gnus-killed-hashtb))))))
6294      gnus-active-hashtb))
6295   (if (not gnus-killed-list)
6296       (gnus-message 6 "No killed groups")
6297     (let (gnus-group-list-mode)
6298       (funcall gnus-group-prepare-function
6299                gnus-level-killed t gnus-level-killed))
6300     (goto-char (point-min)))
6301   (gnus-group-position-point))
6302
6303 (defun gnus-group-list-zombies ()
6304   "List all zombie newsgroups in the group buffer."
6305   (interactive)
6306   (if (not gnus-zombie-list)
6307       (gnus-message 6 "No zombie groups")
6308     (let (gnus-group-list-mode)
6309       (funcall gnus-group-prepare-function
6310                gnus-level-zombie t gnus-level-zombie))
6311     (goto-char (point-min)))
6312   (gnus-group-position-point))
6313
6314 (defun gnus-group-list-active ()
6315   "List all groups that are available from the server(s)."
6316   (interactive)
6317   ;; First we make sure that we have really read the active file.
6318   (unless gnus-have-read-active-file
6319     (let ((gnus-read-active-file t))
6320       (gnus-read-active-file)))
6321   ;; Find all groups and sort them.
6322   (let ((groups
6323          (sort
6324           (let (list)
6325             (mapatoms
6326              (lambda (sym)
6327                (and (symbol-value sym)
6328                     (setq list (cons (symbol-name sym) list))))
6329              gnus-active-hashtb)
6330             list)
6331           'string<))
6332         (buffer-read-only nil))
6333     (erase-buffer)
6334     (while groups
6335       (gnus-group-insert-group-line-info (pop groups)))
6336     (goto-char (point-min))))
6337
6338 (defun gnus-activate-all-groups (level)
6339   "Activate absolutely all groups."
6340   (interactive (list 7))
6341   (let ((gnus-activate-level level)
6342         (gnus-activate-foreign-newsgroups level))
6343     (gnus-group-get-new-news)))
6344
6345 (defun gnus-group-get-new-news (&optional arg)
6346   "Get newly arrived articles.
6347 If ARG is a number, it specifies which levels you are interested in
6348 re-scanning.  If ARG is non-nil and not a number, this will force
6349 \"hard\" re-reading of the active files from all servers."
6350   (interactive "P")
6351   (run-hooks 'gnus-get-new-news-hook)
6352   ;; We might read in new NoCeM messages here.
6353   (when (and gnus-use-nocem 
6354              (null arg))
6355     (gnus-nocem-scan-groups))
6356   ;; If ARG is not a number, then we read the active file.
6357   (when (and arg (not (numberp arg)))
6358     (let ((gnus-read-active-file t))
6359       (gnus-read-active-file))
6360     (setq arg nil))
6361
6362   (setq arg (gnus-group-default-level arg t))
6363   (if (and gnus-read-active-file (not arg))
6364       (progn
6365         (gnus-read-active-file)
6366         (gnus-get-unread-articles arg))
6367     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6368       (gnus-get-unread-articles arg)))
6369   (run-hooks 'gnus-after-getting-new-news-hook)
6370   (gnus-group-list-groups))
6371
6372 (defun gnus-group-get-new-news-this-group (&optional n)
6373   "Check for newly arrived news in the current group (and the N-1 next groups).
6374 The difference between N and the number of newsgroup checked is returned.
6375 If N is negative, this group and the N-1 previous groups will be checked."
6376   (interactive "P")
6377   (let* ((groups (gnus-group-process-prefix n))
6378          (ret (if (numberp n) (- n (length groups)) 0))
6379          (beg (unless n (point)))
6380          group)
6381     (while (setq group (pop groups))
6382       (gnus-group-remove-mark group)
6383       (if (gnus-activate-group group 'scan)
6384           (progn
6385             (gnus-get-unread-articles-in-group
6386              (gnus-get-info group) (gnus-active group) t)
6387             (unless (gnus-virtual-group-p group)
6388               (gnus-close-group group))
6389             (gnus-group-update-group group))
6390         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6391     (when beg (goto-char beg))
6392     (when gnus-goto-next-group-when-activating
6393       (gnus-group-next-unread-group 1 t))
6394     (gnus-summary-position-point)
6395     ret))
6396
6397 (defun gnus-group-fetch-faq (group &optional faq-dir)
6398   "Fetch the FAQ for the current group."
6399   (interactive
6400    (list
6401     (gnus-group-real-name (gnus-group-group-name))
6402     (cond (current-prefix-arg
6403            (completing-read
6404             "Faq dir: " (and (listp gnus-group-faq-directory)
6405                              gnus-group-faq-directory))))))
6406   (or faq-dir
6407       (setq faq-dir (if (listp gnus-group-faq-directory)
6408                         (car gnus-group-faq-directory)
6409                       gnus-group-faq-directory)))
6410   (or group (error "No group name given"))
6411   (let ((file (concat (file-name-as-directory faq-dir)
6412                       (gnus-group-real-name group))))
6413     (if (not (file-exists-p file))
6414         (error "No such file: %s" file)
6415       (find-file file))))
6416
6417 (defun gnus-group-describe-group (force &optional group)
6418   "Display a description of the current newsgroup."
6419   (interactive (list current-prefix-arg (gnus-group-group-name)))
6420   (and force (setq gnus-description-hashtb nil))
6421   (let ((method (gnus-find-method-for-group group))
6422         desc)
6423     (or group (error "No group name given"))
6424     (and (or (and gnus-description-hashtb
6425                   ;; We check whether this group's method has been
6426                   ;; queried for a description file.
6427                   (gnus-gethash
6428                    (gnus-group-prefixed-name "" method)
6429                    gnus-description-hashtb))
6430              (setq desc (gnus-group-get-description group))
6431              (gnus-read-descriptions-file method))
6432          (gnus-message 1
6433           (or desc (gnus-gethash group gnus-description-hashtb)
6434               "No description available")))))
6435
6436 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6437 (defun gnus-group-describe-all-groups (&optional force)
6438   "Pop up a buffer with descriptions of all newsgroups."
6439   (interactive "P")
6440   (and force (setq gnus-description-hashtb nil))
6441   (if (not (or gnus-description-hashtb
6442                (gnus-read-all-descriptions-files)))
6443       (error "Couldn't request descriptions file"))
6444   (let ((buffer-read-only nil)
6445         b)
6446     (erase-buffer)
6447     (mapatoms
6448      (lambda (group)
6449        (setq b (point))
6450        (insert (format "      *: %-20s %s\n" (symbol-name group)
6451                        (symbol-value group)))
6452        (add-text-properties
6453         b (1+ b) (list 'gnus-group group
6454                        'gnus-unread t 'gnus-marked nil
6455                        'gnus-level (1+ gnus-level-subscribed))))
6456      gnus-description-hashtb)
6457     (goto-char (point-min))
6458     (gnus-group-position-point)))
6459
6460 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6461 (defun gnus-group-apropos (regexp &optional search-description)
6462   "List all newsgroups that have names that match a regexp."
6463   (interactive "sGnus apropos (regexp): ")
6464   (let ((prev "")
6465         (obuf (current-buffer))
6466         groups des)
6467     ;; Go through all newsgroups that are known to Gnus.
6468     (mapatoms
6469      (lambda (group)
6470        (and (symbol-name group)
6471             (string-match regexp (symbol-name group))
6472             (setq groups (cons (symbol-name group) groups))))
6473      gnus-active-hashtb)
6474     ;; Also go through all descriptions that are known to Gnus.
6475     (when search-description
6476       (mapatoms
6477        (lambda (group)
6478          (and (string-match regexp (symbol-value group))
6479               (gnus-active (symbol-name group))
6480               (setq groups (cons (symbol-name group) groups))))
6481        gnus-description-hashtb))
6482     (if (not groups)
6483         (gnus-message 3 "No groups matched \"%s\"." regexp)
6484       ;; Print out all the groups.
6485       (save-excursion
6486         (pop-to-buffer "*Gnus Help*")
6487         (buffer-disable-undo (current-buffer))
6488         (erase-buffer)
6489         (setq groups (sort groups 'string<))
6490         (while groups
6491           ;; Groups may be entered twice into the list of groups.
6492           (if (not (string= (car groups) prev))
6493               (progn
6494                 (insert (setq prev (car groups)) "\n")
6495                 (if (and gnus-description-hashtb
6496                          (setq des (gnus-gethash (car groups)
6497                                                  gnus-description-hashtb)))
6498                     (insert "  " des "\n"))))
6499           (setq groups (cdr groups)))
6500         (goto-char (point-min))))
6501     (pop-to-buffer obuf)))
6502
6503 (defun gnus-group-description-apropos (regexp)
6504   "List all newsgroups that have names or descriptions that match a regexp."
6505   (interactive "sGnus description apropos (regexp): ")
6506   (if (not (or gnus-description-hashtb
6507                (gnus-read-all-descriptions-files)))
6508       (error "Couldn't request descriptions file"))
6509   (gnus-group-apropos regexp t))
6510
6511 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6512 (defun gnus-group-list-matching (level regexp &optional all lowest)
6513   "List all groups with unread articles that match REGEXP.
6514 If the prefix LEVEL is non-nil, it should be a number that says which
6515 level to cut off listing groups.
6516 If ALL, also list groups with no unread articles.
6517 If LOWEST, don't list groups with level lower than LOWEST."
6518   (interactive "P\nsList newsgroups matching: ")
6519   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6520                            all (or lowest 1) regexp)
6521   (goto-char (point-min))
6522   (gnus-group-position-point))
6523
6524 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6525   "List all groups that match REGEXP.
6526 If the prefix LEVEL is non-nil, it should be a number that says which
6527 level to cut off listing groups.
6528 If LOWEST, don't list groups with level lower than LOWEST."
6529   (interactive "P\nsList newsgroups matching: ")
6530   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6531
6532 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6533 (defun gnus-group-save-newsrc (&optional force)
6534   "Save the Gnus startup files.
6535 If FORCE, force saving whether it is necessary or not."
6536   (interactive "P")
6537   (gnus-save-newsrc-file force))
6538
6539 (defun gnus-group-restart (&optional arg)
6540   "Force Gnus to read the .newsrc file."
6541   (interactive "P")
6542   (when (gnus-yes-or-no-p
6543          (format "Are you sure you want to read %s? "
6544                  gnus-current-startup-file))
6545     (gnus-save-newsrc-file)
6546     (gnus-setup-news 'force)
6547     (gnus-group-list-groups arg)))
6548
6549 (defun gnus-group-read-init-file ()
6550   "Read the Gnus elisp init file."
6551   (interactive)
6552   (gnus-read-init-file))
6553
6554 (defun gnus-group-check-bogus-groups (&optional silent)
6555   "Check bogus newsgroups.
6556 If given a prefix, don't ask for confirmation before removing a bogus
6557 group."
6558   (interactive "P")
6559   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6560   (gnus-group-list-groups))
6561
6562 (defun gnus-group-edit-global-kill (&optional article group)
6563   "Edit the global kill file.
6564 If GROUP, edit that local kill file instead."
6565   (interactive "P")
6566   (setq gnus-current-kill-article article)
6567   (gnus-kill-file-edit-file group)
6568   (gnus-message
6569    6
6570    (substitute-command-keys
6571     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6572             (if group "local" "global")))))
6573
6574 (defun gnus-group-edit-local-kill (article group)
6575   "Edit a local kill file."
6576   (interactive (list nil (gnus-group-group-name)))
6577   (gnus-group-edit-global-kill article group))
6578
6579 (defun gnus-group-force-update ()
6580   "Update `.newsrc' file."
6581   (interactive)
6582   (gnus-save-newsrc-file))
6583
6584 (defun gnus-group-suspend ()
6585   "Suspend the current Gnus session.
6586 In fact, cleanup buffers except for group mode buffer.
6587 The hook gnus-suspend-gnus-hook is called before actually suspending."
6588   (interactive)
6589   (run-hooks 'gnus-suspend-gnus-hook)
6590   ;; Kill Gnus buffers except for group mode buffer.
6591   (let ((group-buf (get-buffer gnus-group-buffer)))
6592     ;; Do this on a separate list in case the user does a ^G before we finish
6593     (let ((gnus-buffer-list
6594            (delq group-buf (delq gnus-dribble-buffer
6595                                  (append gnus-buffer-list nil)))))
6596       (while gnus-buffer-list
6597         (gnus-kill-buffer (car gnus-buffer-list))
6598         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6599     (if group-buf
6600         (progn
6601           (setq gnus-buffer-list (list group-buf))
6602           (bury-buffer group-buf)
6603           (delete-windows-on group-buf t)))))
6604
6605 (defun gnus-group-clear-dribble ()
6606   "Clear all information from the dribble buffer."
6607   (interactive)
6608   (gnus-dribble-clear)
6609   (gnus-message 7 "Cleared dribble buffer"))
6610
6611 (defun gnus-group-exit ()
6612   "Quit reading news after updating .newsrc.eld and .newsrc.
6613 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6614   (interactive)
6615   (when 
6616       (or noninteractive                ;For gnus-batch-kill
6617           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6618           (not gnus-interactive-exit)   ;Without confirmation
6619           gnus-expert-user
6620           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6621     (run-hooks 'gnus-exit-gnus-hook)
6622     ;; Offer to save data from non-quitted summary buffers.
6623     (gnus-offer-save-summaries)
6624     ;; Save the newsrc file(s).
6625     (gnus-save-newsrc-file)
6626     ;; Kill-em-all.
6627     (gnus-close-backends)
6628     ;; Reset everything.
6629     (gnus-clear-system)
6630     ;; Allow the user to do things after cleaning up.
6631     (run-hooks 'gnus-after-exiting-gnus-hook)))
6632
6633 (defun gnus-close-backends ()
6634   ;; Send a close request to all backends that support such a request.
6635   (let ((methods gnus-valid-select-methods)
6636         func)
6637     (while methods
6638       (if (fboundp (setq func (intern (concat (caar methods)
6639                                               "-request-close"))))
6640           (funcall func))
6641       (setq methods (cdr methods)))))
6642
6643 (defun gnus-group-quit ()
6644   "Quit reading news without updating .newsrc.eld or .newsrc.
6645 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6646   (interactive)
6647   (when (or noninteractive              ;For gnus-batch-kill
6648             (zerop (buffer-size))
6649             (not (gnus-server-opened gnus-select-method))
6650             gnus-expert-user
6651             (not gnus-current-startup-file)
6652             (gnus-yes-or-no-p
6653              (format "Quit reading news without saving %s? "
6654                      (file-name-nondirectory gnus-current-startup-file))))
6655     (run-hooks 'gnus-exit-gnus-hook)
6656     (if gnus-use-full-window
6657         (delete-other-windows)
6658       (gnus-remove-some-windows))
6659     (gnus-dribble-save)
6660     (gnus-close-backends)
6661     (gnus-clear-system)
6662     ;; Allow the user to do things after cleaning up.
6663     (run-hooks 'gnus-after-exiting-gnus-hook)))
6664
6665 (defun gnus-offer-save-summaries ()
6666   "Offer to save all active summary buffers."
6667   (save-excursion
6668     (let ((buflist (buffer-list))
6669           buffers bufname)
6670       ;; Go through all buffers and find all summaries.
6671       (while buflist
6672         (and (setq bufname (buffer-name (car buflist)))
6673              (string-match "Summary" bufname)
6674              (save-excursion
6675                (set-buffer bufname)
6676                ;; We check that this is, indeed, a summary buffer.
6677                (and (eq major-mode 'gnus-summary-mode)
6678                     ;; Also make sure this isn't bogus.
6679                     gnus-newsgroup-prepared))
6680              (push bufname buffers))
6681         (setq buflist (cdr buflist)))
6682       ;; Go through all these summary buffers and offer to save them.
6683       (when buffers
6684         (map-y-or-n-p
6685          "Update summary buffer %s? "
6686          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6687          buffers)))))
6688
6689 (defun gnus-group-describe-briefly ()
6690   "Give a one line description of the group mode commands."
6691   (interactive)
6692   (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")))
6693
6694 (defun gnus-group-browse-foreign-server (method)
6695   "Browse a foreign news server.
6696 If called interactively, this function will ask for a select method
6697  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6698 If not, METHOD should be a list where the first element is the method
6699 and the second element is the address."
6700   (interactive
6701    (list (let ((how (completing-read
6702                      "Which backend: "
6703                      (append gnus-valid-select-methods gnus-server-alist)
6704                      nil t (cons "nntp" 0) 'gnus-method-history)))
6705            ;; We either got a backend name or a virtual server name.
6706            ;; If the first, we also need an address.
6707            (if (assoc how gnus-valid-select-methods)
6708                (list (intern how)
6709                      ;; Suggested by mapjph@bath.ac.uk.
6710                      (completing-read
6711                       "Address: "
6712                       (mapcar (lambda (server) (list server))
6713                               gnus-secondary-servers)))
6714              ;; We got a server name, so we find the method.
6715              (gnus-server-to-method how)))))
6716   (gnus-browse-foreign-server method))
6717
6718 \f
6719 ;;;
6720 ;;; Gnus summary mode
6721 ;;;
6722
6723 (defvar gnus-summary-mode-map nil)
6724
6725 (put 'gnus-summary-mode 'mode-class 'special)
6726
6727 (unless gnus-summary-mode-map
6728   (setq gnus-summary-mode-map (make-keymap))
6729   (suppress-keymap gnus-summary-mode-map)
6730
6731   ;; Non-orthogonal keys
6732
6733   (gnus-define-keys gnus-summary-mode-map
6734     " " gnus-summary-next-page
6735     "\177" gnus-summary-prev-page
6736     [delete] gnus-summary-prev-page
6737     "\r" gnus-summary-scroll-up
6738     "n" gnus-summary-next-unread-article
6739     "p" gnus-summary-prev-unread-article
6740     "N" gnus-summary-next-article
6741     "P" gnus-summary-prev-article
6742     "\M-\C-n" gnus-summary-next-same-subject
6743     "\M-\C-p" gnus-summary-prev-same-subject
6744     "\M-n" gnus-summary-next-unread-subject
6745     "\M-p" gnus-summary-prev-unread-subject
6746     "." gnus-summary-first-unread-article
6747     "," gnus-summary-best-unread-article
6748     "\M-s" gnus-summary-search-article-forward
6749     "\M-r" gnus-summary-search-article-backward
6750     "<" gnus-summary-beginning-of-article
6751     ">" gnus-summary-end-of-article
6752     "j" gnus-summary-goto-article
6753     "^" gnus-summary-refer-parent-article
6754     "\M-^" gnus-summary-refer-article
6755     "u" gnus-summary-tick-article-forward
6756     "!" gnus-summary-tick-article-forward
6757     "U" gnus-summary-tick-article-backward
6758     "d" gnus-summary-mark-as-read-forward
6759     "D" gnus-summary-mark-as-read-backward
6760     "E" gnus-summary-mark-as-expirable
6761     "\M-u" gnus-summary-clear-mark-forward
6762     "\M-U" gnus-summary-clear-mark-backward
6763     "k" gnus-summary-kill-same-subject-and-select
6764     "\C-k" gnus-summary-kill-same-subject
6765     "\M-\C-k" gnus-summary-kill-thread
6766     "\M-\C-l" gnus-summary-lower-thread
6767     "e" gnus-summary-edit-article
6768     "#" gnus-summary-mark-as-processable
6769     "\M-#" gnus-summary-unmark-as-processable
6770     "\M-\C-t" gnus-summary-toggle-threads
6771     "\M-\C-s" gnus-summary-show-thread
6772     "\M-\C-h" gnus-summary-hide-thread
6773     "\M-\C-f" gnus-summary-next-thread
6774     "\M-\C-b" gnus-summary-prev-thread
6775     "\M-\C-u" gnus-summary-up-thread
6776     "\M-\C-d" gnus-summary-down-thread
6777     "&" gnus-summary-execute-command
6778     "c" gnus-summary-catchup-and-exit
6779     "\C-w" gnus-summary-mark-region-as-read
6780     "\C-t" gnus-summary-toggle-truncation
6781     "?" gnus-summary-mark-as-dormant
6782     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6783     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6784     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6785     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6786     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6787     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6788     "=" gnus-summary-expand-window
6789     "\C-x\C-s" gnus-summary-reselect-current-group
6790     "\M-g" gnus-summary-rescan-group
6791     "w" gnus-summary-stop-page-breaking
6792     "\C-c\C-r" gnus-summary-caesar-message
6793     "\M-t" gnus-summary-toggle-mime
6794     "f" gnus-summary-followup
6795     "F" gnus-summary-followup-with-original
6796     "C" gnus-summary-cancel-article
6797     "r" gnus-summary-reply
6798     "R" gnus-summary-reply-with-original
6799     "\C-c\C-f" gnus-summary-mail-forward
6800     "o" gnus-summary-save-article
6801     "\C-o" gnus-summary-save-article-mail
6802     "|" gnus-summary-pipe-output
6803     "\M-k" gnus-summary-edit-local-kill
6804     "\M-K" gnus-summary-edit-global-kill
6805     "V" gnus-version
6806     "\C-c\C-d" gnus-summary-describe-group
6807     "q" gnus-summary-exit
6808     "Q" gnus-summary-exit-no-update
6809     "\C-c\C-i" gnus-info-find-node
6810     gnus-mouse-2 gnus-mouse-pick-article
6811     "m" gnus-summary-mail-other-window
6812     "a" gnus-summary-post-news
6813     "x" gnus-summary-limit-to-unread
6814     "s" gnus-summary-isearch-article
6815     "t" gnus-article-hide-headers
6816     "g" gnus-summary-show-article
6817     "l" gnus-summary-goto-last-article
6818     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6819     "\C-d" gnus-summary-enter-digest-group
6820     "\C-c\C-b" gnus-bug
6821     "*" gnus-cache-enter-article
6822     "\M-*" gnus-cache-remove-article
6823     "\M-&" gnus-summary-universal-argument
6824     "\C-l" gnus-recenter
6825     "I" gnus-summary-increase-score
6826     "L" gnus-summary-lower-score
6827
6828     "V" gnus-summary-score-map
6829     "X" gnus-uu-extract-map
6830     "S" gnus-summary-send-map)
6831
6832   ;; Sort of orthogonal keymap
6833   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6834     "t" gnus-summary-tick-article-forward
6835     "!" gnus-summary-tick-article-forward
6836     "d" gnus-summary-mark-as-read-forward
6837     "r" gnus-summary-mark-as-read-forward
6838     "c" gnus-summary-clear-mark-forward
6839     " " gnus-summary-clear-mark-forward
6840     "e" gnus-summary-mark-as-expirable
6841     "x" gnus-summary-mark-as-expirable
6842     "?" gnus-summary-mark-as-dormant
6843     "b" gnus-summary-set-bookmark
6844     "B" gnus-summary-remove-bookmark
6845     "#" gnus-summary-mark-as-processable
6846     "\M-#" gnus-summary-unmark-as-processable
6847     "S" gnus-summary-limit-include-expunged
6848     "C" gnus-summary-catchup
6849     "H" gnus-summary-catchup-to-here
6850     "\C-c" gnus-summary-catchup-all
6851     "k" gnus-summary-kill-same-subject-and-select
6852     "K" gnus-summary-kill-same-subject
6853     "P" gnus-uu-mark-map)
6854
6855   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6856     "c" gnus-summary-clear-above
6857     "u" gnus-summary-tick-above
6858     "m" gnus-summary-mark-above
6859     "k" gnus-summary-kill-below)
6860
6861   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6862     "/" gnus-summary-limit-to-subject
6863     "n" gnus-summary-limit-to-articles
6864     "w" gnus-summary-pop-limit
6865     "s" gnus-summary-limit-to-subject
6866     "a" gnus-summary-limit-to-author
6867     "u" gnus-summary-limit-to-unread
6868     "m" gnus-summary-limit-to-marks
6869     "v" gnus-summary-limit-to-score
6870     "D" gnus-summary-limit-include-dormant
6871     "d" gnus-summary-limit-exclude-dormant
6872     ;;  "t" gnus-summary-limit-exclude-thread
6873     "E" gnus-summary-limit-include-expunged
6874     "c" gnus-summary-limit-exclude-childless-dormant
6875     "C" gnus-summary-limit-mark-excluded-as-read)
6876
6877   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6878     "n" gnus-summary-next-unread-article
6879     "p" gnus-summary-prev-unread-article
6880     "N" gnus-summary-next-article
6881     "P" gnus-summary-prev-article
6882     "\C-n" gnus-summary-next-same-subject
6883     "\C-p" gnus-summary-prev-same-subject
6884     "\M-n" gnus-summary-next-unread-subject
6885     "\M-p" gnus-summary-prev-unread-subject
6886     "f" gnus-summary-first-unread-article
6887     "b" gnus-summary-best-unread-article
6888     "j" gnus-summary-goto-article
6889     "g" gnus-summary-goto-subject
6890     "l" gnus-summary-goto-last-article
6891     "p" gnus-summary-pop-article)
6892
6893   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6894     "k" gnus-summary-kill-thread
6895     "l" gnus-summary-lower-thread
6896     "i" gnus-summary-raise-thread
6897     "T" gnus-summary-toggle-threads
6898     "t" gnus-summary-rethread-current
6899     "^" gnus-summary-reparent-thread
6900     "s" gnus-summary-show-thread
6901     "S" gnus-summary-show-all-threads
6902     "h" gnus-summary-hide-thread
6903     "H" gnus-summary-hide-all-threads
6904     "n" gnus-summary-next-thread
6905     "p" gnus-summary-prev-thread
6906     "u" gnus-summary-up-thread
6907     "o" gnus-summary-top-thread
6908     "d" gnus-summary-down-thread
6909     "#" gnus-uu-mark-thread
6910     "\M-#" gnus-uu-unmark-thread)
6911
6912   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6913     "c" gnus-summary-catchup-and-exit
6914     "C" gnus-summary-catchup-all-and-exit
6915     "E" gnus-summary-exit-no-update
6916     "Q" gnus-summary-exit
6917     "Z" gnus-summary-exit
6918     "n" gnus-summary-catchup-and-goto-next-group
6919     "R" gnus-summary-reselect-current-group
6920     "G" gnus-summary-rescan-group
6921     "N" gnus-summary-next-group
6922     "P" gnus-summary-prev-group)
6923
6924   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6925     " " gnus-summary-next-page
6926     "n" gnus-summary-next-page
6927     "\177" gnus-summary-prev-page
6928     [delete] gnus-summary-prev-page
6929     "p" gnus-summary-prev-page
6930     "\r" gnus-summary-scroll-up
6931     "<" gnus-summary-beginning-of-article
6932     ">" gnus-summary-end-of-article
6933     "b" gnus-summary-beginning-of-article
6934     "e" gnus-summary-end-of-article
6935     "^" gnus-summary-refer-parent-article
6936     "r" gnus-summary-refer-parent-article
6937     "R" gnus-summary-refer-references
6938     "g" gnus-summary-show-article
6939     "s" gnus-summary-isearch-article)
6940
6941   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6942     "b" gnus-article-add-buttons
6943     "B" gnus-article-add-buttons-to-head
6944     "o" gnus-article-treat-overstrike
6945     ;;  "w" gnus-article-word-wrap
6946     "w" gnus-article-fill-cited-article
6947     "c" gnus-article-remove-cr
6948     "L" gnus-article-remove-trailing-blank-lines
6949     "q" gnus-article-de-quoted-unreadable
6950     "f" gnus-article-display-x-face
6951     "l" gnus-summary-stop-page-breaking
6952     "r" gnus-summary-caesar-message
6953     "t" gnus-article-hide-headers
6954     "v" gnus-summary-verbose-headers
6955     "m" gnus-summary-toggle-mime)
6956
6957   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6958     "a" gnus-article-hide
6959     "h" gnus-article-hide-headers
6960     "b" gnus-article-hide-boring-headers
6961     "s" gnus-article-hide-signature
6962     "c" gnus-article-hide-citation
6963     "p" gnus-article-hide-pgp
6964     "\C-c" gnus-article-hide-citation-maybe)
6965
6966   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6967     "a" gnus-article-highlight
6968     "h" gnus-article-highlight-headers
6969     "c" gnus-article-highlight-citation
6970     "s" gnus-article-highlight-signature)
6971
6972   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6973     "z" gnus-article-date-ut
6974     "u" gnus-article-date-ut
6975     "l" gnus-article-date-local
6976     "e" gnus-article-date-lapsed
6977     "o" gnus-article-date-original)
6978
6979   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6980     "v" gnus-version
6981     "f" gnus-summary-fetch-faq
6982     "d" gnus-summary-describe-group
6983     "h" gnus-summary-describe-briefly
6984     "i" gnus-info-find-node)
6985
6986   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6987     "e" gnus-summary-expire-articles
6988     "\M-\C-e" gnus-summary-expire-articles-now
6989     "\177" gnus-summary-delete-article
6990     [delete] gnus-summary-delete-article
6991     "m" gnus-summary-move-article
6992     "r" gnus-summary-respool-article
6993     "w" gnus-summary-edit-article
6994     "c" gnus-summary-copy-article
6995     "B" gnus-summary-crosspost-article
6996     "q" gnus-summary-respool-query
6997     "i" gnus-summary-import-article)
6998
6999   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7000     "o" gnus-summary-save-article
7001     "m" gnus-summary-save-article-mail
7002     "r" gnus-summary-save-article-rmail
7003     "f" gnus-summary-save-article-file
7004     "b" gnus-summary-save-article-body-file
7005     "h" gnus-summary-save-article-folder
7006     "v" gnus-summary-save-article-vm
7007     "p" gnus-summary-pipe-output
7008     "s" gnus-soup-add-article)
7009   )
7010
7011 \f
7012
7013 (defun gnus-summary-mode (&optional group)
7014   "Major mode for reading articles.
7015
7016 All normal editing commands are switched off.
7017 \\<gnus-summary-mode-map>
7018 Each line in this buffer represents one article.  To read an
7019 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7020 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7021 respectively.
7022
7023 You can also post articles and send mail from this buffer.  To
7024 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7025 of an article, type `\\[gnus-summary-reply]'.
7026
7027 There are approx. one gazillion commands you can execute in this
7028 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7029
7030 The following commands are available:
7031
7032 \\{gnus-summary-mode-map}"
7033   (interactive)
7034   (when (and menu-bar-mode
7035              (gnus-visual-p 'summary-menu 'menu))
7036     (gnus-summary-make-menu-bar))
7037   (kill-all-local-variables)
7038   (let ((locals gnus-summary-local-variables))
7039     (while locals
7040       (if (consp (car locals))
7041           (progn
7042             (make-local-variable (caar locals))
7043             (set (caar locals) (eval (cdar locals))))
7044         (make-local-variable (car locals))
7045         (set (car locals) nil))
7046       (setq locals (cdr locals))))
7047   (gnus-make-thread-indent-array)
7048   (gnus-simplify-mode-line)
7049   (setq major-mode 'gnus-summary-mode)
7050   (setq mode-name "Summary")
7051   (make-local-variable 'minor-mode-alist)
7052   (use-local-map gnus-summary-mode-map)
7053   (buffer-disable-undo (current-buffer))
7054   (setq buffer-read-only t)             ;Disable modification
7055   (setq truncate-lines t)
7056   (setq selective-display t)
7057   (setq selective-display-ellipses t)   ;Display `...'
7058   (setq buffer-display-table gnus-summary-display-table)
7059   (setq gnus-newsgroup-name group)
7060   (make-local-variable 'gnus-summary-line-format)
7061   (make-local-variable 'gnus-summary-line-format-spec)
7062   (run-hooks 'gnus-summary-mode-hook))
7063
7064 (defun gnus-summary-make-display-table ()
7065   ;; Change the display table.  Odd characters have a tendency to mess
7066   ;; up nicely formatted displays - we make all possible glyphs
7067   ;; display only a single character.
7068
7069   ;; We start from the standard display table, if any.
7070   (setq gnus-summary-display-table
7071         (or (copy-sequence standard-display-table)
7072             (make-display-table)))
7073   ;; Nix out all the control chars...
7074   (let ((i 32))
7075     (while (>= (setq i (1- i)) 0)
7076       (aset gnus-summary-display-table i [??])))
7077   ;; ... but not newline and cr, of course. (cr is necessary for the
7078   ;; selective display).
7079   (aset gnus-summary-display-table ?\n nil)
7080   (aset gnus-summary-display-table ?\r nil)
7081   ;; We nix out any glyphs over 126 that are not set already.
7082   (let ((i 256))
7083     (while (>= (setq i (1- i)) 127)
7084       ;; Only modify if the entry is nil.
7085       (or (aref gnus-summary-display-table i)
7086           (aset gnus-summary-display-table i [??])))))
7087
7088 (defun gnus-summary-clear-local-variables ()
7089   (let ((locals gnus-summary-local-variables))
7090     (while locals
7091       (if (consp (car locals))
7092           (and (vectorp (caar locals))
7093                (set (caar locals) nil))
7094         (and (vectorp (car locals))
7095              (set (car locals) nil)))
7096       (setq locals (cdr locals)))))
7097
7098 ;; Summary data functions.
7099
7100 (defmacro gnus-data-number (data)
7101   `(car ,data))
7102
7103 (defmacro gnus-data-set-number (data number)
7104   `(setcar ,data ,number))
7105
7106 (defmacro gnus-data-mark (data)
7107   `(nth 1 ,data))
7108
7109 (defmacro gnus-data-set-mark (data mark)
7110   `(setcar (nthcdr 1 ,data) ,mark))
7111
7112 (defmacro gnus-data-pos (data)
7113   `(nth 2 ,data))
7114
7115 (defmacro gnus-data-set-pos (data pos)
7116   `(setcar (nthcdr 2 ,data) ,pos))
7117
7118 (defmacro gnus-data-header (data)
7119   `(nth 3 ,data))
7120
7121 (defmacro gnus-data-level (data)
7122   `(nth 4 ,data))
7123
7124 (defmacro gnus-data-unread-p (data)
7125   `(= (nth 1 ,data) gnus-unread-mark))
7126
7127 (defmacro gnus-data-pseudo-p (data)
7128   `(consp (nth 3 ,data)))
7129
7130 (defmacro gnus-data-find (number)
7131   `(assq ,number gnus-newsgroup-data))
7132
7133 (defmacro gnus-data-find-list (number &optional data)
7134   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7135      (memq (assq ,number bdata)
7136            bdata)))
7137
7138 (defmacro gnus-data-make (number mark pos header level)
7139   `(list ,number ,mark ,pos ,header ,level))
7140
7141 (defun gnus-data-enter (after-article number mark pos header level offset)
7142   (let ((data (gnus-data-find-list after-article)))
7143     (or data (error "No such article: %d" after-article))
7144     (setcdr data (cons (gnus-data-make number mark pos header level)
7145                        (cdr data)))
7146     (setq gnus-newsgroup-data-reverse nil)
7147     (gnus-data-update-list (cddr data) offset)))
7148
7149 (defun gnus-data-enter-list (after-article list &optional offset)
7150   (when list
7151     (let ((data (and after-article (gnus-data-find-list after-article)))
7152           (ilist list))
7153       (or data (not after-article) (error "No such article: %d" after-article))
7154       ;; Find the last element in the list to be spliced into the main
7155       ;; list.
7156       (while (cdr list)
7157         (setq list (cdr list)))
7158       (if (not data)
7159           (progn
7160             (setcdr list gnus-newsgroup-data)
7161             (setq gnus-newsgroup-data ilist)
7162             (and offset (gnus-data-update-list (cdr list) offset)))
7163         (setcdr list (cdr data))
7164         (setcdr data ilist)
7165         (and offset (gnus-data-update-list (cdr data) offset)))
7166       (setq gnus-newsgroup-data-reverse nil))))
7167
7168 (defun gnus-data-remove (article &optional offset)
7169   (let ((data gnus-newsgroup-data))
7170     (if (= (gnus-data-number (car data)) article)
7171         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7172               gnus-newsgroup-data-reverse nil)
7173       (while (cdr data)
7174         (and (= (gnus-data-number (cadr data)) article)
7175              (progn
7176                (setcdr data (cddr data))
7177                (and offset (gnus-data-update-list (cdr data) offset))
7178                (setq data nil
7179                      gnus-newsgroup-data-reverse nil)))
7180         (setq data (cdr data))))))
7181
7182 (defmacro gnus-data-list (backward)
7183   `(if ,backward
7184        (or gnus-newsgroup-data-reverse
7185            (setq gnus-newsgroup-data-reverse
7186                  (reverse gnus-newsgroup-data)))
7187      gnus-newsgroup-data))
7188
7189 (defun gnus-data-update-list (data offset)
7190   "Add OFFSET to the POS of all data entries in DATA."
7191   (while data
7192     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7193     (setq data (cdr data))))
7194
7195 (defun gnus-data-compute-positions ()
7196   "Compute the positions of all articles."
7197   (let ((data gnus-newsgroup-data)
7198         pos)
7199     (while data
7200       (when (setq pos (text-property-any
7201                        (point-min) (point-max)
7202                        'gnus-number (gnus-data-number (car data))))
7203         (gnus-data-set-pos (car data) (+ pos 3)))
7204       (setq data (cdr data)))))
7205
7206 (defun gnus-summary-article-pseudo-p (article)
7207   "Say whether this article is a pseudo article or not."
7208   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7209
7210 (defun gnus-article-parent-p (number)
7211   "Say whether this article is a parent or not."
7212   (let ((data (gnus-data-find-list number)))
7213     (and (cdr data)                     ; There has to be an article after...
7214          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7215             (gnus-data-level (nth 1 data))))))
7216
7217 (defun gnus-article-children (number)
7218   "Return a list of all children to NUMBER."
7219   (let* ((data (gnus-data-find-list number))
7220          (level (gnus-data-level (car data)))
7221          children)
7222     (setq data (cdr data))
7223     (while (and data            
7224                 (= (gnus-data-level (car data)) (1+ level)))
7225       (push (gnus-data-number (car data)) children)
7226       (setq data (cdr data)))
7227     children))
7228
7229 (defmacro gnus-summary-skip-intangible ()
7230   "If the current article is intangible, then jump to a different article."
7231   '(let ((to (get-text-property (point) 'gnus-intangible)))
7232     (and to (gnus-summary-goto-subject to))))
7233
7234 (defmacro gnus-summary-article-intangible-p ()
7235   "Say whether this article is intangible or not."
7236   '(get-text-property (point) 'gnus-intangible))
7237
7238 ;; Some summary mode macros.
7239
7240 (defmacro gnus-summary-article-number ()
7241   "The article number of the article on the current line.
7242 If there isn's an article number here, then we return the current
7243 article number."
7244   '(progn
7245      (gnus-summary-skip-intangible)
7246      (or (get-text-property (point) 'gnus-number)
7247          (gnus-summary-last-subject))))
7248
7249 (defmacro gnus-summary-article-header (&optional number)
7250   `(gnus-data-header (gnus-data-find
7251                       ,(or number '(gnus-summary-article-number)))))
7252
7253 (defmacro gnus-summary-thread-level (&optional number)
7254   `(if (and (eq gnus-summary-make-false-root 'dummy)
7255             (get-text-property (point) 'gnus-intangible))
7256        0
7257      (gnus-data-level (gnus-data-find
7258                        ,(or number '(gnus-summary-article-number))))))
7259
7260 (defmacro gnus-summary-article-mark (&optional number)
7261   `(gnus-data-mark (gnus-data-find
7262                     ,(or number '(gnus-summary-article-number)))))
7263
7264 (defmacro gnus-summary-article-pos (&optional number)
7265   `(gnus-data-pos (gnus-data-find
7266                    ,(or number '(gnus-summary-article-number)))))
7267
7268 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7269 (defmacro gnus-summary-article-subject (&optional number)
7270   "Return current subject string or nil if nothing."
7271   `(let ((headers
7272           ,(if number
7273                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7274              '(gnus-data-header (assq (gnus-summary-article-number)
7275                                       gnus-newsgroup-data)))))
7276      (and headers
7277           (vectorp headers)
7278           (mail-header-subject headers))))
7279
7280 (defmacro gnus-summary-article-score (&optional number)
7281   "Return current article score."
7282   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7283                   gnus-newsgroup-scored))
7284        gnus-summary-default-score 0))
7285
7286 (defun gnus-summary-article-children (&optional number)
7287   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7288          (level (gnus-data-level (car data)))
7289          l children)
7290     (while (and (setq data (cdr data))
7291                 (> (setq l (gnus-data-level (car data))) level))
7292       (and (= (1+ level) l)
7293            (setq children (cons (gnus-data-number (car data))
7294                                 children))))
7295     (nreverse children)))
7296
7297 (defun gnus-summary-article-parent (&optional number)
7298   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7299                                     (gnus-data-list t)))
7300          (level (gnus-data-level (car data))))
7301     (if (zerop level)
7302         () ; This is a root.
7303       ;; We search until we find an article with a level less than
7304       ;; this one.  That function has to be the parent.
7305       (while (and (setq data (cdr data))
7306                   (not (< (gnus-data-level (car data)) level))))
7307       (and data (gnus-data-number (car data))))))
7308
7309 (defun gnus-unread-mark-p (mark)
7310   "Say whether MARK is the unread mark."
7311   (= mark gnus-unread-mark))
7312
7313 (defun gnus-read-mark-p (mark)
7314   "Say whether MARK is one of the marks that mark as read.
7315 This is all marks except unread, ticked, dormant, and expirable."
7316   (not (or (= mark gnus-unread-mark)
7317            (= mark gnus-ticked-mark)
7318            (= mark gnus-dormant-mark)
7319            (= mark gnus-expirable-mark))))
7320
7321 ;; Various summary mode internalish functions.
7322
7323 (defun gnus-mouse-pick-article (e)
7324   (interactive "e")
7325   (mouse-set-point e)
7326   (gnus-summary-next-page nil t))
7327
7328 (defun gnus-summary-setup-buffer (group)
7329   "Initialize summary buffer."
7330   (let ((buffer (concat "*Summary " group "*")))
7331     (if (get-buffer buffer)
7332         (progn
7333           (set-buffer buffer)
7334           (setq gnus-summary-buffer (current-buffer))
7335           (not gnus-newsgroup-prepared))
7336       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7337       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7338       (gnus-add-current-to-buffer-list)
7339       (gnus-summary-mode group)
7340       (when gnus-carpal
7341         (gnus-carpal-setup-buffer 'summary))
7342       (unless gnus-single-article-buffer
7343         (make-local-variable 'gnus-article-buffer)
7344         (make-local-variable 'gnus-article-current)
7345         (make-local-variable 'gnus-original-article-buffer))
7346       (setq gnus-newsgroup-name group)
7347       t)))
7348
7349 (defun gnus-set-global-variables ()
7350   ;; Set the global equivalents of the summary buffer-local variables
7351   ;; to the latest values they had.  These reflect the summary buffer
7352   ;; that was in action when the last article was fetched.
7353   (when (eq major-mode 'gnus-summary-mode)
7354     (setq gnus-summary-buffer (current-buffer))
7355     (let ((name gnus-newsgroup-name)
7356           (marked gnus-newsgroup-marked)
7357           (unread gnus-newsgroup-unreads)
7358           (headers gnus-current-headers)
7359           (data gnus-newsgroup-data)
7360           (summary gnus-summary-buffer)
7361           (article-buffer gnus-article-buffer)
7362           (original gnus-original-article-buffer)
7363           (gac gnus-article-current)
7364           (score-file gnus-current-score-file))
7365       (save-excursion
7366         (set-buffer gnus-group-buffer)
7367         (setq gnus-newsgroup-name name)
7368         (setq gnus-newsgroup-marked marked)
7369         (setq gnus-newsgroup-unreads unread)
7370         (setq gnus-current-headers headers)
7371         (setq gnus-newsgroup-data data)
7372         (setq gnus-article-current gac)
7373         (setq gnus-summary-buffer summary)
7374         (setq gnus-article-buffer article-buffer)
7375         (setq gnus-original-article-buffer original)
7376         (setq gnus-current-score-file score-file)))))
7377
7378 (defun gnus-summary-last-article-p (&optional article)
7379   "Return whether ARTICLE is the last article in the buffer."
7380   (if (not (setq article (or article (gnus-summary-article-number))))
7381       t ; All non-existant numbers are the last article. :-)
7382     (not (cdr (gnus-data-find-list article)))))
7383
7384 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7385   "Insert a dummy root in the summary buffer."
7386   (beginning-of-line)
7387   (add-text-properties
7388    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7389    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7390
7391 (defvar gnus-thread-indent-array nil)
7392 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7393 (defun gnus-make-thread-indent-array ()
7394   (let ((n 200))
7395     (unless (and gnus-thread-indent-array
7396                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7397       (setq gnus-thread-indent-array (make-vector 201 "")
7398             gnus-thread-indent-array-level gnus-thread-indent-level)
7399       (while (>= n 0)
7400         (aset gnus-thread-indent-array n
7401               (make-string (* n gnus-thread-indent-level) ? ))
7402         (setq n (1- n))))))
7403
7404 (defun gnus-summary-insert-line
7405   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7406                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7407                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7408   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7409          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7410          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7411          (gnus-tmp-score-char
7412           (if (or (null gnus-summary-default-score)
7413                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7414                       gnus-summary-zcore-fuzz)) ? 
7415             (if (< gnus-tmp-score gnus-summary-default-score)
7416                 gnus-score-below-mark gnus-score-over-mark)))
7417          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7418                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7419                                   gnus-cached-mark)
7420                                  (gnus-tmp-replied gnus-replied-mark)
7421                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7422                                   gnus-saved-mark)
7423                                  (t gnus-unread-mark)))
7424          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7425          (gnus-tmp-name
7426           (cond
7427            ((string-match "(.+)" gnus-tmp-from)
7428             (substring gnus-tmp-from
7429                        (1+ (match-beginning 0)) (1- (match-end 0))))
7430            ((string-match "<[^>]+> *$" gnus-tmp-from)
7431             (let ((beg (match-beginning 0)))
7432               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7433                        (substring gnus-tmp-from (1+ (match-beginning 0))
7434                                   (1- (match-end 0))))
7435                   (substring gnus-tmp-from 0 beg))))
7436            (t gnus-tmp-from)))
7437          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7438          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7439          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7440          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7441          (buffer-read-only nil))
7442     (when (string= gnus-tmp-name "")
7443       (setq gnus-tmp-name gnus-tmp-from))
7444     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7445     (put-text-property
7446      (point)
7447      (progn (eval gnus-summary-line-format-spec) (point))
7448      'gnus-number gnus-tmp-number)
7449     (when (gnus-visual-p 'summary-highlight 'highlight)
7450       (forward-line -1)
7451       (run-hooks 'gnus-summary-update-hook)
7452       (forward-line 1))))
7453
7454 (defun gnus-summary-update-line (&optional dont-update)
7455   ;; Update summary line after change.
7456   (when (and gnus-summary-default-score
7457              (not gnus-summary-inhibit-highlight))
7458     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7459            (article (gnus-summary-article-number))
7460            (score (gnus-summary-article-score article)))
7461       (unless dont-update
7462         (if (and gnus-summary-mark-below
7463                  (< (gnus-summary-article-score)
7464                     gnus-summary-mark-below))
7465             ;; This article has a low score, so we mark it as read.
7466             (when (memq article gnus-newsgroup-unreads)
7467               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7468           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7469             ;; This article was previously marked as read on account
7470             ;; of a low score, but now it has risen, so we mark it as
7471             ;; unread.
7472             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7473         (gnus-summary-update-mark
7474          (if (or (null gnus-summary-default-score)
7475                  (<= (abs (- score gnus-summary-default-score))
7476                      gnus-summary-zcore-fuzz)) ? 
7477            (if (< score gnus-summary-default-score)
7478                gnus-score-below-mark gnus-score-over-mark)) 'score))
7479       ;; Do visual highlighting.
7480       (when (gnus-visual-p 'summary-highlight 'highlight)
7481         (run-hooks 'gnus-summary-update-hook)))))
7482
7483 (defvar gnus-tmp-new-adopts nil)
7484
7485 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7486   ;; Sum up all elements (and sub-elements) in a list.
7487   (let* ((number
7488           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7489           (cond
7490            ((and (consp thread) (cdr thread))
7491             (apply
7492              '+ 1 (mapcar
7493                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7494            ((null thread)
7495             1)
7496            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7497             1)
7498            (t 1))))
7499     (when (and level (zerop level) gnus-tmp-new-adopts)
7500       (incf number
7501             (apply '+ (mapcar
7502                        'gnus-summary-number-of-articles-in-thread
7503                        gnus-tmp-new-adopts))))
7504     (if char
7505         (if (> number 1) gnus-not-empty-thread-mark
7506           gnus-empty-thread-mark)
7507       number)))
7508
7509 (defun gnus-summary-set-local-parameters (group)
7510  "Go through the local params of GROUP and set all variable specs in that list."
7511   (let ((params (gnus-info-params (gnus-get-info group)))
7512         elem)
7513     (while params
7514       (setq elem (car params)
7515             params (cdr params))
7516       (and (consp elem)                 ; Has to be a cons.
7517            (consp (cdr elem))           ; The cdr has to be a list.
7518            (symbolp (car elem))         ; Has to be a symbol in there.
7519            (not (memq (car elem) 
7520                       '(quit-config to-address to-list to-group)))
7521            (progn                       ; So we set it.
7522              (make-local-variable (car elem))
7523              (set (car elem) (eval (nth 1 elem))))))))
7524
7525 (defun gnus-summary-read-group (group &optional show-all no-article
7526                                       kill-buffer no-display)
7527   "Start reading news in newsgroup GROUP.
7528 If SHOW-ALL is non-nil, already read articles are also listed.
7529 If NO-ARTICLE is non-nil, no article is selected initially.
7530 If NO-DISPLAY, don't generate a summary buffer."
7531   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7532   (let* ((new-group (gnus-summary-setup-buffer group))
7533          (quit-config (gnus-group-quit-config group))
7534          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7535     (cond
7536      ;; This summary buffer exists already, so we just select it.
7537      ((not new-group)
7538       (gnus-set-global-variables)
7539       (when kill-buffer
7540         (gnus-kill-or-deaden-summary kill-buffer))
7541       (gnus-configure-windows 'summary 'force)
7542       (gnus-set-mode-line 'summary)
7543       (gnus-summary-position-point)
7544       (message "")
7545       t)
7546      ;; We couldn't select this group.
7547      ((null did-select)
7548       (when (and (eq major-mode 'gnus-summary-mode)
7549                  (not (equal (current-buffer) kill-buffer)))
7550         (kill-buffer (current-buffer))
7551         (if (not quit-config)
7552             (progn
7553               (set-buffer gnus-group-buffer)
7554               (gnus-group-jump-to-group group)
7555               (gnus-group-next-unread-group 1))
7556           (if (not (buffer-name (car quit-config)))
7557               (gnus-configure-windows 'group 'force)
7558             (set-buffer (car quit-config))
7559             (and (eq major-mode 'gnus-summary-mode)
7560                  (gnus-set-global-variables))
7561             (gnus-configure-windows (cdr quit-config)))))
7562       (gnus-message 3 "Can't select group")
7563       nil)
7564      ;; The user did a `C-g' while prompting for number of articles,
7565      ;; so we exit this group.
7566      ((eq did-select 'quit)
7567       (and (eq major-mode 'gnus-summary-mode)
7568            (not (equal (current-buffer) kill-buffer))
7569            (kill-buffer (current-buffer)))
7570       (when kill-buffer
7571         (gnus-kill-or-deaden-summary kill-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             (gnus-configure-windows 'group 'force))
7578         (if (not (buffer-name (car quit-config)))
7579             (gnus-configure-windows 'group 'force)
7580           (set-buffer (car quit-config))
7581           (and (eq major-mode 'gnus-summary-mode)
7582                (gnus-set-global-variables))
7583           (gnus-configure-windows (cdr quit-config))))
7584       ;; Finally signal the quit.
7585       (signal 'quit nil))
7586      ;; The group was successfully selected.
7587      (t
7588       (gnus-set-global-variables)
7589       ;; Save the active value in effect when the group was entered.
7590       (setq gnus-newsgroup-active
7591             (gnus-copy-sequence
7592              (gnus-active gnus-newsgroup-name)))
7593       ;; You can change the summary buffer in some way with this hook.
7594       (run-hooks 'gnus-select-group-hook)
7595       ;; Set any local variables in the group parameters.
7596       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7597       (gnus-update-format-specifications)
7598       ;; Do score processing.
7599       (when gnus-use-scoring
7600         (gnus-possibly-score-headers))
7601       ;; Check whether to fill in the gaps in the threads.
7602       (when gnus-build-sparse-threads
7603         (gnus-build-sparse-threads))
7604       ;; Find the initial limit.
7605       (if gnus-show-threads
7606           (if show-all
7607               (let ((gnus-newsgroup-dormant nil))
7608                 (gnus-summary-initial-limit show-all))
7609             (gnus-summary-initial-limit show-all))
7610         (setq gnus-newsgroup-limit 
7611               (mapcar 
7612                (lambda (header) (mail-header-number header))
7613                gnus-newsgroup-headers)))
7614       ;; Generate the summary buffer.
7615       (unless no-display
7616         (gnus-summary-prepare))
7617       (when gnus-use-trees
7618         (gnus-tree-open group)
7619         (setq gnus-summary-highlight-line-function
7620               'gnus-tree-highlight-article))
7621       ;; If the summary buffer is empty, but there are some low-scored
7622       ;; articles or some excluded dormants, we include these in the
7623       ;; buffer.
7624       (when (and (zerop (buffer-size))
7625                  (not no-display))
7626         (cond (gnus-newsgroup-dormant
7627                (gnus-summary-limit-include-dormant))
7628               ((and gnus-newsgroup-scored show-all)
7629                (gnus-summary-limit-include-expunged))))
7630       ;; Function `gnus-apply-kill-file' must be called in this hook.
7631       (run-hooks 'gnus-apply-kill-hook)
7632       (if (and (zerop (buffer-size))
7633                (not no-display))
7634           (progn
7635             ;; This newsgroup is empty.
7636             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7637             (gnus-message 6 "No unread news")
7638             (when kill-buffer
7639               (gnus-kill-or-deaden-summary kill-buffer))
7640             ;; Return nil from this function.
7641             nil)
7642         ;; Hide conversation thread subtrees.  We cannot do this in
7643         ;; gnus-summary-prepare-hook since kill processing may not
7644         ;; work with hidden articles.
7645         (and gnus-show-threads
7646              gnus-thread-hide-subtree
7647              (gnus-summary-hide-all-threads))
7648         ;; Show first unread article if requested.
7649         (if (and (not no-article)
7650                  (not no-display)
7651                  gnus-newsgroup-unreads
7652                  gnus-auto-select-first)
7653             (unless (if (eq gnus-auto-select-first 'best)
7654                         (gnus-summary-best-unread-article)
7655                       (gnus-summary-first-unread-article))
7656               (gnus-configure-windows 'summary))
7657           ;; Don't select any articles, just move point to the first
7658           ;; article in the group.
7659           (goto-char (point-min))
7660           (gnus-summary-position-point)
7661           (gnus-set-mode-line 'summary)
7662           (gnus-configure-windows 'summary 'force))
7663         ;; If we are in async mode, we send some info to the backend.
7664         (when gnus-newsgroup-async
7665           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7666         (when kill-buffer
7667           (gnus-kill-or-deaden-summary kill-buffer))
7668         (when (get-buffer-window gnus-group-buffer t)
7669           ;; Gotta use windows, because recenter does wierd stuff if
7670           ;; the current buffer ain't the displayed window.
7671           (let ((owin (selected-window)))
7672             (select-window (get-buffer-window gnus-group-buffer t))
7673             (when (gnus-group-goto-group group)
7674               (recenter))
7675             (select-window owin))))
7676       ;; Mark this buffer as "prepared".
7677       (setq gnus-newsgroup-prepared t)
7678       t))))
7679
7680 (defun gnus-summary-prepare ()
7681   "Generate the summary buffer."
7682   (let ((buffer-read-only nil))
7683     (erase-buffer)
7684     (setq gnus-newsgroup-data nil
7685           gnus-newsgroup-data-reverse nil)
7686     (run-hooks 'gnus-summary-generate-hook)
7687     ;; Generate the buffer, either with threads or without.
7688     (when gnus-newsgroup-headers
7689       (gnus-summary-prepare-threads
7690        (if gnus-show-threads
7691            (gnus-sort-gathered-threads
7692             (funcall gnus-summary-thread-gathering-function
7693                      (gnus-sort-threads
7694                       (gnus-cut-threads (gnus-make-threads)))))
7695          ;; Unthreaded display.
7696          (gnus-sort-articles gnus-newsgroup-headers))))
7697     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7698     ;; Call hooks for modifying summary buffer.
7699     (goto-char (point-min))
7700     (run-hooks 'gnus-summary-prepare-hook)))
7701
7702 (defun gnus-gather-threads-by-subject (threads)
7703   "Gather threads by looking at Subject headers."
7704   (if (not gnus-summary-make-false-root)
7705       threads
7706     (let ((hashtb (gnus-make-hashtable 1023))
7707           (prev threads)
7708           (result threads)
7709           subject hthread whole-subject)
7710       (while threads
7711         (setq whole-subject (mail-header-subject (caar threads)))
7712         (setq subject
7713               (cond
7714                ;; Truncate the subject.
7715                ((numberp gnus-summary-gather-subject-limit)
7716                 (setq subject (gnus-simplify-subject-re whole-subject))
7717                 (if (> (length subject) gnus-summary-gather-subject-limit)
7718                     (substring subject 0 gnus-summary-gather-subject-limit)
7719                   subject))
7720                ;; Fuzzily simplify it.
7721                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7722                 (gnus-simplify-subject-fuzzy whole-subject))
7723                ;; Just remove the leading "Re:".
7724                (t
7725                 (gnus-simplify-subject-re whole-subject))))
7726
7727         (if (and gnus-summary-gather-exclude-subject
7728                  (string-match gnus-summary-gather-exclude-subject
7729                                subject))
7730             ()          ; We don't want to do anything with this article.
7731           ;; We simplify the subject before looking it up in the
7732           ;; hash table.
7733
7734           (if (setq hthread (gnus-gethash subject hashtb))
7735               (progn
7736                 ;; We enter a dummy root into the thread, if we
7737                 ;; haven't done that already.
7738                 (unless (stringp (caar hthread))
7739                   (setcar hthread (list whole-subject (car hthread))))
7740                 ;; We add this new gathered thread to this gathered
7741                 ;; thread.
7742                 (setcdr (car hthread)
7743                         (nconc (cdar hthread) (list (car threads))))
7744                 ;; Remove it from the list of threads.
7745                 (setcdr prev (cdr threads))
7746                 (setq threads prev))
7747             ;; Enter this thread into the hash table.
7748             (gnus-sethash subject threads hashtb)))
7749         (setq prev threads)
7750         (setq threads (cdr threads)))
7751       result)))
7752
7753 (defun gnus-gather-threads-by-references (threads)
7754   "Gather threads by looking at References headers."
7755   (let ((idhashtb (gnus-make-hashtable 1023))
7756         (thhashtb (gnus-make-hashtable 1023))
7757         (prev threads)
7758         (result threads)
7759         ids references id gthread gid entered)
7760     (while threads
7761       (when (setq references (mail-header-references (caar threads)))
7762         (setq id (mail-header-id (caar threads)))
7763         (setq ids (gnus-split-references references))
7764         (setq entered nil)
7765         (while ids
7766           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7767               (progn
7768                 (gnus-sethash (car ids) id idhashtb)
7769                 (gnus-sethash id threads thhashtb))
7770             (setq gthread (gnus-gethash gid thhashtb))
7771             (unless entered
7772               ;; We enter a dummy root into the thread, if we
7773               ;; haven't done that already.
7774               (unless (stringp (caar gthread))
7775                 (setcar gthread (list (mail-header-subject (caar gthread))
7776                                       (car gthread))))
7777               ;; We add this new gathered thread to this gathered
7778               ;; thread.
7779               (setcdr (car gthread)
7780                       (nconc (cdar gthread) (list (car threads)))))
7781             ;; Add it into the thread hash table.
7782             (gnus-sethash id gthread thhashtb)
7783             (setq entered t)
7784             ;; Remove it from the list of threads.
7785             (setcdr prev (cdr threads))
7786             (setq threads prev))
7787           (setq ids (cdr ids))))
7788       (setq prev threads)
7789       (setq threads (cdr threads)))
7790     result))
7791
7792 (defun gnus-sort-gathered-threads (threads)
7793   "Sort subtreads inside each gathered thread by article number."
7794   (let ((result threads))
7795     (while threads
7796       (when (stringp (caar threads))
7797         (setcdr (car threads)
7798                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7799       (setq threads (cdr threads)))
7800     result))
7801
7802 (defun gnus-make-threads ()
7803   "Go through the dependency hashtb and find the roots.  Return all threads."
7804   (let (threads)
7805     (mapatoms
7806      (lambda (refs)
7807        (unless (car (symbol-value refs))
7808          ;; These threads do not refer back to any other articles,
7809          ;; so they're roots.
7810          (setq threads (append (cdr (symbol-value refs)) threads))))
7811      gnus-newsgroup-dependencies)
7812     threads))
7813
7814 (defun gnus-build-sparse-threads ()
7815   (let ((headers gnus-newsgroup-headers)
7816         (deps gnus-newsgroup-dependencies)
7817         header references generation relations 
7818         cthread subject child end pthread relation)
7819     ;; First we create an alist of generations/relations, where 
7820     ;; generations is how much we trust the ralation, and the relation
7821     ;; is parent/child.
7822     (gnus-message 7 "Making sparse threads...")
7823     (save-excursion
7824       (nnheader-set-temp-buffer " *gnus sparse threads*")
7825       (while (setq header (pop headers))
7826         (when (and (setq references (mail-header-references header))
7827                    (not (string= references "")))
7828           (insert references)
7829           (setq child (mail-header-id header)
7830                 subject (mail-header-subject header))
7831           (setq generation 0)
7832           (while (search-backward ">" nil t)
7833             (setq end (1+ (point)))
7834             (when (search-backward "<" nil t)
7835               (push (list (incf generation) 
7836                           child (setq child (buffer-substring (point) end))
7837                           subject)
7838                     relations)))
7839           (push (list (1+ generation) child nil subject) relations)
7840           (erase-buffer)))
7841       (kill-buffer (current-buffer)))
7842     ;; Sort over trustworthiness.
7843     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7844     (while (setq relation (pop relations))
7845       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7846                 (unless (car (symbol-value cthread))
7847                   ;; Make this article the parent of these threads.
7848                   (setcar (symbol-value cthread)
7849                           (vector gnus-reffed-article-number 
7850                                   (cadddr relation) 
7851                                   "" ""
7852                                   (cadr relation) 
7853                                   (or (caddr relation) "") 0 0 "")))
7854               (set cthread (list (vector gnus-reffed-article-number
7855                                          (cadddr relation) 
7856                                          "" "" (cadr relation) 
7857                                          (or (caddr relation) "") 0 0 ""))))
7858         (push gnus-reffed-article-number gnus-newsgroup-limit)
7859         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7860         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7861               gnus-newsgroup-reads)
7862         (decf gnus-reffed-article-number)
7863         ;; Make this new thread the child of its parent.
7864         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7865             (setcdr (symbol-value pthread)
7866                     (nconc (cdr (symbol-value pthread))
7867                            (list (symbol-value cthread))))
7868           (set pthread (list nil (symbol-value cthread))))))
7869     (gnus-message 7 "Making sparse threads...done")))
7870
7871 (defun gnus-build-old-threads ()
7872   ;; Look at all the articles that refer back to old articles, and
7873   ;; fetch the headers for the articles that aren't there.  This will
7874   ;; build complete threads - if the roots haven't been expired by the
7875   ;; server, that is.
7876   (let (id heads)
7877     (mapatoms
7878      (lambda (refs)
7879        (when (not (car (symbol-value refs)))
7880          (setq heads (cdr (symbol-value refs)))
7881          (while heads
7882            (if (memq (mail-header-number (caar heads))
7883                      gnus-newsgroup-dormant)
7884                (setq heads (cdr heads))
7885              (setq id (symbol-name refs))
7886              (while (and (setq id (gnus-build-get-header id))
7887                          (not (car (gnus-gethash
7888                                     id gnus-newsgroup-dependencies)))))
7889              (setq heads nil)))))
7890      gnus-newsgroup-dependencies)))
7891
7892 (defun gnus-build-get-header (id)
7893   ;; Look through the buffer of NOV lines and find the header to
7894   ;; ID.  Enter this line into the dependencies hash table, and return
7895   ;; the id of the parent article (if any).
7896   (let ((deps gnus-newsgroup-dependencies)
7897         found header)
7898     (prog1
7899         (save-excursion
7900           (set-buffer nntp-server-buffer)
7901           (goto-char (point-min))
7902           (while (and (not found) (search-forward id nil t))
7903             (beginning-of-line)
7904             (setq found (looking-at
7905                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7906                                  (regexp-quote id))))
7907             (or found (beginning-of-line 2)))
7908           (when found
7909             (beginning-of-line)
7910             (and
7911              (setq header (gnus-nov-parse-line
7912                            (read (current-buffer)) deps))
7913              (gnus-parent-id (mail-header-references header)))))
7914       (when header
7915         (let ((number (mail-header-number header)))
7916           (push number gnus-newsgroup-limit)
7917           (push header gnus-newsgroup-headers)
7918           (if (memq number gnus-newsgroup-unselected)
7919               (progn
7920                 (push number gnus-newsgroup-unreads)
7921                 (setq gnus-newsgroup-unselected
7922                       (delq number gnus-newsgroup-unselected)))
7923             (push number gnus-newsgroup-ancient)))))))
7924
7925 (defun gnus-summary-update-article (article &optional header)
7926   "Update ARTICLE in the summary buffer."
7927   (set-buffer gnus-summary-buffer)
7928   (let* ((header (or header (gnus-summary-article-header article)))
7929          (id (mail-header-id header))
7930          (data (gnus-data-find article))
7931          (thread (gnus-id-to-thread id))
7932          (parent
7933           (gnus-id-to-thread (or (gnus-parent-id 
7934                                   (mail-header-references header))
7935                                  "tull")))
7936          (buffer-read-only nil)
7937          (old (car thread))
7938          (number (mail-header-number header))
7939          pos)
7940     (when thread
7941       (setcar thread nil)
7942       (when parent
7943         (delq thread parent))
7944       (if (gnus-summary-insert-subject id header)
7945           ;; Set the (possibly) new article number in the data structure.
7946           (gnus-data-set-number data (gnus-id-to-article id))
7947         (setcar thread old)
7948         nil))))
7949
7950 (defun gnus-rebuild-thread (id)
7951   "Rebuild the thread containing ID."
7952   (let ((buffer-read-only nil)
7953         current thread data)
7954     (if (not gnus-show-threads)
7955         (setq thread (list (car (gnus-id-to-thread id))))
7956       ;; Get the thread this article is part of.
7957       (setq thread (gnus-remove-thread id)))
7958     (setq current (save-excursion
7959                     (and (zerop (forward-line -1))
7960                          (gnus-summary-article-number))))
7961     ;; If this is a gathered thread, we have to go some re-gathering.
7962     (when (stringp (car thread))
7963       (let ((subject (car thread))
7964             roots thr)
7965         (setq thread (cdr thread))
7966         (while thread
7967           (unless (memq (setq thr (gnus-id-to-thread
7968                                       (gnus-root-id
7969                                        (mail-header-id (caar thread)))))
7970                         roots)
7971             (push thr roots))
7972           (setq thread (cdr thread)))
7973         ;; We now have all (unique) roots.
7974         (if (= (length roots) 1)
7975             ;; All the loose roots are now one solid root.
7976             (setq thread (car roots))
7977           (setq thread (cons subject (gnus-sort-threads roots))))))
7978     (let (threads)
7979       ;; We then insert this thread into the summary buffer.
7980       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7981         (gnus-summary-prepare-threads (list thread))
7982         (setq data (nreverse gnus-newsgroup-data))
7983         (setq threads gnus-newsgroup-threads))
7984       ;; We splice the new data into the data structure.
7985       (gnus-data-enter-list current data)
7986       (gnus-data-compute-positions)
7987       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7988
7989 (defun gnus-id-to-thread (id)
7990   "Return the (sub-)thread where ID appears."
7991   (gnus-gethash id gnus-newsgroup-dependencies))
7992
7993 (defun gnus-id-to-article (id)
7994   "Return the article number of ID."
7995   (let ((thread (gnus-id-to-thread id)))
7996     (when thread
7997       (mail-header-number (car thread)))))
7998
7999 (defun gnus-id-to-header (id)
8000   "Return the article headers of ID."
8001   (car (gnus-id-to-thread id)))
8002
8003 (defun gnus-article-displayed-root-p (article)
8004   "Say whether ARTICLE is a root(ish) article."
8005   (let ((level (gnus-summary-thread-level article))
8006         particle)
8007     (cond 
8008      ((null level) nil)
8009      ((zerop level) t)
8010      ((and (= 1 level)
8011            (null (setq particle (gnus-id-to-article
8012                                  (gnus-parent-id 
8013                                   (mail-header-references 
8014                                    (gnus-summary-article-header article))))))
8015            (null (gnus-summary-thread-level particle)))))))
8016
8017 (defun gnus-root-id (id)
8018   "Return the id of the root of the thread where ID appears."
8019   (let (last-id prev)
8020     (while (and id (setq prev (car (gnus-gethash 
8021                                     id gnus-newsgroup-dependencies))))
8022       (setq last-id id
8023             id (gnus-parent-id (mail-header-references prev))))
8024     last-id))
8025
8026 (defun gnus-remove-thread (id &optional dont-remove)
8027   "Remove the thread that has ID in it."
8028   (let ((dep gnus-newsgroup-dependencies)
8029         headers thread last-id)
8030     ;; First go up in this thread until we find the root.
8031     (setq last-id (gnus-root-id id))
8032     (setq headers (list (car (gnus-id-to-thread last-id))
8033                         (caadr (gnus-id-to-thread last-id))))
8034     ;; We have now found the real root of this thread.  It might have
8035     ;; been gathered into some loose thread, so we have to search
8036     ;; through the threads to find the thread we wanted.
8037     (let ((threads gnus-newsgroup-threads)
8038           sub)
8039       (while threads
8040         (setq sub (car threads))
8041         (if (stringp (car sub))
8042             ;; This is a gathered threads, so we look at the roots
8043             ;; below it to find whether this article in in this
8044             ;; gathered root.
8045             (progn
8046               (setq sub (cdr sub))
8047               (while sub
8048                 (when (member (caar sub) headers)
8049                   (setq thread (car threads)
8050                         threads nil
8051                         sub nil))
8052                 (setq sub (cdr sub))))
8053           ;; It's an ordinary thread, so we check it.
8054           (when (eq (car sub) (car headers))
8055             (setq thread sub
8056                   threads nil)))
8057         (setq threads (cdr threads)))
8058       ;; If this article is in no thread, then it's a root.
8059       (if thread
8060           (unless dont-remove
8061             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8062         (setq thread (gnus-gethash last-id dep)))
8063       (when thread
8064         (prog1
8065             thread ; We return this thread.
8066           (unless dont-remove
8067             (if (stringp (car thread))
8068                 (progn
8069                   ;; If we use dummy roots, then we have to remove the
8070                   ;; dummy root as well.
8071                   (when (eq gnus-summary-make-false-root 'dummy)
8072                     ;; Uhm.
8073                     )
8074                   (setq thread (cdr thread))
8075                   (while thread
8076                     (gnus-remove-thread-1 (car thread))
8077                     (setq thread (cdr thread))))
8078               (gnus-remove-thread-1 thread))))))))
8079
8080 (defun gnus-remove-thread-1 (thread)
8081   "Remove the thread THREAD recursively."
8082   (let ((number (mail-header-number (car thread)))
8083         pos)
8084     (when (setq pos (text-property-any
8085                      (point-min) (point-max) 'gnus-number number))
8086       (goto-char pos)
8087       (gnus-delete-line)
8088       (gnus-data-remove number))
8089     (setq thread (cdr thread))
8090     (while thread
8091       (gnus-remove-thread-1 (pop thread)))))
8092
8093 (defun gnus-sort-threads (threads)
8094   "Sort THREADS."
8095   (if (not gnus-thread-sort-functions)
8096       threads
8097     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8098                     (car gnus-thread-sort-functions)
8099                   `(lambda (t1 t2)
8100                      ,(gnus-make-sort-function 
8101                        (reverse gnus-thread-sort-functions))))))
8102       (gnus-message 7 "Sorting threads...")
8103       (prog1
8104           (sort threads func)
8105         (gnus-message 7 "Sorting threads...done")))))
8106
8107 (defun gnus-sort-articles (articles)
8108   "Sort ARTICLES."
8109   (when gnus-article-sort-functions
8110     (let ((func (if (= 1 (length gnus-article-sort-functions))
8111                     (car gnus-article-sort-functions)
8112                   `(lambda (t1 t2)
8113                      ,(gnus-make-sort-function 
8114                        (reverse gnus-article-sort-functions))))))
8115       (gnus-message 7 "Sorting articles...")
8116       (prog1
8117           (setq gnus-newsgroup-headers (sort articles func))
8118         (gnus-message 7 "Sorting articles...done")))))
8119
8120 (defun gnus-make-sort-function (funs)
8121   "Return a composite sort condition based on the functions in FUNC."
8122   (if (cdr funs)
8123       `(or (,(car funs) t1 t2)
8124            (and (not (,(car funs) t2 t1))
8125                 ,(gnus-make-sort-function (cdr funs))))
8126     `(,(car funs) t1 t2)))
8127                  
8128 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8129 (defmacro gnus-thread-header (thread)
8130   ;; Return header of first article in THREAD.
8131   ;; Note that THREAD must never, ever be anything else than a variable -
8132   ;; using some other form will lead to serious barfage.
8133   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8134   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8135   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8136         (vector thread) 2))
8137
8138 (defsubst gnus-article-sort-by-number (h1 h2)
8139   "Sort articles by article number."
8140   (< (mail-header-number h1)
8141      (mail-header-number h2)))
8142
8143 (defun gnus-thread-sort-by-number (h1 h2)
8144   "Sort threads by root article number."
8145   (gnus-article-sort-by-number
8146    (gnus-thread-header h1) (gnus-thread-header h2)))
8147
8148 (defsubst gnus-article-sort-by-author (h1 h2)
8149   "Sort articles by root author."
8150   (string-lessp
8151    (let ((extract (funcall
8152                    gnus-extract-address-components
8153                    (mail-header-from h1))))
8154      (or (car extract) (cdr extract)))
8155    (let ((extract (funcall
8156                    gnus-extract-address-components
8157                    (mail-header-from h2))))
8158      (or (car extract) (cdr extract)))))
8159
8160 (defun gnus-thread-sort-by-author (h1 h2)
8161   "Sort threads by root author."
8162   (gnus-article-sort-by-author
8163    (gnus-thread-header h1)  (gnus-thread-header h2)))
8164
8165 (defsubst gnus-article-sort-by-subject (h1 h2)
8166   "Sort articles by root subject."
8167   (string-lessp
8168    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8169    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8170
8171 (defun gnus-thread-sort-by-subject (h1 h2)
8172   "Sort threads by root subject."
8173   (gnus-article-sort-by-subject
8174    (gnus-thread-header h1) (gnus-thread-header h2)))
8175
8176 (defsubst gnus-article-sort-by-date (h1 h2)
8177   "Sort articles by root article date."
8178   (string-lessp
8179    (gnus-sortable-date (mail-header-date h1))
8180    (gnus-sortable-date (mail-header-date h2))))
8181
8182 (defun gnus-thread-sort-by-date (h1 h2)
8183   "Sort threads by root article date."
8184   (gnus-article-sort-by-date
8185    (gnus-thread-header h1) (gnus-thread-header h2)))
8186
8187 (defsubst gnus-article-sort-by-score (h1 h2)
8188   "Sort articles by root article score.
8189 Unscored articles will be counted as having a score of zero."
8190   (> (or (cdr (assq (mail-header-number h1)
8191                     gnus-newsgroup-scored))
8192          gnus-summary-default-score 0)
8193      (or (cdr (assq (mail-header-number h2)
8194                     gnus-newsgroup-scored))
8195          gnus-summary-default-score 0)))
8196
8197 (defun gnus-thread-sort-by-score (h1 h2)
8198   "Sort threads by root article score."
8199   (gnus-article-sort-by-score
8200    (gnus-thread-header h1) (gnus-thread-header h2)))
8201
8202 (defun gnus-thread-sort-by-total-score (h1 h2)
8203   "Sort threads by the sum of all scores in the thread.
8204 Unscored articles will be counted as having a score of zero."
8205   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8206
8207 (defun gnus-thread-total-score (thread)
8208   ;;  This function find the total score of THREAD.
8209   (if (consp thread)
8210       (if (stringp (car thread))
8211           (apply gnus-thread-score-function 0
8212                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8213         (gnus-thread-total-score-1 thread))
8214     (gnus-thread-total-score-1 (list thread))))
8215
8216 (defun gnus-thread-total-score-1 (root)
8217   ;; This function find the total score of the thread below ROOT.
8218   (setq root (car root))
8219   (apply gnus-thread-score-function
8220          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8221              gnus-summary-default-score 0)
8222          (mapcar 'gnus-thread-total-score
8223                  (cdr (gnus-gethash (mail-header-id root)
8224                                     gnus-newsgroup-dependencies)))))
8225
8226 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8227 (defvar gnus-tmp-prev-subject nil)
8228 (defvar gnus-tmp-false-parent nil)
8229 (defvar gnus-tmp-root-expunged nil)
8230 (defvar gnus-tmp-dummy-line nil)
8231
8232 (defun gnus-summary-prepare-threads (threads)
8233   "Prepare summary buffer from THREADS and indentation LEVEL.
8234 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8235 or a straight list of headers."
8236   (gnus-message 7 "Generating summary...")
8237
8238   (setq gnus-newsgroup-threads threads)
8239   (beginning-of-line)
8240
8241   (let ((gnus-tmp-level 0)
8242         (default-score (or gnus-summary-default-score 0))
8243         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8244         thread number subject stack state gnus-tmp-gathered beg-match
8245         new-roots gnus-tmp-new-adopts thread-end
8246         gnus-tmp-header gnus-tmp-unread
8247         gnus-tmp-replied gnus-tmp-subject-or-nil
8248         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8249         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8250         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8251
8252     (setq gnus-tmp-prev-subject nil)
8253
8254     (if (vectorp (car threads))
8255         ;; If this is a straight (sic) list of headers, then a
8256         ;; threaded summary display isn't required, so we just create
8257         ;; an unthreaded one.
8258         (gnus-summary-prepare-unthreaded threads)
8259
8260       ;; Do the threaded display.
8261
8262       (while (or threads stack gnus-tmp-new-adopts new-roots)
8263
8264         (if (and (= gnus-tmp-level 0)
8265                  (not (setq gnus-tmp-dummy-line nil))
8266                  (or (not stack)
8267                      (= (caar stack) 0))
8268                  (not gnus-tmp-false-parent)
8269                  (or gnus-tmp-new-adopts new-roots))
8270             (if gnus-tmp-new-adopts
8271                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8272                       thread (list (car gnus-tmp-new-adopts))
8273                       gnus-tmp-header (caar thread)
8274                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8275               (if new-roots
8276                   (setq thread (list (car new-roots))
8277                         gnus-tmp-header (caar thread)
8278                         new-roots (cdr new-roots))))
8279
8280           (if threads
8281               ;; If there are some threads, we do them before the
8282               ;; threads on the stack.
8283               (setq thread threads
8284                     gnus-tmp-header (caar thread))
8285             ;; There were no current threads, so we pop something off
8286             ;; the stack.
8287             (setq state (car stack)
8288                   gnus-tmp-level (car state)
8289                   thread (cdr state)
8290                   stack (cdr stack)
8291                   gnus-tmp-header (caar thread))))
8292
8293         (setq gnus-tmp-false-parent nil)
8294         (setq gnus-tmp-root-expunged nil)
8295         (setq thread-end nil)
8296
8297         (if (stringp gnus-tmp-header)
8298             ;; The header is a dummy root.
8299             (cond
8300              ((eq gnus-summary-make-false-root 'adopt)
8301               ;; We let the first article adopt the rest.
8302               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8303                                                (cddar thread)))
8304               (setq gnus-tmp-gathered
8305                     (nconc (mapcar
8306                             (lambda (h) (mail-header-number (car h)))
8307                             (cddar thread))
8308                            gnus-tmp-gathered))
8309               (setq thread (cons (list (caar thread)
8310                                        (cadar thread))
8311                                  (cdr thread)))
8312               (setq gnus-tmp-level -1
8313                     gnus-tmp-false-parent t))
8314              ((eq gnus-summary-make-false-root 'empty)
8315               ;; We print adopted articles with empty subject fields.
8316               (setq gnus-tmp-gathered
8317                     (nconc (mapcar
8318                             (lambda (h) (mail-header-number (car h)))
8319                             (cddar thread))
8320                            gnus-tmp-gathered))
8321               (setq gnus-tmp-level -1))
8322              ((eq gnus-summary-make-false-root 'dummy)
8323               ;; We remember that we probably want to output a dummy
8324               ;; root.
8325               (setq gnus-tmp-dummy-line gnus-tmp-header)
8326               (setq gnus-tmp-prev-subject gnus-tmp-header))
8327              (t
8328               ;; We do not make a root for the gathered
8329               ;; sub-threads at all.
8330               (setq gnus-tmp-level -1)))
8331
8332           (setq number (mail-header-number gnus-tmp-header)
8333                 subject (mail-header-subject gnus-tmp-header))
8334
8335           (cond
8336            ;; If the thread has changed subject, we might want to make
8337            ;; this subthread into a root.
8338            ((and (null gnus-thread-ignore-subject)
8339                  (not (zerop gnus-tmp-level))
8340                  gnus-tmp-prev-subject
8341                  (not (inline
8342                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8343             (setq new-roots (nconc new-roots (list (car thread)))
8344                   thread-end t
8345                   gnus-tmp-header nil))
8346            ;; If the article lies outside the current limit,
8347            ;; then we do not display it.
8348            ((and (not (memq number gnus-newsgroup-limit))
8349                  (not gnus-tmp-dummy-line))
8350             (setq gnus-tmp-gathered
8351                   (nconc (mapcar
8352                           (lambda (h) (mail-header-number (car h)))
8353                           (cdar thread))
8354                          gnus-tmp-gathered))
8355             (setq gnus-tmp-new-adopts (if (cdar thread)
8356                                           (append gnus-tmp-new-adopts
8357                                                   (cdar thread))
8358                                         gnus-tmp-new-adopts)
8359                   thread-end t
8360                   gnus-tmp-header nil)
8361             (when (zerop gnus-tmp-level)
8362               (setq gnus-tmp-root-expunged t)))
8363            ;; Perhaps this article is to be marked as read?
8364            ((and gnus-summary-mark-below
8365                  (< (or (cdr (assq number gnus-newsgroup-scored))
8366                         default-score)
8367                     gnus-summary-mark-below)
8368                  ;; Don't touch sparse articles.
8369                  (not (memq number gnus-newsgroup-sparse)))
8370             (setq gnus-newsgroup-unreads
8371                   (delq number gnus-newsgroup-unreads))
8372             (if gnus-newsgroup-auto-expire
8373                 (push number gnus-newsgroup-expirable)
8374               (push (cons number gnus-low-score-mark)
8375                     gnus-newsgroup-reads))))
8376
8377           (when gnus-tmp-header
8378             ;; We may have an old dummy line to output before this
8379             ;; article.
8380             (when gnus-tmp-dummy-line
8381               (gnus-summary-insert-dummy-line
8382                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8383               (setq gnus-tmp-dummy-line nil))
8384
8385             ;; Compute the mark.
8386             (setq
8387              gnus-tmp-unread
8388              (cond
8389               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8390               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8391               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8392               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8393               (t (or (cdr (assq number gnus-newsgroup-reads))
8394                      gnus-ancient-mark))))
8395
8396             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8397                                   gnus-tmp-header gnus-tmp-level)
8398                   gnus-newsgroup-data)
8399
8400             ;; Actually insert the line.
8401             (setq
8402              gnus-tmp-subject-or-nil
8403              (cond
8404               ((and gnus-thread-ignore-subject
8405                     gnus-tmp-prev-subject
8406                     (not (inline (gnus-subject-equal
8407                                   gnus-tmp-prev-subject subject))))
8408                subject)
8409               ((zerop gnus-tmp-level)
8410                (if (and (eq gnus-summary-make-false-root 'empty)
8411                         (memq number gnus-tmp-gathered)
8412                         gnus-tmp-prev-subject
8413                         (inline (gnus-subject-equal
8414                                  gnus-tmp-prev-subject subject)))
8415                    gnus-summary-same-subject
8416                  subject))
8417               (t gnus-summary-same-subject)))
8418             (if (and (eq gnus-summary-make-false-root 'adopt)
8419                      (= gnus-tmp-level 1)
8420                      (memq number gnus-tmp-gathered))
8421                 (setq gnus-tmp-opening-bracket ?\<
8422                       gnus-tmp-closing-bracket ?\>)
8423               (setq gnus-tmp-opening-bracket ?\[
8424                     gnus-tmp-closing-bracket ?\]))
8425             (setq
8426              gnus-tmp-indentation
8427              (aref gnus-thread-indent-array gnus-tmp-level)
8428              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8429              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8430                                 gnus-summary-default-score 0)
8431              gnus-tmp-score-char
8432              (if (or (null gnus-summary-default-score)
8433                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8434                          gnus-summary-zcore-fuzz)) ? 
8435                (if (< gnus-tmp-score gnus-summary-default-score)
8436                    gnus-score-below-mark gnus-score-over-mark))
8437              gnus-tmp-replied
8438              (cond ((memq number gnus-newsgroup-processable)
8439                     gnus-process-mark)
8440                    ((memq number gnus-newsgroup-cached)
8441                     gnus-cached-mark)
8442                    ((memq number gnus-newsgroup-replied)
8443                     gnus-replied-mark)
8444                    (t gnus-unread-mark))
8445              gnus-tmp-from (mail-header-from gnus-tmp-header)
8446              gnus-tmp-name
8447              (cond
8448               ((string-match "(.+)" gnus-tmp-from)
8449                (substring gnus-tmp-from
8450                           (1+ (match-beginning 0)) (1- (match-end 0))))
8451               ((string-match "<[^>]+> *$" gnus-tmp-from)
8452                (setq beg-match (match-beginning 0))
8453                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8454                         (substring gnus-tmp-from (1+ (match-beginning 0))
8455                                    (1- (match-end 0))))
8456                    (substring gnus-tmp-from 0 beg-match)))
8457               (t gnus-tmp-from)))
8458             (when (string= gnus-tmp-name "")
8459               (setq gnus-tmp-name gnus-tmp-from))
8460             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8461             (put-text-property
8462              (point)
8463              (progn (eval gnus-summary-line-format-spec) (point))
8464              'gnus-number number)
8465             (when gnus-visual-p
8466               (forward-line -1)
8467               (run-hooks 'gnus-summary-update-hook)
8468               (forward-line 1))
8469
8470             (setq gnus-tmp-prev-subject subject)))
8471
8472         (when (nth 1 thread)
8473           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8474         (incf gnus-tmp-level)
8475         (setq threads (if thread-end nil (cdar thread)))
8476         (unless threads
8477           (setq gnus-tmp-level 0)))))
8478   (gnus-message 7 "Generating summary...done"))
8479
8480 (defun gnus-summary-prepare-unthreaded (headers)
8481   "Generate an unthreaded summary buffer based on HEADERS."
8482   (let (header number mark)
8483
8484     (while headers
8485       ;; We may have to root out some bad articles...
8486       (when (memq (setq number (mail-header-number
8487                                 (setq header (pop headers))))
8488                   gnus-newsgroup-limit)
8489         ;; Mark article as read when it has a low score.
8490         (when (and gnus-summary-mark-below
8491                    (< (or (cdr (assq number gnus-newsgroup-scored))
8492                           gnus-summary-default-score 0)
8493                       gnus-summary-mark-below))
8494           (setq gnus-newsgroup-unreads
8495                 (delq number gnus-newsgroup-unreads))
8496           (if gnus-newsgroup-auto-expire
8497               (push number gnus-newsgroup-expirable)
8498             (push (cons number gnus-low-score-mark)
8499                   gnus-newsgroup-reads)))
8500
8501         (setq mark
8502               (cond
8503                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8504                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8505                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8506                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8507                (t (or (cdr (assq number gnus-newsgroup-reads))
8508                       gnus-ancient-mark))))
8509         (setq gnus-newsgroup-data
8510               (cons (gnus-data-make number mark (1+ (point)) header 0)
8511                     gnus-newsgroup-data))
8512         (gnus-summary-insert-line
8513          header 0 nil mark (memq number gnus-newsgroup-replied)
8514          (memq number gnus-newsgroup-expirable)
8515          (mail-header-subject header) nil
8516          (cdr (assq number gnus-newsgroup-scored))
8517          (memq number gnus-newsgroup-processable))))))
8518
8519 (defun gnus-select-newsgroup (group &optional read-all)
8520   "Select newsgroup GROUP.
8521 If READ-ALL is non-nil, all articles in the group are selected."
8522   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8523          (info (nth 2 entry))
8524          articles fetched-articles cached)
8525
8526     (or (gnus-check-server
8527          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8528         (error "Couldn't open server"))
8529
8530     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8531         (gnus-activate-group group) ; Or we can activate it...
8532         (progn ; Or we bug out.
8533           (when (equal major-mode 'gnus-summary-mode)
8534             (kill-buffer (current-buffer)))
8535           (error "Couldn't request group %s: %s"
8536                  group (gnus-status-message group))))
8537
8538     (setq gnus-newsgroup-name group)
8539     (setq gnus-newsgroup-unselected nil)
8540     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8541
8542     (and gnus-asynchronous
8543          (gnus-check-backend-function
8544           'request-asynchronous gnus-newsgroup-name)
8545          (setq gnus-newsgroup-async
8546                (gnus-request-asynchronous gnus-newsgroup-name)))
8547
8548     ;; Adjust and set lists of article marks.
8549     (when info
8550       (gnus-adjust-marked-articles info))
8551
8552     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8553     (when (gnus-virtual-group-p group)
8554       (setq cached gnus-newsgroup-cached))
8555
8556     (setq gnus-newsgroup-unreads
8557           (gnus-set-difference
8558            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8559            gnus-newsgroup-dormant))
8560
8561     (setq gnus-newsgroup-processable nil)
8562
8563     (setq articles (gnus-articles-to-read group read-all))
8564
8565     (cond
8566      ((null articles)
8567       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8568       'quit)
8569      ((eq articles 0) nil)
8570      (t
8571       ;; Init the dependencies hash table.
8572       (setq gnus-newsgroup-dependencies
8573             (gnus-make-hashtable (length articles)))
8574       ;; Retrieve the headers and read them in.
8575       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8576       (setq gnus-newsgroup-headers
8577             (if (eq 'nov
8578                     (setq gnus-headers-retrieved-by
8579                           (gnus-retrieve-headers
8580                            articles gnus-newsgroup-name
8581                            ;; We might want to fetch old headers, but
8582                            ;; not if there is only 1 article.
8583                            (and gnus-fetch-old-headers
8584                                 (or (and
8585                                      (not (eq gnus-fetch-old-headers 'some))
8586                                      (not (numberp gnus-fetch-old-headers)))
8587                                     (> (length articles) 1))))))
8588                 (gnus-get-newsgroup-headers-xover articles)
8589               (gnus-get-newsgroup-headers)))
8590       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8591
8592       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8593       (when cached
8594         (setq gnus-newsgroup-cached cached))
8595
8596       ;; Set the initial limit.
8597       (setq gnus-newsgroup-limit (copy-sequence articles))
8598       ;; Remove canceled articles from the list of unread articles.
8599       (setq gnus-newsgroup-unreads
8600             (gnus-set-sorted-intersection
8601              gnus-newsgroup-unreads
8602              (setq fetched-articles
8603                    (mapcar (lambda (headers) (mail-header-number headers))
8604                            gnus-newsgroup-headers))))
8605       ;; Removed marked articles that do not exist.
8606       (gnus-update-missing-marks
8607        (gnus-sorted-complement fetched-articles articles))
8608       ;; We might want to build some more threads first.
8609       (and gnus-fetch-old-headers
8610            (eq gnus-headers-retrieved-by 'nov)
8611            (gnus-build-old-threads))
8612       ;; Check whether auto-expire is to be done in this group.
8613       (setq gnus-newsgroup-auto-expire
8614             (gnus-group-auto-expirable-p group))
8615       ;; Set up the article buffer now, if necessary.
8616       (unless gnus-single-article-buffer
8617         (gnus-article-setup-buffer))
8618       ;; First and last article in this newsgroup.
8619       (and gnus-newsgroup-headers
8620            (setq gnus-newsgroup-begin
8621                  (mail-header-number (car gnus-newsgroup-headers)))
8622            (setq gnus-newsgroup-end
8623                  (mail-header-number
8624                   (gnus-last-element gnus-newsgroup-headers))))
8625       (setq gnus-reffed-article-number -1)
8626       ;; GROUP is successfully selected.
8627       (or gnus-newsgroup-headers t)))))
8628
8629 (defun gnus-articles-to-read (group read-all)
8630   ;; Find out what articles the user wants to read.
8631   (let* ((articles
8632           ;; Select all articles if `read-all' is non-nil, or if there
8633           ;; are no unread articles.
8634           (if (or read-all
8635                   (and (zerop (length gnus-newsgroup-marked))
8636                        (zerop (length gnus-newsgroup-unreads))))
8637               (gnus-uncompress-range (gnus-active group))
8638             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8639                           (copy-sequence gnus-newsgroup-unreads))
8640                   '<)))
8641          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8642          (scored (length scored-list))
8643          (number (length articles))
8644          (marked (+ (length gnus-newsgroup-marked)
8645                     (length gnus-newsgroup-dormant)))
8646          (select
8647           (cond
8648            ((numberp read-all)
8649             read-all)
8650            (t
8651             (condition-case ()
8652                 (cond
8653                  ((and (or (<= scored marked) (= scored number))
8654                        (numberp gnus-large-newsgroup)
8655                        (> number gnus-large-newsgroup))
8656                   (let ((input
8657                          (read-string
8658                           (format
8659                            "How many articles from %s (default %d): "
8660                            gnus-newsgroup-name number))))
8661                     (if (string-match "^[ \t]*$" input) number input)))
8662                  ((and (> scored marked) (< scored number))
8663                   (let ((input
8664                          (read-string
8665                           (format "%s %s (%d scored, %d total): "
8666                                   "How many articles from"
8667                                   group scored number))))
8668                     (if (string-match "^[ \t]*$" input)
8669                         number input)))
8670                  (t number))
8671               (quit nil))))))
8672     (setq select (if (stringp select) (string-to-number select) select))
8673     (if (or (null select) (zerop select))
8674         select
8675       (if (and (not (zerop scored)) (<= (abs select) scored))
8676           (progn
8677             (setq articles (sort scored-list '<))
8678             (setq number (length articles)))
8679         (setq articles (copy-sequence articles)))
8680
8681       (if (< (abs select) number)
8682           (if (< select 0)
8683               ;; Select the N oldest articles.
8684               (setcdr (nthcdr (1- (abs select)) articles) nil)
8685             ;; Select the N most recent articles.
8686             (setq articles (nthcdr (- number select) articles))))
8687       (setq gnus-newsgroup-unselected
8688             (gnus-sorted-intersection
8689              gnus-newsgroup-unreads
8690              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8691       articles)))
8692
8693 (defun gnus-killed-articles (killed articles)
8694   (let (out)
8695     (while articles
8696       (if (inline (gnus-member-of-range (car articles) killed))
8697           (setq out (cons (car articles) out)))
8698       (setq articles (cdr articles)))
8699     out))
8700
8701 (defun gnus-uncompress-marks (marks)
8702   "Uncompress the mark ranges in MARKS."
8703   (let ((uncompressed '(score bookmark))
8704         out)
8705     (while marks
8706       (if (memq (caar marks) uncompressed)
8707           (push (car marks) out)
8708         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8709       (setq marks (cdr marks)))
8710     out))
8711
8712 (defun gnus-adjust-marked-articles (info)
8713   "Set all article lists and remove all marks that are no longer legal."
8714   (let* ((marked-lists (gnus-info-marks info))
8715          (active (gnus-active (gnus-info-group info)))
8716          (min (car active))
8717          (max (cdr active))
8718          (types gnus-article-mark-lists)
8719          (uncompressed '(score bookmark))
8720          marks var articles article mark)
8721
8722     (while marked-lists
8723       (setq marks (pop marked-lists))
8724       (set (setq var (intern (format "gnus-newsgroup-%s"
8725                                      (car (rassq (setq mark (car marks))
8726                                                  types)))))
8727            (if (memq (car marks) uncompressed) (cdr marks)
8728              (gnus-uncompress-range (cdr marks))))
8729
8730       (setq articles (symbol-value var))
8731
8732       ;; All articles have to be subsets of the active articles.
8733       (cond
8734        ;; Adjust "simple" lists.
8735        ((memq mark '(tick dormant expirable reply killed save))
8736         (while articles
8737           (when (or (< (setq article (pop articles)) min) (> article max))
8738             (set var (delq article (symbol-value var))))))
8739        ;; Adjust assocs.
8740        ((memq mark '(score bookmark))
8741         (while articles
8742           (when (or (< (car (setq article (pop articles))) min)
8743                     (> (car article) max))
8744             (set var (delq article (symbol-value var))))))))))
8745
8746 (defun gnus-update-missing-marks (missing)
8747   "Go through the list of MISSING articles and remove them mark lists."
8748   (when missing
8749     (let ((types gnus-article-mark-lists)
8750           var m)
8751       ;; Go through all types.
8752       (while types
8753         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8754         (when (symbol-value var)
8755           ;; This list has articles.  So we delete all missing articles
8756           ;; from it.
8757           (setq m missing)
8758           (while m
8759             (set var (delq (pop m) (symbol-value var)))))))))
8760
8761 (defun gnus-update-marks ()
8762   "Enter the various lists of marked articles into the newsgroup info list."
8763   (let ((types gnus-article-mark-lists)
8764         (info (gnus-get-info gnus-newsgroup-name))
8765         (uncompressed '(score bookmark killed))
8766         type list newmarked symbol)
8767     (when info
8768       ;; Add all marks lists that are non-nil to the list of marks lists.
8769       (while types
8770         (setq type (pop types))
8771         (when (setq list (symbol-value
8772                           (setq symbol
8773                                 (intern (format "gnus-newsgroup-%s"
8774                                                 (car type))))))
8775           (push (cons (cdr type)
8776                       (if (memq (cdr type) uncompressed) list
8777                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8778                 newmarked)))
8779
8780       ;; Enter these new marks into the info of the group.
8781       (if (nthcdr 3 info)
8782           (setcar (nthcdr 3 info) newmarked)
8783         ;; Add the marks lists to the end of the info.
8784         (when newmarked
8785           (setcdr (nthcdr 2 info) (list newmarked))))
8786
8787       ;; Cut off the end of the info if there's nothing else there.
8788       (let ((i 5))
8789         (while (and (> i 2)
8790                     (not (nth i info)))
8791           (when (nthcdr (decf i) info)
8792             (setcdr (nthcdr i info) nil)))))))
8793
8794 (defun gnus-add-marked-articles (group type articles &optional info force)
8795   ;; Add ARTICLES of TYPE to the info of GROUP.
8796   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8797   ;; add, but replace marked articles of TYPE with ARTICLES.
8798   (let ((info (or info (gnus-get-info group)))
8799         (uncompressed '(score bookmark killed))
8800         marked m)
8801     (or (not info)
8802         (and (not (setq marked (nthcdr 3 info)))
8803              (or (null articles)
8804                  (setcdr (nthcdr 2 info)
8805                          (list (list (cons type (gnus-compress-sequence
8806                                                  articles t)))))))
8807         (and (not (setq m (assq type (car marked))))
8808              (or (null articles)
8809                  (setcar marked
8810                          (cons (cons type (gnus-compress-sequence articles t) )
8811                                (car marked)))))
8812         (if force
8813             (if (null articles)
8814                 (setcar (nthcdr 3 info)
8815                         (delq (assq type (car marked)) (car marked)))
8816               (setcdr m (gnus-compress-sequence articles t)))
8817           (setcdr m (gnus-compress-sequence
8818                      (sort (nconc (gnus-uncompress-range (cdr m))
8819                                   (copy-sequence articles)) '<) t))))))
8820
8821 (defun gnus-set-mode-line (where)
8822   "This function sets the mode line of the article or summary buffers.
8823 If WHERE is `summary', the summary mode line format will be used."
8824   ;; Is this mode line one we keep updated?
8825   (when (memq where gnus-updated-mode-lines)
8826     (let (mode-string)
8827       (save-excursion
8828         ;; We evaluate this in the summary buffer since these
8829         ;; variables are buffer-local to that buffer.
8830         (set-buffer gnus-summary-buffer)
8831         ;; We bind all these variables that are used in the `eval' form
8832         ;; below.
8833         (let* ((mformat (symbol-value
8834                          (intern
8835                           (format "gnus-%s-mode-line-format-spec" where))))
8836                (gnus-tmp-group-name gnus-newsgroup-name)
8837                (gnus-tmp-article-number (or gnus-current-article 0))
8838                (gnus-tmp-unread gnus-newsgroup-unreads)
8839                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8840                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8841                (gnus-tmp-unread-and-unselected
8842                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8843                             (zerop gnus-tmp-unselected)) "")
8844                       ((zerop gnus-tmp-unselected)
8845                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8846                       (t (format "{%d(+%d) more}"
8847                                  gnus-tmp-unread-and-unticked
8848                                  gnus-tmp-unselected))))
8849                (gnus-tmp-subject
8850                 (if (and gnus-current-headers
8851                          (vectorp gnus-current-headers))
8852                     (mail-header-subject gnus-current-headers) ""))
8853                max-len
8854                gnus-tmp-header);; passed as argument to any user-format-funcs
8855           (setq mode-string (eval mformat))
8856           (setq max-len (max 4 (if gnus-mode-non-string-length
8857                                    (- (frame-width)
8858                                       gnus-mode-non-string-length)
8859                                  (length mode-string))))
8860           ;; We might have to chop a bit of the string off...
8861           (when (> (length mode-string) max-len)
8862             (setq mode-string
8863                   (concat (gnus-truncate-string mode-string (- max-len 3))
8864                           "...")))
8865           ;; Pad the mode string a bit.
8866           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8867       ;; Update the mode line.
8868       (setq mode-line-buffer-identification (list mode-string))
8869       (set-buffer-modified-p t))))
8870
8871 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8872   "Go through the HEADERS list and add all Xrefs to a hash table.
8873 The resulting hash table is returned, or nil if no Xrefs were found."
8874   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8875          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8876          (xref-hashtb (make-vector 63 0))
8877          start group entry number xrefs header)
8878     (while headers
8879       (setq header (pop headers))
8880       (when (and (setq xrefs (mail-header-xref header))
8881                  (not (memq (setq number (mail-header-number header))
8882                             unreads)))
8883         (setq start 0)
8884         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8885           (setq start (match-end 0))
8886           (setq group (if prefix
8887                           (concat prefix (substring xrefs (match-beginning 1)
8888                                                     (match-end 1)))
8889                         (substring xrefs (match-beginning 1) (match-end 1))))
8890           (setq number
8891                 (string-to-int (substring xrefs (match-beginning 2)
8892                                           (match-end 2))))
8893           (if (setq entry (gnus-gethash group xref-hashtb))
8894               (setcdr entry (cons number (cdr entry)))
8895             (gnus-sethash group (cons number nil) xref-hashtb)))))
8896     (and start xref-hashtb)))
8897
8898 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8899   "Look through all the headers and mark the Xrefs as read."
8900   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8901         name entry info xref-hashtb idlist method nth4)
8902     (save-excursion
8903       (set-buffer gnus-group-buffer)
8904       (when (setq xref-hashtb
8905                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8906         (mapatoms
8907          (lambda (group)
8908            (unless (string= from-newsgroup (setq name (symbol-name group)))
8909              (setq idlist (symbol-value group))
8910              ;; Dead groups are not updated.
8911              (and (prog1
8912                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8913                             info (nth 2 entry))
8914                     (if (stringp (setq nth4 (gnus-info-method info)))
8915                         (setq nth4 (gnus-server-to-method nth4))))
8916                   ;; Only do the xrefs if the group has the same
8917                   ;; select method as the group we have just read.
8918                   (or (gnus-methods-equal-p
8919                        nth4 (gnus-find-method-for-group from-newsgroup))
8920                       virtual
8921                       (equal nth4 (setq method (gnus-find-method-for-group
8922                                                 from-newsgroup)))
8923                       (and (equal (car nth4) (car method))
8924                            (equal (nth 1 nth4) (nth 1 method))))
8925                   gnus-use-cross-reference
8926                   (or (not (eq gnus-use-cross-reference t))
8927                       virtual
8928                       ;; Only do cross-references on subscribed
8929                       ;; groups, if that is what is wanted.
8930                       (<= (gnus-info-level info) gnus-level-subscribed))
8931                   (gnus-group-make-articles-read name idlist))))
8932          xref-hashtb)))))
8933
8934 (defun gnus-group-make-articles-read (group articles)
8935   (let* ((num 0)
8936          (entry (gnus-gethash group gnus-newsrc-hashtb))
8937          (info (nth 2 entry))
8938          (active (gnus-active group))
8939          range)
8940     ;; First peel off all illegal article numbers.
8941     (if active
8942         (let ((ids articles)
8943               id first)
8944           (while ids
8945             (setq id (car ids))
8946             (if (and first (> id (cdr active)))
8947                 (progn
8948                   ;; We'll end up in this situation in one particular
8949                   ;; obscure situation.  If you re-scan a group and get
8950                   ;; a new article that is cross-posted to a different
8951                   ;; group that has not been re-scanned, you might get
8952                   ;; crossposted article that has a higher number than
8953                   ;; Gnus believes possible.  So we re-activate this
8954                   ;; group as well.  This might mean doing the
8955                   ;; crossposting thingy will *increase* the number
8956                   ;; of articles in some groups.  Tsk, tsk.
8957                   (setq active (or (gnus-activate-group group) active))))
8958             (if (or (> id (cdr active))
8959                     (< id (car active)))
8960                 (setq articles (delq id articles)))
8961             (setq ids (cdr ids)))))
8962     ;; If the read list is nil, we init it.
8963     (and active
8964          (null (gnus-info-read info))
8965          (> (car active) 1)
8966          (gnus-info-set-read info (cons 1 (1- (car active)))))
8967     ;; Then we add the read articles to the range.
8968     (gnus-info-set-read
8969      info
8970      (setq range
8971            (gnus-add-to-range
8972             (gnus-info-read info) (setq articles (sort articles '<)))))
8973     ;; Then we have to re-compute how many unread
8974     ;; articles there are in this group.
8975     (if active
8976         (progn
8977           (cond
8978            ((not range)
8979             (setq num (- (1+ (cdr active)) (car active))))
8980            ((not (listp (cdr range)))
8981             (setq num (- (cdr active) (- (1+ (cdr range))
8982                                          (car range)))))
8983            (t
8984             (while range
8985               (if (numberp (car range))
8986                   (setq num (1+ num))
8987                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8988               (setq range (cdr range)))
8989             (setq num (- (cdr active) num))))
8990           ;; Update the number of unread articles.
8991           (setcar entry num)
8992           ;; Update the group buffer.
8993           (gnus-group-update-group group t)))))
8994
8995 (defun gnus-methods-equal-p (m1 m2)
8996   (let ((m1 (or m1 gnus-select-method))
8997         (m2 (or m2 gnus-select-method)))
8998     (or (equal m1 m2)
8999         (and (eq (car m1) (car m2))
9000              (or (not (memq 'address (assoc (symbol-name (car m1))
9001                                             gnus-valid-select-methods)))
9002                  (equal (nth 1 m1) (nth 1 m2)))))))
9003
9004 (defsubst gnus-header-value ()
9005   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9006
9007 (defvar gnus-newsgroup-none-id 0)
9008
9009 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9010   (let ((cur nntp-server-buffer)
9011         (dependencies
9012          (or dependencies
9013              (save-excursion (set-buffer gnus-summary-buffer)
9014                              gnus-newsgroup-dependencies)))
9015         headers id id-dep ref-dep end ref)
9016     (save-excursion
9017       (set-buffer nntp-server-buffer)
9018       (let ((case-fold-search t)
9019             in-reply-to header p lines)
9020         (goto-char (point-min))
9021         ;; Search to the beginning of the next header.  Error messages
9022         ;; do not begin with 2 or 3.
9023         (while (re-search-forward "^[23][0-9]+ " nil t)
9024           (setq id nil
9025                 ref nil)
9026           ;; This implementation of this function, with nine
9027           ;; search-forwards instead of the one re-search-forward and
9028           ;; a case (which basically was the old function) is actually
9029           ;; about twice as fast, even though it looks messier.  You
9030           ;; can't have everything, I guess.  Speed and elegance
9031           ;; doesn't always go hand in hand.
9032           (setq
9033            header
9034            (vector
9035             ;; Number.
9036             (prog1
9037                 (read cur)
9038               (end-of-line)
9039               (setq p (point))
9040               (narrow-to-region (point)
9041                                 (or (and (search-forward "\n.\n" nil t)
9042                                          (- (point) 2))
9043                                     (point))))
9044             ;; Subject.
9045             (progn
9046               (goto-char p)
9047               (if (search-forward "\nsubject: " nil t)
9048                   (gnus-header-value) "(none)"))
9049             ;; From.
9050             (progn
9051               (goto-char p)
9052               (if (search-forward "\nfrom: " nil t)
9053                   (gnus-header-value) "(nobody)"))
9054             ;; Date.
9055             (progn
9056               (goto-char p)
9057               (if (search-forward "\ndate: " nil t)
9058                   (gnus-header-value) ""))
9059             ;; Message-ID.
9060             (progn
9061               (goto-char p)
9062               (if (search-forward "\nmessage-id: " nil t)
9063                   (setq id (gnus-header-value))
9064                 ;; If there was no message-id, we just fake one to make
9065                 ;; subsequent routines simpler.
9066                 (setq id (concat "none+"
9067                                  (int-to-string
9068                                   (setq gnus-newsgroup-none-id
9069                                         (1+ gnus-newsgroup-none-id)))))))
9070             ;; References.
9071             (progn
9072               (goto-char p)
9073               (if (search-forward "\nreferences: " nil t)
9074                   (prog1
9075                       (gnus-header-value)
9076                     (setq end (match-end 0))
9077                     (save-excursion
9078                       (setq ref
9079                             (buffer-substring
9080                              (progn
9081                                (end-of-line)
9082                                (search-backward ">" end t)
9083                                (1+ (point)))
9084                              (progn
9085                                (search-backward "<" end t)
9086                                (point))))))
9087                 ;; Get the references from the in-reply-to header if there
9088                 ;; were no references and the in-reply-to header looks
9089                 ;; promising.
9090                 (if (and (search-forward "\nin-reply-to: " nil t)
9091                          (setq in-reply-to (gnus-header-value))
9092                          (string-match "<[^>]+>" in-reply-to))
9093                     (setq ref (substring in-reply-to (match-beginning 0)
9094                                          (match-end 0)))
9095                   (setq ref ""))))
9096             ;; Chars.
9097             0
9098             ;; Lines.
9099             (progn
9100               (goto-char p)
9101               (if (search-forward "\nlines: " nil t)
9102                   (if (numberp (setq lines (read cur)))
9103                       lines 0)
9104                 0))
9105             ;; Xref.
9106             (progn
9107               (goto-char p)
9108               (and (search-forward "\nxref: " nil t)
9109                    (gnus-header-value)))))
9110           ;; We do the threading while we read the headers.  The
9111           ;; message-id and the last reference are both entered into
9112           ;; the same hash table.  Some tippy-toeing around has to be
9113           ;; done in case an article has arrived before the article
9114           ;; which it refers to.
9115           (if (boundp (setq id-dep (intern id dependencies)))
9116               (if (and (car (symbol-value id-dep))
9117                        (not force-new))
9118                   ;; An article with this Message-ID has already
9119                   ;; been seen, so we ignore this one, except we add
9120                   ;; any additional Xrefs (in case the two articles
9121                   ;; came from different servers).
9122                   (progn
9123                     (mail-header-set-xref
9124                      (car (symbol-value id-dep))
9125                      (concat (or (mail-header-xref
9126                                   (car (symbol-value id-dep))) "")
9127                              (or (mail-header-xref header) "")))
9128                     (setq header nil))
9129                 (setcar (symbol-value id-dep) header))
9130             (set id-dep (list header)))
9131           (when header
9132             (if (boundp (setq ref-dep (intern ref dependencies)))
9133                 (setcdr (symbol-value ref-dep)
9134                         (nconc (cdr (symbol-value ref-dep))
9135                                (list (symbol-value id-dep))))
9136               (set ref-dep (list nil (symbol-value id-dep))))
9137             (setq headers (cons header headers)))
9138           (goto-char (point-max))
9139           (widen))
9140         (nreverse headers)))))
9141
9142 ;; The following macros and functions were written by Felix Lee
9143 ;; <flee@cse.psu.edu>.
9144
9145 (defmacro gnus-nov-read-integer ()
9146   '(prog1
9147        (if (= (following-char) ?\t)
9148            0
9149          (let ((num (condition-case nil (read buffer) (error nil))))
9150            (if (numberp num) num 0)))
9151      (or (eobp) (forward-char 1))))
9152
9153 (defmacro gnus-nov-skip-field ()
9154   '(search-forward "\t" eol 'move))
9155
9156 (defmacro gnus-nov-field ()
9157   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9158
9159 ;; Goes through the xover lines and returns a list of vectors
9160 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9161                                                   force-new dependencies)
9162   "Parse the news overview data in the server buffer, and return a
9163 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9164   ;; Get the Xref when the users reads the articles since most/some
9165   ;; NNTP servers do not include Xrefs when using XOVER.
9166   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9167   (let ((cur nntp-server-buffer)
9168         (dependencies (or dependencies gnus-newsgroup-dependencies))
9169         number headers header)
9170     (save-excursion
9171       (set-buffer nntp-server-buffer)
9172       ;; Allow the user to mangle the headers before parsing them.
9173       (run-hooks 'gnus-parse-headers-hook)
9174       (goto-char (point-min))
9175       (while (and sequence (not (eobp)))
9176         (setq number (read cur))
9177         (while (and sequence (< (car sequence) number))
9178           (setq sequence (cdr sequence)))
9179         (and sequence
9180              (eq number (car sequence))
9181              (progn
9182                (setq sequence (cdr sequence))
9183                (if (setq header
9184                          (inline (gnus-nov-parse-line
9185                                   number dependencies force-new)))
9186                    (setq headers (cons header headers)))))
9187         (forward-line 1))
9188       (setq headers (nreverse headers)))
9189     headers))
9190
9191 ;; This function has to be called with point after the article number
9192 ;; on the beginning of the line.
9193 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9194   (let ((none 0)
9195         (eol (gnus-point-at-eol))
9196         (buffer (current-buffer))
9197         header ref id id-dep ref-dep)
9198
9199     ;; overview: [num subject from date id refs chars lines misc]
9200     (narrow-to-region (point) eol)
9201     (or (eobp) (forward-char))
9202
9203     (condition-case nil
9204         (setq header
9205               (vector
9206                number                   ; number
9207                (gnus-nov-field)         ; subject
9208                (gnus-nov-field)         ; from
9209                (gnus-nov-field)         ; date
9210                (setq id (or (gnus-nov-field)
9211                             (concat "none+"
9212                                     (int-to-string
9213                                      (setq none (1+ none)))))) ; id
9214                (progn
9215                  (save-excursion
9216                    (let ((beg (point)))
9217                      (search-forward "\t" eol)
9218                      (if (search-backward ">" beg t)
9219                          (setq ref
9220                                (buffer-substring
9221                                 (1+ (point))
9222                                 (search-backward "<" beg t)))
9223                        (setq ref nil))))
9224                  (gnus-nov-field))      ; refs
9225                (gnus-nov-read-integer)  ; chars
9226                (gnus-nov-read-integer)  ; lines
9227                (if (= (following-char) ?\n)
9228                    nil
9229                  (gnus-nov-field))      ; misc
9230                ))
9231       (error (progn
9232                (gnus-error 4 "Strange nov line")
9233                (setq header nil)
9234                (goto-char eol))))
9235
9236     (widen)
9237
9238     ;; We build the thread tree.
9239     (when header
9240       (if (boundp (setq id-dep (intern id dependencies)))
9241           (if (and (car (symbol-value id-dep))
9242                    (not force-new))
9243               ;; An article with this Message-ID has already been seen,
9244               ;; so we ignore this one, except we add any additional
9245               ;; Xrefs (in case the two articles came from different
9246               ;; servers.
9247               (progn
9248                 (mail-header-set-xref
9249                  (car (symbol-value id-dep))
9250                  (concat (or (mail-header-xref
9251                               (car (symbol-value id-dep))) "")
9252                          (or (mail-header-xref header) "")))
9253                 (setq header nil))
9254             (setcar (symbol-value id-dep) header))
9255         (set id-dep (list header))))
9256     (if header
9257         (progn
9258           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9259               (setcdr (symbol-value ref-dep)
9260                       (nconc (cdr (symbol-value ref-dep))
9261                              (list (symbol-value id-dep))))
9262             (set ref-dep (list nil (symbol-value id-dep))))))
9263     header))
9264
9265 (defun gnus-article-get-xrefs ()
9266   "Fill in the Xref value in `gnus-current-headers', if necessary.
9267 This is meant to be called in `gnus-article-internal-prepare-hook'."
9268   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9269                                  gnus-current-headers)))
9270     (or (not gnus-use-cross-reference)
9271         (not headers)
9272         (and (mail-header-xref headers)
9273              (not (string= (mail-header-xref headers) "")))
9274         (let ((case-fold-search t)
9275               xref)
9276           (save-restriction
9277             (nnheader-narrow-to-headers)
9278             (goto-char (point-min))
9279             (if (or (and (eq (downcase (following-char)) ?x)
9280                          (looking-at "Xref:"))
9281                     (search-forward "\nXref:" nil t))
9282                 (progn
9283                   (goto-char (1+ (match-end 0)))
9284                   (setq xref (buffer-substring (point)
9285                                                (progn (end-of-line) (point))))
9286                   (mail-header-set-xref headers xref))))))))
9287
9288 (defun gnus-summary-insert-subject (id &optional old-header)
9289   "Find article ID and insert the summary line for that article."
9290   (let ((header (gnus-read-header id))
9291         (number (and (numberp id) id))
9292         pos)
9293     (when header
9294       ;; Rebuild the thread that this article is part of and go to the
9295       ;; article we have fetched.
9296       (when old-header
9297         (when (setq pos (text-property-any
9298                          (point-min) (point-max) 'gnus-number 
9299                          (mail-header-number old-header)))
9300           (goto-char pos)
9301           (gnus-delete-line)
9302           (gnus-data-remove (mail-header-number old-header))))
9303       (gnus-rebuild-thread (mail-header-id header))
9304       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9305     (when (and (numberp number)
9306                (> number 0))
9307       ;; We have to update the boundaries even if we can't fetch the
9308       ;; article if ID is a number -- so that the next `P' or `N'
9309       ;; command will fetch the previous (or next) article even
9310       ;; if the one we tried to fetch this time has been canceled.
9311       (and (> number gnus-newsgroup-end)
9312            (setq gnus-newsgroup-end number))
9313       (and (< number gnus-newsgroup-begin)
9314            (setq gnus-newsgroup-begin number))
9315       (setq gnus-newsgroup-unselected
9316             (delq number gnus-newsgroup-unselected)))
9317     ;; Report back a success?
9318     (and header (mail-header-number header))))
9319
9320 (defun gnus-summary-work-articles (n)
9321   "Return a list of articles to be worked upon.  The prefix argument,
9322 the list of process marked articles, and the current article will be
9323 taken into consideration."
9324   (cond
9325    ((and n (numberp n))
9326     ;; A numerical prefix has been given.
9327     (let ((backward (< n 0))
9328           (n (abs n))
9329           articles article)
9330       (save-excursion
9331         (while
9332             (and (> n 0)
9333                  (push (setq article (gnus-summary-article-number))
9334                        articles)
9335                  (if backward
9336                      (gnus-summary-find-prev nil article)
9337                    (gnus-summary-find-next nil article)))
9338           (decf n)))
9339       (nreverse articles)))
9340    ((and (boundp 'transient-mark-mode)
9341          transient-mark-mode
9342          mark-active)
9343     ;; Work on the region between point and mark.
9344     (let ((max (max (point) (mark)))
9345           articles article)
9346       (save-excursion
9347         (goto-char (min (point) (mark)))
9348         (while
9349             (and
9350              (push (setq article (gnus-summary-article-number)) articles)
9351              (gnus-summary-find-next nil article)
9352              (< (point) max)))
9353         (nreverse articles))))
9354    (gnus-newsgroup-processable
9355     ;; There are process-marked articles present.
9356     (reverse gnus-newsgroup-processable))
9357    (t
9358     ;; Just return the current article.
9359     (list (gnus-summary-article-number)))))
9360
9361 (defun gnus-summary-search-group (&optional backward use-level)
9362   "Search for next unread newsgroup.
9363 If optional argument BACKWARD is non-nil, search backward instead."
9364   (save-excursion
9365     (set-buffer gnus-group-buffer)
9366     (if (gnus-group-search-forward
9367          backward nil (if use-level (gnus-group-group-level) nil))
9368         (gnus-group-group-name))))
9369
9370 (defun gnus-summary-best-group (&optional exclude-group)
9371   "Find the name of the best unread group.
9372 If EXCLUDE-GROUP, do not go to this group."
9373   (save-excursion
9374     (set-buffer gnus-group-buffer)
9375     (save-excursion
9376       (gnus-group-best-unread-group exclude-group))))
9377
9378 (defun gnus-summary-find-next (&optional unread article backward)
9379   (if backward (gnus-summary-find-prev)
9380     (let* ((article (or article (gnus-summary-article-number)))
9381            (arts (gnus-data-find-list article))
9382            result)
9383       (when (or (not gnus-summary-check-current)
9384                 (not unread)
9385                 (not (gnus-data-unread-p (car arts))))
9386         (setq arts (cdr arts)))
9387       (when (setq result
9388                   (if unread
9389                       (progn
9390                         (while arts
9391                           (when (gnus-data-unread-p (car arts))
9392                             (setq result (car arts)
9393                                   arts nil))
9394                           (setq arts (cdr arts)))
9395                         result)
9396                     (car arts)))
9397         (goto-char (gnus-data-pos result))
9398         (gnus-data-number result)))))
9399
9400 (defun gnus-summary-find-prev (&optional unread article)
9401   (let* ((article (or article (gnus-summary-article-number)))
9402          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9403          result)
9404     (when (or (not gnus-summary-check-current)
9405               (not unread)
9406               (not (gnus-data-unread-p (car arts))))
9407       (setq arts (cdr arts)))
9408     (if (setq result
9409               (if unread
9410                   (progn
9411                     (while arts
9412                       (and (gnus-data-unread-p (car arts))
9413                            (setq result (car arts)
9414                                  arts nil))
9415                       (setq arts (cdr arts)))
9416                     result)
9417                 (car arts)))
9418         (progn
9419           (goto-char (gnus-data-pos result))
9420           (gnus-data-number result)))))
9421
9422 (defun gnus-summary-find-subject (subject &optional unread backward article)
9423   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9424          (article (or article (gnus-summary-article-number)))
9425          (articles (gnus-data-list backward))
9426          (arts (gnus-data-find-list article articles))
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     (while arts
9433       (and (or (not unread)
9434                (gnus-data-unread-p (car arts)))
9435            (vectorp (gnus-data-header (car arts)))
9436            (gnus-subject-equal
9437             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9438            (setq result (car arts)
9439                  arts nil))
9440       (setq arts (cdr arts)))
9441     (and result
9442          (goto-char (gnus-data-pos result))
9443          (gnus-data-number result))))
9444
9445 (defun gnus-summary-search-forward (&optional unread subject backward)
9446   "Search forward for an article.
9447 If UNREAD, look for unread articles.  If SUBJECT, look for
9448 articles with that subject.  If BACKWARD, search backward instead."
9449   (cond (subject (gnus-summary-find-subject subject unread backward))
9450         (backward (gnus-summary-find-prev unread))
9451         (t (gnus-summary-find-next unread))))
9452
9453 (defun gnus-recenter (&optional n)
9454   "Center point in window and redisplay frame.
9455 Also do horizontal recentering."
9456   (interactive "P")
9457   (when (and gnus-auto-center-summary
9458              (not (eq gnus-auto-center-summary 'vertical)))
9459     (gnus-horizontal-recenter))
9460   (recenter n))
9461
9462 (defun gnus-summary-recenter ()
9463   "Center point in the summary window.
9464 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9465 displayed, no centering will be performed."
9466   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9467   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9468   (let* ((top (cond ((< (window-height) 4) 0)
9469                     ((< (window-height) 7) 1)
9470                     (t 2)))
9471          (height (1- (window-height)))
9472          (bottom (save-excursion (goto-char (point-max))
9473                                  (forward-line (- height))
9474                                  (point)))
9475          (window (get-buffer-window (current-buffer))))
9476     ;; The user has to want it.
9477     (when gnus-auto-center-summary
9478       (when (get-buffer-window gnus-article-buffer)
9479        ;; Only do recentering when the article buffer is displayed,
9480        ;; Set the window start to either `bottom', which is the biggest
9481        ;; possible valid number, or the second line from the top,
9482        ;; whichever is the least.
9483        (set-window-start
9484         window (min bottom (save-excursion 
9485                              (forward-line (- top)) (point)))))
9486       ;; Do horizontal recentering while we're at it.
9487       (when (and (get-buffer-window (current-buffer) t)
9488                  (not (eq gnus-auto-center-summary 'vertical)))
9489         (let ((selected (selected-window)))
9490           (select-window (get-buffer-window (current-buffer) t))
9491           (gnus-summary-position-point)
9492           (gnus-horizontal-recenter)
9493           (select-window selected))))))
9494
9495 (defun gnus-horizontal-recenter ()
9496   "Recenter the current buffer horizontally."
9497   (if (< (current-column) (/ (window-width) 2))
9498       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9499     (let* ((orig (point))
9500            (end (window-end (get-buffer-window (current-buffer) t)))
9501            (max 0))
9502       ;; Find the longest line currently displayed in the window.
9503       (goto-char (window-start))
9504       (while (and (not (eobp)) 
9505                   (< (point) end))
9506         (end-of-line)
9507         (setq max (max max (current-column)))
9508         (forward-line 1))
9509       (goto-char orig)
9510       ;; Scroll horizontally to center (sort of) the point.
9511       (if (> max (window-width))
9512           (set-window-hscroll 
9513            (get-buffer-window (current-buffer) t)
9514            (min (- (current-column) (/ (window-width) 3))
9515                 (+ 2 (- max (window-width)))))
9516         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9517       max)))
9518
9519 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9520 (defun gnus-short-group-name (group &optional levels)
9521   "Collapse GROUP name LEVELS."
9522   (let* ((name "") 
9523          (foreign "")
9524          (depth 0) 
9525          (skip 1)
9526          (levels (or levels
9527                      (progn
9528                        (while (string-match "\\." group skip)
9529                          (setq skip (match-end 0)
9530                                depth (+ depth 1)))
9531                        depth))))
9532     (if (string-match ":" group)
9533         (setq foreign (substring group 0 (match-end 0))
9534               group (substring group (match-end 0))))
9535     (while group
9536       (if (and (string-match "\\." group)
9537                (> levels (- gnus-group-uncollapsed-levels 1)))
9538           (setq name (concat name (substring group 0 1))
9539                 group (substring group (match-end 0))
9540                 levels (- levels 1)
9541                 name (concat name "."))
9542         (setq name (concat foreign name group)
9543               group nil)))
9544     name))
9545
9546 (defun gnus-summary-jump-to-group (newsgroup)
9547   "Move point to NEWSGROUP in group mode buffer."
9548   ;; Keep update point of group mode buffer if visible.
9549   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9550       (save-window-excursion
9551         ;; Take care of tree window mode.
9552         (if (get-buffer-window gnus-group-buffer)
9553             (pop-to-buffer gnus-group-buffer))
9554         (gnus-group-jump-to-group newsgroup))
9555     (save-excursion
9556       ;; Take care of tree window mode.
9557       (if (get-buffer-window gnus-group-buffer)
9558           (pop-to-buffer gnus-group-buffer)
9559         (set-buffer gnus-group-buffer))
9560       (gnus-group-jump-to-group newsgroup))))
9561
9562 ;; This function returns a list of article numbers based on the
9563 ;; difference between the ranges of read articles in this group and
9564 ;; the range of active articles.
9565 (defun gnus-list-of-unread-articles (group)
9566   (let* ((read (gnus-info-read (gnus-get-info group)))
9567          (active (gnus-active group))
9568          (last (cdr active))
9569          first nlast unread)
9570     ;; If none are read, then all are unread.
9571     (if (not read)
9572         (setq first (car active))
9573       ;; If the range of read articles is a single range, then the
9574       ;; first unread article is the article after the last read
9575       ;; article.  Sounds logical, doesn't it?
9576       (if (not (listp (cdr read)))
9577           (setq first (1+ (cdr read)))
9578         ;; `read' is a list of ranges.
9579         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9580                                 (caar read))) 1)
9581             (setq first 1))
9582         (while read
9583           (if first
9584               (while (< first nlast)
9585                 (setq unread (cons first unread))
9586                 (setq first (1+ first))))
9587           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9588           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9589           (setq read (cdr read)))))
9590     ;; And add the last unread articles.
9591     (while (<= first last)
9592       (setq unread (cons first unread))
9593       (setq first (1+ first)))
9594     ;; Return the list of unread articles.
9595     (nreverse unread)))
9596
9597 (defun gnus-list-of-read-articles (group)
9598   "Return a list of unread, unticked and non-dormant articles."
9599   (let* ((info (gnus-get-info group))
9600          (marked (gnus-info-marks info))
9601          (active (gnus-active group)))
9602     (and info active
9603          (gnus-set-difference
9604           (gnus-sorted-complement
9605            (gnus-uncompress-range active)
9606            (gnus-list-of-unread-articles group))
9607           (append
9608            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9609            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9610
9611 ;; Various summary commands
9612
9613 (defun gnus-summary-universal-argument (arg)
9614   "Perform any operation on all articles that are process/prefixed."
9615   (interactive "P")
9616   (gnus-set-global-variables)
9617   (let ((articles (gnus-summary-work-articles arg))
9618         func article)
9619     (if (eq
9620          (setq
9621           func
9622           (key-binding
9623            (read-key-sequence
9624             (substitute-command-keys
9625              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9626              ))))
9627          'undefined)
9628         (gnus-error 1 "Undefined key")
9629       (save-excursion
9630         (while articles
9631           (gnus-summary-goto-subject (setq article (pop articles)))
9632           (command-execute func)
9633           (gnus-summary-remove-process-mark article)))))
9634   (gnus-summary-position-point))
9635
9636 (defun gnus-summary-toggle-truncation (&optional arg)
9637   "Toggle truncation of summary lines.
9638 With arg, turn line truncation on iff arg is positive."
9639   (interactive "P")
9640   (setq truncate-lines
9641         (if (null arg) (not truncate-lines)
9642           (> (prefix-numeric-value arg) 0)))
9643   (redraw-display))
9644
9645 (defun gnus-summary-reselect-current-group (&optional all rescan)
9646   "Exit and then reselect the current newsgroup.
9647 The prefix argument ALL means to select all articles."
9648   (interactive "P")
9649   (gnus-set-global-variables)
9650   (let ((current-subject (gnus-summary-article-number))
9651         (group gnus-newsgroup-name))
9652     (setq gnus-newsgroup-begin nil)
9653     (gnus-summary-exit)
9654     ;; We have to adjust the point of group mode buffer because the
9655     ;; current point was moved to the next unread newsgroup by
9656     ;; exiting.
9657     (gnus-summary-jump-to-group group)
9658     (when rescan
9659       (save-excursion
9660         (gnus-group-get-new-news-this-group 1)))
9661     (gnus-group-read-group all t)
9662     (gnus-summary-goto-subject current-subject)))
9663
9664 (defun gnus-summary-rescan-group (&optional all)
9665   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9666   (interactive "P")
9667   (gnus-summary-reselect-current-group all t))
9668
9669 (defun gnus-summary-update-info ()
9670   (let* ((group gnus-newsgroup-name))
9671     (when gnus-newsgroup-kill-headers
9672       (setq gnus-newsgroup-killed
9673             (gnus-compress-sequence
9674              (nconc
9675               (gnus-set-sorted-intersection
9676                (gnus-uncompress-range gnus-newsgroup-killed)
9677                (setq gnus-newsgroup-unselected
9678                      (sort gnus-newsgroup-unselected '<)))
9679               (setq gnus-newsgroup-unreads
9680                     (sort gnus-newsgroup-unreads '<))) t)))
9681     (unless (listp (cdr gnus-newsgroup-killed))
9682       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9683     (let ((headers gnus-newsgroup-headers))
9684       (run-hooks 'gnus-exit-group-hook)
9685       (unless gnus-save-score
9686         (setq gnus-newsgroup-scored nil))
9687       ;; Set the new ranges of read articles.
9688       (gnus-update-read-articles
9689        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9690       ;; Set the current article marks.
9691       (gnus-update-marks)
9692       ;; Do the cross-ref thing.
9693       (when gnus-use-cross-reference
9694         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9695       ;; Do adaptive scoring, and possibly save score files.
9696       (when gnus-newsgroup-adaptive
9697         (gnus-score-adaptive))
9698       (when gnus-use-scoring
9699         (gnus-score-save))
9700       ;; Do not switch windows but change the buffer to work.
9701       (set-buffer gnus-group-buffer)
9702       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9703           (gnus-group-update-group group)))))
9704
9705 (defun gnus-summary-exit (&optional temporary)
9706   "Exit reading current newsgroup, and then return to group selection mode.
9707 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9708   (interactive)
9709   (gnus-set-global-variables)
9710   (gnus-kill-save-kill-buffer)
9711   (let* ((group gnus-newsgroup-name)
9712          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9713          (mode major-mode)
9714          (buf (current-buffer)))
9715     (run-hooks 'gnus-summary-prepare-exit-hook)
9716     ;; If we have several article buffers, we kill them at exit.
9717     (unless gnus-single-article-buffer
9718       (gnus-kill-buffer gnus-article-buffer)
9719       (gnus-kill-buffer gnus-original-article-buffer)
9720       (setq gnus-article-current nil))
9721     (when gnus-use-cache
9722       (gnus-cache-possibly-remove-articles)
9723       (gnus-cache-save-buffers))
9724     (when gnus-use-trees
9725       (gnus-tree-close group))
9726     ;; Make all changes in this group permanent.
9727     (unless quit-config
9728       (gnus-summary-update-info))
9729     (gnus-close-group group)
9730     ;; Make sure where I was, and go to next newsgroup.
9731     (set-buffer gnus-group-buffer)
9732     (unless quit-config
9733       (gnus-group-jump-to-group group)
9734       (gnus-group-next-unread-group 1))
9735     (run-hooks 'gnus-summary-exit-hook)
9736     (unless gnus-single-article-buffer
9737       (setq gnus-article-current nil))
9738     (if temporary
9739         nil                             ;Nothing to do.
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       (set-buffer buf)
9746       (if (not gnus-kill-summary-on-exit)
9747           (gnus-deaden-summary)
9748         ;; We set all buffer-local variables to nil.  It is unclear why
9749         ;; this is needed, but if we don't, buffer-local variables are
9750         ;; not garbage-collected, it seems.  This would the lead to en
9751         ;; ever-growing Emacs.
9752         (gnus-summary-clear-local-variables)
9753         (when (get-buffer gnus-article-buffer)
9754           (bury-buffer gnus-article-buffer))
9755         ;; We clear the global counterparts of the buffer-local
9756         ;; variables as well, just to be on the safe side.
9757         (gnus-configure-windows 'group 'force)
9758         (gnus-summary-clear-local-variables)
9759         ;; Return to group mode buffer.
9760         (if (eq mode 'gnus-summary-mode)
9761             (gnus-kill-buffer buf)))
9762       (setq gnus-current-select-method gnus-select-method)
9763       (pop-to-buffer gnus-group-buffer)
9764       ;; Clear the current group name.
9765       (if (not quit-config)
9766           (progn
9767             (gnus-group-jump-to-group group)
9768             (gnus-group-next-unread-group 1)
9769             (gnus-configure-windows 'group 'force))
9770         (if (not (buffer-name (car quit-config)))
9771             (gnus-configure-windows 'group 'force)
9772           (set-buffer (car quit-config))
9773           (and (eq major-mode 'gnus-summary-mode)
9774                (gnus-set-global-variables))
9775           (gnus-configure-windows (cdr quit-config))))
9776       (unless quit-config
9777         (setq gnus-newsgroup-name nil)))))
9778
9779 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9780 (defun gnus-summary-exit-no-update (&optional no-questions)
9781   "Quit reading current newsgroup without updating read article info."
9782   (interactive)
9783   (gnus-set-global-variables)
9784   (let* ((group gnus-newsgroup-name)
9785          (quit-config (gnus-group-quit-config group)))
9786     (when (or no-questions
9787               gnus-expert-user
9788               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9789       ;; If we have several article buffers, we kill them at exit.
9790       (unless gnus-single-article-buffer
9791         (gnus-kill-buffer gnus-article-buffer)
9792         (gnus-kill-buffer gnus-original-article-buffer)
9793         (setq gnus-article-current nil))
9794       (if (not gnus-kill-summary-on-exit)
9795           (gnus-deaden-summary)
9796         (gnus-close-group group)
9797         (gnus-summary-clear-local-variables)
9798         (set-buffer gnus-group-buffer)
9799         (gnus-summary-clear-local-variables)
9800         (when (get-buffer gnus-summary-buffer)
9801           (kill-buffer gnus-summary-buffer)))
9802       (unless gnus-single-article-buffer
9803         (setq gnus-article-current nil))
9804       (when gnus-use-trees
9805         (gnus-tree-close group))
9806       (when (get-buffer gnus-article-buffer)
9807         (bury-buffer gnus-article-buffer))
9808       ;; Return to the group buffer.
9809       (gnus-configure-windows 'group 'force)
9810       ;; Clear the current group name.
9811       (setq gnus-newsgroup-name nil)
9812       (when (equal (gnus-group-group-name) group)
9813         (gnus-group-next-unread-group 1))
9814       (when quit-config
9815         (if (not (buffer-name (car quit-config)))
9816             (gnus-configure-windows 'group 'force)
9817           (set-buffer (car quit-config))
9818           (when (eq major-mode 'gnus-summary-mode)
9819             (gnus-set-global-variables))
9820           (gnus-configure-windows (cdr quit-config)))))))
9821
9822 ;;; Dead summaries.
9823
9824 (defvar gnus-dead-summary-mode-map nil)
9825
9826 (if gnus-dead-summary-mode-map
9827     nil
9828   (setq gnus-dead-summary-mode-map (make-keymap))
9829   (suppress-keymap gnus-dead-summary-mode-map)
9830   (substitute-key-definition
9831    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9832   (let ((keys '("\C-d" "\r" "\177")))
9833     (while keys
9834       (define-key gnus-dead-summary-mode-map
9835         (pop keys) 'gnus-summary-wake-up-the-dead))))
9836
9837 (defvar gnus-dead-summary-mode nil
9838   "Minor mode for Gnus summary buffers.")
9839
9840 (defun gnus-dead-summary-mode (&optional arg)
9841   "Minor mode for Gnus summary buffers."
9842   (interactive "P")
9843   (when (eq major-mode 'gnus-summary-mode)
9844     (make-local-variable 'gnus-dead-summary-mode)
9845     (setq gnus-dead-summary-mode
9846           (if (null arg) (not gnus-dead-summary-mode)
9847             (> (prefix-numeric-value arg) 0)))
9848     (when gnus-dead-summary-mode
9849       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9850         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9851       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9852         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9853               minor-mode-map-alist)))))
9854
9855 (defun gnus-deaden-summary ()
9856   "Make the current summary buffer into a dead summary buffer."
9857   ;; Kill any previous dead summary buffer.
9858   (when (and gnus-dead-summary
9859              (buffer-name gnus-dead-summary))
9860     (save-excursion
9861       (set-buffer gnus-dead-summary)
9862       (when gnus-dead-summary-mode
9863         (kill-buffer (current-buffer)))))
9864   ;; Make this the current dead summary.
9865   (setq gnus-dead-summary (current-buffer))
9866   (gnus-dead-summary-mode 1)
9867   (let ((name (buffer-name)))
9868     (when (string-match "Summary" name)
9869       (rename-buffer
9870        (concat (substring name 0 (match-beginning 0)) "Dead "
9871                (substring name (match-beginning 0))) t))))
9872
9873 (defun gnus-kill-or-deaden-summary (buffer)
9874   "Kill or deaden the summary BUFFER."
9875   (cond (gnus-kill-summary-on-exit
9876          (when (and gnus-use-trees
9877                     (and (get-buffer buffer)
9878                          (buffer-name (get-buffer buffer))))
9879            (save-excursion
9880              (set-buffer (get-buffer buffer))
9881              (gnus-tree-close gnus-newsgroup-name)))
9882          (gnus-kill-buffer buffer))
9883         ((and (get-buffer buffer)
9884               (buffer-name (get-buffer buffer)))
9885          (save-excursion
9886            (set-buffer buffer)
9887            (gnus-deaden-summary)))))
9888
9889 (defun gnus-summary-wake-up-the-dead (&rest args)
9890   "Wake up the dead summary buffer."
9891   (interactive)
9892   (gnus-dead-summary-mode -1)
9893   (let ((name (buffer-name)))
9894     (when (string-match "Dead " name)
9895       (rename-buffer
9896        (concat (substring name 0 (match-beginning 0))
9897                (substring name (match-end 0))) t)))
9898   (gnus-message 3 "This dead summary is now alive again"))
9899
9900 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9901 (defun gnus-summary-fetch-faq (&optional faq-dir)
9902   "Fetch the FAQ for the current group.
9903 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9904 in."
9905   (interactive
9906    (list
9907     (if current-prefix-arg
9908         (completing-read
9909          "Faq dir: " (and (listp gnus-group-faq-directory)
9910                           gnus-group-faq-directory)))))
9911   (let (gnus-faq-buffer)
9912     (and (setq gnus-faq-buffer
9913                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9914          (gnus-configure-windows 'summary-faq))))
9915
9916 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9917 (defun gnus-summary-describe-group (&optional force)
9918   "Describe the current newsgroup."
9919   (interactive "P")
9920   (gnus-group-describe-group force gnus-newsgroup-name))
9921
9922 (defun gnus-summary-describe-briefly ()
9923   "Describe summary mode commands briefly."
9924   (interactive)
9925   (gnus-message 6
9926                 (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")))
9927
9928 ;; Walking around group mode buffer from summary mode.
9929
9930 (defun gnus-summary-next-group (&optional no-article target-group backward)
9931   "Exit current newsgroup and then select next unread newsgroup.
9932 If prefix argument NO-ARTICLE is non-nil, no article is selected
9933 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9934 previous group instead."
9935   (interactive "P")
9936   (gnus-set-global-variables)
9937   (let ((current-group gnus-newsgroup-name)
9938         (current-buffer (current-buffer))
9939         entered)
9940     ;; First we semi-exit this group to update Xrefs and all variables.
9941     ;; We can't do a real exit, because the window conf must remain
9942     ;; the same in case the user is prompted for info, and we don't
9943     ;; want the window conf to change before that...
9944     (gnus-summary-exit t)
9945     (while (not entered)
9946       ;; Then we find what group we are supposed to enter.
9947       (set-buffer gnus-group-buffer)
9948       (gnus-group-jump-to-group current-group)
9949       (setq target-group
9950             (or target-group
9951                 (if (eq gnus-keep-same-level 'best)
9952                     (gnus-summary-best-group gnus-newsgroup-name)
9953                   (gnus-summary-search-group backward gnus-keep-same-level))))
9954       (if (not target-group)
9955           ;; There are no further groups, so we return to the group
9956           ;; buffer.
9957           (progn
9958             (gnus-message 5 "Returning to the group buffer")
9959             (setq entered t)
9960             (set-buffer current-buffer)
9961             (gnus-summary-exit))
9962         ;; We try to enter the target group.
9963         (gnus-group-jump-to-group target-group)
9964         (let ((unreads (gnus-group-group-unread)))
9965           (if (and (or (eq t unreads)
9966                        (and unreads (not (zerop unreads))))
9967                    (gnus-summary-read-group
9968                     target-group nil no-article current-buffer))
9969               (setq entered t)
9970             (setq current-group target-group
9971                   target-group nil)))))))
9972
9973 (defun gnus-summary-prev-group (&optional no-article)
9974   "Exit current newsgroup and then select previous unread newsgroup.
9975 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9976   (interactive "P")
9977   (gnus-summary-next-group no-article nil t))
9978
9979 ;; Walking around summary lines.
9980
9981 (defun gnus-summary-first-subject (&optional unread)
9982   "Go to the first unread subject.
9983 If UNREAD is non-nil, go to the first unread article.
9984 Returns the article selected or nil if there are no unread articles."
9985   (interactive "P")
9986   (prog1
9987       (cond
9988        ;; Empty summary.
9989        ((null gnus-newsgroup-data)
9990         (gnus-message 3 "No articles in the group")
9991         nil)
9992        ;; Pick the first article.
9993        ((not unread)
9994         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9995         (gnus-data-number (car gnus-newsgroup-data)))
9996        ;; No unread articles.
9997        ((null gnus-newsgroup-unreads)
9998         (gnus-message 3 "No more unread articles")
9999         nil)
10000        ;; Find the first unread article.
10001        (t
10002         (let ((data gnus-newsgroup-data))
10003           (while (and data
10004                       (not (gnus-data-unread-p (car data))))
10005             (setq data (cdr data)))
10006           (if data
10007               (progn
10008                 (goto-char (gnus-data-pos (car data)))
10009                 (gnus-data-number (car data)))))))
10010     (gnus-summary-position-point)))
10011
10012 (defun gnus-summary-next-subject (n &optional unread dont-display)
10013   "Go to next N'th summary line.
10014 If N is negative, go to the previous N'th subject line.
10015 If UNREAD is non-nil, only unread articles are selected.
10016 The difference between N and the actual number of steps taken is
10017 returned."
10018   (interactive "p")
10019   (let ((backward (< n 0))
10020         (n (abs n)))
10021     (while (and (> n 0)
10022                 (if backward
10023                     (gnus-summary-find-prev unread)
10024                   (gnus-summary-find-next unread)))
10025       (setq n (1- n)))
10026     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10027                                (if unread " unread" "")))
10028     (unless dont-display
10029       (gnus-summary-recenter)
10030       (gnus-summary-position-point))
10031     n))
10032
10033 (defun gnus-summary-next-unread-subject (n)
10034   "Go to next N'th unread summary line."
10035   (interactive "p")
10036   (gnus-summary-next-subject n t))
10037
10038 (defun gnus-summary-prev-subject (n &optional unread)
10039   "Go to previous N'th summary line.
10040 If optional argument UNREAD is non-nil, only unread article is selected."
10041   (interactive "p")
10042   (gnus-summary-next-subject (- n) unread))
10043
10044 (defun gnus-summary-prev-unread-subject (n)
10045   "Go to previous N'th unread summary line."
10046   (interactive "p")
10047   (gnus-summary-next-subject (- n) t))
10048
10049 (defun gnus-summary-goto-subject (article &optional force silent)
10050   "Go the subject line of ARTICLE.
10051 If FORCE, also allow jumping to articles not currently shown."
10052   (let ((b (point))
10053         (data (gnus-data-find article)))
10054     ;; We read in the article if we have to.
10055     (and (not data)
10056          force
10057          (gnus-summary-insert-subject article)
10058          (setq data (gnus-data-find article)))
10059     (goto-char b)
10060     (if (not data)
10061         (progn
10062           (unless silent
10063             (gnus-message 3 "Can't find article %d" article))
10064           nil)
10065       (goto-char (gnus-data-pos data))
10066       article)))
10067
10068 ;; Walking around summary lines with displaying articles.
10069
10070 (defun gnus-summary-expand-window (&optional arg)
10071   "Make the summary buffer take up the entire Emacs frame.
10072 Given a prefix, will force an `article' buffer configuration."
10073   (interactive "P")
10074   (gnus-set-global-variables)
10075   (if arg
10076       (gnus-configure-windows 'article 'force)
10077     (gnus-configure-windows 'summary 'force)))
10078
10079 (defun gnus-summary-display-article (article &optional all-header)
10080   "Display ARTICLE in article buffer."
10081   (gnus-set-global-variables)
10082   (if (null article)
10083       nil
10084     (prog1
10085         (if gnus-summary-display-article-function
10086             (funcall gnus-summary-display-article-function article all-header)
10087           (gnus-article-prepare article all-header))
10088       (run-hooks 'gnus-select-article-hook)
10089       (gnus-summary-recenter)
10090       (gnus-summary-goto-subject article)
10091       (when gnus-use-trees
10092         (gnus-possibly-generate-tree article)
10093         (gnus-highlight-selected-tree article))
10094       ;; Successfully display article.
10095       (gnus-article-set-window-start
10096        (cdr (assq article gnus-newsgroup-bookmarks)))
10097       t)))
10098
10099 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10100   "Select the current article.
10101 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10102 non-nil, the article will be re-fetched even if it already present in
10103 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10104 be displayed."
10105   (let ((article (or article (gnus-summary-article-number)))
10106         (all-headers (not (not all-headers))) ;Must be T or NIL.
10107         gnus-summary-display-article-function
10108         did)
10109     (and (not pseudo)
10110          (gnus-summary-article-pseudo-p article)
10111          (error "This is a pseudo-article."))
10112     (prog1
10113         (save-excursion
10114           (set-buffer gnus-summary-buffer)
10115           (if (or (and gnus-single-article-buffer
10116                        (or (null gnus-current-article)
10117                            (null gnus-article-current)
10118                            (null (get-buffer gnus-article-buffer))
10119                            (not (eq article (cdr gnus-article-current)))
10120                            (not (equal (car gnus-article-current)
10121                                        gnus-newsgroup-name))))
10122                   (and (not gnus-single-article-buffer)
10123                        (or (null gnus-current-article)
10124                            (not (eq gnus-current-article article))))
10125                   force)
10126               ;; The requested article is different from the current article.
10127               (prog1
10128                   (gnus-summary-display-article article all-headers)
10129                 (setq did article))
10130             (if (or all-headers gnus-show-all-headers)
10131                 (gnus-article-show-all-headers))
10132             'old))
10133       (if did
10134           (gnus-article-set-window-start
10135            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10136
10137 (defun gnus-summary-set-current-mark (&optional current-mark)
10138   "Obsolete function."
10139   nil)
10140
10141 (defun gnus-summary-next-article (&optional unread subject backward push)
10142   "Select the next article.
10143 If UNREAD, only unread articles are selected.
10144 If SUBJECT, only articles with SUBJECT are selected.
10145 If BACKWARD, the previous article is selected instead of the next."
10146   (interactive "P")
10147   (gnus-set-global-variables)
10148   (cond
10149    ;; Is there such an article?
10150    ((and (gnus-summary-search-forward unread subject backward)
10151          (or (gnus-summary-display-article (gnus-summary-article-number))
10152              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10153     (gnus-summary-position-point))
10154    ;; If not, we try the first unread, if that is wanted.
10155    ((and subject
10156          gnus-auto-select-same
10157          (or (gnus-summary-first-unread-article)
10158              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10159     (gnus-summary-position-point)
10160     (gnus-message 6 "Wrapped"))
10161    ;; Try to get next/previous article not displayed in this group.
10162    ((and gnus-auto-extend-newsgroup
10163          (not unread) (not subject))
10164     (gnus-summary-goto-article
10165      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10166      nil t))
10167    ;; Go to next/previous group.
10168    (t
10169     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10170         (gnus-summary-jump-to-group gnus-newsgroup-name))
10171     (let ((cmd last-command-char)
10172           (group
10173            (if (eq gnus-keep-same-level 'best)
10174                (gnus-summary-best-group gnus-newsgroup-name)
10175              (gnus-summary-search-group backward gnus-keep-same-level))))
10176       ;; For some reason, the group window gets selected.  We change
10177       ;; it back.
10178       (select-window (get-buffer-window (current-buffer)))
10179       ;; Select next unread newsgroup automagically.
10180       (cond
10181        ((not gnus-auto-select-next)
10182         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10183        ((or (eq gnus-auto-select-next 'quietly)
10184             (and (eq gnus-auto-select-next 'slightly-quietly)
10185                  push)
10186             (and (eq gnus-auto-select-next 'almost-quietly)
10187                  (gnus-summary-last-article-p)))
10188         ;; Select quietly.
10189         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10190             (gnus-summary-exit)
10191           (gnus-message 7 "No more%s articles (%s)..."
10192                         (if unread " unread" "")
10193                         (if group (concat "selecting " group)
10194                           "exiting"))
10195           (gnus-summary-next-group nil group backward)))
10196        (t
10197         (gnus-summary-walk-group-buffer
10198          gnus-newsgroup-name cmd unread backward)))))))
10199
10200 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10201   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10202                       (?\C-p (gnus-group-prev-unread-group 1))))
10203         keve key group ended)
10204     (save-excursion
10205       (set-buffer gnus-group-buffer)
10206       (gnus-summary-jump-to-group from-group)
10207       (setq group
10208             (if (eq gnus-keep-same-level 'best)
10209                 (gnus-summary-best-group gnus-newsgroup-name)
10210               (gnus-summary-search-group backward gnus-keep-same-level))))
10211     (while (not ended)
10212       (gnus-message
10213        5 "No more%s articles%s" (if unread " unread" "")
10214        (if (and group
10215                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10216            (format " (Type %s for %s [%s])"
10217                    (single-key-description cmd) group
10218                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10219          (format " (Type %s to exit %s)"
10220                  (single-key-description cmd)
10221                  gnus-newsgroup-name)))
10222       ;; Confirm auto selection.
10223       (setq key (car (setq keve (gnus-read-event-char))))
10224       (setq ended t)
10225       (cond
10226        ((assq key keystrokes)
10227         (let ((obuf (current-buffer)))
10228           (switch-to-buffer gnus-group-buffer)
10229           (and group
10230                (gnus-group-jump-to-group group))
10231           (eval (cadr (assq key keystrokes)))
10232           (setq group (gnus-group-group-name))
10233           (switch-to-buffer obuf))
10234         (setq ended nil))
10235        ((equal key cmd)
10236         (if (or (not group)
10237                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10238             (gnus-summary-exit)
10239           (gnus-summary-next-group nil group backward)))
10240        (t
10241         (push (cdr keve) unread-command-events))))))
10242
10243 (defun gnus-read-event-char ()
10244   "Get the next event."
10245   (let ((event (read-event)))
10246     (cons (and (numberp event) event) event)))
10247
10248 (defun gnus-summary-next-unread-article ()
10249   "Select unread article after current one."
10250   (interactive)
10251   (gnus-summary-next-article t (and gnus-auto-select-same
10252                                     (gnus-summary-article-subject))))
10253
10254 (defun gnus-summary-prev-article (&optional unread subject)
10255   "Select the article after the current one.
10256 If UNREAD is non-nil, only unread articles are selected."
10257   (interactive "P")
10258   (gnus-summary-next-article unread subject t))
10259
10260 (defun gnus-summary-prev-unread-article ()
10261   "Select unred article before current one."
10262   (interactive)
10263   (gnus-summary-prev-article t (and gnus-auto-select-same
10264                                     (gnus-summary-article-subject))))
10265
10266 (defun gnus-summary-next-page (&optional lines circular)
10267   "Show next page of the selected article.
10268 If at the end of the current article, select the next article.
10269 LINES says how many lines should be scrolled up.
10270
10271 If CIRCULAR is non-nil, go to the start of the article instead of
10272 selecting the next article when reaching the end of the current
10273 article."
10274   (interactive "P")
10275   (setq gnus-summary-buffer (current-buffer))
10276   (gnus-set-global-variables)
10277   (let ((article (gnus-summary-article-number))
10278         (endp nil))
10279     (gnus-configure-windows 'article)
10280     (if (or (null gnus-current-article)
10281             (null gnus-article-current)
10282             (/= article (cdr gnus-article-current))
10283             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10284         ;; Selected subject is different from current article's.
10285         (gnus-summary-display-article article)
10286       (gnus-eval-in-buffer-window
10287        gnus-article-buffer
10288        (setq endp (gnus-article-next-page lines)))
10289       (if endp
10290           (cond (circular
10291                  (gnus-summary-beginning-of-article))
10292                 (lines
10293                  (gnus-message 3 "End of message"))
10294                 ((null lines)
10295                  (if (and (eq gnus-summary-goto-unread 'never)
10296                           (not (gnus-summary-last-article-p article)))
10297                      (gnus-summary-next-article)
10298                    (gnus-summary-next-unread-article))))))
10299     (gnus-summary-recenter)
10300     (gnus-summary-position-point)))
10301
10302 (defun gnus-summary-prev-page (&optional lines)
10303   "Show previous page of selected article.
10304 Argument LINES specifies lines to be scrolled down."
10305   (interactive "P")
10306   (gnus-set-global-variables)
10307   (let ((article (gnus-summary-article-number)))
10308     (gnus-configure-windows 'article)
10309     (if (or (null gnus-current-article)
10310             (null gnus-article-current)
10311             (/= article (cdr gnus-article-current))
10312             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10313         ;; Selected subject is different from current article's.
10314         (gnus-summary-display-article article)
10315       (gnus-summary-recenter)
10316       (gnus-eval-in-buffer-window gnus-article-buffer
10317                                   (gnus-article-prev-page lines))))
10318   (gnus-summary-position-point))
10319
10320 (defun gnus-summary-scroll-up (lines)
10321   "Scroll up (or down) one line current article.
10322 Argument LINES specifies lines to be scrolled up (or down if negative)."
10323   (interactive "p")
10324   (gnus-set-global-variables)
10325   (gnus-configure-windows 'article)
10326   (gnus-summary-show-thread)
10327   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10328     (gnus-eval-in-buffer-window
10329      gnus-article-buffer
10330      (cond ((> lines 0)
10331             (if (gnus-article-next-page lines)
10332                 (gnus-message 3 "End of message")))
10333            ((< lines 0)
10334             (gnus-article-prev-page (- lines))))))
10335   (gnus-summary-recenter)
10336   (gnus-summary-position-point))
10337
10338 (defun gnus-summary-next-same-subject ()
10339   "Select next article which has the same subject as current one."
10340   (interactive)
10341   (gnus-set-global-variables)
10342   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10343
10344 (defun gnus-summary-prev-same-subject ()
10345   "Select previous article which has the same subject as current one."
10346   (interactive)
10347   (gnus-set-global-variables)
10348   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10349
10350 (defun gnus-summary-next-unread-same-subject ()
10351   "Select next unread article which has the same subject as current one."
10352   (interactive)
10353   (gnus-set-global-variables)
10354   (gnus-summary-next-article t (gnus-summary-article-subject)))
10355
10356 (defun gnus-summary-prev-unread-same-subject ()
10357   "Select previous unread article which has the same subject as current one."
10358   (interactive)
10359   (gnus-set-global-variables)
10360   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10361
10362 (defun gnus-summary-first-unread-article ()
10363   "Select the first unread article.
10364 Return nil if there are no unread articles."
10365   (interactive)
10366   (gnus-set-global-variables)
10367   (prog1
10368       (if (gnus-summary-first-subject t)
10369           (progn
10370             (gnus-summary-show-thread)
10371             (gnus-summary-first-subject t)
10372             (gnus-summary-display-article (gnus-summary-article-number))))
10373     (gnus-summary-position-point)))
10374
10375 (defun gnus-summary-best-unread-article ()
10376   "Select the unread article with the highest score."
10377   (interactive)
10378   (gnus-set-global-variables)
10379   (let ((best -1000000)
10380         (data gnus-newsgroup-data)
10381         article score)
10382     (while data
10383       (and (gnus-data-unread-p (car data))
10384            (> (setq score
10385                     (gnus-summary-article-score (gnus-data-number (car data))))
10386               best)
10387            (setq best score
10388                  article (gnus-data-number (car data))))
10389       (setq data (cdr data)))
10390     (prog1
10391         (if article
10392             (gnus-summary-goto-article article)
10393           (error "No unread articles"))
10394       (gnus-summary-position-point))))
10395
10396 (defun gnus-summary-last-subject ()
10397   "Go to the last displayed subject line in the group."
10398   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10399     (when article
10400       (gnus-summary-goto-subject article))))
10401
10402 (defun gnus-summary-goto-article (article &optional all-headers force)
10403   "Fetch ARTICLE and display it if it exists.
10404 If ALL-HEADERS is non-nil, no header lines are hidden."
10405   (interactive
10406    (list
10407     (string-to-int
10408      (completing-read
10409       "Article number: "
10410       (mapcar (lambda (number) (list (int-to-string number)))
10411               gnus-newsgroup-limit)))
10412     current-prefix-arg
10413     t))
10414   (prog1
10415       (if (gnus-summary-goto-subject article force)
10416           (gnus-summary-display-article article all-headers)
10417         (gnus-message 4 "Couldn't go to article %s" article) nil)
10418     (gnus-summary-position-point)))
10419
10420 (defun gnus-summary-goto-last-article ()
10421   "Go to the previously read article."
10422   (interactive)
10423   (prog1
10424       (and gnus-last-article
10425            (gnus-summary-goto-article gnus-last-article))
10426     (gnus-summary-position-point)))
10427
10428 (defun gnus-summary-pop-article (number)
10429   "Pop one article off the history and go to the previous.
10430 NUMBER articles will be popped off."
10431   (interactive "p")
10432   (let (to)
10433     (setq gnus-newsgroup-history
10434           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10435     (if to
10436         (gnus-summary-goto-article (car to))
10437       (error "Article history empty")))
10438   (gnus-summary-position-point))
10439
10440 ;; Summary commands and functions for limiting the summary buffer.
10441
10442 (defun gnus-summary-limit-to-articles (n)
10443   "Limit the summary buffer to the next N articles.
10444 If not given a prefix, use the process marked articles instead."
10445   (interactive "P")
10446   (gnus-set-global-variables)
10447   (prog1
10448       (let ((articles (gnus-summary-work-articles n)))
10449         (setq gnus-newsgroup-processable nil)
10450         (gnus-summary-limit articles))
10451     (gnus-summary-position-point)))
10452
10453 (defun gnus-summary-pop-limit (&optional total)
10454   "Restore the previous limit.
10455 If given a prefix, remove all limits."
10456   (interactive "P")
10457   (gnus-set-global-variables)
10458   (when total 
10459     (setq gnus-newsgroup-limits
10460           (list (mapcar (lambda (h) (mail-header-number h))
10461                         gnus-newsgroup-headers))))
10462   (unless gnus-newsgroup-limits
10463     (error "No limit to pop"))
10464   (prog1
10465       (gnus-summary-limit nil 'pop)
10466     (gnus-summary-position-point)))
10467
10468 (defun gnus-summary-limit-to-subject (subject &optional header)
10469   "Limit the summary buffer to articles that have subjects that match a regexp."
10470   (interactive "sRegexp: ")
10471   (unless header
10472     (setq header "subject"))
10473   (when (not (equal "" subject))
10474     (prog1
10475         (let ((articles (gnus-summary-find-matching
10476                          (or header "subject") subject 'all)))
10477           (or articles (error "Found no matches for \"%s\"" subject))
10478           (gnus-summary-limit articles))
10479       (gnus-summary-position-point))))
10480
10481 (defun gnus-summary-limit-to-author (from)
10482   "Limit the summary buffer to articles that have authors that match a regexp."
10483   (interactive "sRegexp: ")
10484   (gnus-summary-limit-to-subject from "from"))
10485
10486 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10487 (make-obsolete
10488  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10489
10490 (defun gnus-summary-limit-to-unread (&optional all)
10491   "Limit the summary buffer to articles that are not marked as read.
10492 If ALL is non-nil, limit strictly to unread articles."
10493   (interactive "P")
10494   (if all
10495       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10496     (gnus-summary-limit-to-marks
10497      ;; Concat all the marks that say that an article is read and have
10498      ;; those removed.
10499      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10500            gnus-killed-mark gnus-kill-file-mark
10501            gnus-low-score-mark gnus-expirable-mark
10502            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10503      'reverse)))
10504
10505 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10506 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10507
10508 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10509   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10510 If REVERSE, limit the summary buffer to articles that are not marked
10511 with MARKS.  MARKS can either be a string of marks or a list of marks.
10512 Returns how many articles were removed."
10513   (interactive "sMarks: ")
10514   (gnus-set-global-variables)
10515   (prog1
10516       (let ((data gnus-newsgroup-data)
10517             (marks (if (listp marks) marks
10518                      (append marks nil))) ; Transform to list.
10519             articles)
10520         (while data
10521           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10522                  (memq (gnus-data-mark (car data)) marks))
10523                (setq articles (cons (gnus-data-number (car data)) articles)))
10524           (setq data (cdr data)))
10525         (gnus-summary-limit articles))
10526     (gnus-summary-position-point)))
10527
10528 (defun gnus-summary-limit-to-score (&optional score)
10529   "Limit to articles with score at or above SCORE."
10530   (interactive "P")
10531   (gnus-set-global-variables)
10532   (setq score (if score
10533                   (prefix-numeric-value score)
10534                 (or gnus-summary-default-score 0)))
10535   (let ((data gnus-newsgroup-data)
10536         articles)
10537     (while data
10538       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10539                 score)
10540         (push (gnus-data-number (car data)) articles))
10541       (setq data (cdr data)))
10542     (prog1
10543         (gnus-summary-limit articles)
10544       (gnus-summary-position-point))))
10545
10546 (defun gnus-summary-limit-include-dormant ()
10547   "Display all the hidden articles that are marked as dormant."
10548   (interactive)
10549   (gnus-set-global-variables)
10550   (or gnus-newsgroup-dormant
10551       (error "There are no dormant articles in this group"))
10552   (prog1
10553       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10554     (gnus-summary-position-point)))
10555
10556 (defun gnus-summary-limit-exclude-dormant ()
10557   "Hide all dormant articles."
10558   (interactive)
10559   (gnus-set-global-variables)
10560   (prog1
10561       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10562     (gnus-summary-position-point)))
10563
10564 (defun gnus-summary-limit-exclude-childless-dormant ()
10565   "Hide all dormant articles that have no children."
10566   (interactive)
10567   (gnus-set-global-variables)
10568   (let ((data (gnus-data-list t))
10569         articles d children)
10570     ;; Find all articles that are either not dormant or have
10571     ;; children.
10572     (while (setq d (pop data))
10573       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10574                 (and (setq children 
10575                            (gnus-article-children (gnus-data-number d)))
10576                      (let (found)
10577                        (while children
10578                          (when (memq (car children) articles)
10579                            (setq children nil
10580                                  found t))
10581                          (pop children))
10582                        found)))
10583         (push (gnus-data-number d) articles)))
10584     ;; Do the limiting.
10585     (prog1
10586         (gnus-summary-limit articles)
10587       (gnus-summary-position-point))))
10588
10589 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10590   "Mark all unread excluded articles as read.
10591 If ALL, mark even excluded ticked and dormants as read."
10592   (interactive "P")
10593   (let ((articles (gnus-sorted-complement
10594                    (sort
10595                     (mapcar (lambda (h) (mail-header-number h))
10596                             gnus-newsgroup-headers)
10597                     '<)
10598                    (sort gnus-newsgroup-limit '<)))
10599         article)
10600     (setq gnus-newsgroup-unreads nil)
10601     (if all
10602         (setq gnus-newsgroup-dormant nil
10603               gnus-newsgroup-marked nil
10604               gnus-newsgroup-reads
10605               (nconc
10606                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10607                gnus-newsgroup-reads))
10608       (while (setq article (pop articles))
10609         (unless (or (memq article gnus-newsgroup-dormant)
10610                     (memq article gnus-newsgroup-marked))
10611           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10612
10613 (defun gnus-summary-limit (articles &optional pop)
10614   (if pop
10615       ;; We pop the previous limit off the stack and use that.
10616       (setq articles (car gnus-newsgroup-limits)
10617             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10618     ;; We use the new limit, so we push the old limit on the stack.
10619     (setq gnus-newsgroup-limits
10620           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10621   ;; Set the limit.
10622   (setq gnus-newsgroup-limit articles)
10623   (let ((total (length gnus-newsgroup-data))
10624         (data (gnus-data-find-list (gnus-summary-article-number)))
10625         found)
10626     ;; This will do all the work of generating the new summary buffer
10627     ;; according to the new limit.
10628     (gnus-summary-prepare)
10629     ;; Hide any threads, possibly.
10630     (and gnus-show-threads
10631          gnus-thread-hide-subtree
10632          (gnus-summary-hide-all-threads))
10633     ;; Try to return to the article you were at, or one in the
10634     ;; neighborhood.
10635     (if data
10636         ;; We try to find some article after the current one.
10637         (while data
10638           (and (gnus-summary-goto-subject
10639                 (gnus-data-number (car data)) nil t)
10640                (setq data nil
10641                      found t))
10642           (setq data (cdr data))))
10643     (or found
10644         ;; If there is no data, that means that we were after the last
10645         ;; article.  The same goes when we can't find any articles
10646         ;; after the current one.
10647         (progn
10648           (goto-char (point-max))
10649           (gnus-summary-find-prev)))
10650     ;; We return how many articles were removed from the summary
10651     ;; buffer as a result of the new limit.
10652     (- total (length gnus-newsgroup-data))))
10653
10654 (defsubst gnus-cut-thread (thread)
10655   "Go forwards in the thread until we find an article that we want to display."
10656   (when (eq gnus-fetch-old-headers 'some)
10657     ;; Deal with old-fetched headers.
10658     (while (and thread
10659                 (memq (mail-header-number (car thread)) 
10660                       gnus-newsgroup-ancient)
10661                 (<= (length (cdr thread)) 1))
10662       (setq thread (cadr thread))))
10663   ;; Deal with sparse threads.
10664   (when (or (eq gnus-build-sparse-threads 'some)
10665             (eq gnus-build-sparse-threads 'more))
10666     (while (and thread
10667                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10668                 (= (length (cdr thread)) 1))
10669       (setq thread (cadr thread))))
10670   thread)
10671
10672 (defun gnus-cut-threads (threads)
10673   "Cut off all uninteresting articles from the beginning of threads."
10674   (when (or (eq gnus-fetch-old-headers 'some)
10675             (eq gnus-build-sparse-threads 'some)
10676             (eq gnus-build-sparse-threads 'more))
10677     (let ((th threads))
10678       (while th
10679         (setcar th (gnus-cut-thread (car th)))
10680         (setq th (cdr th)))))
10681   ;; Remove nixed out threads.
10682   (delq nil threads))
10683
10684 (defun gnus-summary-initial-limit (&optional show-if-empty)
10685   "Figure out what the initial limit is supposed to be on group entry.
10686 This entails weeding out unwanted dormants, low-scored articles,
10687 fetch-old-headers verbiage, and so on."
10688   ;; Most groups have nothing to remove.
10689   (if (or gnus-inhibit-limiting
10690           (and (null gnus-newsgroup-dormant)
10691                (not (eq gnus-fetch-old-headers 'some))
10692                (null gnus-summary-expunge-below)
10693                (not (eq gnus-build-sparse-threads 'some))
10694                (not (eq gnus-build-sparse-threads 'more))
10695                (null gnus-thread-expunge-below)
10696                (not gnus-use-nocem)))
10697       () ; Do nothing.
10698     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10699     (setq gnus-newsgroup-limit nil)
10700     (mapatoms
10701      (lambda (node)
10702        (unless (car (symbol-value node))
10703          ;; These threads have no parents -- they are roots.
10704          (let ((nodes (cdr (symbol-value node)))
10705                thread)
10706            (while nodes
10707              (if (and gnus-thread-expunge-below
10708                       (< (gnus-thread-total-score (car nodes))
10709                          gnus-thread-expunge-below))
10710                  (gnus-expunge-thread (pop nodes))
10711                (setq thread (pop nodes))
10712                (gnus-summary-limit-children thread))))))
10713      gnus-newsgroup-dependencies)
10714     ;; If this limitation resulted in an empty group, we might
10715     ;; pop the previous limit and use it instead.
10716     (when (and (not gnus-newsgroup-limit)
10717                show-if-empty)
10718       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10719     gnus-newsgroup-limit))
10720
10721 (defun gnus-summary-limit-children (thread)
10722   "Return 1 if this subthread is visible and 0 if it is not."
10723   ;; First we get the number of visible children to this thread.  This
10724   ;; is done by recursing down the thread using this function, so this
10725   ;; will really go down to a leaf article first, before slowly
10726   ;; working its way up towards the root.
10727   (when thread
10728     (let ((children
10729            (if (cdr thread)
10730                (apply '+ (mapcar 'gnus-summary-limit-children
10731                                  (cdr thread)))
10732              0))
10733           (number (mail-header-number (car thread)))
10734           score)
10735       (if (or
10736            ;; If this article is dormant and has absolutely no visible
10737            ;; children, then this article isn't visible.
10738            (and (memq number gnus-newsgroup-dormant)
10739                 (= children 0))
10740            ;; If this is a "fetch-old-headered" and there is only one
10741            ;; visible child (or less), then we don't want this article.
10742            (and (eq gnus-fetch-old-headers 'some)
10743                 (memq number gnus-newsgroup-ancient)
10744                 (zerop children))
10745            ;; If this is a sparsely inserted article with no children,
10746            ;; we don't want it.
10747            (and (eq gnus-build-sparse-threads 'some)
10748                 (memq number gnus-newsgroup-sparse)
10749                 (zerop children))
10750            ;; If we use expunging, and this article is really
10751            ;; low-scored, then we don't want this article.
10752            (when (and gnus-summary-expunge-below
10753                       (< (setq score
10754                                (or (cdr (assq number gnus-newsgroup-scored))
10755                                    gnus-summary-default-score))
10756                          gnus-summary-expunge-below))
10757              ;; We increase the expunge-tally here, but that has
10758              ;; nothing to do with the limits, really.
10759              (incf gnus-newsgroup-expunged-tally)
10760              ;; We also mark as read here, if that's wanted.
10761              (when (and gnus-summary-mark-below
10762                         (< score gnus-summary-mark-below))
10763                (setq gnus-newsgroup-unreads
10764                      (delq number gnus-newsgroup-unreads))
10765                (if gnus-newsgroup-auto-expire
10766                    (push number gnus-newsgroup-expirable)
10767                  (push (cons number gnus-low-score-mark)
10768                        gnus-newsgroup-reads)))
10769              t)
10770            (and gnus-use-nocem
10771                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10772           ;; Nope, invisible article.
10773           0
10774         ;; Ok, this article is to be visible, so we add it to the limit
10775         ;; and return 1.
10776         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10777         1))))
10778
10779 (defun gnus-expunge-thread (thread)
10780   "Mark all articles in THREAD as read."
10781   (let* ((number (mail-header-number (car thread))))
10782     (incf gnus-newsgroup-expunged-tally)
10783     ;; We also mark as read here, if that's wanted.
10784     (setq gnus-newsgroup-unreads
10785           (delq number gnus-newsgroup-unreads))
10786     (if gnus-newsgroup-auto-expire
10787         (push number gnus-newsgroup-expirable)
10788       (push (cons number gnus-low-score-mark)
10789             gnus-newsgroup-reads)))
10790   ;; Go recursively through all subthreads.
10791   (mapcar 'gnus-expunge-thread (cdr thread)))
10792
10793 ;; Summary article oriented commands
10794
10795 (defun gnus-summary-refer-parent-article (n)
10796   "Refer parent article N times.
10797 The difference between N and the number of articles fetched is returned."
10798   (interactive "p")
10799   (gnus-set-global-variables)
10800   (while
10801       (and
10802        (> n 0)
10803        (let* ((header (gnus-summary-article-header))
10804               (ref
10805                ;; If we try to find the parent of the currently
10806                ;; displayed article, then we take a look at the actual
10807                ;; References header, since this is slightly more
10808                ;; reliable than the References field we got from the
10809                ;; server.
10810                (if (and (eq (mail-header-number header)
10811                             (cdr gnus-article-current))
10812                         (equal gnus-newsgroup-name
10813                                (car gnus-article-current)))
10814                    (save-excursion
10815                      (set-buffer gnus-original-article-buffer)
10816                      (nnheader-narrow-to-headers)
10817                      (prog1
10818                          (mail-fetch-field "references")
10819                        (widen)))
10820                  ;; It's not the current article, so we take a bet on
10821                  ;; the value we got from the server.
10822                  (mail-header-references header))))
10823          (if (setq ref (or ref (mail-header-references header)))
10824              (or (gnus-summary-refer-article (gnus-parent-id ref))
10825                  (gnus-message 1 "Couldn't find parent"))
10826            (gnus-message 1 "No references in article %d"
10827                          (gnus-summary-article-number))
10828            nil)))
10829     (setq n (1- n)))
10830   (gnus-summary-position-point)
10831   n)
10832
10833 (defun gnus-summary-refer-references ()
10834   "Fetch all articles mentioned in the References header.
10835 Return how many articles were fetched."
10836   (interactive)
10837   (gnus-set-global-variables)
10838   (let ((ref (mail-header-references (gnus-summary-article-header)))
10839         (current (gnus-summary-article-number))
10840         (n 0))
10841     ;; For each Message-ID in the References header...
10842     (while (string-match "<[^>]*>" ref)
10843       (incf n)
10844       ;; ... fetch that article.
10845       (gnus-summary-refer-article
10846        (prog1 (match-string 0 ref)
10847          (setq ref (substring ref (match-end 0))))))
10848     (gnus-summary-goto-subject current)
10849     (gnus-summary-position-point)
10850     n))
10851
10852 (defun gnus-summary-refer-article (message-id)
10853   "Fetch an article specified by MESSAGE-ID."
10854   (interactive "sMessage-ID: ")
10855   (when (and (stringp message-id)
10856              (not (zerop (length message-id))))
10857     ;; Construct the correct Message-ID if necessary.
10858     ;; Suggested by tale@pawl.rpi.edu.
10859     (unless (string-match "^<" message-id)
10860       (setq message-id (concat "<" message-id)))
10861     (unless (string-match ">$" message-id)
10862       (setq message-id (concat message-id ">")))
10863     (let ((header (car (gnus-gethash message-id
10864                                      gnus-newsgroup-dependencies))))
10865       (if header
10866           ;; The article is present in the buffer, to we just go to it.
10867           (gnus-summary-goto-article (mail-header-number header) nil t)
10868         ;; We fetch the article
10869         (let ((gnus-override-method 
10870                (and (gnus-news-group-p gnus-newsgroup-name)
10871                     gnus-refer-article-method))
10872               number)
10873           ;; Start the special refer-article method, if necessary.
10874           (when gnus-refer-article-method
10875             (gnus-check-server gnus-refer-article-method))
10876           ;; Fetch the header, and display the article.
10877           (if (setq number (gnus-summary-insert-subject message-id))
10878               (gnus-summary-select-article nil nil nil number)
10879             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10880
10881 (defun gnus-summary-enter-digest-group (&optional force)
10882   "Enter a digest group based on the current article."
10883   (interactive "P")
10884   (gnus-set-global-variables)
10885   (gnus-summary-select-article)
10886   (let ((name (format "%s-%d"
10887                       (gnus-group-prefixed-name
10888                        gnus-newsgroup-name (list 'nndoc ""))
10889                       gnus-current-article))
10890         (ogroup gnus-newsgroup-name)
10891         (case-fold-search t)
10892         (buf (current-buffer))
10893         dig)
10894     (save-excursion
10895       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10896       (insert-buffer-substring gnus-original-article-buffer)
10897       (narrow-to-region
10898        (goto-char (point-min))
10899        (or (search-forward "\n\n" nil t) (point)))
10900       (goto-char (point-min))
10901       (delete-matching-lines "^\\(Path\\):\\|^From ")
10902       (widen))
10903     (unwind-protect
10904         (if (gnus-group-read-ephemeral-group
10905              name `(nndoc ,name (nndoc-address
10906                                  ,(get-buffer dig))
10907                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10908             ;; Make all postings to this group go to the parent group.
10909             (nconc (gnus-info-params (gnus-get-info name))
10910                    (list (cons 'to-group ogroup)))
10911           ;; Couldn't select this doc group.
10912           (switch-to-buffer buf)
10913           (gnus-set-global-variables)
10914           (gnus-configure-windows 'summary)
10915           (gnus-message 3 "Article couldn't be entered?"))
10916       (kill-buffer dig))))
10917
10918 (defun gnus-summary-isearch-article (&optional regexp-p)
10919   "Do incremental search forward on the current article.
10920 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10921   (interactive "P")
10922   (gnus-set-global-variables)
10923   (gnus-summary-select-article)
10924   (gnus-configure-windows 'article)
10925   (gnus-eval-in-buffer-window
10926    gnus-article-buffer
10927    (goto-char (point-min))
10928    (isearch-forward regexp-p)))
10929
10930 (defun gnus-summary-search-article-forward (regexp &optional backward)
10931   "Search for an article containing REGEXP forward.
10932 If BACKWARD, search backward instead."
10933   (interactive
10934    (list (read-string
10935           (format "Search article %s (regexp%s): "
10936                   (if current-prefix-arg "backward" "forward")
10937                   (if gnus-last-search-regexp
10938                       (concat ", default " gnus-last-search-regexp)
10939                     "")))
10940          current-prefix-arg))
10941   (gnus-set-global-variables)
10942   (if (string-equal regexp "")
10943       (setq regexp (or gnus-last-search-regexp ""))
10944     (setq gnus-last-search-regexp regexp))
10945   (if (gnus-summary-search-article regexp backward)
10946       (gnus-article-set-window-start
10947        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10948     (error "Search failed: \"%s\"" regexp)))
10949
10950 (defun gnus-summary-search-article-backward (regexp)
10951   "Search for an article containing REGEXP backward."
10952   (interactive
10953    (list (read-string
10954           (format "Search article backward (regexp%s): "
10955                   (if gnus-last-search-regexp
10956                       (concat ", default " gnus-last-search-regexp)
10957                     "")))))
10958   (gnus-summary-search-article-forward regexp 'backward))
10959
10960 (defun gnus-summary-search-article (regexp &optional backward)
10961   "Search for an article containing REGEXP.
10962 Optional argument BACKWARD means do search for backward.
10963 gnus-select-article-hook is not called during the search."
10964   (let ((gnus-select-article-hook nil)  ;Disable hook.
10965         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10966         (re-search
10967          (if backward
10968              (function re-search-backward) (function re-search-forward)))
10969         (found nil)
10970         (last nil))
10971     ;; Hidden thread subtrees must be searched for ,too.
10972     (gnus-summary-show-all-threads)
10973     ;; First of all, search current article.
10974     ;; We don't want to read article again from NNTP server nor reset
10975     ;; current point.
10976     (gnus-summary-select-article)
10977     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10978     (setq last gnus-current-article)
10979     (gnus-eval-in-buffer-window
10980      gnus-article-buffer
10981      (save-restriction
10982        (widen)
10983        ;; Begin search from current point.
10984        (setq found (funcall re-search regexp nil t))))
10985     ;; Then search next articles.
10986     (while (and (not found)
10987                 (gnus-summary-display-article
10988                  (if backward (gnus-summary-find-prev)
10989                    (gnus-summary-find-next))))
10990       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10991       (gnus-eval-in-buffer-window
10992        gnus-article-buffer
10993        (save-restriction
10994          (widen)
10995          (goto-char (if backward (point-max) (point-min)))
10996          (setq found (funcall re-search regexp nil t)))))
10997     (message "")
10998     ;; Adjust article pointer.
10999     (or (eq last gnus-current-article)
11000         (setq gnus-last-article last))
11001     ;; Return T if found such article.
11002     found))
11003
11004 (defun gnus-summary-find-matching (header regexp &optional backward unread
11005                                           not-case-fold)
11006   "Return a list of all articles that match REGEXP on HEADER.
11007 The search stars on the current article and goes forwards unless
11008 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11009 If UNREAD is non-nil, only unread articles will
11010 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11011 in the comparisons."
11012   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11013                 (gnus-data-find-list
11014                  (gnus-summary-article-number) (gnus-data-list backward))))
11015         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11016         (case-fold-search (not not-case-fold))
11017         articles d)
11018     (or (fboundp (intern (concat "mail-header-" header)))
11019         (error "%s is not a valid header" header))
11020     (while data
11021       (setq d (car data))
11022       (and (or (not unread)             ; We want all articles...
11023                (gnus-data-unread-p d))  ; Or just unreads.
11024            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11025            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11026            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11027       (setq data (cdr data)))
11028     (nreverse articles)))
11029
11030 (defun gnus-summary-execute-command (header regexp command &optional backward)
11031   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11032 If HEADER is an empty string (or nil), the match is done on the entire
11033 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11034   (interactive
11035    (list (let ((completion-ignore-case t))
11036            (completing-read
11037             "Header name: "
11038             (mapcar (lambda (string) (list string))
11039                     '("Number" "Subject" "From" "Lines" "Date"
11040                       "Message-ID" "Xref" "References" "Body"))
11041             nil 'require-match))
11042          (read-string "Regexp: ")
11043          (read-key-sequence "Command: ")
11044          current-prefix-arg))
11045   (when (equal header "Body")
11046     (setq header ""))
11047   (gnus-set-global-variables)
11048   ;; Hidden thread subtrees must be searched as well.
11049   (gnus-summary-show-all-threads)
11050   ;; We don't want to change current point nor window configuration.
11051   (save-excursion
11052     (save-window-excursion
11053       (gnus-message 6 "Executing %s..." (key-description command))
11054       ;; We'd like to execute COMMAND interactively so as to give arguments.
11055       (gnus-execute header regexp
11056                     `(lambda () (call-interactively ',(key-binding command)))
11057                     backward)
11058       (gnus-message 6 "Executing %s...done" (key-description command)))))
11059
11060 (defun gnus-summary-beginning-of-article ()
11061   "Scroll the article back to the beginning."
11062   (interactive)
11063   (gnus-set-global-variables)
11064   (gnus-summary-select-article)
11065   (gnus-configure-windows 'article)
11066   (gnus-eval-in-buffer-window
11067    gnus-article-buffer
11068    (widen)
11069    (goto-char (point-min))
11070    (and gnus-break-pages (gnus-narrow-to-page))))
11071
11072 (defun gnus-summary-end-of-article ()
11073   "Scroll to the end of the article."
11074   (interactive)
11075   (gnus-set-global-variables)
11076   (gnus-summary-select-article)
11077   (gnus-configure-windows 'article)
11078   (gnus-eval-in-buffer-window
11079    gnus-article-buffer
11080    (widen)
11081    (goto-char (point-max))
11082    (recenter -3)
11083    (and gnus-break-pages (gnus-narrow-to-page))))
11084
11085 (defun gnus-summary-show-article (&optional arg)
11086   "Force re-fetching of the current article.
11087 If ARG (the prefix) is non-nil, show the raw article without any
11088 article massaging functions being run."
11089   (interactive "P")
11090   (gnus-set-global-variables)
11091   (if (not arg)
11092       ;; Select the article the normal way.
11093       (gnus-summary-select-article nil 'force)
11094     ;; Bind the article treatment functions to nil.
11095     (let ((gnus-have-all-headers t)
11096           gnus-article-display-hook
11097           gnus-article-prepare-hook
11098           gnus-break-pages
11099           gnus-visual)
11100       (gnus-summary-select-article nil 'force)))
11101 ;  (gnus-configure-windows 'article)
11102   (gnus-summary-position-point))
11103
11104 (defun gnus-summary-verbose-headers (&optional arg)
11105   "Toggle permanent full header display.
11106 If ARG is a positive number, turn header display on.
11107 If ARG is a negative number, turn header display off."
11108   (interactive "P")
11109   (gnus-set-global-variables)
11110   (gnus-summary-toggle-header arg)
11111   (setq gnus-show-all-headers
11112         (cond ((or (not (numberp arg))
11113                    (zerop arg))
11114                (not gnus-show-all-headers))
11115               ((natnump arg)
11116                t))))
11117
11118 (defun gnus-summary-toggle-header (&optional arg)
11119   "Show the headers if they are hidden, or hide them if they are shown.
11120 If ARG is a positive number, show the entire header.
11121 If ARG is a negative number, hide the unwanted header lines."
11122   (interactive "P")
11123   (gnus-set-global-variables)
11124   (save-excursion
11125     (set-buffer gnus-article-buffer)
11126     (let* ((buffer-read-only nil)
11127            (inhibit-point-motion-hooks t)
11128            (hidden (text-property-any
11129                     (goto-char (point-min)) (search-forward "\n\n")
11130                     'invisible t))
11131            e)
11132       (goto-char (point-min))
11133       (when (search-forward "\n\n" nil t)
11134         (delete-region (point-min) (1- (point))))
11135       (goto-char (point-min))
11136       (save-excursion
11137         (set-buffer gnus-original-article-buffer)
11138         (goto-char (point-min))
11139         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11140       (insert-buffer-substring gnus-original-article-buffer 1 e)
11141       (let ((gnus-inhibit-hiding t))
11142         (run-hooks 'gnus-article-display-hook))
11143       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11144           (gnus-article-hide-headers)))))
11145
11146 (defun gnus-summary-show-all-headers ()
11147   "Make all header lines visible."
11148   (interactive)
11149   (gnus-set-global-variables)
11150   (gnus-article-show-all-headers))
11151
11152 (defun gnus-summary-toggle-mime (&optional arg)
11153   "Toggle MIME processing.
11154 If ARG is a positive number, turn MIME processing on."
11155   (interactive "P")
11156   (gnus-set-global-variables)
11157   (setq gnus-show-mime
11158         (if (null arg) (not gnus-show-mime)
11159           (> (prefix-numeric-value arg) 0)))
11160   (gnus-summary-select-article t 'force))
11161
11162 (defun gnus-summary-caesar-message (&optional arg)
11163   "Caesar rotate the current article by 13.
11164 The numerical prefix specifies how manu places to rotate each letter
11165 forward."
11166   (interactive "P")
11167   (gnus-set-global-variables)
11168   (gnus-summary-select-article)
11169   (let ((mail-header-separator ""))
11170     (gnus-eval-in-buffer-window
11171      gnus-article-buffer
11172      (save-restriction
11173        (widen)
11174        (let ((start (window-start)))
11175          (news-caesar-buffer-body arg)
11176          (set-window-start (get-buffer-window (current-buffer)) start))))))
11177
11178 (defun gnus-summary-stop-page-breaking ()
11179   "Stop page breaking in the current article."
11180   (interactive)
11181   (gnus-set-global-variables)
11182   (gnus-summary-select-article)
11183   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11184
11185 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11186   "Move the current article to a different newsgroup.
11187 If N is a positive number, move the N next articles.
11188 If N is a negative number, move the N previous articles.
11189 If N is nil and any articles have been marked with the process mark,
11190 move those articles instead.
11191 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11192 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11193 re-spool using this method.
11194
11195 For this function to work, both the current newsgroup and the
11196 newsgroup that you want to move to have to support the `request-move'
11197 and `request-accept' functions."
11198   (interactive "P")
11199   (unless action (setq action 'move))
11200   (gnus-set-global-variables)
11201   ;; Check whether the source group supports the required functions.
11202   (cond ((and (eq action 'move)
11203               (not (gnus-check-backend-function
11204                     'request-move-article gnus-newsgroup-name)))
11205          (error "The current group does not support article moving"))
11206         ((and (eq action 'crosspost)
11207               (not (gnus-check-backend-function
11208                     'request-replace-article gnus-newsgroup-name)))
11209          (error "The current group does not support article editing")))
11210   (let ((articles (gnus-summary-work-articles n))
11211         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11212         (names '((move "move" "Moving")
11213                  (copy "copy" "Copying")
11214                  (crosspost "crosspost" "Crossposting")))
11215         (copy-buf (save-excursion
11216                     (nnheader-set-temp-buffer " *copy article*")))
11217         art-group to-method new-xref article to-groups)
11218     (unless (assq action names)
11219       (error "Unknown action %s" action))
11220     ;; Read the newsgroup name.
11221     (when (and (not to-newsgroup)
11222                (not select-method))
11223       (setq to-newsgroup
11224             (gnus-read-move-group-name
11225              (cadr (assq action names))
11226              gnus-current-move-group articles prefix))
11227       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11228     (setq to-method (or select-method 
11229                         (gnus-find-method-for-group to-newsgroup)))
11230     ;; Check the method we are to move this article to...
11231     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11232         (error "%s does not support article copying" (car to-method)))
11233     (or (gnus-check-server to-method)
11234         (error "Can't open server %s" (car to-method)))
11235     (gnus-message 6 "%s to %s: %s..."
11236                   (caddr (assq action names))
11237                   (or (car select-method) to-newsgroup) articles)
11238     (while articles
11239       (setq article (pop articles))
11240       (setq
11241        art-group
11242        (cond
11243         ;; Move the article.
11244         ((eq action 'move)
11245          (gnus-request-move-article
11246           article                       ; Article to move
11247           gnus-newsgroup-name           ; From newsgrouo
11248           (nth 1 (gnus-find-method-for-group
11249                   gnus-newsgroup-name)) ; Server
11250           (list 'gnus-request-accept-article
11251                 to-newsgroup (list 'quote select-method)
11252                 (not articles))         ; Accept form
11253           (not articles)))              ; Only save nov last time
11254         ;; Copy the article.
11255         ((eq action 'copy)
11256          (save-excursion
11257            (set-buffer copy-buf)
11258            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11259            (gnus-request-accept-article
11260             to-newsgroup select-method (not articles))))
11261         ;; Crosspost the article.
11262         ((eq action 'crosspost)
11263          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11264            (setq new-xref (concat gnus-newsgroup-name ":" article))
11265            (if (and xref (not (string= xref "")))
11266                (progn
11267                  (when (string-match "^Xref: " xref)
11268                    (setq xref (substring xref (match-end 0))))
11269                  (setq new-xref (concat xref " " new-xref)))
11270              (setq new-xref (concat (system-name) " " new-xref)))
11271            (save-excursion
11272              (set-buffer copy-buf)
11273              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11274              (nnheader-replace-header "xref" new-xref)
11275              (gnus-request-accept-article
11276               to-newsgroup select-method (not articles)))))))
11277       (if (not art-group)
11278           (gnus-message 1 "Couldn't %s article %s"
11279                         (cadr (assq action names)) article)
11280         (let* ((entry
11281                 (or
11282                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11283                  (gnus-gethash
11284                   (gnus-group-prefixed-name
11285                    (car art-group)
11286                    (or select-method 
11287                        (gnus-find-method-for-group to-newsgroup)))
11288                   gnus-newsrc-hashtb)))
11289                (info (nth 2 entry))
11290                (to-group (gnus-info-group info)))
11291           ;; Update the group that has been moved to.
11292           (when (and info
11293                      (memq action '(move copy)))
11294             (unless (member to-group to-groups)
11295               (push to-group to-groups))
11296
11297             (unless (memq article gnus-newsgroup-unreads)
11298               (gnus-info-set-read
11299                info (gnus-add-to-range (gnus-info-read info)
11300                                        (list (cdr art-group)))))
11301
11302             ;; Copy any marks over to the new group.
11303             (let ((marks gnus-article-mark-lists)
11304                   (to-article (cdr art-group)))
11305
11306               ;; See whether the article is to be put in the cache.
11307               (when gnus-use-cache
11308                 (gnus-cache-possibly-enter-article
11309                  to-group to-article
11310                  (let ((header (copy-sequence
11311                                 (gnus-summary-article-header article))))
11312                    (mail-header-set-number header to-article)
11313                    header)
11314                  (memq article gnus-newsgroup-marked)
11315                  (memq article gnus-newsgroup-dormant)
11316                  (memq article gnus-newsgroup-unreads)))
11317
11318               (while marks
11319                 (when (memq article (symbol-value
11320                                      (intern (format "gnus-newsgroup-%s"
11321                                                      (caar marks)))))
11322                   ;; If the other group is the same as this group,
11323                   ;; then we have to add the mark to the list.
11324                   (when (equal to-group gnus-newsgroup-name)
11325                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11326                          (cons to-article
11327                                (symbol-value
11328                                 (intern (format "gnus-newsgroup-%s"
11329                                                 (caar marks)))))))
11330                   ;; Copy mark to other group.
11331                   (gnus-add-marked-articles
11332                    to-group (cdar marks) (list to-article) info))
11333                 (setq marks (cdr marks)))))
11334
11335           ;; Update the Xref header in this article to point to
11336           ;; the new crossposted article we have just created.
11337           (when (eq action 'crosspost)
11338             (save-excursion
11339               (set-buffer copy-buf)
11340               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11341               (nnheader-replace-header
11342                "xref" (concat new-xref " " (gnus-group-prefixed-name
11343                                             (car art-group) to-method)
11344                               ":" (cdr art-group)))
11345               (gnus-request-replace-article
11346                article gnus-newsgroup-name (current-buffer)))))
11347
11348         (gnus-summary-goto-subject article)
11349         (when (eq action 'move)
11350           (gnus-summary-mark-article article gnus-canceled-mark)))
11351       (gnus-summary-remove-process-mark article))
11352     ;; Re-activate all groups that have been moved to.
11353     (while to-groups
11354       (gnus-activate-group (pop to-groups)))
11355     
11356     (gnus-kill-buffer copy-buf)
11357     (gnus-summary-position-point)
11358     (gnus-set-mode-line 'summary)))
11359
11360 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11361   "Move the current article to a different newsgroup.
11362 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11363 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11364 re-spool using this method."
11365   (interactive "P")
11366   (gnus-summary-move-article n nil select-method 'copy))
11367
11368 (defun gnus-summary-crosspost-article (&optional n)
11369   "Crosspost the current article to some other group."
11370   (interactive "P")
11371   (gnus-summary-move-article n nil nil 'crosspost))
11372
11373 (defvar gnus-summary-respool-default-method nil
11374   "Default method for respooling an article.  
11375 If nil, use to the current newsgroup method.")
11376
11377 (defun gnus-summary-respool-article (&optional n method)
11378   "Respool the current article.
11379 The article will be squeezed through the mail spooling process again,
11380 which means that it will be put in some mail newsgroup or other
11381 depending on `nnmail-split-methods'.
11382 If N is a positive number, respool the N next articles.
11383 If N is a negative number, respool the N previous articles.
11384 If N is nil and any articles have been marked with the process mark,
11385 respool those articles instead.
11386
11387 Respooling can be done both from mail groups and \"real\" newsgroups.
11388 In the former case, the articles in question will be moved from the
11389 current group into whatever groups they are destined to.  In the
11390 latter case, they will be copied into the relevant groups."
11391   (interactive 
11392    (list current-prefix-arg
11393          (let* ((methods (gnus-methods-using 'respool))
11394                 (methname
11395                  (symbol-name (or gnus-summary-respool-default-method
11396                                   (car (gnus-find-method-for-group
11397                                         gnus-newsgroup-name)))))
11398                 (method
11399                  (gnus-completing-read 
11400                   methname "What backend do you want to use when? "
11401                   methods nil t nil 'gnus-method-history))
11402                 ms)
11403            (cond
11404             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11405              (list (intern method) ""))
11406             ((= 1 (length ms))
11407              (car ms))
11408             (t
11409              (cdr (completing-read 
11410                    "Server name: "
11411                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11412   (gnus-set-global-variables)
11413   (unless method
11414     (error "No method given for respooling"))
11415   (if (assoc (symbol-name
11416               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11417              (gnus-methods-using 'respool))
11418       (gnus-summary-move-article n nil method)
11419     (gnus-summary-copy-article n nil method)))
11420
11421 (defun gnus-summary-import-article (file)
11422   "Import a random file into a mail newsgroup."
11423   (interactive "fImport file: ")
11424   (gnus-set-global-variables)
11425   (let ((group gnus-newsgroup-name)
11426         (now (current-time))
11427         atts lines)
11428     (or (gnus-check-backend-function 'request-accept-article group)
11429         (error "%s does not support article importing" group))
11430     (or (file-readable-p file)
11431         (not (file-regular-p file))
11432         (error "Can't read %s" file))
11433     (save-excursion
11434       (set-buffer (get-buffer-create " *import file*"))
11435       (buffer-disable-undo (current-buffer))
11436       (erase-buffer)
11437       (insert-file-contents file)
11438       (goto-char (point-min))
11439       (unless (nnheader-article-p)
11440         ;; This doesn't look like an article, so we fudge some headers.
11441         (setq atts (file-attributes file)
11442               lines (count-lines (point-min) (point-max)))
11443         (insert "From: " (read-string "From: ") "\n"
11444                 "Subject: " (read-string "Subject: ") "\n"
11445                 "Date: " (timezone-make-date-arpa-standard
11446                           (current-time-string (nth 5 atts))
11447                           (current-time-zone now)
11448                           (current-time-zone now)) "\n"
11449                 "Message-ID: " (gnus-inews-message-id) "\n"
11450                 "Lines: " (int-to-string lines) "\n"
11451                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11452       (gnus-request-accept-article group nil t)
11453       (kill-buffer (current-buffer)))))
11454
11455 (defun gnus-summary-expire-articles ()
11456   "Expire all articles that are marked as expirable in the current group."
11457   (interactive)
11458   (gnus-set-global-variables)
11459   (when (gnus-check-backend-function
11460          'request-expire-articles gnus-newsgroup-name)
11461     ;; This backend supports expiry.
11462     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11463            (expirable (if total
11464                           (gnus-list-of-read-articles gnus-newsgroup-name)
11465                         (setq gnus-newsgroup-expirable
11466                               (sort gnus-newsgroup-expirable '<))))
11467            (expiry-wait (gnus-group-get-parameter
11468                          gnus-newsgroup-name 'expiry-wait))
11469            es)
11470       (when expirable
11471         ;; There are expirable articles in this group, so we run them
11472         ;; through the expiry process.
11473         (gnus-message 6 "Expiring articles...")
11474         ;; The list of articles that weren't expired is returned.
11475         (if expiry-wait
11476             (let ((nnmail-expiry-wait-function nil)
11477                   (nnmail-expiry-wait expiry-wait))
11478               (setq es (gnus-request-expire-articles
11479                         expirable gnus-newsgroup-name)))
11480           (setq es (gnus-request-expire-articles
11481                     expirable gnus-newsgroup-name)))
11482         (or total (setq gnus-newsgroup-expirable es))
11483         ;; We go through the old list of expirable, and mark all
11484         ;; really expired articles as nonexistent.
11485         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11486           (let ((gnus-use-cache nil))
11487             (while expirable
11488               (unless (memq (car expirable) es)
11489                 (when (gnus-data-find (car expirable))
11490                   (gnus-summary-mark-article
11491                    (car expirable) gnus-canceled-mark)))
11492               (setq expirable (cdr expirable)))))
11493         (gnus-message 6 "Expiring articles...done")))))
11494
11495 (defun gnus-summary-expire-articles-now ()
11496   "Expunge all expirable articles in the current group.
11497 This means that *all* articles that are marked as expirable will be
11498 deleted forever, right now."
11499   (interactive)
11500   (gnus-set-global-variables)
11501   (or gnus-expert-user
11502       (gnus-y-or-n-p
11503        "Are you really, really, really sure you want to expunge? ")
11504       (error "Phew!"))
11505   (let ((nnmail-expiry-wait 'immediate)
11506         (nnmail-expiry-wait-function nil))
11507     (gnus-summary-expire-articles)))
11508
11509 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11510 (defun gnus-summary-delete-article (&optional n)
11511   "Delete the N next (mail) articles.
11512 This command actually deletes articles.  This is not a marking
11513 command.  The article will disappear forever from your life, never to
11514 return.
11515 If N is negative, delete backwards.
11516 If N is nil and articles have been marked with the process mark,
11517 delete these instead."
11518   (interactive "P")
11519   (gnus-set-global-variables)
11520   (or (gnus-check-backend-function 'request-expire-articles
11521                                    gnus-newsgroup-name)
11522       (error "The current newsgroup does not support article deletion."))
11523   ;; Compute the list of articles to delete.
11524   (let ((articles (gnus-summary-work-articles n))
11525         not-deleted)
11526     (if (and gnus-novice-user
11527              (not (gnus-y-or-n-p
11528                    (format "Do you really want to delete %s forever? "
11529                            (if (> (length articles) 1) "these articles"
11530                              "this article")))))
11531         ()
11532       ;; Delete the articles.
11533       (setq not-deleted (gnus-request-expire-articles
11534                          articles gnus-newsgroup-name 'force))
11535       (while articles
11536         (gnus-summary-remove-process-mark (car articles))
11537         ;; The backend might not have been able to delete the article
11538         ;; after all.
11539         (or (memq (car articles) not-deleted)
11540             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11541         (setq articles (cdr articles))))
11542     (gnus-summary-position-point)
11543     (gnus-set-mode-line 'summary)
11544     not-deleted))
11545
11546 (defun gnus-summary-edit-article (&optional force)
11547   "Enter into a buffer and edit the current article.
11548 This will have permanent effect only in mail groups.
11549 If FORCE is non-nil, allow editing of articles even in read-only
11550 groups."
11551   (interactive "P")
11552   (save-excursion
11553     (set-buffer gnus-summary-buffer)
11554     (gnus-set-global-variables)
11555     (when (and (not force)
11556                (gnus-group-read-only-p))
11557       (error "The current newsgroup does not support article editing."))
11558     (gnus-summary-select-article t nil t)
11559     (gnus-configure-windows 'article)
11560     (select-window (get-buffer-window gnus-article-buffer))
11561     (gnus-message 6 "C-c C-c to end edits")
11562     (setq buffer-read-only nil)
11563     (text-mode)
11564     (use-local-map (copy-keymap (current-local-map)))
11565     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11566     (buffer-enable-undo)
11567     (widen)
11568     (goto-char (point-min))
11569     (search-forward "\n\n" nil t)))
11570
11571 (defun gnus-summary-edit-article-done ()
11572   "Make edits to the current article permanent."
11573   (interactive)
11574   (if (gnus-group-read-only-p)
11575       (progn
11576         (gnus-summary-edit-article-postpone)
11577         (gnus-error
11578          1 "The current newsgroup does not support article editing."))
11579     (let ((buf (format "%s" (buffer-string))))
11580       (erase-buffer)
11581       (insert buf)
11582       (if (not (gnus-request-replace-article
11583                 (cdr gnus-article-current) (car gnus-article-current)
11584                 (current-buffer)))
11585           (error "Couldn't replace article.")
11586         (gnus-article-mode)
11587         (use-local-map gnus-article-mode-map)
11588         (setq buffer-read-only t)
11589         (buffer-disable-undo (current-buffer))
11590         (gnus-configure-windows 'summary)
11591         (gnus-summary-update-article (cdr gnus-article-current))
11592         (when gnus-use-cache
11593           (gnus-cache-update-article 
11594            (cdr gnus-article-current) (car gnus-article-current))))
11595       (run-hooks 'gnus-article-display-hook)
11596       (and (gnus-visual-p 'summary-highlight 'highlight)
11597            (run-hooks 'gnus-visual-mark-article-hook)))))
11598
11599 (defun gnus-summary-edit-article-postpone ()
11600   "Postpone changes to the current article."
11601   (interactive)
11602   (gnus-article-mode)
11603   (use-local-map gnus-article-mode-map)
11604   (setq buffer-read-only t)
11605   (buffer-disable-undo (current-buffer))
11606   (gnus-configure-windows 'summary)
11607   (and (gnus-visual-p 'summary-highlight 'highlight)
11608        (run-hooks 'gnus-visual-mark-article-hook)))
11609
11610 (defun gnus-summary-respool-query ()
11611   "Query where the respool algorithm would put this article."
11612   (interactive)
11613   (gnus-set-global-variables)
11614   (gnus-summary-select-article)
11615   (save-excursion
11616     (set-buffer gnus-article-buffer)
11617     (save-restriction
11618       (goto-char (point-min))
11619       (search-forward "\n\n")
11620       (narrow-to-region (point-min) (point))
11621       (pp-eval-expression
11622        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11623
11624 ;; Summary score commands.
11625
11626 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11627
11628 (defun gnus-summary-raise-score (n)
11629   "Raise the score of the current article by N."
11630   (interactive "p")
11631   (gnus-set-global-variables)
11632   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11633
11634 (defun gnus-summary-set-score (n)
11635   "Set the score of the current article to N."
11636   (interactive "p")
11637   (gnus-set-global-variables)
11638   (save-excursion
11639     (gnus-summary-show-thread)
11640     (let ((buffer-read-only nil))
11641       ;; Set score.
11642       (gnus-summary-update-mark
11643        (if (= n (or gnus-summary-default-score 0)) ? 
11644          (if (< n (or gnus-summary-default-score 0))
11645              gnus-score-below-mark gnus-score-over-mark)) 'score))
11646     (let* ((article (gnus-summary-article-number))
11647            (score (assq article gnus-newsgroup-scored)))
11648       (if score (setcdr score n)
11649         (setq gnus-newsgroup-scored
11650               (cons (cons article n) gnus-newsgroup-scored))))
11651     (gnus-summary-update-line)))
11652
11653 (defun gnus-summary-current-score ()
11654   "Return the score of the current article."
11655   (interactive)
11656   (gnus-set-global-variables)
11657   (gnus-message 1 "%s" (gnus-summary-article-score)))
11658
11659 ;; Summary marking commands.
11660
11661 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11662   "Mark articles which has the same subject as read, and then select the next.
11663 If UNMARK is positive, remove any kind of mark.
11664 If UNMARK is negative, tick articles."
11665   (interactive "P")
11666   (gnus-set-global-variables)
11667   (if unmark
11668       (setq unmark (prefix-numeric-value unmark)))
11669   (let ((count
11670          (gnus-summary-mark-same-subject
11671           (gnus-summary-article-subject) unmark)))
11672     ;; Select next unread article.  If auto-select-same mode, should
11673     ;; select the first unread article.
11674     (gnus-summary-next-article t (and gnus-auto-select-same
11675                                       (gnus-summary-article-subject)))
11676     (gnus-message 7 "%d article%s marked as %s"
11677                   count (if (= count 1) " is" "s are")
11678                   (if unmark "unread" "read"))))
11679
11680 (defun gnus-summary-kill-same-subject (&optional unmark)
11681   "Mark articles which has the same subject as read.
11682 If UNMARK is positive, remove any kind of mark.
11683 If UNMARK is negative, tick articles."
11684   (interactive "P")
11685   (gnus-set-global-variables)
11686   (if unmark
11687       (setq unmark (prefix-numeric-value unmark)))
11688   (let ((count
11689          (gnus-summary-mark-same-subject
11690           (gnus-summary-article-subject) unmark)))
11691     ;; If marked as read, go to next unread subject.
11692     (if (null unmark)
11693         ;; Go to next unread subject.
11694         (gnus-summary-next-subject 1 t))
11695     (gnus-message 7 "%d articles are marked as %s"
11696                   count (if unmark "unread" "read"))))
11697
11698 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11699   "Mark articles with same SUBJECT as read, and return marked number.
11700 If optional argument UNMARK is positive, remove any kinds of marks.
11701 If optional argument UNMARK is negative, mark articles as unread instead."
11702   (let ((count 1))
11703     (save-excursion
11704       (cond
11705        ((null unmark)                   ; Mark as read.
11706         (while (and
11707                 (progn
11708                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11709                   (gnus-summary-show-thread) t)
11710                 (gnus-summary-find-subject subject))
11711           (setq count (1+ count))))
11712        ((> unmark 0)                    ; Tick.
11713         (while (and
11714                 (progn
11715                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11716                   (gnus-summary-show-thread) t)
11717                 (gnus-summary-find-subject subject))
11718           (setq count (1+ count))))
11719        (t                               ; Mark as unread.
11720         (while (and
11721                 (progn
11722                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11723                   (gnus-summary-show-thread) t)
11724                 (gnus-summary-find-subject subject))
11725           (setq count (1+ count)))))
11726       (gnus-set-mode-line 'summary)
11727       ;; Return the number of marked articles.
11728       count)))
11729
11730 (defun gnus-summary-mark-as-processable (n &optional unmark)
11731   "Set the process mark on the next N articles.
11732 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11733 the process mark instead.  The difference between N and the actual
11734 number of articles marked is returned."
11735   (interactive "p")
11736   (gnus-set-global-variables)
11737   (let ((backward (< n 0))
11738         (n (abs n)))
11739     (while (and
11740             (> n 0)
11741             (if unmark
11742                 (gnus-summary-remove-process-mark
11743                  (gnus-summary-article-number))
11744               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11745             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11746       (setq n (1- n)))
11747     (if (/= 0 n) (gnus-message 7 "No more articles"))
11748     (gnus-summary-recenter)
11749     (gnus-summary-position-point)
11750     n))
11751
11752 (defun gnus-summary-unmark-as-processable (n)
11753   "Remove the process mark from the next N articles.
11754 If N is negative, mark backward instead.  The difference between N and
11755 the actual number of articles marked is returned."
11756   (interactive "p")
11757   (gnus-set-global-variables)
11758   (gnus-summary-mark-as-processable n t))
11759
11760 (defun gnus-summary-unmark-all-processable ()
11761   "Remove the process mark from all articles."
11762   (interactive)
11763   (gnus-set-global-variables)
11764   (save-excursion
11765     (while gnus-newsgroup-processable
11766       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11767   (gnus-summary-position-point))
11768
11769 (defun gnus-summary-mark-as-expirable (n)
11770   "Mark N articles forward as expirable.
11771 If N is negative, mark backward instead.  The difference between N and
11772 the actual number of articles marked is returned."
11773   (interactive "p")
11774   (gnus-set-global-variables)
11775   (gnus-summary-mark-forward n gnus-expirable-mark))
11776
11777 (defun gnus-summary-mark-article-as-replied (article)
11778   "Mark ARTICLE replied and update the summary line."
11779   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11780   (let ((buffer-read-only nil))
11781     (when (gnus-summary-goto-subject article)
11782       (gnus-summary-update-secondary-mark article))))
11783
11784 (defun gnus-summary-set-bookmark (article)
11785   "Set a bookmark in current article."
11786   (interactive (list (gnus-summary-article-number)))
11787   (gnus-set-global-variables)
11788   (if (or (not (get-buffer gnus-article-buffer))
11789           (not gnus-current-article)
11790           (not gnus-article-current)
11791           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11792       (error "No current article selected"))
11793   ;; Remove old bookmark, if one exists.
11794   (let ((old (assq article gnus-newsgroup-bookmarks)))
11795     (if old (setq gnus-newsgroup-bookmarks
11796                   (delq old gnus-newsgroup-bookmarks))))
11797   ;; Set the new bookmark, which is on the form
11798   ;; (article-number . line-number-in-body).
11799   (setq gnus-newsgroup-bookmarks
11800         (cons
11801          (cons article
11802                (save-excursion
11803                  (set-buffer gnus-article-buffer)
11804                  (count-lines
11805                   (min (point)
11806                        (save-excursion
11807                          (goto-char (point-min))
11808                          (search-forward "\n\n" nil t)
11809                          (point)))
11810                   (point))))
11811          gnus-newsgroup-bookmarks))
11812   (gnus-message 6 "A bookmark has been added to the current article."))
11813
11814 (defun gnus-summary-remove-bookmark (article)
11815   "Remove the bookmark from the current article."
11816   (interactive (list (gnus-summary-article-number)))
11817   (gnus-set-global-variables)
11818   ;; Remove old bookmark, if one exists.
11819   (let ((old (assq article gnus-newsgroup-bookmarks)))
11820     (if old
11821         (progn
11822           (setq gnus-newsgroup-bookmarks
11823                 (delq old gnus-newsgroup-bookmarks))
11824           (gnus-message 6 "Removed bookmark."))
11825       (gnus-message 6 "No bookmark in current article."))))
11826
11827 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11828 (defun gnus-summary-mark-as-dormant (n)
11829   "Mark N articles forward as dormant.
11830 If N is negative, mark backward instead.  The difference between N and
11831 the actual number of articles marked is returned."
11832   (interactive "p")
11833   (gnus-set-global-variables)
11834   (gnus-summary-mark-forward n gnus-dormant-mark))
11835
11836 (defun gnus-summary-set-process-mark (article)
11837   "Set the process mark on ARTICLE and update the summary line."
11838   (setq gnus-newsgroup-processable
11839         (cons article
11840               (delq article gnus-newsgroup-processable)))
11841   (when (gnus-summary-goto-subject article)
11842     (gnus-summary-show-thread)
11843     (gnus-summary-update-secondary-mark article)))
11844
11845 (defun gnus-summary-remove-process-mark (article)
11846   "Remove the process mark from ARTICLE and update the summary line."
11847   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11848   (when (gnus-summary-goto-subject article)
11849     (gnus-summary-show-thread)
11850     (gnus-summary-update-secondary-mark article)))
11851
11852 (defun gnus-summary-set-saved-mark (article)
11853   "Set the process mark on ARTICLE and update the summary line."
11854   (push article gnus-newsgroup-saved)
11855   (when (gnus-summary-goto-subject article)
11856     (gnus-summary-update-secondary-mark article)))
11857
11858 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11859   "Mark N articles as read forwards.
11860 If N is negative, mark backwards instead.
11861 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11862 marked as unread.
11863 The difference between N and the actual number of articles marked is
11864 returned."
11865   (interactive "p")
11866   (gnus-set-global-variables)
11867   (let ((backward (< n 0))
11868         (gnus-summary-goto-unread
11869          (and gnus-summary-goto-unread
11870               (not (eq gnus-summary-goto-unread 'never))
11871               (not (memq mark (list gnus-unread-mark
11872                                     gnus-ticked-mark gnus-dormant-mark)))))
11873         (n (abs n))
11874         (mark (or mark gnus-del-mark)))
11875     (while (and (> n 0)
11876                 (gnus-summary-mark-article nil mark no-expire)
11877                 (zerop (gnus-summary-next-subject
11878                         (if backward -1 1)
11879                         (and gnus-summary-goto-unread
11880                              (not (eq gnus-summary-goto-unread 'never)))
11881                         t)))
11882       (setq n (1- n)))
11883     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11884     (gnus-summary-recenter)
11885     (gnus-summary-position-point)
11886     (gnus-set-mode-line 'summary)
11887     n))
11888
11889 (defun gnus-summary-mark-article-as-read (mark)
11890   "Mark the current article quickly as read with MARK."
11891   (let ((article (gnus-summary-article-number)))
11892     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11893     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11894     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11895     (setq gnus-newsgroup-reads
11896           (cons (cons article mark) gnus-newsgroup-reads))
11897     ;; Possibly remove from cache, if that is used.
11898     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11899     ;; Allow the backend to change the mark.
11900     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11901     ;; Check for auto-expiry.
11902     (when (and gnus-newsgroup-auto-expire
11903                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11904                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11905                    (= mark gnus-ancient-mark)
11906                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11907       (setq mark gnus-expirable-mark)
11908       (push article gnus-newsgroup-expirable))
11909     ;; Set the mark in the buffer.
11910     (gnus-summary-update-mark mark 'unread)
11911     t))
11912
11913 (defun gnus-summary-mark-article-as-unread (mark)
11914   "Mark the current article quickly as unread with MARK."
11915   (let ((article (gnus-summary-article-number)))
11916     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11917     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11918     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11919     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11920     (cond ((= mark gnus-ticked-mark)
11921            (push article gnus-newsgroup-marked))
11922           ((= mark gnus-dormant-mark)
11923            (push article gnus-newsgroup-dormant))
11924           (t
11925            (push article gnus-newsgroup-unreads)))
11926     (setq gnus-newsgroup-reads
11927           (delq (assq article gnus-newsgroup-reads)
11928                 gnus-newsgroup-reads))
11929
11930     ;; See whether the article is to be put in the cache.
11931     (and gnus-use-cache
11932          (vectorp (gnus-summary-article-header article))
11933          (save-excursion
11934            (gnus-cache-possibly-enter-article
11935             gnus-newsgroup-name article
11936             (gnus-summary-article-header article)
11937             (= mark gnus-ticked-mark)
11938             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11939
11940     ;; Fix the mark.
11941     (gnus-summary-update-mark mark 'unread)
11942     t))
11943
11944 (defun gnus-summary-mark-article (&optional article mark no-expire)
11945   "Mark ARTICLE with MARK.  MARK can be any character.
11946 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11947 `??' (dormant) and `?E' (expirable).
11948 If MARK is nil, then the default character `?D' is used.
11949 If ARTICLE is nil, then the article on the current line will be
11950 marked."
11951   ;; The mark might be a string.
11952   (and (stringp mark)
11953        (setq mark (aref mark 0)))
11954   ;; If no mark is given, then we check auto-expiring.
11955   (and (not no-expire)
11956        gnus-newsgroup-auto-expire
11957        (or (not mark)
11958            (and (numberp mark)
11959                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11960                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11961                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11962        (setq mark gnus-expirable-mark))
11963   (let* ((mark (or mark gnus-del-mark))
11964          (article (or article (gnus-summary-article-number))))
11965     (or article (error "No article on current line"))
11966     (if (or (= mark gnus-unread-mark)
11967             (= mark gnus-ticked-mark)
11968             (= mark gnus-dormant-mark))
11969         (gnus-mark-article-as-unread article mark)
11970       (gnus-mark-article-as-read article mark))
11971
11972     ;; See whether the article is to be put in the cache.
11973     (and gnus-use-cache
11974          (not (= mark gnus-canceled-mark))
11975          (vectorp (gnus-summary-article-header article))
11976          (save-excursion
11977            (gnus-cache-possibly-enter-article
11978             gnus-newsgroup-name article
11979             (gnus-summary-article-header article)
11980             (= mark gnus-ticked-mark)
11981             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11982
11983     (if (gnus-summary-goto-subject article nil t)
11984         (let ((buffer-read-only nil))
11985           (gnus-summary-show-thread)
11986           ;; Fix the mark.
11987           (gnus-summary-update-mark mark 'unread)
11988           t))))
11989
11990 (defun gnus-summary-update-secondary-mark (article)
11991   "Update the secondary (read, process, cache) mark."
11992   (gnus-summary-update-mark
11993    (cond ((memq article gnus-newsgroup-processable)
11994           gnus-process-mark)
11995          ((memq article gnus-newsgroup-cached)
11996           gnus-cached-mark)
11997          ((memq article gnus-newsgroup-replied)
11998           gnus-replied-mark)
11999          ((memq article gnus-newsgroup-saved)
12000           gnus-saved-mark)
12001          (t gnus-unread-mark))
12002    'replied)
12003   (when (gnus-visual-p 'summary-highlight 'highlight)
12004     (run-hooks 'gnus-summary-update-hook))
12005   t)
12006
12007 (defun gnus-summary-update-mark (mark type)
12008   (beginning-of-line)
12009   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12010         (buffer-read-only nil))
12011     (when (and forward
12012                (<= (+ forward (point)) (point-max)))
12013       ;; Go to the right position on the line.
12014       (goto-char (+ forward (point)))
12015       ;; Replace the old mark with the new mark.
12016       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12017       ;; Optionally update the marks by some user rule.
12018       (when (eq type 'unread)
12019         (gnus-data-set-mark
12020          (gnus-data-find (gnus-summary-article-number)) mark)
12021         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12022
12023 (defun gnus-mark-article-as-read (article &optional mark)
12024   "Enter ARTICLE in the pertinent lists and remove it from others."
12025   ;; Make the article expirable.
12026   (let ((mark (or mark gnus-del-mark)))
12027     (if (= mark gnus-expirable-mark)
12028         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12029       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12030     ;; Remove from unread and marked lists.
12031     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12032     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12033     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12034     (push (cons article mark) gnus-newsgroup-reads)
12035     ;; Possibly remove from cache, if that is used.
12036     (when gnus-use-cache
12037       (gnus-cache-enter-remove-article article))))
12038
12039 (defun gnus-mark-article-as-unread (article &optional mark)
12040   "Enter ARTICLE in the pertinent lists and remove it from others."
12041   (let ((mark (or mark gnus-ticked-mark)))
12042     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12043     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12044     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12045     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12046     (cond ((= mark gnus-ticked-mark)
12047            (push article gnus-newsgroup-marked))
12048           ((= mark gnus-dormant-mark)
12049            (push article gnus-newsgroup-dormant))
12050           (t
12051            (push article gnus-newsgroup-unreads)))
12052     (setq gnus-newsgroup-reads
12053           (delq (assq article gnus-newsgroup-reads)
12054                 gnus-newsgroup-reads))))
12055
12056 (defalias 'gnus-summary-mark-as-unread-forward
12057   'gnus-summary-tick-article-forward)
12058 (make-obsolete 'gnus-summary-mark-as-unread-forward
12059                'gnus-summary-tick-article-forward)
12060 (defun gnus-summary-tick-article-forward (n)
12061   "Tick N articles forwards.
12062 If N is negative, tick backwards instead.
12063 The difference between N and the number of articles ticked is returned."
12064   (interactive "p")
12065   (gnus-summary-mark-forward n gnus-ticked-mark))
12066
12067 (defalias 'gnus-summary-mark-as-unread-backward
12068   'gnus-summary-tick-article-backward)
12069 (make-obsolete 'gnus-summary-mark-as-unread-backward
12070                'gnus-summary-tick-article-backward)
12071 (defun gnus-summary-tick-article-backward (n)
12072   "Tick N articles backwards.
12073 The difference between N and the number of articles ticked is returned."
12074   (interactive "p")
12075   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12076
12077 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12078 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12079 (defun gnus-summary-tick-article (&optional article clear-mark)
12080   "Mark current article as unread.
12081 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12082 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12083   (interactive)
12084   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12085                                        gnus-ticked-mark)))
12086
12087 (defun gnus-summary-mark-as-read-forward (n)
12088   "Mark N articles as read forwards.
12089 If N is negative, mark backwards instead.
12090 The difference between N and the actual number of articles marked is
12091 returned."
12092   (interactive "p")
12093   (gnus-summary-mark-forward n gnus-del-mark t))
12094
12095 (defun gnus-summary-mark-as-read-backward (n)
12096   "Mark the N articles as read backwards.
12097 The difference between N and the actual number of articles marked is
12098 returned."
12099   (interactive "p")
12100   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12101
12102 (defun gnus-summary-mark-as-read (&optional article mark)
12103   "Mark current article as read.
12104 ARTICLE specifies the article to be marked as read.
12105 MARK specifies a string to be inserted at the beginning of the line."
12106   (gnus-summary-mark-article article mark))
12107
12108 (defun gnus-summary-clear-mark-forward (n)
12109   "Clear marks from N articles forward.
12110 If N is negative, clear backward instead.
12111 The difference between N and the number of marks cleared is returned."
12112   (interactive "p")
12113   (gnus-summary-mark-forward n gnus-unread-mark))
12114
12115 (defun gnus-summary-clear-mark-backward (n)
12116   "Clear marks from N articles backward.
12117 The difference between N and the number of marks cleared is returned."
12118   (interactive "p")
12119   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12120
12121 (defun gnus-summary-mark-unread-as-read ()
12122   "Intended to be used by `gnus-summary-mark-article-hook'."
12123   (when (memq gnus-current-article gnus-newsgroup-unreads)
12124     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12125
12126 (defun gnus-summary-mark-read-and-unread-as-read ()
12127   "Intended to be used by `gnus-summary-mark-article-hook'."
12128   (let ((mark (gnus-summary-article-mark)))
12129     (when (or (gnus-unread-mark-p mark)
12130               (gnus-read-mark-p mark))
12131       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12132
12133 (defun gnus-summary-mark-region-as-read (point mark all)
12134   "Mark all unread articles between point and mark as read.
12135 If given a prefix, mark all articles between point and mark as read,
12136 even ticked and dormant ones."
12137   (interactive "r\nP")
12138   (save-excursion
12139     (let (article)
12140       (goto-char point)
12141       (beginning-of-line)
12142       (while (and
12143               (< (point) mark)
12144               (progn
12145                 (when (or all
12146                           (memq (setq article (gnus-summary-article-number))
12147                                 gnus-newsgroup-unreads))
12148                   (gnus-summary-mark-article article gnus-del-mark))
12149                 t)
12150               (gnus-summary-find-next))))))
12151
12152 (defun gnus-summary-mark-below (score mark)
12153   "Mark articles with score less than SCORE with MARK."
12154   (interactive "P\ncMark: ")
12155   (gnus-set-global-variables)
12156   (setq score (if score
12157                   (prefix-numeric-value score)
12158                 (or gnus-summary-default-score 0)))
12159   (save-excursion
12160     (set-buffer gnus-summary-buffer)
12161     (goto-char (point-min))
12162     (while 
12163         (progn
12164           (and (< (gnus-summary-article-score) score)
12165                (gnus-summary-mark-article nil mark))
12166           (gnus-summary-find-next)))))
12167
12168 (defun gnus-summary-kill-below (&optional score)
12169   "Mark articles with score below SCORE as read."
12170   (interactive "P")
12171   (gnus-set-global-variables)
12172   (gnus-summary-mark-below score gnus-killed-mark))
12173
12174 (defun gnus-summary-clear-above (&optional score)
12175   "Clear all marks from articles with score above SCORE."
12176   (interactive "P")
12177   (gnus-set-global-variables)
12178   (gnus-summary-mark-above score gnus-unread-mark))
12179
12180 (defun gnus-summary-tick-above (&optional score)
12181   "Tick all articles with score above SCORE."
12182   (interactive "P")
12183   (gnus-set-global-variables)
12184   (gnus-summary-mark-above score gnus-ticked-mark))
12185
12186 (defun gnus-summary-mark-above (score mark)
12187   "Mark articles with score over SCORE with MARK."
12188   (interactive "P\ncMark: ")
12189   (gnus-set-global-variables)
12190   (setq score (if score
12191                   (prefix-numeric-value score)
12192                 (or gnus-summary-default-score 0)))
12193   (save-excursion
12194     (set-buffer gnus-summary-buffer)
12195     (goto-char (point-min))
12196     (while (and (progn
12197                   (if (> (gnus-summary-article-score) score)
12198                       (gnus-summary-mark-article nil mark))
12199                   t)
12200                 (gnus-summary-find-next)))))
12201
12202 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12203 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12204 (defun gnus-summary-limit-include-expunged ()
12205   "Display all the hidden articles that were expunged for low scores."
12206   (interactive)
12207   (gnus-set-global-variables)
12208   (let ((buffer-read-only nil))
12209     (let ((scored gnus-newsgroup-scored)
12210           headers h)
12211       (while scored
12212         (or (gnus-summary-goto-subject (caar scored))
12213             (and (setq h (gnus-summary-article-header (caar scored)))
12214                  (< (cdar scored) gnus-summary-expunge-below)
12215                  (setq headers (cons h headers))))
12216         (setq scored (cdr scored)))
12217       (or headers (error "No expunged articles hidden."))
12218       (goto-char (point-min))
12219       (gnus-summary-prepare-unthreaded (nreverse headers)))
12220     (goto-char (point-min))
12221     (gnus-summary-position-point)))
12222
12223 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12224   "Mark all articles not marked as unread in this newsgroup as read.
12225 If prefix argument ALL is non-nil, all articles are marked as read.
12226 If QUIETLY is non-nil, no questions will be asked.
12227 If TO-HERE is non-nil, it should be a point in the buffer.  All
12228 articles before this point will be marked as read.
12229 The number of articles marked as read is returned."
12230   (interactive "P")
12231   (gnus-set-global-variables)
12232   (prog1
12233       (if (or quietly
12234               (not gnus-interactive-catchup) ;Without confirmation?
12235               gnus-expert-user
12236               (gnus-y-or-n-p
12237                (if all
12238                    "Mark absolutely all articles as read? "
12239                  "Mark all unread articles as read? ")))
12240           (if (and not-mark
12241                    (not gnus-newsgroup-adaptive)
12242                    (not gnus-newsgroup-auto-expire))
12243               (progn
12244                 (when all
12245                   (setq gnus-newsgroup-marked nil
12246                         gnus-newsgroup-dormant nil))
12247                 (setq gnus-newsgroup-unreads nil))
12248             ;; We actually mark all articles as canceled, which we
12249             ;; have to do when using auto-expiry or adaptive scoring.
12250             (gnus-summary-show-all-threads)
12251             (if (gnus-summary-first-subject (not all))
12252                 (while (and
12253                         (if to-here (< (point) to-here) t)
12254                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12255                         (gnus-summary-find-next (not all)))))
12256             (unless to-here
12257               (setq gnus-newsgroup-unreads nil))
12258             (gnus-set-mode-line 'summary)))
12259     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12260       (if (and (not to-here) (eq 'nnvirtual (car method)))
12261           (nnvirtual-catchup-group
12262            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12263     (gnus-summary-position-point)))
12264
12265 (defun gnus-summary-catchup-to-here (&optional all)
12266   "Mark all unticked articles before the current one as read.
12267 If ALL is non-nil, also mark ticked and dormant articles as read."
12268   (interactive "P")
12269   (gnus-set-global-variables)
12270   (save-excursion
12271     (let ((beg (point)))
12272       ;; We check that there are unread articles.
12273       (when (or all (gnus-summary-find-prev))
12274         (gnus-summary-catchup all t beg))))
12275   (gnus-summary-position-point))
12276
12277 (defun gnus-summary-catchup-all (&optional quietly)
12278   "Mark all articles in this newsgroup as read."
12279   (interactive "P")
12280   (gnus-set-global-variables)
12281   (gnus-summary-catchup t quietly))
12282
12283 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12284   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12285 If prefix argument ALL is non-nil, all articles are marked as read."
12286   (interactive "P")
12287   (gnus-set-global-variables)
12288   (gnus-summary-catchup all quietly nil 'fast)
12289   ;; Select next newsgroup or exit.
12290   (if (eq gnus-auto-select-next 'quietly)
12291       (gnus-summary-next-group nil)
12292     (gnus-summary-exit)))
12293
12294 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12295   "Mark all articles in this newsgroup as read, and then exit."
12296   (interactive "P")
12297   (gnus-set-global-variables)
12298   (gnus-summary-catchup-and-exit t quietly))
12299
12300 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12301 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12302   "Mark all articles in this group as read and select the next group.
12303 If given a prefix, mark all articles, unread as well as ticked, as
12304 read."
12305   (interactive "P")
12306   (gnus-set-global-variables)
12307   (save-excursion
12308     (gnus-summary-catchup all))
12309   (gnus-summary-next-article t nil nil t))
12310
12311 ;; Thread-based commands.
12312
12313 (defun gnus-summary-articles-in-thread (&optional article)
12314   "Return a list of all articles in the current thread.
12315 If ARTICLE is non-nil, return all articles in the thread that starts
12316 with that article."
12317   (let* ((article (or article (gnus-summary-article-number)))
12318          (data (gnus-data-find-list article))
12319          (top-level (gnus-data-level (car data)))
12320          (top-subject
12321           (cond ((null gnus-thread-operation-ignore-subject)
12322                  (gnus-simplify-subject-re
12323                   (mail-header-subject (gnus-data-header (car data)))))
12324                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12325                  (gnus-simplify-subject-fuzzy
12326                   (mail-header-subject (gnus-data-header (car data)))))
12327                 (t nil)))
12328          articles)
12329     (if (not data)
12330         ()                              ; This article doesn't exist.
12331       (while data
12332         (and (or (not top-subject)
12333                  (string= top-subject
12334                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12335                               (gnus-simplify-subject-fuzzy
12336                                (mail-header-subject
12337                                 (gnus-data-header (car data))))
12338                             (gnus-simplify-subject-re
12339                              (mail-header-subject
12340                               (gnus-data-header (car data)))))))
12341              (setq articles (cons (gnus-data-number (car data)) articles)))
12342         (if (and (setq data (cdr data))
12343                  (> (gnus-data-level (car data)) top-level))
12344             ()
12345           (setq data nil)))
12346       ;; Return the list of articles.
12347       (nreverse articles))))
12348
12349 (defun gnus-summary-rethread-current ()
12350   "Rethread the thread the current article is part of."
12351   (interactive)
12352   (gnus-set-global-variables)
12353   (let* ((gnus-show-threads t)
12354          (article (gnus-summary-article-number))
12355          (id (mail-header-id (gnus-summary-article-header)))
12356          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12357     (unless id
12358       (error "No article on the current line"))
12359     (gnus-rebuild-thread id)
12360     (gnus-summary-goto-subject article)))
12361
12362 (defun gnus-summary-reparent-thread ()
12363   "Make current article child of the marked (or previous) article.
12364
12365 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12366 is non-nil or the Subject: of both articles are the same."
12367   (interactive)
12368   (or (not (gnus-group-read-only-p))
12369       (error "The current newsgroup does not support article editing."))
12370   (or (<= (length gnus-newsgroup-processable) 1)
12371       (error "No more than one article may be marked."))
12372   (save-window-excursion
12373     (let ((gnus-article-buffer " *reparent*")
12374           (current-article (gnus-summary-article-number))
12375           ; first grab the marked article, otherwise one line up.
12376           (parent-article (if (not (null gnus-newsgroup-processable))
12377                               (car gnus-newsgroup-processable)
12378                             (save-excursion
12379                               (if (eq (forward-line -1) 0)
12380                                   (gnus-summary-article-number)
12381                                 (error "Beginning of summary buffer."))))))
12382       (or (not (eq current-article parent-article))
12383           (error "An article may not be self-referential."))
12384       (let ((message-id (mail-header-id 
12385                          (gnus-summary-article-header parent-article))))
12386         (or (and message-id (not (equal message-id "")))
12387             (error "No message-id in desired parent."))
12388         (gnus-summary-select-article t t nil current-article)
12389         (set-buffer gnus-article-buffer)
12390         (setq buffer-read-only nil)
12391         (let ((buf (format "%s" (buffer-string))))
12392           (erase-buffer)
12393           (insert buf))
12394         (goto-char (point-min))
12395         (if (search-forward-regexp "^References: " nil t)
12396             (insert message-id " " )
12397           (insert "References: " message-id "\n"))
12398         (or (gnus-request-replace-article current-article
12399                                           (car gnus-article-current)
12400                                           gnus-article-buffer)
12401             (error "Couldn't replace article."))
12402         (set-buffer gnus-summary-buffer)
12403         (gnus-summary-unmark-all-processable)
12404         (gnus-summary-rethread-current)
12405         (gnus-message 3 "Article %d is now the child of article %d."
12406                       current-article parent-article)))))
12407
12408 (defun gnus-summary-toggle-threads (&optional arg)
12409   "Toggle showing conversation threads.
12410 If ARG is positive number, turn showing conversation threads on."
12411   (interactive "P")
12412   (gnus-set-global-variables)
12413   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12414     (setq gnus-show-threads
12415           (if (null arg) (not gnus-show-threads)
12416             (> (prefix-numeric-value arg) 0)))
12417     (gnus-summary-prepare)
12418     (gnus-summary-goto-subject current)
12419     (gnus-summary-position-point)))
12420
12421 (defun gnus-summary-show-all-threads ()
12422   "Show all threads."
12423   (interactive)
12424   (gnus-set-global-variables)
12425   (save-excursion
12426     (let ((buffer-read-only nil))
12427       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12428   (gnus-summary-position-point))
12429
12430 (defun gnus-summary-show-thread ()
12431   "Show thread subtrees.
12432 Returns nil if no thread was there to be shown."
12433   (interactive)
12434   (gnus-set-global-variables)
12435   (let ((buffer-read-only nil)
12436         (orig (point))
12437         ;; first goto end then to beg, to have point at beg after let
12438         (end (progn (end-of-line) (point)))
12439         (beg (progn (beginning-of-line) (point))))
12440     (prog1
12441         ;; Any hidden lines here?
12442         (search-forward "\r" end t)
12443       (subst-char-in-region beg end ?\^M ?\n t)
12444       (goto-char orig)
12445       (gnus-summary-position-point))))
12446
12447 (defun gnus-summary-hide-all-threads ()
12448   "Hide all thread subtrees."
12449   (interactive)
12450   (gnus-set-global-variables)
12451   (save-excursion
12452     (goto-char (point-min))
12453     (gnus-summary-hide-thread)
12454     (while (zerop (gnus-summary-next-thread 1 t))
12455       (gnus-summary-hide-thread)))
12456   (gnus-summary-position-point))
12457
12458 (defun gnus-summary-hide-thread ()
12459   "Hide thread subtrees.
12460 Returns nil if no threads were there to be hidden."
12461   (interactive)
12462   (gnus-set-global-variables)
12463   (let ((buffer-read-only nil)
12464         (start (point))
12465         (article (gnus-summary-article-number)))
12466     (goto-char start)
12467     ;; Go forward until either the buffer ends or the subthread
12468     ;; ends.
12469     (when (and (not (eobp))
12470                (or (zerop (gnus-summary-next-thread 1 t))
12471                    (goto-char (point-max))))
12472       (prog1
12473           (if (and (> (point) start)
12474                    (search-backward "\n" start t))
12475               (progn
12476                 (subst-char-in-region start (point) ?\n ?\^M)
12477                 (gnus-summary-goto-subject article))
12478             (goto-char start)
12479             nil)
12480         ;;(gnus-summary-position-point)
12481         ))))
12482
12483 (defun gnus-summary-go-to-next-thread (&optional previous)
12484   "Go to the same level (or less) next thread.
12485 If PREVIOUS is non-nil, go to previous thread instead.
12486 Return the article number moved to, or nil if moving was impossible."
12487   (let ((level (gnus-summary-thread-level))
12488         (way (if previous -1 1))
12489         (beg (point)))
12490     (forward-line way)
12491     (while (and (not (eobp))
12492                 (< level (gnus-summary-thread-level)))
12493       (forward-line way))
12494     (if (eobp)
12495         (progn
12496           (goto-char beg)
12497           nil)
12498       (setq beg (point))
12499       (prog1
12500           (gnus-summary-article-number)
12501         (goto-char beg)))))
12502
12503 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12504   "Go to the same level (or less) next thread.
12505 If PREVIOUS is non-nil, go to previous thread instead.
12506 Return the article number moved to, or nil if moving was impossible."
12507   (if (and (eq gnus-summary-make-false-root 'dummy)
12508            (gnus-summary-article-intangible-p))
12509       (let ((beg (point)))
12510         (while (and (zerop (forward-line 1))
12511                     (not (gnus-summary-article-intangible-p))
12512                     (not (zerop (save-excursion 
12513                                   (gnus-summary-thread-level))))))
12514         (if (eobp)
12515             (progn
12516               (goto-char beg)
12517               nil)
12518           (point)))
12519     (let* ((level (gnus-summary-thread-level))
12520            (article (gnus-summary-article-number))
12521            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12522            oart)
12523       (while data
12524         (if (<= (gnus-data-level (car data)) level)
12525             (setq oart (gnus-data-number (car data))
12526                   data nil)
12527           (setq data (cdr data))))
12528       (and oart
12529            (gnus-summary-goto-subject oart)))))
12530
12531 (defun gnus-summary-next-thread (n &optional silent)
12532   "Go to the same level next N'th thread.
12533 If N is negative, search backward instead.
12534 Returns the difference between N and the number of skips actually
12535 done.
12536
12537 If SILENT, don't output messages."
12538   (interactive "p")
12539   (gnus-set-global-variables)
12540   (let ((backward (< n 0))
12541         (n (abs n))
12542         old dum int)
12543     (while (and (> n 0)
12544                 (gnus-summary-go-to-next-thread backward))
12545       (decf n))
12546     (unless silent 
12547       (gnus-summary-position-point))
12548     (when (and (not silent) (/= 0 n))
12549       (gnus-message 7 "No more threads"))
12550     n))
12551
12552 (defun gnus-summary-prev-thread (n)
12553   "Go to the same level previous N'th thread.
12554 Returns the difference between N and the number of skips actually
12555 done."
12556   (interactive "p")
12557   (gnus-set-global-variables)
12558   (gnus-summary-next-thread (- n)))
12559
12560 (defun gnus-summary-go-down-thread ()
12561   "Go down one level in the current thread."
12562   (let ((children (gnus-summary-article-children)))
12563     (and children
12564          (gnus-summary-goto-subject (car children)))))
12565
12566 (defun gnus-summary-go-up-thread ()
12567   "Go up one level in the current thread."
12568   (let ((parent (gnus-summary-article-parent)))
12569     (and parent
12570          (gnus-summary-goto-subject parent))))
12571
12572 (defun gnus-summary-down-thread (n)
12573   "Go down thread N steps.
12574 If N is negative, go up instead.
12575 Returns the difference between N and how many steps down that were
12576 taken."
12577   (interactive "p")
12578   (gnus-set-global-variables)
12579   (let ((up (< n 0))
12580         (n (abs n)))
12581     (while (and (> n 0)
12582                 (if up (gnus-summary-go-up-thread)
12583                   (gnus-summary-go-down-thread)))
12584       (setq n (1- n)))
12585     (gnus-summary-position-point)
12586     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12587     n))
12588
12589 (defun gnus-summary-up-thread (n)
12590   "Go up thread N steps.
12591 If N is negative, go up instead.
12592 Returns the difference between N and how many steps down that were
12593 taken."
12594   (interactive "p")
12595   (gnus-set-global-variables)
12596   (gnus-summary-down-thread (- n)))
12597
12598 (defun gnus-summary-top-thread ()
12599   "Go to the top of the thread."
12600   (interactive)
12601   (gnus-set-global-variables)
12602   (while (gnus-summary-go-up-thread))
12603   (gnus-summary-article-number))
12604
12605 (defun gnus-summary-kill-thread (&optional unmark)
12606   "Mark articles under current thread as read.
12607 If the prefix argument is positive, remove any kinds of marks.
12608 If the prefix argument is negative, tick articles instead."
12609   (interactive "P")
12610   (gnus-set-global-variables)
12611   (if unmark
12612       (setq unmark (prefix-numeric-value unmark)))
12613   (let ((articles (gnus-summary-articles-in-thread)))
12614     (save-excursion
12615       ;; Expand the thread.
12616       (gnus-summary-show-thread)
12617       ;; Mark all the articles.
12618       (while articles
12619         (gnus-summary-goto-subject (car articles))
12620         (cond ((null unmark)
12621                (gnus-summary-mark-article-as-read gnus-killed-mark))
12622               ((> unmark 0)
12623                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12624               (t
12625                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12626         (setq articles (cdr articles))))
12627     ;; Hide killed subtrees.
12628     (and (null unmark)
12629          gnus-thread-hide-killed
12630          (gnus-summary-hide-thread))
12631     ;; If marked as read, go to next unread subject.
12632     (if (null unmark)
12633         ;; Go to next unread subject.
12634         (gnus-summary-next-subject 1 t)))
12635   (gnus-set-mode-line 'summary))
12636
12637 ;; Summary sorting commands
12638
12639 (defun gnus-summary-sort-by-number (&optional reverse)
12640   "Sort summary buffer by article number.
12641 Argument REVERSE means reverse order."
12642   (interactive "P")
12643   (gnus-summary-sort 'number reverse))
12644
12645 (defun gnus-summary-sort-by-author (&optional reverse)
12646   "Sort summary buffer by author name alphabetically.
12647 If case-fold-search is non-nil, case of letters is ignored.
12648 Argument REVERSE means reverse order."
12649   (interactive "P")
12650   (gnus-summary-sort 'author reverse))
12651
12652 (defun gnus-summary-sort-by-subject (&optional reverse)
12653   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12654 If case-fold-search is non-nil, case of letters is ignored.
12655 Argument REVERSE means reverse order."
12656   (interactive "P")
12657   (gnus-summary-sort 'subject reverse))
12658
12659 (defun gnus-summary-sort-by-date (&optional reverse)
12660   "Sort summary buffer by date.
12661 Argument REVERSE means reverse order."
12662   (interactive "P")
12663   (gnus-summary-sort 'date reverse))
12664
12665 (defun gnus-summary-sort-by-score (&optional reverse)
12666   "Sort summary buffer by score.
12667 Argument REVERSE means reverse order."
12668   (interactive "P")
12669   (gnus-summary-sort 'score reverse))
12670
12671 (defun gnus-summary-sort (predicate reverse)
12672   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12673   (gnus-set-global-variables)
12674   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12675          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12676          (gnus-thread-sort-functions
12677           (list
12678            (if (not reverse)
12679                thread
12680              `(lambda (t1 t2)
12681                 (,thread t2 t1)))))
12682          (gnus-article-sort-functions
12683           (list
12684            (if (not reverse)
12685                article
12686              `(lambda (t1 t2)
12687                 (,article t2 t1)))))
12688          (buffer-read-only)
12689          (gnus-summary-prepare-hook nil))
12690     ;; We do the sorting by regenerating the threads.
12691     (gnus-summary-prepare)
12692     ;; Hide subthreads if needed.
12693     (when (and gnus-show-threads gnus-thread-hide-subtree)
12694       (gnus-summary-hide-all-threads)))
12695   ;; If in async mode, we send some info to the backend.
12696   (when gnus-newsgroup-async
12697     (gnus-request-asynchronous
12698      gnus-newsgroup-name gnus-newsgroup-data)))
12699
12700 (defun gnus-sortable-date (date)
12701   "Make sortable string by string-lessp from DATE.
12702 Timezone package is used."
12703   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12704          (year (aref date 0))
12705          (month (aref date 1))
12706          (day (aref date 2)))
12707     (timezone-make-sortable-date
12708      year month day
12709      (timezone-make-time-string
12710       (aref date 3) (aref date 4) (aref date 5)))))
12711
12712 ;; Summary saving commands.
12713
12714 (defun gnus-summary-save-article (&optional n not-saved)
12715   "Save the current article using the default saver function.
12716 If N is a positive number, save the N next articles.
12717 If N is a negative number, save the N previous articles.
12718 If N is nil and any articles have been marked with the process mark,
12719 save those articles instead.
12720 The variable `gnus-default-article-saver' specifies the saver function."
12721   (interactive "P")
12722   (gnus-set-global-variables)
12723   (let ((articles (gnus-summary-work-articles n))
12724         file header article)
12725     (while articles
12726       (setq header (gnus-summary-article-header
12727                     (setq article (pop articles))))
12728       (if (not (vectorp header))
12729           ;; This is a pseudo-article.
12730           (if (assq 'name header)
12731               (gnus-copy-file (cdr (assq 'name header)))
12732             (gnus-message 1 "Article %d is unsaveable" article))
12733         ;; This is a real article.
12734         (save-window-excursion
12735           (gnus-summary-select-article t nil nil article))
12736         (unless gnus-save-all-headers
12737           ;; Remove headers accoring to `gnus-saved-headers'.
12738           (let ((gnus-visible-headers
12739                  (or gnus-saved-headers gnus-visible-headers)))
12740             (gnus-article-hide-headers nil t)))
12741         ;; Remove any X-Gnus lines.
12742         (save-excursion
12743           (set-buffer gnus-article-buffer)
12744           (save-restriction
12745             (let ((buffer-read-only nil))
12746               (nnheader-narrow-to-headers)
12747               (while (re-search-forward "^X-Gnus" nil t)
12748                 (gnus-delete-line)))))
12749         (save-window-excursion
12750           (if (not gnus-default-article-saver)
12751               (error "No default saver is defined.")
12752             (setq file (funcall
12753                         gnus-default-article-saver
12754                         (cond
12755                          ((not gnus-prompt-before-saving)
12756                           'default)
12757                          ((eq gnus-prompt-before-saving 'always)
12758                           nil)
12759                          (t file))))))
12760         (gnus-summary-remove-process-mark article)
12761         (unless not-saved
12762           (gnus-summary-set-saved-mark article))))
12763     (gnus-summary-position-point)
12764     n))
12765
12766 (defun gnus-summary-pipe-output (&optional arg)
12767   "Pipe the current article to a subprocess.
12768 If N is a positive number, pipe the N next articles.
12769 If N is a negative number, pipe the N previous articles.
12770 If N is nil and any articles have been marked with the process mark,
12771 pipe those articles instead."
12772   (interactive "P")
12773   (gnus-set-global-variables)
12774   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12775     (gnus-summary-save-article arg t))
12776   (gnus-configure-windows 'pipe))
12777
12778 (defun gnus-summary-save-article-mail (&optional arg)
12779   "Append the current article to an mail file.
12780 If N is a positive number, save the N next articles.
12781 If N is a negative number, save the N previous articles.
12782 If N is nil and any articles have been marked with the process mark,
12783 save those articles instead."
12784   (interactive "P")
12785   (gnus-set-global-variables)
12786   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12787     (gnus-summary-save-article arg)))
12788
12789 (defun gnus-summary-save-article-rmail (&optional arg)
12790   "Append the current article to an rmail file.
12791 If N is a positive number, save the N next articles.
12792 If N is a negative number, save the N previous articles.
12793 If N is nil and any articles have been marked with the process mark,
12794 save those articles instead."
12795   (interactive "P")
12796   (gnus-set-global-variables)
12797   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12798     (gnus-summary-save-article arg)))
12799
12800 (defun gnus-summary-save-article-file (&optional arg)
12801   "Append the current article to a file.
12802 If N is a positive number, save the N next articles.
12803 If N is a negative number, save the N previous articles.
12804 If N is nil and any articles have been marked with the process mark,
12805 save those articles instead."
12806   (interactive "P")
12807   (gnus-set-global-variables)
12808   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12809     (gnus-summary-save-article arg)))
12810
12811 (defun gnus-summary-save-article-body-file (&optional arg)
12812   "Append the current article body to a file.
12813 If N is a positive number, save the N next articles.
12814 If N is a negative number, save the N previous articles.
12815 If N is nil and any articles have been marked with the process mark,
12816 save those articles instead."
12817   (interactive "P")
12818   (gnus-set-global-variables)
12819   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12820     (gnus-summary-save-article arg)))
12821
12822 (defun gnus-get-split-value (methods)
12823   "Return a value based on the split METHODS."
12824   (let (split-name method result match)
12825     (when methods
12826       (save-excursion
12827         (set-buffer gnus-original-article-buffer)
12828         (save-restriction
12829           (nnheader-narrow-to-headers)
12830           (while methods
12831             (goto-char (point-min))
12832             (setq method (pop methods))
12833             (setq match (car method))
12834             (when (cond
12835                    ((stringp match)
12836                     ;; Regular expression.
12837                     (condition-case ()
12838                         (re-search-forward match nil t)
12839                       (error nil)))
12840                    ((gnus-functionp match)
12841                     ;; Function.
12842                     (save-restriction
12843                       (widen)
12844                       (setq result (funcall match gnus-newsgroup-name))))
12845                    ((consp match)
12846                     ;; Form.
12847                     (save-restriction
12848                       (widen)
12849                       (setq result (eval match)))))
12850               (setq split-name (append (cdr method) split-name))
12851               (cond ((stringp result)
12852                      (push result split-name))
12853                     ((consp result)
12854                      (setq split-name (append result split-name)))))))))
12855     split-name))
12856
12857 (defun gnus-read-move-group-name (prompt default articles prefix)
12858   "Read a group name."
12859   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12860          (prom
12861           (format "Where do you want to %s %s? "
12862                   prompt
12863                   (if (> (length articles) 1)
12864                       (format "these %d articles" (length articles))
12865                     "this article")))
12866          (to-newsgroup
12867           (cond
12868            ((null split-name)
12869             (completing-read
12870              (concat prom
12871                      (if default
12872                          (format "(default %s) " default)
12873                        ""))
12874              gnus-active-hashtb nil nil prefix))
12875            ((= 1 (length split-name))
12876             (completing-read prom gnus-active-hashtb
12877                              nil nil (cons (car split-name) 0)))
12878            (t
12879             (completing-read
12880              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12881
12882     (when to-newsgroup
12883       (if (or (string= to-newsgroup "")
12884               (string= to-newsgroup prefix))
12885           (setq to-newsgroup (or default "")))
12886       (or (gnus-active to-newsgroup)
12887           (gnus-activate-group to-newsgroup)
12888           (error "No such group: %s" to-newsgroup)))
12889     to-newsgroup))
12890
12891 (defun gnus-read-save-file-name (prompt default-name)
12892   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12893          (file
12894           ;; Let the split methods have their say.
12895           (cond
12896            ;; No split name was found.
12897            ((null split-name)
12898             (read-file-name
12899              (concat prompt " (default "
12900                      (file-name-nondirectory default-name) ") ")
12901              (file-name-directory default-name)
12902              default-name))
12903            ;; A single split name was found
12904            ((= 1 (length split-name))
12905             (read-file-name
12906              (concat prompt " (default " (car split-name) ") ")
12907              gnus-article-save-directory
12908              (concat gnus-article-save-directory (car split-name))))
12909            ;; A list of splits was found.
12910            (t
12911             (setq split-name (mapcar (lambda (el) (list el))
12912                                      (nreverse split-name)))
12913             (let ((result (completing-read
12914                            (concat prompt " ") split-name nil nil)))
12915               (concat gnus-article-save-directory
12916                       (if (string= result "")
12917                           (caar split-name)
12918                         result)))))))
12919     ;; If we have read a directory, we append the default file name.
12920     (when (file-directory-p file)
12921       (setq file (concat (file-name-as-directory file)
12922                          (file-name-nondirectory default-name))))
12923     ;; Possibly translate some charaters.
12924     (nnheader-translate-file-chars file)))
12925
12926 (defun gnus-article-archive-name (group)
12927   "Return the first instance of an \"Archive-name\" in the current buffer."
12928   (let ((case-fold-search t))
12929     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12930       (match-string 1))))
12931
12932 (defun gnus-summary-save-in-rmail (&optional filename)
12933   "Append this article to Rmail file.
12934 Optional argument FILENAME specifies file name.
12935 Directory to save to is default to `gnus-article-save-directory' which
12936 is initialized from the SAVEDIR environment variable."
12937   (interactive)
12938   (gnus-set-global-variables)
12939   (let ((default-name
12940           (funcall gnus-rmail-save-name gnus-newsgroup-name
12941                    gnus-current-headers gnus-newsgroup-last-rmail)))
12942     (setq filename
12943           (cond ((eq filename 'default)
12944                  default-name)
12945                 (filename filename)
12946                 (t (gnus-read-save-file-name
12947                     "Save in rmail file:" default-name))))
12948     (gnus-make-directory (file-name-directory filename))
12949     (gnus-eval-in-buffer-window
12950      gnus-original-article-buffer
12951      (save-excursion
12952        (save-restriction
12953          (widen)
12954          (gnus-output-to-rmail filename))))
12955     ;; Remember the directory name to save articles
12956     (setq gnus-newsgroup-last-rmail filename)))
12957
12958 (defun gnus-summary-save-in-mail (&optional filename)
12959   "Append this article to Unix mail file.
12960 Optional argument FILENAME specifies file name.
12961 Directory to save to is default to `gnus-article-save-directory' which
12962 is initialized from the SAVEDIR environment variable."
12963   (interactive)
12964   (gnus-set-global-variables)
12965   (let ((default-name
12966           (funcall gnus-mail-save-name gnus-newsgroup-name
12967                    gnus-current-headers gnus-newsgroup-last-mail)))
12968     (setq filename
12969           (cond ((eq filename 'default)
12970                  default-name)
12971                 (filename filename)
12972                 (t (gnus-read-save-file-name
12973                     "Save in Unix mail file:" default-name))))
12974     (setq filename
12975           (expand-file-name filename
12976                             (and default-name
12977                                  (file-name-directory 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          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12985              (gnus-output-to-rmail filename)
12986            (let ((mail-use-rfc822 t))
12987              (rmail-output filename 1 t t))))))
12988     ;; Remember the directory name to save articles.
12989     (setq gnus-newsgroup-last-mail filename)))
12990
12991 (defun gnus-summary-save-in-file (&optional filename)
12992   "Append this article to file.
12993 Optional argument FILENAME specifies file name.
12994 Directory to save to is default to `gnus-article-save-directory' which
12995 is initialized from the SAVEDIR environment variable."
12996   (interactive)
12997   (gnus-set-global-variables)
12998   (let ((default-name
12999           (funcall gnus-file-save-name gnus-newsgroup-name
13000                    gnus-current-headers gnus-newsgroup-last-file)))
13001     (setq filename
13002           (cond ((eq filename 'default)
13003                  default-name)
13004                 (filename filename)
13005                 (t (gnus-read-save-file-name
13006                     "Save in file:" default-name))))
13007     (gnus-make-directory (file-name-directory filename))
13008     (gnus-eval-in-buffer-window
13009      gnus-original-article-buffer
13010      (save-excursion
13011        (save-restriction
13012          (widen)
13013          (gnus-output-to-file filename))))
13014     ;; Remember the directory name to save articles.
13015     (setq gnus-newsgroup-last-file filename)))
13016
13017 (defun gnus-summary-save-body-in-file (&optional filename)
13018   "Append this article body to a file.
13019 Optional argument FILENAME specifies file name.
13020 The directory to save in defaults to `gnus-article-save-directory' which
13021 is initialized from the SAVEDIR environment variable."
13022   (interactive)
13023   (gnus-set-global-variables)
13024   (let ((default-name
13025           (funcall gnus-file-save-name gnus-newsgroup-name
13026                    gnus-current-headers gnus-newsgroup-last-file)))
13027     (setq filename
13028           (cond ((eq filename 'default)
13029                  default-name)
13030                 (filename filename)
13031                 (t (gnus-read-save-file-name
13032                     "Save body in file:" default-name))))
13033     (gnus-make-directory (file-name-directory filename))
13034     (gnus-eval-in-buffer-window
13035      gnus-article-buffer
13036      (save-excursion
13037        (save-restriction
13038          (widen)
13039          (goto-char (point-min))
13040          (and (search-forward "\n\n" nil t)
13041               (narrow-to-region (point) (point-max)))
13042          (gnus-output-to-file filename))))
13043     ;; Remember the directory name to save articles.
13044     (setq gnus-newsgroup-last-file filename)))
13045
13046 (defun gnus-summary-save-in-pipe (&optional command)
13047   "Pipe this article to subprocess."
13048   (interactive)
13049   (gnus-set-global-variables)
13050   (setq command
13051         (cond ((eq command 'default)
13052                gnus-last-shell-command)
13053               (command command)
13054               (t (read-string "Shell command on article: "
13055                               gnus-last-shell-command))))
13056   (if (string-equal command "")
13057       (setq command gnus-last-shell-command))
13058   (gnus-eval-in-buffer-window
13059    gnus-article-buffer
13060    (save-restriction
13061      (widen)
13062      (shell-command-on-region (point-min) (point-max) command nil)))
13063   (setq gnus-last-shell-command command))
13064
13065 ;; Summary extract commands
13066
13067 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13068   (let ((buffer-read-only nil)
13069         (article (gnus-summary-article-number))
13070         after-article b e)
13071     (or (gnus-summary-goto-subject article)
13072         (error (format "No such article: %d" article)))
13073     (gnus-summary-position-point)
13074     ;; If all commands are to be bunched up on one line, we collect
13075     ;; them here.
13076     (if gnus-view-pseudos-separately
13077         ()
13078       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13079             files action)
13080         (while ps
13081           (setq action (cdr (assq 'action (car ps))))
13082           (setq files (list (cdr (assq 'name (car ps)))))
13083           (while (and ps (cdr ps)
13084                       (string= (or action "1")
13085                                (or (cdr (assq 'action (cadr ps))) "2")))
13086             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13087             (setcdr ps (cddr ps)))
13088           (if (not files)
13089               ()
13090             (if (not (string-match "%s" action))
13091                 (setq files (cons " " files)))
13092             (setq files (cons " " files))
13093             (and (assq 'execute (car ps))
13094                  (setcdr (assq 'execute (car ps))
13095                          (funcall (if (string-match "%s" action)
13096                                       'format 'concat)
13097                                   action
13098                                   (mapconcat (lambda (f) f) files " ")))))
13099           (setq ps (cdr ps)))))
13100     (if (and gnus-view-pseudos (not not-view))
13101         (while pslist
13102           (and (assq 'execute (car pslist))
13103                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13104                                      (eq gnus-view-pseudos 'not-confirm)))
13105           (setq pslist (cdr pslist)))
13106       (save-excursion
13107         (while pslist
13108           (setq after-article (or (cdr (assq 'article (car pslist)))
13109                                   (gnus-summary-article-number)))
13110           (gnus-summary-goto-subject after-article)
13111           (forward-line 1)
13112           (setq b (point))
13113           (insert "    " (file-name-nondirectory
13114                                 (cdr (assq 'name (car pslist))))
13115                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13116           (setq e (point))
13117           (forward-line -1)             ; back to `b'
13118           (add-text-properties
13119            b e (list 'gnus-number gnus-reffed-article-number
13120                      gnus-mouse-face-prop gnus-mouse-face))
13121           (gnus-data-enter
13122            after-article gnus-reffed-article-number
13123            gnus-unread-mark b (car pslist) 0 (- e b))
13124           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13125           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13126           (setq pslist (cdr pslist)))))))
13127
13128 (defun gnus-pseudos< (p1 p2)
13129   (let ((c1 (cdr (assq 'action p1)))
13130         (c2 (cdr (assq 'action p2))))
13131     (and c1 c2 (string< c1 c2))))
13132
13133 (defun gnus-request-pseudo-article (props)
13134   (cond ((assq 'execute props)
13135          (gnus-execute-command (cdr (assq 'execute props)))))
13136   (let ((gnus-current-article (gnus-summary-article-number)))
13137     (run-hooks 'gnus-mark-article-hook)))
13138
13139 (defun gnus-execute-command (command &optional automatic)
13140   (save-excursion
13141     (gnus-article-setup-buffer)
13142     (set-buffer gnus-article-buffer)
13143     (let ((command (if automatic command (read-string "Command: " command)))
13144           (buffer-read-only nil))
13145       (erase-buffer)
13146       (insert "$ " command "\n\n")
13147       (if gnus-view-pseudo-asynchronously
13148           (start-process "gnus-execute" nil "sh" "-c" command)
13149         (call-process "sh" nil t nil "-c" command)))))
13150
13151 (defun gnus-copy-file (file &optional to)
13152   "Copy FILE to TO."
13153   (interactive
13154    (list (read-file-name "Copy file: " default-directory)
13155          (read-file-name "Copy file to: " default-directory)))
13156   (gnus-set-global-variables)
13157   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13158   (and (file-directory-p to)
13159        (setq to (concat (file-name-as-directory to)
13160                         (file-name-nondirectory file))))
13161   (copy-file file to))
13162
13163 ;; Summary kill commands.
13164
13165 (defun gnus-summary-edit-global-kill (article)
13166   "Edit the \"global\" kill file."
13167   (interactive (list (gnus-summary-article-number)))
13168   (gnus-set-global-variables)
13169   (gnus-group-edit-global-kill article))
13170
13171 (defun gnus-summary-edit-local-kill ()
13172   "Edit a local kill file applied to the current newsgroup."
13173   (interactive)
13174   (gnus-set-global-variables)
13175   (setq gnus-current-headers (gnus-summary-article-header))
13176   (gnus-set-global-variables)
13177   (gnus-group-edit-local-kill
13178    (gnus-summary-article-number) gnus-newsgroup-name))
13179
13180 \f
13181 ;;;
13182 ;;; Gnus article mode
13183 ;;;
13184
13185 (put 'gnus-article-mode 'mode-class 'special)
13186
13187 (if gnus-article-mode-map
13188     nil
13189   (setq gnus-article-mode-map (make-keymap))
13190   (suppress-keymap gnus-article-mode-map)
13191
13192   (gnus-define-keys gnus-article-mode-map
13193     " " gnus-article-goto-next-page
13194     "\177" gnus-article-goto-prev-page
13195     [delete] gnus-article-goto-prev-page
13196     "\C-c^" gnus-article-refer-article
13197     "h" gnus-article-show-summary
13198     "s" gnus-article-show-summary
13199     "\C-c\C-m" gnus-article-mail
13200     "?" gnus-article-describe-briefly
13201     gnus-mouse-2 gnus-article-push-button
13202     "\r" gnus-article-press-button
13203     "\t" gnus-article-next-button
13204     "\M-\t" gnus-article-prev-button
13205     "\C-c\C-b" gnus-bug)
13206
13207   (substitute-key-definition
13208    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13209
13210 (defun gnus-article-mode ()
13211   "Major mode for displaying an article.
13212
13213 All normal editing commands are switched off.
13214
13215 The following commands are available:
13216
13217 \\<gnus-article-mode-map>
13218 \\[gnus-article-next-page]\t Scroll the article one page forwards
13219 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13220 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13221 \\[gnus-article-show-summary]\t Display the summary buffer
13222 \\[gnus-article-mail]\t Send a reply to the address near point
13223 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13224 \\[gnus-info-find-node]\t Go to the Gnus info node"
13225   (interactive)
13226   (when (and menu-bar-mode
13227              (gnus-visual-p 'article-menu 'menu))
13228     (gnus-article-make-menu-bar))
13229   (kill-all-local-variables)
13230   (gnus-simplify-mode-line)
13231   (setq mode-name "Article")
13232   (setq major-mode 'gnus-article-mode)
13233   (make-local-variable 'minor-mode-alist)
13234   (or (assq 'gnus-show-mime minor-mode-alist)
13235       (setq minor-mode-alist
13236             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13237   (use-local-map gnus-article-mode-map)
13238   (make-local-variable 'page-delimiter)
13239   (setq page-delimiter gnus-page-delimiter)
13240   (buffer-disable-undo (current-buffer))
13241   (setq buffer-read-only t)             ;Disable modification
13242   (run-hooks 'gnus-article-mode-hook))
13243
13244 (defun gnus-article-setup-buffer ()
13245   "Initialize the article buffer."
13246   (let* ((name (if gnus-single-article-buffer "*Article*"
13247                  (concat "*Article " gnus-newsgroup-name "*")))
13248          (original
13249           (progn (string-match "\\*Article" name)
13250                  (concat " *Original Article"
13251                          (substring name (match-end 0))))))
13252     (setq gnus-article-buffer name)
13253     (setq gnus-original-article-buffer original)
13254     ;; This might be a variable local to the summary buffer.
13255     (unless gnus-single-article-buffer
13256       (save-excursion
13257         (set-buffer gnus-summary-buffer)
13258         (setq gnus-article-buffer name)
13259         (setq gnus-original-article-buffer original)
13260         (gnus-set-global-variables))
13261       (make-local-variable 'gnus-summary-buffer))
13262     ;; Init original article buffer.
13263     (save-excursion
13264       (set-buffer (get-buffer-create gnus-original-article-buffer))
13265       (buffer-disable-undo (current-buffer))
13266       (setq major-mode 'gnus-original-article-mode)
13267       (make-local-variable 'gnus-original-article))
13268     (if (get-buffer name)
13269         (save-excursion
13270           (set-buffer name)
13271           (buffer-disable-undo (current-buffer))
13272           (setq buffer-read-only t)
13273           (gnus-add-current-to-buffer-list)
13274           (or (eq major-mode 'gnus-article-mode)
13275               (gnus-article-mode))
13276           (current-buffer))
13277       (save-excursion
13278         (set-buffer (get-buffer-create name))
13279         (gnus-add-current-to-buffer-list)
13280         (gnus-article-mode)
13281         (current-buffer)))))
13282
13283 ;; Set article window start at LINE, where LINE is the number of lines
13284 ;; from the head of the article.
13285 (defun gnus-article-set-window-start (&optional line)
13286   (set-window-start
13287    (get-buffer-window gnus-article-buffer)
13288    (save-excursion
13289      (set-buffer gnus-article-buffer)
13290      (goto-char (point-min))
13291      (if (not line)
13292          (point-min)
13293        (gnus-message 6 "Moved to bookmark")
13294        (search-forward "\n\n" nil t)
13295        (forward-line line)
13296        (point)))))
13297
13298 (defun gnus-kill-all-overlays ()
13299   "Delete all overlays in the current buffer."
13300   (when (fboundp 'overlay-lists)
13301     (let* ((overlayss (overlay-lists))
13302            (buffer-read-only nil)
13303            (overlays (nconc (car overlayss) (cdr overlayss))))
13304       (while overlays
13305         (delete-overlay (pop overlays))))))
13306
13307 (defun gnus-request-article-this-buffer (article group)
13308   "Get an article and insert it into this buffer."
13309   (let (do-update-line)
13310     (prog1
13311         (save-excursion
13312           (erase-buffer)
13313           (gnus-kill-all-overlays)
13314           (setq group (or group gnus-newsgroup-name))
13315
13316           ;; Open server if it has closed.
13317           (gnus-check-server (gnus-find-method-for-group group))
13318
13319           ;; Using `gnus-request-article' directly will insert the article into
13320           ;; `nntp-server-buffer' - so we'll save some time by not having to
13321           ;; copy it from the server buffer into the article buffer.
13322
13323           ;; We only request an article by message-id when we do not have the
13324           ;; headers for it, so we'll have to get those.
13325           (when (stringp article)
13326             (let ((gnus-override-method gnus-refer-article-method))
13327               (gnus-read-header article)))
13328
13329           ;; If the article number is negative, that means that this article
13330           ;; doesn't belong in this newsgroup (possibly), so we find its
13331           ;; message-id and request it by id instead of number.
13332           (when (and (numberp article)
13333                      gnus-summary-buffer
13334                      (get-buffer gnus-summary-buffer)
13335                      (buffer-name (get-buffer gnus-summary-buffer)))
13336             (save-excursion
13337               (set-buffer gnus-summary-buffer)
13338               (let ((header (gnus-summary-article-header article)))
13339                 (if (< article 0)
13340                     (cond 
13341                      ((memq article gnus-newsgroup-sparse)
13342                       ;; This is a sparse gap article.
13343                       (setq do-update-line article)
13344                       (setq article (mail-header-id header))
13345                       (let ((gnus-override-method gnus-refer-article-method))
13346                         (gnus-read-header article)))
13347                      ((vectorp header)
13348                       ;; It's a real article.
13349                       (setq article (mail-header-id header)))
13350                      (t
13351                       ;; It is an extracted pseudo-article.
13352                       (setq article 'pseudo)
13353                       (gnus-request-pseudo-article header))))
13354                 
13355                 (let ((method (gnus-find-method-for-group 
13356                                gnus-newsgroup-name)))
13357                   (if (not (eq (car method) 'nneething))
13358                       ()
13359                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13360                                        (mail-header-subject header))))
13361                       (if (file-directory-p dir)
13362                           (progn
13363                             (setq article 'nneething)
13364                             (gnus-group-enter-directory dir)))))))))
13365
13366           (cond
13367            ;; We first check `gnus-original-article-buffer'.
13368            ((and (get-buffer gnus-original-article-buffer)
13369                  (save-excursion
13370                    (set-buffer gnus-original-article-buffer)
13371                    (and (equal (car gnus-original-article) group)
13372                         (eq (cdr gnus-original-article) article))))
13373             (insert-buffer-substring gnus-original-article-buffer)
13374             'article)
13375            ;; Check the backlog.
13376            ((and gnus-keep-backlog
13377                  (gnus-backlog-request-article group article (current-buffer)))
13378             'article)
13379            ;; Check the cache.
13380            ((and gnus-use-cache
13381                  (numberp article)
13382                  (gnus-cache-request-article article group))
13383             'article)
13384            ;; Get the article and put into the article buffer.
13385            ((or (stringp article) (numberp article))
13386             (let ((gnus-override-method
13387                    (and (stringp article) gnus-refer-article-method))
13388                   (buffer-read-only nil))
13389               (erase-buffer)
13390               (gnus-kill-all-overlays)
13391               (if (gnus-request-article article group (current-buffer))
13392                   (progn
13393                     (and gnus-keep-backlog
13394                          (numberp article)
13395                          (gnus-backlog-enter-article
13396                           group article (current-buffer)))
13397                     'article))))
13398            ;; It was a pseudo.
13399            (t article)))
13400
13401       ;; Take the article from the original article buffer
13402       ;; and place it in the buffer it's supposed to be in.
13403       (when (and (get-buffer gnus-article-buffer)
13404                  (equal (buffer-name (current-buffer))
13405                         (buffer-name (get-buffer gnus-article-buffer))))
13406         (save-excursion
13407           (if (get-buffer gnus-original-article-buffer)
13408               (set-buffer (get-buffer gnus-original-article-buffer))
13409             (set-buffer (get-buffer-create gnus-original-article-buffer))
13410             (buffer-disable-undo (current-buffer))
13411             (setq major-mode 'gnus-original-article-mode)
13412             (setq buffer-read-only t)
13413             (gnus-add-current-to-buffer-list))
13414           (let (buffer-read-only)
13415             (erase-buffer)
13416             (insert-buffer-substring gnus-article-buffer))
13417           (setq gnus-original-article (cons group article))))
13418     
13419       ;; Update sparse articles.
13420       (when do-update-line
13421         (save-excursion
13422           (set-buffer gnus-summary-buffer)
13423           (gnus-summary-update-article do-update-line)
13424           (gnus-summary-goto-subject do-update-line)
13425           (set-window-point (get-buffer-window (current-buffer) t)
13426                             (point)))))))
13427
13428 (defun gnus-read-header (id &optional header)
13429   "Read the headers of article ID and enter them into the Gnus system."
13430   (let ((group gnus-newsgroup-name)
13431         where)
13432     ;; First we check to see whether the header in question is already
13433     ;; fetched.
13434     (if (stringp id)
13435         ;; This is a Message-ID.
13436         (setq header (or header (gnus-id-to-header id)))
13437       ;; This is an article number.
13438       (setq header (or header (gnus-summary-article-header id))))
13439     (if (and header
13440              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13441         ;; We have found the header.
13442         header
13443       ;; We have to really fetch the header to this article.
13444       (when (setq where
13445                   (if (gnus-check-backend-function 'request-head group)
13446                       (gnus-request-head id group)
13447                     (gnus-request-article id group)))
13448         (save-excursion
13449           (set-buffer nntp-server-buffer)
13450           (and (search-forward "\n\n" nil t)
13451                (delete-region (1- (point)) (point-max)))
13452           (goto-char (point-max))
13453           (insert ".\n")
13454           (goto-char (point-min))
13455           (insert "211 ")
13456           (princ (cond
13457                   ((numberp id) id)
13458                   ((cdr where) (cdr where))
13459                   (header (mail-header-number header))
13460                   (t gnus-reffed-article-number))
13461                  (current-buffer))
13462           (insert " Article retrieved.\n"))
13463         ;(when (and header
13464         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13465         ;  (setcar (gnus-id-to-thread id) nil))
13466         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13467             ()                          ; Malformed head.
13468           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13469             (if (and (stringp id)
13470                      (not (string= (gnus-group-real-name group)
13471                                    (car where))))
13472                 ;; If we fetched by Message-ID and the article came
13473                 ;; from a different group, we fudge some bogus article
13474                 ;; numbers for this article.
13475                 (mail-header-set-number header gnus-reffed-article-number))
13476             (decf gnus-reffed-article-number)
13477             (push header gnus-newsgroup-headers)
13478             (setq gnus-current-headers header)
13479             (push (mail-header-number header) gnus-newsgroup-limit))
13480           header)))))
13481
13482 (defun gnus-article-prepare (article &optional all-headers header)
13483   "Prepare ARTICLE in article mode buffer.
13484 ARTICLE should either be an article number or a Message-ID.
13485 If ARTICLE is an id, HEADER should be the article headers.
13486 If ALL-HEADERS is non-nil, no headers are hidden."
13487   (save-excursion
13488     ;; Make sure we start in a summary buffer.
13489     (unless (eq major-mode 'gnus-summary-mode)
13490       (set-buffer gnus-summary-buffer))
13491     (setq gnus-summary-buffer (current-buffer))
13492     ;; Make sure the connection to the server is alive.
13493     (unless (gnus-server-opened
13494              (gnus-find-method-for-group gnus-newsgroup-name))
13495       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13496       (gnus-request-group gnus-newsgroup-name t))
13497     (let* ((article (if header (mail-header-number header) article))
13498            (summary-buffer (current-buffer))
13499            (internal-hook gnus-article-internal-prepare-hook)
13500            (group gnus-newsgroup-name)
13501            result)
13502       (save-excursion
13503         (gnus-article-setup-buffer)
13504         (set-buffer gnus-article-buffer)
13505         ;; Deactivate active regions.
13506         (when (and (boundp 'transient-mark-mode)
13507                    transient-mark-mode)
13508           (setq mark-active nil))
13509         (if (not (setq result (let ((buffer-read-only nil))
13510                                 (gnus-request-article-this-buffer
13511                                  article group))))
13512             ;; There is no such article.
13513             (save-excursion
13514               (when (and (numberp article)
13515                          (not (memq article gnus-newsgroup-sparse)))
13516                 (setq gnus-article-current
13517                       (cons gnus-newsgroup-name article))
13518                 (set-buffer gnus-summary-buffer)
13519                 (setq gnus-current-article article)
13520                 (gnus-summary-mark-article article gnus-canceled-mark))
13521               (unless (memq article gnus-newsgroup-sparse)
13522                 (gnus-error
13523                  1 "No such article (may have expired or been canceled)")))
13524           (if (or (eq result 'pseudo) (eq result 'nneething))
13525               (progn
13526                 (save-excursion
13527                   (set-buffer summary-buffer)
13528                   (setq gnus-last-article gnus-current-article
13529                         gnus-newsgroup-history (cons gnus-current-article
13530                                                      gnus-newsgroup-history)
13531                         gnus-current-article 0
13532                         gnus-current-headers nil
13533                         gnus-article-current nil)
13534                   (if (eq result 'nneething)
13535                       (gnus-configure-windows 'summary)
13536                     (gnus-configure-windows 'article))
13537                   (gnus-set-global-variables))
13538                 (gnus-set-mode-line 'article))
13539             ;; The result from the `request' was an actual article -
13540             ;; or at least some text that is now displayed in the
13541             ;; article buffer.
13542             (if (and (numberp article)
13543                      (not (eq article gnus-current-article)))
13544                 ;; Seems like a new article has been selected.
13545                 ;; `gnus-current-article' must be an article number.
13546                 (save-excursion
13547                   (set-buffer summary-buffer)
13548                   (setq gnus-last-article gnus-current-article
13549                         gnus-newsgroup-history (cons gnus-current-article
13550                                                      gnus-newsgroup-history)
13551                         gnus-current-article article
13552                         gnus-current-headers
13553                         (gnus-summary-article-header gnus-current-article)
13554                         gnus-article-current
13555                         (cons gnus-newsgroup-name gnus-current-article))
13556                   (unless (vectorp gnus-current-headers)
13557                     (setq gnus-current-headers nil))
13558                   (gnus-summary-show-thread)
13559                   (run-hooks 'gnus-mark-article-hook)
13560                   (gnus-set-mode-line 'summary)
13561                   (and (gnus-visual-p 'article-highlight 'highlight)
13562                        (run-hooks 'gnus-visual-mark-article-hook))
13563                   ;; Set the global newsgroup variables here.
13564                   ;; Suggested by Jim Sisolak
13565                   ;; <sisolak@trans4.neep.wisc.edu>.
13566                   (gnus-set-global-variables)
13567                   (setq gnus-have-all-headers
13568                         (or all-headers gnus-show-all-headers))
13569                   (and gnus-use-cache
13570                        (vectorp (gnus-summary-article-header article))
13571                        (gnus-cache-possibly-enter-article
13572                         group article
13573                         (gnus-summary-article-header article)
13574                         (memq article gnus-newsgroup-marked)
13575                         (memq article gnus-newsgroup-dormant)
13576                         (memq article gnus-newsgroup-unreads)))))
13577             ;; Hooks for getting information from the article.
13578             ;; This hook must be called before being narrowed.
13579             (let (buffer-read-only)
13580               (run-hooks 'internal-hook)
13581               (run-hooks 'gnus-article-prepare-hook)
13582               ;; Decode MIME message.
13583               (if gnus-show-mime
13584                   (if (or (not gnus-strict-mime)
13585                           (gnus-fetch-field "Mime-Version"))
13586                       (funcall gnus-show-mime-method)
13587                     (funcall gnus-decode-encoded-word-method)))
13588               ;; Perform the article display hooks.
13589               (run-hooks 'gnus-article-display-hook))
13590             ;; Do page break.
13591             (goto-char (point-min))
13592             (and gnus-break-pages (gnus-narrow-to-page))
13593             (gnus-set-mode-line 'article)
13594             (gnus-configure-windows 'article)
13595             (goto-char (point-min))
13596             t))))))
13597
13598 (defun gnus-article-show-all-headers ()
13599   "Show all article headers in article mode buffer."
13600   (save-excursion
13601     (gnus-article-setup-buffer)
13602     (set-buffer gnus-article-buffer)
13603     (let ((buffer-read-only nil))
13604       (gnus-unhide-text (point-min) (point-max)))))
13605
13606 (defun gnus-article-hide-headers-if-wanted ()
13607   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13608 Provided for backwards compatibility."
13609   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13610       gnus-inhibit-hiding
13611       (gnus-article-hide-headers)))
13612
13613 (defun gnus-article-hide-headers (&optional arg delete)
13614   "Toggle whether to hide unwanted headers and possibly sort them as well.
13615 If given a negative prefix, always show; if given a positive prefix,
13616 always hide."
13617   (interactive "P")
13618   (unless (gnus-article-check-hidden-text 'headers arg)
13619     ;; This function might be inhibited.
13620     (unless gnus-inhibit-hiding
13621       (save-excursion
13622         (set-buffer gnus-article-buffer)
13623         (save-restriction
13624           (let ((buffer-read-only nil)
13625                 (props (nconc (list 'gnus-type 'headers)
13626                               gnus-hidden-properties))
13627                 (max (1+ (length gnus-sorted-header-list)))
13628                 (ignored (when (not (stringp gnus-visible-headers))
13629                            (cond ((stringp gnus-ignored-headers)
13630                                   gnus-ignored-headers)
13631                                  ((listp gnus-ignored-headers)
13632                                   (mapconcat 'identity gnus-ignored-headers
13633                                              "\\|")))))
13634                 (visible
13635                  (cond ((stringp gnus-visible-headers)
13636                         gnus-visible-headers)
13637                        ((and gnus-visible-headers
13638                              (listp gnus-visible-headers))
13639                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13640                 want-list beg)
13641             ;; First we narrow to just the headers.
13642             (widen)
13643             (goto-char (point-min))
13644             ;; Hide any "From " lines at the beginning of (mail) articles.
13645             (while (looking-at "From ")
13646               (forward-line 1))
13647             (unless (bobp)
13648               (gnus-hide-text (point-min) (point) props))
13649             ;; Then treat the rest of the header lines.
13650             (narrow-to-region
13651              (point)
13652              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13653             ;; Then we use the two regular expressions
13654             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13655             ;; select which header lines is to remain visible in the
13656             ;; article buffer.
13657             (goto-char (point-min))
13658             (while (re-search-forward "^[^ \t]*:" nil t)
13659               (beginning-of-line)
13660               ;; We add the headers we want to keep to a list and delete
13661               ;; them from the buffer.
13662               (put-text-property 
13663                (point) (1+ (point)) 'message-rank
13664                (if (or (and visible (looking-at visible))
13665                        (and ignored
13666                             (not (looking-at ignored))))
13667                    (gnus-article-header-rank) 
13668                  (+ 2 max)))
13669               (forward-line 1))
13670             (message-sort-headers-1)
13671             (when (setq beg (text-property-any 
13672                              (point-min) (point-max) 'message-rank (+ 2 max)))
13673               ;; We make the unwanted headers invisible.
13674               (if delete
13675                   (delete-region beg (point-max))
13676                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13677                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13678
13679 (defsubst gnus-article-header-rank ()
13680   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13681   (let ((list gnus-sorted-header-list)
13682         (i 0))
13683     (while list
13684       (when (looking-at (car list))
13685         (setq list nil))
13686       (setq list (cdr list))
13687       (incf i))
13688     i))
13689
13690 (defun gnus-article-hide-boring-headers (&optional arg)
13691   "Toggle hiding of headers that aren't very interesting.
13692 If given a negative prefix, always show; if given a positive prefix,
13693 always hide."
13694   (interactive "P")
13695   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13696     (save-excursion
13697       (set-buffer gnus-article-buffer)
13698       (save-restriction
13699         (let ((buffer-read-only nil)
13700               (list gnus-boring-article-headers)
13701               (inhibit-point-motion-hooks t)
13702               elem)
13703           (nnheader-narrow-to-headers)
13704           (while list
13705             (setq elem (pop list))
13706             (goto-char (point-min))
13707             (cond
13708              ;; Hide empty headers.
13709              ((eq elem 'empty)
13710               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13711                 (forward-line -1)
13712                 (gnus-hide-text-type
13713                  (progn (beginning-of-line) (point))
13714                  (progn 
13715                    (end-of-line)
13716                    (if (re-search-forward "^[^ \t]" nil t)
13717                        (match-beginning 0)
13718                      (point-max)))
13719                  'boring-headers)))
13720              ;; Hide boring Newsgroups header.
13721              ((eq elem 'newsgroups)
13722               (when (equal (mail-fetch-field "newsgroups")
13723                            (gnus-group-real-name gnus-newsgroup-name))
13724                 (gnus-article-hide-header "newsgroups")))
13725              ((eq elem 'followup-to)
13726               (when (equal (mail-fetch-field "followup-to")
13727                            (mail-fetch-field "newsgroups"))
13728                 (gnus-article-hide-header "followup-to")))
13729              ((eq elem 'reply-to)
13730               (let ((from (mail-fetch-field "from"))
13731                     (reply-to (mail-fetch-field "reply-to")))
13732                 (when (and
13733                        from reply-to
13734                        (equal 
13735                         (nth 1 (funcall gnus-extract-address-components from))
13736                         (nth 1 (funcall gnus-extract-address-components
13737                                         reply-to))))
13738                   (gnus-article-hide-header "reply-to"))))
13739              ((eq elem 'date)
13740               (let ((date (mail-fetch-field "date")))
13741                 (when (and date
13742                            (< (gnus-days-between date (current-time-string))
13743                               4))
13744                   (gnus-article-hide-header "date")))))))))))
13745
13746 (defun gnus-article-hide-header (header)
13747   (save-excursion
13748     (goto-char (point-min))
13749     (when (re-search-forward (concat "^" header ":") nil t)
13750       (gnus-hide-text-type
13751        (progn (beginning-of-line) (point))
13752        (progn 
13753          (end-of-line)
13754          (if (re-search-forward "^[^ \t]" nil t)
13755              (match-beginning 0)
13756            (point-max)))
13757        'boring-headers))))
13758
13759 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13760 (defun gnus-article-treat-overstrike ()
13761   "Translate overstrikes into bold text."
13762   (interactive)
13763   (save-excursion
13764     (set-buffer gnus-article-buffer)
13765     (let ((buffer-read-only nil))
13766       (while (search-forward "\b" nil t)
13767         (let ((next (following-char))
13768               (previous (char-after (- (point) 2))))
13769           (cond ((eq next previous)
13770                  (put-text-property (- (point) 2) (point) 'invisible t)
13771                  (put-text-property (point) (1+ (point)) 'face 'bold))
13772                 ((eq next ?_)
13773                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13774                  (put-text-property
13775                   (- (point) 2) (1- (point)) 'face 'underline))
13776                 ((eq previous ?_)
13777                  (put-text-property (- (point) 2) (point) 'invisible t)
13778                  (put-text-property
13779                   (point) (1+ (point))  'face 'underline))))))))
13780
13781 (defun gnus-article-word-wrap ()
13782   "Format too long lines."
13783   (interactive)
13784   (save-excursion
13785     (set-buffer gnus-article-buffer)
13786     (let ((buffer-read-only nil))
13787       (widen)
13788       (goto-char (point-min))
13789       (search-forward "\n\n" nil t)
13790       (end-of-line 1)
13791       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13792             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13793             (adaptive-fill-mode t))
13794         (while (not (eobp))
13795           (and (>= (current-column) (min fill-column (window-width)))
13796                (/= (preceding-char) ?:)
13797                (fill-paragraph nil))
13798           (end-of-line 2))))))
13799
13800 (defun gnus-article-remove-cr ()
13801   "Remove carriage returns from an article."
13802   (interactive)
13803   (save-excursion
13804     (set-buffer gnus-article-buffer)
13805     (let ((buffer-read-only nil))
13806       (goto-char (point-min))
13807       (while (search-forward "\r" nil t)
13808         (replace-match "" t t)))))
13809
13810 (defun gnus-article-remove-trailing-blank-lines ()
13811   "Remove all trailing blank lines from the article."
13812   (interactive)
13813   (save-excursion
13814     (set-buffer gnus-article-buffer)
13815     (let ((buffer-read-only nil))
13816       (goto-char (point-max))
13817       (delete-region
13818        (point)
13819        (progn
13820          (while (looking-at "^[ \t]*$")
13821            (forward-line -1))
13822          (forward-line 1)
13823          (point))))))
13824
13825 (defun gnus-article-display-x-face (&optional force)
13826   "Look for an X-Face header and display it if present."
13827   (interactive (list 'force))
13828   (save-excursion
13829     (set-buffer gnus-article-buffer)
13830     ;; Delete the old process, if any.
13831     (when (process-status "gnus-x-face")
13832       (delete-process "gnus-x-face"))
13833     (let ((inhibit-point-motion-hooks t)
13834           (case-fold-search nil)
13835           from)
13836       (save-restriction
13837         (nnheader-narrow-to-headers)
13838         (setq from (mail-fetch-field "from"))
13839         (goto-char (point-min))
13840         (when (and gnus-article-x-face-command
13841                    (or force
13842                        ;; Check whether this face is censored.
13843                        (not gnus-article-x-face-too-ugly)
13844                        (and gnus-article-x-face-too-ugly from
13845                             (not (string-match gnus-article-x-face-too-ugly
13846                                                from))))
13847                    ;; Has to be present.
13848                    (re-search-forward "^X-Face: " nil t))
13849           ;; We now have the area of the buffer where the X-Face is stored.
13850           (let ((beg (point))
13851                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13852             ;; We display the face.
13853             (if (symbolp gnus-article-x-face-command)
13854                 ;; The command is a lisp function, so we call it.
13855                 (if (gnus-functionp gnus-article-x-face-command)
13856                     (funcall gnus-article-x-face-command beg end)
13857                   (error "%s is not a function" gnus-article-x-face-command))
13858               ;; The command is a string, so we interpret the command
13859               ;; as a, well, command, and fork it off.
13860               (let ((process-connection-type nil))
13861                 (process-kill-without-query
13862                  (start-process
13863                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13864                 (process-send-region "gnus-x-face" beg end)
13865                 (process-send-eof "gnus-x-face")))))))))
13866
13867 (defun gnus-headers-decode-quoted-printable ()
13868   "Hack to remove QP encoding from headers."
13869   (let ((case-fold-search t)
13870         (inhibit-point-motion-hooks t)
13871         string)
13872     (goto-char (point-min))
13873     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13874       (setq string (match-string 1))
13875       (narrow-to-region (match-beginning 0) (match-end 0))
13876       (delete-region (point-min) (point-max))
13877       (insert string)
13878       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13879       (subst-char-in-region (point-min) (point-max) ?_ ? )
13880       (widen)
13881       (goto-char (point-min)))))
13882
13883 (defun gnus-article-de-quoted-unreadable (&optional force)
13884   "Do a naive translation of a quoted-printable-encoded article.
13885 This is in no way, shape or form meant as a replacement for real MIME
13886 processing, but is simply a stop-gap measure until MIME support is
13887 written.
13888 If FORCE, decode the article whether it is marked as quoted-printable
13889 or not."
13890   (interactive (list 'force))
13891   (save-excursion
13892     (set-buffer gnus-article-buffer)
13893     (let ((case-fold-search t)
13894           (buffer-read-only nil)
13895           (type (gnus-fetch-field "content-transfer-encoding")))
13896       (when (or force
13897                 (and type (string-match "quoted-printable" (downcase type))))
13898         (gnus-headers-decode-quoted-printable)
13899         (goto-char (point-min))
13900         (search-forward "\n\n" nil 'move)
13901         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13902
13903 (defun gnus-mime-decode-quoted-printable (from to)
13904   "Decode Quoted-Printable in the region between FROM and TO."
13905   (goto-char from)
13906   (while (search-forward "=" to t)
13907     (cond ((eq (following-char) ?\n)
13908            (delete-char -1)
13909            (delete-char 1))
13910           ((looking-at "[0-9A-F][0-9A-F]")
13911            (delete-char -1)
13912            (insert (hexl-hex-string-to-integer
13913                     (buffer-substring (point) (+ 2 (point)))))
13914            (delete-char 2))
13915           ((looking-at "=")
13916            (delete-char 1))
13917           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13918
13919 (defun gnus-article-hide-pgp (&optional arg)
13920   "Toggle hiding of any PGP headers and signatures in the current article.
13921 If given a negative prefix, always show; if given a positive prefix,
13922 always hide."
13923   (interactive "P")
13924   (unless (gnus-article-check-hidden-text 'pgp arg)
13925     (save-excursion
13926       (set-buffer gnus-article-buffer)
13927       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13928             buffer-read-only beg end)
13929         (widen)
13930         (goto-char (point-min))
13931         ;; Hide the "header".
13932         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13933              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13934         (setq beg (point))
13935         ;; Hide the actual signature.
13936         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13937              (setq end (1+ (match-beginning 0)))
13938              (gnus-hide-text
13939               end
13940               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13941                   (match-end 0)
13942                 ;; Perhaps we shouldn't hide to the end of the buffer
13943                 ;; if there is no end to the signature?
13944                 (point-max))
13945               props))
13946         ;; Hide "- " PGP quotation markers.
13947         (when (and beg end)
13948           (narrow-to-region beg end)
13949           (goto-char (point-min))
13950           (while (re-search-forward "^- " nil t)
13951             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13952           (widen))))))
13953
13954 (defun gnus-article-hide-signature (&optional arg)
13955   "Hide the signature in the current article.
13956 If given a negative prefix, always show; if given a positive prefix,
13957 always hide."
13958   (interactive "P")
13959   (unless (gnus-article-check-hidden-text 'signature arg)
13960     (save-excursion
13961       (set-buffer gnus-article-buffer)
13962       (save-restriction
13963         (let ((buffer-read-only nil))
13964           (when (gnus-narrow-to-signature)
13965             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13966
13967 (defun gnus-article-strip-leading-blank-lines ()
13968   "Remove all blank lines from the beginning of the article."
13969   (interactive)
13970   (save-excursion
13971     (set-buffer gnus-article-buffer)
13972     (let (buffer-read-only)
13973       (goto-char (point-min))
13974       (when (search-forward "\n\n" nil t)
13975         (while (looking-at "[ \t]$")
13976           (gnus-delete-line))))))
13977
13978 (defun gnus-narrow-to-signature ()
13979   "Narrow to the signature."
13980   (widen)
13981   (goto-char (point-max))
13982   (when (re-search-backward gnus-signature-separator nil t)
13983     (forward-line 1)
13984     (when (or (null gnus-signature-limit)
13985               (and (numberp gnus-signature-limit)
13986                    (< (- (point-max) (point)) gnus-signature-limit))
13987               (and (gnus-functionp gnus-signature-limit)
13988                    (funcall gnus-signature-limit))
13989               (and (stringp gnus-signature-limit)
13990                    (not (re-search-forward gnus-signature-limit nil t))))
13991       (narrow-to-region (point) (point-max))
13992       t)))
13993
13994 (defun gnus-article-check-hidden-text (type arg)
13995   "Return nil if hiding is necessary."
13996   (save-excursion
13997     (set-buffer gnus-article-buffer)
13998     (let ((hide (gnus-article-hidden-text-p type)))
13999       (cond ((or (and (null arg) (eq hide 'hidden))
14000                  (and arg (< 0 (prefix-numeric-value arg))))
14001              (gnus-article-show-hidden-text type))
14002             ((eq hide 'shown)
14003              (gnus-article-show-hidden-text type t))
14004             (t nil)))))
14005
14006 (defun gnus-article-hidden-text-p (type)
14007   "Say whether the current buffer contains hidden text of type TYPE."
14008   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14009     (when pos
14010       (if (get-text-property pos 'invisible)
14011           'hidden
14012         'shown))))
14013
14014 (defun gnus-article-hide (&optional arg force)
14015   "Hide all the gruft in the current article.
14016 This means that PGP stuff, signatures, cited text and (some)
14017 headers will be hidden.
14018 If given a prefix, show the hidden text instead."
14019   (interactive (list current-prefix-arg 'force))
14020   (gnus-article-hide-headers arg)
14021   (gnus-article-hide-pgp arg)
14022   (gnus-article-hide-citation-maybe arg force)
14023   (gnus-article-hide-signature arg))
14024
14025 (defun gnus-article-show-hidden-text (type &optional hide)
14026   "Show all hidden text of type TYPE.
14027 If HIDE, hide the text instead."
14028   (save-excursion
14029     (set-buffer gnus-article-buffer)
14030     (let ((buffer-read-only nil)
14031           (inhibit-point-motion-hooks t)
14032           (beg (point-min)))
14033       (while (gnus-goto-char (text-property-any
14034                               beg (point-max) 'gnus-type type))
14035         (setq beg (point))
14036         (forward-char)
14037         (if hide
14038             (gnus-hide-text beg (point) gnus-hidden-properties)
14039           (gnus-unhide-text beg (point)))
14040         (setq beg (point)))
14041       t)))
14042
14043 (defvar gnus-article-time-units
14044   `((year . ,(* 365.25 24 60 60))
14045     (week . ,(* 7 24 60 60))
14046     (day . ,(* 24 60 60))
14047     (hour . ,(* 60 60))
14048     (minute . 60)
14049     (second . 1))
14050   "Mapping from time units to seconds.")
14051
14052 (defun gnus-article-date-ut (&optional type highlight)
14053   "Convert DATE date to universal time in the current article.
14054 If TYPE is `local', convert to local time; if it is `lapsed', output
14055 how much time has lapsed since DATE."
14056   (interactive (list 'ut t))
14057   (let* ((header (or gnus-current-headers
14058                      (gnus-summary-article-header) ""))
14059          (date (and (vectorp header) (mail-header-date header)))
14060          (date-regexp "^Date: \\|^X-Sent: ")
14061          (now (current-time))
14062          (inhibit-point-motion-hooks t)
14063          bface eface)
14064     (when (and date (not (string= date "")))
14065       (save-excursion
14066         (set-buffer gnus-article-buffer)
14067         (save-restriction
14068           (nnheader-narrow-to-headers)
14069           (let ((buffer-read-only nil))
14070             ;; Delete any old Date headers.
14071             (if (re-search-forward date-regexp nil t)
14072                 (progn
14073                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14074                         eface (get-text-property (gnus-point-at-eol) 'face))
14075                   (message-remove-header date-regexp t)
14076                   (beginning-of-line))
14077               (goto-char (point-max)))
14078             (insert
14079              (cond
14080               ;; Convert to the local timezone.  We have to slap a
14081               ;; `condition-case' round the calls to the timezone
14082               ;; functions since they aren't particularly resistant to
14083               ;; buggy dates.
14084               ((eq type 'local)
14085                (concat "Date: " (condition-case ()
14086                                     (timezone-make-date-arpa-standard date)
14087                                   (error date))
14088                        "\n"))
14089               ;; Convert to Universal Time.
14090               ((eq type 'ut)
14091                (concat "Date: "
14092                        (condition-case ()
14093                            (timezone-make-date-arpa-standard date nil "UT")
14094                          (error date))
14095                        "\n"))
14096               ;; Get the original date from the article.
14097               ((eq type 'original)
14098                (concat "Date: " date "\n"))
14099               ;; Do an X-Sent lapsed format.
14100               ((eq type 'lapsed)
14101                ;; If the date is seriously mangled, the timezone
14102                ;; functions are liable to bug out, so we condition-case
14103                ;; the entire thing.
14104                (let* ((real-time
14105                        (condition-case ()
14106                            (gnus-time-minus
14107                             (gnus-encode-date
14108                              (timezone-make-date-arpa-standard
14109                               (current-time-string now)
14110                               (current-time-zone now) "UT"))
14111                             (gnus-encode-date
14112                              (timezone-make-date-arpa-standard
14113                               date nil "UT")))
14114                          (error '(0 0))))
14115                       (real-sec (+ (* (float (car real-time)) 65536)
14116                                    (cadr real-time)))
14117                       (sec (abs real-sec))
14118                       num prev)
14119                  (if (zerop sec)
14120                      "X-Sent: Now\n"
14121                    (concat
14122                     "X-Sent: "
14123                     ;; This is a bit convoluted, but basically we go
14124                     ;; through the time units for years, weeks, etc,
14125                     ;; and divide things to see whether that results
14126                     ;; in positive answers.
14127                     (mapconcat
14128                      (lambda (unit)
14129                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14130                            ;; The (remaining) seconds are too few to
14131                            ;; be divided into this time unit.
14132                            ""
14133                          ;; It's big enough, so we output it.
14134                          (setq sec (- sec (* num (cdr unit))))
14135                          (prog1
14136                              (concat (if prev ", " "") (int-to-string
14137                                                         (floor num))
14138                                      " " (symbol-name (car unit))
14139                                      (if (> num 1) "s" ""))
14140                            (setq prev t))))
14141                      gnus-article-time-units "")
14142                     ;; If dates are odd, then it might appear like the
14143                     ;; article was sent in the future.
14144                     (if (> real-sec 0)
14145                         " ago\n"
14146                       " in the future\n")))))
14147               (t
14148                (error "Unknown conversion type: %s" type)))))
14149           ;; Do highlighting.
14150           (beginning-of-line)
14151           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14152                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14153             (put-text-property (match-beginning 1) (match-end 1)
14154                                'face bface)
14155             (put-text-property (match-beginning 2) (match-end 2)
14156                                'face eface)))))))
14157
14158 (defun gnus-article-date-local (&optional highlight)
14159   "Convert the current article date to the local timezone."
14160   (interactive (list t))
14161   (gnus-article-date-ut 'local highlight))
14162
14163 (defun gnus-article-date-original (&optional highlight)
14164   "Convert the current article date to what it was originally.
14165 This is only useful if you have used some other date conversion
14166 function and want to see what the date was before converting."
14167   (interactive (list t))
14168   (gnus-article-date-ut 'original highlight))
14169
14170 (defun gnus-article-date-lapsed (&optional highlight)
14171   "Convert the current article date to time lapsed since it was sent."
14172   (interactive (list t))
14173   (gnus-article-date-ut 'lapsed highlight))
14174
14175 (defun gnus-article-maybe-highlight ()
14176   "Do some article highlighting if `gnus-visual' is non-nil."
14177   (if (gnus-visual-p 'article-highlight 'highlight)
14178       (gnus-article-highlight-some)))
14179
14180 ;; Article savers.
14181
14182 (defun gnus-output-to-rmail (file-name)
14183   "Append the current article to an Rmail file named FILE-NAME."
14184   (require 'rmail)
14185   ;; Most of these codes are borrowed from rmailout.el.
14186   (setq file-name (expand-file-name file-name))
14187   (setq rmail-default-rmail-file file-name)
14188   (let ((artbuf (current-buffer))
14189         (tmpbuf (get-buffer-create " *Gnus-output*")))
14190     (save-excursion
14191       (or (get-file-buffer file-name)
14192           (file-exists-p file-name)
14193           (if (gnus-yes-or-no-p
14194                (concat "\"" file-name "\" does not exist, create it? "))
14195               (let ((file-buffer (create-file-buffer file-name)))
14196                 (save-excursion
14197                   (set-buffer file-buffer)
14198                   (rmail-insert-rmail-file-header)
14199                   (let ((require-final-newline nil))
14200                     (write-region (point-min) (point-max) file-name t 1)))
14201                 (kill-buffer file-buffer))
14202             (error "Output file does not exist")))
14203       (set-buffer tmpbuf)
14204       (buffer-disable-undo (current-buffer))
14205       (erase-buffer)
14206       (insert-buffer-substring artbuf)
14207       (gnus-convert-article-to-rmail)
14208       ;; Decide whether to append to a file or to an Emacs buffer.
14209       (let ((outbuf (get-file-buffer file-name)))
14210         (if (not outbuf)
14211             (append-to-file (point-min) (point-max) file-name)
14212           ;; File has been visited, in buffer OUTBUF.
14213           (set-buffer outbuf)
14214           (let ((buffer-read-only nil)
14215                 (msg (and (boundp 'rmail-current-message)
14216                           (symbol-value 'rmail-current-message))))
14217             ;; If MSG is non-nil, buffer is in RMAIL mode.
14218             (if msg
14219                 (progn (widen)
14220                        (narrow-to-region (point-max) (point-max))))
14221             (insert-buffer-substring tmpbuf)
14222             (if msg
14223                 (progn
14224                   (goto-char (point-min))
14225                   (widen)
14226                   (search-backward "\^_")
14227                   (narrow-to-region (point) (point-max))
14228                   (goto-char (1+ (point-min)))
14229                   (rmail-count-new-messages t)
14230                   (rmail-show-message msg)))))))
14231     (kill-buffer tmpbuf)))
14232
14233 (defun gnus-output-to-file (file-name)
14234   "Append the current article to a file named FILE-NAME."
14235   (setq file-name (expand-file-name file-name))
14236   (let ((artbuf (current-buffer))
14237         (tmpbuf (get-buffer-create " *Gnus-output*")))
14238     (save-excursion
14239       (set-buffer tmpbuf)
14240       (buffer-disable-undo (current-buffer))
14241       (erase-buffer)
14242       (insert-buffer-substring artbuf)
14243       ;; Append newline at end of the buffer as separator, and then
14244       ;; save it to file.
14245       (goto-char (point-max))
14246       (insert "\n")
14247       (append-to-file (point-min) (point-max) file-name))
14248     (kill-buffer tmpbuf)))
14249
14250 (defun gnus-convert-article-to-rmail ()
14251   "Convert article in current buffer to Rmail message format."
14252   (let ((buffer-read-only nil))
14253     ;; Convert article directly into Babyl format.
14254     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14255     (goto-char (point-min))
14256     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14257     (while (search-forward "\n\^_" nil t) ;single char
14258       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14259     (goto-char (point-max))
14260     (insert "\^_")))
14261
14262 (defun gnus-narrow-to-page (&optional arg)
14263   "Narrow the article buffer to a page.
14264 If given a numerical ARG, move forward ARG pages."
14265   (interactive "P")
14266   (setq arg (if arg (prefix-numeric-value arg) 0))
14267   (save-excursion
14268     (set-buffer gnus-article-buffer)
14269     (goto-char (point-min))
14270     (widen)
14271     (when (gnus-visual-p 'page-marker)
14272       (let ((buffer-read-only nil))
14273         (gnus-remove-text-with-property 'gnus-prev)
14274         (gnus-remove-text-with-property 'gnus-next)))
14275     (when
14276         (cond ((< arg 0)
14277                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14278               ((> arg 0)
14279                (re-search-forward page-delimiter nil 'move arg)))
14280       (goto-char (match-end 0)))
14281     (narrow-to-region
14282      (point)
14283      (if (re-search-forward page-delimiter nil 'move)
14284          (match-beginning 0)
14285        (point)))
14286     (when (and (gnus-visual-p 'page-marker)
14287                (not (= (point-min) 1)))
14288       (save-excursion
14289         (goto-char (point-min))
14290         (gnus-insert-prev-page-button)))
14291     (when (and (gnus-visual-p 'page-marker)
14292                (not (= (1- (point-max)) (buffer-size))))
14293       (save-excursion
14294         (goto-char (point-max))
14295         (gnus-insert-next-page-button)))))
14296
14297 ;; Article mode commands
14298
14299 (defun gnus-article-goto-next-page ()
14300   "Show the next page of the article."
14301   (interactive)
14302   (when (gnus-article-next-page)
14303     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14304
14305 (defun gnus-article-goto-prev-page ()
14306   "Show the next page of the article."
14307   (interactive)
14308   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14309     (gnus-article-prev-page nil)))
14310
14311 (defun gnus-article-next-page (&optional lines)
14312   "Show the next page of the current article.
14313 If end of article, return non-nil.  Otherwise return nil.
14314 Argument LINES specifies lines to be scrolled up."
14315   (interactive "p")
14316   (move-to-window-line -1)
14317   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14318   (if (save-excursion
14319         (end-of-line)
14320         (and (pos-visible-in-window-p)  ;Not continuation line.
14321              (eobp)))
14322       ;; Nothing in this page.
14323       (if (or (not gnus-break-pages)
14324               (save-excursion
14325                 (save-restriction
14326                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14327           t                             ;Nothing more.
14328         (gnus-narrow-to-page 1)         ;Go to next page.
14329         nil)
14330     ;; More in this page.
14331     (condition-case ()
14332         (scroll-up lines)
14333       (end-of-buffer
14334        ;; Long lines may cause an end-of-buffer error.
14335        (goto-char (point-max))))
14336     (move-to-window-line 0)
14337     nil))
14338
14339 (defun gnus-article-prev-page (&optional lines)
14340   "Show previous page of current article.
14341 Argument LINES specifies lines to be scrolled down."
14342   (interactive "p")
14343   (move-to-window-line 0)
14344   (if (and gnus-break-pages
14345            (bobp)
14346            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14347       (progn
14348         (gnus-narrow-to-page -1)        ;Go to previous page.
14349         (goto-char (point-max))
14350         (recenter -1))
14351     (prog1
14352         (condition-case ()
14353             (scroll-down lines)
14354           (error nil))
14355       (move-to-window-line 0))))
14356
14357 (defun gnus-article-refer-article ()
14358   "Read article specified by message-id around point."
14359   (interactive)
14360   (let ((point (point)))
14361     (search-forward ">" nil t)          ;Move point to end of "<....>".
14362     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14363         (let ((message-id (match-string 1)))
14364           (goto-char point)
14365           (set-buffer gnus-summary-buffer)
14366           (gnus-summary-refer-article message-id))
14367       (goto-char (point))
14368       (error "No references around point"))))
14369
14370 (defun gnus-article-show-summary ()
14371   "Reconfigure windows to show summary buffer."
14372   (interactive)
14373   (gnus-configure-windows 'article)
14374   (gnus-summary-goto-subject gnus-current-article))
14375
14376 (defun gnus-article-describe-briefly ()
14377   "Describe article mode commands briefly."
14378   (interactive)
14379   (gnus-message 6
14380                 (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")))
14381
14382 (defun gnus-article-summary-command ()
14383   "Execute the last keystroke in the summary buffer."
14384   (interactive)
14385   (let ((obuf (current-buffer))
14386         (owin (current-window-configuration))
14387         func)
14388     (switch-to-buffer gnus-summary-buffer 'norecord)
14389     (setq func (lookup-key (current-local-map) (this-command-keys)))
14390     (call-interactively func)
14391     (set-buffer obuf)
14392     (set-window-configuration owin)
14393     (set-window-point (get-buffer-window (current-buffer)) (point))))
14394
14395 (defun gnus-article-summary-command-nosave ()
14396   "Execute the last keystroke in the summary buffer."
14397   (interactive)
14398   (let (func)
14399     (pop-to-buffer gnus-summary-buffer 'norecord)
14400     (setq func (lookup-key (current-local-map) (this-command-keys)))
14401     (call-interactively func)))
14402
14403 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14404   "Read a summary buffer key sequence and execute it from the article buffer."
14405   (interactive "P")
14406   (let ((nosaves
14407          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14408            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14409            "=" "^" "\M-^"))
14410         keys)
14411     (save-excursion
14412       (set-buffer gnus-summary-buffer)
14413       (push (or key last-command-event) unread-command-events)
14414       (setq keys (read-key-sequence nil)))
14415     (message "")
14416
14417     (if (member keys nosaves)
14418         (let (func)
14419           (pop-to-buffer gnus-summary-buffer 'norecord)
14420           (if (setq func (lookup-key (current-local-map) keys))
14421               (call-interactively func)
14422             (ding)))
14423       (let ((obuf (current-buffer))
14424             (owin (current-window-configuration))
14425             (opoint (point))
14426             func in-buffer)
14427         (if not-restore-window
14428             (pop-to-buffer gnus-summary-buffer 'norecord)
14429           (switch-to-buffer gnus-summary-buffer 'norecord))
14430         (setq in-buffer (current-buffer))
14431         (if (setq func (lookup-key (current-local-map) keys))
14432             (call-interactively func)
14433           (ding))
14434         (when (eq in-buffer (current-buffer))
14435           (set-buffer obuf)
14436           (unless not-restore-window
14437             (set-window-configuration owin))
14438           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14439
14440 \f
14441 ;;;
14442 ;;; Kill file handling.
14443 ;;;
14444
14445 ;;;###autoload
14446 (defalias 'gnus-batch-kill 'gnus-batch-score)
14447 ;;;###autoload
14448 (defun gnus-batch-score ()
14449   "Run batched scoring.
14450 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14451 Newsgroups is a list of strings in Bnews format.  If you want to score
14452 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14453 score the alt hierarchy, you'd say \"!alt.all\"."
14454   (interactive)
14455   (let* ((yes-and-no
14456           (gnus-newsrc-parse-options
14457            (apply (function concat)
14458                   (mapcar (lambda (g) (concat g " "))
14459                           command-line-args-left))))
14460          (gnus-expert-user t)
14461          (nnmail-spool-file nil)
14462          (gnus-use-dribble-file nil)
14463          (yes (car yes-and-no))
14464          (no (cdr yes-and-no))
14465          group newsrc entry
14466          ;; Disable verbose message.
14467          gnus-novice-user gnus-large-newsgroup)
14468     ;; Eat all arguments.
14469     (setq command-line-args-left nil)
14470     ;; Start Gnus.
14471     (gnus)
14472     ;; Apply kills to specified newsgroups in command line arguments.
14473     (setq newsrc (cdr gnus-newsrc-alist))
14474     (while newsrc
14475       (setq group (caar newsrc))
14476       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14477       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14478                (and (car entry)
14479                     (or (eq (car entry) t)
14480                         (not (zerop (car entry)))))
14481                (if yes (string-match yes group) t)
14482                (or (null no) (not (string-match no group))))
14483           (progn
14484             (gnus-summary-read-group group nil t nil t)
14485             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14486                  (gnus-summary-exit))))
14487       (setq newsrc (cdr newsrc)))
14488     ;; Exit Emacs.
14489     (switch-to-buffer gnus-group-buffer)
14490     (gnus-group-save-newsrc)))
14491
14492 (defun gnus-apply-kill-file ()
14493   "Apply a kill file to the current newsgroup.
14494 Returns the number of articles marked as read."
14495   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14496           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14497       (gnus-apply-kill-file-internal)
14498     0))
14499
14500 (defun gnus-kill-save-kill-buffer ()
14501   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14502     (when (get-file-buffer file)
14503       (save-excursion
14504         (set-buffer (get-file-buffer file))
14505         (and (buffer-modified-p) (save-buffer))
14506         (kill-buffer (current-buffer))))))
14507
14508 (defvar gnus-kill-file-name "KILL"
14509   "Suffix of the kill files.")
14510
14511 (defun gnus-newsgroup-kill-file (newsgroup)
14512   "Return the name of a kill file name for NEWSGROUP.
14513 If NEWSGROUP is nil, return the global kill file name instead."
14514   (cond 
14515    ;; The global KILL file is placed at top of the directory.
14516    ((or (null newsgroup)
14517         (string-equal newsgroup ""))
14518     (expand-file-name gnus-kill-file-name
14519                       (or gnus-kill-files-directory "~/News")))
14520    ;; Append ".KILL" to newsgroup name.
14521    ((gnus-use-long-file-name 'not-kill)
14522     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14523                               "." gnus-kill-file-name)
14524                       (or gnus-kill-files-directory "~/News")))
14525    ;; Place "KILL" under the hierarchical directory.
14526    (t
14527     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14528                               "/" gnus-kill-file-name)
14529                       (or gnus-kill-files-directory "~/News")))))
14530
14531 \f
14532 ;;;
14533 ;;; Dribble file
14534 ;;;
14535
14536 (defvar gnus-dribble-ignore nil)
14537 (defvar gnus-dribble-eval-file nil)
14538
14539 (defun gnus-dribble-file-name ()
14540   "Return the dribble file for the current .newsrc."
14541   (concat
14542    (if gnus-dribble-directory
14543        (concat (file-name-as-directory gnus-dribble-directory)
14544                (file-name-nondirectory gnus-current-startup-file))
14545      gnus-current-startup-file)
14546    "-dribble"))
14547
14548 (defun gnus-dribble-enter (string)
14549   "Enter STRING into the dribble buffer."
14550   (if (and (not gnus-dribble-ignore)
14551            gnus-dribble-buffer
14552            (buffer-name gnus-dribble-buffer))
14553       (let ((obuf (current-buffer)))
14554         (set-buffer gnus-dribble-buffer)
14555         (insert string "\n")
14556         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14557         (set-buffer obuf))))
14558
14559 (defun gnus-dribble-read-file ()
14560   "Read the dribble file from disk."
14561   (let ((dribble-file (gnus-dribble-file-name)))
14562     (save-excursion
14563       (set-buffer (setq gnus-dribble-buffer
14564                         (get-buffer-create
14565                          (file-name-nondirectory dribble-file))))
14566       (gnus-add-current-to-buffer-list)
14567       (erase-buffer)
14568       (setq buffer-file-name dribble-file)
14569       (auto-save-mode t)
14570       (buffer-disable-undo (current-buffer))
14571       (bury-buffer (current-buffer))
14572       (set-buffer-modified-p nil)
14573       (let ((auto (make-auto-save-file-name))
14574             (gnus-dribble-ignore t))
14575         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14576           ;; Load whichever file is newest -- the auto save file
14577           ;; or the "real" file.
14578           (if (file-newer-than-file-p auto dribble-file)
14579               (insert-file-contents auto)
14580             (insert-file-contents dribble-file))
14581           (unless (zerop (buffer-size))
14582             (set-buffer-modified-p t))
14583           ;; Set the file modes to reflect the .newsrc file modes.
14584           (save-buffer)
14585           (when (file-exists-p gnus-current-startup-file)
14586             (set-file-modes dribble-file
14587                             (file-modes gnus-current-startup-file)))
14588           ;; Possibly eval the file later.
14589           (when (gnus-y-or-n-p
14590                  "Auto-save file exists.  Do you want to read it? ")
14591             (setq gnus-dribble-eval-file t)))))))
14592
14593 (defun gnus-dribble-eval-file ()
14594   (if (not gnus-dribble-eval-file)
14595       ()
14596     (setq gnus-dribble-eval-file nil)
14597     (save-excursion
14598       (let ((gnus-dribble-ignore t))
14599         (set-buffer gnus-dribble-buffer)
14600         (eval-buffer (current-buffer))))))
14601
14602 (defun gnus-dribble-delete-file ()
14603   (if (file-exists-p (gnus-dribble-file-name))
14604       (delete-file (gnus-dribble-file-name)))
14605   (if gnus-dribble-buffer
14606       (save-excursion
14607         (set-buffer gnus-dribble-buffer)
14608         (let ((auto (make-auto-save-file-name)))
14609           (if (file-exists-p auto)
14610               (delete-file auto))
14611           (erase-buffer)
14612           (set-buffer-modified-p nil)))))
14613
14614 (defun gnus-dribble-save ()
14615   (if (and gnus-dribble-buffer
14616            (buffer-name gnus-dribble-buffer))
14617       (save-excursion
14618         (set-buffer gnus-dribble-buffer)
14619         (save-buffer))))
14620
14621 (defun gnus-dribble-clear ()
14622   (save-excursion
14623     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14624         (progn
14625           (set-buffer gnus-dribble-buffer)
14626           (erase-buffer)
14627           (set-buffer-modified-p nil)
14628           (setq buffer-saved-size (buffer-size))))))
14629
14630 \f
14631 ;;;
14632 ;;; Server Communication
14633 ;;;
14634
14635 (defun gnus-start-news-server (&optional confirm)
14636   "Open a method for getting news.
14637 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14638   (let (how)
14639     (if gnus-current-select-method
14640         ;; Stream is already opened.
14641         nil
14642       ;; Open NNTP server.
14643       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14644       (if confirm
14645           (progn
14646             ;; Read server name with completion.
14647             (setq gnus-nntp-server
14648                   (completing-read "NNTP server: "
14649                                    (mapcar (lambda (server) (list server))
14650                                            (cons (list gnus-nntp-server)
14651                                                  gnus-secondary-servers))
14652                                    nil nil gnus-nntp-server))))
14653
14654       (if (and gnus-nntp-server
14655                (stringp gnus-nntp-server)
14656                (not (string= gnus-nntp-server "")))
14657           (setq gnus-select-method
14658                 (cond ((or (string= gnus-nntp-server "")
14659                            (string= gnus-nntp-server "::"))
14660                        (list 'nnspool (system-name)))
14661                       ((string-match "^:" gnus-nntp-server)
14662                        (list 'nnmh gnus-nntp-server
14663                              (list 'nnmh-directory
14664                                    (file-name-as-directory
14665                                     (expand-file-name
14666                                      (concat "~/" (substring
14667                                                    gnus-nntp-server 1)))))
14668                              (list 'nnmh-get-new-mail nil)))
14669                       (t
14670                        (list 'nntp gnus-nntp-server)))))
14671
14672       (setq how (car gnus-select-method))
14673       (cond ((eq how 'nnspool)
14674              (require 'nnspool)
14675              (gnus-message 5 "Looking up local news spool..."))
14676             ((eq how 'nnmh)
14677              (require 'nnmh)
14678              (gnus-message 5 "Looking up mh spool..."))
14679             (t
14680              (require 'nntp)))
14681       (setq gnus-current-select-method gnus-select-method)
14682       (run-hooks 'gnus-open-server-hook)
14683       (or
14684        ;; gnus-open-server-hook might have opened it
14685        (gnus-server-opened gnus-select-method)
14686        (gnus-open-server gnus-select-method)
14687        (gnus-y-or-n-p
14688         (format
14689          "%s (%s) open error: '%s'.     Continue? "
14690          (car gnus-select-method) (cadr gnus-select-method)
14691          (gnus-status-message gnus-select-method)))
14692        (gnus-error 1 "Couldn't open server on %s"
14693                    (nth 1 gnus-select-method))))))
14694
14695 (defun gnus-check-group (group)
14696   "Try to make sure that the server where GROUP exists is alive."
14697   (let ((method (gnus-find-method-for-group group)))
14698     (or (gnus-server-opened method)
14699         (gnus-open-server method))))
14700
14701 (defun gnus-check-server (&optional method silent)
14702   "Check whether the connection to METHOD is down.
14703 If METHOD is nil, use `gnus-select-method'.
14704 If it is down, start it up (again)."
14705   (let ((method (or method gnus-select-method)))
14706     ;; Transform virtual server names into select methods.
14707     (when (stringp method)
14708       (setq method (gnus-server-to-method method)))
14709     (if (gnus-server-opened method)
14710         ;; The stream is already opened.
14711         t
14712       ;; Open the server.
14713       (unless silent
14714         (gnus-message 5 "Opening %s server%s..." (car method)
14715                       (if (equal (nth 1 method) "") ""
14716                         (format " on %s" (nth 1 method)))))
14717       (run-hooks 'gnus-open-server-hook)
14718       (prog1
14719           (gnus-open-server method)
14720         (unless silent
14721           (message ""))))))
14722
14723 (defun gnus-get-function (method function)
14724   "Return a function symbol based on METHOD and FUNCTION."
14725   ;; Translate server names into methods.
14726   (unless method
14727     (error "Attempted use of a nil select method"))
14728   (when (stringp method)
14729     (setq method (gnus-server-to-method method)))
14730   (let ((func (intern (format "%s-%s" (car method) function))))
14731     ;; If the functions isn't bound, we require the backend in
14732     ;; question.
14733     (unless (fboundp func)
14734       (require (car method))
14735       (unless (fboundp func)
14736         ;; This backend doesn't implement this function.
14737         (error "No such function: %s" func)))
14738     func))
14739
14740 \f
14741 ;;;
14742 ;;; Interface functions to the backends.
14743 ;;;
14744
14745 (defun gnus-open-server (method)
14746   "Open a connection to METHOD."
14747   (when (stringp method)
14748     (setq method (gnus-server-to-method method)))
14749   (let ((elem (assoc method gnus-opened-servers)))
14750     ;; If this method was previously denied, we just return nil.
14751     (if (eq (nth 1 elem) 'denied)
14752         (progn
14753           (gnus-message 1 "Denied server")
14754           nil)
14755       ;; Open the server.
14756       (let ((result
14757              (funcall (gnus-get-function method 'open-server)
14758                       (nth 1 method) (nthcdr 2 method))))
14759         ;; If this hasn't been opened before, we add it to the list.
14760         (unless elem
14761           (setq elem (list method nil)
14762                 gnus-opened-servers (cons elem gnus-opened-servers)))
14763         ;; Set the status of this server.
14764         (setcar (cdr elem) (if result 'ok 'denied))
14765         ;; Return the result from the "open" call.
14766         result))))
14767
14768 (defun gnus-close-server (method)
14769   "Close the connection to METHOD."
14770   (when (stringp method)
14771     (setq method (gnus-server-to-method method)))
14772   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14773
14774 (defun gnus-request-list (method)
14775   "Request the active file from METHOD."
14776   (when (stringp method)
14777     (setq method (gnus-server-to-method method)))
14778   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14779
14780 (defun gnus-request-list-newsgroups (method)
14781   "Request the newsgroups file from METHOD."
14782   (when (stringp method)
14783     (setq method (gnus-server-to-method method)))
14784   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14785
14786 (defun gnus-request-newgroups (date method)
14787   "Request all new groups since DATE from METHOD."
14788   (when (stringp method)
14789     (setq method (gnus-server-to-method method)))
14790   (funcall (gnus-get-function method 'request-newgroups)
14791            date (nth 1 method)))
14792
14793 (defun gnus-server-opened (method)
14794   "Check whether a connection to METHOD has been opened."
14795   (when (stringp method)
14796     (setq method (gnus-server-to-method method)))
14797   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14798
14799 (defun gnus-status-message (method)
14800   "Return the status message from METHOD.
14801 If METHOD is a string, it is interpreted as a group name.   The method
14802 this group uses will be queried."
14803   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14804                   method)))
14805     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14806
14807 (defun gnus-request-group (group &optional dont-check method)
14808   "Request GROUP.  If DONT-CHECK, no information is required."
14809   (let ((method (or method (gnus-find-method-for-group group))))
14810     (when (stringp method)
14811       (setq method (gnus-server-to-method method)))
14812     (funcall (gnus-get-function method 'request-group)
14813              (gnus-group-real-name group) (nth 1 method) dont-check)))
14814
14815 (defun gnus-request-asynchronous (group &optional articles)
14816   "Request that GROUP behave asynchronously.
14817 ARTICLES is the `data' of the group."
14818   (let ((method (gnus-find-method-for-group group)))
14819     (funcall (gnus-get-function method 'request-asynchronous)
14820              (gnus-group-real-name group) (nth 1 method) articles)))
14821
14822 (defun gnus-list-active-group (group)
14823   "Request active information on GROUP."
14824   (let ((method (gnus-find-method-for-group group))
14825         (func 'list-active-group))
14826     (when (gnus-check-backend-function func group)
14827       (funcall (gnus-get-function method func)
14828                (gnus-group-real-name group) (nth 1 method)))))
14829
14830 (defun gnus-request-group-description (group)
14831   "Request a description of GROUP."
14832   (let ((method (gnus-find-method-for-group group))
14833         (func 'request-group-description))
14834     (when (gnus-check-backend-function func group)
14835       (funcall (gnus-get-function method func)
14836                (gnus-group-real-name group) (nth 1 method)))))
14837
14838 (defun gnus-close-group (group)
14839   "Request the GROUP be closed."
14840   (let ((method (gnus-find-method-for-group group)))
14841     (funcall (gnus-get-function method 'close-group)
14842              (gnus-group-real-name group) (nth 1 method))))
14843
14844 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14845   "Request headers for ARTICLES in GROUP.
14846 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14847   (let ((method (gnus-find-method-for-group group)))
14848     (if (and gnus-use-cache (numberp (car articles)))
14849         (gnus-cache-retrieve-headers articles group fetch-old)
14850       (funcall (gnus-get-function method 'retrieve-headers)
14851                articles (gnus-group-real-name group) (nth 1 method)
14852                fetch-old))))
14853
14854 (defun gnus-retrieve-groups (groups method)
14855   "Request active information on GROUPS from METHOD."
14856   (when (stringp method)
14857     (setq method (gnus-server-to-method method)))
14858   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14859
14860 (defun gnus-request-type (group &optional article)
14861   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14862   (let ((method (gnus-find-method-for-group group)))
14863     (if (not (gnus-check-backend-function 'request-type (car method)))
14864         'unknown
14865       (funcall (gnus-get-function method 'request-type)
14866                (gnus-group-real-name group) article))))
14867
14868 (defun gnus-request-update-mark (group article mark)
14869   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14870   (let ((method (gnus-find-method-for-group group)))
14871     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14872         mark
14873       (funcall (gnus-get-function method 'request-update-mark)
14874                (gnus-group-real-name group) article mark))))
14875
14876 (defun gnus-request-article (article group &optional buffer)
14877   "Request the ARTICLE in GROUP.
14878 ARTICLE can either be an article number or an article Message-ID.
14879 If BUFFER, insert the article in that group."
14880   (let ((method (gnus-find-method-for-group group)))
14881     (funcall (gnus-get-function method 'request-article)
14882              article (gnus-group-real-name group) (nth 1 method) buffer)))
14883
14884 (defun gnus-request-head (article group)
14885   "Request the head of ARTICLE in GROUP."
14886   (let ((method (gnus-find-method-for-group group)))
14887     (funcall (gnus-get-function method 'request-head)
14888              article (gnus-group-real-name group) (nth 1 method))))
14889
14890 (defun gnus-request-body (article group)
14891   "Request the body of ARTICLE in GROUP."
14892   (let ((method (gnus-find-method-for-group group)))
14893     (funcall (gnus-get-function method 'request-body)
14894              article (gnus-group-real-name group) (nth 1 method))))
14895
14896 (defun gnus-request-post (method)
14897   "Post the current buffer using METHOD."
14898   (when (stringp method)
14899     (setq method (gnus-server-to-method method)))
14900   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14901
14902 (defun gnus-request-scan (group method)
14903   "Request a SCAN being performed in GROUP from METHOD.
14904 If GROUP is nil, all groups on METHOD are scanned."
14905   (let ((method (if group (gnus-find-method-for-group group) method)))
14906     (funcall (gnus-get-function method 'request-scan)
14907              (and group (gnus-group-real-name group)) (nth 1 method))))
14908
14909 (defsubst gnus-request-update-info (info method)
14910   "Request that METHOD update INFO."
14911   (when (stringp method)
14912     (setq method (gnus-server-to-method method)))
14913   (when (gnus-check-backend-function 'request-update-info (car method))
14914     (funcall (gnus-get-function method 'request-update-info)
14915              (gnus-group-real-name (gnus-info-group info))
14916              info (nth 1 method))))
14917
14918 (defun gnus-request-expire-articles (articles group &optional force)
14919   (let ((method (gnus-find-method-for-group group)))
14920     (funcall (gnus-get-function method 'request-expire-articles)
14921              articles (gnus-group-real-name group) (nth 1 method)
14922              force)))
14923
14924 (defun gnus-request-move-article
14925   (article group server accept-function &optional last)
14926   (let ((method (gnus-find-method-for-group group)))
14927     (funcall (gnus-get-function method 'request-move-article)
14928              article (gnus-group-real-name group)
14929              (nth 1 method) accept-function last)))
14930
14931 (defun gnus-request-accept-article (group method &optional last)
14932   ;; Make sure there's a newline at the end of the article.
14933   (when (stringp method)
14934     (setq method (gnus-server-to-method method)))
14935   (when (and (not method)
14936              (stringp group))
14937     (setq method (gnus-find-method-for-group group)))
14938   (goto-char (point-max))
14939   (unless (bolp)
14940     (insert "\n"))
14941   (let ((func (car (or method (gnus-find-method-for-group group)))))
14942     (funcall (intern (format "%s-request-accept-article" func))
14943              (if (stringp group) (gnus-group-real-name group) group)
14944              (cadr method)
14945              last)))
14946
14947 (defun gnus-request-replace-article (article group buffer)
14948   (let ((func (car (gnus-find-method-for-group group))))
14949     (funcall (intern (format "%s-request-replace-article" func))
14950              article (gnus-group-real-name group) buffer)))
14951
14952 (defun gnus-request-associate-buffer (group)
14953   (let ((method (gnus-find-method-for-group group)))
14954     (funcall (gnus-get-function method 'request-associate-buffer)
14955              (gnus-group-real-name group))))
14956
14957 (defun gnus-request-restore-buffer (article group)
14958   "Request a new buffer restored to the state of ARTICLE."
14959   (let ((method (gnus-find-method-for-group group)))
14960     (funcall (gnus-get-function method 'request-restore-buffer)
14961              article (gnus-group-real-name group) (nth 1 method))))
14962
14963 (defun gnus-request-create-group (group &optional method)
14964   (when (stringp method)
14965     (setq method (gnus-server-to-method method)))
14966   (let ((method (or method (gnus-find-method-for-group group))))
14967     (funcall (gnus-get-function method 'request-create-group)
14968              (gnus-group-real-name group) (nth 1 method))))
14969
14970 (defun gnus-request-delete-group (group &optional force)
14971   (let ((method (gnus-find-method-for-group group)))
14972     (funcall (gnus-get-function method 'request-delete-group)
14973              (gnus-group-real-name group) force (nth 1 method))))
14974
14975 (defun gnus-request-rename-group (group new-name)
14976   (let ((method (gnus-find-method-for-group group)))
14977     (funcall (gnus-get-function method 'request-rename-group)
14978              (gnus-group-real-name group)
14979              (gnus-group-real-name new-name) (nth 1 method))))
14980
14981 (defun gnus-member-of-valid (symbol group)
14982   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14983   (memq symbol (assoc
14984                 (symbol-name (car (gnus-find-method-for-group group)))
14985                 gnus-valid-select-methods)))
14986
14987 (defun gnus-method-option-p (method option)
14988   "Return non-nil if select METHOD has OPTION as a parameter."
14989   (when (stringp method)
14990     (setq method (gnus-server-to-method method)))
14991   (memq option (assoc (format "%s" (car method))
14992                       gnus-valid-select-methods)))
14993
14994 (defun gnus-server-extend-method (group method)
14995   ;; This function "extends" a virtual server.  If the server is
14996   ;; "hello", and the select method is ("hello" (my-var "something"))
14997   ;; in the group "alt.alt", this will result in a new virtual server
14998   ;; called "hello+alt.alt".
14999   (let ((entry
15000          (gnus-copy-sequence
15001           (if (equal (car method) "native") gnus-select-method
15002             (cdr (assoc (car method) gnus-server-alist))))))
15003     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15004     (nconc entry (cdr method))))
15005
15006 (defun gnus-find-method-for-group (group &optional info)
15007   "Find the select method that GROUP uses."
15008   (or gnus-override-method
15009       (and (not group)
15010            gnus-select-method)
15011       (let ((info (or info (gnus-get-info group)))
15012             method)
15013         (if (or (not info)
15014                 (not (setq method (gnus-info-method info)))
15015                 (equal method "native"))
15016             gnus-select-method
15017           (setq method
15018                 (cond ((stringp method)
15019                        (gnus-server-to-method method))
15020                       ((stringp (car method))
15021                        (gnus-server-extend-method group method))
15022                       (t
15023                        method)))
15024           (if (equal (cadr method) "")
15025               method
15026             (gnus-server-add-address method))))))
15027
15028 (defun gnus-check-backend-function (func group)
15029   "Check whether GROUP supports function FUNC."
15030   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15031                   group)))
15032     (fboundp (intern (format "%s-%s" method func)))))
15033
15034 (defun gnus-methods-using (feature)
15035   "Find all methods that have FEATURE."
15036   (let ((valids gnus-valid-select-methods)
15037         outs)
15038     (while valids
15039       (if (memq feature (car valids))
15040           (setq outs (cons (car valids) outs)))
15041       (setq valids (cdr valids)))
15042     outs))
15043
15044 \f
15045 ;;;
15046 ;;; Active & Newsrc File Handling
15047 ;;;
15048
15049 (defun gnus-setup-news (&optional rawfile level dont-connect)
15050   "Setup news information.
15051 If RAWFILE is non-nil, the .newsrc file will also be read.
15052 If LEVEL is non-nil, the news will be set up at level LEVEL."
15053   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15054
15055     (when init 
15056       ;; Clear some variables to re-initialize news information.
15057       (setq gnus-newsrc-alist nil
15058             gnus-active-hashtb nil)
15059       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15060       (gnus-read-newsrc-file rawfile))
15061
15062     (when (and (not (assoc "archive" gnus-server-alist))
15063                gnus-message-archive-method)
15064       (push (cons "archive" gnus-message-archive-method)
15065             gnus-server-alist))
15066
15067     ;; If we don't read the complete active file, we fill in the
15068     ;; hashtb here.
15069     (if (or (null gnus-read-active-file)
15070             (eq gnus-read-active-file 'some))
15071         (gnus-update-active-hashtb-from-killed))
15072
15073     ;; Read the active file and create `gnus-active-hashtb'.
15074     ;; If `gnus-read-active-file' is nil, then we just create an empty
15075     ;; hash table.  The partial filling out of the hash table will be
15076     ;; done in `gnus-get-unread-articles'.
15077     (and gnus-read-active-file
15078          (not level)
15079          (gnus-read-active-file))
15080
15081     (or gnus-active-hashtb
15082         (setq gnus-active-hashtb (make-vector 4095 0)))
15083
15084     ;; Initialize the cache.
15085     (when gnus-use-cache
15086       (gnus-cache-open))
15087
15088     ;; Possibly eval the dribble file.
15089     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15090
15091     (gnus-update-format-specifications)
15092
15093     ;; See whether we need to read the description file.
15094     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15095              (not gnus-description-hashtb)
15096              (not dont-connect)
15097              gnus-read-active-file)
15098         (gnus-read-all-descriptions-files))
15099
15100     ;; Find new newsgroups and treat them.
15101     (if (and init gnus-check-new-newsgroups (not level)
15102              (gnus-check-server gnus-select-method))
15103         (gnus-find-new-newsgroups))
15104
15105     ;; We might read in new NoCeM messages here.
15106     (when gnus-use-nocem 
15107       (gnus-nocem-scan-groups))
15108
15109     ;; Find the number of unread articles in each non-dead group.
15110     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15111       (gnus-get-unread-articles level))
15112
15113     (if (and init gnus-check-bogus-newsgroups
15114              gnus-read-active-file (not level)
15115              (gnus-server-opened gnus-select-method))
15116         (gnus-check-bogus-newsgroups))))
15117
15118 (defun gnus-find-new-newsgroups (&optional arg)
15119   "Search for new newsgroups and add them.
15120 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15121 The `-n' option line from .newsrc is respected.
15122 If ARG (the prefix), use the `ask-server' method to query
15123 the server for new groups."
15124   (interactive "P")
15125   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15126                        (null gnus-read-active-file)
15127                        (eq gnus-read-active-file 'some))
15128                    'ask-server gnus-check-new-newsgroups)))
15129     (unless (gnus-check-first-time-used)
15130       (if (or (consp check)
15131               (eq check 'ask-server))
15132           ;; Ask the server for new groups.
15133           (gnus-ask-server-for-new-groups)
15134         ;; Go through the active hashtb and look for new groups.
15135         (let ((groups 0)
15136               group new-newsgroups)
15137           (gnus-message 5 "Looking for new newsgroups...")
15138           (unless gnus-have-read-active-file
15139             (gnus-read-active-file))
15140           (setq gnus-newsrc-last-checked-date (current-time-string))
15141           (unless gnus-killed-hashtb
15142             (gnus-make-hashtable-from-killed))
15143           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15144           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15145           (mapatoms
15146            (lambda (sym)
15147              (if (or (null (setq group (symbol-name sym)))
15148                      (not (boundp sym))
15149                      (null (symbol-value sym))
15150                      (gnus-gethash group gnus-killed-hashtb)
15151                      (gnus-gethash group gnus-newsrc-hashtb))
15152                  ()
15153                (let ((do-sub (gnus-matches-options-n group)))
15154                  (cond
15155                   ((eq do-sub 'subscribe)
15156                    (setq groups (1+ groups))
15157                    (gnus-sethash group group gnus-killed-hashtb)
15158                    (funcall gnus-subscribe-options-newsgroup-method group))
15159                   ((eq do-sub 'ignore)
15160                    nil)
15161                   (t
15162                    (setq groups (1+ groups))
15163                    (gnus-sethash group group gnus-killed-hashtb)
15164                    (if gnus-subscribe-hierarchical-interactive
15165                        (setq new-newsgroups (cons group new-newsgroups))
15166                      (funcall gnus-subscribe-newsgroup-method group)))))))
15167            gnus-active-hashtb)
15168           (when new-newsgroups
15169             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15170           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15171           (if (> groups 0)
15172               (gnus-message 6 "%d new newsgroup%s arrived."
15173                             groups (if (> groups 1) "s have" " has"))
15174             (gnus-message 6 "No new newsgroups.")))))))
15175
15176 (defun gnus-matches-options-n (group)
15177   ;; Returns `subscribe' if the group is to be unconditionally
15178   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15179   ;; no match for the group.
15180
15181   ;; First we check the two user variables.
15182   (cond
15183    ((and gnus-options-subscribe
15184          (string-match gnus-options-subscribe group))
15185     'subscribe)
15186    ((and gnus-auto-subscribed-groups
15187          (string-match gnus-auto-subscribed-groups group))
15188     'subscribe)
15189    ((and gnus-options-not-subscribe
15190          (string-match gnus-options-not-subscribe group))
15191     'ignore)
15192    ;; Then we go through the list that was retrieved from the .newsrc
15193    ;; file.  This list has elements on the form
15194    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15195    ;; is in the reverse order of the options line) is returned.
15196    (t
15197     (let ((regs gnus-newsrc-options-n))
15198       (while (and regs
15199                   (not (string-match (caar regs) group)))
15200         (setq regs (cdr regs)))
15201       (and regs (cdar regs))))))
15202
15203 (defun gnus-ask-server-for-new-groups ()
15204   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15205          (methods (cons gnus-select-method
15206                         (nconc
15207                          (when gnus-message-archive-method
15208                            (list "archive"))
15209                          (append
15210                           (and (consp gnus-check-new-newsgroups)
15211                                gnus-check-new-newsgroups)
15212                           gnus-secondary-select-methods))))
15213          (groups 0)
15214          (new-date (current-time-string))
15215          group new-newsgroups got-new method hashtb
15216          gnus-override-subscribe-method)
15217     ;; Go through both primary and secondary select methods and
15218     ;; request new newsgroups.
15219     (while (setq method (gnus-server-get-method nil (pop methods)))
15220       (setq new-newsgroups nil)
15221       (setq gnus-override-subscribe-method method)
15222       (when (and (gnus-check-server method)
15223                  (gnus-request-newgroups date method))
15224         (save-excursion
15225           (setq got-new t)
15226           (setq hashtb (gnus-make-hashtable 100))
15227           (set-buffer nntp-server-buffer)
15228           ;; Enter all the new groups into a hashtable.
15229           (gnus-active-to-gnus-format method hashtb 'ignore))
15230         ;; Now all new groups from `method' are in `hashtb'.
15231         (mapatoms
15232          (lambda (group-sym)
15233            (if (or (null (setq group (symbol-name group-sym)))
15234                    (not (boundp group-sym))
15235                    (null (symbol-value group-sym))
15236                    (gnus-gethash group gnus-newsrc-hashtb)
15237                    (member group gnus-zombie-list)
15238                    (member group gnus-killed-list))
15239                ;; The group is already known.
15240                ()
15241              ;; Make this group active.
15242              (when (symbol-value group-sym)
15243                (gnus-set-active group (symbol-value group-sym)))
15244              ;; Check whether we want it or not.
15245              (let ((do-sub (gnus-matches-options-n group)))
15246                (cond
15247                 ((eq do-sub 'subscribe)
15248                  (incf groups)
15249                  (gnus-sethash group group gnus-killed-hashtb)
15250                  (funcall gnus-subscribe-options-newsgroup-method group))
15251                 ((eq do-sub 'ignore)
15252                  nil)
15253                 (t
15254                  (incf groups)
15255                  (gnus-sethash group group gnus-killed-hashtb)
15256                  (if gnus-subscribe-hierarchical-interactive
15257                      (push group new-newsgroups)
15258                    (funcall gnus-subscribe-newsgroup-method group)))))))
15259          hashtb))
15260       (when new-newsgroups
15261         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15262     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15263     (when (> groups 0)
15264       (gnus-message 6 "%d new newsgroup%s arrived."
15265                     groups (if (> groups 1) "s have" " has")))
15266     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15267     got-new))
15268
15269 (defun gnus-check-first-time-used ()
15270   (if (or (> (length gnus-newsrc-alist) 1)
15271           (file-exists-p gnus-startup-file)
15272           (file-exists-p (concat gnus-startup-file ".el"))
15273           (file-exists-p (concat gnus-startup-file ".eld")))
15274       nil
15275     (gnus-message 6 "First time user; subscribing you to default groups")
15276     (unless gnus-have-read-active-file
15277       (gnus-read-active-file))
15278     (setq gnus-newsrc-last-checked-date (current-time-string))
15279     (let ((groups gnus-default-subscribed-newsgroups)
15280           group)
15281       (if (eq groups t)
15282           nil
15283         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15284         (mapatoms
15285          (lambda (sym)
15286            (if (null (setq group (symbol-name sym)))
15287                ()
15288              (let ((do-sub (gnus-matches-options-n group)))
15289                (cond
15290                 ((eq do-sub 'subscribe)
15291                  (gnus-sethash group group gnus-killed-hashtb)
15292                  (funcall gnus-subscribe-options-newsgroup-method group))
15293                 ((eq do-sub 'ignore)
15294                  nil)
15295                 (t
15296                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15297          gnus-active-hashtb)
15298         (while groups
15299           (if (gnus-active (car groups))
15300               (gnus-group-change-level
15301                (car groups) gnus-level-default-subscribed gnus-level-killed))
15302           (setq groups (cdr groups)))
15303         (gnus-group-make-help-group)
15304         (and gnus-novice-user
15305              (gnus-message 7 "`A k' to list killed groups"))))))
15306
15307 (defun gnus-subscribe-group (group previous &optional method)
15308   (gnus-group-change-level
15309    (if method
15310        (list t group gnus-level-default-subscribed nil nil method)
15311      group)
15312    gnus-level-default-subscribed gnus-level-killed previous t))
15313
15314 ;; `gnus-group-change-level' is the fundamental function for changing
15315 ;; subscription levels of newsgroups.  This might mean just changing
15316 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15317 ;; again, which subscribes/unsubscribes a group, which is equally
15318 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15319 ;; from 8-9 to 1-7 means that you remove the group from the list of
15320 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15321 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15322 ;; which is trivial.
15323 ;; ENTRY can either be a string (newsgroup name) or a list (if
15324 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15325 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15326 ;; entries.
15327 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15328 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15329 ;; after.
15330 (defun gnus-group-change-level (entry level &optional oldlevel
15331                                       previous fromkilled)
15332   (let (group info active num)
15333     ;; Glean what info we can from the arguments
15334     (if (consp entry)
15335         (if fromkilled (setq group (nth 1 entry))
15336           (setq group (car (nth 2 entry))))
15337       (setq group entry))
15338     (if (and (stringp entry)
15339              oldlevel
15340              (< oldlevel gnus-level-zombie))
15341         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15342     (if (and (not oldlevel)
15343              (consp entry))
15344         (setq oldlevel (gnus-info-level (nth 2 entry)))
15345       (setq oldlevel (or oldlevel 9)))
15346     (if (stringp previous)
15347         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15348
15349     (if (and (>= oldlevel gnus-level-zombie)
15350              (gnus-gethash group gnus-newsrc-hashtb))
15351         ;; We are trying to subscribe a group that is already
15352         ;; subscribed.
15353         ()                              ; Do nothing.
15354
15355       (or (gnus-ephemeral-group-p group)
15356           (gnus-dribble-enter
15357            (format "(gnus-group-change-level %S %S %S %S %S)"
15358                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15359
15360       ;; Then we remove the newgroup from any old structures, if needed.
15361       ;; If the group was killed, we remove it from the killed or zombie
15362       ;; list.  If not, and it is in fact going to be killed, we remove
15363       ;; it from the newsrc hash table and assoc.
15364       (cond
15365        ((>= oldlevel gnus-level-zombie)
15366         (if (= oldlevel gnus-level-zombie)
15367             (setq gnus-zombie-list (delete group gnus-zombie-list))
15368           (setq gnus-killed-list (delete group gnus-killed-list))))
15369        (t
15370         (if (and (>= level gnus-level-zombie)
15371                  entry)
15372             (progn
15373               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15374               (if (nth 3 entry)
15375                   (setcdr (gnus-gethash (car (nth 3 entry))
15376                                         gnus-newsrc-hashtb)
15377                           (cdr entry)))
15378               (setcdr (cdr entry) (cdddr entry))))))
15379
15380       ;; Finally we enter (if needed) the list where it is supposed to
15381       ;; go, and change the subscription level.  If it is to be killed,
15382       ;; we enter it into the killed or zombie list.
15383       (cond 
15384        ((>= level gnus-level-zombie)
15385         ;; Remove from the hash table.
15386         (gnus-sethash group nil gnus-newsrc-hashtb)
15387         ;; We do not enter foreign groups into the list of dead
15388         ;; groups.
15389         (unless (gnus-group-foreign-p group)
15390           (if (= level gnus-level-zombie)
15391               (setq gnus-zombie-list (cons group gnus-zombie-list))
15392             (setq gnus-killed-list (cons group gnus-killed-list)))))
15393        (t
15394         ;; If the list is to be entered into the newsrc assoc, and
15395         ;; it was killed, we have to create an entry in the newsrc
15396         ;; hashtb format and fix the pointers in the newsrc assoc.
15397         (if (< oldlevel gnus-level-zombie)
15398             ;; It was alive, and it is going to stay alive, so we
15399             ;; just change the level and don't change any pointers or
15400             ;; hash table entries.
15401             (setcar (cdaddr entry) level)
15402           (if (listp entry)
15403               (setq info (cdr entry)
15404                     num (car entry))
15405             (setq active (gnus-active group))
15406             (setq num
15407                   (if active (- (1+ (cdr active)) (car active)) t))
15408             ;; Check whether the group is foreign.  If so, the
15409             ;; foreign select method has to be entered into the
15410             ;; info.
15411             (let ((method (or gnus-override-subscribe-method
15412                               (gnus-group-method group))))
15413               (if (eq method gnus-select-method)
15414                   (setq info (list group level nil))
15415                 (setq info (list group level nil nil method)))))
15416           (unless previous
15417             (setq previous
15418                   (let ((p gnus-newsrc-alist))
15419                     (while (cddr p)
15420                       (setq p (cdr p)))
15421                     p)))
15422           (setq entry (cons info (cddr previous)))
15423           (if (cdr previous)
15424               (progn
15425                 (setcdr (cdr previous) entry)
15426                 (gnus-sethash group (cons num (cdr previous))
15427                               gnus-newsrc-hashtb))
15428             (setcdr previous entry)
15429             (gnus-sethash group (cons num previous)
15430                           gnus-newsrc-hashtb))
15431           (when (cdr entry)
15432             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15433       (when gnus-group-change-level-function
15434         (funcall gnus-group-change-level-function group level oldlevel)))))
15435
15436 (defun gnus-kill-newsgroup (newsgroup)
15437   "Obsolete function.  Kills a newsgroup."
15438   (gnus-group-change-level
15439    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15440
15441 (defun gnus-check-bogus-newsgroups (&optional confirm)
15442   "Remove bogus newsgroups.
15443 If CONFIRM is non-nil, the user has to confirm the deletion of every
15444 newsgroup."
15445   (let ((newsrc (cdr gnus-newsrc-alist))
15446         bogus group entry info)
15447     (gnus-message 5 "Checking bogus newsgroups...")
15448     (unless gnus-have-read-active-file
15449       (gnus-read-active-file))
15450     (when (member gnus-select-method gnus-have-read-active-file)
15451       ;; Find all bogus newsgroup that are subscribed.
15452       (while newsrc
15453         (setq info (pop newsrc)
15454               group (gnus-info-group info))
15455         (unless (or (gnus-active group) ; Active
15456                     (gnus-info-method info) ; Foreign
15457                     (and confirm
15458                          (not (gnus-y-or-n-p
15459                                (format "Remove bogus newsgroup: %s " group)))))
15460           ;; Found a bogus newsgroup.
15461           (push group bogus)))
15462       ;; Remove all bogus subscribed groups by first killing them, and
15463       ;; then removing them from the list of killed groups.
15464       (while bogus
15465         (when (setq entry (gnus-gethash (setq group (pop bogus))
15466                                         gnus-newsrc-hashtb))
15467           (gnus-group-change-level entry gnus-level-killed)
15468           (setq gnus-killed-list (delete group gnus-killed-list))))
15469       ;; Then we remove all bogus groups from the list of killed and
15470       ;; zombie groups.  They are are removed without confirmation.
15471       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15472             killed)
15473         (while dead-lists
15474           (setq killed (symbol-value (car dead-lists)))
15475           (while killed
15476             (unless (gnus-active (setq group (pop killed)))
15477               ;; The group is bogus.
15478               ;; !!!Slow as hell.
15479               (set (car dead-lists)
15480                    (delete group (symbol-value (car dead-lists))))))
15481           (setq dead-lists (cdr dead-lists))))
15482       (gnus-message 5 "Checking bogus newsgroups...done"))))
15483
15484 (defun gnus-check-duplicate-killed-groups ()
15485   "Remove duplicates from the list of killed groups."
15486   (interactive)
15487   (let ((killed gnus-killed-list))
15488     (while killed
15489       (gnus-message 9 "%d" (length killed))
15490       (setcdr killed (delete (car killed) (cdr killed)))
15491       (setq killed (cdr killed)))))
15492
15493 ;; We want to inline a function from gnus-cache, so we cheat here:
15494 (eval-when-compile
15495   (provide 'gnus)
15496   (require 'gnus-cache))
15497
15498 (defun gnus-get-unread-articles-in-group (info active &optional update)
15499   (when active
15500     ;; Allow the backend to update the info in the group.
15501     (when (and update 
15502                (gnus-request-update-info
15503                 info (gnus-find-method-for-group (gnus-info-group info))))
15504       (gnus-activate-group (gnus-info-group info) nil t))
15505     (let* ((range (gnus-info-read info))
15506            (num 0))
15507       ;; If a cache is present, we may have to alter the active info.
15508       (when (and gnus-use-cache info)
15509         (inline (gnus-cache-possibly-alter-active 
15510                  (gnus-info-group info) active)))
15511       ;; Modify the list of read articles according to what articles
15512       ;; are available; then tally the unread articles and add the
15513       ;; number to the group hash table entry.
15514       (cond
15515        ((zerop (cdr active))
15516         (setq num 0))
15517        ((not range)
15518         (setq num (- (1+ (cdr active)) (car active))))
15519        ((not (listp (cdr range)))
15520         ;; Fix a single (num . num) range according to the
15521         ;; active hash table.
15522         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15523         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15524         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15525         ;; Compute number of unread articles.
15526         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15527        (t
15528         ;; The read list is a list of ranges.  Fix them according to
15529         ;; the active hash table.
15530         ;; First peel off any elements that are below the lower
15531         ;; active limit.
15532         (while (and (cdr range)
15533                     (>= (car active)
15534                         (or (and (atom (cadr range)) (cadr range))
15535                             (caadr range))))
15536           (if (numberp (car range))
15537               (setcar range
15538                       (cons (car range)
15539                             (or (and (numberp (cadr range))
15540                                      (cadr range))
15541                                 (cdadr range))))
15542             (setcdr (car range)
15543                     (or (and (numberp (nth 1 range)) (nth 1 range))
15544                         (cdadr range))))
15545           (setcdr range (cddr range)))
15546         ;; Adjust the first element to be the same as the lower limit.
15547         (if (and (not (atom (car range)))
15548                  (< (cdar range) (car active)))
15549             (setcdr (car range) (1- (car active))))
15550         ;; Then we want to peel off any elements that are higher
15551         ;; than the upper active limit.
15552         (let ((srange range))
15553           ;; Go past all legal elements.
15554           (while (and (cdr srange)
15555                       (<= (or (and (atom (cadr srange))
15556                                    (cadr srange))
15557                               (caadr srange)) (cdr active)))
15558             (setq srange (cdr srange)))
15559           (if (cdr srange)
15560               ;; Nuke all remaining illegal elements.
15561               (setcdr srange nil))
15562
15563           ;; Adjust the final element.
15564           (if (and (not (atom (car srange)))
15565                    (> (cdar srange) (cdr active)))
15566               (setcdr (car srange) (cdr active))))
15567         ;; Compute the number of unread articles.
15568         (while range
15569           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15570                                       (cdar range)))
15571                               (or (and (atom (car range)) (car range))
15572                                   (caar range)))))
15573           (setq range (cdr range)))
15574         (setq num (max 0 (- (cdr active) num)))))
15575       ;; Set the number of unread articles.
15576       (when info
15577         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15578       num)))
15579
15580 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15581 ;; and compute how many unread articles there are in each group.
15582 (defun gnus-get-unread-articles (&optional level)
15583   (let* ((newsrc (cdr gnus-newsrc-alist))
15584          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15585          (foreign-level
15586           (min
15587            (cond ((and gnus-activate-foreign-newsgroups
15588                        (not (numberp gnus-activate-foreign-newsgroups)))
15589                   (1+ gnus-level-subscribed))
15590                  ((numberp gnus-activate-foreign-newsgroups)
15591                   gnus-activate-foreign-newsgroups)
15592                  (t 0))
15593            level))
15594          info group active method)
15595     (gnus-message 5 "Checking new news...")
15596
15597     (while newsrc
15598       (setq active (gnus-active (setq group (gnus-info-group
15599                                              (setq info (pop newsrc))))))
15600
15601       ;; Check newsgroups.  If the user doesn't want to check them, or
15602       ;; they can't be checked (for instance, if the news server can't
15603       ;; be reached) we just set the number of unread articles in this
15604       ;; newsgroup to t.  This means that Gnus thinks that there are
15605       ;; unread articles, but it has no idea how many.
15606       (if (and (setq method (gnus-info-method info))
15607                (not (gnus-server-equal
15608                      gnus-select-method
15609                      (setq method (gnus-server-get-method nil method))))
15610                (not (gnus-secondary-method-p method)))
15611           ;; These groups are foreign.  Check the level.
15612           (when (<= (gnus-info-level info) foreign-level)
15613             (setq active (gnus-activate-group group 'scan))
15614             (unless (inline (gnus-virtual-group-p group))
15615               (inline (gnus-close-group group))))
15616
15617         ;; These groups are native or secondary.
15618         (when (and (<= (gnus-info-level info) level)
15619                    (not gnus-read-active-file))
15620           (setq active (gnus-activate-group group 'scan))
15621           (inline (gnus-close-group group))))
15622
15623       (if active
15624           (inline (gnus-get-unread-articles-in-group 
15625                    info active
15626                    (and method
15627                         (fboundp (intern (concat (symbol-name (car method))
15628                                                  "-request-update-info"))))))
15629         ;; The group couldn't be reached, so we nix out the number of
15630         ;; unread articles and stuff.
15631         (gnus-set-active group nil)
15632         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15633
15634     (gnus-message 5 "Checking new news...done")))
15635
15636 ;; Create a hash table out of the newsrc alist.  The `car's of the
15637 ;; alist elements are used as keys.
15638 (defun gnus-make-hashtable-from-newsrc-alist ()
15639   (let ((alist gnus-newsrc-alist)
15640         (ohashtb gnus-newsrc-hashtb)
15641         prev)
15642     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15643     (setq alist
15644           (setq prev (setq gnus-newsrc-alist
15645                            (if (equal (caar gnus-newsrc-alist)
15646                                       "dummy.group")
15647                                gnus-newsrc-alist
15648                              (cons (list "dummy.group" 0 nil) alist)))))
15649     (while alist
15650       (gnus-sethash
15651        (caar alist)
15652        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15653              prev)
15654        gnus-newsrc-hashtb)
15655       (setq prev alist
15656             alist (cdr alist)))))
15657
15658 (defun gnus-make-hashtable-from-killed ()
15659   "Create a hash table from the killed and zombie lists."
15660   (let ((lists '(gnus-killed-list gnus-zombie-list))
15661         list)
15662     (setq gnus-killed-hashtb
15663           (gnus-make-hashtable
15664            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15665     (while (setq list (pop lists))
15666       (setq list (symbol-value list))
15667       (while list
15668         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15669
15670 (defun gnus-activate-group (group &optional scan dont-check)
15671   ;; Check whether a group has been activated or not.
15672   ;; If SCAN, request a scan of that group as well.
15673   (let ((method (gnus-find-method-for-group group))
15674         active)
15675     (and (gnus-check-server method)
15676          ;; We escape all bugs and quit here to make it possible to
15677          ;; continue if a group is so out-there that it reports bugs
15678          ;; and stuff.
15679          (progn
15680            (and scan
15681                 (gnus-check-backend-function 'request-scan (car method))
15682                 (gnus-request-scan group method))
15683            t)
15684          (condition-case ()
15685              (gnus-request-group group dont-check)
15686         ;   (error nil)
15687            (quit nil))
15688          (save-excursion
15689            (set-buffer nntp-server-buffer)
15690            (goto-char (point-min))
15691            ;; Parse the result we got from `gnus-request-group'.
15692            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15693                 (progn
15694                   (goto-char (match-beginning 1))
15695                   (gnus-set-active
15696                    group (setq active (cons (read (current-buffer))
15697                                             (read (current-buffer)))))
15698                   ;; Return the new active info.
15699                   active))))))
15700
15701 (defun gnus-update-read-articles (group unread)
15702   "Update the list of read and ticked articles in GROUP using the
15703 UNREAD and TICKED lists.
15704 Note: UNSELECTED has to be sorted over `<'.
15705 Returns whether the updating was successful."
15706   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15707          (entry (gnus-gethash group gnus-newsrc-hashtb))
15708          (info (nth 2 entry))
15709          (prev 1)
15710          (unread (sort (copy-sequence unread) '<))
15711          read)
15712     (if (or (not info) (not active))
15713         ;; There is no info on this group if it was, in fact,
15714         ;; killed.  Gnus stores no information on killed groups, so
15715         ;; there's nothing to be done.
15716         ;; One could store the information somewhere temporarily,
15717         ;; perhaps...  Hmmm...
15718         ()
15719       ;; Remove any negative articles numbers.
15720       (while (and unread (< (car unread) 0))
15721         (setq unread (cdr unread)))
15722       ;; Remove any expired article numbers
15723       (while (and unread (< (car unread) (car active)))
15724         (setq unread (cdr unread)))
15725       ;; Compute the ranges of read articles by looking at the list of
15726       ;; unread articles.
15727       (while unread
15728         (if (/= (car unread) prev)
15729             (setq read (cons (if (= prev (1- (car unread))) prev
15730                                (cons prev (1- (car unread)))) read)))
15731         (setq prev (1+ (car unread)))
15732         (setq unread (cdr unread)))
15733       (when (<= prev (cdr active))
15734         (setq read (cons (cons prev (cdr active)) read)))
15735       ;; Enter this list into the group info.
15736       (gnus-info-set-read
15737        info (if (> (length read) 1) (nreverse read) read))
15738       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15739       (gnus-get-unread-articles-in-group info (gnus-active group))
15740       t)))
15741
15742 (defun gnus-make-articles-unread (group articles)
15743   "Mark ARTICLES in GROUP as unread."
15744   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15745                           (gnus-gethash (gnus-group-real-name group)
15746                                         gnus-newsrc-hashtb))))
15747          (ranges (gnus-info-read info))
15748          news article)
15749     (while articles
15750       (when (gnus-member-of-range
15751              (setq article (pop articles)) ranges)
15752         (setq news (cons article news))))
15753     (when news
15754       (gnus-info-set-read
15755        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15756       (gnus-group-update-group group t))))
15757
15758 ;; Enter all dead groups into the hashtb.
15759 (defun gnus-update-active-hashtb-from-killed ()
15760   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15761         (lists (list gnus-killed-list gnus-zombie-list))
15762         killed)
15763     (while lists
15764       (setq killed (car lists))
15765       (while killed
15766         (gnus-sethash (car killed) nil hashtb)
15767         (setq killed (cdr killed)))
15768       (setq lists (cdr lists)))))
15769
15770 ;; Get the active file(s) from the backend(s).
15771 (defun gnus-read-active-file ()
15772   (gnus-group-set-mode-line)
15773   (let ((methods 
15774          (append
15775           (if (gnus-check-server gnus-select-method)
15776               ;; The native server is available.
15777               (cons gnus-select-method gnus-secondary-select-methods)
15778             ;; The native server is down, so we just do the
15779             ;; secondary ones.
15780             gnus-secondary-select-methods)
15781           ;; Also read from the archive server.
15782           (when gnus-message-archive-method
15783             (list "archive"))))
15784         list-type)
15785     (setq gnus-have-read-active-file nil)
15786     (save-excursion
15787       (set-buffer nntp-server-buffer)
15788       (while methods
15789         (let* ((method (if (stringp (car methods))
15790                            (gnus-server-get-method nil (car methods))
15791                          (car methods)))
15792                (where (nth 1 method))
15793                (mesg (format "Reading active file%s via %s..."
15794                              (if (and where (not (zerop (length where))))
15795                                  (concat " from " where) "")
15796                              (car method))))
15797           (gnus-message 5 mesg)
15798           (when (gnus-check-server method)
15799             ;; Request that the backend scan its incoming messages.
15800             (and (gnus-check-backend-function 'request-scan (car method))
15801                  (gnus-request-scan nil method))
15802             (cond
15803              ((and (eq gnus-read-active-file 'some)
15804                    (gnus-check-backend-function 'retrieve-groups (car method)))
15805               (let ((newsrc (cdr gnus-newsrc-alist))
15806                     (gmethod (gnus-server-get-method nil method))
15807                     groups info)
15808                 (while (setq info (pop newsrc))
15809                   (when (gnus-server-equal
15810                          (gnus-find-method-for-group 
15811                           (gnus-info-group info) info)
15812                          gmethod)
15813                     (push (gnus-group-real-name (gnus-info-group info)) 
15814                           groups)))
15815                 (when groups
15816                   (gnus-check-server method)
15817                   (setq list-type (gnus-retrieve-groups groups method))
15818                   (cond
15819                    ((not list-type)
15820                     (gnus-error
15821                      1.2 "Cannot read partial active file from %s server."
15822                      (car method)))
15823                    ((eq list-type 'active)
15824                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15825                    (t
15826                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15827              (t
15828               (if (not (gnus-request-list method))
15829                   (unless (equal method gnus-message-archive-method)
15830                     (gnus-error 1 "Cannot read active file from %s server."
15831                                 (car method)))
15832                 (gnus-active-to-gnus-format method)
15833                 ;; We mark this active file as read.
15834                 (push method gnus-have-read-active-file)
15835                 (gnus-message 5 "%sdone" mesg))))))
15836         (setq methods (cdr methods))))))
15837
15838 ;; Read an active file and place the results in `gnus-active-hashtb'.
15839 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15840   (unless method
15841     (setq method gnus-select-method))
15842   (let ((cur (current-buffer))
15843         (hashtb (or hashtb
15844                     (if (and gnus-active-hashtb
15845                              (not (equal method gnus-select-method)))
15846                         gnus-active-hashtb
15847                       (setq gnus-active-hashtb
15848                             (if (equal method gnus-select-method)
15849                                 (gnus-make-hashtable
15850                                  (count-lines (point-min) (point-max)))
15851                               (gnus-make-hashtable 4096)))))))
15852     ;; Delete unnecessary lines.
15853     (goto-char (point-min))
15854     (while (search-forward "\nto." nil t)
15855       (delete-region (1+ (match-beginning 0))
15856                      (progn (forward-line 1) (point))))
15857     (or (string= gnus-ignored-newsgroups "")
15858         (progn
15859           (goto-char (point-min))
15860           (delete-matching-lines gnus-ignored-newsgroups)))
15861     ;; Make the group names readable as a lisp expression even if they
15862     ;; contain special characters.
15863     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15864     (goto-char (point-max))
15865     (while (re-search-backward "[][';?()#]" nil t)
15866       (insert ?\\))
15867     ;; If these are groups from a foreign select method, we insert the
15868     ;; group prefix in front of the group names.
15869     (and method (not (gnus-server-equal
15870                       (gnus-server-get-method nil method)
15871                       (gnus-server-get-method nil gnus-select-method)))
15872          (let ((prefix (gnus-group-prefixed-name "" method)))
15873            (goto-char (point-min))
15874            (while (and (not (eobp))
15875                        (progn (insert prefix)
15876                               (zerop (forward-line 1)))))))
15877     ;; Store the active file in a hash table.
15878     (goto-char (point-min))
15879     (if (string-match "%[oO]" gnus-group-line-format)
15880         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15881         ;; If we want information on moderated groups, we use this
15882         ;; loop...
15883         (let* ((mod-hashtb (make-vector 7 0))
15884                (m (intern "m" mod-hashtb))
15885                group max min)
15886           (while (not (eobp))
15887             (condition-case nil
15888                 (progn
15889                   (narrow-to-region (point) (gnus-point-at-eol))
15890                   (setq group (let ((obarray hashtb)) (read cur)))
15891                   (if (and (numberp (setq max (read cur)))
15892                            (numberp (setq min (read cur)))
15893                            (progn
15894                              (skip-chars-forward " \t")
15895                              (not
15896                               (or (= (following-char) ?=)
15897                                   (= (following-char) ?x)
15898                                   (= (following-char) ?j)))))
15899                       (set group (cons min max))
15900                     (set group nil))
15901                   ;; Enter moderated groups into a list.
15902                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15903                       (setq gnus-moderated-list
15904                             (cons (symbol-name group) gnus-moderated-list))))
15905               (error
15906                (and group
15907                     (symbolp group)
15908                     (set group nil))))
15909             (widen)
15910             (forward-line 1)))
15911       ;; And if we do not care about moderation, we use this loop,
15912       ;; which is faster.
15913       (let (group max min)
15914         (while (not (eobp))
15915           (condition-case ()
15916               (progn
15917                 (narrow-to-region (point) (gnus-point-at-eol))
15918                 ;; group gets set to a symbol interned in the hash table
15919                 ;; (what a hack!!) - jwz
15920                 (setq group (let ((obarray hashtb)) (read cur)))
15921                 (if (and (numberp (setq max (read cur)))
15922                          (numberp (setq min (read cur)))
15923                          (progn
15924                            (skip-chars-forward " \t")
15925                            (not
15926                             (or (= (following-char) ?=)
15927                                 (= (following-char) ?x)
15928                                 (= (following-char) ?j)))))
15929                     (set group (cons min max))
15930                   (set group nil)))
15931             (error
15932              (progn
15933                (and group
15934                     (symbolp group)
15935                     (set group nil))
15936                (or ignore-errors
15937                    (gnus-message 3 "Warning - illegal active: %s"
15938                                  (buffer-substring
15939                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15940           (widen)
15941           (forward-line 1))))))
15942
15943 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15944   ;; Parse a "groups" active file.
15945   (let ((cur (current-buffer))
15946         (hashtb (or hashtb
15947                     (if (and method gnus-active-hashtb)
15948                         gnus-active-hashtb
15949                       (setq gnus-active-hashtb
15950                             (gnus-make-hashtable
15951                              (count-lines (point-min) (point-max)))))))
15952         (prefix (and method
15953                      (not (gnus-server-equal
15954                            (gnus-server-get-method nil method)
15955                            (gnus-server-get-method nil gnus-select-method)))
15956                      (gnus-group-prefixed-name "" method))))
15957
15958     (goto-char (point-min))
15959     ;; We split this into to separate loops, one with the prefix
15960     ;; and one without to speed the reading up somewhat.
15961     (if prefix
15962         (let (min max opoint group)
15963           (while (not (eobp))
15964             (condition-case ()
15965                 (progn
15966                   (read cur) (read cur)
15967                   (setq min (read cur)
15968                         max (read cur)
15969                         opoint (point))
15970                   (skip-chars-forward " \t")
15971                   (insert prefix)
15972                   (goto-char opoint)
15973                   (set (let ((obarray hashtb)) (read cur))
15974                        (cons min max)))
15975               (error (and group (symbolp group) (set group nil))))
15976             (forward-line 1)))
15977       (let (min max group)
15978         (while (not (eobp))
15979           (condition-case ()
15980               (if (= (following-char) ?2)
15981                   (progn
15982                     (read cur) (read cur)
15983                     (setq min (read cur)
15984                           max (read cur))
15985                     (set (setq group (let ((obarray hashtb)) (read cur)))
15986                          (cons min max))))
15987             (error (and group (symbolp group) (set group nil))))
15988           (forward-line 1))))))
15989
15990 (defun gnus-read-newsrc-file (&optional force)
15991   "Read startup file.
15992 If FORCE is non-nil, the .newsrc file is read."
15993   ;; Reset variables that might be defined in the .newsrc.eld file.
15994   (let ((variables gnus-variable-list))
15995     (while variables
15996       (set (car variables) nil)
15997       (setq variables (cdr variables))))
15998   (let* ((newsrc-file gnus-current-startup-file)
15999          (quick-file (concat newsrc-file ".el")))
16000     (save-excursion
16001       ;; We always load the .newsrc.eld file.  If always contains
16002       ;; much information that can not be gotten from the .newsrc
16003       ;; file (ticked articles, killed groups, foreign methods, etc.)
16004       (gnus-read-newsrc-el-file quick-file)
16005
16006       (if (and (file-exists-p gnus-current-startup-file)
16007                (or force
16008                    (and (file-newer-than-file-p newsrc-file quick-file)
16009                         (file-newer-than-file-p newsrc-file
16010                                                 (concat quick-file "d")))
16011                    (not gnus-newsrc-alist)))
16012           ;; We read the .newsrc file.  Note that if there if a
16013           ;; .newsrc.eld file exists, it has already been read, and
16014           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16015           ;; the .newsrc file, Gnus will only use the information it
16016           ;; can find there for changing the data already read -
16017           ;; ie. reading the .newsrc file will not trash the data
16018           ;; already read (except for read articles).
16019           (save-excursion
16020             (gnus-message 5 "Reading %s..." newsrc-file)
16021             (set-buffer (find-file-noselect newsrc-file))
16022             (buffer-disable-undo (current-buffer))
16023             (gnus-newsrc-to-gnus-format)
16024             (kill-buffer (current-buffer))
16025             (gnus-message 5 "Reading %s...done" newsrc-file)))
16026
16027       ;; Read any slave files.
16028       (or gnus-slave
16029           (gnus-master-read-slave-newsrc)))))
16030
16031 (defun gnus-read-newsrc-el-file (file)
16032   (let ((ding-file (concat file "d")))
16033     ;; We always, always read the .eld file.
16034     (gnus-message 5 "Reading %s..." ding-file)
16035     (let (gnus-newsrc-assoc)
16036       (condition-case nil
16037           (load ding-file t t t)
16038         (error
16039          (gnus-error 1 "Error in %s" ding-file)))
16040       (when gnus-newsrc-assoc
16041         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16042     (gnus-make-hashtable-from-newsrc-alist)
16043     (when (file-newer-than-file-p file ding-file)
16044       ;; Old format quick file
16045       (gnus-message 5 "Reading %s..." file)
16046       ;; The .el file is newer than the .eld file, so we read that one
16047       ;; as well.
16048       (gnus-read-old-newsrc-el-file file))))
16049
16050 ;; Parse the old-style quick startup file
16051 (defun gnus-read-old-newsrc-el-file (file)
16052   (let (newsrc killed marked group m info)
16053     (prog1
16054         (let ((gnus-killed-assoc nil)
16055               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16056           (prog1
16057               (condition-case nil
16058                   (load file t t t)
16059                 (error nil))
16060             (setq newsrc gnus-newsrc-assoc
16061                   killed gnus-killed-assoc
16062                   marked gnus-marked-assoc)))
16063       (setq gnus-newsrc-alist nil)
16064       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16065         (if info
16066             (progn
16067               (gnus-info-set-read info (cddr group))
16068               (gnus-info-set-level
16069                info (if (nth 1 group) gnus-level-default-subscribed
16070                       gnus-level-default-unsubscribed))
16071               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16072           (push (setq info
16073                       (list (car group)
16074                             (if (nth 1 group) gnus-level-default-subscribed
16075                               gnus-level-default-unsubscribed)
16076                             (cddr group)))
16077                 gnus-newsrc-alist))
16078         ;; Copy marks into info.
16079         (when (setq m (assoc (car group) marked))
16080           (unless (nthcdr 3 info)
16081             (nconc info (list nil)))
16082           (gnus-info-set-marks
16083            info (list (cons 'tick (gnus-compress-sequence 
16084                                    (sort (cdr m) '<) t))))))
16085       (setq newsrc killed)
16086       (while newsrc
16087         (setcar newsrc (caar newsrc))
16088         (setq newsrc (cdr newsrc)))
16089       (setq gnus-killed-list killed))
16090     ;; The .el file version of this variable does not begin with
16091     ;; "options", while the .eld version does, so we just add it if it
16092     ;; isn't there.
16093     (and
16094      gnus-newsrc-options
16095      (progn
16096        (and (not (string-match "^ *options" gnus-newsrc-options))
16097             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16098        (and (not (string-match "\n$" gnus-newsrc-options))
16099             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16100        ;; Finally, if we read some options lines, we parse them.
16101        (or (string= gnus-newsrc-options "")
16102            (gnus-newsrc-parse-options gnus-newsrc-options))))
16103
16104     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16105     (gnus-make-hashtable-from-newsrc-alist)))
16106
16107 (defun gnus-make-newsrc-file (file)
16108   "Make server dependent file name by catenating FILE and server host name."
16109   (let* ((file (expand-file-name file nil))
16110          (real-file (concat file "-" (nth 1 gnus-select-method))))
16111     (if (or (file-exists-p real-file)
16112             (file-exists-p (concat real-file ".el"))
16113             (file-exists-p (concat real-file ".eld")))
16114         real-file file)))
16115
16116 (defun gnus-newsrc-to-gnus-format ()
16117   (setq gnus-newsrc-options "")
16118   (setq gnus-newsrc-options-n nil)
16119
16120   (or gnus-active-hashtb
16121       (setq gnus-active-hashtb (make-vector 4095 0)))
16122   (let ((buf (current-buffer))
16123         (already-read (> (length gnus-newsrc-alist) 1))
16124         group subscribed options-symbol newsrc Options-symbol
16125         symbol reads num1)
16126     (goto-char (point-min))
16127     ;; We intern the symbol `options' in the active hashtb so that we
16128     ;; can `eq' against it later.
16129     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16130     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16131
16132     (while (not (eobp))
16133       ;; We first read the first word on the line by narrowing and
16134       ;; then reading into `gnus-active-hashtb'.  Most groups will
16135       ;; already exist in that hashtb, so this will save some string
16136       ;; space.
16137       (narrow-to-region
16138        (point)
16139        (progn (skip-chars-forward "^ \t!:\n") (point)))
16140       (goto-char (point-min))
16141       (setq symbol
16142             (and (/= (point-min) (point-max))
16143                  (let ((obarray gnus-active-hashtb)) (read buf))))
16144       (widen)
16145       ;; Now, the symbol we have read is either `options' or a group
16146       ;; name.  If it is an options line, we just add it to a string.
16147       (cond
16148        ((or (eq symbol options-symbol)
16149             (eq symbol Options-symbol))
16150         (setq gnus-newsrc-options
16151               ;; This concating is quite inefficient, but since our
16152               ;; thorough studies show that approx 99.37% of all
16153               ;; .newsrc files only contain a single options line, we
16154               ;; don't give a damn, frankly, my dear.
16155               (concat gnus-newsrc-options
16156                       (buffer-substring
16157                        (gnus-point-at-bol)
16158                        ;; Options may continue on the next line.
16159                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16160                                 (progn (beginning-of-line) (point)))
16161                            (point)))))
16162         (forward-line -1))
16163        (symbol
16164         ;; Group names can be just numbers.  
16165         (when (numberp symbol) 
16166           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16167         (or (boundp symbol) (set symbol nil))
16168         ;; It was a group name.
16169         (setq subscribed (= (following-char) ?:)
16170               group (symbol-name symbol)
16171               reads nil)
16172         (if (eolp)
16173             ;; If the line ends here, this is clearly a buggy line, so
16174             ;; we put point a the beginning of line and let the cond
16175             ;; below do the error handling.
16176             (beginning-of-line)
16177           ;; We skip to the beginning of the ranges.
16178           (skip-chars-forward "!: \t"))
16179         ;; We are now at the beginning of the list of read articles.
16180         ;; We read them range by range.
16181         (while
16182             (cond
16183              ((looking-at "[0-9]+")
16184               ;; We narrow and read a number instead of buffer-substring/
16185               ;; string-to-int because it's faster.  narrow/widen is
16186               ;; faster than save-restriction/narrow, and save-restriction
16187               ;; produces a garbage object.
16188               (setq num1 (progn
16189                            (narrow-to-region (match-beginning 0) (match-end 0))
16190                            (read buf)))
16191               (widen)
16192               ;; If the next character is a dash, then this is a range.
16193               (if (= (following-char) ?-)
16194                   (progn
16195                     ;; We read the upper bound of the range.
16196                     (forward-char 1)
16197                     (if (not (looking-at "[0-9]+"))
16198                         ;; This is a buggy line, by we pretend that
16199                         ;; it's kinda OK.  Perhaps the user should be
16200                         ;; dinged?
16201                         (setq reads (cons num1 reads))
16202                       (setq reads
16203                             (cons
16204                              (cons num1
16205                                    (progn
16206                                      (narrow-to-region (match-beginning 0)
16207                                                        (match-end 0))
16208                                      (read buf)))
16209                              reads))
16210                       (widen)))
16211                 ;; It was just a simple number, so we add it to the
16212                 ;; list of ranges.
16213                 (setq reads (cons num1 reads)))
16214               ;; If the next char in ?\n, then we have reached the end
16215               ;; of the line and return nil.
16216               (/= (following-char) ?\n))
16217              ((= (following-char) ?\n)
16218               ;; End of line, so we end.
16219               nil)
16220              (t
16221               ;; Not numbers and not eol, so this might be a buggy
16222               ;; line...
16223               (or (eobp)
16224                   ;; If it was eob instead of ?\n, we allow it.
16225                   (progn
16226                     ;; The line was buggy.
16227                     (setq group nil)
16228                     (gnus-error 3.1 "Mangled line: %s"
16229                                 (buffer-substring (gnus-point-at-bol)
16230                                                   (gnus-point-at-eol)))))
16231               nil))
16232           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16233           ;; we allow them, because it's a common mistake to put a
16234           ;; space after the comma.
16235           (skip-chars-forward ", "))
16236
16237         ;; We have already read .newsrc.eld, so we gently update the
16238         ;; data in the hash table with the information we have just
16239         ;; read.
16240         (when group
16241           (let ((info (gnus-get-info group))
16242                 level)
16243             (if info
16244                 ;; There is an entry for this file in the alist.
16245                 (progn
16246                   (gnus-info-set-read info (nreverse reads))
16247                   ;; We update the level very gently.  In fact, we
16248                   ;; only change it if there's been a status change
16249                   ;; from subscribed to unsubscribed, or vice versa.
16250                   (setq level (gnus-info-level info))
16251                   (cond ((and (<= level gnus-level-subscribed)
16252                               (not subscribed))
16253                          (setq level (if reads
16254                                          gnus-level-default-unsubscribed
16255                                        (1+ gnus-level-default-unsubscribed))))
16256                         ((and (> level gnus-level-subscribed) subscribed)
16257                          (setq level gnus-level-default-subscribed)))
16258                   (gnus-info-set-level info level))
16259               ;; This is a new group.
16260               (setq info (list group
16261                                (if subscribed
16262                                    gnus-level-default-subscribed
16263                                  (if reads
16264                                      (1+ gnus-level-subscribed)
16265                                    gnus-level-default-unsubscribed))
16266                                (nreverse reads))))
16267             (setq newsrc (cons info newsrc))))))
16268       (forward-line 1))
16269
16270     (setq newsrc (nreverse newsrc))
16271
16272     (if (not already-read)
16273         ()
16274       ;; We now have two newsrc lists - `newsrc', which is what we
16275       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16276       ;; what we've read from .newsrc.eld.  We have to merge these
16277       ;; lists.  We do this by "attaching" any (foreign) groups in the
16278       ;; gnus-newsrc-alist to the (native) group that precedes them.
16279       (let ((rc (cdr gnus-newsrc-alist))
16280             (prev gnus-newsrc-alist)
16281             entry mentry)
16282         (while rc
16283           (or (null (nth 4 (car rc)))   ; It's a native group.
16284               (assoc (caar rc) newsrc) ; It's already in the alist.
16285               (if (setq entry (assoc (caar prev) newsrc))
16286                   (setcdr (setq mentry (memq entry newsrc))
16287                           (cons (car rc) (cdr mentry)))
16288                 (setq newsrc (cons (car rc) newsrc))))
16289           (setq prev rc
16290                 rc (cdr rc)))))
16291
16292     (setq gnus-newsrc-alist newsrc)
16293     ;; We make the newsrc hashtb.
16294     (gnus-make-hashtable-from-newsrc-alist)
16295
16296     ;; Finally, if we read some options lines, we parse them.
16297     (or (string= gnus-newsrc-options "")
16298         (gnus-newsrc-parse-options gnus-newsrc-options))))
16299
16300 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16301 ;; The return value will be a list on the form
16302 ;; ((regexp1 . ignore)
16303 ;;  (regexp2 . subscribe)...)
16304 ;; When handling new newsgroups, groups that match a `ignore' regexp
16305 ;; will be ignored, and groups that match a `subscribe' regexp will be
16306 ;; subscribed.  A line like
16307 ;; options -n !all rec.all
16308 ;; will lead to a list that looks like
16309 ;; (("^rec\\..+" . subscribe)
16310 ;;  ("^.+" . ignore))
16311 ;; So all "rec.*" groups will be subscribed, while all the other
16312 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16313 ;; different from "options -n rec.all !all".
16314 (defun gnus-newsrc-parse-options (options)
16315   (let (out eol)
16316     (save-excursion
16317       (gnus-set-work-buffer)
16318       (insert (regexp-quote options))
16319       ;; First we treat all continuation lines.
16320       (goto-char (point-min))
16321       (while (re-search-forward "\n[ \t]+" nil t)
16322         (replace-match " " t t))
16323       ;; Then we transform all "all"s into ".+"s.
16324       (goto-char (point-min))
16325       (while (re-search-forward "\\ball\\b" nil t)
16326         (replace-match ".+" t t))
16327       (goto-char (point-min))
16328       ;; We remove all other options than the "-n" ones.
16329       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16330         (replace-match " ")
16331         (forward-char -1))
16332       (goto-char (point-min))
16333
16334       ;; We are only interested in "options -n" lines - we
16335       ;; ignore the other option lines.
16336       (while (re-search-forward "[ \t]-n" nil t)
16337         (setq eol
16338               (or (save-excursion
16339                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16340                          (- (point) 2)))
16341                   (gnus-point-at-eol)))
16342         ;; Search for all "words"...
16343         (while (re-search-forward "[^ \t,\n]+" eol t)
16344           (if (= (char-after (match-beginning 0)) ?!)
16345               ;; If the word begins with a bang (!), this is a "not"
16346               ;; spec.  We put this spec (minus the bang) and the
16347               ;; symbol `ignore' into the list.
16348               (setq out (cons (cons (concat
16349                                      "^" (buffer-substring
16350                                           (1+ (match-beginning 0))
16351                                           (match-end 0)))
16352                                     'ignore) out))
16353             ;; There was no bang, so this is a "yes" spec.
16354             (setq out (cons (cons (concat "^" (match-string 0))
16355                                   'subscribe) out)))))
16356
16357       (setq gnus-newsrc-options-n out))))
16358
16359 (defun gnus-save-newsrc-file (&optional force)
16360   "Save .newsrc file."
16361   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16362   ;; from the variable gnus-newsrc-alist.
16363   (when (and (or gnus-newsrc-alist gnus-killed-list)
16364              gnus-current-startup-file)
16365     (save-excursion
16366       (if (and (or gnus-use-dribble-file gnus-slave)
16367                (not force)
16368                (or (not gnus-dribble-buffer)
16369                    (not (buffer-name gnus-dribble-buffer))
16370                    (zerop (save-excursion
16371                             (set-buffer gnus-dribble-buffer)
16372                             (buffer-size)))))
16373           (gnus-message 4 "(No changes need to be saved)")
16374         (run-hooks 'gnus-save-newsrc-hook)
16375         (if gnus-slave
16376             (gnus-slave-save-newsrc)
16377           ;; Save .newsrc.
16378           (when gnus-save-newsrc-file
16379             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16380             (gnus-gnus-to-newsrc-format)
16381             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16382           ;; Save .newsrc.eld.
16383           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16384           (make-local-variable 'version-control)
16385           (setq version-control 'never)
16386           (setq buffer-file-name
16387                 (concat gnus-current-startup-file ".eld"))
16388           (gnus-add-current-to-buffer-list)
16389           (buffer-disable-undo (current-buffer))
16390           (erase-buffer)
16391           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16392           (gnus-gnus-to-quick-newsrc-format)
16393           (run-hooks 'gnus-save-quick-newsrc-hook)
16394           (save-buffer)
16395           (kill-buffer (current-buffer))
16396           (gnus-message
16397            5 "Saving %s.eld...done" gnus-current-startup-file))
16398         (gnus-dribble-delete-file)))))
16399
16400 (defun gnus-gnus-to-quick-newsrc-format ()
16401   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16402   (insert ";; Gnus startup file.\n")
16403   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16404   (insert ";; to read .newsrc.\n")
16405   (insert "(setq gnus-newsrc-file-version "
16406           (prin1-to-string gnus-version) ")\n")
16407   (let ((variables
16408          (if gnus-save-killed-list gnus-variable-list
16409            ;; Remove the `gnus-killed-list' from the list of variables
16410            ;; to be saved, if required.
16411            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16412         ;; Peel off the "dummy" group.
16413         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16414         variable)
16415     ;; Insert the variables into the file.
16416     (while variables
16417       (when (and (boundp (setq variable (pop variables)))
16418                  (symbol-value variable))
16419         (insert "(setq " (symbol-name variable) " '")
16420         (prin1 (symbol-value variable) (current-buffer))
16421         (insert ")\n")))))
16422
16423 (defun gnus-gnus-to-newsrc-format ()
16424   ;; Generate and save the .newsrc file.
16425   (save-excursion
16426     (set-buffer (create-file-buffer gnus-current-startup-file))
16427     (let ((newsrc (cdr gnus-newsrc-alist))
16428           (standard-output (current-buffer))
16429           info ranges range method)
16430       (setq buffer-file-name gnus-current-startup-file)
16431       (buffer-disable-undo (current-buffer))
16432       (erase-buffer)
16433       ;; Write options.
16434       (if gnus-newsrc-options (insert gnus-newsrc-options))
16435       ;; Write subscribed and unsubscribed.
16436       (while (setq info (pop newsrc))
16437         ;; Don't write foreign groups to .newsrc.
16438         (when (or (null (setq method (gnus-info-method info)))
16439                   (equal method "native")
16440                   (gnus-server-equal method gnus-select-method))
16441           (insert (gnus-info-group info)
16442                   (if (> (gnus-info-level info) gnus-level-subscribed)
16443                       "!" ":"))
16444           (when (setq ranges (gnus-info-read info))
16445             (insert " ")
16446             (if (not (listp (cdr ranges)))
16447                 (if (= (car ranges) (cdr ranges))
16448                     (princ (car ranges))
16449                   (princ (car ranges))
16450                   (insert "-")
16451                   (princ (cdr ranges)))
16452               (while (setq range (pop ranges))
16453                 (if (or (atom range) (= (car range) (cdr range)))
16454                     (princ (or (and (atom range) range) (car range)))
16455                   (princ (car range))
16456                   (insert "-")
16457                   (princ (cdr range)))
16458                 (if ranges (insert ",")))))
16459           (insert "\n")))
16460       (make-local-variable 'version-control)
16461       (setq version-control 'never)
16462       ;; It has been reported that sometime the modtime on the .newsrc
16463       ;; file seems to be off.  We really do want to overwrite it, so
16464       ;; we clear the modtime here before saving.  It's a bit odd,
16465       ;; though...
16466       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16467       ;; delete the silly thing entirely first.  but this fails to provide
16468       ;; such niceties as .newsrc~ creation.
16469       (if gnus-modtime-botch
16470           (delete-file gnus-startup-file)
16471         (clear-visited-file-modtime))
16472       (run-hooks 'gnus-save-standard-newsrc-hook)
16473       (save-buffer)
16474       (kill-buffer (current-buffer)))))
16475
16476 \f
16477 ;;;
16478 ;;; Slave functions.
16479 ;;;
16480
16481 (defun gnus-slave-save-newsrc ()
16482   (save-excursion
16483     (set-buffer gnus-dribble-buffer)
16484     (let ((slave-name
16485            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16486       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16487
16488 (defun gnus-master-read-slave-newsrc ()
16489   (let ((slave-files
16490          (directory-files
16491           (file-name-directory gnus-current-startup-file)
16492           t (concat
16493              "^" (regexp-quote
16494                   (concat
16495                    (file-name-nondirectory gnus-current-startup-file)
16496                    "-slave-")))
16497           t))
16498         file)
16499     (if (not slave-files)
16500         ()                              ; There are no slave files to read.
16501       (gnus-message 7 "Reading slave newsrcs...")
16502       (save-excursion
16503         (set-buffer (get-buffer-create " *gnus slave*"))
16504         (buffer-disable-undo (current-buffer))
16505         (setq slave-files
16506               (sort (mapcar (lambda (file)
16507                               (list (nth 5 (file-attributes file)) file))
16508                             slave-files)
16509                     (lambda (f1 f2)
16510                       (or (< (caar f1) (caar f2))
16511                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16512         (while slave-files
16513           (erase-buffer)
16514           (setq file (nth 1 (car slave-files)))
16515           (insert-file-contents file)
16516           (if (condition-case ()
16517                   (progn
16518                     (eval-buffer (current-buffer))
16519                     t)
16520                 (error
16521                  (gnus-error 3.2 "Possible error in %s" file)
16522                  nil))
16523               (or gnus-slave ; Slaves shouldn't delete these files.
16524                   (condition-case ()
16525                       (delete-file file)
16526                     (error nil))))
16527           (setq slave-files (cdr slave-files))))
16528       (gnus-message 7 "Reading slave newsrcs...done"))))
16529
16530 \f
16531 ;;;
16532 ;;; Group description.
16533 ;;;
16534
16535 (defun gnus-read-all-descriptions-files ()
16536   (let ((methods (cons gnus-select-method 
16537                        (nconc
16538                         (when gnus-message-archive-method
16539                           (list "archive"))
16540                         gnus-secondary-select-methods))))
16541     (while methods
16542       (gnus-read-descriptions-file (car methods))
16543       (setq methods (cdr methods)))
16544     t))
16545
16546 (defun gnus-read-descriptions-file (&optional method)
16547   (let ((method (or method gnus-select-method)))
16548     (when (stringp method)
16549       (setq method (gnus-server-to-method method)))
16550     ;; We create the hashtable whether we manage to read the desc file
16551     ;; to avoid trying to re-read after a failed read.
16552     (or gnus-description-hashtb
16553         (setq gnus-description-hashtb
16554               (gnus-make-hashtable (length gnus-active-hashtb))))
16555     ;; Mark this method's desc file as read.
16556     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16557                   gnus-description-hashtb)
16558
16559     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16560     (cond
16561      ((not (gnus-check-server method))
16562       (gnus-message 1 "Couldn't open server")
16563       nil)
16564      ((not (gnus-request-list-newsgroups method))
16565       (gnus-message 1 "Couldn't read newsgroups descriptions")
16566       nil)
16567      (t
16568       (let (group)
16569         (save-excursion
16570           (save-restriction
16571             (set-buffer nntp-server-buffer)
16572             (goto-char (point-min))
16573             (if (or (search-forward "\n.\n" nil t)
16574                     (goto-char (point-max)))
16575                 (progn
16576                   (beginning-of-line)
16577                   (narrow-to-region (point-min) (point))))
16578             (goto-char (point-min))
16579             (while (not (eobp))
16580               ;; If we get an error, we set group to 0, which is not a
16581               ;; symbol...
16582               (setq group
16583                     (condition-case ()
16584                         (let ((obarray gnus-description-hashtb))
16585                           ;; Group is set to a symbol interned in this
16586                           ;; hash table.
16587                           (read nntp-server-buffer))
16588                       (error 0)))
16589               (skip-chars-forward " \t")
16590               ;; ...  which leads to this line being effectively ignored.
16591               (and (symbolp group)
16592                    (set group (buffer-substring
16593                                (point) (progn (end-of-line) (point)))))
16594               (forward-line 1))))
16595         (gnus-message 5 "Reading descriptions file...done")
16596         t)))))
16597
16598 (defun gnus-group-get-description (group)
16599   "Get the description of a group by sending XGTITLE to the server."
16600   (when (gnus-request-group-description group)
16601     (save-excursion
16602       (set-buffer nntp-server-buffer)
16603       (goto-char (point-min))
16604       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16605         (match-string 1)))))
16606
16607 \f
16608 ;;;
16609 ;;; Buffering of read articles.
16610 ;;;
16611
16612 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16613 (defvar gnus-backlog-articles nil)
16614 (defvar gnus-backlog-hashtb nil)
16615
16616 (defun gnus-backlog-buffer ()
16617   "Return the backlog buffer."
16618   (or (get-buffer gnus-backlog-buffer)
16619       (save-excursion
16620         (set-buffer (get-buffer-create gnus-backlog-buffer))
16621         (buffer-disable-undo (current-buffer))
16622         (setq buffer-read-only t)
16623         (gnus-add-current-to-buffer-list)
16624         (get-buffer gnus-backlog-buffer))))
16625
16626 (defun gnus-backlog-setup ()
16627   "Initialize backlog variables."
16628   (unless gnus-backlog-hashtb
16629     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16630
16631 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16632
16633 (defun gnus-backlog-shutdown ()
16634   "Clear all backlog variables and buffers."
16635   (when (get-buffer gnus-backlog-buffer)
16636     (kill-buffer gnus-backlog-buffer))
16637   (setq gnus-backlog-hashtb nil
16638         gnus-backlog-articles nil))
16639
16640 (defun gnus-backlog-enter-article (group number buffer)
16641   (gnus-backlog-setup)
16642   (let ((ident (intern (concat group ":" (int-to-string number))
16643                        gnus-backlog-hashtb))
16644         b)
16645     (if (memq ident gnus-backlog-articles)
16646         () ; It's already kept.
16647       ;; Remove the oldest article, if necessary.
16648       (and (numberp gnus-keep-backlog)
16649            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16650            (gnus-backlog-remove-oldest-article))
16651       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16652       ;; Insert the new article.
16653       (save-excursion
16654         (set-buffer (gnus-backlog-buffer))
16655         (let (buffer-read-only)
16656           (goto-char (point-max))
16657           (or (bolp) (insert "\n"))
16658           (setq b (point))
16659           (insert-buffer-substring buffer)
16660           ;; Tag the beginning of the article with the ident.
16661           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16662
16663 (defun gnus-backlog-remove-oldest-article ()
16664   (save-excursion
16665     (set-buffer (gnus-backlog-buffer))
16666     (goto-char (point-min))
16667     (if (zerop (buffer-size))
16668         () ; The buffer is empty.
16669       (let ((ident (get-text-property (point) 'gnus-backlog))
16670             buffer-read-only)
16671         ;; Remove the ident from the list of articles.
16672         (when ident
16673           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16674         ;; Delete the article itself.
16675         (delete-region
16676          (point) (next-single-property-change
16677                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16678
16679 (defun gnus-backlog-request-article (group number buffer)
16680   (when (numberp number)
16681     (gnus-backlog-setup)
16682     (let ((ident (intern (concat group ":" (int-to-string number))
16683                          gnus-backlog-hashtb))
16684           beg end)
16685       (when (memq ident gnus-backlog-articles)
16686         ;; It was in the backlog.
16687         (save-excursion
16688           (set-buffer (gnus-backlog-buffer))
16689           (if (not (setq beg (text-property-any
16690                               (point-min) (point-max) 'gnus-backlog
16691                               ident)))
16692               ;; It wasn't in the backlog after all.
16693               (ignore
16694                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16695             ;; Find the end (i. e., the beginning of the next article).
16696             (setq end
16697                   (next-single-property-change
16698                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16699         (let ((buffer-read-only nil))
16700           (erase-buffer)
16701           (insert-buffer-substring gnus-backlog-buffer beg end)
16702           t)))))
16703
16704 ;; Allow redefinition of Gnus functions.
16705
16706 (gnus-ems-redefine)
16707
16708 (provide 'gnus)
16709
16710 ;;; gnus.el ends here