*** 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-tree-buffer "*Tree*"
1537   "Buffer where Gnus thread trees are displayed.")
1538
1539 ;; Dummy variable.
1540 (defvar gnus-use-generic-from nil)
1541
1542 (defvar gnus-thread-indent-array nil)
1543 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1544
1545 (defvar gnus-newsrc-file-version nil)
1546
1547 (defvar gnus-method-history nil)
1548 ;; Variable holding the user answers to all method prompts.
1549
1550 (defvar gnus-group-history nil)
1551 ;; Variable holding the user answers to all group prompts.
1552
1553 (defvar gnus-server-alist nil
1554   "List of available servers.")
1555
1556 (defvar gnus-group-indentation-function nil)
1557
1558 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1559
1560 (defvar gnus-goto-missing-group-function nil)
1561
1562 (defvar gnus-override-subscribe-method nil)
1563
1564 (defvar gnus-group-goto-next-group-function nil
1565   "Function to override finding the next group after listing groups.")
1566
1567 (defconst gnus-article-mark-lists
1568   '((marked . tick) (replied . reply)
1569     (expirable . expire) (killed . killed)
1570     (bookmarks . bookmark) (dormant . dormant)
1571     (scored . score) (saved . save)
1572     (cached . cache)
1573     ))
1574
1575 ;; Avoid highlighting in kill files.
1576 (defvar gnus-summary-inhibit-highlight nil)
1577 (defvar gnus-newsgroup-selected-overlay nil)
1578
1579 (defvar gnus-inhibit-hiding nil)
1580 (defvar gnus-group-indentation "")
1581 (defvar gnus-inhibit-limiting nil)
1582 (defvar gnus-created-frames nil)
1583
1584 (defvar gnus-article-mode-map nil)
1585 (defvar gnus-dribble-buffer nil)
1586 (defvar gnus-headers-retrieved-by nil)
1587 (defvar gnus-article-reply nil)
1588 (defvar gnus-override-method nil)
1589 (defvar gnus-article-check-size nil)
1590
1591 (defvar gnus-current-score-file nil)
1592 (defvar gnus-newsgroup-adaptive-score-file nil)
1593 (defvar gnus-scores-exclude-files nil)
1594
1595 (defvar gnus-opened-servers nil)
1596
1597 (defvar gnus-current-move-group nil)
1598 (defvar gnus-current-copy-group nil)
1599 (defvar gnus-current-crosspost-group nil)
1600
1601 (defvar gnus-newsgroup-dependencies nil)
1602 (defvar gnus-newsgroup-async nil)
1603 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1604
1605 (defvar gnus-newsgroup-adaptive nil)
1606
1607 (defvar gnus-summary-display-table nil)
1608 (defvar gnus-summary-display-article-function nil)
1609
1610 (defvar gnus-summary-highlight-line-function nil
1611   "Function called after highlighting a summary line.")
1612
1613 (defvar gnus-group-line-format-alist
1614   `((?M gnus-tmp-marked-mark ?c)
1615     (?S gnus-tmp-subscribed ?c)
1616     (?L gnus-tmp-level ?d)
1617     (?N (cond ((eq number t) "*" )
1618               ((numberp number) 
1619                (int-to-string
1620                 (+ number
1621                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1622                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1623               (t number)) ?s)
1624     (?R gnus-tmp-number-of-read ?s)
1625     (?t gnus-tmp-number-total ?d)
1626     (?y gnus-tmp-number-of-unread ?s)
1627     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1628     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1629     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1630            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1631     (?g gnus-tmp-group ?s)
1632     (?G gnus-tmp-qualified-group ?s)
1633     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1634     (?D gnus-tmp-newsgroup-description ?s)
1635     (?o gnus-tmp-moderated ?c)
1636     (?O gnus-tmp-moderated-string ?s)
1637     (?p gnus-tmp-process-marked ?c)
1638     (?s gnus-tmp-news-server ?s)
1639     (?n gnus-tmp-news-method ?s)
1640     (?P gnus-group-indentation ?s)
1641     (?l gnus-tmp-grouplens ?s)
1642     (?z gnus-tmp-news-method-string ?s)
1643     (?u gnus-tmp-user-defined ?s)))
1644
1645 (defvar gnus-summary-line-format-alist
1646   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1647     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1648     (?s gnus-tmp-subject-or-nil ?s)
1649     (?n gnus-tmp-name ?s)
1650     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1651         ?s)
1652     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1653             gnus-tmp-from) ?s)
1654     (?F gnus-tmp-from ?s)
1655     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1656     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1657     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1658     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1659     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1660     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1661     (?L gnus-tmp-lines ?d)
1662     (?I gnus-tmp-indentation ?s)
1663     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1664     (?R gnus-tmp-replied ?c)
1665     (?\[ gnus-tmp-opening-bracket ?c)
1666     (?\] gnus-tmp-closing-bracket ?c)
1667     (?\> (make-string gnus-tmp-level ? ) ?s)
1668     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1669     (?i gnus-tmp-score ?d)
1670     (?z gnus-tmp-score-char ?c)
1671     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1672     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1673     (?U gnus-tmp-unread ?c)
1674     (?t (gnus-summary-number-of-articles-in-thread
1675          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1676         ?d)
1677     (?e (gnus-summary-number-of-articles-in-thread
1678          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1679         ?c)
1680     (?u gnus-tmp-user-defined ?s))
1681   "An alist of format specifications that can appear in summary lines,
1682 and what variables they correspond with, along with the type of the
1683 variable (string, integer, character, etc).")
1684
1685 (defvar gnus-summary-dummy-line-format-alist
1686   `((?S gnus-tmp-subject ?s)
1687     (?N gnus-tmp-number ?d)
1688     (?u gnus-tmp-user-defined ?s)))
1689
1690 (defvar gnus-summary-mode-line-format-alist
1691   `((?G gnus-tmp-group-name ?s)
1692     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1693     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1694     (?A gnus-tmp-article-number ?d)
1695     (?Z gnus-tmp-unread-and-unselected ?s)
1696     (?V gnus-version ?s)
1697     (?U gnus-tmp-unread ?d)
1698     (?S gnus-tmp-subject ?s)
1699     (?e gnus-tmp-unselected ?d)
1700     (?u gnus-tmp-user-defined ?s)
1701     (?d (length gnus-newsgroup-dormant) ?d)
1702     (?t (length gnus-newsgroup-marked) ?d)
1703     (?r (length gnus-newsgroup-reads) ?d)
1704     (?E gnus-newsgroup-expunged-tally ?d)
1705     (?s (gnus-current-score-file-nondirectory) ?s)))
1706
1707 (defvar gnus-article-mode-line-format-alist
1708   gnus-summary-mode-line-format-alist)
1709
1710 (defvar gnus-group-mode-line-format-alist
1711   `((?S gnus-tmp-news-server ?s)
1712     (?M gnus-tmp-news-method ?s)
1713     (?u gnus-tmp-user-defined ?s)))
1714
1715 (defvar gnus-have-read-active-file nil)
1716
1717 (defconst gnus-maintainer
1718   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1719   "The mail address of the Gnus maintainers.")
1720
1721 (defconst gnus-version "September Gnus v0.84"
1722   "Version number for this version of Gnus.")
1723
1724 (defvar gnus-info-nodes
1725   '((gnus-group-mode "(gnus)The Group Buffer")
1726     (gnus-summary-mode "(gnus)The Summary Buffer")
1727     (gnus-article-mode "(gnus)The Article Buffer"))
1728   "Assoc list of major modes and related Info nodes.")
1729
1730 (defvar gnus-group-buffer "*Group*")
1731 (defvar gnus-summary-buffer "*Summary*")
1732 (defvar gnus-article-buffer "*Article*")
1733 (defvar gnus-server-buffer "*Server*")
1734
1735 (defvar gnus-work-buffer " *gnus work*")
1736
1737 (defvar gnus-original-article-buffer " *Original Article*")
1738 (defvar gnus-original-article nil)
1739
1740 (defvar gnus-buffer-list nil
1741   "Gnus buffers that should be killed on exit.")
1742
1743 (defvar gnus-slave nil
1744   "Whether this Gnus is a slave or not.")
1745
1746 (defvar gnus-variable-list
1747   '(gnus-newsrc-options gnus-newsrc-options-n
1748     gnus-newsrc-last-checked-date
1749     gnus-newsrc-alist gnus-server-alist
1750     gnus-killed-list gnus-zombie-list
1751     gnus-topic-topology gnus-topic-alist
1752     gnus-format-specs)
1753   "Gnus variables saved in the quick startup file.")
1754
1755 (defvar gnus-newsrc-options nil
1756   "Options line in the .newsrc file.")
1757
1758 (defvar gnus-newsrc-options-n nil
1759   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1760
1761 (defvar gnus-newsrc-last-checked-date nil
1762   "Date Gnus last asked server for new newsgroups.")
1763
1764 (defvar gnus-topic-topology nil
1765   "The complete topic hierarchy.")
1766
1767 (defvar gnus-topic-alist nil
1768   "The complete topic-group alist.")
1769
1770 (defvar gnus-newsrc-alist nil
1771   "Assoc list of read articles.
1772 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1773
1774 (defvar gnus-newsrc-hashtb nil
1775   "Hashtable of gnus-newsrc-alist.")
1776
1777 (defvar gnus-killed-list nil
1778   "List of killed newsgroups.")
1779
1780 (defvar gnus-killed-hashtb nil
1781   "Hash table equivalent of gnus-killed-list.")
1782
1783 (defvar gnus-zombie-list nil
1784   "List of almost dead newsgroups.")
1785
1786 (defvar gnus-description-hashtb nil
1787   "Descriptions of newsgroups.")
1788
1789 (defvar gnus-list-of-killed-groups nil
1790   "List of newsgroups that have recently been killed by the user.")
1791
1792 (defvar gnus-active-hashtb nil
1793   "Hashtable of active articles.")
1794
1795 (defvar gnus-moderated-list nil
1796   "List of moderated newsgroups.")
1797
1798 (defvar gnus-group-marked nil)
1799
1800 (defvar gnus-current-startup-file nil
1801   "Startup file for the current host.")
1802
1803 (defvar gnus-last-search-regexp nil
1804   "Default regexp for article search command.")
1805
1806 (defvar gnus-last-shell-command nil
1807   "Default shell command on article.")
1808
1809 (defvar gnus-current-select-method nil
1810   "The current method for selecting a newsgroup.")
1811
1812 (defvar gnus-group-list-mode nil)
1813
1814 (defvar gnus-article-internal-prepare-hook nil)
1815
1816 (defvar gnus-newsgroup-name nil)
1817 (defvar gnus-newsgroup-begin nil)
1818 (defvar gnus-newsgroup-end nil)
1819 (defvar gnus-newsgroup-last-rmail nil)
1820 (defvar gnus-newsgroup-last-mail nil)
1821 (defvar gnus-newsgroup-last-folder nil)
1822 (defvar gnus-newsgroup-last-file nil)
1823 (defvar gnus-newsgroup-auto-expire nil)
1824 (defvar gnus-newsgroup-active nil)
1825
1826 (defvar gnus-newsgroup-data nil)
1827 (defvar gnus-newsgroup-data-reverse nil)
1828 (defvar gnus-newsgroup-limit nil)
1829 (defvar gnus-newsgroup-limits nil)
1830
1831 (defvar gnus-newsgroup-unreads nil
1832   "List of unread articles in the current newsgroup.")
1833
1834 (defvar gnus-newsgroup-unselected nil
1835   "List of unselected unread articles in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-reads nil
1838   "Alist of read articles and article marks in the current newsgroup.")
1839
1840 (defvar gnus-newsgroup-expunged-tally nil)
1841
1842 (defvar gnus-newsgroup-marked nil
1843   "List of ticked articles in the current newsgroup (a subset of unread art).")
1844
1845 (defvar gnus-newsgroup-killed nil
1846   "List of ranges of articles that have been through the scoring process.")
1847
1848 (defvar gnus-newsgroup-cached nil
1849   "List of articles that come from the article cache.")
1850
1851 (defvar gnus-newsgroup-saved nil
1852   "List of articles that have been saved.")
1853
1854 (defvar gnus-newsgroup-kill-headers nil)
1855
1856 (defvar gnus-newsgroup-replied nil
1857   "List of articles that have been replied to in the current newsgroup.")
1858
1859 (defvar gnus-newsgroup-expirable nil
1860   "List of articles in the current newsgroup that can be expired.")
1861
1862 (defvar gnus-newsgroup-processable nil
1863   "List of articles in the current newsgroup that can be processed.")
1864
1865 (defvar gnus-newsgroup-bookmarks nil
1866   "List of articles in the current newsgroup that have bookmarks.")
1867
1868 (defvar gnus-newsgroup-dormant nil
1869   "List of dormant articles in the current newsgroup.")
1870
1871 (defvar gnus-newsgroup-scored nil
1872   "List of scored articles in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-headers nil
1875   "List of article headers in the current newsgroup.")
1876
1877 (defvar gnus-newsgroup-threads nil)
1878
1879 (defvar gnus-newsgroup-prepared nil
1880   "Whether the current group has been prepared properly.")
1881
1882 (defvar gnus-newsgroup-ancient nil
1883   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1884
1885 (defvar gnus-newsgroup-sparse nil)
1886
1887 (defvar gnus-current-article nil)
1888 (defvar gnus-article-current nil)
1889 (defvar gnus-current-headers nil)
1890 (defvar gnus-have-all-headers nil)
1891 (defvar gnus-last-article nil)
1892 (defvar gnus-newsgroup-history nil)
1893 (defvar gnus-current-kill-article nil)
1894
1895 ;; Save window configuration.
1896 (defvar gnus-prev-winconf nil)
1897
1898 (defvar gnus-summary-mark-positions nil)
1899 (defvar gnus-group-mark-positions nil)
1900
1901 (defvar gnus-reffed-article-number nil)
1902
1903 ;;; Let the byte-compiler know that we know about this variable.
1904 (defvar rmail-default-rmail-file)
1905
1906 (defvar gnus-cache-removable-articles nil)
1907
1908 (defvar gnus-dead-summary nil)
1909
1910 (defconst gnus-summary-local-variables
1911   '(gnus-newsgroup-name
1912     gnus-newsgroup-begin gnus-newsgroup-end
1913     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1914     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1915     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1916     gnus-newsgroup-unselected gnus-newsgroup-marked
1917     gnus-newsgroup-reads gnus-newsgroup-saved
1918     gnus-newsgroup-replied gnus-newsgroup-expirable
1919     gnus-newsgroup-processable gnus-newsgroup-killed
1920     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1921     gnus-newsgroup-headers gnus-newsgroup-threads
1922     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1923     gnus-current-article gnus-current-headers gnus-have-all-headers
1924     gnus-last-article gnus-article-internal-prepare-hook
1925     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1926     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1927     gnus-newsgroup-async gnus-thread-expunge-below
1928     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1929     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1930     gnus-newsgroup-history gnus-newsgroup-ancient
1931     gnus-newsgroup-sparse
1932     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1933     gnus-newsgroup-adaptive-score-file
1934     (gnus-newsgroup-expunged-tally . 0)
1935     gnus-cache-removable-articles gnus-newsgroup-cached
1936     gnus-newsgroup-data gnus-newsgroup-data-reverse
1937     gnus-newsgroup-limit gnus-newsgroup-limits)
1938   "Variables that are buffer-local to the summary buffers.")
1939
1940 (defconst gnus-bug-message
1941   "Sending a bug report to the Gnus Towers.
1942 ========================================
1943
1944 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1945 be sent to the Gnus Bug Exterminators.
1946
1947 At the bottom of the buffer you'll see lots of variable settings.
1948 Please do not delete those.  They will tell the Bug People what your
1949 environment is, so that it will be easier to locate the bugs.
1950
1951 If you have found a bug that makes Emacs go \"beep\", set
1952 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1953 and include the backtrace in your bug report.
1954
1955 Please describe the bug in annoying, painstaking detail.
1956
1957 Thank you for your help in stamping out bugs.
1958 ")
1959
1960 ;;; End of variables.
1961
1962 ;; Define some autoload functions Gnus might use.
1963 (eval-and-compile
1964
1965   ;; This little mapcar goes through the list below and marks the
1966   ;; symbols in question as autoloaded functions.
1967   (mapcar
1968    (lambda (package)
1969      (let ((interactive (nth 1 (memq ':interactive package))))
1970        (mapcar
1971         (lambda (function)
1972           (let (keymap)
1973             (when (consp function)
1974               (setq keymap (car (memq 'keymap function)))
1975               (setq function (car function)))
1976             (autoload function (car package) nil interactive keymap)))
1977         (if (eq (nth 1 package) ':interactive)
1978             (cdddr package)
1979           (cdr package)))))
1980    '(("metamail" metamail-buffer)
1981      ("info" Info-goto-node)
1982      ("hexl" hexl-hex-string-to-integer)
1983      ("pp" pp pp-to-string pp-eval-expression)
1984      ("mail-extr" mail-extract-address-components)
1985      ("nnmail" nnmail-split-fancy nnmail-article-group)
1986      ("nnvirtual" nnvirtual-catchup-group)
1987      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1988       timezone-make-sortable-date timezone-make-time-string)
1989      ("sendmail" mail-position-on-field mail-setup)
1990      ("rmailout" rmail-output)
1991      ("rnewspost" news-mail-other-window news-reply-yank-original
1992       news-caesar-buffer-body)
1993      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1994       rmail-show-message)
1995      ("gnus-soup" :interactive t
1996       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1997       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1998      ("nnsoup" nnsoup-pack-replies)
1999      ("gnus-scomo" :interactive t gnus-score-mode)
2000      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2001       gnus-Folder-save-name gnus-folder-save-name)
2002      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2003      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2004       gnus-server-make-menu-bar gnus-article-make-menu-bar
2005       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2006       gnus-summary-highlight-line gnus-carpal-setup-buffer
2007       gnus-group-highlight-line
2008       gnus-article-add-button gnus-insert-next-page-button
2009       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2010      ("gnus-vis" :interactive t
2011       gnus-article-push-button gnus-article-press-button
2012       gnus-article-highlight gnus-article-highlight-some
2013       gnus-article-highlight-headers gnus-article-highlight-signature
2014       gnus-article-add-buttons gnus-article-add-buttons-to-head
2015       gnus-article-next-button gnus-article-prev-button)
2016      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2017       gnus-demon-add-disconnection gnus-demon-add-handler
2018       gnus-demon-remove-handler)
2019      ("gnus-demon" :interactive t
2020       gnus-demon-init gnus-demon-cancel)
2021      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2022       gnus-tree-open gnus-tree-close)
2023      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2024       gnus-nocem-unwanted-article-p)
2025      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2026      ("gnus-srvr" gnus-browse-foreign-server)
2027      ("gnus-cite" :interactive t
2028       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2029       gnus-article-hide-citation gnus-article-fill-cited-article
2030       gnus-article-hide-citation-in-followups)
2031      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2032       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2033       gnus-execute gnus-expunge)
2034      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2035       gnus-cache-possibly-remove-articles gnus-cache-request-article
2036       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2037       gnus-cache-enter-remove-article gnus-cached-article-p
2038       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2039      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2040       gnus-cache-remove-article)
2041      ("gnus-score" :interactive t
2042       gnus-summary-increase-score gnus-summary-lower-score
2043       gnus-score-flush-cache gnus-score-close
2044       gnus-score-raise-same-subject-and-select
2045       gnus-score-raise-same-subject gnus-score-default
2046       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2047       gnus-score-lower-same-subject gnus-score-lower-thread
2048       gnus-possibly-score-headers)
2049      ("gnus-score"
2050       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2051       gnus-current-score-file-nondirectory gnus-score-adaptive
2052       gnus-score-find-trace gnus-score-file-name)
2053      ("gnus-edit" :interactive t gnus-score-customize)
2054      ("gnus-topic" :interactive t gnus-topic-mode)
2055      ("gnus-topic" gnus-topic-remove-group)
2056      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2057      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2058      ("gnus-uu" :interactive t
2059       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2060       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2061       gnus-uu-mark-by-regexp gnus-uu-mark-all
2062       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2063       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2064       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2065       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2066       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2067       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2068       gnus-uu-decode-binhex-view)
2069      ("gnus-msg" (gnus-summary-send-map keymap)
2070       gnus-mail-yank-original gnus-mail-send-and-exit
2071       gnus-sendmail-setup-mail gnus-article-mail
2072       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2073      ("gnus-msg" :interactive t
2074       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2075       gnus-summary-followup gnus-summary-followup-with-original
2076       gnus-summary-followup-and-reply
2077       gnus-summary-followup-and-reply-with-original
2078       gnus-summary-cancel-article gnus-summary-supersede-article
2079       gnus-post-news gnus-inews-news gnus-cancel-news
2080       gnus-summary-reply gnus-summary-reply-with-original
2081       gnus-summary-mail-forward gnus-summary-mail-other-window
2082       gnus-bug)
2083      ("gnus-picon" :interactive t gnus-article-display-picons
2084       gnus-group-display-picons gnus-picons-article-display-x-face)
2085      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2086       gnus-grouplens-mode)
2087      ("gnus-vm" gnus-vm-mail-setup)
2088      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2089       gnus-summary-save-article-vm gnus-yank-article))))
2090
2091 \f
2092
2093 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2094 ;; If you want the cursor to go somewhere else, set these two
2095 ;; functions in some startup hook to whatever you want.
2096 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2097 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2098
2099 ;;; Various macros and substs.
2100
2101 (defun gnus-header-from (header)
2102   (mail-header-from header))
2103
2104 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2105   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2106   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2107     `(let ((,tempvar (selected-window)))
2108        (unwind-protect
2109            (progn
2110              (pop-to-buffer ,buffer)
2111              ,@forms)
2112          (select-window ,tempvar)))))
2113
2114 (defmacro gnus-gethash (string hashtable)
2115   "Get hash value of STRING in HASHTABLE."
2116   `(symbol-value (intern-soft ,string ,hashtable)))
2117
2118 (defmacro gnus-sethash (string value hashtable)
2119   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2120   `(set (intern ,string ,hashtable) ,value))
2121
2122 (defmacro gnus-intern-safe (string hashtable)
2123   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2124   `(let ((symbol (intern ,string ,hashtable)))
2125      (or (boundp symbol)
2126          (set symbol nil))
2127      symbol))
2128
2129 (defmacro gnus-group-unread (group)
2130   "Get the currently computed number of unread articles in GROUP."
2131   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2132
2133 (defmacro gnus-group-entry (group)
2134   "Get the newsrc entry for GROUP."
2135   `(gnus-gethash ,group gnus-newsrc-hashtb))
2136
2137 (defmacro gnus-active (group)
2138   "Get active info on GROUP."
2139   `(gnus-gethash ,group gnus-active-hashtb))
2140
2141 (defmacro gnus-set-active (group active)
2142   "Set GROUP's active info."
2143   `(gnus-sethash ,group ,active gnus-active-hashtb))
2144
2145 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2146 ;;   function `substring' might cut on a middle of multi-octet
2147 ;;   character.
2148 (defun gnus-truncate-string (str width)
2149   (substring str 0 width))
2150
2151 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2152 ;; to limit the length of a string.  This function is necessary since
2153 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2154 (defsubst gnus-limit-string (str width)
2155   (if (> (length str) width)
2156       (substring str 0 width)
2157     str))
2158
2159 (defsubst gnus-simplify-subject-re (subject)
2160   "Remove \"Re:\" from subject lines."
2161   (if (string-match "^[Rr][Ee]: *" subject)
2162       (substring subject (match-end 0))
2163     subject))
2164
2165 (defsubst gnus-functionp (form)
2166   "Return non-nil if FORM is funcallable."
2167   (or (and (symbolp form) (fboundp form))
2168       (and (listp form) (eq (car form) 'lambda))))
2169
2170 (defsubst gnus-goto-char (point)
2171   (and point (goto-char point)))
2172
2173 (defmacro gnus-buffer-exists-p (buffer)
2174   `(let ((buffer ,buffer))
2175      (and buffer
2176           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2177                    buffer))))
2178
2179 (defmacro gnus-kill-buffer (buffer)
2180   `(let ((buf ,buffer))
2181      (if (gnus-buffer-exists-p buf)
2182          (kill-buffer buf))))
2183
2184 (defsubst gnus-point-at-bol ()
2185   "Return point at the beginning of the line."
2186   (let ((p (point)))
2187     (beginning-of-line)
2188     (prog1
2189         (point)
2190       (goto-char p))))
2191
2192 (defsubst gnus-point-at-eol ()
2193   "Return point at the end of the line."
2194   (let ((p (point)))
2195     (end-of-line)
2196     (prog1
2197         (point)
2198       (goto-char p))))
2199
2200 (defun gnus-alive-p ()
2201   "Say whether Gnus is running or not."
2202   (and gnus-group-buffer
2203        (get-buffer gnus-group-buffer)))
2204
2205 ;; Delete the current line (and the next N lines.);
2206 (defmacro gnus-delete-line (&optional n)
2207   `(delete-region (progn (beginning-of-line) (point))
2208                   (progn (forward-line ,(or n 1)) (point))))
2209
2210 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2211 (defvar gnus-init-inhibit nil)
2212 (defun gnus-read-init-file (&optional inhibit-next)
2213   (if gnus-init-inhibit
2214       (setq gnus-init-inhibit nil)
2215     (setq gnus-init-inhibit inhibit-next)
2216     (and gnus-init-file
2217          (or (and (file-exists-p gnus-init-file)
2218                   ;; Don't try to load a directory.
2219                   (not (file-directory-p gnus-init-file)))
2220              (file-exists-p (concat gnus-init-file ".el"))
2221              (file-exists-p (concat gnus-init-file ".elc")))
2222          (condition-case var
2223              (load gnus-init-file nil t)
2224            (error
2225             (error "Error in %s: %s" gnus-init-file var))))))
2226
2227 ;; Info access macros.
2228
2229 (defmacro gnus-info-group (info)
2230   `(nth 0 ,info))
2231 (defmacro gnus-info-rank (info)
2232   `(nth 1 ,info))
2233 (defmacro gnus-info-read (info)
2234   `(nth 2 ,info))
2235 (defmacro gnus-info-marks (info)
2236   `(nth 3 ,info))
2237 (defmacro gnus-info-method (info)
2238   `(nth 4 ,info))
2239 (defmacro gnus-info-params (info)
2240   `(nth 5 ,info))
2241
2242 (defmacro gnus-info-level (info)
2243   `(let ((rank (gnus-info-rank ,info)))
2244      (if (consp rank)
2245          (car rank)
2246        rank)))
2247 (defmacro gnus-info-score (info)
2248   `(let ((rank (gnus-info-rank ,info)))
2249      (or (and (consp rank) (cdr rank)) 0)))
2250
2251 (defmacro gnus-info-set-group (info group)
2252   `(setcar ,info ,group))
2253 (defmacro gnus-info-set-rank (info rank)
2254   `(setcar (nthcdr 1 ,info) ,rank))
2255 (defmacro gnus-info-set-read (info read)
2256   `(setcar (nthcdr 2 ,info) ,read))
2257 (defmacro gnus-info-set-marks (info marks)
2258   `(setcar (nthcdr 3 ,info) ,marks))
2259 (defmacro gnus-info-set-method (info method)
2260   `(setcar (nthcdr 4 ,info) ,method))
2261 (defmacro gnus-info-set-params (info params)
2262   `(setcar (nthcdr 5 ,info) ,params))
2263
2264 (defmacro gnus-info-set-level (info level)
2265   `(let ((rank (cdr ,info)))
2266      (if (consp (car rank))
2267          (setcar (car rank) ,level)
2268        (setcar rank ,level))))
2269 (defmacro gnus-info-set-score (info score)
2270   `(let ((rank (cdr ,info)))
2271      (if (consp (car rank))
2272          (setcdr (car rank) ,score)
2273        (setcar rank (cons (car rank) ,score)))))
2274
2275 (defmacro gnus-get-info (group)
2276   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2277
2278 (defun gnus-byte-code (func)
2279   "Return a form that can be `eval'ed based on FUNC."
2280   (let ((fval (symbol-function func)))
2281     (if (byte-code-function-p fval)
2282         (let ((flist (append fval nil)))
2283           (setcar flist 'byte-code)
2284           flist)
2285       (cons 'progn (cddr fval)))))
2286
2287 ;;; Load the compatability functions.
2288
2289 (require 'gnus-cus)
2290 (require 'gnus-ems)
2291
2292 \f
2293 ;;;
2294 ;;; Shutdown
2295 ;;;
2296
2297 (defvar gnus-shutdown-alist nil)
2298
2299 (defun gnus-add-shutdown (function &rest symbols)
2300   "Run FUNCTION whenever one of SYMBOLS is shut down."
2301   (push (cons function symbols) gnus-shutdown-alist))
2302
2303 (defun gnus-shutdown (symbol)
2304   "Shut down everything that waits for SYMBOL."
2305   (let ((alist gnus-shutdown-alist)
2306         entry)
2307     (while (setq entry (pop alist))
2308       (when (memq symbol (cdr entry))
2309         (funcall (car entry))))))
2310
2311 \f
2312
2313 ;; Format specs.  The chunks below are the machine-generated forms
2314 ;; that are to be evaled as the result of the default format strings.
2315 ;; We write them in here to get them byte-compiled.  That way the
2316 ;; default actions will be quite fast, while still retaining the full
2317 ;; flexibility of the user-defined format specs.
2318
2319 ;; First we have lots of dummy defvars to let the compiler know these
2320 ;; are really dynamic variables.
2321
2322 (defvar gnus-tmp-unread)
2323 (defvar gnus-tmp-replied)
2324 (defvar gnus-tmp-score-char)
2325 (defvar gnus-tmp-indentation)
2326 (defvar gnus-tmp-opening-bracket)
2327 (defvar gnus-tmp-lines)
2328 (defvar gnus-tmp-name)
2329 (defvar gnus-tmp-closing-bracket)
2330 (defvar gnus-tmp-subject-or-nil)
2331 (defvar gnus-tmp-subject)
2332 (defvar gnus-tmp-marked)
2333 (defvar gnus-tmp-marked-mark)
2334 (defvar gnus-tmp-subscribed)
2335 (defvar gnus-tmp-process-marked)
2336 (defvar gnus-tmp-number-of-unread)
2337 (defvar gnus-tmp-group-name)
2338 (defvar gnus-tmp-group)
2339 (defvar gnus-tmp-article-number)
2340 (defvar gnus-tmp-unread-and-unselected)
2341 (defvar gnus-tmp-news-method)
2342 (defvar gnus-tmp-news-server)
2343 (defvar gnus-tmp-article-number)
2344 (defvar gnus-mouse-face)
2345 (defvar gnus-mouse-face-prop)
2346
2347 (defun gnus-summary-line-format-spec ()
2348   (insert gnus-tmp-unread gnus-tmp-replied
2349           gnus-tmp-score-char gnus-tmp-indentation)
2350   (put-text-property
2351    (point)
2352    (progn
2353      (insert
2354       gnus-tmp-opening-bracket
2355       (format "%4d: %-20s"
2356               gnus-tmp-lines
2357               (if (> (length gnus-tmp-name) 20)
2358                   (substring gnus-tmp-name 0 20)
2359                 gnus-tmp-name))
2360       gnus-tmp-closing-bracket)
2361      (point))
2362    gnus-mouse-face-prop gnus-mouse-face)
2363   (insert " " gnus-tmp-subject-or-nil "\n"))
2364
2365 (defvar gnus-summary-line-format-spec
2366   (gnus-byte-code 'gnus-summary-line-format-spec))
2367
2368 (defun gnus-summary-dummy-line-format-spec ()
2369   (insert "*  ")
2370   (put-text-property
2371    (point)
2372    (progn
2373      (insert ":                          :")
2374      (point))
2375    gnus-mouse-face-prop gnus-mouse-face)
2376   (insert " " gnus-tmp-subject "\n"))
2377
2378 (defvar gnus-summary-dummy-line-format-spec
2379   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2380
2381 (defun gnus-group-line-format-spec ()
2382   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2383           gnus-tmp-process-marked
2384           gnus-group-indentation
2385           (format "%5s: " gnus-tmp-number-of-unread))
2386   (put-text-property
2387    (point)
2388    (progn
2389      (insert gnus-tmp-group "\n")
2390      (1- (point)))
2391    gnus-mouse-face-prop gnus-mouse-face))
2392 (defvar gnus-group-line-format-spec
2393   (gnus-byte-code 'gnus-group-line-format-spec))
2394
2395 (defvar gnus-format-specs
2396   `((version . ,emacs-version)
2397     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2398     (summary-dummy ,gnus-summary-dummy-line-format
2399                    ,gnus-summary-dummy-line-format-spec)
2400     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2401
2402 (defvar gnus-article-mode-line-format-spec nil)
2403 (defvar gnus-summary-mode-line-format-spec nil)
2404 (defvar gnus-group-mode-line-format-spec nil)
2405
2406 ;;; Phew.  All that gruft is over, fortunately.
2407
2408 \f
2409 ;;;
2410 ;;; Gnus Utility Functions
2411 ;;;
2412
2413 (defun gnus-extract-address-components (from)
2414   (let (name address)
2415     ;; First find the address - the thing with the @ in it.  This may
2416     ;; not be accurate in mail addresses, but does the trick most of
2417     ;; the time in news messages.
2418     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2419         (setq address (substring from (match-beginning 0) (match-end 0))))
2420     ;; Then we check whether the "name <address>" format is used.
2421     (and address
2422          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2423          ;; Linear white space is not required.
2424          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2425          (and (setq name (substring from 0 (match-beginning 0)))
2426               ;; Strip any quotes from the name.
2427               (string-match "\".*\"" name)
2428               (setq name (substring name 1 (1- (match-end 0))))))
2429     ;; If not, then "address (name)" is used.
2430     (or name
2431         (and (string-match "(.+)" from)
2432              (setq name (substring from (1+ (match-beginning 0))
2433                                    (1- (match-end 0)))))
2434         (and (string-match "()" from)
2435              (setq name address))
2436         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2437         ;; XOVER might not support folded From headers.
2438         (and (string-match "(.*" from)
2439              (setq name (substring from (1+ (match-beginning 0))
2440                                    (match-end 0)))))
2441     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2442     (list (or name from) (or address from))))
2443
2444 (defun gnus-fetch-field (field)
2445   "Return the value of the header FIELD of current article."
2446   (save-excursion
2447     (save-restriction
2448       (let ((case-fold-search t)
2449             (inhibit-point-motion-hooks t))
2450         (nnheader-narrow-to-headers)
2451         (mail-fetch-field field)))))
2452
2453 (defun gnus-goto-colon ()
2454   (beginning-of-line)
2455   (search-forward ":" (gnus-point-at-eol) t))
2456
2457 ;;;###autoload
2458 (defun gnus-update-format (var)
2459   "Update the format specification near point."
2460   (interactive
2461    (list
2462     (save-excursion
2463       (eval-defun nil)
2464       ;; Find the end of the current word.
2465       (re-search-forward "[ \t\n]" nil t)
2466       ;; Search backward.
2467       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2468         (match-string 1)))))
2469   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2470                               (match-string 1 var))))
2471          (entry (assq type gnus-format-specs))
2472          value spec)
2473     (when entry
2474       (setq gnus-format-specs (delq entry gnus-format-specs)))
2475     (set
2476      (intern (format "%s-spec" var))
2477      (gnus-parse-format (setq value (symbol-value (intern var)))
2478                         (symbol-value (intern (format "%s-alist" var)))
2479                         (not (string-match "mode" var))))
2480     (setq spec (symbol-value (intern (format "%s-spec" var))))
2481     (push (list type value spec) gnus-format-specs)
2482
2483     (pop-to-buffer "*Gnus Format*")
2484     (erase-buffer)
2485     (lisp-interaction-mode)
2486     (insert (pp-to-string spec))))
2487
2488 (defun gnus-update-format-specifications (&optional force)
2489   "Update all (necessary) format specifications."
2490   ;; Make the indentation array.
2491   (gnus-make-thread-indent-array)
2492
2493   ;; See whether all the stored info needs to be flushed.
2494   (when (or force
2495             (not (equal emacs-version
2496                         (cdr (assq 'version gnus-format-specs)))))
2497     (setq gnus-format-specs nil))
2498
2499   ;; Go through all the formats and see whether they need updating.
2500   (let ((types '(summary summary-dummy group
2501                          summary-mode group-mode article-mode))
2502         new-format entry type val)
2503     (while (setq type (pop types))
2504       ;; Jump to the proper buffer to find out the value of
2505       ;; the variable, if possible.  (It may be buffer-local.)
2506       (save-excursion
2507         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2508               val)
2509           (when (and (boundp buffer)
2510                      (setq val (symbol-value buffer))
2511                      (get-buffer val)
2512                      (buffer-name (get-buffer val)))
2513             (set-buffer (get-buffer val)))
2514           (setq new-format (symbol-value
2515                             (intern (format "gnus-%s-line-format" type))))))
2516       (setq entry (cdr (assq type gnus-format-specs)))
2517       (if (and entry
2518                (equal (car entry) new-format))
2519           ;; Use the old format.
2520           (set (intern (format "gnus-%s-line-format-spec" type))
2521                (cadr entry))
2522         ;; This is a new format.
2523         (setq val
2524               (if (not (stringp new-format))
2525                   ;; This is a function call or something.
2526                   new-format
2527                 ;; This is a "real" format.
2528                 (gnus-parse-format
2529                  new-format
2530                  (symbol-value
2531                   (intern (format "gnus-%s-line-format-alist"
2532                                   (if (eq type 'article-mode)
2533                                       'summary-mode type))))
2534                  (not (string-match "mode$" (symbol-name type))))))
2535         ;; Enter the new format spec into the list.
2536         (if entry
2537             (progn
2538               (setcar (cdr entry) val)
2539               (setcar entry new-format))
2540           (push (list type new-format val) gnus-format-specs))
2541         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2542
2543   (unless (assq 'version gnus-format-specs)
2544     (push (cons 'version emacs-version) gnus-format-specs))
2545
2546   (gnus-update-group-mark-positions)
2547   (gnus-update-summary-mark-positions))
2548
2549 (defun gnus-update-summary-mark-positions ()
2550   "Compute where the summary marks are to go."
2551   (save-excursion
2552     (when (and gnus-summary-buffer
2553                (get-buffer gnus-summary-buffer)
2554                (buffer-name (get-buffer gnus-summary-buffer)))
2555       (set-buffer gnus-summary-buffer))
2556     (let ((gnus-replied-mark 129)
2557           (gnus-score-below-mark 130)
2558           (gnus-score-over-mark 130)
2559           (thread nil)
2560           (gnus-visual nil)
2561           (spec gnus-summary-line-format-spec)
2562           pos)
2563       (save-excursion
2564         (gnus-set-work-buffer)
2565         (let ((gnus-summary-line-format-spec spec))
2566           (gnus-summary-insert-line
2567            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2568           (goto-char (point-min))
2569           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2570                                              (- (point) 2)))))
2571           (goto-char (point-min))
2572           (push (cons 'replied (and (search-forward "\201" nil t) 
2573                                     (- (point) 2)))
2574                 pos)
2575           (goto-char (point-min))
2576           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2577                 pos)))
2578       (setq gnus-summary-mark-positions pos))))
2579
2580 (defun gnus-update-group-mark-positions ()
2581   (save-excursion
2582     (let ((gnus-process-mark 128)
2583           (gnus-group-marked '("dummy.group"))
2584           (gnus-active-hashtb (make-vector 10 0)))
2585       (gnus-set-active "dummy.group" '(0 . 0))
2586       (gnus-set-work-buffer)
2587       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2588       (goto-char (point-min))
2589       (setq gnus-group-mark-positions
2590             (list (cons 'process (and (search-forward "\200" nil t)
2591                                       (- (point) 2))))))))
2592
2593 (defvar gnus-mouse-face-0 'highlight)
2594 (defvar gnus-mouse-face-1 'highlight)
2595 (defvar gnus-mouse-face-2 'highlight)
2596 (defvar gnus-mouse-face-3 'highlight)
2597 (defvar gnus-mouse-face-4 'highlight)
2598
2599 (defun gnus-mouse-face-function (form type)
2600   `(put-text-property
2601     (point) (progn ,@form (point))
2602     gnus-mouse-face-prop
2603     ,(if (equal type 0)
2604          'gnus-mouse-face
2605        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2606
2607 (defvar gnus-face-0 'bold)
2608 (defvar gnus-face-1 'italic)
2609 (defvar gnus-face-2 'bold-italic)
2610 (defvar gnus-face-3 'bold)
2611 (defvar gnus-face-4 'bold)
2612
2613 (defun gnus-face-face-function (form type)
2614   `(put-text-property
2615     (point) (progn ,@form (point))
2616     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2617
2618 (defun gnus-max-width-function (el max-width)
2619   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2620   (if (symbolp el)
2621       `(if (> (length ,el) ,max-width)
2622            (substring ,el 0 ,max-width)
2623          ,el)
2624     `(let ((val (eval ,el)))
2625        (if (numberp val)
2626            (setq val (int-to-string val)))
2627        (if (> (length val) ,max-width)
2628            (substring val 0 ,max-width)
2629          val))))
2630
2631 (defun gnus-parse-format (format spec-alist &optional insert)
2632   ;; This function parses the FORMAT string with the help of the
2633   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2634   ;; string.  If the FORMAT string contains the specifiers %( and %)
2635   ;; the text between them will have the mouse-face text property.
2636   (if (string-match
2637        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2638        format)
2639       (gnus-parse-complex-format format spec-alist)
2640     ;; This is a simple format.
2641     (gnus-parse-simple-format format spec-alist insert)))
2642
2643 (defun gnus-parse-complex-format (format spec-alist)
2644   (save-excursion
2645     (gnus-set-work-buffer)
2646     (insert format)
2647     (goto-char (point-min))
2648     (while (re-search-forward "\"" nil t)
2649       (replace-match "\\\"" nil t))
2650     (goto-char (point-min))
2651     (insert "(\"")
2652     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2653       (let ((number (if (match-beginning 1)
2654                         (match-string 1) "0"))
2655             (delim (aref (match-string 2) 0)))
2656         (if (or (= delim ?\() (= delim ?\{))
2657             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2658                                    " " number " \""))
2659           (replace-match "\")\""))))
2660     (goto-char (point-max))
2661     (insert "\")")
2662     (goto-char (point-min))
2663     (let ((form (read (current-buffer))))
2664       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2665
2666 (defun gnus-complex-form-to-spec (form spec-alist)
2667   (delq nil
2668         (mapcar
2669          (lambda (sform)
2670            (if (stringp sform)
2671                (gnus-parse-simple-format sform spec-alist t)
2672              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2673                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2674                       (nth 1 sform))))
2675          form)))
2676
2677 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2678   ;; This function parses the FORMAT string with the help of the
2679   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2680   ;; string.
2681   (let ((max-width 0)
2682         spec flist fstring newspec elem beg result dontinsert)
2683     (save-excursion
2684       (gnus-set-work-buffer)
2685       (insert format)
2686       (goto-char (point-min))
2687       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2688                                 nil t)
2689         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2690               (setq newspec "%"
2691                     beg (1+ (match-beginning 0)))
2692           ;; First check if there are any specs that look anything like
2693           ;; "%12,12A", ie. with a "max width specification".  These have
2694           ;; to be treated specially.
2695           (if (setq beg (match-beginning 1))
2696               (setq max-width
2697                     (string-to-int
2698                      (buffer-substring
2699                       (1+ (match-beginning 1)) (match-end 1))))
2700             (setq max-width 0)
2701             (setq beg (match-beginning 2)))
2702           ;; Find the specification from `spec-alist'.
2703           (unless (setq elem (cdr (assq spec spec-alist)))
2704             (setq elem '("*" ?s)))
2705           ;; Treat user defined format specifiers specially.
2706           (when (eq (car elem) 'gnus-tmp-user-defined)
2707             (setq elem
2708                   (list
2709                    (list (intern (concat "gnus-user-format-function-"
2710                                          (match-string 3)))
2711                          'gnus-tmp-header) ?s))
2712             (delete-region (match-beginning 3) (match-end 3)))
2713           (if (not (zerop max-width))
2714               (let ((el (car elem)))
2715                 (cond ((= (cadr elem) ?c)
2716                        (setq el (list 'char-to-string el)))
2717                       ((= (cadr elem) ?d)
2718                        (setq el (list 'int-to-string el))))
2719                 (setq flist (cons (gnus-max-width-function el max-width)
2720                                   flist))
2721                 (setq newspec ?s))
2722             (progn
2723               (setq flist (cons (car elem) flist))
2724               (setq newspec (cadr elem)))))
2725         ;; Remove the old specification (and possibly a ",12" string).
2726         (delete-region beg (match-end 2))
2727         ;; Insert the new specification.
2728         (goto-char beg)
2729         (insert newspec))
2730       (setq fstring (buffer-substring 1 (point-max))))
2731     ;; Do some postprocessing to increase efficiency.
2732     (setq
2733      result
2734      (cond
2735       ;; Emptyness.
2736       ((string= fstring "")
2737        nil)
2738       ;; Not a format string.
2739       ((not (string-match "%" fstring))
2740        (list fstring))
2741       ;; A format string with just a single string spec.
2742       ((string= fstring "%s")
2743        (list (car flist)))
2744       ;; A single character.
2745       ((string= fstring "%c")
2746        (list (car flist)))
2747       ;; A single number.
2748       ((string= fstring "%d")
2749        (setq dontinsert)
2750        (if insert
2751            (list `(princ ,(car flist)))
2752          (list `(int-to-string ,(car flist)))))
2753       ;; Just lots of chars and strings.
2754       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2755        (nreverse flist))
2756       ;; A single string spec at the beginning of the spec.
2757       ((string-match "\\`%[sc][^%]+\\'" fstring)
2758        (list (car flist) (substring fstring 2)))
2759       ;; A single string spec in the middle of the spec.
2760       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2761        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2762       ;; A single string spec in the end of the spec.
2763       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2764        (list (match-string 1 fstring) (car flist)))
2765       ;; A more complex spec.
2766       (t
2767        (list (cons 'format (cons fstring (nreverse flist)))))))
2768
2769     (if insert
2770         (when result
2771           (if dontinsert
2772               result
2773             (cons 'insert result)))
2774       (cond ((stringp result)
2775              result)
2776             ((consp result)
2777              (cons 'concat result))
2778             (t "")))))
2779
2780 (defun gnus-eval-format (format &optional alist props)
2781   "Eval the format variable FORMAT, using ALIST.
2782 If PROPS, insert the result."
2783   (let ((form (gnus-parse-format format alist props)))
2784     (if props
2785         (add-text-properties (point) (progn (eval form) (point)) props)
2786       (eval form))))
2787
2788 (defun gnus-remove-text-with-property (prop)
2789   "Delete all text in the current buffer with text property PROP."
2790   (save-excursion
2791     (goto-char (point-min))
2792     (while (not (eobp))
2793       (while (get-text-property (point) prop)
2794         (delete-char 1))
2795       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2796
2797 (defun gnus-set-work-buffer ()
2798   (if (get-buffer gnus-work-buffer)
2799       (progn
2800         (set-buffer gnus-work-buffer)
2801         (erase-buffer))
2802     (set-buffer (get-buffer-create gnus-work-buffer))
2803     (kill-all-local-variables)
2804     (buffer-disable-undo (current-buffer))
2805     (gnus-add-current-to-buffer-list)))
2806
2807 ;; Article file names when saving.
2808
2809 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2810   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2811 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2812 Otherwise, it is like ~/News/news/group/num."
2813   (let ((default
2814           (expand-file-name
2815            (concat (if (gnus-use-long-file-name 'not-save)
2816                        (gnus-capitalize-newsgroup newsgroup)
2817                      (gnus-newsgroup-directory-form newsgroup))
2818                    "/" (int-to-string (mail-header-number headers)))
2819            (or gnus-article-save-directory "~/News"))))
2820     (if (and last-file
2821              (string-equal (file-name-directory default)
2822                            (file-name-directory last-file))
2823              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2824         default
2825       (or last-file default))))
2826
2827 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2828   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2829 If variable `gnus-use-long-file-name' is non-nil, it is
2830 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2831   (let ((default
2832           (expand-file-name
2833            (concat (if (gnus-use-long-file-name 'not-save)
2834                        newsgroup
2835                      (gnus-newsgroup-directory-form newsgroup))
2836                    "/" (int-to-string (mail-header-number headers)))
2837            (or gnus-article-save-directory "~/News"))))
2838     (if (and last-file
2839              (string-equal (file-name-directory default)
2840                            (file-name-directory last-file))
2841              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2842         default
2843       (or last-file default))))
2844
2845 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2846   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2847 If variable `gnus-use-long-file-name' is non-nil, it is
2848 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2849   (or last-file
2850       (expand-file-name
2851        (if (gnus-use-long-file-name 'not-save)
2852            (gnus-capitalize-newsgroup newsgroup)
2853          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2854        (or gnus-article-save-directory "~/News"))))
2855
2856 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2857   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2858 If variable `gnus-use-long-file-name' is non-nil, it is
2859 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2860   (or last-file
2861       (expand-file-name
2862        (if (gnus-use-long-file-name 'not-save)
2863            newsgroup
2864          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2865        (or gnus-article-save-directory "~/News"))))
2866
2867 ;; For subscribing new newsgroup
2868
2869 (defun gnus-subscribe-hierarchical-interactive (groups)
2870   (let ((groups (sort groups 'string<))
2871         prefixes prefix start ans group starts)
2872     (while groups
2873       (setq prefixes (list "^"))
2874       (while (and groups prefixes)
2875         (while (not (string-match (car prefixes) (car groups)))
2876           (setq prefixes (cdr prefixes)))
2877         (setq prefix (car prefixes))
2878         (setq start (1- (length prefix)))
2879         (if (and (string-match "[^\\.]\\." (car groups) start)
2880                  (cdr groups)
2881                  (setq prefix
2882                        (concat "^" (substring (car groups) 0 (match-end 0))))
2883                  (string-match prefix (cadr groups)))
2884             (progn
2885               (setq prefixes (cons prefix prefixes))
2886               (message "Descend hierarchy %s? ([y]nsq): "
2887                        (substring prefix 1 (1- (length prefix))))
2888               (setq ans (read-char))
2889               (cond ((= ans ?n)
2890                      (while (and groups
2891                                  (string-match prefix
2892                                                (setq group (car groups))))
2893                        (setq gnus-killed-list
2894                              (cons group gnus-killed-list))
2895                        (gnus-sethash group group gnus-killed-hashtb)
2896                        (setq groups (cdr groups)))
2897                      (setq starts (cdr starts)))
2898                     ((= ans ?s)
2899                      (while (and groups
2900                                  (string-match prefix
2901                                                (setq group (car groups))))
2902                        (gnus-sethash group group gnus-killed-hashtb)
2903                        (gnus-subscribe-alphabetically (car groups))
2904                        (setq groups (cdr groups)))
2905                      (setq starts (cdr starts)))
2906                     ((= ans ?q)
2907                      (while groups
2908                        (setq group (car groups))
2909                        (setq gnus-killed-list (cons group gnus-killed-list))
2910                        (gnus-sethash group group gnus-killed-hashtb)
2911                        (setq groups (cdr groups))))
2912                     (t nil)))
2913           (message "Subscribe %s? ([n]yq)" (car groups))
2914           (setq ans (read-char))
2915           (setq group (car groups))
2916           (cond ((= ans ?y)
2917                  (gnus-subscribe-alphabetically (car groups))
2918                  (gnus-sethash group group gnus-killed-hashtb))
2919                 ((= ans ?q)
2920                  (while groups
2921                    (setq group (car groups))
2922                    (setq gnus-killed-list (cons group gnus-killed-list))
2923                    (gnus-sethash group group gnus-killed-hashtb)
2924                    (setq groups (cdr groups))))
2925                 (t
2926                  (setq gnus-killed-list (cons group gnus-killed-list))
2927                  (gnus-sethash group group gnus-killed-hashtb)))
2928           (setq groups (cdr groups)))))))
2929
2930 (defun gnus-subscribe-randomly (newsgroup)
2931   "Subscribe new NEWSGROUP by making it the first newsgroup."
2932   (gnus-subscribe-newsgroup newsgroup))
2933
2934 (defun gnus-subscribe-alphabetically (newgroup)
2935   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2936   (let ((groups (cdr gnus-newsrc-alist))
2937         before)
2938     (while (and (not before) groups)
2939       (if (string< newgroup (caar groups))
2940           (setq before (caar groups))
2941         (setq groups (cdr groups))))
2942     (gnus-subscribe-newsgroup newgroup before)))
2943
2944 (defun gnus-subscribe-hierarchically (newgroup)
2945   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2946   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2947   (save-excursion
2948     (set-buffer (find-file-noselect gnus-current-startup-file))
2949     (let ((groupkey newgroup)
2950           before)
2951       (while (and (not before) groupkey)
2952         (goto-char (point-min))
2953         (let ((groupkey-re
2954                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2955           (while (and (re-search-forward groupkey-re nil t)
2956                       (progn
2957                         (setq before (match-string 1))
2958                         (string< before newgroup)))))
2959         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2960         (setq groupkey
2961               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2962                   (substring groupkey (match-beginning 1) (match-end 1)))))
2963       (gnus-subscribe-newsgroup newgroup before))))
2964
2965 (defun gnus-subscribe-interactively (group)
2966   "Subscribe the new GROUP interactively.
2967 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2968 it is killed."
2969   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2970       (gnus-subscribe-hierarchically group)
2971     (push group gnus-killed-list)))
2972
2973 (defun gnus-subscribe-zombies (group)
2974   "Make the new GROUP into a zombie group."
2975   (push group gnus-zombie-list))
2976
2977 (defun gnus-subscribe-killed (group)
2978   "Make the new GROUP a killed group."
2979   (push group gnus-killed-list))
2980
2981 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2982   "Subscribe new NEWSGROUP.
2983 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2984 the first newsgroup."
2985   ;; We subscribe the group by changing its level to `subscribed'.
2986   (gnus-group-change-level
2987    newsgroup gnus-level-default-subscribed
2988    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2989   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2990
2991 ;; For directories
2992
2993 (defun gnus-newsgroup-directory-form (newsgroup)
2994   "Make hierarchical directory name from NEWSGROUP name."
2995   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2996         (len (length newsgroup))
2997         idx)
2998     ;; If this is a foreign group, we don't want to translate the
2999     ;; entire name.
3000     (if (setq idx (string-match ":" newsgroup))
3001         (aset newsgroup idx ?/)
3002       (setq idx 0))
3003     ;; Replace all occurrences of `.' with `/'.
3004     (while (< idx len)
3005       (if (= (aref newsgroup idx) ?.)
3006           (aset newsgroup idx ?/))
3007       (setq idx (1+ idx)))
3008     newsgroup))
3009
3010 (defun gnus-newsgroup-savable-name (group)
3011   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3012   ;; with dots.
3013   (nnheader-replace-chars-in-string group ?/ ?.))
3014
3015 (defun gnus-make-directory (dir)
3016   "Make DIRECTORY recursively."
3017   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3018   ;; of the many mysteries of the universe.
3019   (let* ((dir (expand-file-name dir default-directory))
3020          dirs err)
3021     (if (string-match "/$" dir)
3022         (setq dir (substring dir 0 (match-beginning 0))))
3023     ;; First go down the path until we find a directory that exists.
3024     (while (not (file-exists-p dir))
3025       (setq dirs (cons dir dirs))
3026       (string-match "/[^/]+$" dir)
3027       (setq dir (substring dir 0 (match-beginning 0))))
3028     ;; Then create all the subdirs.
3029     (while (and dirs (not err))
3030       (condition-case ()
3031           (make-directory (car dirs))
3032         (error (setq err t)))
3033       (setq dirs (cdr dirs)))
3034     ;; We return whether we were successful or not.
3035     (not dirs)))
3036
3037 (defun gnus-capitalize-newsgroup (newsgroup)
3038   "Capitalize NEWSGROUP name."
3039   (and (not (zerop (length newsgroup)))
3040        (concat (char-to-string (upcase (aref newsgroup 0)))
3041                (substring newsgroup 1))))
3042
3043 ;; Various... things.
3044
3045 (defun gnus-simplify-subject (subject &optional re-only)
3046   "Remove `Re:' and words in parentheses.
3047 If RE-ONLY is non-nil, strip leading `Re:'s only."
3048   (let ((case-fold-search t))           ;Ignore case.
3049     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3050     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3051       (setq subject (substring subject (match-end 0))))
3052     ;; Remove uninteresting prefixes.
3053     (if (and (not re-only)
3054              gnus-simplify-ignored-prefixes
3055              (string-match gnus-simplify-ignored-prefixes subject))
3056         (setq subject (substring subject (match-end 0))))
3057     ;; Remove words in parentheses from end.
3058     (unless re-only
3059       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3060         (setq subject (substring subject 0 (match-beginning 0)))))
3061     ;; Return subject string.
3062     subject))
3063
3064 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3065 ;; all whitespace.
3066 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3067 (defun gnus-simplify-buffer-fuzzy ()
3068   (goto-char (point-min))
3069   (while (search-forward "\t" nil t)
3070     (replace-match " " t t))
3071   (goto-char (point-min))
3072   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3073   (goto-char (match-beginning 0))
3074   (while (or
3075           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3076           (looking-at "^[[].*: .*[]]$"))
3077     (goto-char (point-min))
3078     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3079                               nil t)
3080       (replace-match "" t t))
3081     (goto-char (point-min))
3082     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3083       (goto-char (match-end 0))
3084       (delete-char -1)
3085       (delete-region
3086        (progn (goto-char (match-beginning 0)))
3087        (re-search-forward ":"))))
3088   (goto-char (point-min))
3089   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3090     (replace-match "" t t))
3091   (goto-char (point-min))
3092   (while (re-search-forward "  +" nil t)
3093     (replace-match " " t t))
3094   (goto-char (point-min))
3095   (while (re-search-forward " $" nil t)
3096     (replace-match "" t t))
3097   (goto-char (point-min))
3098   (while (re-search-forward "^ +" nil t)
3099     (replace-match "" t t))
3100   (goto-char (point-min))
3101   (when gnus-simplify-subject-fuzzy-regexp
3102     (if (listp gnus-simplify-subject-fuzzy-regexp)
3103         (let ((list gnus-simplify-subject-fuzzy-regexp))
3104           (while list
3105             (goto-char (point-min))
3106             (while (re-search-forward (car list) nil t)
3107               (replace-match "" t t))
3108             (setq list (cdr list))))
3109       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3110         (replace-match "" t t)))))
3111
3112 (defun gnus-simplify-subject-fuzzy (subject)
3113   "Siplify a subject string fuzzily."
3114   (save-excursion
3115     (gnus-set-work-buffer)
3116     (let ((case-fold-search t))
3117       (insert subject)
3118       (inline (gnus-simplify-buffer-fuzzy))
3119       (buffer-string))))
3120
3121 ;; Add the current buffer to the list of buffers to be killed on exit.
3122 (defun gnus-add-current-to-buffer-list ()
3123   (or (memq (current-buffer) gnus-buffer-list)
3124       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3125
3126 (defun gnus-string> (s1 s2)
3127   (not (or (string< s1 s2)
3128            (string= s1 s2))))
3129
3130 (defun gnus-read-active-file-p ()
3131   "Say whether the active file has been read from `gnus-select-method'."
3132   (memq gnus-select-method gnus-have-read-active-file))
3133
3134 ;;; General various misc type functions.
3135
3136 (defun gnus-clear-system ()
3137   "Clear all variables and buffers."
3138   ;; Clear Gnus variables.
3139   (let ((variables gnus-variable-list))
3140     (while variables
3141       (set (car variables) nil)
3142       (setq variables (cdr variables))))
3143   ;; Clear other internal variables.
3144   (setq gnus-list-of-killed-groups nil
3145         gnus-have-read-active-file nil
3146         gnus-newsrc-alist nil
3147         gnus-newsrc-hashtb nil
3148         gnus-killed-list nil
3149         gnus-zombie-list nil
3150         gnus-killed-hashtb nil
3151         gnus-active-hashtb nil
3152         gnus-moderated-list nil
3153         gnus-description-hashtb nil
3154         gnus-current-headers nil
3155         gnus-thread-indent-array nil
3156         gnus-newsgroup-headers nil
3157         gnus-newsgroup-name nil
3158         gnus-server-alist nil
3159         gnus-group-list-mode nil
3160         gnus-opened-servers nil
3161         gnus-current-select-method nil)
3162   (gnus-shutdown 'gnus)
3163   ;; Kill the startup file.
3164   (and gnus-current-startup-file
3165        (get-file-buffer gnus-current-startup-file)
3166        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3167   ;; Clear the dribble buffer.
3168   (gnus-dribble-clear)
3169   ;; Kill global KILL file buffer.
3170   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3171     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3172   (gnus-kill-buffer nntp-server-buffer)
3173   ;; Kill Gnus buffers.
3174   (while gnus-buffer-list
3175     (gnus-kill-buffer (pop gnus-buffer-list)))
3176   ;; Remove Gnus frames.
3177   (while gnus-created-frames
3178     (when (frame-live-p (car gnus-created-frames))
3179       ;; We slap a condition-case around this `delete-frame' to ensure 
3180       ;; agains errors if we try do delete the single frame that's left.
3181       (condition-case ()
3182           (delete-frame (car gnus-created-frames))
3183         (error nil)))
3184     (pop gnus-created-frames)))
3185
3186 (defun gnus-windows-old-to-new (setting)
3187   ;; First we take care of the really, really old Gnus 3 actions.
3188   (when (symbolp setting)
3189     (setq setting
3190           ;; Take care of ooold GNUS 3.x values.
3191           (cond ((eq setting 'SelectArticle) 'article)
3192                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3193                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3194                 (t setting))))
3195   (if (or (listp setting)
3196           (not (and gnus-window-configuration
3197                     (memq setting '(group summary article)))))
3198       setting
3199     (let* ((setting (if (eq setting 'group)
3200                         (if (assq 'newsgroup gnus-window-configuration)
3201                             'newsgroup
3202                           'newsgroups) setting))
3203            (elem (cadr (assq setting gnus-window-configuration)))
3204            (total (apply '+ elem))
3205            (types '(group summary article))
3206            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3207            (i 0)
3208            perc
3209            out)
3210       (while (< i 3)
3211         (or (not (numberp (nth i elem)))
3212             (zerop (nth i elem))
3213             (progn
3214               (setq perc  (/ (float (nth 0 elem)) total))
3215               (setq out (cons (if (eq pbuf (nth i types))
3216                                   (vector (nth i types) perc 'point)
3217                                 (vector (nth i types) perc))
3218                               out))))
3219         (setq i (1+ i)))
3220       (list (nreverse out)))))
3221
3222 ;;;###autoload
3223 (defun gnus-add-configuration (conf)
3224   "Add the window configuration CONF to `gnus-buffer-configuration'."
3225   (setq gnus-buffer-configuration
3226         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3227                          gnus-buffer-configuration))))
3228
3229 (defvar gnus-frame-list nil)
3230
3231 (defun gnus-configure-frame (split &optional window)
3232   "Split WINDOW according to SPLIT."
3233   (unless window
3234     (setq window (get-buffer-window (current-buffer))))
3235   (select-window window)
3236   ;; This might be an old-stylee buffer config.
3237   (when (vectorp split)
3238     (setq split (append split nil)))
3239   (when (or (consp (car split))
3240             (vectorp (car split)))
3241     (push 1.0 split)
3242     (push 'vertical split))
3243   ;; The SPLIT might be something that is to be evaled to
3244   ;; return a new SPLIT.
3245   (while (and (not (assq (car split) gnus-window-to-buffer))
3246               (gnus-functionp (car split)))
3247     (setq split (eval split)))
3248   (let* ((type (car split))
3249          (subs (cddr split))
3250          (len (if (eq type 'horizontal) (window-width) (window-height)))
3251          (total 0)
3252          (window-min-width (or gnus-window-min-width window-min-width))
3253          (window-min-height (or gnus-window-min-height window-min-height))
3254          s result new-win rest comp-subs size sub)
3255     (cond
3256      ;; Nothing to do here.
3257      ((null split))
3258      ;; Don't switch buffers.
3259      ((null type)
3260       (and (memq 'point split) window))
3261      ;; This is a buffer to be selected.
3262      ((not (memq type '(frame horizontal vertical)))
3263       (let ((buffer (cond ((stringp type) type)
3264                           (t (cdr (assq type gnus-window-to-buffer)))))
3265             buf)
3266         (unless buffer
3267           (error "Illegal buffer type: %s" type))
3268         (unless (setq buf (get-buffer (if (symbolp buffer)
3269                                           (symbol-value buffer) buffer)))
3270           (setq buf (get-buffer-create (if (symbolp buffer)
3271                                            (symbol-value buffer) buffer))))
3272         (switch-to-buffer buf)
3273         ;; We return the window if it has the `point' spec.
3274         (and (memq 'point split) window)))
3275      ;; This is a frame split.
3276      ((eq type 'frame)
3277       (unless gnus-frame-list
3278         (setq gnus-frame-list (list (window-frame
3279                                      (get-buffer-window (current-buffer))))))
3280       (let ((i 0)
3281             params frame fresult)
3282         (while (< i (length subs))
3283           ;; Frame parameter is gotten from the sub-split.
3284           (setq params (cadr (elt subs i)))
3285           ;; It should be a list.
3286           (unless (listp params)
3287             (setq params nil))
3288           ;; Create a new frame?
3289           (unless (setq frame (elt gnus-frame-list i))
3290             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3291             (push frame gnus-created-frames))
3292           ;; Is the old frame still alive?
3293           (unless (frame-live-p frame)
3294             (setcar (nthcdr i gnus-frame-list)
3295                     (setq frame (make-frame params))))
3296           ;; Select the frame in question and do more splits there.
3297           (select-frame frame)
3298           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3299           (incf i))
3300         ;; Select the frame that has the selected buffer.
3301         (when fresult
3302           (select-frame (window-frame fresult)))))
3303      ;; This is a normal split.
3304      (t
3305       (when (> (length subs) 0)
3306         ;; First we have to compute the sizes of all new windows.
3307         (while subs
3308           (setq sub (append (pop subs) nil))
3309           (while (and (not (assq (car sub) gnus-window-to-buffer))
3310                       (gnus-functionp (car sub)))
3311             (setq sub (eval sub)))
3312           (when sub
3313             (push sub comp-subs)
3314             (setq size (cadar comp-subs))
3315             (cond ((equal size 1.0)
3316                    (setq rest (car comp-subs))
3317                    (setq s 0))
3318                   ((floatp size)
3319                    (setq s (floor (* size len))))
3320                   ((integerp size)
3321                    (setq s size))
3322                   (t
3323                    (error "Illegal size: %s" size)))
3324             ;; Try to make sure that we are inside the safe limits.
3325             (cond ((zerop s))
3326                   ((eq type 'horizontal)
3327                    (setq s (max s window-min-width)))
3328                   ((eq type 'vertical)
3329                    (setq s (max s window-min-height))))
3330             (setcar (cdar comp-subs) s)
3331             (incf total s)))
3332         ;; Take care of the "1.0" spec.
3333         (if rest
3334             (setcar (cdr rest) (- len total))
3335           (error "No 1.0 specs in %s" split))
3336         ;; The we do the actual splitting in a nice recursive
3337         ;; fashion.
3338         (setq comp-subs (nreverse comp-subs))
3339         (while comp-subs
3340           (if (null (cdr comp-subs))
3341               (setq new-win window)
3342             (setq new-win
3343                   (split-window window (cadar comp-subs)
3344                                 (eq type 'horizontal))))
3345           (setq result (or (gnus-configure-frame
3346                             (car comp-subs) window) result))
3347           (select-window new-win)
3348           (setq window new-win)
3349           (setq comp-subs (cdr comp-subs))))
3350       ;; Return the proper window, if any.
3351       (when result
3352         (select-window result))))))
3353
3354 (defvar gnus-frame-split-p nil)
3355
3356 (defun gnus-configure-windows (setting &optional force)
3357   (setq setting (gnus-windows-old-to-new setting))
3358   (let ((split (if (symbolp setting)
3359                    (cadr (assq setting gnus-buffer-configuration))
3360                  setting))
3361         all-visible)
3362
3363     (setq gnus-frame-split-p nil)
3364
3365     (unless split
3366       (error "No such setting: %s" setting))
3367
3368     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3369              (not force))
3370         ;; All the windows mentioned are already visible, so we just
3371         ;; put point in the assigned buffer, and do not touch the
3372         ;; winconf.
3373         (select-window all-visible)
3374
3375       ;; Either remove all windows or just remove all Gnus windows.
3376       (let ((frame (selected-frame)))
3377         (unwind-protect
3378             (if gnus-use-full-window
3379                 ;; We want to remove all other windows.
3380                 (if (not gnus-frame-split-p)
3381                     ;; This is not a `frame' split, so we ignore the
3382                     ;; other frames.  
3383                     (delete-other-windows)
3384                   ;; This is a `frame' split, so we delete all windows
3385                   ;; on all frames.
3386                   (mapcar 
3387                    (lambda (frame)
3388                      (unless (eq (cdr (assq 'minibuffer
3389                                             (frame-parameters frame)))
3390                                  'only)
3391                        (select-frame frame)
3392                        (delete-other-windows)))
3393                    (frame-list)))
3394               ;; Just remove some windows.
3395               (gnus-remove-some-windows)
3396               (switch-to-buffer nntp-server-buffer))
3397           (select-frame frame)))
3398
3399       (switch-to-buffer nntp-server-buffer)
3400       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3401
3402 (defun gnus-all-windows-visible-p (split)
3403   "Say whether all buffers in SPLIT are currently visible.
3404 In particular, the value returned will be the window that
3405 should have point."
3406   (let ((stack (list split))
3407         (all-visible t)
3408         type buffer win buf)
3409     (while (and (setq split (pop stack))
3410                 all-visible)
3411       ;; Be backwards compatible.
3412       (when (vectorp split)
3413         (setq split (append split nil)))
3414       (when (or (consp (car split))
3415                 (vectorp (car split)))
3416         (push 1.0 split)
3417         (push 'vertical split))
3418       ;; The SPLIT might be something that is to be evaled to
3419       ;; return a new SPLIT.
3420       (while (and (not (assq (car split) gnus-window-to-buffer))
3421                   (gnus-functionp (car split)))
3422         (setq split (eval split)))
3423
3424       (setq type (elt split 0))
3425       (cond
3426        ;; Nothing here.
3427        ((null split) t)
3428        ;; A buffer.
3429        ((not (memq type '(horizontal vertical frame)))
3430         (setq buffer (cond ((stringp type) type)
3431                            (t (cdr (assq type gnus-window-to-buffer)))))
3432         (unless buffer
3433           (error "Illegal buffer type: %s" type))
3434         (when (setq buf (get-buffer (if (symbolp buffer)
3435                                         (symbol-value buffer)
3436                                       buffer)))
3437           (setq win (get-buffer-window buf t)))
3438         (if win
3439             (when (memq 'point split)
3440                 (setq all-visible win))
3441           (setq all-visible nil)))
3442        (t
3443         (when (eq type 'frame)
3444           (setq gnus-frame-split-p t))
3445         (setq stack (append (cddr split) stack)))))
3446     (unless (eq all-visible t)
3447       all-visible)))
3448
3449 (defun gnus-window-top-edge (&optional window)
3450   (nth 1 (window-edges window)))
3451
3452 (defun gnus-remove-some-windows ()
3453   (let ((buffers gnus-window-to-buffer)
3454         buf bufs lowest-buf lowest)
3455     (save-excursion
3456       ;; Remove windows on all known Gnus buffers.
3457       (while buffers
3458         (setq buf (cdar buffers))
3459         (if (symbolp buf)
3460             (setq buf (and (boundp buf) (symbol-value buf))))
3461         (and buf
3462              (get-buffer-window buf)
3463              (progn
3464                (setq bufs (cons buf bufs))
3465                (pop-to-buffer buf)
3466                (if (or (not lowest)
3467                        (< (gnus-window-top-edge) lowest))
3468                    (progn
3469                      (setq lowest (gnus-window-top-edge))
3470                      (setq lowest-buf buf)))))
3471         (setq buffers (cdr buffers)))
3472       ;; Remove windows on *all* summary buffers.
3473       (walk-windows
3474        (lambda (win)
3475          (let ((buf (window-buffer win)))
3476            (if (string-match    "^\\*Summary" (buffer-name buf))
3477                (progn
3478                  (setq bufs (cons buf bufs))
3479                  (pop-to-buffer buf)
3480                  (if (or (not lowest)
3481                          (< (gnus-window-top-edge) lowest))
3482                      (progn
3483                        (setq lowest-buf buf)
3484                        (setq lowest (gnus-window-top-edge)))))))))
3485       (and lowest-buf
3486            (progn
3487              (pop-to-buffer lowest-buf)
3488              (switch-to-buffer nntp-server-buffer)))
3489       (while bufs
3490         (and (not (eq (car bufs) lowest-buf))
3491              (delete-windows-on (car bufs)))
3492         (setq bufs (cdr bufs))))))
3493
3494 (defun gnus-version ()
3495   "Version numbers of this version of Gnus."
3496   (interactive)
3497   (let ((methods gnus-valid-select-methods)
3498         (mess gnus-version)
3499         meth)
3500     ;; Go through all the legal select methods and add their version
3501     ;; numbers to the total version string.  Only the backends that are
3502     ;; currently in use will have their message numbers taken into
3503     ;; consideration.
3504     (while methods
3505       (setq meth (intern (concat (caar methods) "-version")))
3506       (and (boundp meth)
3507            (stringp (symbol-value meth))
3508            (setq mess (concat mess "; " (symbol-value meth))))
3509       (setq methods (cdr methods)))
3510     (gnus-message 2 mess)))
3511
3512 (defun gnus-info-find-node ()
3513   "Find Info documentation of Gnus."
3514   (interactive)
3515   ;; Enlarge info window if needed.
3516   (let ((mode major-mode)
3517         gnus-info-buffer)
3518     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3519     (setq gnus-info-buffer (current-buffer))
3520     (gnus-configure-windows 'info)))
3521
3522 (defun gnus-days-between (date1 date2)
3523   ;; Return the number of days between date1 and date2.
3524   (- (gnus-day-number date1) (gnus-day-number date2)))
3525
3526 (defun gnus-day-number (date)
3527   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3528                      (timezone-parse-date date))))
3529     (timezone-absolute-from-gregorian
3530      (nth 1 dat) (nth 2 dat) (car dat))))
3531
3532 (defun gnus-encode-date (date)
3533   "Convert DATE to internal time."
3534   (let* ((parse (timezone-parse-date date))
3535          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3536          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3537     (encode-time (caddr time) (cadr time) (car time)
3538                  (caddr date) (cadr date) (car date) (nth 4 date))))
3539
3540 (defun gnus-time-minus (t1 t2)
3541   "Subtract two internal times."
3542   (let ((borrow (< (cadr t1) (cadr t2))))
3543     (list (- (car t1) (car t2) (if borrow 1 0))
3544           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3545
3546 (defun gnus-file-newer-than (file date)
3547   (let ((fdate (nth 5 (file-attributes file))))
3548     (or (> (car fdate) (car date))
3549         (and (= (car fdate) (car date))
3550              (> (nth 1 fdate) (nth 1 date))))))
3551
3552 (defmacro gnus-local-set-keys (&rest plist)
3553   "Set the keys in PLIST in the current keymap."
3554   `(gnus-define-keys-1 (current-local-map) ',plist))
3555
3556 (defmacro gnus-define-keys (keymap &rest plist)
3557   "Define all keys in PLIST in KEYMAP."
3558   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3559
3560 (put 'gnus-define-keys 'lisp-indent-function 1)
3561 (put 'gnus-define-keys 'lisp-indent-hook 1)
3562 (put 'gnus-define-keymap 'lisp-indent-function 1)
3563 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3564
3565 (defmacro gnus-define-keymap (keymap &rest plist)
3566   "Define all keys in PLIST in KEYMAP."
3567   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3568
3569 (defun gnus-define-keys-1 (keymap plist)
3570   (when (null keymap)
3571     (error "Can't set keys in a null keymap"))
3572   (cond ((symbolp keymap)
3573          (setq keymap (symbol-value keymap)))
3574         ((keymapp keymap))
3575         ((listp keymap)
3576          (set (car keymap) nil)
3577          (define-prefix-command (car keymap))
3578          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3579          (setq keymap (symbol-value (car keymap)))))
3580   (let (key)
3581     (while plist
3582       (when (symbolp (setq key (pop plist)))
3583         (setq key (symbol-value key)))
3584       (define-key keymap key (pop plist)))))
3585
3586 (defun gnus-group-read-only-p (&optional group)
3587   "Check whether GROUP supports editing or not.
3588 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3589 that that variable is buffer-local to the summary buffers."
3590   (let ((group (or group gnus-newsgroup-name)))
3591     (not (gnus-check-backend-function 'request-replace-article group))))
3592
3593 (defun gnus-group-total-expirable-p (group)
3594   "Check whether GROUP is total-expirable or not."
3595   (let ((params (gnus-info-params (gnus-get-info group))))
3596     (or (memq 'total-expire params)
3597         (cdr (assq 'total-expire params)) ; (total-expire . t)
3598         (and gnus-total-expirable-newsgroups ; Check var.
3599              (string-match gnus-total-expirable-newsgroups group)))))
3600
3601 (defun gnus-group-auto-expirable-p (group)
3602   "Check whether GROUP is total-expirable or not."
3603   (let ((params (gnus-info-params (gnus-get-info group))))
3604     (or (memq 'auto-expire params)
3605         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3606         (and gnus-auto-expirable-newsgroups ; Check var.
3607              (string-match gnus-auto-expirable-newsgroups group)))))
3608
3609 (defun gnus-virtual-group-p (group)
3610   "Say whether GROUP is virtual or not."
3611   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3612                         gnus-valid-select-methods)))
3613
3614 (defun gnus-news-group-p (group &optional article)
3615   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3616   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3617       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3618            (eq (gnus-request-type group article) 'news))))
3619
3620 (defsubst gnus-simplify-subject-fully (subject)
3621   "Simplify a subject string according to the user's wishes."
3622   (cond
3623    ((null gnus-summary-gather-subject-limit)
3624     (gnus-simplify-subject-re subject))
3625    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3626     (gnus-simplify-subject-fuzzy subject))
3627    ((numberp gnus-summary-gather-subject-limit)
3628     (gnus-limit-string (gnus-simplify-subject-re subject)
3629                        gnus-summary-gather-subject-limit))
3630    (t
3631     subject)))
3632
3633 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3634   "Check whether two subjects are equal.  If optional argument
3635 simple-first is t, first argument is already simplified."
3636   (cond
3637    ((null simple-first)
3638     (equal (gnus-simplify-subject-fully s1)
3639            (gnus-simplify-subject-fully s2)))
3640    (t
3641     (equal s1
3642            (gnus-simplify-subject-fully s2)))))
3643
3644 ;; Returns a list of writable groups.
3645 (defun gnus-writable-groups ()
3646   (let ((alist gnus-newsrc-alist)
3647         groups group)
3648     (while (setq group (car (pop alist)))
3649       (unless (gnus-group-read-only-p group)
3650         (push group groups)))
3651     (nreverse groups)))
3652
3653 (defun gnus-completing-read (default prompt &rest args)
3654   ;; Like `completing-read', except that DEFAULT is the default argument.
3655   (let* ((prompt (if default 
3656                      (concat prompt " (default " default ") ")
3657                    (concat prompt " ")))
3658          (answer (apply 'completing-read prompt args)))
3659     (if (or (null answer) (zerop (length answer)))
3660         default
3661       answer)))
3662
3663 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3664 ;; the echo area.
3665 (defun gnus-y-or-n-p (prompt)
3666   (prog1
3667       (y-or-n-p prompt)
3668     (message "")))
3669
3670 (defun gnus-yes-or-no-p (prompt)
3671   (prog1
3672       (yes-or-no-p prompt)
3673     (message "")))
3674
3675 ;; Check whether to use long file names.
3676 (defun gnus-use-long-file-name (symbol)
3677   ;; The variable has to be set...
3678   (and gnus-use-long-file-name
3679        ;; If it isn't a list, then we return t.
3680        (or (not (listp gnus-use-long-file-name))
3681            ;; If it is a list, and the list contains `symbol', we
3682            ;; return nil.
3683            (not (memq symbol gnus-use-long-file-name)))))
3684
3685 ;; I suspect there's a better way, but I haven't taken the time to do
3686 ;; it yet. -erik selberg@cs.washington.edu
3687 (defun gnus-dd-mmm (messy-date)
3688   "Return a string like DD-MMM from a big messy string"
3689   (let ((datevec (timezone-parse-date messy-date)))
3690     (format "%2s-%s"
3691             (condition-case ()
3692                 ;; Make sure leading zeroes are stripped.
3693                 (number-to-string (string-to-number (aref datevec 2)))
3694               (error "??"))
3695             (capitalize
3696              (or (car
3697                   (nth (1- (string-to-number (aref datevec 1)))
3698                        timezone-months-assoc))
3699                  "???")))))
3700
3701 ;; Make a hash table (default and minimum size is 255).
3702 ;; Optional argument HASHSIZE specifies the table size.
3703 (defun gnus-make-hashtable (&optional hashsize)
3704   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3705
3706 ;; Make a number that is suitable for hashing; bigger than MIN and one
3707 ;; less than 2^x.
3708 (defun gnus-create-hash-size (min)
3709   (let ((i 1))
3710     (while (< i min)
3711       (setq i (* 2 i)))
3712     (1- i)))
3713
3714 ;; Show message if message has a lower level than `gnus-verbose'.
3715 ;; Guideline for numbers:
3716 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3717 ;; for things that take a long time, 7 - not very important messages
3718 ;; on stuff, 9 - messages inside loops.
3719 (defun gnus-message (level &rest args)
3720   (if (<= level gnus-verbose)
3721       (apply 'message args)
3722     ;; We have to do this format thingy here even if the result isn't
3723     ;; shown - the return value has to be the same as the return value
3724     ;; from `message'.
3725     (apply 'format args)))
3726
3727 (defun gnus-error (level &rest args)
3728   "Beep an error if `gnus-verbose' is on LEVEL or less."
3729   (when (<= (floor level) gnus-verbose)
3730     (apply 'message args)
3731     (ding)
3732     (let (duration)
3733       (when (and (floatp level)
3734                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3735         (sit-for duration))))
3736   nil)
3737
3738 ;; Generate a unique new group name.
3739 (defun gnus-generate-new-group-name (leaf)
3740   (let ((name leaf)
3741         (num 0))
3742     (while (gnus-gethash name gnus-newsrc-hashtb)
3743       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3744     name))
3745
3746 (defsubst gnus-hide-text (b e props)
3747   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3748   (add-text-properties b e props)
3749   (when (memq 'intangible props)
3750     (put-text-property (max (1- b) (point-min))
3751                        b 'intangible (cddr (memq 'intangible props)))))
3752
3753 (defsubst gnus-unhide-text (b e)
3754   "Remove hidden text properties from region between B and E."
3755   (remove-text-properties b e gnus-hidden-properties)
3756   (when (memq 'intangible gnus-hidden-properties)
3757     (put-text-property (max (1- b) (point-min))
3758                        b 'intangible nil)))
3759
3760 (defun gnus-hide-text-type (b e type)
3761   "Hide text of TYPE between B and E."
3762   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3763
3764 ;; Find out whether the gnus-visual TYPE is wanted.
3765 (defun gnus-visual-p (&optional type class)
3766   (and gnus-visual                      ; Has to be non-nil, at least.
3767        (if (not type)                   ; We don't care about type.
3768            gnus-visual
3769          (if (listp gnus-visual)        ; It's a list, so we check it.
3770              (or (memq type gnus-visual)
3771                  (memq class gnus-visual))
3772            t))))
3773
3774 (defun gnus-parent-id (references)
3775   "Return the last Message-ID in REFERENCES."
3776   (when (and references
3777              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3778     (substring references (match-beginning 1) (match-end 1))))
3779
3780 (defun gnus-split-references (references)
3781   "Return a list of Message-IDs in REFERENCES."
3782   (let ((beg 0)
3783         ids)
3784     (while (string-match "<[^>]+>" references beg)
3785       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3786             ids))
3787     (nreverse ids)))
3788
3789 (defun gnus-buffer-live-p (buffer)
3790   "Say whether BUFFER is alive or not."
3791   (and buffer
3792        (get-buffer buffer)
3793        (buffer-name (get-buffer buffer))))
3794
3795 (defun gnus-ephemeral-group-p (group)
3796   "Say whether GROUP is ephemeral or not."
3797   (gnus-group-get-parameter group 'quit-config))
3798
3799 (defun gnus-group-quit-config (group)
3800   "Return the quit-config of GROUP."
3801   (gnus-group-get-parameter group 'quit-config))
3802
3803 (defun gnus-simplify-mode-line ()
3804   "Make mode lines a bit simpler."
3805   (setq mode-line-modified "-- ")
3806   (when (listp mode-line-format)
3807     (make-local-variable 'mode-line-format)
3808     (setq mode-line-format (copy-sequence mode-line-format))
3809     (when (equal (nth 3 mode-line-format) "   ")
3810       (setcar (nthcdr 3 mode-line-format) " "))))
3811
3812 ;;; List and range functions
3813
3814 (defun gnus-last-element (list)
3815   "Return last element of LIST."
3816   (while (cdr list)
3817     (setq list (cdr list)))
3818   (car list))
3819
3820 (defun gnus-copy-sequence (list)
3821   "Do a complete, total copy of a list."
3822   (if (and (consp list) (not (consp (cdr list))))
3823       (cons (car list) (cdr list))
3824     (mapcar (lambda (elem) (if (consp elem)
3825                                (if (consp (cdr elem))
3826                                    (gnus-copy-sequence elem)
3827                                  (cons (car elem) (cdr elem)))
3828                              elem))
3829             list)))
3830
3831 (defun gnus-set-difference (list1 list2)
3832   "Return a list of elements of LIST1 that do not appear in LIST2."
3833   (let ((list1 (copy-sequence list1)))
3834     (while list2
3835       (setq list1 (delq (car list2) list1))
3836       (setq list2 (cdr list2)))
3837     list1))
3838
3839 (defun gnus-sorted-complement (list1 list2)
3840   "Return a list of elements of LIST1 that do not appear in LIST2.
3841 Both lists have to be sorted over <."
3842   (let (out)
3843     (if (or (null list1) (null list2))
3844         (or list1 list2)
3845       (while (and list1 list2)
3846         (cond ((= (car list1) (car list2))
3847                (setq list1 (cdr list1)
3848                      list2 (cdr list2)))
3849               ((< (car list1) (car list2))
3850                (setq out (cons (car list1) out))
3851                (setq list1 (cdr list1)))
3852               (t
3853                (setq out (cons (car list2) out))
3854                (setq list2 (cdr list2)))))
3855       (nconc (nreverse out) (or list1 list2)))))
3856
3857 (defun gnus-intersection (list1 list2)
3858   (let ((result nil))
3859     (while list2
3860       (if (memq (car list2) list1)
3861           (setq result (cons (car list2) result)))
3862       (setq list2 (cdr list2)))
3863     result))
3864
3865 (defun gnus-sorted-intersection (list1 list2)
3866   ;; LIST1 and LIST2 have to be sorted over <.
3867   (let (out)
3868     (while (and list1 list2)
3869       (cond ((= (car list1) (car list2))
3870              (setq out (cons (car list1) out)
3871                    list1 (cdr list1)
3872                    list2 (cdr list2)))
3873             ((< (car list1) (car list2))
3874              (setq list1 (cdr list1)))
3875             (t
3876              (setq list2 (cdr list2)))))
3877     (nreverse out)))
3878
3879 (defun gnus-set-sorted-intersection (list1 list2)
3880   ;; LIST1 and LIST2 have to be sorted over <.
3881   ;; This function modifies LIST1.
3882   (let* ((top (cons nil list1))
3883          (prev top))
3884     (while (and list1 list2)
3885       (cond ((= (car list1) (car list2))
3886              (setq prev list1
3887                    list1 (cdr list1)
3888                    list2 (cdr list2)))
3889             ((< (car list1) (car list2))
3890              (setcdr prev (cdr list1))
3891              (setq list1 (cdr list1)))
3892             (t
3893              (setq list2 (cdr list2)))))
3894     (setcdr prev nil)
3895     (cdr top)))
3896
3897 (defun gnus-compress-sequence (numbers &optional always-list)
3898   "Convert list of numbers to a list of ranges or a single range.
3899 If ALWAYS-LIST is non-nil, this function will always release a list of
3900 ranges."
3901   (let* ((first (car numbers))
3902          (last (car numbers))
3903          result)
3904     (if (null numbers)
3905         nil
3906       (if (not (listp (cdr numbers)))
3907           numbers
3908         (while numbers
3909           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3910                 ((= (1+ last) (car numbers)) ;Still in sequence
3911                  (setq last (car numbers)))
3912                 (t                      ;End of one sequence
3913                  (setq result
3914                        (cons (if (= first last) first
3915                                (cons first last)) result))
3916                  (setq first (car numbers))
3917                  (setq last  (car numbers))))
3918           (setq numbers (cdr numbers)))
3919         (if (and (not always-list) (null result))
3920             (if (= first last) (list first) (cons first last))
3921           (nreverse (cons (if (= first last) first (cons first last))
3922                           result)))))))
3923
3924 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3925 (defun gnus-uncompress-range (ranges)
3926   "Expand a list of ranges into a list of numbers.
3927 RANGES is either a single range on the form `(num . num)' or a list of
3928 these ranges."
3929   (let (first last result)
3930     (cond
3931      ((null ranges)
3932       nil)
3933      ((not (listp (cdr ranges)))
3934       (setq first (car ranges))
3935       (setq last (cdr ranges))
3936       (while (<= first last)
3937         (setq result (cons first result))
3938         (setq first (1+ first)))
3939       (nreverse result))
3940      (t
3941       (while ranges
3942         (if (atom (car ranges))
3943             (if (numberp (car ranges))
3944                 (setq result (cons (car ranges) result)))
3945           (setq first (caar ranges))
3946           (setq last  (cdar ranges))
3947           (while (<= first last)
3948             (setq result (cons first result))
3949             (setq first (1+ first))))
3950         (setq ranges (cdr ranges)))
3951       (nreverse result)))))
3952
3953 (defun gnus-add-to-range (ranges list)
3954   "Return a list of ranges that has all articles from both RANGES and LIST.
3955 Note: LIST has to be sorted over `<'."
3956   (if (not ranges)
3957       (gnus-compress-sequence list t)
3958     (setq list (copy-sequence list))
3959     (or (listp (cdr ranges))
3960         (setq ranges (list ranges)))
3961     (let ((out ranges)
3962           ilist lowest highest temp)
3963       (while (and ranges list)
3964         (setq ilist list)
3965         (setq lowest (or (and (atom (car ranges)) (car ranges))
3966                          (caar ranges)))
3967         (while (and list (cdr list) (< (cadr list) lowest))
3968           (setq list (cdr list)))
3969         (if (< (car ilist) lowest)
3970             (progn
3971               (setq temp list)
3972               (setq list (cdr list))
3973               (setcdr temp nil)
3974               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3975         (setq highest (or (and (atom (car ranges)) (car ranges))
3976                           (cdar ranges)))
3977         (while (and list (<= (car list) highest))
3978           (setq list (cdr list)))
3979         (setq ranges (cdr ranges)))
3980       (if list
3981           (setq out (nconc (gnus-compress-sequence list t) out)))
3982       (setq out (sort out (lambda (r1 r2)
3983                             (< (or (and (atom r1) r1) (car r1))
3984                                (or (and (atom r2) r2) (car r2))))))
3985       (setq ranges out)
3986       (while ranges
3987         (if (atom (car ranges))
3988             (if (cdr ranges)
3989                 (if (atom (cadr ranges))
3990                     (if (= (1+ (car ranges)) (cadr ranges))
3991                         (progn
3992                           (setcar ranges (cons (car ranges)
3993                                                (cadr ranges)))
3994                           (setcdr ranges (cddr ranges))))
3995                   (if (= (1+ (car ranges)) (caadr ranges))
3996                       (progn
3997                         (setcar (cadr ranges) (car ranges))
3998                         (setcar ranges (cadr ranges))
3999                         (setcdr ranges (cddr ranges))))))
4000           (if (cdr ranges)
4001               (if (atom (cadr ranges))
4002                   (if (= (1+ (cdar ranges)) (cadr ranges))
4003                       (progn
4004                         (setcdr (car ranges) (cadr ranges))
4005                         (setcdr ranges (cddr ranges))))
4006                 (if (= (1+ (cdar ranges)) (caadr ranges))
4007                     (progn
4008                       (setcdr (car ranges) (cdadr ranges))
4009                       (setcdr ranges (cddr ranges)))))))
4010         (setq ranges (cdr ranges)))
4011       out)))
4012
4013 (defun gnus-remove-from-range (ranges list)
4014   "Return a list of ranges that has all articles from LIST removed from RANGES.
4015 Note: LIST has to be sorted over `<'."
4016   ;; !!! This function shouldn't look like this, but I've got a headache.
4017   (gnus-compress-sequence
4018    (gnus-sorted-complement
4019     (gnus-uncompress-range ranges) list)))
4020
4021 (defun gnus-member-of-range (number ranges)
4022   (if (not (listp (cdr ranges)))
4023       (and (>= number (car ranges))
4024            (<= number (cdr ranges)))
4025     (let ((not-stop t))
4026       (while (and ranges
4027                   (if (numberp (car ranges))
4028                       (>= number (car ranges))
4029                     (>= number (caar ranges)))
4030                   not-stop)
4031         (if (if (numberp (car ranges))
4032                 (= number (car ranges))
4033               (and (>= number (caar ranges))
4034                    (<= number (cdar ranges))))
4035             (setq not-stop nil))
4036         (setq ranges (cdr ranges)))
4037       (not not-stop))))
4038
4039 (defun gnus-range-length (range)
4040   "Return the length RANGE would have if uncompressed."
4041   (length (gnus-uncompress-range range)))
4042
4043 (defun gnus-sublist-p (list sublist)
4044   "Test whether all elements in SUBLIST are members of LIST."
4045   (let ((sublistp t))
4046     (while sublist
4047       (unless (memq (pop sublist) list)
4048         (setq sublistp nil
4049               sublist nil)))
4050     sublistp))
4051
4052 \f
4053 ;;;
4054 ;;; Gnus group mode
4055 ;;;
4056
4057 (defvar gnus-group-mode-map nil)
4058 (put 'gnus-group-mode 'mode-class 'special)
4059
4060 (unless gnus-group-mode-map
4061   (setq gnus-group-mode-map (make-keymap))
4062   (suppress-keymap gnus-group-mode-map)
4063
4064   (gnus-define-keys gnus-group-mode-map
4065     " " gnus-group-read-group
4066     "=" gnus-group-select-group
4067     "\r" gnus-group-select-group
4068     "\M-\r" gnus-group-quick-select-group
4069     "j" gnus-group-jump-to-group
4070     "n" gnus-group-next-unread-group
4071     "p" gnus-group-prev-unread-group
4072     "\177" gnus-group-prev-unread-group
4073     [delete] gnus-group-prev-unread-group
4074     "N" gnus-group-next-group
4075     "P" gnus-group-prev-group
4076     "\M-n" gnus-group-next-unread-group-same-level
4077     "\M-p" gnus-group-prev-unread-group-same-level
4078     "," gnus-group-best-unread-group
4079     "." gnus-group-first-unread-group
4080     "u" gnus-group-unsubscribe-current-group
4081     "U" gnus-group-unsubscribe-group
4082     "c" gnus-group-catchup-current
4083     "C" gnus-group-catchup-current-all
4084     "l" gnus-group-list-groups
4085     "L" gnus-group-list-all-groups
4086     "m" gnus-group-mail
4087     "g" gnus-group-get-new-news
4088     "\M-g" gnus-group-get-new-news-this-group
4089     "R" gnus-group-restart
4090     "r" gnus-group-read-init-file
4091     "B" gnus-group-browse-foreign-server
4092     "b" gnus-group-check-bogus-groups
4093     "F" gnus-find-new-newsgroups
4094     "\C-c\C-d" gnus-group-describe-group
4095     "\M-d" gnus-group-describe-all-groups
4096     "\C-c\C-a" gnus-group-apropos
4097     "\C-c\M-\C-a" gnus-group-description-apropos
4098     "a" gnus-group-post-news
4099     "\ek" gnus-group-edit-local-kill
4100     "\eK" gnus-group-edit-global-kill
4101     "\C-k" gnus-group-kill-group
4102     "\C-y" gnus-group-yank-group
4103     "\C-w" gnus-group-kill-region
4104     "\C-x\C-t" gnus-group-transpose-groups
4105     "\C-c\C-l" gnus-group-list-killed
4106     "\C-c\C-x" gnus-group-expire-articles
4107     "\C-c\M-\C-x" gnus-group-expire-all-groups
4108     "V" gnus-version
4109     "s" gnus-group-save-newsrc
4110     "z" gnus-group-suspend
4111     "Z" gnus-group-clear-dribble
4112     "q" gnus-group-exit
4113     "Q" gnus-group-quit
4114     "?" gnus-group-describe-briefly
4115     "\C-c\C-i" gnus-info-find-node
4116     "\M-e" gnus-group-edit-group-method
4117     "^" gnus-group-enter-server-mode
4118     gnus-mouse-2 gnus-mouse-pick-group
4119     "<" beginning-of-buffer
4120     ">" end-of-buffer
4121     "\C-c\C-b" gnus-bug
4122     "\C-c\C-s" gnus-group-sort-groups
4123     "t" gnus-topic-mode
4124     "\C-c\M-g" gnus-activate-all-groups
4125     "\M-&" gnus-group-universal-argument
4126     "#" gnus-group-mark-group
4127     "\M-#" gnus-group-unmark-group)
4128
4129   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4130     "m" gnus-group-mark-group
4131     "u" gnus-group-unmark-group
4132     "w" gnus-group-mark-region
4133     "m" gnus-group-mark-buffer
4134     "r" gnus-group-mark-regexp
4135     "U" gnus-group-unmark-all-groups)
4136
4137   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4138     "d" gnus-group-make-directory-group
4139     "h" gnus-group-make-help-group
4140     "a" gnus-group-make-archive-group
4141     "k" gnus-group-make-kiboze-group
4142     "m" gnus-group-make-group
4143     "E" gnus-group-edit-group
4144     "e" gnus-group-edit-group-method
4145     "p" gnus-group-edit-group-parameters
4146     "v" gnus-group-add-to-virtual
4147     "V" gnus-group-make-empty-virtual
4148     "D" gnus-group-enter-directory
4149     "f" gnus-group-make-doc-group
4150     "r" gnus-group-rename-group
4151     "\177" gnus-group-delete-group
4152     [delete] gnus-group-delete-group)
4153
4154    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4155      "b" gnus-group-brew-soup
4156      "w" gnus-soup-save-areas
4157      "s" gnus-soup-send-replies
4158      "p" gnus-soup-pack-packet
4159      "r" nnsoup-pack-replies)
4160
4161    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4162      "s" gnus-group-sort-groups
4163      "a" gnus-group-sort-groups-by-alphabet
4164      "u" gnus-group-sort-groups-by-unread
4165      "l" gnus-group-sort-groups-by-level
4166      "v" gnus-group-sort-groups-by-score
4167      "r" gnus-group-sort-groups-by-rank
4168      "m" gnus-group-sort-groups-by-method)
4169
4170    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4171      "k" gnus-group-list-killed
4172      "z" gnus-group-list-zombies
4173      "s" gnus-group-list-groups
4174      "u" gnus-group-list-all-groups
4175      "A" gnus-group-list-active
4176      "a" gnus-group-apropos
4177      "d" gnus-group-description-apropos
4178      "m" gnus-group-list-matching
4179      "M" gnus-group-list-all-matching
4180      "l" gnus-group-list-level)
4181
4182    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4183      "f" gnus-score-flush-cache)
4184
4185    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4186      "f" gnus-group-fetch-faq)
4187
4188    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4189      "l" gnus-group-set-current-level
4190      "t" gnus-group-unsubscribe-current-group
4191      "s" gnus-group-unsubscribe-group
4192      "k" gnus-group-kill-group
4193      "y" gnus-group-yank-group
4194      "w" gnus-group-kill-region
4195      "\C-k" gnus-group-kill-level
4196      "z" gnus-group-kill-all-zombies))
4197
4198 (defun gnus-group-mode ()
4199   "Major mode for reading news.
4200
4201 All normal editing commands are switched off.
4202 \\<gnus-group-mode-map>
4203 The group buffer lists (some of) the groups available.  For instance,
4204 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4205 lists all zombie groups.
4206
4207 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4208 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4209
4210 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4211
4212 The following commands are available:
4213
4214 \\{gnus-group-mode-map}"
4215   (interactive)
4216   (when (and menu-bar-mode
4217              (gnus-visual-p 'group-menu 'menu))
4218     (gnus-group-make-menu-bar))
4219   (kill-all-local-variables)
4220   (gnus-simplify-mode-line)
4221   (setq major-mode 'gnus-group-mode)
4222   (setq mode-name "Group")
4223   (gnus-group-set-mode-line)
4224   (setq mode-line-process nil)
4225   (use-local-map gnus-group-mode-map)
4226   (buffer-disable-undo (current-buffer))
4227   (setq truncate-lines t)
4228   (setq buffer-read-only t)
4229   (run-hooks 'gnus-group-mode-hook))
4230
4231 (defun gnus-mouse-pick-group (e)
4232   "Enter the group under the mouse pointer."
4233   (interactive "e")
4234   (mouse-set-point e)
4235   (gnus-group-read-group nil))
4236
4237 ;; Look at LEVEL and find out what the level is really supposed to be.
4238 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4239 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4240 (defun gnus-group-default-level (&optional level number-or-nil)
4241   (cond
4242    (gnus-group-use-permanent-levels
4243     (or (setq gnus-group-use-permanent-levels
4244               (or level (if (numberp gnus-group-use-permanent-levels)
4245                             gnus-group-use-permanent-levels
4246                           (or gnus-group-default-list-level
4247                               gnus-level-subscribed))))
4248         gnus-group-default-list-level gnus-level-subscribed))
4249    (number-or-nil
4250     level)
4251    (t
4252     (or level gnus-group-default-list-level gnus-level-subscribed))))
4253
4254 ;;;###autoload
4255 (defun gnus-slave-no-server (&optional arg)
4256   "Read network news as a slave, without connecting to local server"
4257   (interactive "P")
4258   (gnus-no-server arg t))
4259
4260 ;;;###autoload
4261 (defun gnus-no-server (&optional arg slave)
4262   "Read network news.
4263 If ARG is a positive number, Gnus will use that as the
4264 startup level.  If ARG is nil, Gnus will be started at level 2.
4265 If ARG is non-nil and not a positive number, Gnus will
4266 prompt the user for the name of an NNTP server to use.
4267 As opposed to `gnus', this command will not connect to the local server."
4268   (interactive "P")
4269   (make-local-variable 'gnus-group-use-permanent-levels)
4270   (setq gnus-group-use-permanent-levels
4271         (or arg (1- gnus-level-default-subscribed)))
4272   (gnus gnus-group-use-permanent-levels t slave))
4273
4274 ;;;###autoload
4275 (defun gnus-slave (&optional arg)
4276   "Read news as a slave."
4277   (interactive "P")
4278   (gnus arg nil 'slave))
4279
4280 ;;;###autoload
4281 (defun gnus-other-frame (&optional arg)
4282   "Pop up a frame to read news."
4283   (interactive "P")
4284   (if (get-buffer gnus-group-buffer)
4285       (let ((pop-up-frames t))
4286         (gnus arg))
4287     (select-frame (make-frame))
4288     (gnus arg)))
4289
4290 ;;;###autoload
4291 (defun gnus (&optional arg dont-connect slave)
4292   "Read network news.
4293 If ARG is non-nil and a positive number, Gnus will use that as the
4294 startup level.  If ARG is non-nil and not a positive number, Gnus will
4295 prompt the user for the name of an NNTP server to use."
4296   (interactive "P")
4297
4298   (if (get-buffer gnus-group-buffer)
4299       (progn
4300         (switch-to-buffer gnus-group-buffer)
4301         (gnus-group-get-new-news))
4302
4303     (gnus-clear-system)
4304     (nnheader-init-server-buffer)
4305     (gnus-read-init-file)
4306     (setq gnus-slave slave)
4307
4308     (gnus-group-setup-buffer)
4309     (let ((buffer-read-only nil))
4310       (erase-buffer)
4311       (if (not gnus-inhibit-startup-message)
4312           (progn
4313             (gnus-group-startup-message)
4314             (sit-for 0))))
4315
4316     (let ((level (and (numberp arg) (> arg 0) arg))
4317           did-connect)
4318       (unwind-protect
4319           (progn
4320             (or dont-connect
4321                 (setq did-connect
4322                       (gnus-start-news-server (and arg (not level))))))
4323         (if (and (not dont-connect)
4324                  (not did-connect))
4325             (gnus-group-quit)
4326           (run-hooks 'gnus-startup-hook)
4327           ;; NNTP server is successfully open.
4328
4329           ;; Find the current startup file name.
4330           (setq gnus-current-startup-file
4331                 (gnus-make-newsrc-file gnus-startup-file))
4332
4333           ;; Read the dribble file.
4334           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4335
4336           ;; Allow using GroupLens predictions.
4337           (when gnus-use-grouplens
4338             (bbb-login)
4339             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4340
4341           (gnus-summary-make-display-table)
4342           ;; Do the actual startup.
4343           (gnus-setup-news nil level dont-connect)
4344           ;; Generate the group buffer.
4345           (gnus-group-list-groups level)
4346           (gnus-group-first-unread-group)
4347           (gnus-configure-windows 'group)
4348           (gnus-group-set-mode-line))))))
4349
4350 (defun gnus-unload ()
4351   "Unload all Gnus features."
4352   (interactive)
4353   (or (boundp 'load-history)
4354       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4355   (let ((history load-history)
4356         feature)
4357     (while history
4358       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4359            (setq feature (cdr (assq 'provide (car history))))
4360            (unload-feature feature 'force))
4361       (setq history (cdr history)))))
4362
4363 (defun gnus-compile ()
4364   "Byte-compile the user-defined format specs."
4365   (interactive)
4366   (let ((entries gnus-format-specs)
4367         entry gnus-tmp-func)
4368     (save-excursion
4369       (gnus-message 7 "Compiling format specs...")
4370
4371       (while entries
4372         (setq entry (pop entries))
4373         (if (eq (car entry) 'version)
4374             (setq gnus-format-specs (delq entry gnus-format-specs))
4375           (when (and (listp (caddr entry))
4376                      (not (eq 'byte-code (caaddr entry))))
4377             (fset 'gnus-tmp-func
4378                   `(lambda () ,(caddr entry)))
4379             (byte-compile 'gnus-tmp-func)
4380             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4381
4382       (push (cons 'version emacs-version) gnus-format-specs)
4383
4384       (gnus-message 7 "Compiling user specs...done"))))
4385
4386 (defun gnus-indent-rigidly (start end arg)
4387   "Indent rigidly using only spaces and no tabs."
4388   (save-excursion
4389     (save-restriction
4390       (narrow-to-region start end)
4391       (indent-rigidly start end arg)
4392       (goto-char (point-min))
4393       (while (search-forward "\t" nil t)
4394         (replace-match "        " t t)))))
4395
4396 (defun gnus-group-startup-message (&optional x y)
4397   "Insert startup message in current buffer."
4398   ;; Insert the message.
4399   (erase-buffer)
4400   (insert
4401    (format "              %s
4402           _    ___ _             _
4403           _ ___ __ ___  __    _ ___
4404           __   _     ___    __  ___
4405               _           ___     _
4406              _  _ __             _
4407              ___   __            _
4408                    __           _
4409                     _      _   _
4410                    _      _    _
4411                       _  _    _
4412                   __  ___
4413                  _   _ _     _
4414                 _   _
4415               _    _
4416              _    _
4417             _
4418           __
4419
4420 "
4421            ""))
4422   ;; And then hack it.
4423   (gnus-indent-rigidly (point-min) (point-max)
4424                        (/ (max (- (window-width) (or x 46)) 0) 2))
4425   (goto-char (point-min))
4426   (forward-line 1)
4427   (let* ((pheight (count-lines (point-min) (point-max)))
4428          (wheight (window-height))
4429          (rest (- wheight pheight)))
4430     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4431   ;; Fontify some.
4432   (goto-char (point-min))
4433   (and (search-forward "Praxis" nil t)
4434        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4435   (goto-char (point-min))
4436   (let* ((mode-string (gnus-group-set-mode-line)))
4437     (setq mode-line-buffer-identification
4438           (list (concat gnus-version (substring (car mode-string) 4))))
4439     (set-buffer-modified-p t)))
4440
4441 (defun gnus-group-setup-buffer ()
4442   (or (get-buffer gnus-group-buffer)
4443       (progn
4444         (switch-to-buffer gnus-group-buffer)
4445         (gnus-add-current-to-buffer-list)
4446         (gnus-group-mode)
4447         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4448
4449 (defun gnus-group-list-groups (&optional level unread lowest)
4450   "List newsgroups with level LEVEL or lower that have unread articles.
4451 Default is all subscribed groups.
4452 If argument UNREAD is non-nil, groups with no unread articles are also
4453 listed."
4454   (interactive (list (if current-prefix-arg
4455                          (prefix-numeric-value current-prefix-arg)
4456                        (or
4457                         (gnus-group-default-level nil t)
4458                         gnus-group-default-list-level
4459                         gnus-level-subscribed))))
4460   (or level
4461       (setq level (car gnus-group-list-mode)
4462             unread (cdr gnus-group-list-mode)))
4463   (setq level (gnus-group-default-level level))
4464   (gnus-group-setup-buffer)             ;May call from out of group buffer
4465   (gnus-update-format-specifications)
4466   (let ((case-fold-search nil)
4467         (props (text-properties-at (gnus-point-at-bol)))
4468         (group (gnus-group-group-name)))
4469     (set-buffer gnus-group-buffer)
4470     (funcall gnus-group-prepare-function level unread lowest)
4471     (if (zerop (buffer-size))
4472         (gnus-message 5 gnus-no-groups-message)
4473       (goto-char (point-max))
4474       (when (or (not gnus-group-goto-next-group-function)
4475                 (not (funcall gnus-group-goto-next-group-function 
4476                               group props)))
4477         (if (not group)
4478             ;; Go to the first group with unread articles.
4479             (gnus-group-search-forward t)
4480           ;; Find the right group to put point on.  If the current group
4481           ;; has disappeared in the new listing, try to find the next
4482           ;; one.        If no next one can be found, just leave point at the
4483           ;; first newsgroup in the buffer.
4484           (if (not (gnus-goto-char
4485                     (text-property-any
4486                      (point-min) (point-max)
4487                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4488               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4489                 (while (and newsrc
4490                             (not (gnus-goto-char
4491                                   (text-property-any
4492                                    (point-min) (point-max) 'gnus-group
4493                                    (gnus-intern-safe
4494                                     (caar newsrc) gnus-active-hashtb)))))
4495                   (setq newsrc (cdr newsrc)))
4496                 (or newsrc (progn (goto-char (point-max))
4497                                   (forward-line -1)))))))
4498       ;; Adjust cursor point.
4499       (gnus-group-position-point))))
4500
4501 (defun gnus-group-list-level (level &optional all)
4502   "List groups on LEVEL.
4503 If ALL (the prefix), also list groups that have no unread articles."
4504   (interactive "nList groups on level: \nP")
4505   (gnus-group-list-groups level all level))
4506
4507 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4508   "List all newsgroups with unread articles of level LEVEL or lower.
4509 If ALL is non-nil, list groups that have no unread articles.
4510 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4511 If REGEXP, only list groups matching REGEXP."
4512   (set-buffer gnus-group-buffer)
4513   (let ((buffer-read-only nil)
4514         (newsrc (cdr gnus-newsrc-alist))
4515         (lowest (or lowest 1))
4516         info clevel unread group params)
4517     (erase-buffer)
4518     (if (< lowest gnus-level-zombie)
4519         ;; List living groups.
4520         (while newsrc
4521           (setq info (car newsrc)
4522                 group (gnus-info-group info)
4523                 params (gnus-info-params info)
4524                 newsrc (cdr newsrc)
4525                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4526           (and unread                   ; This group might be bogus
4527                (or (not regexp)
4528                    (string-match regexp group))
4529                (<= (setq clevel (gnus-info-level info)) level)
4530                (>= clevel lowest)
4531                (or all                  ; We list all groups?
4532                    (if (eq unread t)    ; Unactivated?
4533                        gnus-group-list-inactive-groups ; We list unactivated 
4534                      (> unread 0))      ; We list groups with unread articles
4535                    (and gnus-list-groups-with-ticked-articles
4536                         (cdr (assq 'tick (gnus-info-marks info))))
4537                                         ; And groups with tickeds
4538                    ;; Check for permanent visibility.
4539                    (and gnus-permanently-visible-groups
4540                         (string-match gnus-permanently-visible-groups
4541                                       group))
4542                    (memq 'visible params)
4543                    (cdr (assq 'visible params)))
4544                (gnus-group-insert-group-line
4545                 group (gnus-info-level info)
4546                 (gnus-info-marks info) unread (gnus-info-method info)))))
4547
4548     ;; List dead groups.
4549     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4550          (gnus-group-prepare-flat-list-dead
4551           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4552           gnus-level-zombie ?Z
4553           regexp))
4554     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4555          (gnus-group-prepare-flat-list-dead
4556           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4557           gnus-level-killed ?K regexp))
4558
4559     (gnus-group-set-mode-line)
4560     (setq gnus-group-list-mode (cons level all))
4561     (run-hooks 'gnus-group-prepare-hook)))
4562
4563 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4564   ;; List zombies and killed lists somewhat faster, which was
4565   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4566   ;; this by ignoring the group format specification altogether.
4567   (let (group)
4568     (if regexp
4569         ;; This loop is used when listing groups that match some
4570         ;; regexp.
4571         (while groups
4572           (setq group (pop groups))
4573           (when (string-match regexp group)
4574             (add-text-properties
4575              (point) (prog1 (1+ (point))
4576                        (insert " " mark "     *: " group "\n"))
4577              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4578                    'gnus-unread t
4579                    'gnus-level level))))
4580       ;; This loop is used when listing all groups.
4581       (while groups
4582         (add-text-properties
4583          (point) (prog1 (1+ (point))
4584                    (insert " " mark "     *: "
4585                            (setq group (pop groups)) "\n"))
4586          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4587                'gnus-unread t
4588                'gnus-level level))))))
4589
4590 (defmacro gnus-group-real-name (group)
4591   "Find the real name of a foreign newsgroup."
4592   `(let ((gname ,group))
4593      (if (string-match ":[^:]+$" gname)
4594          (substring gname (1+ (match-beginning 0)))
4595        gname)))
4596
4597 (defsubst gnus-server-add-address (method)
4598   (let ((method-name (symbol-name (car method))))
4599     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4600              (not (assq (intern (concat method-name "-address")) method)))
4601         (append method (list (list (intern (concat method-name "-address"))
4602                                    (nth 1 method))))
4603       method)))
4604
4605 (defsubst gnus-server-get-method (group method)
4606   ;; Input either a server name, and extended server name, or a
4607   ;; select method, and return a select method.
4608   (cond ((stringp method)
4609          (gnus-server-to-method method))
4610         ((equal method gnus-select-method)
4611          gnus-select-method)
4612         ((and (stringp (car method)) group)
4613          (gnus-server-extend-method group method))
4614         ((and method (not group)
4615               (equal (cadr method) ""))
4616          method)
4617         (t
4618          (gnus-server-add-address method))))
4619
4620 (defun gnus-server-to-method (server)
4621   "Map virtual server names to select methods."
4622   (or 
4623    ;; Perhaps this is the native server?
4624    (and (equal server "native") gnus-select-method)
4625    ;; It should be in the server alist.
4626    (cdr (assoc server gnus-server-alist))
4627    ;; If not, we look through all the opened server
4628    ;; to see whether we can find it there.
4629    (let ((opened gnus-opened-servers))
4630      (while (and opened
4631                  (not (equal server (format "%s:%s" (caaar opened)
4632                                             (cadaar opened)))))
4633        (pop opened))
4634      (caar opened))))
4635
4636 (defmacro gnus-method-equal (ss1 ss2)
4637   "Say whether two servers are equal."
4638   `(let ((s1 ,ss1)
4639          (s2 ,ss2))
4640      (or (equal s1 s2)
4641          (and (= (length s1) (length s2))
4642               (progn
4643                 (while (and s1 (member (car s1) s2))
4644                   (setq s1 (cdr s1)))
4645                 (null s1))))))
4646
4647 (defun gnus-server-equal (m1 m2)
4648   "Say whether two methods are equal."
4649   (let ((m1 (cond ((null m1) gnus-select-method)
4650                   ((stringp m1) (gnus-server-to-method m1))
4651                   (t m1)))
4652         (m2 (cond ((null m2) gnus-select-method)
4653                   ((stringp m2) (gnus-server-to-method m2))
4654                   (t m2))))
4655     (gnus-method-equal m1 m2)))
4656
4657 (defun gnus-servers-using-backend (backend)
4658   "Return a list of known servers using BACKEND."
4659   (let ((opened gnus-opened-servers)
4660         out)
4661     (while opened
4662       (when (eq backend (caaar opened))
4663         (push (caar opened) out))
4664       (pop opened))
4665     out))
4666
4667 (defun gnus-group-prefixed-name (group method)
4668   "Return the whole name from GROUP and METHOD."
4669   (and (stringp method) (setq method (gnus-server-to-method method)))
4670   (concat (format "%s" (car method))
4671           (if (and
4672                (or (assoc (format "%s" (car method)) 
4673                           (gnus-methods-using 'address))
4674                    (gnus-server-equal method gnus-message-archive-method))
4675                (nth 1 method)
4676                (not (string= (nth 1 method) "")))
4677               (concat "+" (nth 1 method)))
4678           ":" group))
4679
4680 (defun gnus-group-real-prefix (group)
4681   "Return the prefix of the current group name."
4682   (if (string-match "^[^:]+:" group)
4683       (substring group 0 (match-end 0))
4684     ""))
4685
4686 (defun gnus-group-method (group)
4687   "Return the server or method used for selecting GROUP."
4688   (let ((prefix (gnus-group-real-prefix group)))
4689     (if (equal prefix "")
4690         gnus-select-method
4691       (let ((servers gnus-opened-servers)
4692             (server "")
4693             backend possible found)
4694         (if (string-match "^[^\\+]+\\+" prefix)
4695             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4696                   server (substring prefix (match-end 0) (1- (length prefix))))
4697           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4698         (while servers
4699           (when (eq (caaar servers) backend)
4700             (setq possible (caar servers))
4701             (when (equal (cadaar servers) server)
4702               (setq found (caar servers))))
4703           (pop servers))
4704         (or (car (rassoc found gnus-server-alist))
4705             found
4706             (car (rassoc possible gnus-server-alist))
4707             possible
4708             (list backend server))))))
4709
4710 (defsubst gnus-secondary-method-p (method)
4711   "Return whether METHOD is a secondary select method."
4712   (let ((methods gnus-secondary-select-methods)
4713         (gmethod (gnus-server-get-method nil method)))
4714     (while (and methods
4715                 (not (equal (gnus-server-get-method nil (car methods))
4716                             gmethod)))
4717       (setq methods (cdr methods)))
4718     methods))
4719
4720 (defun gnus-group-foreign-p (group)
4721   "Say whether a group is foreign or not."
4722   (and (not (gnus-group-native-p group))
4723        (not (gnus-group-secondary-p group))))
4724
4725 (defun gnus-group-native-p (group)
4726   "Say whether the group is native or not."
4727   (not (string-match ":" group)))
4728
4729 (defun gnus-group-secondary-p (group)
4730   "Say whether the group is secondary or not."
4731   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4732
4733 (defun gnus-group-get-parameter (group &optional symbol)
4734   "Returns the group parameters for GROUP.
4735 If SYMBOL, return the value of that symbol in the group parameters."
4736   (let ((params (gnus-info-params (gnus-get-info group))))
4737     (if symbol
4738         (gnus-group-parameter-value params symbol)
4739       params)))
4740
4741 (defun gnus-group-parameter-value (params symbol)
4742   "Return the value of SYMBOL in group PARAMS."
4743   (or (car (memq symbol params))        ; It's either a simple symbol
4744       (cdr (assq symbol params))))      ; or a cons.
4745
4746 (defun gnus-group-add-parameter (group param)
4747   "Add parameter PARAM to GROUP."
4748   (let ((info (gnus-get-info group)))
4749     (if (not info)
4750         () ; This is a dead group.  We just ignore it.
4751       ;; Cons the new param to the old one and update.
4752       (gnus-group-set-info (cons param (gnus-info-params info))
4753                            group 'params))))
4754
4755 (defun gnus-group-set-parameter (group name value)
4756   "Set parameter NAME to VALUE in GROUP."
4757   (let ((info (gnus-get-info group)))
4758     (if (not info)
4759         () ; This is a dead group.  We just ignore it.
4760       (let ((old-params (gnus-info-params info))
4761             (new-params (list (cons name value))))
4762         (while old-params
4763           (if (or (not (listp (car old-params)))
4764                   (not (eq (caar old-params) name)))
4765               (setq new-params (append new-params (list (car old-params)))))
4766           (setq old-params (cdr old-params)))
4767         (gnus-group-set-info new-params group 'params)))))
4768
4769 (defun gnus-group-add-score (group &optional score)
4770   "Add SCORE to the GROUP score.
4771 If SCORE is nil, add 1 to the score of GROUP."
4772   (let ((info (gnus-get-info group)))
4773     (when info
4774       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4775
4776 (defun gnus-summary-bubble-group ()
4777   "Increase the score of the current group.
4778 This is a handy function to add to `gnus-summary-exit-hook' to
4779 increase the score of each group you read."
4780   (gnus-group-add-score gnus-newsgroup-name))
4781
4782 (defun gnus-group-set-info (info &optional method-only-group part)
4783   (let* ((entry (gnus-gethash
4784                  (or method-only-group (gnus-info-group info))
4785                  gnus-newsrc-hashtb))
4786          (part-info info)
4787          (info (if method-only-group (nth 2 entry) info))
4788          method)
4789     (when method-only-group
4790       (unless entry
4791         (error "Trying to change non-existent group %s" method-only-group))
4792       ;; We have received parts of the actual group info - either the
4793       ;; select method or the group parameters.  We first check
4794       ;; whether we have to extend the info, and if so, do that.
4795       (let ((len (length info))
4796             (total (if (eq part 'method) 5 6)))
4797         (when (< len total)
4798           (setcdr (nthcdr (1- len) info)
4799                   (make-list (- total len) nil)))
4800         ;; Then we enter the new info.
4801         (setcar (nthcdr (1- total) info) part-info)))
4802     (unless entry
4803       ;; This is a new group, so we just create it.
4804       (save-excursion
4805         (set-buffer gnus-group-buffer)
4806         (setq method (gnus-info-method info))
4807         (when (gnus-server-equal method "native")
4808           (setq method nil))
4809         (save-excursion
4810           (set-buffer gnus-group-buffer)
4811           (if method
4812               ;; It's a foreign group...
4813               (gnus-group-make-group
4814                (gnus-group-real-name (gnus-info-group info))
4815                (if (stringp method) method
4816                  (prin1-to-string (car method)))
4817                (and (consp method)
4818                     (nth 1 (gnus-info-method info))))
4819             ;; It's a native group.
4820             (gnus-group-make-group (gnus-info-group info))))
4821         (gnus-message 6 "Note: New group created")
4822         (setq entry
4823               (gnus-gethash (gnus-group-prefixed-name
4824                              (gnus-group-real-name (gnus-info-group info))
4825                              (or (gnus-info-method info) gnus-select-method))
4826                             gnus-newsrc-hashtb))))
4827     ;; Whether it was a new group or not, we now have the entry, so we
4828     ;; can do the update.
4829     (if entry
4830         (progn
4831           (setcar (nthcdr 2 entry) info)
4832           (when (and (not (eq (car entry) t))
4833                      (gnus-active (gnus-info-group info)))
4834             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4835       (error "No such group: %s" (gnus-info-group info)))))
4836
4837 (defun gnus-group-set-method-info (group select-method)
4838   (gnus-group-set-info select-method group 'method))
4839
4840 (defun gnus-group-set-params-info (group params)
4841   (gnus-group-set-info params group 'params))
4842
4843 (defun gnus-group-update-group-line ()
4844   "Update the current line in the group buffer."
4845   (let* ((buffer-read-only nil)
4846          (group (gnus-group-group-name))
4847          (gnus-group-indentation (gnus-group-group-indentation))
4848          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4849     (and entry
4850          (not (gnus-ephemeral-group-p group))
4851          (gnus-dribble-enter
4852           (concat "(gnus-group-set-info '"
4853                   (prin1-to-string (nth 2 entry)) ")")))
4854     (gnus-delete-line)
4855     (gnus-group-insert-group-line-info group)
4856     (forward-line -1)
4857     (gnus-group-position-point)))
4858
4859 (defun gnus-group-insert-group-line-info (group)
4860   "Insert GROUP on the current line."
4861   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4862         active info)
4863     (setq gnus-group-indentation (gnus-group-group-indentation))
4864     (if entry
4865         (progn
4866           ;; (Un)subscribed group.
4867           (setq info (nth 2 entry))
4868           (gnus-group-insert-group-line
4869            group (gnus-info-level info) (gnus-info-marks info)
4870            (or (car entry) t) (gnus-info-method info)))
4871       ;; This group is dead.
4872       (gnus-group-insert-group-line
4873        group
4874        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4875        nil
4876        (if (setq active (gnus-active group))
4877            (- (1+ (cdr active)) (car active)) 0)
4878        nil))))
4879
4880 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4881                                                     gnus-tmp-marked number
4882                                                     gnus-tmp-method)
4883   "Insert a group line in the group buffer."
4884   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4885          (gnus-tmp-number-total
4886           (if gnus-tmp-active
4887               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4888             0))
4889          (gnus-tmp-number-of-unread
4890           (if (numberp number) (int-to-string (max 0 number))
4891             "*"))
4892          (gnus-tmp-number-of-read
4893           (if (numberp number)
4894               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4895             "*"))
4896          (gnus-tmp-subscribed
4897           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4898                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4899                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4900                 (t ?K)))
4901          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4902          (gnus-tmp-newsgroup-description
4903           (if gnus-description-hashtb
4904               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4905             ""))
4906          (gnus-tmp-moderated
4907           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4908          (gnus-tmp-moderated-string
4909           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4910          (gnus-tmp-method
4911           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4912          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4913          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4914          (gnus-tmp-news-method-string
4915           (if gnus-tmp-method
4916               (format "(%s:%s)" (car gnus-tmp-method)
4917                       (cadr gnus-tmp-method)) ""))
4918          (gnus-tmp-marked-mark
4919           (if (and (numberp number)
4920                    (zerop number)
4921                    (cdr (assq 'tick gnus-tmp-marked)))
4922               ?* ? ))
4923          (gnus-tmp-process-marked
4924           (if (member gnus-tmp-group gnus-group-marked)
4925               gnus-process-mark ? ))
4926          (gnus-tmp-grouplens
4927           (or (and gnus-use-grouplens
4928                    (bbb-grouplens-group-p gnus-tmp-group))
4929               ""))
4930          (buffer-read-only nil)
4931          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4932     (beginning-of-line)
4933     (add-text-properties
4934      (point)
4935      (prog1 (1+ (point))
4936        ;; Insert the text.
4937        (eval gnus-group-line-format-spec))
4938      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4939        gnus-unread ,(if (numberp number)
4940                         (string-to-int gnus-tmp-number-of-unread)
4941                       t)
4942        gnus-marked ,gnus-tmp-marked-mark
4943        gnus-indentation ,gnus-group-indentation
4944        gnus-level ,gnus-tmp-level))
4945     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4946       (forward-line -1)
4947       (run-hooks 'gnus-group-update-hook)
4948       (forward-line))
4949     ;; Allow XEmacs to remove front-sticky text properties.
4950     (gnus-group-remove-excess-properties)))
4951
4952 (defun gnus-group-update-group (group &optional visible-only)
4953   "Update all lines where GROUP appear.
4954 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4955 already."
4956   (save-excursion
4957     (set-buffer gnus-group-buffer)
4958     ;; The buffer may be narrowed.
4959     (save-restriction
4960       (widen)
4961       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4962             (loc (point-min))
4963             found buffer-read-only)
4964         ;; Enter the current status into the dribble buffer.
4965         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4966           (if (and entry (not (gnus-ephemeral-group-p group)))
4967               (gnus-dribble-enter
4968                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4969                        ")"))))
4970         ;; Find all group instances.  If topics are in use, each group
4971         ;; may be listed in more than once.
4972         (while (setq loc (text-property-any
4973                           loc (point-max) 'gnus-group ident))
4974           (setq found t)
4975           (goto-char loc)
4976           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4977             (gnus-delete-line)
4978             (gnus-group-insert-group-line-info group))
4979           (setq loc (1+ loc)))
4980         (unless (or found visible-only)
4981           ;; No such line in the buffer, find out where it's supposed to
4982           ;; go, and insert it there (or at the end of the buffer).
4983           (if gnus-goto-missing-group-function
4984               (funcall gnus-goto-missing-group-function group)
4985             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4986               (while (and entry (car entry)
4987                           (not
4988                            (gnus-goto-char
4989                             (text-property-any
4990                              (point-min) (point-max)
4991                              'gnus-group (gnus-intern-safe
4992                                           (caar entry) gnus-active-hashtb)))))
4993                 (setq entry (cdr entry)))
4994               (or entry (goto-char (point-max)))))
4995           ;; Finally insert the line.
4996           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4997             (gnus-group-insert-group-line-info group)))
4998         (gnus-group-set-mode-line)))))
4999
5000 (defun gnus-group-set-mode-line ()
5001   (when (memq 'group gnus-updated-mode-lines)
5002     (let* ((gformat (or gnus-group-mode-line-format-spec
5003                         (setq gnus-group-mode-line-format-spec
5004                               (gnus-parse-format
5005                                gnus-group-mode-line-format
5006                                gnus-group-mode-line-format-alist))))
5007            (gnus-tmp-news-server (cadr gnus-select-method))
5008            (gnus-tmp-news-method (car gnus-select-method))
5009            (max-len 60)
5010            gnus-tmp-header                      ;Dummy binding for user-defined formats
5011            ;; Get the resulting string.
5012            (mode-string (eval gformat)))
5013       ;; If the line is too long, we chop it off.
5014       (when (> (length mode-string) max-len)
5015         (setq mode-string (substring mode-string 0 (- max-len 4))))
5016       (prog1
5017           (setq mode-line-buffer-identification (list mode-string))
5018         (set-buffer-modified-p t)))))
5019
5020 (defun gnus-group-group-name ()
5021   "Get the name of the newsgroup on the current line."
5022   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5023     (and group (symbol-name group))))
5024
5025 (defun gnus-group-group-level ()
5026   "Get the level of the newsgroup on the current line."
5027   (get-text-property (gnus-point-at-bol) 'gnus-level))
5028
5029 (defun gnus-group-group-indentation ()
5030   "Get the indentation of the newsgroup on the current line."
5031   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5032       (and gnus-group-indentation-function
5033            (funcall gnus-group-indentation-function))
5034       ""))
5035
5036 (defun gnus-group-group-unread ()
5037   "Get the number of unread articles of the newsgroup on the current line."
5038   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5039
5040 (defun gnus-group-search-forward (&optional backward all level first-too)
5041   "Find the next newsgroup with unread articles.
5042 If BACKWARD is non-nil, find the previous newsgroup instead.
5043 If ALL is non-nil, just find any newsgroup.
5044 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5045 group exists.
5046 If FIRST-TOO, the current line is also eligible as a target."
5047   (let ((way (if backward -1 1))
5048         (low gnus-level-killed)
5049         (beg (point))
5050         pos found lev)
5051     (if (and backward (progn (beginning-of-line)) (bobp))
5052         nil
5053       (or first-too (forward-line way))
5054       (while (and
5055               (not (eobp))
5056               (not (setq
5057                     found
5058                     (and (or all
5059                              (and
5060                               (let ((unread
5061                                      (get-text-property (point) 'gnus-unread)))
5062                                 (and (numberp unread) (> unread 0)))
5063                               (setq lev (get-text-property (point)
5064                                                            'gnus-level))
5065                               (<= lev gnus-level-subscribed)))
5066                          (or (not level)
5067                              (and (setq lev (get-text-property (point)
5068                                                                'gnus-level))
5069                                   (or (= lev level)
5070                                       (and (< lev low)
5071                                            (< level lev)
5072                                            (progn
5073                                              (setq low lev)
5074                                              (setq pos (point))
5075                                              nil))))))))
5076               (zerop (forward-line way)))))
5077     (if found
5078         (progn (gnus-group-position-point) t)
5079       (goto-char (or pos beg))
5080       (and pos t))))
5081
5082 ;;; Gnus group mode commands
5083
5084 ;; Group marking.
5085
5086 (defun gnus-group-mark-group (n &optional unmark no-advance)
5087   "Mark the current group."
5088   (interactive "p")
5089   (let ((buffer-read-only nil)
5090         group)
5091     (while (and (> n 0)
5092                 (not (eobp)))
5093       (when (setq group (gnus-group-group-name))
5094         ;; Update the mark.
5095         (beginning-of-line)
5096         (forward-char
5097          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5098         (delete-char 1)
5099         (if unmark
5100             (progn
5101               (insert " ")
5102               (setq gnus-group-marked (delete group gnus-group-marked)))
5103           (insert "#")
5104           (setq gnus-group-marked
5105                 (cons group (delete group gnus-group-marked))))
5106         (or no-advance (zerop (gnus-group-next-group 1))))
5107       (decf n))
5108     (gnus-summary-position-point)
5109     n))
5110
5111 (defun gnus-group-unmark-group (n)
5112   "Remove the mark from the current group."
5113   (interactive "p")
5114   (gnus-group-mark-group n 'unmark)
5115   (gnus-group-position-point))
5116
5117 (defun gnus-group-unmark-all-groups ()
5118   "Unmark all groups."
5119   (interactive)
5120   (let ((groups gnus-group-marked))
5121     (save-excursion
5122       (while groups
5123         (gnus-group-remove-mark (pop groups)))))
5124   (gnus-group-position-point))
5125
5126 (defun gnus-group-mark-region (unmark beg end)
5127   "Mark all groups between point and mark.
5128 If UNMARK, remove the mark instead."
5129   (interactive "P\nr")
5130   (let ((num (count-lines beg end)))
5131     (save-excursion
5132       (goto-char beg)
5133       (- num (gnus-group-mark-group num unmark)))))
5134
5135 (defun gnus-group-mark-buffer (&optional unmark)
5136   "Mark all groups in the buffer.
5137 If UNMARK, remove the mark instead."
5138   (interactive "P")
5139   (gnus-group-mark-region unmark (point-min) (point-max)))
5140
5141 (defun gnus-group-mark-regexp (regexp)
5142   "Mark all groups that match some regexp."
5143   (interactive "sMark (regexp): ")
5144   (let ((alist (cdr gnus-newsrc-alist))
5145         group)
5146     (while alist
5147       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5148         (gnus-group-set-mark group))))
5149   (gnus-group-position-point))
5150
5151 (defun gnus-group-remove-mark (group)
5152   "Remove the process mark from GROUP and move point there.
5153 Return nil if the group isn't displayed."
5154   (if (gnus-group-goto-group group)
5155       (save-excursion
5156         (gnus-group-mark-group 1 'unmark t)
5157         t)
5158     (setq gnus-group-marked
5159           (delete group gnus-group-marked))
5160     nil))
5161
5162 (defun gnus-group-set-mark (group)
5163   "Set the process mark on GROUP."
5164   (if (gnus-group-goto-group group) 
5165       (save-excursion
5166         (gnus-group-mark-group 1 nil t))
5167     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5168
5169 (defun gnus-group-universal-argument (arg &optional groups func)
5170   "Perform any command on all groups accoring to the process/prefix convention."
5171   (interactive "P")
5172   (let ((groups (or groups (gnus-group-process-prefix arg)))
5173         group func)
5174     (if (eq (setq func (or func
5175                            (key-binding
5176                             (read-key-sequence
5177                              (substitute-command-keys
5178                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5179             'undefined)
5180         (gnus-error 1 "Undefined key")
5181       (while groups
5182         (gnus-group-remove-mark (setq group (pop groups)))
5183         (command-execute func))))
5184   (gnus-group-position-point))
5185
5186 (defun gnus-group-process-prefix (n)
5187   "Return a list of groups to work on.
5188 Take into consideration N (the prefix) and the list of marked groups."
5189   (cond
5190    (n
5191     (setq n (prefix-numeric-value n))
5192     ;; There is a prefix, so we return a list of the N next
5193     ;; groups.
5194     (let ((way (if (< n 0) -1 1))
5195           (n (abs n))
5196           group groups)
5197       (save-excursion
5198         (while (and (> n 0)
5199                     (setq group (gnus-group-group-name)))
5200           (setq groups (cons group groups))
5201           (setq n (1- n))
5202           (gnus-group-next-group way)))
5203       (nreverse groups)))
5204    ((and (boundp 'transient-mark-mode)
5205          transient-mark-mode
5206          (boundp 'mark-active)
5207          mark-active)
5208     ;; Work on the region between point and mark.
5209     (let ((max (max (point) (mark)))
5210           groups)
5211       (save-excursion
5212         (goto-char (min (point) (mark)))
5213         (while
5214             (and
5215              (push (gnus-group-group-name) groups)
5216              (zerop (gnus-group-next-group 1))
5217              (< (point) max)))
5218         (nreverse groups))))
5219    (gnus-group-marked
5220     ;; No prefix, but a list of marked articles.
5221     (reverse gnus-group-marked))
5222    (t
5223     ;; Neither marked articles or a prefix, so we return the
5224     ;; current group.
5225     (let ((group (gnus-group-group-name)))
5226       (and group (list group))))))
5227
5228 ;; Selecting groups.
5229
5230 (defun gnus-group-read-group (&optional all no-article group)
5231   "Read news in this newsgroup.
5232 If the prefix argument ALL is non-nil, already read articles become
5233 readable.  IF ALL is a number, fetch this number of articles.  If the
5234 optional argument NO-ARTICLE is non-nil, no article will be
5235 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5236 group."
5237   (interactive "P")
5238   (let ((group (or group (gnus-group-group-name)))
5239         number active marked entry)
5240     (or group (error "No group on current line"))
5241     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5242                                             group gnus-newsrc-hashtb)))))
5243     ;; This group might be a dead group.  In that case we have to get
5244     ;; the number of unread articles from `gnus-active-hashtb'.
5245     (setq number
5246           (cond ((numberp all) all)
5247                 (entry (car entry))
5248                 ((setq active (gnus-active group))
5249                  (- (1+ (cdr active)) (car active)))))
5250     (gnus-summary-read-group
5251      group (or all (and (numberp number)
5252                         (zerop (+ number (length (cdr (assq 'tick marked)))
5253                                   (length (cdr (assq 'dormant marked)))))))
5254      no-article)))
5255
5256 (defun gnus-group-select-group (&optional all)
5257   "Select this newsgroup.
5258 No article is selected automatically.
5259 If ALL is non-nil, already read articles become readable.
5260 If ALL is a number, fetch this number of articles."
5261   (interactive "P")
5262   (gnus-group-read-group all t))
5263
5264 (defun gnus-group-quick-select-group (&optional all)
5265   "Select the current group \"quickly\".
5266 This means that no highlighting or scoring will be performed."
5267   (interactive "P")
5268   (let (gnus-visual
5269         gnus-score-find-score-files-function
5270         gnus-apply-kill-hook
5271         gnus-summary-expunge-below)
5272     (gnus-group-read-group all t)))
5273
5274 (defun gnus-group-visible-select-group (&optional all)
5275   "Select the current group without hiding any articles."
5276   (interactive "P")
5277   (let ((gnus-inhibit-limiting t))
5278     (gnus-group-read-group all t)))
5279
5280 ;;;###autoload
5281 (defun gnus-fetch-group (group)
5282   "Start Gnus if necessary and enter GROUP.
5283 Returns whether the fetching was successful or not."
5284   (interactive "sGroup name: ")
5285   (or (get-buffer gnus-group-buffer)
5286       (gnus))
5287   (gnus-group-read-group nil nil group))
5288
5289 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5290 ;; if selection was successful.
5291 (defun gnus-group-read-ephemeral-group
5292   (group method &optional activate quit-config)
5293   (let ((group (if (gnus-group-foreign-p group) group
5294                  (gnus-group-prefixed-name group method))))
5295     (gnus-sethash
5296      group
5297      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5298                      ((quit-config . ,(if quit-config quit-config
5299                                         (cons (current-buffer) 'summary))))))
5300      gnus-newsrc-hashtb)
5301     (set-buffer gnus-group-buffer)
5302     (or (gnus-check-server method)
5303         (error "Unable to contact server: %s" (gnus-status-message method)))
5304     (if activate (or (gnus-request-group group)
5305                      (error "Couldn't request group")))
5306     (condition-case ()
5307         (gnus-group-read-group t t group)
5308       (error nil)
5309       (quit nil))))
5310
5311 (defun gnus-group-jump-to-group (group)
5312   "Jump to newsgroup GROUP."
5313   (interactive
5314    (list (completing-read
5315           "Group: " gnus-active-hashtb nil
5316           (gnus-read-active-file-p)
5317           nil
5318           'gnus-group-history)))
5319
5320   (when (equal group "")
5321     (error "Empty group name"))
5322
5323   (when (string-match "[\000-\032]" group)
5324     (error "Control characters in group: %s" group))
5325
5326   (let ((b (text-property-any
5327             (point-min) (point-max)
5328             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5329     (unless (gnus-ephemeral-group-p group)
5330       (if b
5331           ;; Either go to the line in the group buffer...
5332           (goto-char b)
5333         ;; ... or insert the line.
5334         (or
5335          (gnus-active group)
5336          (gnus-activate-group group)
5337          (error "%s error: %s" group (gnus-status-message group)))
5338
5339         (gnus-group-update-group group)
5340         (goto-char (text-property-any
5341                     (point-min) (point-max)
5342                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5343     ;; Adjust cursor point.
5344     (gnus-group-position-point)))
5345
5346 (defun gnus-group-goto-group (group)
5347   "Goto to newsgroup GROUP."
5348   (when group
5349     (let ((b (text-property-any (point-min) (point-max)
5350                                 'gnus-group (gnus-intern-safe
5351                                              group gnus-active-hashtb))))
5352       (and b (goto-char b)))))
5353
5354 (defun gnus-group-next-group (n)
5355   "Go to next N'th newsgroup.
5356 If N is negative, search backward instead.
5357 Returns the difference between N and the number of skips actually
5358 done."
5359   (interactive "p")
5360   (gnus-group-next-unread-group n t))
5361
5362 (defun gnus-group-next-unread-group (n &optional all level)
5363   "Go to next N'th unread newsgroup.
5364 If N is negative, search backward instead.
5365 If ALL is non-nil, choose any newsgroup, unread or not.
5366 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5367 such group can be found, the next group with a level higher than
5368 LEVEL.
5369 Returns the difference between N and the number of skips actually
5370 made."
5371   (interactive "p")
5372   (let ((backward (< n 0))
5373         (n (abs n)))
5374     (while (and (> n 0)
5375                 (gnus-group-search-forward
5376                  backward (or (not gnus-group-goto-unread) all) level))
5377       (setq n (1- n)))
5378     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5379                                (if level " on this level or higher" "")))
5380     n))
5381
5382 (defun gnus-group-prev-group (n)
5383   "Go to previous N'th newsgroup.
5384 Returns the difference between N and the number of skips actually
5385 done."
5386   (interactive "p")
5387   (gnus-group-next-unread-group (- n) t))
5388
5389 (defun gnus-group-prev-unread-group (n)
5390   "Go to previous N'th unread newsgroup.
5391 Returns the difference between N and the number of skips actually
5392 done."
5393   (interactive "p")
5394   (gnus-group-next-unread-group (- n)))
5395
5396 (defun gnus-group-next-unread-group-same-level (n)
5397   "Go to next N'th unread newsgroup on the same level.
5398 If N is negative, search backward instead.
5399 Returns the difference between N and the number of skips actually
5400 done."
5401   (interactive "p")
5402   (gnus-group-next-unread-group n t (gnus-group-group-level))
5403   (gnus-group-position-point))
5404
5405 (defun gnus-group-prev-unread-group-same-level (n)
5406   "Go to next N'th unread newsgroup on the same level.
5407 Returns the difference between N and the number of skips actually
5408 done."
5409   (interactive "p")
5410   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5411   (gnus-group-position-point))
5412
5413 (defun gnus-group-best-unread-group (&optional exclude-group)
5414   "Go to the group with the highest level.
5415 If EXCLUDE-GROUP, do not go to that group."
5416   (interactive)
5417   (goto-char (point-min))
5418   (let ((best 100000)
5419         unread best-point)
5420     (while (not (eobp))
5421       (setq unread (get-text-property (point) 'gnus-unread))
5422       (if (and (numberp unread) (> unread 0))
5423           (progn
5424             (if (and (get-text-property (point) 'gnus-level)
5425                      (< (get-text-property (point) 'gnus-level) best)
5426                      (or (not exclude-group)
5427                          (not (equal exclude-group (gnus-group-group-name)))))
5428                 (progn
5429                   (setq best (get-text-property (point) 'gnus-level))
5430                   (setq best-point (point))))))
5431       (forward-line 1))
5432     (if best-point (goto-char best-point))
5433     (gnus-summary-position-point)
5434     (and best-point (gnus-group-group-name))))
5435
5436 (defun gnus-group-first-unread-group ()
5437   "Go to the first group with unread articles."
5438   (interactive)
5439   (prog1
5440       (let ((opoint (point))
5441             unread)
5442         (goto-char (point-min))
5443         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5444                 (and (numberp unread)   ; Not a topic.
5445                      (not (zerop unread))) ; Has unread articles.
5446                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5447             (point)                     ; Success.
5448           (goto-char opoint)
5449           nil))                         ; Not success.
5450     (gnus-group-position-point)))
5451
5452 (defun gnus-group-enter-server-mode ()
5453   "Jump to the server buffer."
5454   (interactive)
5455   (gnus-enter-server-buffer))
5456
5457 (defun gnus-group-make-group (name &optional method address)
5458   "Add a new newsgroup.
5459 The user will be prompted for a NAME, for a select METHOD, and an
5460 ADDRESS."
5461   (interactive
5462    (cons
5463     (read-string "Group name: ")
5464     (let ((method
5465            (completing-read
5466             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5467             nil t nil 'gnus-method-history)))
5468       (cond ((assoc method gnus-valid-select-methods)
5469              (list method
5470                    (if (memq 'prompt-address
5471                              (assoc method gnus-valid-select-methods))
5472                        (read-string "Address: ")
5473                      "")))
5474             ((assoc method gnus-server-alist)
5475              (list method))
5476             (t
5477              (list method ""))))))
5478
5479   (let* ((meth (and method (if address (list (intern method) address)
5480                              method)))
5481          (nname (if method (gnus-group-prefixed-name name meth) name))
5482          backend info)
5483     (when (gnus-gethash nname gnus-newsrc-hashtb)
5484       (error "Group %s already exists" nname))
5485     ;; Subscribe to the new group.
5486     (gnus-group-change-level
5487      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5488      gnus-level-default-subscribed gnus-level-killed
5489      (and (gnus-group-group-name)
5490           (gnus-gethash (gnus-group-group-name)
5491                         gnus-newsrc-hashtb))
5492      t)
5493     ;; Make it active.
5494     (gnus-set-active nname (cons 1 0))
5495     (or (gnus-ephemeral-group-p name)
5496         (gnus-dribble-enter
5497          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5498     ;; Insert the line.
5499     (gnus-group-insert-group-line-info nname)
5500     (forward-line -1)
5501     (gnus-group-position-point)
5502
5503     ;; Load the backend and try to make the backend create
5504     ;; the group as well.
5505     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5506                                                   nil meth))))
5507                  gnus-valid-select-methods)
5508       (require backend))
5509     (gnus-check-server meth)
5510     (and (gnus-check-backend-function 'request-create-group nname)
5511          (gnus-request-create-group nname))
5512     t))
5513
5514 (defun gnus-group-delete-group (group &optional force)
5515   "Delete the current group.
5516 If FORCE (the prefix) is non-nil, all the articles in the group will
5517 be deleted.  This is \"deleted\" as in \"removed forever from the face
5518 of the Earth\".  There is no undo."
5519   (interactive
5520    (list (gnus-group-group-name)
5521          current-prefix-arg))
5522   (or group (error "No group to rename"))
5523   (or (gnus-check-backend-function 'request-delete-group group)
5524       (error "This backend does not support group deletion"))
5525   (prog1
5526       (if (not (gnus-yes-or-no-p
5527                 (format
5528                  "Do you really want to delete %s%s? "
5529                  group (if force " and all its contents" ""))))
5530           () ; Whew!
5531         (gnus-message 6 "Deleting group %s..." group)
5532         (if (not (gnus-request-delete-group group force))
5533             (gnus-error 3 "Couldn't delete group %s" group)
5534           (gnus-message 6 "Deleting group %s...done" group)
5535           (gnus-group-goto-group group)
5536           (gnus-group-kill-group 1 t)
5537           (gnus-sethash group nil gnus-active-hashtb)
5538           t))
5539     (gnus-group-position-point)))
5540
5541 (defun gnus-group-rename-group (group new-name)
5542   (interactive
5543    (list
5544     (gnus-group-group-name)
5545     (progn
5546       (or (gnus-check-backend-function
5547            'request-rename-group (gnus-group-group-name))
5548           (error "This backend does not support renaming groups"))
5549       (read-string "New group name: "))))
5550
5551   (or (gnus-check-backend-function 'request-rename-group group)
5552       (error "This backend does not support renaming groups"))
5553
5554   (or group (error "No group to rename"))
5555   (and (string-match "^[ \t]*$" new-name)
5556        (error "Not a valid group name"))
5557
5558   ;; We find the proper prefixed name.
5559   (setq new-name
5560         (gnus-group-prefixed-name
5561          (gnus-group-real-name new-name)
5562          (gnus-info-method (gnus-get-info group))))
5563
5564   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5565   (prog1
5566       (if (not (gnus-request-rename-group group new-name))
5567           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5568         ;; We rename the group internally by killing it...
5569         (gnus-group-goto-group group)
5570         (gnus-group-kill-group)
5571         ;; ... changing its name ...
5572         (setcar (cdar gnus-list-of-killed-groups) new-name)
5573         ;; ... and then yanking it.  Magic!
5574         (gnus-group-yank-group)
5575         (gnus-set-active new-name (gnus-active group))
5576         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5577         new-name)
5578     (gnus-group-position-point)))
5579
5580 (defun gnus-group-edit-group (group &optional part)
5581   "Edit the group on the current line."
5582   (interactive (list (gnus-group-group-name)))
5583   (let* ((part (or part 'info))
5584          (done-func `(lambda ()
5585                        "Exit editing mode and update the information."
5586                        (interactive)
5587                        (gnus-group-edit-group-done ',part ,group)))
5588          (winconf (current-window-configuration))
5589          info)
5590     (or group (error "No group on current line"))
5591     (or (setq info (gnus-get-info group))
5592         (error "Killed group; can't be edited"))
5593     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5594     (gnus-configure-windows 'edit-group)
5595     (gnus-add-current-to-buffer-list)
5596     (emacs-lisp-mode)
5597     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5598     (use-local-map (copy-keymap emacs-lisp-mode-map))
5599     (local-set-key "\C-c\C-c" done-func)
5600     (make-local-variable 'gnus-prev-winconf)
5601     (setq gnus-prev-winconf winconf)
5602     (erase-buffer)
5603     (insert
5604      (cond
5605       ((eq part 'method)
5606        ";; Type `C-c C-c' after editing the select method.\n\n")
5607       ((eq part 'params)
5608        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5609       ((eq part 'info)
5610        ";; Type `C-c C-c' after editing the group info.\n\n")))
5611     (insert
5612      (pp-to-string
5613       (cond ((eq part 'method)
5614              (or (gnus-info-method info) "native"))
5615             ((eq part 'params)
5616              (gnus-info-params info))
5617             (t info)))
5618      "\n")))
5619
5620 (defun gnus-group-edit-group-method (group)
5621   "Edit the select method of GROUP."
5622   (interactive (list (gnus-group-group-name)))
5623   (gnus-group-edit-group group 'method))
5624
5625 (defun gnus-group-edit-group-parameters (group)
5626   "Edit the group parameters of GROUP."
5627   (interactive (list (gnus-group-group-name)))
5628   (gnus-group-edit-group group 'params))
5629
5630 (defun gnus-group-edit-group-done (part group)
5631   "Get info from buffer, update variables and jump to the group buffer."
5632   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5633   (goto-char (point-min))
5634   (let* ((form (read (current-buffer)))
5635          (winconf gnus-prev-winconf)
5636          (method (cond ((eq part 'info) (nth 4 form))
5637                        ((eq part 'method) form)
5638                        (t nil)))
5639          (info (cond ((eq part 'info) form)
5640                      ((eq part 'method) (gnus-get-info group))
5641                      (t nil)))
5642          (new-group (if info
5643                       (if (or (not method)
5644                               (gnus-server-equal
5645                                gnus-select-method method))
5646                           (gnus-group-real-name (car info))
5647                         (gnus-group-prefixed-name
5648                          (gnus-group-real-name (car info)) method))
5649                       nil)))
5650     (when (and new-group
5651                (not (equal new-group group)))
5652       (when (gnus-group-goto-group group)
5653         (gnus-group-kill-group 1))
5654       (gnus-activate-group new-group))
5655     ;; Set the info.
5656     (if (and info new-group)
5657         (progn
5658           (setq info (gnus-copy-sequence info))
5659           (setcar info new-group)
5660           (unless (gnus-server-equal method "native")
5661             (unless (nthcdr 3 info)
5662               (nconc info (list nil nil)))
5663             (unless (nthcdr 4 info)
5664               (nconc info (list nil)))
5665             (gnus-info-set-method info method))
5666           (gnus-group-set-info info))
5667       (gnus-group-set-info form (or new-group group) part))
5668     (kill-buffer (current-buffer))
5669     (and winconf (set-window-configuration winconf))
5670     (set-buffer gnus-group-buffer)
5671     (gnus-group-update-group (or new-group group))
5672     (gnus-group-position-point)))
5673
5674 (defun gnus-group-make-help-group ()
5675   "Create the Gnus documentation group."
5676   (interactive)
5677   (let ((path load-path)
5678         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5679         file dir)
5680     (and (gnus-gethash name gnus-newsrc-hashtb)
5681          (error "Documentation group already exists"))
5682     (while path
5683       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5684             file nil)
5685       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5686                 (file-exists-p
5687                  (setq file (concat (file-name-directory
5688                                      (directory-file-name dir))
5689                                     "etc/gnus-tut.txt"))))
5690         (setq path nil)))
5691     (if (not file)
5692         (gnus-message 1 "Couldn't find doc group")
5693       (gnus-group-make-group
5694        (gnus-group-real-name name)
5695        (list 'nndoc "gnus-help"
5696              (list 'nndoc-address file)
5697              (list 'nndoc-article-type 'mbox)))))
5698   (gnus-group-position-point))
5699
5700 (defun gnus-group-make-doc-group (file type)
5701   "Create a group that uses a single file as the source."
5702   (interactive
5703    (list (read-file-name "File name: ")
5704          (and current-prefix-arg 'ask)))
5705   (when (eq type 'ask)
5706     (let ((err "")
5707           char found)
5708       (while (not found)
5709         (message
5710          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5711          err)
5712         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5713                           ((= char ?b) 'babyl)
5714                           ((= char ?d) 'digest)
5715                           ((= char ?f) 'forward)
5716                           ((= char ?a) 'mmfd)
5717                           (t (setq err (format "%c unknown. " char))
5718                              nil))))
5719       (setq type found)))
5720   (let* ((file (expand-file-name file))
5721          (name (gnus-generate-new-group-name
5722                 (gnus-group-prefixed-name
5723                  (file-name-nondirectory file) '(nndoc "")))))
5724     (gnus-group-make-group
5725      (gnus-group-real-name name)
5726      (list 'nndoc (file-name-nondirectory file)
5727            (list 'nndoc-address file)
5728            (list 'nndoc-article-type (or type 'guess))))))
5729
5730 (defun gnus-group-make-archive-group (&optional all)
5731   "Create the (ding) Gnus archive group of the most recent articles.
5732 Given a prefix, create a full group."
5733   (interactive "P")
5734   (let ((group (gnus-group-prefixed-name
5735                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5736     (and (gnus-gethash group gnus-newsrc-hashtb)
5737          (error "Archive group already exists"))
5738     (gnus-group-make-group
5739      (gnus-group-real-name group)
5740      (list 'nndir (if all "hpc" "edu")
5741            (list 'nndir-directory
5742                  (if all gnus-group-archive-directory
5743                    gnus-group-recent-archive-directory))))))
5744
5745 (defun gnus-group-make-directory-group (dir)
5746   "Create an nndir group.
5747 The user will be prompted for a directory.  The contents of this
5748 directory will be used as a newsgroup.  The directory should contain
5749 mail messages or news articles in files that have numeric names."
5750   (interactive
5751    (list (read-file-name "Create group from directory: ")))
5752   (or (file-exists-p dir) (error "No such directory"))
5753   (or (file-directory-p dir) (error "Not a directory"))
5754   (let ((ext "")
5755         (i 0)
5756         group)
5757     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5758       (setq group
5759             (gnus-group-prefixed-name
5760              (concat (file-name-as-directory (directory-file-name dir))
5761                      ext)
5762              '(nndir "")))
5763       (setq ext (format "<%d>" (setq i (1+ i)))))
5764     (gnus-group-make-group
5765      (gnus-group-real-name group)
5766      (list 'nndir group (list 'nndir-directory dir)))))
5767
5768 (defun gnus-group-make-kiboze-group (group address scores)
5769   "Create an nnkiboze group.
5770 The user will be prompted for a name, a regexp to match groups, and
5771 score file entries for articles to include in the group."
5772   (interactive
5773    (list
5774     (read-string "nnkiboze group name: ")
5775     (read-string "Source groups (regexp): ")
5776     (let ((headers (mapcar (lambda (group) (list group))
5777                            '("subject" "from" "number" "date" "message-id"
5778                              "references" "chars" "lines" "xref"
5779                              "followup" "all" "body" "head")))
5780           scores header regexp regexps)
5781       (while (not (equal "" (setq header (completing-read
5782                                           "Match on header: " headers nil t))))
5783         (setq regexps nil)
5784         (while (not (equal "" (setq regexp (read-string
5785                                             (format "Match on %s (string): "
5786                                                     header)))))
5787           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5788         (setq scores (cons (cons header regexps) scores)))
5789       scores)))
5790   (gnus-group-make-group group "nnkiboze" address)
5791   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5792     (let (emacs-lisp-mode-hook)
5793       (pp scores (current-buffer)))))
5794
5795 (defun gnus-group-add-to-virtual (n vgroup)
5796   "Add the current group to a virtual group."
5797   (interactive
5798    (list current-prefix-arg
5799          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5800                           "nnvirtual:")))
5801   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5802       (error "%s is not an nnvirtual group" vgroup))
5803   (let* ((groups (gnus-group-process-prefix n))
5804          (method (gnus-info-method (gnus-get-info vgroup))))
5805     (setcar (cdr method)
5806             (concat
5807              (nth 1 method) "\\|"
5808              (mapconcat
5809               (lambda (s)
5810                 (gnus-group-remove-mark s)
5811                 (concat "\\(^" (regexp-quote s) "$\\)"))
5812               groups "\\|"))))
5813   (gnus-group-position-point))
5814
5815 (defun gnus-group-make-empty-virtual (group)
5816   "Create a new, fresh, empty virtual group."
5817   (interactive "sCreate new, empty virtual group: ")
5818   (let* ((method (list 'nnvirtual "^$"))
5819          (pgroup (gnus-group-prefixed-name group method)))
5820     ;; Check whether it exists already.
5821     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5822          (error "Group %s already exists." pgroup))
5823     ;; Subscribe the new group after the group on the current line.
5824     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5825     (gnus-group-update-group pgroup)
5826     (forward-line -1)
5827     (gnus-group-position-point)))
5828
5829 (defun gnus-group-enter-directory (dir)
5830   "Enter an ephemeral nneething group."
5831   (interactive "DDirectory to read: ")
5832   (let* ((method (list 'nneething dir))
5833          (leaf (gnus-group-prefixed-name
5834                 (file-name-nondirectory (directory-file-name dir))
5835                 method))
5836          (name (gnus-generate-new-group-name leaf)))
5837     (let ((nneething-read-only t))
5838       (or (gnus-group-read-ephemeral-group
5839            name method t
5840            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5841                                       'summary 'group)))
5842           (error "Couldn't enter %s" dir)))))
5843
5844 ;; Group sorting commands
5845 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5846
5847 (defun gnus-group-sort-groups (func &optional reverse)
5848   "Sort the group buffer according to FUNC.
5849 If REVERSE, reverse the sorting order."
5850   (interactive (list gnus-group-sort-function
5851                      current-prefix-arg))
5852   (let ((func (cond 
5853                ((not (listp func)) func)
5854                ((null func) func)
5855                ((= 1 (length func)) (car func))
5856                (t `(lambda (t1 t2)
5857                      ,(gnus-make-sort-function 
5858                        (reverse func)))))))
5859     ;; We peel off the dummy group from the alist.
5860     (when func
5861       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5862         (pop gnus-newsrc-alist))
5863       ;; Do the sorting.
5864       (setq gnus-newsrc-alist
5865             (sort gnus-newsrc-alist func))
5866       (when reverse
5867         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5868       ;; Regenerate the hash table.
5869       (gnus-make-hashtable-from-newsrc-alist)
5870       (gnus-group-list-groups))))
5871
5872 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5873   "Sort the group buffer alphabetically by group name.
5874 If REVERSE, sort in reverse order."
5875   (interactive "P")
5876   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5877
5878 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5879   "Sort the group buffer by number of unread articles.
5880 If REVERSE, sort in reverse order."
5881   (interactive "P")
5882   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5883
5884 (defun gnus-group-sort-groups-by-level (&optional reverse)
5885   "Sort the group buffer by group level.
5886 If REVERSE, sort in reverse order."
5887   (interactive "P")
5888   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5889
5890 (defun gnus-group-sort-groups-by-score (&optional reverse)
5891   "Sort the group buffer by group score.
5892 If REVERSE, sort in reverse order."
5893   (interactive "P")
5894   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5895
5896 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5897   "Sort the group buffer by group rank.
5898 If REVERSE, sort in reverse order."
5899   (interactive "P")
5900   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5901
5902 (defun gnus-group-sort-groups-by-method (&optional reverse)
5903   "Sort the group buffer alphabetically by backend name.
5904 If REVERSE, sort in reverse order."
5905   (interactive "P")
5906   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5907
5908 (defun gnus-group-sort-by-alphabet (info1 info2)
5909   "Sort alphabetically."
5910   (string< (gnus-info-group info1) (gnus-info-group info2)))
5911
5912 (defun gnus-group-sort-by-unread (info1 info2)
5913   "Sort by number of unread articles."
5914   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5915         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5916     (< (or (and (numberp n1) n1) 0)
5917        (or (and (numberp n2) n2) 0))))
5918
5919 (defun gnus-group-sort-by-level (info1 info2)
5920   "Sort by level."
5921   (< (gnus-info-level info1) (gnus-info-level info2)))
5922
5923 (defun gnus-group-sort-by-method (info1 info2)
5924   "Sort alphabetically by backend name."
5925   (string< (symbol-name (car (gnus-find-method-for-group
5926                               (gnus-info-group info1) info1)))
5927            (symbol-name (car (gnus-find-method-for-group
5928                               (gnus-info-group info2) info2)))))
5929
5930 (defun gnus-group-sort-by-score (info1 info2)
5931   "Sort by group score."
5932   (< (gnus-info-score info1) (gnus-info-score info2)))
5933
5934 (defun gnus-group-sort-by-rank (info1 info2)
5935   "Sort by level and score."
5936   (let ((level1 (gnus-info-level info1))
5937         (level2 (gnus-info-level info2)))
5938     (or (< level1 level2)
5939         (and (= level1 level2)
5940              (> (gnus-info-score info1) (gnus-info-score info2))))))
5941
5942 ;; Group catching up.
5943
5944 (defun gnus-group-clear-data (n)
5945   "Clear all marks and read ranges from the current group."
5946   (interactive "P")
5947   (let ((groups (gnus-group-process-prefix n))
5948         group info)
5949     (while (setq group (pop groups))
5950       (setq info (gnus-get-info group))
5951       (gnus-info-set-read info nil)
5952       (when (gnus-info-marks info)
5953         (gnus-info-set-marks info nil))
5954       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5955       (when (gnus-group-goto-group group)
5956         (gnus-group-remove-mark group)
5957         (gnus-group-update-group-line)))))
5958
5959 (defun gnus-group-catchup-current (&optional n all)
5960   "Mark all articles not marked as unread in current newsgroup as read.
5961 If prefix argument N is numeric, the ARG next newsgroups will be
5962 caught up.  If ALL is non-nil, marked articles will also be marked as
5963 read.  Cross references (Xref: header) of articles are ignored.
5964 The difference between N and actual number of newsgroups that were
5965 caught up is returned."
5966   (interactive "P")
5967   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5968                gnus-expert-user
5969                (gnus-y-or-n-p
5970                 (if all
5971                     "Do you really want to mark all articles as read? "
5972                   "Mark all unread articles as read? "))))
5973       n
5974     (let ((groups (gnus-group-process-prefix n))
5975           (ret 0))
5976       (while groups
5977         ;; Virtual groups have to be given special treatment.
5978         (let ((method (gnus-find-method-for-group (car groups))))
5979           (if (eq 'nnvirtual (car method))
5980               (nnvirtual-catchup-group
5981                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5982         (gnus-group-remove-mark (car groups))
5983         (if (>= (gnus-group-group-level) gnus-level-zombie)
5984             (gnus-message 2 "Dead groups can't be caught up")
5985           (if (prog1
5986                   (gnus-group-goto-group (car groups))
5987                 (gnus-group-catchup (car groups) all))
5988               (gnus-group-update-group-line)
5989             (setq ret (1+ ret))))
5990         (setq groups (cdr groups)))
5991       (gnus-group-next-unread-group 1)
5992       ret)))
5993
5994 (defun gnus-group-catchup-current-all (&optional n)
5995   "Mark all articles in current newsgroup as read.
5996 Cross references (Xref: header) of articles are ignored."
5997   (interactive "P")
5998   (gnus-group-catchup-current n 'all))
5999
6000 (defun gnus-group-catchup (group &optional all)
6001   "Mark all articles in GROUP as read.
6002 If ALL is non-nil, all articles are marked as read.
6003 The return value is the number of articles that were marked as read,
6004 or nil if no action could be taken."
6005   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6006          (num (car entry)))
6007     ;; Do the updating only if the newsgroup isn't killed.
6008     (if (not (numberp (car entry)))
6009         (gnus-message 1 "Can't catch up; non-active group")
6010       ;; Do auto-expirable marks if that's required.
6011       (when (gnus-group-auto-expirable-p group)
6012         (gnus-add-marked-articles
6013          group 'expire (gnus-list-of-unread-articles group))
6014         (when all
6015           (let ((marks (nth 3 (nth 2 entry))))
6016             (gnus-add-marked-articles
6017              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6018             (gnus-add-marked-articles
6019              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6020       (when entry
6021         (gnus-update-read-articles group nil)
6022         ;; Also nix out the lists of marks and dormants.
6023         (when all
6024           (gnus-add-marked-articles group 'tick nil nil 'force)
6025           (gnus-add-marked-articles group 'dormant nil nil 'force))
6026         (run-hooks 'gnus-group-catchup-group-hook)
6027         num))))
6028
6029 (defun gnus-group-expire-articles (&optional n)
6030   "Expire all expirable articles in the current newsgroup."
6031   (interactive "P")
6032   (let ((groups (gnus-group-process-prefix n))
6033         group)
6034     (unless groups
6035       (error "No groups to expire"))
6036     (while (setq group (pop groups))
6037       (gnus-group-remove-mark group)
6038       (when (gnus-check-backend-function 'request-expire-articles group)
6039         (gnus-message 6 "Expiring articles in %s..." group)
6040         (let* ((info (gnus-get-info group))
6041                (expirable (if (gnus-group-total-expirable-p group)
6042                               (cons nil (gnus-list-of-read-articles group))
6043                             (assq 'expire (gnus-info-marks info))))
6044                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6045           (when expirable
6046             (setcdr
6047              expirable
6048              (gnus-compress-sequence
6049               (if expiry-wait
6050                   ;; We set the expiry variables to the groupp
6051                   ;; parameter. 
6052                   (let ((nnmail-expiry-wait-function nil)
6053                         (nnmail-expiry-wait expiry-wait))
6054                     (gnus-request-expire-articles
6055                      (gnus-uncompress-sequence (cdr expirable)) group))
6056                 ;; Just expire using the normal expiry values.
6057                 (gnus-request-expire-articles
6058                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6059           (gnus-message 6 "Expiring articles in %s...done" group)))
6060       (gnus-group-position-point))))
6061
6062 (defun gnus-group-expire-all-groups ()
6063   "Expire all expirable articles in all newsgroups."
6064   (interactive)
6065   (save-excursion
6066     (gnus-message 5 "Expiring...")
6067     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6068                                      (cdr gnus-newsrc-alist))))
6069       (gnus-group-expire-articles nil)))
6070   (gnus-group-position-point)
6071   (gnus-message 5 "Expiring...done"))
6072
6073 (defun gnus-group-set-current-level (n level)
6074   "Set the level of the next N groups to LEVEL."
6075   (interactive
6076    (list
6077     current-prefix-arg
6078     (string-to-int
6079      (let ((s (read-string
6080                (format "Level (default %s): "
6081                        (or (gnus-group-group-level) 
6082                            gnus-level-default-subscribed)))))
6083        (if (string-match "^\\s-*$" s)
6084            (int-to-string (or (gnus-group-group-level) 
6085                               gnus-level-default-subscribed))
6086          s)))))
6087   (or (and (>= level 1) (<= level gnus-level-killed))
6088       (error "Illegal level: %d" level))
6089   (let ((groups (gnus-group-process-prefix n))
6090         group)
6091     (while (setq group (pop groups))
6092       (gnus-group-remove-mark group)
6093       (gnus-message 6 "Changed level of %s from %d to %d"
6094                     group (or (gnus-group-group-level) gnus-level-killed)
6095                     level)
6096       (gnus-group-change-level
6097        group level (or (gnus-group-group-level) gnus-level-killed))
6098       (gnus-group-update-group-line)))
6099   (gnus-group-position-point))
6100
6101 (defun gnus-group-unsubscribe-current-group (&optional n)
6102   "Toggle subscription of the current group.
6103 If given numerical prefix, toggle the N next groups."
6104   (interactive "P")
6105   (let ((groups (gnus-group-process-prefix n))
6106         group)
6107     (while groups
6108       (setq group (car groups)
6109             groups (cdr groups))
6110       (gnus-group-remove-mark group)
6111       (gnus-group-unsubscribe-group
6112        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6113                  gnus-level-default-unsubscribed
6114                gnus-level-default-subscribed) t)
6115       (gnus-group-update-group-line))
6116     (gnus-group-next-group 1)))
6117
6118 (defun gnus-group-unsubscribe-group (group &optional level silent)
6119   "Toggle subscription to GROUP.
6120 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6121 group line."
6122   (interactive
6123    (list (completing-read
6124           "Group: " gnus-active-hashtb nil
6125           (gnus-read-active-file-p)
6126           nil 
6127           'gnus-group-history)))
6128   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6129     (cond
6130      ((string-match "^[ \t]$" group)
6131       (error "Empty group name"))
6132      (newsrc
6133       ;; Toggle subscription flag.
6134       (gnus-group-change-level
6135        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6136                                       gnus-level-subscribed)
6137                                   (1+ gnus-level-subscribed)
6138                                 gnus-level-default-subscribed)))
6139       (unless silent
6140         (gnus-group-update-group group)))
6141      ((and (stringp group)
6142            (or (not (gnus-read-active-file-p))
6143                (gnus-active group)))
6144       ;; Add new newsgroup.
6145       (gnus-group-change-level
6146        group
6147        (if level level gnus-level-default-subscribed)
6148        (or (and (member group gnus-zombie-list)
6149                 gnus-level-zombie)
6150            gnus-level-killed)
6151        (and (gnus-group-group-name)
6152             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6153       (unless silent
6154         (gnus-group-update-group group)))
6155      (t (error "No such newsgroup: %s" group)))
6156     (gnus-group-position-point)))
6157
6158 (defun gnus-group-transpose-groups (n)
6159   "Move the current newsgroup up N places.
6160 If given a negative prefix, move down instead.  The difference between
6161 N and the number of steps taken is returned."
6162   (interactive "p")
6163   (or (gnus-group-group-name)
6164       (error "No group on current line"))
6165   (gnus-group-kill-group 1)
6166   (prog1
6167       (forward-line (- n))
6168     (gnus-group-yank-group)
6169     (gnus-group-position-point)))
6170
6171 (defun gnus-group-kill-all-zombies ()
6172   "Kill all zombie newsgroups."
6173   (interactive)
6174   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6175   (setq gnus-zombie-list nil)
6176   (gnus-group-list-groups))
6177
6178 (defun gnus-group-kill-region (begin end)
6179   "Kill newsgroups in current region (excluding current point).
6180 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6181   (interactive "r")
6182   (let ((lines
6183          ;; Count lines.
6184          (save-excursion
6185            (count-lines
6186             (progn
6187               (goto-char begin)
6188               (beginning-of-line)
6189               (point))
6190             (progn
6191               (goto-char end)
6192               (beginning-of-line)
6193               (point))))))
6194     (goto-char begin)
6195     (beginning-of-line)                 ;Important when LINES < 1
6196     (gnus-group-kill-group lines)))
6197
6198 (defun gnus-group-kill-group (&optional n discard)
6199   "Kill the next N groups.
6200 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6201 However, only groups that were alive can be yanked; already killed
6202 groups or zombie groups can't be yanked.
6203 The return value is the name of the group that was killed, or a list
6204 of groups killed."
6205   (interactive "P")
6206   (let ((buffer-read-only nil)
6207         (groups (gnus-group-process-prefix n))
6208         group entry level out)
6209     (if (< (length groups) 10)
6210         ;; This is faster when there are few groups.
6211         (while groups
6212           (push (setq group (pop groups)) out)
6213           (gnus-group-remove-mark group)
6214           (setq level (gnus-group-group-level))
6215           (gnus-delete-line)
6216           (when (and (not discard)
6217                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6218             (push (cons (car entry) (nth 2 entry))
6219                   gnus-list-of-killed-groups))
6220           (gnus-group-change-level
6221            (if entry entry group) gnus-level-killed (if entry nil level)))
6222       ;; If there are lots and lots of groups to be killed, we use
6223       ;; this thing instead.
6224       (let (entry)
6225         (setq groups (nreverse groups))
6226         (while groups
6227           (gnus-group-remove-mark (setq group (pop groups)))
6228           (gnus-delete-line)
6229           (cond
6230            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6231             (push (cons (car entry) (nth 2 entry))
6232                   gnus-list-of-killed-groups)
6233             (setcdr (cdr entry) (cdddr entry)))
6234            ((member group gnus-zombie-list)
6235             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6236         (gnus-make-hashtable-from-newsrc-alist)))
6237
6238     (gnus-group-position-point)
6239     (if (< (length out) 2) (car out) (nreverse out))))
6240
6241 (defun gnus-group-yank-group (&optional arg)
6242   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6243 inserting it before the current newsgroup.  The numeric ARG specifies
6244 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6245 is returned, or (if several groups are yanked) a list of yanked groups
6246 is returned."
6247   (interactive "p")
6248   (setq arg (or arg 1))
6249   (let (info group prev out)
6250     (while (>= (decf arg) 0)
6251       (if (not (setq info (pop gnus-list-of-killed-groups)))
6252           (error "No more newsgroups to yank"))
6253       (push (setq group (nth 1 info)) out)
6254       ;; Find which newsgroup to insert this one before - search
6255       ;; backward until something suitable is found.  If there are no
6256       ;; other newsgroups in this buffer, just make this newsgroup the
6257       ;; first newsgroup.
6258       (setq prev (gnus-group-group-name))
6259       (gnus-group-change-level
6260        info (gnus-info-level (cdr info)) gnus-level-killed
6261        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6262        t)
6263       (gnus-group-insert-group-line-info group))
6264     (forward-line -1)
6265     (gnus-group-position-point)
6266     (if (< (length out) 2) (car out) (nreverse out))))
6267
6268 (defun gnus-group-kill-level (level)
6269   "Kill all groups that is on a certain LEVEL."
6270   (interactive "nKill all groups on level: ")
6271   (cond
6272    ((= level gnus-level-zombie)
6273     (setq gnus-killed-list
6274           (nconc gnus-zombie-list gnus-killed-list))
6275     (setq gnus-zombie-list nil))
6276    ((and (< level gnus-level-zombie)
6277          (> level 0)
6278          (or gnus-expert-user
6279              (gnus-yes-or-no-p
6280               (format
6281                "Do you really want to kill all groups on level %d? "
6282                level))))
6283     (let* ((prev gnus-newsrc-alist)
6284            (alist (cdr prev)))
6285       (while alist
6286         (if (= (gnus-info-level level) level)
6287             (setcdr prev (cdr alist))
6288           (setq prev alist))
6289         (setq alist (cdr alist)))
6290       (gnus-make-hashtable-from-newsrc-alist)
6291       (gnus-group-list-groups)))
6292    (t
6293     (error "Can't kill; illegal level: %d" level))))
6294
6295 (defun gnus-group-list-all-groups (&optional arg)
6296   "List all newsgroups with level ARG or lower.
6297 Default is gnus-level-unsubscribed, which lists all subscribed and most
6298 unsubscribed groups."
6299   (interactive "P")
6300   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6301
6302 ;; Redefine this to list ALL killed groups if prefix arg used.
6303 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6304 (defun gnus-group-list-killed (&optional arg)
6305   "List all killed newsgroups in the group buffer.
6306 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6307 entail asking the server for the groups."
6308   (interactive "P")
6309   ;; Find all possible killed newsgroups if arg.
6310   (when arg
6311     (gnus-get-killed-groups))
6312   (if (not gnus-killed-list)
6313       (gnus-message 6 "No killed groups")
6314     (let (gnus-group-list-mode)
6315       (funcall gnus-group-prepare-function
6316                gnus-level-killed t gnus-level-killed))
6317     (goto-char (point-min)))
6318   (gnus-group-position-point))
6319
6320 (defun gnus-group-list-zombies ()
6321   "List all zombie newsgroups in the group buffer."
6322   (interactive)
6323   (if (not gnus-zombie-list)
6324       (gnus-message 6 "No zombie groups")
6325     (let (gnus-group-list-mode)
6326       (funcall gnus-group-prepare-function
6327                gnus-level-zombie t gnus-level-zombie))
6328     (goto-char (point-min)))
6329   (gnus-group-position-point))
6330
6331 (defun gnus-group-list-active ()
6332   "List all groups that are available from the server(s)."
6333   (interactive)
6334   ;; First we make sure that we have really read the active file.
6335   (unless (gnus-read-active-file-p)
6336     (let ((gnus-read-active-file t))
6337       (gnus-read-active-file)))
6338   ;; Find all groups and sort them.
6339   (let ((groups
6340          (sort
6341           (let (list)
6342             (mapatoms
6343              (lambda (sym)
6344                (and (symbol-value sym)
6345                     (setq list (cons (symbol-name sym) list))))
6346              gnus-active-hashtb)
6347             list)
6348           'string<))
6349         (buffer-read-only nil))
6350     (erase-buffer)
6351     (while groups
6352       (gnus-group-insert-group-line-info (pop groups)))
6353     (goto-char (point-min))))
6354
6355 (defun gnus-activate-all-groups (level)
6356   "Activate absolutely all groups."
6357   (interactive (list 7))
6358   (let ((gnus-activate-level level)
6359         (gnus-activate-foreign-newsgroups level))
6360     (gnus-group-get-new-news)))
6361
6362 (defun gnus-group-get-new-news (&optional arg)
6363   "Get newly arrived articles.
6364 If ARG is a number, it specifies which levels you are interested in
6365 re-scanning.  If ARG is non-nil and not a number, this will force
6366 \"hard\" re-reading of the active files from all servers."
6367   (interactive "P")
6368   (run-hooks 'gnus-get-new-news-hook)
6369   ;; We might read in new NoCeM messages here.
6370   (when (and gnus-use-nocem 
6371              (null arg))
6372     (gnus-nocem-scan-groups))
6373   ;; If ARG is not a number, then we read the active file.
6374   (when (and arg (not (numberp arg)))
6375     (let ((gnus-read-active-file t))
6376       (gnus-read-active-file))
6377     (setq arg nil))
6378
6379   (setq arg (gnus-group-default-level arg t))
6380   (if (and gnus-read-active-file (not arg))
6381       (progn
6382         (gnus-read-active-file)
6383         (gnus-get-unread-articles arg))
6384     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6385       (gnus-get-unread-articles arg)))
6386   (run-hooks 'gnus-after-getting-new-news-hook)
6387   (gnus-group-list-groups))
6388
6389 (defun gnus-group-get-new-news-this-group (&optional n)
6390   "Check for newly arrived news in the current group (and the N-1 next groups).
6391 The difference between N and the number of newsgroup checked is returned.
6392 If N is negative, this group and the N-1 previous groups will be checked."
6393   (interactive "P")
6394   (let* ((groups (gnus-group-process-prefix n))
6395          (ret (if (numberp n) (- n (length groups)) 0))
6396          (beg (unless n (point)))
6397          group)
6398     (while (setq group (pop groups))
6399       (gnus-group-remove-mark group)
6400       (if (gnus-activate-group group 'scan)
6401           (progn
6402             (gnus-get-unread-articles-in-group
6403              (gnus-get-info group) (gnus-active group) t)
6404             (unless (gnus-virtual-group-p group)
6405               (gnus-close-group group))
6406             (gnus-group-update-group group))
6407         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6408     (when beg (goto-char beg))
6409     (when gnus-goto-next-group-when-activating
6410       (gnus-group-next-unread-group 1 t))
6411     (gnus-summary-position-point)
6412     ret))
6413
6414 (defun gnus-group-fetch-faq (group &optional faq-dir)
6415   "Fetch the FAQ for the current group."
6416   (interactive
6417    (list
6418     (gnus-group-real-name (gnus-group-group-name))
6419     (cond (current-prefix-arg
6420            (completing-read
6421             "Faq dir: " (and (listp gnus-group-faq-directory)
6422                              gnus-group-faq-directory))))))
6423   (or faq-dir
6424       (setq faq-dir (if (listp gnus-group-faq-directory)
6425                         (car gnus-group-faq-directory)
6426                       gnus-group-faq-directory)))
6427   (or group (error "No group name given"))
6428   (let ((file (concat (file-name-as-directory faq-dir)
6429                       (gnus-group-real-name group))))
6430     (if (not (file-exists-p file))
6431         (error "No such file: %s" file)
6432       (find-file file))))
6433
6434 (defun gnus-group-describe-group (force &optional group)
6435   "Display a description of the current newsgroup."
6436   (interactive (list current-prefix-arg (gnus-group-group-name)))
6437   (and force (setq gnus-description-hashtb nil))
6438   (let ((method (gnus-find-method-for-group group))
6439         desc)
6440     (or group (error "No group name given"))
6441     (and (or (and gnus-description-hashtb
6442                   ;; We check whether this group's method has been
6443                   ;; queried for a description file.
6444                   (gnus-gethash
6445                    (gnus-group-prefixed-name "" method)
6446                    gnus-description-hashtb))
6447              (setq desc (gnus-group-get-description group))
6448              (gnus-read-descriptions-file method))
6449          (gnus-message 1
6450           (or desc (gnus-gethash group gnus-description-hashtb)
6451               "No description available")))))
6452
6453 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6454 (defun gnus-group-describe-all-groups (&optional force)
6455   "Pop up a buffer with descriptions of all newsgroups."
6456   (interactive "P")
6457   (and force (setq gnus-description-hashtb nil))
6458   (if (not (or gnus-description-hashtb
6459                (gnus-read-all-descriptions-files)))
6460       (error "Couldn't request descriptions file"))
6461   (let ((buffer-read-only nil)
6462         b)
6463     (erase-buffer)
6464     (mapatoms
6465      (lambda (group)
6466        (setq b (point))
6467        (insert (format "      *: %-20s %s\n" (symbol-name group)
6468                        (symbol-value group)))
6469        (add-text-properties
6470         b (1+ b) (list 'gnus-group group
6471                        'gnus-unread t 'gnus-marked nil
6472                        'gnus-level (1+ gnus-level-subscribed))))
6473      gnus-description-hashtb)
6474     (goto-char (point-min))
6475     (gnus-group-position-point)))
6476
6477 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6478 (defun gnus-group-apropos (regexp &optional search-description)
6479   "List all newsgroups that have names that match a regexp."
6480   (interactive "sGnus apropos (regexp): ")
6481   (let ((prev "")
6482         (obuf (current-buffer))
6483         groups des)
6484     ;; Go through all newsgroups that are known to Gnus.
6485     (mapatoms
6486      (lambda (group)
6487        (and (symbol-name group)
6488             (string-match regexp (symbol-name group))
6489             (setq groups (cons (symbol-name group) groups))))
6490      gnus-active-hashtb)
6491     ;; Also go through all descriptions that are known to Gnus.
6492     (when search-description
6493       (mapatoms
6494        (lambda (group)
6495          (and (string-match regexp (symbol-value group))
6496               (gnus-active (symbol-name group))
6497               (setq groups (cons (symbol-name group) groups))))
6498        gnus-description-hashtb))
6499     (if (not groups)
6500         (gnus-message 3 "No groups matched \"%s\"." regexp)
6501       ;; Print out all the groups.
6502       (save-excursion
6503         (pop-to-buffer "*Gnus Help*")
6504         (buffer-disable-undo (current-buffer))
6505         (erase-buffer)
6506         (setq groups (sort groups 'string<))
6507         (while groups
6508           ;; Groups may be entered twice into the list of groups.
6509           (if (not (string= (car groups) prev))
6510               (progn
6511                 (insert (setq prev (car groups)) "\n")
6512                 (if (and gnus-description-hashtb
6513                          (setq des (gnus-gethash (car groups)
6514                                                  gnus-description-hashtb)))
6515                     (insert "  " des "\n"))))
6516           (setq groups (cdr groups)))
6517         (goto-char (point-min))))
6518     (pop-to-buffer obuf)))
6519
6520 (defun gnus-group-description-apropos (regexp)
6521   "List all newsgroups that have names or descriptions that match a regexp."
6522   (interactive "sGnus description apropos (regexp): ")
6523   (if (not (or gnus-description-hashtb
6524                (gnus-read-all-descriptions-files)))
6525       (error "Couldn't request descriptions file"))
6526   (gnus-group-apropos regexp t))
6527
6528 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6529 (defun gnus-group-list-matching (level regexp &optional all lowest)
6530   "List all groups with unread articles that match REGEXP.
6531 If the prefix LEVEL is non-nil, it should be a number that says which
6532 level to cut off listing groups.
6533 If ALL, also list groups with no unread articles.
6534 If LOWEST, don't list groups with level lower than LOWEST.
6535
6536 This command may read the active file."
6537   (interactive "P\nsList newsgroups matching: ")
6538   ;; First make sure active file has been read.
6539   (when (and level
6540              (>= (prefix-numeric-value level) gnus-level-killed))
6541     (gnus-get-killed-groups))
6542   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6543                            all (or lowest 1) regexp)
6544   (goto-char (point-min))
6545   (gnus-group-position-point))
6546
6547 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6548   "List all groups that match REGEXP.
6549 If the prefix LEVEL is non-nil, it should be a number that says which
6550 level to cut off listing groups.
6551 If LOWEST, don't list groups with level lower than LOWEST."
6552   (interactive "P\nsList newsgroups matching: ")
6553   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6554
6555 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6556 (defun gnus-group-save-newsrc (&optional force)
6557   "Save the Gnus startup files.
6558 If FORCE, force saving whether it is necessary or not."
6559   (interactive "P")
6560   (gnus-save-newsrc-file force))
6561
6562 (defun gnus-group-restart (&optional arg)
6563   "Force Gnus to read the .newsrc file."
6564   (interactive "P")
6565   (when (gnus-yes-or-no-p
6566          (format "Are you sure you want to read %s? "
6567                  gnus-current-startup-file))
6568     (gnus-save-newsrc-file)
6569     (gnus-setup-news 'force)
6570     (gnus-group-list-groups arg)))
6571
6572 (defun gnus-group-read-init-file ()
6573   "Read the Gnus elisp init file."
6574   (interactive)
6575   (gnus-read-init-file))
6576
6577 (defun gnus-group-check-bogus-groups (&optional silent)
6578   "Check bogus newsgroups.
6579 If given a prefix, don't ask for confirmation before removing a bogus
6580 group."
6581   (interactive "P")
6582   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6583   (gnus-group-list-groups))
6584
6585 (defun gnus-group-edit-global-kill (&optional article group)
6586   "Edit the global kill file.
6587 If GROUP, edit that local kill file instead."
6588   (interactive "P")
6589   (setq gnus-current-kill-article article)
6590   (gnus-kill-file-edit-file group)
6591   (gnus-message
6592    6
6593    (substitute-command-keys
6594     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6595             (if group "local" "global")))))
6596
6597 (defun gnus-group-edit-local-kill (article group)
6598   "Edit a local kill file."
6599   (interactive (list nil (gnus-group-group-name)))
6600   (gnus-group-edit-global-kill article group))
6601
6602 (defun gnus-group-force-update ()
6603   "Update `.newsrc' file."
6604   (interactive)
6605   (gnus-save-newsrc-file))
6606
6607 (defun gnus-group-suspend ()
6608   "Suspend the current Gnus session.
6609 In fact, cleanup buffers except for group mode buffer.
6610 The hook gnus-suspend-gnus-hook is called before actually suspending."
6611   (interactive)
6612   (run-hooks 'gnus-suspend-gnus-hook)
6613   ;; Kill Gnus buffers except for group mode buffer.
6614   (let ((group-buf (get-buffer gnus-group-buffer)))
6615     ;; Do this on a separate list in case the user does a ^G before we finish
6616     (let ((gnus-buffer-list
6617            (delq group-buf (delq gnus-dribble-buffer
6618                                  (append gnus-buffer-list nil)))))
6619       (while gnus-buffer-list
6620         (gnus-kill-buffer (car gnus-buffer-list))
6621         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6622     (if group-buf
6623         (progn
6624           (setq gnus-buffer-list (list group-buf))
6625           (bury-buffer group-buf)
6626           (delete-windows-on group-buf t)))))
6627
6628 (defun gnus-group-clear-dribble ()
6629   "Clear all information from the dribble buffer."
6630   (interactive)
6631   (gnus-dribble-clear)
6632   (gnus-message 7 "Cleared dribble buffer"))
6633
6634 (defun gnus-group-exit ()
6635   "Quit reading news after updating .newsrc.eld and .newsrc.
6636 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6637   (interactive)
6638   (when 
6639       (or noninteractive                ;For gnus-batch-kill
6640           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6641           (not gnus-interactive-exit)   ;Without confirmation
6642           gnus-expert-user
6643           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6644     (run-hooks 'gnus-exit-gnus-hook)
6645     ;; Offer to save data from non-quitted summary buffers.
6646     (gnus-offer-save-summaries)
6647     ;; Save the newsrc file(s).
6648     (gnus-save-newsrc-file)
6649     ;; Kill-em-all.
6650     (gnus-close-backends)
6651     ;; Reset everything.
6652     (gnus-clear-system)
6653     ;; Allow the user to do things after cleaning up.
6654     (run-hooks 'gnus-after-exiting-gnus-hook)))
6655
6656 (defun gnus-close-backends ()
6657   ;; Send a close request to all backends that support such a request.
6658   (let ((methods gnus-valid-select-methods)
6659         func)
6660     (while methods
6661       (if (fboundp (setq func (intern (concat (caar methods)
6662                                               "-request-close"))))
6663           (funcall func))
6664       (setq methods (cdr methods)))))
6665
6666 (defun gnus-group-quit ()
6667   "Quit reading news without updating .newsrc.eld or .newsrc.
6668 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6669   (interactive)
6670   (when (or noninteractive              ;For gnus-batch-kill
6671             (zerop (buffer-size))
6672             (not (gnus-server-opened gnus-select-method))
6673             gnus-expert-user
6674             (not gnus-current-startup-file)
6675             (gnus-yes-or-no-p
6676              (format "Quit reading news without saving %s? "
6677                      (file-name-nondirectory gnus-current-startup-file))))
6678     (run-hooks 'gnus-exit-gnus-hook)
6679     (if gnus-use-full-window
6680         (delete-other-windows)
6681       (gnus-remove-some-windows))
6682     (gnus-dribble-save)
6683     (gnus-close-backends)
6684     (gnus-clear-system)
6685     ;; Allow the user to do things after cleaning up.
6686     (run-hooks 'gnus-after-exiting-gnus-hook)))
6687
6688 (defun gnus-offer-save-summaries ()
6689   "Offer to save all active summary buffers."
6690   (save-excursion
6691     (let ((buflist (buffer-list))
6692           buffers bufname)
6693       ;; Go through all buffers and find all summaries.
6694       (while buflist
6695         (and (setq bufname (buffer-name (car buflist)))
6696              (string-match "Summary" bufname)
6697              (save-excursion
6698                (set-buffer bufname)
6699                ;; We check that this is, indeed, a summary buffer.
6700                (and (eq major-mode 'gnus-summary-mode)
6701                     ;; Also make sure this isn't bogus.
6702                     gnus-newsgroup-prepared))
6703              (push bufname buffers))
6704         (setq buflist (cdr buflist)))
6705       ;; Go through all these summary buffers and offer to save them.
6706       (when buffers
6707         (map-y-or-n-p
6708          "Update summary buffer %s? "
6709          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6710          buffers)))))
6711
6712 (defun gnus-group-describe-briefly ()
6713   "Give a one line description of the group mode commands."
6714   (interactive)
6715   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
6716
6717 (defun gnus-group-browse-foreign-server (method)
6718   "Browse a foreign news server.
6719 If called interactively, this function will ask for a select method
6720  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6721 If not, METHOD should be a list where the first element is the method
6722 and the second element is the address."
6723   (interactive
6724    (list (let ((how (completing-read
6725                      "Which backend: "
6726                      (append gnus-valid-select-methods gnus-server-alist)
6727                      nil t (cons "nntp" 0) 'gnus-method-history)))
6728            ;; We either got a backend name or a virtual server name.
6729            ;; If the first, we also need an address.
6730            (if (assoc how gnus-valid-select-methods)
6731                (list (intern how)
6732                      ;; Suggested by mapjph@bath.ac.uk.
6733                      (completing-read
6734                       "Address: "
6735                       (mapcar (lambda (server) (list server))
6736                               gnus-secondary-servers)))
6737              ;; We got a server name, so we find the method.
6738              (gnus-server-to-method how)))))
6739   (gnus-browse-foreign-server method))
6740
6741 \f
6742 ;;;
6743 ;;; Gnus summary mode
6744 ;;;
6745
6746 (defvar gnus-summary-mode-map nil)
6747
6748 (put 'gnus-summary-mode 'mode-class 'special)
6749
6750 (unless gnus-summary-mode-map
6751   (setq gnus-summary-mode-map (make-keymap))
6752   (suppress-keymap gnus-summary-mode-map)
6753
6754   ;; Non-orthogonal keys
6755
6756   (gnus-define-keys gnus-summary-mode-map
6757     " " gnus-summary-next-page
6758     "\177" gnus-summary-prev-page
6759     [delete] gnus-summary-prev-page
6760     "\r" gnus-summary-scroll-up
6761     "n" gnus-summary-next-unread-article
6762     "p" gnus-summary-prev-unread-article
6763     "N" gnus-summary-next-article
6764     "P" gnus-summary-prev-article
6765     "\M-\C-n" gnus-summary-next-same-subject
6766     "\M-\C-p" gnus-summary-prev-same-subject
6767     "\M-n" gnus-summary-next-unread-subject
6768     "\M-p" gnus-summary-prev-unread-subject
6769     "." gnus-summary-first-unread-article
6770     "," gnus-summary-best-unread-article
6771     "\M-s" gnus-summary-search-article-forward
6772     "\M-r" gnus-summary-search-article-backward
6773     "<" gnus-summary-beginning-of-article
6774     ">" gnus-summary-end-of-article
6775     "j" gnus-summary-goto-article
6776     "^" gnus-summary-refer-parent-article
6777     "\M-^" gnus-summary-refer-article
6778     "u" gnus-summary-tick-article-forward
6779     "!" gnus-summary-tick-article-forward
6780     "U" gnus-summary-tick-article-backward
6781     "d" gnus-summary-mark-as-read-forward
6782     "D" gnus-summary-mark-as-read-backward
6783     "E" gnus-summary-mark-as-expirable
6784     "\M-u" gnus-summary-clear-mark-forward
6785     "\M-U" gnus-summary-clear-mark-backward
6786     "k" gnus-summary-kill-same-subject-and-select
6787     "\C-k" gnus-summary-kill-same-subject
6788     "\M-\C-k" gnus-summary-kill-thread
6789     "\M-\C-l" gnus-summary-lower-thread
6790     "e" gnus-summary-edit-article
6791     "#" gnus-summary-mark-as-processable
6792     "\M-#" gnus-summary-unmark-as-processable
6793     "\M-\C-t" gnus-summary-toggle-threads
6794     "\M-\C-s" gnus-summary-show-thread
6795     "\M-\C-h" gnus-summary-hide-thread
6796     "\M-\C-f" gnus-summary-next-thread
6797     "\M-\C-b" gnus-summary-prev-thread
6798     "\M-\C-u" gnus-summary-up-thread
6799     "\M-\C-d" gnus-summary-down-thread
6800     "&" gnus-summary-execute-command
6801     "c" gnus-summary-catchup-and-exit
6802     "\C-w" gnus-summary-mark-region-as-read
6803     "\C-t" gnus-summary-toggle-truncation
6804     "?" gnus-summary-mark-as-dormant
6805     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6806     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6807     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6808     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6809     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6810     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6811     "=" gnus-summary-expand-window
6812     "\C-x\C-s" gnus-summary-reselect-current-group
6813     "\M-g" gnus-summary-rescan-group
6814     "w" gnus-summary-stop-page-breaking
6815     "\C-c\C-r" gnus-summary-caesar-message
6816     "\M-t" gnus-summary-toggle-mime
6817     "f" gnus-summary-followup
6818     "F" gnus-summary-followup-with-original
6819     "C" gnus-summary-cancel-article
6820     "r" gnus-summary-reply
6821     "R" gnus-summary-reply-with-original
6822     "\C-c\C-f" gnus-summary-mail-forward
6823     "o" gnus-summary-save-article
6824     "\C-o" gnus-summary-save-article-mail
6825     "|" gnus-summary-pipe-output
6826     "\M-k" gnus-summary-edit-local-kill
6827     "\M-K" gnus-summary-edit-global-kill
6828     "V" gnus-version
6829     "\C-c\C-d" gnus-summary-describe-group
6830     "q" gnus-summary-exit
6831     "Q" gnus-summary-exit-no-update
6832     "\C-c\C-i" gnus-info-find-node
6833     gnus-mouse-2 gnus-mouse-pick-article
6834     "m" gnus-summary-mail-other-window
6835     "a" gnus-summary-post-news
6836     "x" gnus-summary-limit-to-unread
6837     "s" gnus-summary-isearch-article
6838     "t" gnus-article-hide-headers
6839     "g" gnus-summary-show-article
6840     "l" gnus-summary-goto-last-article
6841     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6842     "\C-d" gnus-summary-enter-digest-group
6843     "\C-c\C-b" gnus-bug
6844     "*" gnus-cache-enter-article
6845     "\M-*" gnus-cache-remove-article
6846     "\M-&" gnus-summary-universal-argument
6847     "\C-l" gnus-recenter
6848     "I" gnus-summary-increase-score
6849     "L" gnus-summary-lower-score
6850
6851     "V" gnus-summary-score-map
6852     "X" gnus-uu-extract-map
6853     "S" gnus-summary-send-map)
6854
6855   ;; Sort of orthogonal keymap
6856   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6857     "t" gnus-summary-tick-article-forward
6858     "!" gnus-summary-tick-article-forward
6859     "d" gnus-summary-mark-as-read-forward
6860     "r" gnus-summary-mark-as-read-forward
6861     "c" gnus-summary-clear-mark-forward
6862     " " gnus-summary-clear-mark-forward
6863     "e" gnus-summary-mark-as-expirable
6864     "x" gnus-summary-mark-as-expirable
6865     "?" gnus-summary-mark-as-dormant
6866     "b" gnus-summary-set-bookmark
6867     "B" gnus-summary-remove-bookmark
6868     "#" gnus-summary-mark-as-processable
6869     "\M-#" gnus-summary-unmark-as-processable
6870     "S" gnus-summary-limit-include-expunged
6871     "C" gnus-summary-catchup
6872     "H" gnus-summary-catchup-to-here
6873     "\C-c" gnus-summary-catchup-all
6874     "k" gnus-summary-kill-same-subject-and-select
6875     "K" gnus-summary-kill-same-subject
6876     "P" gnus-uu-mark-map)
6877
6878   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6879     "c" gnus-summary-clear-above
6880     "u" gnus-summary-tick-above
6881     "m" gnus-summary-mark-above
6882     "k" gnus-summary-kill-below)
6883
6884   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6885     "/" gnus-summary-limit-to-subject
6886     "n" gnus-summary-limit-to-articles
6887     "w" gnus-summary-pop-limit
6888     "s" gnus-summary-limit-to-subject
6889     "a" gnus-summary-limit-to-author
6890     "u" gnus-summary-limit-to-unread
6891     "m" gnus-summary-limit-to-marks
6892     "v" gnus-summary-limit-to-score
6893     "D" gnus-summary-limit-include-dormant
6894     "d" gnus-summary-limit-exclude-dormant
6895     ;;  "t" gnus-summary-limit-exclude-thread
6896     "E" gnus-summary-limit-include-expunged
6897     "c" gnus-summary-limit-exclude-childless-dormant
6898     "C" gnus-summary-limit-mark-excluded-as-read)
6899
6900   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6901     "n" gnus-summary-next-unread-article
6902     "p" gnus-summary-prev-unread-article
6903     "N" gnus-summary-next-article
6904     "P" gnus-summary-prev-article
6905     "\C-n" gnus-summary-next-same-subject
6906     "\C-p" gnus-summary-prev-same-subject
6907     "\M-n" gnus-summary-next-unread-subject
6908     "\M-p" gnus-summary-prev-unread-subject
6909     "f" gnus-summary-first-unread-article
6910     "b" gnus-summary-best-unread-article
6911     "j" gnus-summary-goto-article
6912     "g" gnus-summary-goto-subject
6913     "l" gnus-summary-goto-last-article
6914     "p" gnus-summary-pop-article)
6915
6916   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6917     "k" gnus-summary-kill-thread
6918     "l" gnus-summary-lower-thread
6919     "i" gnus-summary-raise-thread
6920     "T" gnus-summary-toggle-threads
6921     "t" gnus-summary-rethread-current
6922     "^" gnus-summary-reparent-thread
6923     "s" gnus-summary-show-thread
6924     "S" gnus-summary-show-all-threads
6925     "h" gnus-summary-hide-thread
6926     "H" gnus-summary-hide-all-threads
6927     "n" gnus-summary-next-thread
6928     "p" gnus-summary-prev-thread
6929     "u" gnus-summary-up-thread
6930     "o" gnus-summary-top-thread
6931     "d" gnus-summary-down-thread
6932     "#" gnus-uu-mark-thread
6933     "\M-#" gnus-uu-unmark-thread)
6934
6935   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6936     "c" gnus-summary-catchup-and-exit
6937     "C" gnus-summary-catchup-all-and-exit
6938     "E" gnus-summary-exit-no-update
6939     "Q" gnus-summary-exit
6940     "Z" gnus-summary-exit
6941     "n" gnus-summary-catchup-and-goto-next-group
6942     "R" gnus-summary-reselect-current-group
6943     "G" gnus-summary-rescan-group
6944     "N" gnus-summary-next-group
6945     "P" gnus-summary-prev-group)
6946
6947   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6948     " " gnus-summary-next-page
6949     "n" gnus-summary-next-page
6950     "\177" gnus-summary-prev-page
6951     [delete] gnus-summary-prev-page
6952     "p" gnus-summary-prev-page
6953     "\r" gnus-summary-scroll-up
6954     "<" gnus-summary-beginning-of-article
6955     ">" gnus-summary-end-of-article
6956     "b" gnus-summary-beginning-of-article
6957     "e" gnus-summary-end-of-article
6958     "^" gnus-summary-refer-parent-article
6959     "r" gnus-summary-refer-parent-article
6960     "R" gnus-summary-refer-references
6961     "g" gnus-summary-show-article
6962     "s" gnus-summary-isearch-article)
6963
6964   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6965     "b" gnus-article-add-buttons
6966     "B" gnus-article-add-buttons-to-head
6967     "o" gnus-article-treat-overstrike
6968     ;;  "w" gnus-article-word-wrap
6969     "w" gnus-article-fill-cited-article
6970     "c" gnus-article-remove-cr
6971     "L" gnus-article-remove-trailing-blank-lines
6972     "q" gnus-article-de-quoted-unreadable
6973     "f" gnus-article-display-x-face
6974     "l" gnus-summary-stop-page-breaking
6975     "r" gnus-summary-caesar-message
6976     "t" gnus-article-hide-headers
6977     "v" gnus-summary-verbose-headers
6978     "m" gnus-summary-toggle-mime)
6979
6980   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6981     "a" gnus-article-hide
6982     "h" gnus-article-hide-headers
6983     "b" gnus-article-hide-boring-headers
6984     "s" gnus-article-hide-signature
6985     "c" gnus-article-hide-citation
6986     "p" gnus-article-hide-pgp
6987     "\C-c" gnus-article-hide-citation-maybe)
6988
6989   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6990     "a" gnus-article-highlight
6991     "h" gnus-article-highlight-headers
6992     "c" gnus-article-highlight-citation
6993     "s" gnus-article-highlight-signature)
6994
6995   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6996     "z" gnus-article-date-ut
6997     "u" gnus-article-date-ut
6998     "l" gnus-article-date-local
6999     "e" gnus-article-date-lapsed
7000     "o" gnus-article-date-original)
7001
7002   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7003     "v" gnus-version
7004     "f" gnus-summary-fetch-faq
7005     "d" gnus-summary-describe-group
7006     "h" gnus-summary-describe-briefly
7007     "i" gnus-info-find-node)
7008
7009   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7010     "e" gnus-summary-expire-articles
7011     "\M-\C-e" gnus-summary-expire-articles-now
7012     "\177" gnus-summary-delete-article
7013     [delete] gnus-summary-delete-article
7014     "m" gnus-summary-move-article
7015     "r" gnus-summary-respool-article
7016     "w" gnus-summary-edit-article
7017     "c" gnus-summary-copy-article
7018     "B" gnus-summary-crosspost-article
7019     "q" gnus-summary-respool-query
7020     "i" gnus-summary-import-article)
7021
7022   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7023     "o" gnus-summary-save-article
7024     "m" gnus-summary-save-article-mail
7025     "r" gnus-summary-save-article-rmail
7026     "f" gnus-summary-save-article-file
7027     "b" gnus-summary-save-article-body-file
7028     "h" gnus-summary-save-article-folder
7029     "v" gnus-summary-save-article-vm
7030     "p" gnus-summary-pipe-output
7031     "s" gnus-soup-add-article)
7032   )
7033
7034 \f
7035
7036 (defun gnus-summary-mode (&optional group)
7037   "Major mode for reading articles.
7038
7039 All normal editing commands are switched off.
7040 \\<gnus-summary-mode-map>
7041 Each line in this buffer represents one article.  To read an
7042 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7043 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7044 respectively.
7045
7046 You can also post articles and send mail from this buffer.  To
7047 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7048 of an article, type `\\[gnus-summary-reply]'.
7049
7050 There are approx. one gazillion commands you can execute in this
7051 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7052
7053 The following commands are available:
7054
7055 \\{gnus-summary-mode-map}"
7056   (interactive)
7057   (when (and menu-bar-mode
7058              (gnus-visual-p 'summary-menu 'menu))
7059     (gnus-summary-make-menu-bar))
7060   (kill-all-local-variables)
7061   (let ((locals gnus-summary-local-variables))
7062     (while locals
7063       (if (consp (car locals))
7064           (progn
7065             (make-local-variable (caar locals))
7066             (set (caar locals) (eval (cdar locals))))
7067         (make-local-variable (car locals))
7068         (set (car locals) nil))
7069       (setq locals (cdr locals))))
7070   (gnus-make-thread-indent-array)
7071   (gnus-simplify-mode-line)
7072   (setq major-mode 'gnus-summary-mode)
7073   (setq mode-name "Summary")
7074   (make-local-variable 'minor-mode-alist)
7075   (use-local-map gnus-summary-mode-map)
7076   (buffer-disable-undo (current-buffer))
7077   (setq buffer-read-only t)             ;Disable modification
7078   (setq truncate-lines t)
7079   (setq selective-display t)
7080   (setq selective-display-ellipses t)   ;Display `...'
7081   (setq buffer-display-table gnus-summary-display-table)
7082   (setq gnus-newsgroup-name group)
7083   (make-local-variable 'gnus-summary-line-format)
7084   (make-local-variable 'gnus-summary-line-format-spec)
7085   (make-local-variable 'gnus-summary-mark-positions)
7086   (run-hooks 'gnus-summary-mode-hook))
7087
7088 (defun gnus-summary-make-display-table ()
7089   ;; Change the display table.  Odd characters have a tendency to mess
7090   ;; up nicely formatted displays - we make all possible glyphs
7091   ;; display only a single character.
7092
7093   ;; We start from the standard display table, if any.
7094   (setq gnus-summary-display-table
7095         (or (copy-sequence standard-display-table)
7096             (make-display-table)))
7097   ;; Nix out all the control chars...
7098   (let ((i 32))
7099     (while (>= (setq i (1- i)) 0)
7100       (aset gnus-summary-display-table i [??])))
7101   ;; ... but not newline and cr, of course. (cr is necessary for the
7102   ;; selective display).
7103   (aset gnus-summary-display-table ?\n nil)
7104   (aset gnus-summary-display-table ?\r nil)
7105   ;; We nix out any glyphs over 126 that are not set already.
7106   (let ((i 256))
7107     (while (>= (setq i (1- i)) 127)
7108       ;; Only modify if the entry is nil.
7109       (or (aref gnus-summary-display-table i)
7110           (aset gnus-summary-display-table i [??])))))
7111
7112 (defun gnus-summary-clear-local-variables ()
7113   (let ((locals gnus-summary-local-variables))
7114     (while locals
7115       (if (consp (car locals))
7116           (and (vectorp (caar locals))
7117                (set (caar locals) nil))
7118         (and (vectorp (car locals))
7119              (set (car locals) nil)))
7120       (setq locals (cdr locals)))))
7121
7122 ;; Summary data functions.
7123
7124 (defmacro gnus-data-number (data)
7125   `(car ,data))
7126
7127 (defmacro gnus-data-set-number (data number)
7128   `(setcar ,data ,number))
7129
7130 (defmacro gnus-data-mark (data)
7131   `(nth 1 ,data))
7132
7133 (defmacro gnus-data-set-mark (data mark)
7134   `(setcar (nthcdr 1 ,data) ,mark))
7135
7136 (defmacro gnus-data-pos (data)
7137   `(nth 2 ,data))
7138
7139 (defmacro gnus-data-set-pos (data pos)
7140   `(setcar (nthcdr 2 ,data) ,pos))
7141
7142 (defmacro gnus-data-header (data)
7143   `(nth 3 ,data))
7144
7145 (defmacro gnus-data-level (data)
7146   `(nth 4 ,data))
7147
7148 (defmacro gnus-data-unread-p (data)
7149   `(= (nth 1 ,data) gnus-unread-mark))
7150
7151 (defmacro gnus-data-pseudo-p (data)
7152   `(consp (nth 3 ,data)))
7153
7154 (defmacro gnus-data-find (number)
7155   `(assq ,number gnus-newsgroup-data))
7156
7157 (defmacro gnus-data-find-list (number &optional data)
7158   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7159      (memq (assq ,number bdata)
7160            bdata)))
7161
7162 (defmacro gnus-data-make (number mark pos header level)
7163   `(list ,number ,mark ,pos ,header ,level))
7164
7165 (defun gnus-data-enter (after-article number mark pos header level offset)
7166   (let ((data (gnus-data-find-list after-article)))
7167     (or data (error "No such article: %d" after-article))
7168     (setcdr data (cons (gnus-data-make number mark pos header level)
7169                        (cdr data)))
7170     (setq gnus-newsgroup-data-reverse nil)
7171     (gnus-data-update-list (cddr data) offset)))
7172
7173 (defun gnus-data-enter-list (after-article list &optional offset)
7174   (when list
7175     (let ((data (and after-article (gnus-data-find-list after-article)))
7176           (ilist list))
7177       (or data (not after-article) (error "No such article: %d" after-article))
7178       ;; Find the last element in the list to be spliced into the main
7179       ;; list.
7180       (while (cdr list)
7181         (setq list (cdr list)))
7182       (if (not data)
7183           (progn
7184             (setcdr list gnus-newsgroup-data)
7185             (setq gnus-newsgroup-data ilist)
7186             (and offset (gnus-data-update-list (cdr list) offset)))
7187         (setcdr list (cdr data))
7188         (setcdr data ilist)
7189         (and offset (gnus-data-update-list (cdr data) offset)))
7190       (setq gnus-newsgroup-data-reverse nil))))
7191
7192 (defun gnus-data-remove (article &optional offset)
7193   (let ((data gnus-newsgroup-data))
7194     (if (= (gnus-data-number (car data)) article)
7195         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7196               gnus-newsgroup-data-reverse nil)
7197       (while (cdr data)
7198         (and (= (gnus-data-number (cadr data)) article)
7199              (progn
7200                (setcdr data (cddr data))
7201                (and offset (gnus-data-update-list (cdr data) offset))
7202                (setq data nil
7203                      gnus-newsgroup-data-reverse nil)))
7204         (setq data (cdr data))))))
7205
7206 (defmacro gnus-data-list (backward)
7207   `(if ,backward
7208        (or gnus-newsgroup-data-reverse
7209            (setq gnus-newsgroup-data-reverse
7210                  (reverse gnus-newsgroup-data)))
7211      gnus-newsgroup-data))
7212
7213 (defun gnus-data-update-list (data offset)
7214   "Add OFFSET to the POS of all data entries in DATA."
7215   (while data
7216     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7217     (setq data (cdr data))))
7218
7219 (defun gnus-data-compute-positions ()
7220   "Compute the positions of all articles."
7221   (let ((data gnus-newsgroup-data)
7222         pos)
7223     (while data
7224       (when (setq pos (text-property-any
7225                        (point-min) (point-max)
7226                        'gnus-number (gnus-data-number (car data))))
7227         (gnus-data-set-pos (car data) (+ pos 3)))
7228       (setq data (cdr data)))))
7229
7230 (defun gnus-summary-article-pseudo-p (article)
7231   "Say whether this article is a pseudo article or not."
7232   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7233
7234 (defun gnus-article-parent-p (number)
7235   "Say whether this article is a parent or not."
7236   (let ((data (gnus-data-find-list number)))
7237     (and (cdr data)                     ; There has to be an article after...
7238          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7239             (gnus-data-level (nth 1 data))))))
7240
7241 (defun gnus-article-children (number)
7242   "Return a list of all children to NUMBER."
7243   (let* ((data (gnus-data-find-list number))
7244          (level (gnus-data-level (car data)))
7245          children)
7246     (setq data (cdr data))
7247     (while (and data            
7248                 (= (gnus-data-level (car data)) (1+ level)))
7249       (push (gnus-data-number (car data)) children)
7250       (setq data (cdr data)))
7251     children))
7252
7253 (defmacro gnus-summary-skip-intangible ()
7254   "If the current article is intangible, then jump to a different article."
7255   '(let ((to (get-text-property (point) 'gnus-intangible)))
7256     (and to (gnus-summary-goto-subject to))))
7257
7258 (defmacro gnus-summary-article-intangible-p ()
7259   "Say whether this article is intangible or not."
7260   '(get-text-property (point) 'gnus-intangible))
7261
7262 ;; Some summary mode macros.
7263
7264 (defmacro gnus-summary-article-number ()
7265   "The article number of the article on the current line.
7266 If there isn's an article number here, then we return the current
7267 article number."
7268   '(progn
7269      (gnus-summary-skip-intangible)
7270      (or (get-text-property (point) 'gnus-number)
7271          (gnus-summary-last-subject))))
7272
7273 (defmacro gnus-summary-article-header (&optional number)
7274   `(gnus-data-header (gnus-data-find
7275                       ,(or number '(gnus-summary-article-number)))))
7276
7277 (defmacro gnus-summary-thread-level (&optional number)
7278   `(if (and (eq gnus-summary-make-false-root 'dummy)
7279             (get-text-property (point) 'gnus-intangible))
7280        0
7281      (gnus-data-level (gnus-data-find
7282                        ,(or number '(gnus-summary-article-number))))))
7283
7284 (defmacro gnus-summary-article-mark (&optional number)
7285   `(gnus-data-mark (gnus-data-find
7286                     ,(or number '(gnus-summary-article-number)))))
7287
7288 (defmacro gnus-summary-article-pos (&optional number)
7289   `(gnus-data-pos (gnus-data-find
7290                    ,(or number '(gnus-summary-article-number)))))
7291
7292 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7293 (defmacro gnus-summary-article-subject (&optional number)
7294   "Return current subject string or nil if nothing."
7295   `(let ((headers
7296           ,(if number
7297                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7298              '(gnus-data-header (assq (gnus-summary-article-number)
7299                                       gnus-newsgroup-data)))))
7300      (and headers
7301           (vectorp headers)
7302           (mail-header-subject headers))))
7303
7304 (defmacro gnus-summary-article-score (&optional number)
7305   "Return current article score."
7306   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7307                   gnus-newsgroup-scored))
7308        gnus-summary-default-score 0))
7309
7310 (defun gnus-summary-article-children (&optional number)
7311   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7312          (level (gnus-data-level (car data)))
7313          l children)
7314     (while (and (setq data (cdr data))
7315                 (> (setq l (gnus-data-level (car data))) level))
7316       (and (= (1+ level) l)
7317            (setq children (cons (gnus-data-number (car data))
7318                                 children))))
7319     (nreverse children)))
7320
7321 (defun gnus-summary-article-parent (&optional number)
7322   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7323                                     (gnus-data-list t)))
7324          (level (gnus-data-level (car data))))
7325     (if (zerop level)
7326         () ; This is a root.
7327       ;; We search until we find an article with a level less than
7328       ;; this one.  That function has to be the parent.
7329       (while (and (setq data (cdr data))
7330                   (not (< (gnus-data-level (car data)) level))))
7331       (and data (gnus-data-number (car data))))))
7332
7333 (defun gnus-unread-mark-p (mark)
7334   "Say whether MARK is the unread mark."
7335   (= mark gnus-unread-mark))
7336
7337 (defun gnus-read-mark-p (mark)
7338   "Say whether MARK is one of the marks that mark as read.
7339 This is all marks except unread, ticked, dormant, and expirable."
7340   (not (or (= mark gnus-unread-mark)
7341            (= mark gnus-ticked-mark)
7342            (= mark gnus-dormant-mark)
7343            (= mark gnus-expirable-mark))))
7344
7345 ;; Various summary mode internalish functions.
7346
7347 (defun gnus-mouse-pick-article (e)
7348   (interactive "e")
7349   (mouse-set-point e)
7350   (gnus-summary-next-page nil t))
7351
7352 (defun gnus-summary-setup-buffer (group)
7353   "Initialize summary buffer."
7354   (let ((buffer (concat "*Summary " group "*")))
7355     (if (get-buffer buffer)
7356         (progn
7357           (set-buffer buffer)
7358           (setq gnus-summary-buffer (current-buffer))
7359           (not gnus-newsgroup-prepared))
7360       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7361       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7362       (gnus-add-current-to-buffer-list)
7363       (gnus-summary-mode group)
7364       (when gnus-carpal
7365         (gnus-carpal-setup-buffer 'summary))
7366       (unless gnus-single-article-buffer
7367         (make-local-variable 'gnus-article-buffer)
7368         (make-local-variable 'gnus-article-current)
7369         (make-local-variable 'gnus-original-article-buffer))
7370       (setq gnus-newsgroup-name group)
7371       t)))
7372
7373 (defun gnus-set-global-variables ()
7374   ;; Set the global equivalents of the summary buffer-local variables
7375   ;; to the latest values they had.  These reflect the summary buffer
7376   ;; that was in action when the last article was fetched.
7377   (when (eq major-mode 'gnus-summary-mode)
7378     (setq gnus-summary-buffer (current-buffer))
7379     (let ((name gnus-newsgroup-name)
7380           (marked gnus-newsgroup-marked)
7381           (unread gnus-newsgroup-unreads)
7382           (headers gnus-current-headers)
7383           (data gnus-newsgroup-data)
7384           (summary gnus-summary-buffer)
7385           (article-buffer gnus-article-buffer)
7386           (original gnus-original-article-buffer)
7387           (gac gnus-article-current)
7388           (score-file gnus-current-score-file))
7389       (save-excursion
7390         (set-buffer gnus-group-buffer)
7391         (setq gnus-newsgroup-name name)
7392         (setq gnus-newsgroup-marked marked)
7393         (setq gnus-newsgroup-unreads unread)
7394         (setq gnus-current-headers headers)
7395         (setq gnus-newsgroup-data data)
7396         (setq gnus-article-current gac)
7397         (setq gnus-summary-buffer summary)
7398         (setq gnus-article-buffer article-buffer)
7399         (setq gnus-original-article-buffer original)
7400         (setq gnus-current-score-file score-file)))))
7401
7402 (defun gnus-summary-last-article-p (&optional article)
7403   "Return whether ARTICLE is the last article in the buffer."
7404   (if (not (setq article (or article (gnus-summary-article-number))))
7405       t ; All non-existant numbers are the last article. :-)
7406     (not (cdr (gnus-data-find-list article)))))
7407
7408 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7409   "Insert a dummy root in the summary buffer."
7410   (beginning-of-line)
7411   (add-text-properties
7412    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7413    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7414
7415 (defun gnus-make-thread-indent-array ()
7416   (let ((n 200))
7417     (unless (and gnus-thread-indent-array
7418                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7419       (setq gnus-thread-indent-array (make-vector 201 "")
7420             gnus-thread-indent-array-level gnus-thread-indent-level)
7421       (while (>= n 0)
7422         (aset gnus-thread-indent-array n
7423               (make-string (* n gnus-thread-indent-level) ? ))
7424         (setq n (1- n))))))
7425
7426 (defun gnus-summary-insert-line
7427   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7428                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7429                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7430   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7431          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7432          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7433          (gnus-tmp-score-char
7434           (if (or (null gnus-summary-default-score)
7435                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7436                       gnus-summary-zcore-fuzz)) ? 
7437             (if (< gnus-tmp-score gnus-summary-default-score)
7438                 gnus-score-below-mark gnus-score-over-mark)))
7439          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7440                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7441                                   gnus-cached-mark)
7442                                  (gnus-tmp-replied gnus-replied-mark)
7443                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7444                                   gnus-saved-mark)
7445                                  (t gnus-unread-mark)))
7446          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7447          (gnus-tmp-name
7448           (cond
7449            ((string-match "(.+)" gnus-tmp-from)
7450             (substring gnus-tmp-from
7451                        (1+ (match-beginning 0)) (1- (match-end 0))))
7452            ((string-match "<[^>]+> *$" gnus-tmp-from)
7453             (let ((beg (match-beginning 0)))
7454               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7455                        (substring gnus-tmp-from (1+ (match-beginning 0))
7456                                   (1- (match-end 0))))
7457                   (substring gnus-tmp-from 0 beg))))
7458            (t gnus-tmp-from)))
7459          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7460          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7461          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7462          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7463          (buffer-read-only nil))
7464     (when (string= gnus-tmp-name "")
7465       (setq gnus-tmp-name gnus-tmp-from))
7466     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7467     (put-text-property
7468      (point)
7469      (progn (eval gnus-summary-line-format-spec) (point))
7470      'gnus-number gnus-tmp-number)
7471     (when (gnus-visual-p 'summary-highlight 'highlight)
7472       (forward-line -1)
7473       (run-hooks 'gnus-summary-update-hook)
7474       (forward-line 1))))
7475
7476 (defun gnus-summary-update-line (&optional dont-update)
7477   ;; Update summary line after change.
7478   (when (and gnus-summary-default-score
7479              (not gnus-summary-inhibit-highlight))
7480     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7481            (article (gnus-summary-article-number))
7482            (score (gnus-summary-article-score article)))
7483       (unless dont-update
7484         (if (and gnus-summary-mark-below
7485                  (< (gnus-summary-article-score)
7486                     gnus-summary-mark-below))
7487             ;; This article has a low score, so we mark it as read.
7488             (when (memq article gnus-newsgroup-unreads)
7489               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7490           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7491             ;; This article was previously marked as read on account
7492             ;; of a low score, but now it has risen, so we mark it as
7493             ;; unread.
7494             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7495         (gnus-summary-update-mark
7496          (if (or (null gnus-summary-default-score)
7497                  (<= (abs (- score gnus-summary-default-score))
7498                      gnus-summary-zcore-fuzz)) ? 
7499            (if (< score gnus-summary-default-score)
7500                gnus-score-below-mark gnus-score-over-mark)) 'score))
7501       ;; Do visual highlighting.
7502       (when (gnus-visual-p 'summary-highlight 'highlight)
7503         (run-hooks 'gnus-summary-update-hook)))))
7504
7505 (defvar gnus-tmp-new-adopts nil)
7506
7507 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7508   ;; Sum up all elements (and sub-elements) in a list.
7509   (let* ((number
7510           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7511           (cond
7512            ((and (consp thread) (cdr thread))
7513             (apply
7514              '+ 1 (mapcar
7515                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7516            ((null thread)
7517             1)
7518            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7519             1)
7520            (t 1))))
7521     (when (and level (zerop level) gnus-tmp-new-adopts)
7522       (incf number
7523             (apply '+ (mapcar
7524                        'gnus-summary-number-of-articles-in-thread
7525                        gnus-tmp-new-adopts))))
7526     (if char
7527         (if (> number 1) gnus-not-empty-thread-mark
7528           gnus-empty-thread-mark)
7529       number)))
7530
7531 (defun gnus-summary-set-local-parameters (group)
7532  "Go through the local params of GROUP and set all variable specs in that list."
7533   (let ((params (gnus-info-params (gnus-get-info group)))
7534         elem)
7535     (while params
7536       (setq elem (car params)
7537             params (cdr params))
7538       (and (consp elem)                 ; Has to be a cons.
7539            (consp (cdr elem))           ; The cdr has to be a list.
7540            (symbolp (car elem))         ; Has to be a symbol in there.
7541            (not (memq (car elem) 
7542                       '(quit-config to-address to-list to-group)))
7543            (progn                       ; So we set it.
7544              (make-local-variable (car elem))
7545              (set (car elem) (eval (nth 1 elem))))))))
7546
7547 (defun gnus-summary-read-group (group &optional show-all no-article
7548                                       kill-buffer no-display)
7549   "Start reading news in newsgroup GROUP.
7550 If SHOW-ALL is non-nil, already read articles are also listed.
7551 If NO-ARTICLE is non-nil, no article is selected initially.
7552 If NO-DISPLAY, don't generate a summary buffer."
7553   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7554   (let* ((new-group (gnus-summary-setup-buffer group))
7555          (quit-config (gnus-group-quit-config group))
7556          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7557     (cond
7558      ;; This summary buffer exists already, so we just select it.
7559      ((not new-group)
7560       (gnus-set-global-variables)
7561       (when kill-buffer
7562         (gnus-kill-or-deaden-summary kill-buffer))
7563       (gnus-configure-windows 'summary 'force)
7564       (gnus-set-mode-line 'summary)
7565       (gnus-summary-position-point)
7566       (message "")
7567       t)
7568      ;; We couldn't select this group.
7569      ((null did-select)
7570       (when (and (eq major-mode 'gnus-summary-mode)
7571                  (not (equal (current-buffer) kill-buffer)))
7572         (kill-buffer (current-buffer))
7573         (if (not quit-config)
7574             (progn
7575               (set-buffer gnus-group-buffer)
7576               (gnus-group-jump-to-group group)
7577               (gnus-group-next-unread-group 1))
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       (gnus-message 3 "Can't select group")
7585       nil)
7586      ;; The user did a `C-g' while prompting for number of articles,
7587      ;; so we exit this group.
7588      ((eq did-select 'quit)
7589       (and (eq major-mode 'gnus-summary-mode)
7590            (not (equal (current-buffer) kill-buffer))
7591            (kill-buffer (current-buffer)))
7592       (when kill-buffer
7593         (gnus-kill-or-deaden-summary kill-buffer))
7594       (if (not quit-config)
7595           (progn
7596             (set-buffer gnus-group-buffer)
7597             (gnus-group-jump-to-group group)
7598             (gnus-group-next-unread-group 1)
7599             (gnus-configure-windows 'group 'force))
7600         (if (not (buffer-name (car quit-config)))
7601             (gnus-configure-windows 'group 'force)
7602           (set-buffer (car quit-config))
7603           (and (eq major-mode 'gnus-summary-mode)
7604                (gnus-set-global-variables))
7605           (gnus-configure-windows (cdr quit-config))))
7606       ;; Finally signal the quit.
7607       (signal 'quit nil))
7608      ;; The group was successfully selected.
7609      (t
7610       (gnus-set-global-variables)
7611       ;; Save the active value in effect when the group was entered.
7612       (setq gnus-newsgroup-active
7613             (gnus-copy-sequence
7614              (gnus-active gnus-newsgroup-name)))
7615       ;; You can change the summary buffer in some way with this hook.
7616       (run-hooks 'gnus-select-group-hook)
7617       ;; Set any local variables in the group parameters.
7618       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7619       (gnus-update-format-specifications)
7620       ;; Do score processing.
7621       (when gnus-use-scoring
7622         (gnus-possibly-score-headers))
7623       ;; Check whether to fill in the gaps in the threads.
7624       (when gnus-build-sparse-threads
7625         (gnus-build-sparse-threads))
7626       ;; Find the initial limit.
7627       (if gnus-show-threads
7628           (if show-all
7629               (let ((gnus-newsgroup-dormant nil))
7630                 (gnus-summary-initial-limit show-all))
7631             (gnus-summary-initial-limit show-all))
7632         (setq gnus-newsgroup-limit 
7633               (mapcar 
7634                (lambda (header) (mail-header-number header))
7635                gnus-newsgroup-headers)))
7636       ;; Generate the summary buffer.
7637       (unless no-display
7638         (gnus-summary-prepare))
7639       (when gnus-use-trees
7640         (gnus-tree-open group)
7641         (setq gnus-summary-highlight-line-function
7642               'gnus-tree-highlight-article))
7643       ;; If the summary buffer is empty, but there are some low-scored
7644       ;; articles or some excluded dormants, we include these in the
7645       ;; buffer.
7646       (when (and (zerop (buffer-size))
7647                  (not no-display))
7648         (cond (gnus-newsgroup-dormant
7649                (gnus-summary-limit-include-dormant))
7650               ((and gnus-newsgroup-scored show-all)
7651                (gnus-summary-limit-include-expunged))))
7652       ;; Function `gnus-apply-kill-file' must be called in this hook.
7653       (run-hooks 'gnus-apply-kill-hook)
7654       (if (and (zerop (buffer-size))
7655                (not no-display))
7656           (progn
7657             ;; This newsgroup is empty.
7658             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7659             (gnus-message 6 "No unread news")
7660             (when kill-buffer
7661               (gnus-kill-or-deaden-summary kill-buffer))
7662             ;; Return nil from this function.
7663             nil)
7664         ;; Hide conversation thread subtrees.  We cannot do this in
7665         ;; gnus-summary-prepare-hook since kill processing may not
7666         ;; work with hidden articles.
7667         (and gnus-show-threads
7668              gnus-thread-hide-subtree
7669              (gnus-summary-hide-all-threads))
7670         ;; Show first unread article if requested.
7671         (if (and (not no-article)
7672                  (not no-display)
7673                  gnus-newsgroup-unreads
7674                  gnus-auto-select-first)
7675             (unless (if (eq gnus-auto-select-first 'best)
7676                         (gnus-summary-best-unread-article)
7677                       (gnus-summary-first-unread-article))
7678               (gnus-configure-windows 'summary))
7679           ;; Don't select any articles, just move point to the first
7680           ;; article in the group.
7681           (goto-char (point-min))
7682           (gnus-summary-position-point)
7683           (gnus-set-mode-line 'summary)
7684           (gnus-configure-windows 'summary 'force))
7685         ;; If we are in async mode, we send some info to the backend.
7686         (when gnus-newsgroup-async
7687           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7688         (when kill-buffer
7689           (gnus-kill-or-deaden-summary kill-buffer))
7690         (when (get-buffer-window gnus-group-buffer t)
7691           ;; Gotta use windows, because recenter does wierd stuff if
7692           ;; the current buffer ain't the displayed window.
7693           (let ((owin (selected-window)))
7694             (select-window (get-buffer-window gnus-group-buffer t))
7695             (when (gnus-group-goto-group group)
7696               (recenter))
7697             (select-window owin))))
7698       ;; Mark this buffer as "prepared".
7699       (setq gnus-newsgroup-prepared t)
7700       t))))
7701
7702 (defun gnus-summary-prepare ()
7703   "Generate the summary buffer."
7704   (let ((buffer-read-only nil))
7705     (erase-buffer)
7706     (setq gnus-newsgroup-data nil
7707           gnus-newsgroup-data-reverse nil)
7708     (run-hooks 'gnus-summary-generate-hook)
7709     ;; Generate the buffer, either with threads or without.
7710     (when gnus-newsgroup-headers
7711       (gnus-summary-prepare-threads
7712        (if gnus-show-threads
7713            (gnus-sort-gathered-threads
7714             (funcall gnus-summary-thread-gathering-function
7715                      (gnus-sort-threads
7716                       (gnus-cut-threads (gnus-make-threads)))))
7717          ;; Unthreaded display.
7718          (gnus-sort-articles gnus-newsgroup-headers))))
7719     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7720     ;; Call hooks for modifying summary buffer.
7721     (goto-char (point-min))
7722     (run-hooks 'gnus-summary-prepare-hook)))
7723
7724 (defun gnus-gather-threads-by-subject (threads)
7725   "Gather threads by looking at Subject headers."
7726   (if (not gnus-summary-make-false-root)
7727       threads
7728     (let ((hashtb (gnus-make-hashtable 1023))
7729           (prev threads)
7730           (result threads)
7731           subject hthread whole-subject)
7732       (while threads
7733         (setq whole-subject (mail-header-subject (caar threads)))
7734         (setq subject
7735               (cond
7736                ;; Truncate the subject.
7737                ((numberp gnus-summary-gather-subject-limit)
7738                 (setq subject (gnus-simplify-subject-re whole-subject))
7739                 (if (> (length subject) gnus-summary-gather-subject-limit)
7740                     (substring subject 0 gnus-summary-gather-subject-limit)
7741                   subject))
7742                ;; Fuzzily simplify it.
7743                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7744                 (gnus-simplify-subject-fuzzy whole-subject))
7745                ;; Just remove the leading "Re:".
7746                (t
7747                 (gnus-simplify-subject-re whole-subject))))
7748
7749         (if (and gnus-summary-gather-exclude-subject
7750                  (string-match gnus-summary-gather-exclude-subject
7751                                subject))
7752             ()          ; We don't want to do anything with this article.
7753           ;; We simplify the subject before looking it up in the
7754           ;; hash table.
7755
7756           (if (setq hthread (gnus-gethash subject hashtb))
7757               (progn
7758                 ;; We enter a dummy root into the thread, if we
7759                 ;; haven't done that already.
7760                 (unless (stringp (caar hthread))
7761                   (setcar hthread (list whole-subject (car hthread))))
7762                 ;; We add this new gathered thread to this gathered
7763                 ;; thread.
7764                 (setcdr (car hthread)
7765                         (nconc (cdar hthread) (list (car threads))))
7766                 ;; Remove it from the list of threads.
7767                 (setcdr prev (cdr threads))
7768                 (setq threads prev))
7769             ;; Enter this thread into the hash table.
7770             (gnus-sethash subject threads hashtb)))
7771         (setq prev threads)
7772         (setq threads (cdr threads)))
7773       result)))
7774
7775 (defun gnus-gather-threads-by-references (threads)
7776   "Gather threads by looking at References headers."
7777   (let ((idhashtb (gnus-make-hashtable 1023))
7778         (thhashtb (gnus-make-hashtable 1023))
7779         (prev threads)
7780         (result threads)
7781         ids references id gthread gid entered)
7782     (while threads
7783       (when (setq references (mail-header-references (caar threads)))
7784         (setq id (mail-header-id (caar threads)))
7785         (setq ids (gnus-split-references references))
7786         (setq entered nil)
7787         (while ids
7788           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7789               (progn
7790                 (gnus-sethash (car ids) id idhashtb)
7791                 (gnus-sethash id threads thhashtb))
7792             (setq gthread (gnus-gethash gid thhashtb))
7793             (unless entered
7794               ;; We enter a dummy root into the thread, if we
7795               ;; haven't done that already.
7796               (unless (stringp (caar gthread))
7797                 (setcar gthread (list (mail-header-subject (caar gthread))
7798                                       (car gthread))))
7799               ;; We add this new gathered thread to this gathered
7800               ;; thread.
7801               (setcdr (car gthread)
7802                       (nconc (cdar gthread) (list (car threads)))))
7803             ;; Add it into the thread hash table.
7804             (gnus-sethash id gthread thhashtb)
7805             (setq entered t)
7806             ;; Remove it from the list of threads.
7807             (setcdr prev (cdr threads))
7808             (setq threads prev))
7809           (setq ids (cdr ids))))
7810       (setq prev threads)
7811       (setq threads (cdr threads)))
7812     result))
7813
7814 (defun gnus-sort-gathered-threads (threads)
7815   "Sort subtreads inside each gathered thread by article number."
7816   (let ((result threads))
7817     (while threads
7818       (when (stringp (caar threads))
7819         (setcdr (car threads)
7820                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7821       (setq threads (cdr threads)))
7822     result))
7823
7824 (defun gnus-make-threads ()
7825   "Go through the dependency hashtb and find the roots.  Return all threads."
7826   (let (threads)
7827     (mapatoms
7828      (lambda (refs)
7829        (unless (car (symbol-value refs))
7830          ;; These threads do not refer back to any other articles,
7831          ;; so they're roots.
7832          (setq threads (append (cdr (symbol-value refs)) threads))))
7833      gnus-newsgroup-dependencies)
7834     threads))
7835
7836 (defun gnus-build-sparse-threads ()
7837   (let ((headers gnus-newsgroup-headers)
7838         (deps gnus-newsgroup-dependencies)
7839         header references generation relations 
7840         cthread subject child end pthread relation)
7841     ;; First we create an alist of generations/relations, where 
7842     ;; generations is how much we trust the ralation, and the relation
7843     ;; is parent/child.
7844     (gnus-message 7 "Making sparse threads...")
7845     (save-excursion
7846       (nnheader-set-temp-buffer " *gnus sparse threads*")
7847       (while (setq header (pop headers))
7848         (when (and (setq references (mail-header-references header))
7849                    (not (string= references "")))
7850           (insert references)
7851           (setq child (mail-header-id header)
7852                 subject (mail-header-subject header))
7853           (setq generation 0)
7854           (while (search-backward ">" nil t)
7855             (setq end (1+ (point)))
7856             (when (search-backward "<" nil t)
7857               (push (list (incf generation) 
7858                           child (setq child (buffer-substring (point) end))
7859                           subject)
7860                     relations)))
7861           (push (list (1+ generation) child nil subject) relations)
7862           (erase-buffer)))
7863       (kill-buffer (current-buffer)))
7864     ;; Sort over trustworthiness.
7865     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7866     (while (setq relation (pop relations))
7867       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7868                 (unless (car (symbol-value cthread))
7869                   ;; Make this article the parent of these threads.
7870                   (setcar (symbol-value cthread)
7871                           (vector gnus-reffed-article-number 
7872                                   (cadddr relation) 
7873                                   "" ""
7874                                   (cadr relation) 
7875                                   (or (caddr relation) "") 0 0 "")))
7876               (set cthread (list (vector gnus-reffed-article-number
7877                                          (cadddr relation) 
7878                                          "" "" (cadr relation) 
7879                                          (or (caddr relation) "") 0 0 ""))))
7880         (push gnus-reffed-article-number gnus-newsgroup-limit)
7881         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7882         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7883               gnus-newsgroup-reads)
7884         (decf gnus-reffed-article-number)
7885         ;; Make this new thread the child of its parent.
7886         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7887             (setcdr (symbol-value pthread)
7888                     (nconc (cdr (symbol-value pthread))
7889                            (list (symbol-value cthread))))
7890           (set pthread (list nil (symbol-value cthread))))))
7891     (gnus-message 7 "Making sparse threads...done")))
7892
7893 (defun gnus-build-old-threads ()
7894   ;; Look at all the articles that refer back to old articles, and
7895   ;; fetch the headers for the articles that aren't there.  This will
7896   ;; build complete threads - if the roots haven't been expired by the
7897   ;; server, that is.
7898   (let (id heads)
7899     (mapatoms
7900      (lambda (refs)
7901        (when (not (car (symbol-value refs)))
7902          (setq heads (cdr (symbol-value refs)))
7903          (while heads
7904            (if (memq (mail-header-number (caar heads))
7905                      gnus-newsgroup-dormant)
7906                (setq heads (cdr heads))
7907              (setq id (symbol-name refs))
7908              (while (and (setq id (gnus-build-get-header id))
7909                          (not (car (gnus-gethash
7910                                     id gnus-newsgroup-dependencies)))))
7911              (setq heads nil)))))
7912      gnus-newsgroup-dependencies)))
7913
7914 (defun gnus-build-get-header (id)
7915   ;; Look through the buffer of NOV lines and find the header to
7916   ;; ID.  Enter this line into the dependencies hash table, and return
7917   ;; the id of the parent article (if any).
7918   (let ((deps gnus-newsgroup-dependencies)
7919         found header)
7920     (prog1
7921         (save-excursion
7922           (set-buffer nntp-server-buffer)
7923           (goto-char (point-min))
7924           (while (and (not found) (search-forward id nil t))
7925             (beginning-of-line)
7926             (setq found (looking-at
7927                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7928                                  (regexp-quote id))))
7929             (or found (beginning-of-line 2)))
7930           (when found
7931             (beginning-of-line)
7932             (and
7933              (setq header (gnus-nov-parse-line
7934                            (read (current-buffer)) deps))
7935              (gnus-parent-id (mail-header-references header)))))
7936       (when header
7937         (let ((number (mail-header-number header)))
7938           (push number gnus-newsgroup-limit)
7939           (push header gnus-newsgroup-headers)
7940           (if (memq number gnus-newsgroup-unselected)
7941               (progn
7942                 (push number gnus-newsgroup-unreads)
7943                 (setq gnus-newsgroup-unselected
7944                       (delq number gnus-newsgroup-unselected)))
7945             (push number gnus-newsgroup-ancient)))))))
7946
7947 (defun gnus-summary-update-article (article &optional header)
7948   "Update ARTICLE in the summary buffer."
7949   (set-buffer gnus-summary-buffer)
7950   (let* ((header (or header (gnus-summary-article-header article)))
7951          (id (mail-header-id header))
7952          (data (gnus-data-find article))
7953          (thread (gnus-id-to-thread id))
7954          (parent
7955           (gnus-id-to-thread (or (gnus-parent-id 
7956                                   (mail-header-references header))
7957                                  "tull")))
7958          (buffer-read-only nil)
7959          (old (car thread))
7960          (number (mail-header-number header))
7961          pos)
7962     (when thread
7963       (setcar thread nil)
7964       (when parent
7965         (delq thread parent))
7966       (if (gnus-summary-insert-subject id header)
7967           ;; Set the (possibly) new article number in the data structure.
7968           (gnus-data-set-number data (gnus-id-to-article id))
7969         (setcar thread old)
7970         nil))))
7971
7972 (defun gnus-rebuild-thread (id)
7973   "Rebuild the thread containing ID."
7974   (let ((buffer-read-only nil)
7975         current thread data)
7976     (if (not gnus-show-threads)
7977         (setq thread (list (car (gnus-id-to-thread id))))
7978       ;; Get the thread this article is part of.
7979       (setq thread (gnus-remove-thread id)))
7980     (setq current (save-excursion
7981                     (and (zerop (forward-line -1))
7982                          (gnus-summary-article-number))))
7983     ;; If this is a gathered thread, we have to go some re-gathering.
7984     (when (stringp (car thread))
7985       (let ((subject (car thread))
7986             roots thr)
7987         (setq thread (cdr thread))
7988         (while thread
7989           (unless (memq (setq thr (gnus-id-to-thread
7990                                       (gnus-root-id
7991                                        (mail-header-id (caar thread)))))
7992                         roots)
7993             (push thr roots))
7994           (setq thread (cdr thread)))
7995         ;; We now have all (unique) roots.
7996         (if (= (length roots) 1)
7997             ;; All the loose roots are now one solid root.
7998             (setq thread (car roots))
7999           (setq thread (cons subject (gnus-sort-threads roots))))))
8000     (let (threads)
8001       ;; We then insert this thread into the summary buffer.
8002       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8003         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8004         (setq data (nreverse gnus-newsgroup-data))
8005         (setq threads gnus-newsgroup-threads))
8006       ;; We splice the new data into the data structure.
8007       (gnus-data-enter-list current data)
8008       (gnus-data-compute-positions)
8009       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8010
8011 (defun gnus-id-to-thread (id)
8012   "Return the (sub-)thread where ID appears."
8013   (gnus-gethash id gnus-newsgroup-dependencies))
8014
8015 (defun gnus-id-to-article (id)
8016   "Return the article number of ID."
8017   (let ((thread (gnus-id-to-thread id)))
8018     (when (and thread
8019                (car thread))
8020       (mail-header-number (car thread)))))
8021
8022 (defun gnus-id-to-header (id)
8023   "Return the article headers of ID."
8024   (car (gnus-id-to-thread id)))
8025
8026 (defun gnus-article-displayed-root-p (article)
8027   "Say whether ARTICLE is a root(ish) article."
8028   (let ((level (gnus-summary-thread-level article))
8029         (refs (mail-header-references  (gnus-summary-article-header article)))
8030         particle)
8031     (cond 
8032      ((null level) nil)
8033      ((zerop level) t)
8034      ((null refs) t)
8035      ((null(gnus-parent-id refs)) t)
8036      ((and (= 1 level)
8037            (null (setq particle (gnus-id-to-article
8038                                  (gnus-parent-id refs))))
8039            (null (gnus-summary-thread-level particle)))))))
8040
8041 (defun gnus-root-id (id)
8042   "Return the id of the root of the thread where ID appears."
8043   (let (last-id prev)
8044     (while (and id (setq prev (car (gnus-gethash 
8045                                     id gnus-newsgroup-dependencies))))
8046       (setq last-id id
8047             id (gnus-parent-id (mail-header-references prev))))
8048     last-id))
8049
8050 (defun gnus-remove-thread (id &optional dont-remove)
8051   "Remove the thread that has ID in it."
8052   (let ((dep gnus-newsgroup-dependencies)
8053         headers thread last-id)
8054     ;; First go up in this thread until we find the root.
8055     (setq last-id (gnus-root-id id))
8056     (setq headers (list (car (gnus-id-to-thread last-id))
8057                         (caadr (gnus-id-to-thread last-id))))
8058     ;; We have now found the real root of this thread.  It might have
8059     ;; been gathered into some loose thread, so we have to search
8060     ;; through the threads to find the thread we wanted.
8061     (let ((threads gnus-newsgroup-threads)
8062           sub)
8063       (while threads
8064         (setq sub (car threads))
8065         (if (stringp (car sub))
8066             ;; This is a gathered threads, so we look at the roots
8067             ;; below it to find whether this article in in this
8068             ;; gathered root.
8069             (progn
8070               (setq sub (cdr sub))
8071               (while sub
8072                 (when (member (caar sub) headers)
8073                   (setq thread (car threads)
8074                         threads nil
8075                         sub nil))
8076                 (setq sub (cdr sub))))
8077           ;; It's an ordinary thread, so we check it.
8078           (when (eq (car sub) (car headers))
8079             (setq thread sub
8080                   threads nil)))
8081         (setq threads (cdr threads)))
8082       ;; If this article is in no thread, then it's a root.
8083       (if thread
8084           (unless dont-remove
8085             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8086         (setq thread (gnus-gethash last-id dep)))
8087       (when thread
8088         (prog1
8089             thread ; We return this thread.
8090           (unless dont-remove
8091             (if (stringp (car thread))
8092                 (progn
8093                   ;; If we use dummy roots, then we have to remove the
8094                   ;; dummy root as well.
8095                   (when (eq gnus-summary-make-false-root 'dummy)
8096                     ;; Uhm.
8097                     )
8098                   (setq thread (cdr thread))
8099                   (while thread
8100                     (gnus-remove-thread-1 (car thread))
8101                     (setq thread (cdr thread))))
8102               (gnus-remove-thread-1 thread))))))))
8103
8104 (defun gnus-remove-thread-1 (thread)
8105   "Remove the thread THREAD recursively."
8106   (let ((number (mail-header-number (car thread)))
8107         pos)
8108     (when (setq pos (text-property-any
8109                      (point-min) (point-max) 'gnus-number number))
8110       (goto-char pos)
8111       (gnus-delete-line)
8112       (gnus-data-remove number))
8113     (setq thread (cdr thread))
8114     (while thread
8115       (gnus-remove-thread-1 (pop thread)))))
8116
8117 (defun gnus-sort-threads (threads)
8118   "Sort THREADS."
8119   (if (not gnus-thread-sort-functions)
8120       threads
8121     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8122                     (car gnus-thread-sort-functions)
8123                   `(lambda (t1 t2)
8124                      ,(gnus-make-sort-function 
8125                        (reverse gnus-thread-sort-functions))))))
8126       (gnus-message 7 "Sorting threads...")
8127       (prog1
8128           (sort threads func)
8129         (gnus-message 7 "Sorting threads...done")))))
8130
8131 (defun gnus-sort-articles (articles)
8132   "Sort ARTICLES."
8133   (when gnus-article-sort-functions
8134     (let ((func (if (= 1 (length gnus-article-sort-functions))
8135                     (car gnus-article-sort-functions)
8136                   `(lambda (t1 t2)
8137                      ,(gnus-make-sort-function 
8138                        (reverse gnus-article-sort-functions))))))
8139       (gnus-message 7 "Sorting articles...")
8140       (prog1
8141           (setq gnus-newsgroup-headers (sort articles func))
8142         (gnus-message 7 "Sorting articles...done")))))
8143
8144 (defun gnus-make-sort-function (funs)
8145   "Return a composite sort condition based on the functions in FUNC."
8146   (if (cdr funs)
8147       `(or (,(car funs) t1 t2)
8148            (and (not (,(car funs) t2 t1))
8149                 ,(gnus-make-sort-function (cdr funs))))
8150     `(,(car funs) t1 t2)))
8151                  
8152 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8153 (defmacro gnus-thread-header (thread)
8154   ;; Return header of first article in THREAD.
8155   ;; Note that THREAD must never, ever be anything else than a variable -
8156   ;; using some other form will lead to serious barfage.
8157   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8158   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8159   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8160         (vector thread) 2))
8161
8162 (defsubst gnus-article-sort-by-number (h1 h2)
8163   "Sort articles by article number."
8164   (< (mail-header-number h1)
8165      (mail-header-number h2)))
8166
8167 (defun gnus-thread-sort-by-number (h1 h2)
8168   "Sort threads by root article number."
8169   (gnus-article-sort-by-number
8170    (gnus-thread-header h1) (gnus-thread-header h2)))
8171
8172 (defsubst gnus-article-sort-by-author (h1 h2)
8173   "Sort articles by root author."
8174   (string-lessp
8175    (let ((extract (funcall
8176                    gnus-extract-address-components
8177                    (mail-header-from h1))))
8178      (or (car extract) (cdr extract)))
8179    (let ((extract (funcall
8180                    gnus-extract-address-components
8181                    (mail-header-from h2))))
8182      (or (car extract) (cdr extract)))))
8183
8184 (defun gnus-thread-sort-by-author (h1 h2)
8185   "Sort threads by root author."
8186   (gnus-article-sort-by-author
8187    (gnus-thread-header h1)  (gnus-thread-header h2)))
8188
8189 (defsubst gnus-article-sort-by-subject (h1 h2)
8190   "Sort articles by root subject."
8191   (string-lessp
8192    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8193    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8194
8195 (defun gnus-thread-sort-by-subject (h1 h2)
8196   "Sort threads by root subject."
8197   (gnus-article-sort-by-subject
8198    (gnus-thread-header h1) (gnus-thread-header h2)))
8199
8200 (defsubst gnus-article-sort-by-date (h1 h2)
8201   "Sort articles by root article date."
8202   (string-lessp
8203    (gnus-sortable-date (mail-header-date h1))
8204    (gnus-sortable-date (mail-header-date h2))))
8205
8206 (defun gnus-thread-sort-by-date (h1 h2)
8207   "Sort threads by root article date."
8208   (gnus-article-sort-by-date
8209    (gnus-thread-header h1) (gnus-thread-header h2)))
8210
8211 (defsubst gnus-article-sort-by-score (h1 h2)
8212   "Sort articles by root article score.
8213 Unscored articles will be counted as having a score of zero."
8214   (> (or (cdr (assq (mail-header-number h1)
8215                     gnus-newsgroup-scored))
8216          gnus-summary-default-score 0)
8217      (or (cdr (assq (mail-header-number h2)
8218                     gnus-newsgroup-scored))
8219          gnus-summary-default-score 0)))
8220
8221 (defun gnus-thread-sort-by-score (h1 h2)
8222   "Sort threads by root article score."
8223   (gnus-article-sort-by-score
8224    (gnus-thread-header h1) (gnus-thread-header h2)))
8225
8226 (defun gnus-thread-sort-by-total-score (h1 h2)
8227   "Sort threads by the sum of all scores in the thread.
8228 Unscored articles will be counted as having a score of zero."
8229   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8230
8231 (defun gnus-thread-total-score (thread)
8232   ;;  This function find the total score of THREAD.
8233   (if (consp thread)
8234       (if (stringp (car thread))
8235           (apply gnus-thread-score-function 0
8236                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8237         (gnus-thread-total-score-1 thread))
8238     (gnus-thread-total-score-1 (list thread))))
8239
8240 (defun gnus-thread-total-score-1 (root)
8241   ;; This function find the total score of the thread below ROOT.
8242   (setq root (car root))
8243   (apply gnus-thread-score-function
8244          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8245              gnus-summary-default-score 0)
8246          (mapcar 'gnus-thread-total-score
8247                  (cdr (gnus-gethash (mail-header-id root)
8248                                     gnus-newsgroup-dependencies)))))
8249
8250 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8251 (defvar gnus-tmp-prev-subject nil)
8252 (defvar gnus-tmp-false-parent nil)
8253 (defvar gnus-tmp-root-expunged nil)
8254 (defvar gnus-tmp-dummy-line nil)
8255
8256 (defun gnus-summary-prepare-threads (threads)
8257   "Prepare summary buffer from THREADS and indentation LEVEL.
8258 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8259 or a straight list of headers."
8260   (gnus-message 7 "Generating summary...")
8261
8262   (setq gnus-newsgroup-threads threads)
8263   (beginning-of-line)
8264
8265   (let ((gnus-tmp-level 0)
8266         (default-score (or gnus-summary-default-score 0))
8267         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8268         thread number subject stack state gnus-tmp-gathered beg-match
8269         new-roots gnus-tmp-new-adopts thread-end
8270         gnus-tmp-header gnus-tmp-unread
8271         gnus-tmp-replied gnus-tmp-subject-or-nil
8272         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8273         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8274         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8275
8276     (setq gnus-tmp-prev-subject nil)
8277
8278     (if (vectorp (car threads))
8279         ;; If this is a straight (sic) list of headers, then a
8280         ;; threaded summary display isn't required, so we just create
8281         ;; an unthreaded one.
8282         (gnus-summary-prepare-unthreaded threads)
8283
8284       ;; Do the threaded display.
8285
8286       (while (or threads stack gnus-tmp-new-adopts new-roots)
8287
8288         (if (and (= gnus-tmp-level 0)
8289                  (not (setq gnus-tmp-dummy-line nil))
8290                  (or (not stack)
8291                      (= (caar stack) 0))
8292                  (not gnus-tmp-false-parent)
8293                  (or gnus-tmp-new-adopts new-roots))
8294             (if gnus-tmp-new-adopts
8295                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8296                       thread (list (car gnus-tmp-new-adopts))
8297                       gnus-tmp-header (caar thread)
8298                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8299               (if new-roots
8300                   (setq thread (list (car new-roots))
8301                         gnus-tmp-header (caar thread)
8302                         new-roots (cdr new-roots))))
8303
8304           (if threads
8305               ;; If there are some threads, we do them before the
8306               ;; threads on the stack.
8307               (setq thread threads
8308                     gnus-tmp-header (caar thread))
8309             ;; There were no current threads, so we pop something off
8310             ;; the stack.
8311             (setq state (car stack)
8312                   gnus-tmp-level (car state)
8313                   thread (cdr state)
8314                   stack (cdr stack)
8315                   gnus-tmp-header (caar thread))))
8316
8317         (setq gnus-tmp-false-parent nil)
8318         (setq gnus-tmp-root-expunged nil)
8319         (setq thread-end nil)
8320
8321         (if (stringp gnus-tmp-header)
8322             ;; The header is a dummy root.
8323             (cond
8324              ((eq gnus-summary-make-false-root 'adopt)
8325               ;; We let the first article adopt the rest.
8326               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8327                                                (cddar thread)))
8328               (setq gnus-tmp-gathered
8329                     (nconc (mapcar
8330                             (lambda (h) (mail-header-number (car h)))
8331                             (cddar thread))
8332                            gnus-tmp-gathered))
8333               (setq thread (cons (list (caar thread)
8334                                        (cadar thread))
8335                                  (cdr thread)))
8336               (setq gnus-tmp-level -1
8337                     gnus-tmp-false-parent t))
8338              ((eq gnus-summary-make-false-root 'empty)
8339               ;; We print adopted articles with empty subject fields.
8340               (setq gnus-tmp-gathered
8341                     (nconc (mapcar
8342                             (lambda (h) (mail-header-number (car h)))
8343                             (cddar thread))
8344                            gnus-tmp-gathered))
8345               (setq gnus-tmp-level -1))
8346              ((eq gnus-summary-make-false-root 'dummy)
8347               ;; We remember that we probably want to output a dummy
8348               ;; root.
8349               (setq gnus-tmp-dummy-line gnus-tmp-header)
8350               (setq gnus-tmp-prev-subject gnus-tmp-header))
8351              (t
8352               ;; We do not make a root for the gathered
8353               ;; sub-threads at all.
8354               (setq gnus-tmp-level -1)))
8355
8356           (setq number (mail-header-number gnus-tmp-header)
8357                 subject (mail-header-subject gnus-tmp-header))
8358
8359           (cond
8360            ;; If the thread has changed subject, we might want to make
8361            ;; this subthread into a root.
8362            ((and (null gnus-thread-ignore-subject)
8363                  (not (zerop gnus-tmp-level))
8364                  gnus-tmp-prev-subject
8365                  (not (inline
8366                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8367             (setq new-roots (nconc new-roots (list (car thread)))
8368                   thread-end t
8369                   gnus-tmp-header nil))
8370            ;; If the article lies outside the current limit,
8371            ;; then we do not display it.
8372            ((and (not (memq number gnus-newsgroup-limit))
8373                  (not gnus-tmp-dummy-line))
8374             (setq gnus-tmp-gathered
8375                   (nconc (mapcar
8376                           (lambda (h) (mail-header-number (car h)))
8377                           (cdar thread))
8378                          gnus-tmp-gathered))
8379             (setq gnus-tmp-new-adopts (if (cdar thread)
8380                                           (append gnus-tmp-new-adopts
8381                                                   (cdar thread))
8382                                         gnus-tmp-new-adopts)
8383                   thread-end t
8384                   gnus-tmp-header nil)
8385             (when (zerop gnus-tmp-level)
8386               (setq gnus-tmp-root-expunged t)))
8387            ;; Perhaps this article is to be marked as read?
8388            ((and gnus-summary-mark-below
8389                  (< (or (cdr (assq number gnus-newsgroup-scored))
8390                         default-score)
8391                     gnus-summary-mark-below)
8392                  ;; Don't touch sparse articles.
8393                  (not (memq number gnus-newsgroup-sparse)))
8394             (setq gnus-newsgroup-unreads
8395                   (delq number gnus-newsgroup-unreads))
8396             (if gnus-newsgroup-auto-expire
8397                 (push number gnus-newsgroup-expirable)
8398               (push (cons number gnus-low-score-mark)
8399                     gnus-newsgroup-reads))))
8400
8401           (when gnus-tmp-header
8402             ;; We may have an old dummy line to output before this
8403             ;; article.
8404             (when gnus-tmp-dummy-line
8405               (gnus-summary-insert-dummy-line
8406                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8407               (setq gnus-tmp-dummy-line nil))
8408
8409             ;; Compute the mark.
8410             (setq
8411              gnus-tmp-unread
8412              (cond
8413               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8414               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8415               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8416               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8417               (t (or (cdr (assq number gnus-newsgroup-reads))
8418                      gnus-ancient-mark))))
8419
8420             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8421                                   gnus-tmp-header gnus-tmp-level)
8422                   gnus-newsgroup-data)
8423
8424             ;; Actually insert the line.
8425             (setq
8426              gnus-tmp-subject-or-nil
8427              (cond
8428               ((and gnus-thread-ignore-subject
8429                     gnus-tmp-prev-subject
8430                     (not (inline (gnus-subject-equal
8431                                   gnus-tmp-prev-subject subject))))
8432                subject)
8433               ((zerop gnus-tmp-level)
8434                (if (and (eq gnus-summary-make-false-root 'empty)
8435                         (memq number gnus-tmp-gathered)
8436                         gnus-tmp-prev-subject
8437                         (inline (gnus-subject-equal
8438                                  gnus-tmp-prev-subject subject)))
8439                    gnus-summary-same-subject
8440                  subject))
8441               (t gnus-summary-same-subject)))
8442             (if (and (eq gnus-summary-make-false-root 'adopt)
8443                      (= gnus-tmp-level 1)
8444                      (memq number gnus-tmp-gathered))
8445                 (setq gnus-tmp-opening-bracket ?\<
8446                       gnus-tmp-closing-bracket ?\>)
8447               (setq gnus-tmp-opening-bracket ?\[
8448                     gnus-tmp-closing-bracket ?\]))
8449             (setq
8450              gnus-tmp-indentation
8451              (aref gnus-thread-indent-array gnus-tmp-level)
8452              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8453              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8454                                 gnus-summary-default-score 0)
8455              gnus-tmp-score-char
8456              (if (or (null gnus-summary-default-score)
8457                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8458                          gnus-summary-zcore-fuzz)) ? 
8459                (if (< gnus-tmp-score gnus-summary-default-score)
8460                    gnus-score-below-mark gnus-score-over-mark))
8461              gnus-tmp-replied
8462              (cond ((memq number gnus-newsgroup-processable)
8463                     gnus-process-mark)
8464                    ((memq number gnus-newsgroup-cached)
8465                     gnus-cached-mark)
8466                    ((memq number gnus-newsgroup-replied)
8467                     gnus-replied-mark)
8468                    (t gnus-unread-mark))
8469              gnus-tmp-from (mail-header-from gnus-tmp-header)
8470              gnus-tmp-name
8471              (cond
8472               ((string-match "(.+)" gnus-tmp-from)
8473                (substring gnus-tmp-from
8474                           (1+ (match-beginning 0)) (1- (match-end 0))))
8475               ((string-match "<[^>]+> *$" gnus-tmp-from)
8476                (setq beg-match (match-beginning 0))
8477                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8478                         (substring gnus-tmp-from (1+ (match-beginning 0))
8479                                    (1- (match-end 0))))
8480                    (substring gnus-tmp-from 0 beg-match)))
8481               (t gnus-tmp-from)))
8482             (when (string= gnus-tmp-name "")
8483               (setq gnus-tmp-name gnus-tmp-from))
8484             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8485             (put-text-property
8486              (point)
8487              (progn (eval gnus-summary-line-format-spec) (point))
8488              'gnus-number number)
8489             (when gnus-visual-p
8490               (forward-line -1)
8491               (run-hooks 'gnus-summary-update-hook)
8492               (forward-line 1))
8493
8494             (setq gnus-tmp-prev-subject subject)))
8495
8496         (when (nth 1 thread)
8497           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8498         (incf gnus-tmp-level)
8499         (setq threads (if thread-end nil (cdar thread)))
8500         (unless threads
8501           (setq gnus-tmp-level 0)))))
8502   (gnus-message 7 "Generating summary...done"))
8503
8504 (defun gnus-summary-prepare-unthreaded (headers)
8505   "Generate an unthreaded summary buffer based on HEADERS."
8506   (let (header number mark)
8507
8508     (while headers
8509       ;; We may have to root out some bad articles...
8510       (when (memq (setq number (mail-header-number
8511                                 (setq header (pop headers))))
8512                   gnus-newsgroup-limit)
8513         ;; Mark article as read when it has a low score.
8514         (when (and gnus-summary-mark-below
8515                    (< (or (cdr (assq number gnus-newsgroup-scored))
8516                           gnus-summary-default-score 0)
8517                       gnus-summary-mark-below))
8518           (setq gnus-newsgroup-unreads
8519                 (delq number gnus-newsgroup-unreads))
8520           (if gnus-newsgroup-auto-expire
8521               (push number gnus-newsgroup-expirable)
8522             (push (cons number gnus-low-score-mark)
8523                   gnus-newsgroup-reads)))
8524
8525         (setq mark
8526               (cond
8527                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8528                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8529                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8530                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8531                (t (or (cdr (assq number gnus-newsgroup-reads))
8532                       gnus-ancient-mark))))
8533         (setq gnus-newsgroup-data
8534               (cons (gnus-data-make number mark (1+ (point)) header 0)
8535                     gnus-newsgroup-data))
8536         (gnus-summary-insert-line
8537          header 0 nil mark (memq number gnus-newsgroup-replied)
8538          (memq number gnus-newsgroup-expirable)
8539          (mail-header-subject header) nil
8540          (cdr (assq number gnus-newsgroup-scored))
8541          (memq number gnus-newsgroup-processable))))))
8542
8543 (defun gnus-select-newsgroup (group &optional read-all)
8544   "Select newsgroup GROUP.
8545 If READ-ALL is non-nil, all articles in the group are selected."
8546   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8547          (info (nth 2 entry))
8548          articles fetched-articles cached)
8549
8550     (or (gnus-check-server
8551          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8552         (error "Couldn't open server"))
8553
8554     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8555         (gnus-activate-group group) ; Or we can activate it...
8556         (progn ; Or we bug out.
8557           (when (equal major-mode 'gnus-summary-mode)
8558             (kill-buffer (current-buffer)))
8559           (error "Couldn't request group %s: %s"
8560                  group (gnus-status-message group))))
8561
8562     (setq gnus-newsgroup-name group)
8563     (setq gnus-newsgroup-unselected nil)
8564     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8565
8566     (and gnus-asynchronous
8567          (gnus-check-backend-function
8568           'request-asynchronous gnus-newsgroup-name)
8569          (setq gnus-newsgroup-async
8570                (gnus-request-asynchronous gnus-newsgroup-name)))
8571
8572     ;; Adjust and set lists of article marks.
8573     (when info
8574       (gnus-adjust-marked-articles info))
8575
8576     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8577     (when (gnus-virtual-group-p group)
8578       (setq cached gnus-newsgroup-cached))
8579
8580     (setq gnus-newsgroup-unreads
8581           (gnus-set-difference
8582            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8583            gnus-newsgroup-dormant))
8584
8585     (setq gnus-newsgroup-processable nil)
8586
8587     (setq articles (gnus-articles-to-read group read-all))
8588
8589     (cond
8590      ((null articles)
8591       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8592       'quit)
8593      ((eq articles 0) nil)
8594      (t
8595       ;; Init the dependencies hash table.
8596       (setq gnus-newsgroup-dependencies
8597             (gnus-make-hashtable (length articles)))
8598       ;; Retrieve the headers and read them in.
8599       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8600       (setq gnus-newsgroup-headers
8601             (if (eq 'nov
8602                     (setq gnus-headers-retrieved-by
8603                           (gnus-retrieve-headers
8604                            articles gnus-newsgroup-name
8605                            ;; We might want to fetch old headers, but
8606                            ;; not if there is only 1 article.
8607                            (and gnus-fetch-old-headers
8608                                 (or (and
8609                                      (not (eq gnus-fetch-old-headers 'some))
8610                                      (not (numberp gnus-fetch-old-headers)))
8611                                     (> (length articles) 1))))))
8612                 (gnus-get-newsgroup-headers-xover articles)
8613               (gnus-get-newsgroup-headers)))
8614       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8615
8616       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8617       (when cached
8618         (setq gnus-newsgroup-cached cached))
8619
8620       ;; Set the initial limit.
8621       (setq gnus-newsgroup-limit (copy-sequence articles))
8622       ;; Remove canceled articles from the list of unread articles.
8623       (setq gnus-newsgroup-unreads
8624             (gnus-set-sorted-intersection
8625              gnus-newsgroup-unreads
8626              (setq fetched-articles
8627                    (mapcar (lambda (headers) (mail-header-number headers))
8628                            gnus-newsgroup-headers))))
8629       ;; Removed marked articles that do not exist.
8630       (gnus-update-missing-marks
8631        (gnus-sorted-complement fetched-articles articles))
8632       ;; We might want to build some more threads first.
8633       (and gnus-fetch-old-headers
8634            (eq gnus-headers-retrieved-by 'nov)
8635            (gnus-build-old-threads))
8636       ;; Check whether auto-expire is to be done in this group.
8637       (setq gnus-newsgroup-auto-expire
8638             (gnus-group-auto-expirable-p group))
8639       ;; Set up the article buffer now, if necessary.
8640       (unless gnus-single-article-buffer
8641         (gnus-article-setup-buffer))
8642       ;; First and last article in this newsgroup.
8643       (and gnus-newsgroup-headers
8644            (setq gnus-newsgroup-begin
8645                  (mail-header-number (car gnus-newsgroup-headers)))
8646            (setq gnus-newsgroup-end
8647                  (mail-header-number
8648                   (gnus-last-element gnus-newsgroup-headers))))
8649       (setq gnus-reffed-article-number -1)
8650       ;; GROUP is successfully selected.
8651       (or gnus-newsgroup-headers t)))))
8652
8653 (defun gnus-articles-to-read (group read-all)
8654   ;; Find out what articles the user wants to read.
8655   (let* ((articles
8656           ;; Select all articles if `read-all' is non-nil, or if there
8657           ;; are no unread articles.
8658           (if (or read-all
8659                   (and (zerop (length gnus-newsgroup-marked))
8660                        (zerop (length gnus-newsgroup-unreads))))
8661               (gnus-uncompress-range (gnus-active group))
8662             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8663                           (copy-sequence gnus-newsgroup-unreads))
8664                   '<)))
8665          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8666          (scored (length scored-list))
8667          (number (length articles))
8668          (marked (+ (length gnus-newsgroup-marked)
8669                     (length gnus-newsgroup-dormant)))
8670          (select
8671           (cond
8672            ((numberp read-all)
8673             read-all)
8674            (t
8675             (condition-case ()
8676                 (cond
8677                  ((and (or (<= scored marked) (= scored number))
8678                        (numberp gnus-large-newsgroup)
8679                        (> number gnus-large-newsgroup))
8680                   (let ((input
8681                          (read-string
8682                           (format
8683                            "How many articles from %s (default %d): "
8684                            gnus-newsgroup-name number))))
8685                     (if (string-match "^[ \t]*$" input) number input)))
8686                  ((and (> scored marked) (< scored number))
8687                   (let ((input
8688                          (read-string
8689                           (format "%s %s (%d scored, %d total): "
8690                                   "How many articles from"
8691                                   group scored number))))
8692                     (if (string-match "^[ \t]*$" input)
8693                         number input)))
8694                  (t number))
8695               (quit nil))))))
8696     (setq select (if (stringp select) (string-to-number select) select))
8697     (if (or (null select) (zerop select))
8698         select
8699       (if (and (not (zerop scored)) (<= (abs select) scored))
8700           (progn
8701             (setq articles (sort scored-list '<))
8702             (setq number (length articles)))
8703         (setq articles (copy-sequence articles)))
8704
8705       (if (< (abs select) number)
8706           (if (< select 0)
8707               ;; Select the N oldest articles.
8708               (setcdr (nthcdr (1- (abs select)) articles) nil)
8709             ;; Select the N most recent articles.
8710             (setq articles (nthcdr (- number select) articles))))
8711       (setq gnus-newsgroup-unselected
8712             (gnus-sorted-intersection
8713              gnus-newsgroup-unreads
8714              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8715       articles)))
8716
8717 (defun gnus-killed-articles (killed articles)
8718   (let (out)
8719     (while articles
8720       (if (inline (gnus-member-of-range (car articles) killed))
8721           (setq out (cons (car articles) out)))
8722       (setq articles (cdr articles)))
8723     out))
8724
8725 (defun gnus-uncompress-marks (marks)
8726   "Uncompress the mark ranges in MARKS."
8727   (let ((uncompressed '(score bookmark))
8728         out)
8729     (while marks
8730       (if (memq (caar marks) uncompressed)
8731           (push (car marks) out)
8732         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8733       (setq marks (cdr marks)))
8734     out))
8735
8736 (defun gnus-adjust-marked-articles (info)
8737   "Set all article lists and remove all marks that are no longer legal."
8738   (let* ((marked-lists (gnus-info-marks info))
8739          (active (gnus-active (gnus-info-group info)))
8740          (min (car active))
8741          (max (cdr active))
8742          (types gnus-article-mark-lists)
8743          (uncompressed '(score bookmark))
8744          marks var articles article mark)
8745
8746     (while marked-lists
8747       (setq marks (pop marked-lists))
8748       (set (setq var (intern (format "gnus-newsgroup-%s"
8749                                      (car (rassq (setq mark (car marks))
8750                                                  types)))))
8751            (if (memq (car marks) uncompressed) (cdr marks)
8752              (gnus-uncompress-range (cdr marks))))
8753
8754       (setq articles (symbol-value var))
8755
8756       ;; All articles have to be subsets of the active articles.
8757       (cond
8758        ;; Adjust "simple" lists.
8759        ((memq mark '(tick dormant expirable reply killed save))
8760         (while articles
8761           (when (or (< (setq article (pop articles)) min) (> article max))
8762             (set var (delq article (symbol-value var))))))
8763        ;; Adjust assocs.
8764        ((memq mark '(score bookmark))
8765         (while articles
8766           (when (or (< (car (setq article (pop articles))) min)
8767                     (> (car article) max))
8768             (set var (delq article (symbol-value var))))))))))
8769
8770 (defun gnus-update-missing-marks (missing)
8771   "Go through the list of MISSING articles and remove them mark lists."
8772   (when missing
8773     (let ((types gnus-article-mark-lists)
8774           var m)
8775       ;; Go through all types.
8776       (while types
8777         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8778         (when (symbol-value var)
8779           ;; This list has articles.  So we delete all missing articles
8780           ;; from it.
8781           (setq m missing)
8782           (while m
8783             (set var (delq (pop m) (symbol-value var)))))))))
8784
8785 (defun gnus-update-marks ()
8786   "Enter the various lists of marked articles into the newsgroup info list."
8787   (let ((types gnus-article-mark-lists)
8788         (info (gnus-get-info gnus-newsgroup-name))
8789         (uncompressed '(score bookmark killed))
8790         type list newmarked symbol)
8791     (when info
8792       ;; Add all marks lists that are non-nil to the list of marks lists.
8793       (while types
8794         (setq type (pop types))
8795         (when (setq list (symbol-value
8796                           (setq symbol
8797                                 (intern (format "gnus-newsgroup-%s"
8798                                                 (car type))))))
8799           (push (cons (cdr type)
8800                       (if (memq (cdr type) uncompressed) list
8801                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8802                 newmarked)))
8803
8804       ;; Enter these new marks into the info of the group.
8805       (if (nthcdr 3 info)
8806           (setcar (nthcdr 3 info) newmarked)
8807         ;; Add the marks lists to the end of the info.
8808         (when newmarked
8809           (setcdr (nthcdr 2 info) (list newmarked))))
8810
8811       ;; Cut off the end of the info if there's nothing else there.
8812       (let ((i 5))
8813         (while (and (> i 2)
8814                     (not (nth i info)))
8815           (when (nthcdr (decf i) info)
8816             (setcdr (nthcdr i info) nil)))))))
8817
8818 (defun gnus-add-marked-articles (group type articles &optional info force)
8819   ;; Add ARTICLES of TYPE to the info of GROUP.
8820   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8821   ;; add, but replace marked articles of TYPE with ARTICLES.
8822   (let ((info (or info (gnus-get-info group)))
8823         (uncompressed '(score bookmark killed))
8824         marked m)
8825     (or (not info)
8826         (and (not (setq marked (nthcdr 3 info)))
8827              (or (null articles)
8828                  (setcdr (nthcdr 2 info)
8829                          (list (list (cons type (gnus-compress-sequence
8830                                                  articles t)))))))
8831         (and (not (setq m (assq type (car marked))))
8832              (or (null articles)
8833                  (setcar marked
8834                          (cons (cons type (gnus-compress-sequence articles t) )
8835                                (car marked)))))
8836         (if force
8837             (if (null articles)
8838                 (setcar (nthcdr 3 info)
8839                         (delq (assq type (car marked)) (car marked)))
8840               (setcdr m (gnus-compress-sequence articles t)))
8841           (setcdr m (gnus-compress-sequence
8842                      (sort (nconc (gnus-uncompress-range (cdr m))
8843                                   (copy-sequence articles)) '<) t))))))
8844
8845 (defun gnus-set-mode-line (where)
8846   "This function sets the mode line of the article or summary buffers.
8847 If WHERE is `summary', the summary mode line format will be used."
8848   ;; Is this mode line one we keep updated?
8849   (when (memq where gnus-updated-mode-lines)
8850     (let (mode-string)
8851       (save-excursion
8852         ;; We evaluate this in the summary buffer since these
8853         ;; variables are buffer-local to that buffer.
8854         (set-buffer gnus-summary-buffer)
8855         ;; We bind all these variables that are used in the `eval' form
8856         ;; below.
8857         (let* ((mformat (symbol-value
8858                          (intern
8859                           (format "gnus-%s-mode-line-format-spec" where))))
8860                (gnus-tmp-group-name gnus-newsgroup-name)
8861                (gnus-tmp-article-number (or gnus-current-article 0))
8862                (gnus-tmp-unread gnus-newsgroup-unreads)
8863                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8864                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8865                (gnus-tmp-unread-and-unselected
8866                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8867                             (zerop gnus-tmp-unselected)) "")
8868                       ((zerop gnus-tmp-unselected)
8869                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8870                       (t (format "{%d(+%d) more}"
8871                                  gnus-tmp-unread-and-unticked
8872                                  gnus-tmp-unselected))))
8873                (gnus-tmp-subject
8874                 (if (and gnus-current-headers
8875                          (vectorp gnus-current-headers))
8876                     (mail-header-subject gnus-current-headers) ""))
8877                max-len
8878                gnus-tmp-header);; passed as argument to any user-format-funcs
8879           (setq mode-string (eval mformat))
8880           (setq max-len (max 4 (if gnus-mode-non-string-length
8881                                    (- (window-width)
8882                                       gnus-mode-non-string-length)
8883                                  (length mode-string))))
8884           ;; We might have to chop a bit of the string off...
8885           (when (> (length mode-string) max-len)
8886             (setq mode-string
8887                   (concat (gnus-truncate-string mode-string (- max-len 3))
8888                           "...")))
8889           ;; Pad the mode string a bit.
8890           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8891       ;; Update the mode line.
8892       (setq mode-line-buffer-identification (list mode-string))
8893       (set-buffer-modified-p t))))
8894
8895 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8896   "Go through the HEADERS list and add all Xrefs to a hash table.
8897 The resulting hash table is returned, or nil if no Xrefs were found."
8898   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8899          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8900          (xref-hashtb (make-vector 63 0))
8901          start group entry number xrefs header)
8902     (while headers
8903       (setq header (pop headers))
8904       (when (and (setq xrefs (mail-header-xref header))
8905                  (not (memq (setq number (mail-header-number header))
8906                             unreads)))
8907         (setq start 0)
8908         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8909           (setq start (match-end 0))
8910           (setq group (if prefix
8911                           (concat prefix (substring xrefs (match-beginning 1)
8912                                                     (match-end 1)))
8913                         (substring xrefs (match-beginning 1) (match-end 1))))
8914           (setq number
8915                 (string-to-int (substring xrefs (match-beginning 2)
8916                                           (match-end 2))))
8917           (if (setq entry (gnus-gethash group xref-hashtb))
8918               (setcdr entry (cons number (cdr entry)))
8919             (gnus-sethash group (cons number nil) xref-hashtb)))))
8920     (and start xref-hashtb)))
8921
8922 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8923   "Look through all the headers and mark the Xrefs as read."
8924   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8925         name entry info xref-hashtb idlist method nth4)
8926     (save-excursion
8927       (set-buffer gnus-group-buffer)
8928       (when (setq xref-hashtb
8929                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8930         (mapatoms
8931          (lambda (group)
8932            (unless (string= from-newsgroup (setq name (symbol-name group)))
8933              (setq idlist (symbol-value group))
8934              ;; Dead groups are not updated.
8935              (and (prog1
8936                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8937                             info (nth 2 entry))
8938                     (if (stringp (setq nth4 (gnus-info-method info)))
8939                         (setq nth4 (gnus-server-to-method nth4))))
8940                   ;; Only do the xrefs if the group has the same
8941                   ;; select method as the group we have just read.
8942                   (or (gnus-methods-equal-p
8943                        nth4 (gnus-find-method-for-group from-newsgroup))
8944                       virtual
8945                       (equal nth4 (setq method (gnus-find-method-for-group
8946                                                 from-newsgroup)))
8947                       (and (equal (car nth4) (car method))
8948                            (equal (nth 1 nth4) (nth 1 method))))
8949                   gnus-use-cross-reference
8950                   (or (not (eq gnus-use-cross-reference t))
8951                       virtual
8952                       ;; Only do cross-references on subscribed
8953                       ;; groups, if that is what is wanted.
8954                       (<= (gnus-info-level info) gnus-level-subscribed))
8955                   (gnus-group-make-articles-read name idlist))))
8956          xref-hashtb)))))
8957
8958 (defun gnus-group-make-articles-read (group articles)
8959   (let* ((num 0)
8960          (entry (gnus-gethash group gnus-newsrc-hashtb))
8961          (info (nth 2 entry))
8962          (active (gnus-active group))
8963          range)
8964     ;; First peel off all illegal article numbers.
8965     (if active
8966         (let ((ids articles)
8967               id first)
8968           (while ids
8969             (setq id (car ids))
8970             (if (and first (> id (cdr active)))
8971                 (progn
8972                   ;; We'll end up in this situation in one particular
8973                   ;; obscure situation.  If you re-scan a group and get
8974                   ;; a new article that is cross-posted to a different
8975                   ;; group that has not been re-scanned, you might get
8976                   ;; crossposted article that has a higher number than
8977                   ;; Gnus believes possible.  So we re-activate this
8978                   ;; group as well.  This might mean doing the
8979                   ;; crossposting thingy will *increase* the number
8980                   ;; of articles in some groups.  Tsk, tsk.
8981                   (setq active (or (gnus-activate-group group) active))))
8982             (if (or (> id (cdr active))
8983                     (< id (car active)))
8984                 (setq articles (delq id articles)))
8985             (setq ids (cdr ids)))))
8986     ;; If the read list is nil, we init it.
8987     (and active
8988          (null (gnus-info-read info))
8989          (> (car active) 1)
8990          (gnus-info-set-read info (cons 1 (1- (car active)))))
8991     ;; Then we add the read articles to the range.
8992     (gnus-info-set-read
8993      info
8994      (setq range
8995            (gnus-add-to-range
8996             (gnus-info-read info) (setq articles (sort articles '<)))))
8997     ;; Then we have to re-compute how many unread
8998     ;; articles there are in this group.
8999     (if active
9000         (progn
9001           (cond
9002            ((not range)
9003             (setq num (- (1+ (cdr active)) (car active))))
9004            ((not (listp (cdr range)))
9005             (setq num (- (cdr active) (- (1+ (cdr range))
9006                                          (car range)))))
9007            (t
9008             (while range
9009               (if (numberp (car range))
9010                   (setq num (1+ num))
9011                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9012               (setq range (cdr range)))
9013             (setq num (- (cdr active) num))))
9014           ;; Update the number of unread articles.
9015           (setcar entry num)
9016           ;; Update the group buffer.
9017           (gnus-group-update-group group t)))))
9018
9019 (defun gnus-methods-equal-p (m1 m2)
9020   (let ((m1 (or m1 gnus-select-method))
9021         (m2 (or m2 gnus-select-method)))
9022     (or (equal m1 m2)
9023         (and (eq (car m1) (car m2))
9024              (or (not (memq 'address (assoc (symbol-name (car m1))
9025                                             gnus-valid-select-methods)))
9026                  (equal (nth 1 m1) (nth 1 m2)))))))
9027
9028 (defsubst gnus-header-value ()
9029   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9030
9031 (defvar gnus-newsgroup-none-id 0)
9032
9033 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9034   (let ((cur nntp-server-buffer)
9035         (dependencies
9036          (or dependencies
9037              (save-excursion (set-buffer gnus-summary-buffer)
9038                              gnus-newsgroup-dependencies)))
9039         headers id id-dep ref-dep end ref)
9040     (save-excursion
9041       (set-buffer nntp-server-buffer)
9042       (run-hooks 'gnus-parse-headers-hook)
9043       (let ((case-fold-search t)
9044             in-reply-to header p lines)
9045         (goto-char (point-min))
9046         ;; Search to the beginning of the next header.  Error messages
9047         ;; do not begin with 2 or 3.
9048         (while (re-search-forward "^[23][0-9]+ " nil t)
9049           (setq id nil
9050                 ref nil)
9051           ;; This implementation of this function, with nine
9052           ;; search-forwards instead of the one re-search-forward and
9053           ;; a case (which basically was the old function) is actually
9054           ;; about twice as fast, even though it looks messier.  You
9055           ;; can't have everything, I guess.  Speed and elegance
9056           ;; doesn't always go hand in hand.
9057           (setq
9058            header
9059            (vector
9060             ;; Number.
9061             (prog1
9062                 (read cur)
9063               (end-of-line)
9064               (setq p (point))
9065               (narrow-to-region (point)
9066                                 (or (and (search-forward "\n.\n" nil t)
9067                                          (- (point) 2))
9068                                     (point))))
9069             ;; Subject.
9070             (progn
9071               (goto-char p)
9072               (if (search-forward "\nsubject: " nil t)
9073                   (gnus-header-value) "(none)"))
9074             ;; From.
9075             (progn
9076               (goto-char p)
9077               (if (search-forward "\nfrom: " nil t)
9078                   (gnus-header-value) "(nobody)"))
9079             ;; Date.
9080             (progn
9081               (goto-char p)
9082               (if (search-forward "\ndate: " nil t)
9083                   (gnus-header-value) ""))
9084             ;; Message-ID.
9085             (progn
9086               (goto-char p)
9087               (if (search-forward "\nmessage-id: " nil t)
9088                   (setq id (gnus-header-value))
9089                 ;; If there was no message-id, we just fake one to make
9090                 ;; subsequent routines simpler.
9091                 (setq id (concat "none+"
9092                                  (int-to-string
9093                                   (setq gnus-newsgroup-none-id
9094                                         (1+ gnus-newsgroup-none-id)))))))
9095             ;; References.
9096             (progn
9097               (goto-char p)
9098               (if (search-forward "\nreferences: " nil t)
9099                   (prog1
9100                       (gnus-header-value)
9101                     (setq end (match-end 0))
9102                     (save-excursion
9103                       (setq ref
9104                             (buffer-substring
9105                              (progn
9106                                (end-of-line)
9107                                (search-backward ">" end t)
9108                                (1+ (point)))
9109                              (progn
9110                                (search-backward "<" end t)
9111                                (point))))))
9112                 ;; Get the references from the in-reply-to header if there
9113                 ;; were no references and the in-reply-to header looks
9114                 ;; promising.
9115                 (if (and (search-forward "\nin-reply-to: " nil t)
9116                          (setq in-reply-to (gnus-header-value))
9117                          (string-match "<[^>]+>" in-reply-to))
9118                     (setq ref (substring in-reply-to (match-beginning 0)
9119                                          (match-end 0)))
9120                   (setq ref ""))))
9121             ;; Chars.
9122             0
9123             ;; Lines.
9124             (progn
9125               (goto-char p)
9126               (if (search-forward "\nlines: " nil t)
9127                   (if (numberp (setq lines (read cur)))
9128                       lines 0)
9129                 0))
9130             ;; Xref.
9131             (progn
9132               (goto-char p)
9133               (and (search-forward "\nxref: " nil t)
9134                    (gnus-header-value)))))
9135           ;; We do the threading while we read the headers.  The
9136           ;; message-id and the last reference are both entered into
9137           ;; the same hash table.  Some tippy-toeing around has to be
9138           ;; done in case an article has arrived before the article
9139           ;; which it refers to.
9140           (if (boundp (setq id-dep (intern id dependencies)))
9141               (if (and (car (symbol-value id-dep))
9142                        (not force-new))
9143                   ;; An article with this Message-ID has already
9144                   ;; been seen, so we ignore this one, except we add
9145                   ;; any additional Xrefs (in case the two articles
9146                   ;; came from different servers).
9147                   (progn
9148                     (mail-header-set-xref
9149                      (car (symbol-value id-dep))
9150                      (concat (or (mail-header-xref
9151                                   (car (symbol-value id-dep))) "")
9152                              (or (mail-header-xref header) "")))
9153                     (setq header nil))
9154                 (setcar (symbol-value id-dep) header))
9155             (set id-dep (list header)))
9156           (when header
9157             (if (boundp (setq ref-dep (intern ref dependencies)))
9158                 (setcdr (symbol-value ref-dep)
9159                         (nconc (cdr (symbol-value ref-dep))
9160                                (list (symbol-value id-dep))))
9161               (set ref-dep (list nil (symbol-value id-dep))))
9162             (setq headers (cons header headers)))
9163           (goto-char (point-max))
9164           (widen))
9165         (nreverse headers)))))
9166
9167 ;; The following macros and functions were written by Felix Lee
9168 ;; <flee@cse.psu.edu>.
9169
9170 (defmacro gnus-nov-read-integer ()
9171   '(prog1
9172        (if (= (following-char) ?\t)
9173            0
9174          (let ((num (condition-case nil (read buffer) (error nil))))
9175            (if (numberp num) num 0)))
9176      (or (eobp) (forward-char 1))))
9177
9178 (defmacro gnus-nov-skip-field ()
9179   '(search-forward "\t" eol 'move))
9180
9181 (defmacro gnus-nov-field ()
9182   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9183
9184 ;; Goes through the xover lines and returns a list of vectors
9185 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9186                                                   force-new dependencies)
9187   "Parse the news overview data in the server buffer, and return a
9188 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9189   ;; Get the Xref when the users reads the articles since most/some
9190   ;; NNTP servers do not include Xrefs when using XOVER.
9191   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9192   (let ((cur nntp-server-buffer)
9193         (dependencies (or dependencies gnus-newsgroup-dependencies))
9194         number headers header)
9195     (save-excursion
9196       (set-buffer nntp-server-buffer)
9197       ;; Allow the user to mangle the headers before parsing them.
9198       (run-hooks 'gnus-parse-headers-hook)
9199       (goto-char (point-min))
9200       (while (and sequence (not (eobp)))
9201         (setq number (read cur))
9202         (while (and sequence (< (car sequence) number))
9203           (setq sequence (cdr sequence)))
9204         (and sequence
9205              (eq number (car sequence))
9206              (progn
9207                (setq sequence (cdr sequence))
9208                (if (setq header
9209                          (inline (gnus-nov-parse-line
9210                                   number dependencies force-new)))
9211                    (setq headers (cons header headers)))))
9212         (forward-line 1))
9213       (setq headers (nreverse headers)))
9214     headers))
9215
9216 ;; This function has to be called with point after the article number
9217 ;; on the beginning of the line.
9218 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9219   (let ((none 0)
9220         (eol (gnus-point-at-eol))
9221         (buffer (current-buffer))
9222         header ref id id-dep ref-dep)
9223
9224     ;; overview: [num subject from date id refs chars lines misc]
9225     (narrow-to-region (point) eol)
9226     (or (eobp) (forward-char))
9227
9228     (condition-case nil
9229         (setq header
9230               (vector
9231                number                   ; number
9232                (gnus-nov-field)         ; subject
9233                (gnus-nov-field)         ; from
9234                (gnus-nov-field)         ; date
9235                (setq id (or (gnus-nov-field)
9236                             (concat "none+"
9237                                     (int-to-string
9238                                      (setq none (1+ none)))))) ; id
9239                (progn
9240                  (save-excursion
9241                    (let ((beg (point)))
9242                      (search-forward "\t" eol)
9243                      (if (search-backward ">" beg t)
9244                          (setq ref
9245                                (buffer-substring
9246                                 (1+ (point))
9247                                 (search-backward "<" beg t)))
9248                        (setq ref nil))))
9249                  (gnus-nov-field))      ; refs
9250                (gnus-nov-read-integer)  ; chars
9251                (gnus-nov-read-integer)  ; lines
9252                (if (= (following-char) ?\n)
9253                    nil
9254                  (gnus-nov-field))      ; misc
9255                ))
9256       (error (progn
9257                (gnus-error 4 "Strange nov line")
9258                (setq header nil)
9259                (goto-char eol))))
9260
9261     (widen)
9262
9263     ;; We build the thread tree.
9264     (when header
9265       (if (boundp (setq id-dep (intern id dependencies)))
9266           (if (and (car (symbol-value id-dep))
9267                    (not force-new))
9268               ;; An article with this Message-ID has already been seen,
9269               ;; so we ignore this one, except we add any additional
9270               ;; Xrefs (in case the two articles came from different
9271               ;; servers.
9272               (progn
9273                 (mail-header-set-xref
9274                  (car (symbol-value id-dep))
9275                  (concat (or (mail-header-xref
9276                               (car (symbol-value id-dep))) "")
9277                          (or (mail-header-xref header) "")))
9278                 (setq header nil))
9279             (setcar (symbol-value id-dep) header))
9280         (set id-dep (list header))))
9281     (if header
9282         (progn
9283           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9284               (setcdr (symbol-value ref-dep)
9285                       (nconc (cdr (symbol-value ref-dep))
9286                              (list (symbol-value id-dep))))
9287             (set ref-dep (list nil (symbol-value id-dep))))))
9288     header))
9289
9290 (defun gnus-article-get-xrefs ()
9291   "Fill in the Xref value in `gnus-current-headers', if necessary.
9292 This is meant to be called in `gnus-article-internal-prepare-hook'."
9293   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9294                                  gnus-current-headers)))
9295     (or (not gnus-use-cross-reference)
9296         (not headers)
9297         (and (mail-header-xref headers)
9298              (not (string= (mail-header-xref headers) "")))
9299         (let ((case-fold-search t)
9300               xref)
9301           (save-restriction
9302             (nnheader-narrow-to-headers)
9303             (goto-char (point-min))
9304             (if (or (and (eq (downcase (following-char)) ?x)
9305                          (looking-at "Xref:"))
9306                     (search-forward "\nXref:" nil t))
9307                 (progn
9308                   (goto-char (1+ (match-end 0)))
9309                   (setq xref (buffer-substring (point)
9310                                                (progn (end-of-line) (point))))
9311                   (mail-header-set-xref headers xref))))))))
9312
9313 (defun gnus-summary-insert-subject (id &optional old-header)
9314   "Find article ID and insert the summary line for that article."
9315   (let ((header (gnus-read-header id))
9316         (number (and (numberp id) id))
9317         pos)
9318     (when header
9319       ;; Rebuild the thread that this article is part of and go to the
9320       ;; article we have fetched.
9321       (when old-header
9322         (when (setq pos (text-property-any
9323                          (point-min) (point-max) 'gnus-number 
9324                          (mail-header-number old-header)))
9325           (goto-char pos)
9326           (gnus-delete-line)
9327           (gnus-data-remove (mail-header-number old-header))))
9328       (when old-header
9329         (mail-header-set-number header (mail-header-number old-header)))
9330       (setq gnus-newsgroup-sparse
9331             (delq (mail-header-number header) gnus-newsgroup-sparse))
9332       (gnus-rebuild-thread (mail-header-id header))
9333       (gnus-summary-goto-subject (setq number (mail-header-number header))
9334                                  nil t))
9335     (when (and (numberp number)
9336                (> number 0))
9337       ;; We have to update the boundaries even if we can't fetch the
9338       ;; article if ID is a number -- so that the next `P' or `N'
9339       ;; command will fetch the previous (or next) article even
9340       ;; if the one we tried to fetch this time has been canceled.
9341       (and (> number gnus-newsgroup-end)
9342            (setq gnus-newsgroup-end number))
9343       (and (< number gnus-newsgroup-begin)
9344            (setq gnus-newsgroup-begin number))
9345       (setq gnus-newsgroup-unselected
9346             (delq number gnus-newsgroup-unselected)))
9347     ;; Report back a success?
9348     (and header (mail-header-number header))))
9349
9350 (defun gnus-summary-work-articles (n)
9351   "Return a list of articles to be worked upon.  The prefix argument,
9352 the list of process marked articles, and the current article will be
9353 taken into consideration."
9354   (cond
9355    ((and n (numberp n))
9356     ;; A numerical prefix has been given.
9357     (let ((backward (< n 0))
9358           (n (abs n))
9359           articles article)
9360       (save-excursion
9361         (while
9362             (and (> n 0)
9363                  (push (setq article (gnus-summary-article-number))
9364                        articles)
9365                  (if backward
9366                      (gnus-summary-find-prev nil article)
9367                    (gnus-summary-find-next nil article)))
9368           (decf n)))
9369       (nreverse articles)))
9370    ((and (boundp 'transient-mark-mode)
9371          transient-mark-mode
9372          mark-active)
9373     ;; Work on the region between point and mark.
9374     (let ((max (max (point) (mark)))
9375           articles article)
9376       (save-excursion
9377         (goto-char (min (point) (mark)))
9378         (while
9379             (and
9380              (push (setq article (gnus-summary-article-number)) articles)
9381              (gnus-summary-find-next nil article)
9382              (< (point) max)))
9383         (nreverse articles))))
9384    (gnus-newsgroup-processable
9385     ;; There are process-marked articles present.
9386     (reverse gnus-newsgroup-processable))
9387    (t
9388     ;; Just return the current article.
9389     (list (gnus-summary-article-number)))))
9390
9391 (defun gnus-summary-search-group (&optional backward use-level)
9392   "Search for next unread newsgroup.
9393 If optional argument BACKWARD is non-nil, search backward instead."
9394   (save-excursion
9395     (set-buffer gnus-group-buffer)
9396     (if (gnus-group-search-forward
9397          backward nil (if use-level (gnus-group-group-level) nil))
9398         (gnus-group-group-name))))
9399
9400 (defun gnus-summary-best-group (&optional exclude-group)
9401   "Find the name of the best unread group.
9402 If EXCLUDE-GROUP, do not go to this group."
9403   (save-excursion
9404     (set-buffer gnus-group-buffer)
9405     (save-excursion
9406       (gnus-group-best-unread-group exclude-group))))
9407
9408 (defun gnus-summary-find-next (&optional unread article backward)
9409   (if backward (gnus-summary-find-prev)
9410     (let* ((dummy (gnus-summary-article-intangible-p))
9411            (article (or article (gnus-summary-article-number)))
9412            (arts (gnus-data-find-list article))
9413            result)
9414       (when (and (not dummy)
9415                  (or (not gnus-summary-check-current)
9416                      (not unread)
9417                      (not (gnus-data-unread-p (car arts)))))
9418         (setq arts (cdr arts)))
9419       (when (setq result
9420                   (if unread
9421                       (progn
9422                         (while arts
9423                           (when (gnus-data-unread-p (car arts))
9424                             (setq result (car arts)
9425                                   arts nil))
9426                           (setq arts (cdr arts)))
9427                         result)
9428                     (car arts)))
9429         (goto-char (gnus-data-pos result))
9430         (gnus-data-number result)))))
9431
9432 (defun gnus-summary-find-prev (&optional unread article)
9433   (let* ((article (or article (gnus-summary-article-number)))
9434          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9435          result)
9436     (when (or (not gnus-summary-check-current)
9437               (not unread)
9438               (not (gnus-data-unread-p (car arts))))
9439       (setq arts (cdr arts)))
9440     (if (setq result
9441               (if unread
9442                   (progn
9443                     (while arts
9444                       (and (gnus-data-unread-p (car arts))
9445                            (setq result (car arts)
9446                                  arts nil))
9447                       (setq arts (cdr arts)))
9448                     result)
9449                 (car arts)))
9450         (progn
9451           (goto-char (gnus-data-pos result))
9452           (gnus-data-number result)))))
9453
9454 (defun gnus-summary-find-subject (subject &optional unread backward article)
9455   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9456          (article (or article (gnus-summary-article-number)))
9457          (articles (gnus-data-list backward))
9458          (arts (gnus-data-find-list article articles))
9459          result)
9460     (when (or (not gnus-summary-check-current)
9461               (not unread)
9462               (not (gnus-data-unread-p (car arts))))
9463       (setq arts (cdr arts)))
9464     (while arts
9465       (and (or (not unread)
9466                (gnus-data-unread-p (car arts)))
9467            (vectorp (gnus-data-header (car arts)))
9468            (gnus-subject-equal
9469             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9470            (setq result (car arts)
9471                  arts nil))
9472       (setq arts (cdr arts)))
9473     (and result
9474          (goto-char (gnus-data-pos result))
9475          (gnus-data-number result))))
9476
9477 (defun gnus-summary-search-forward (&optional unread subject backward)
9478   "Search forward for an article.
9479 If UNREAD, look for unread articles.  If SUBJECT, look for
9480 articles with that subject.  If BACKWARD, search backward instead."
9481   (cond (subject (gnus-summary-find-subject subject unread backward))
9482         (backward (gnus-summary-find-prev unread))
9483         (t (gnus-summary-find-next unread))))
9484
9485 (defun gnus-recenter (&optional n)
9486   "Center point in window and redisplay frame.
9487 Also do horizontal recentering."
9488   (interactive "P")
9489   (when (and gnus-auto-center-summary
9490              (not (eq gnus-auto-center-summary 'vertical)))
9491     (gnus-horizontal-recenter))
9492   (recenter n))
9493
9494 (defun gnus-summary-recenter ()
9495   "Center point in the summary window.
9496 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9497 displayed, no centering will be performed."
9498   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9499   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9500   (let* ((top (cond ((< (window-height) 4) 0)
9501                     ((< (window-height) 7) 1)
9502                     (t 2)))
9503          (height (1- (window-height)))
9504          (bottom (save-excursion (goto-char (point-max))
9505                                  (forward-line (- height))
9506                                  (point)))
9507          (window (get-buffer-window (current-buffer))))
9508     ;; The user has to want it.
9509     (when gnus-auto-center-summary
9510       (when (get-buffer-window gnus-article-buffer)
9511        ;; Only do recentering when the article buffer is displayed,
9512        ;; Set the window start to either `bottom', which is the biggest
9513        ;; possible valid number, or the second line from the top,
9514        ;; whichever is the least.
9515        (set-window-start
9516         window (min bottom (save-excursion 
9517                              (forward-line (- top)) (point)))))
9518       ;; Do horizontal recentering while we're at it.
9519       (when (and (get-buffer-window (current-buffer) t)
9520                  (not (eq gnus-auto-center-summary 'vertical)))
9521         (let ((selected (selected-window)))
9522           (select-window (get-buffer-window (current-buffer) t))
9523           (gnus-summary-position-point)
9524           (gnus-horizontal-recenter)
9525           (select-window selected))))))
9526
9527 (defun gnus-horizontal-recenter ()
9528   "Recenter the current buffer horizontally."
9529   (if (< (current-column) (/ (window-width) 2))
9530       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9531     (let* ((orig (point))
9532            (end (window-end (get-buffer-window (current-buffer) t)))
9533            (max 0))
9534       ;; Find the longest line currently displayed in the window.
9535       (goto-char (window-start))
9536       (while (and (not (eobp)) 
9537                   (< (point) end))
9538         (end-of-line)
9539         (setq max (max max (current-column)))
9540         (forward-line 1))
9541       (goto-char orig)
9542       ;; Scroll horizontally to center (sort of) the point.
9543       (if (> max (window-width))
9544           (set-window-hscroll 
9545            (get-buffer-window (current-buffer) t)
9546            (min (- (current-column) (/ (window-width) 3))
9547                 (+ 2 (- max (window-width)))))
9548         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9549       max)))
9550
9551 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9552 (defun gnus-short-group-name (group &optional levels)
9553   "Collapse GROUP name LEVELS."
9554   (let* ((name "") 
9555          (foreign "")
9556          (depth 0) 
9557          (skip 1)
9558          (levels (or levels
9559                      (progn
9560                        (while (string-match "\\." group skip)
9561                          (setq skip (match-end 0)
9562                                depth (+ depth 1)))
9563                        depth))))
9564     (if (string-match ":" group)
9565         (setq foreign (substring group 0 (match-end 0))
9566               group (substring group (match-end 0))))
9567     (while group
9568       (if (and (string-match "\\." group)
9569                (> levels (- gnus-group-uncollapsed-levels 1)))
9570           (setq name (concat name (substring group 0 1))
9571                 group (substring group (match-end 0))
9572                 levels (- levels 1)
9573                 name (concat name "."))
9574         (setq name (concat foreign name group)
9575               group nil)))
9576     name))
9577
9578 (defun gnus-summary-jump-to-group (newsgroup)
9579   "Move point to NEWSGROUP in group mode buffer."
9580   ;; Keep update point of group mode buffer if visible.
9581   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9582       (save-window-excursion
9583         ;; Take care of tree window mode.
9584         (if (get-buffer-window gnus-group-buffer)
9585             (pop-to-buffer gnus-group-buffer))
9586         (gnus-group-jump-to-group newsgroup))
9587     (save-excursion
9588       ;; Take care of tree window mode.
9589       (if (get-buffer-window gnus-group-buffer)
9590           (pop-to-buffer gnus-group-buffer)
9591         (set-buffer gnus-group-buffer))
9592       (gnus-group-jump-to-group newsgroup))))
9593
9594 ;; This function returns a list of article numbers based on the
9595 ;; difference between the ranges of read articles in this group and
9596 ;; the range of active articles.
9597 (defun gnus-list-of-unread-articles (group)
9598   (let* ((read (gnus-info-read (gnus-get-info group)))
9599          (active (gnus-active group))
9600          (last (cdr active))
9601          first nlast unread)
9602     ;; If none are read, then all are unread.
9603     (if (not read)
9604         (setq first (car active))
9605       ;; If the range of read articles is a single range, then the
9606       ;; first unread article is the article after the last read
9607       ;; article.  Sounds logical, doesn't it?
9608       (if (not (listp (cdr read)))
9609           (setq first (1+ (cdr read)))
9610         ;; `read' is a list of ranges.
9611         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9612                                 (caar read))) 1)
9613             (setq first 1))
9614         (while read
9615           (if first
9616               (while (< first nlast)
9617                 (setq unread (cons first unread))
9618                 (setq first (1+ first))))
9619           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9620           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9621           (setq read (cdr read)))))
9622     ;; And add the last unread articles.
9623     (while (<= first last)
9624       (setq unread (cons first unread))
9625       (setq first (1+ first)))
9626     ;; Return the list of unread articles.
9627     (nreverse unread)))
9628
9629 (defun gnus-list-of-read-articles (group)
9630   "Return a list of unread, unticked and non-dormant articles."
9631   (let* ((info (gnus-get-info group))
9632          (marked (gnus-info-marks info))
9633          (active (gnus-active group)))
9634     (and info active
9635          (gnus-set-difference
9636           (gnus-sorted-complement
9637            (gnus-uncompress-range active)
9638            (gnus-list-of-unread-articles group))
9639           (append
9640            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9641            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9642
9643 ;; Various summary commands
9644
9645 (defun gnus-summary-universal-argument (arg)
9646   "Perform any operation on all articles that are process/prefixed."
9647   (interactive "P")
9648   (gnus-set-global-variables)
9649   (let ((articles (gnus-summary-work-articles arg))
9650         func article)
9651     (if (eq
9652          (setq
9653           func
9654           (key-binding
9655            (read-key-sequence
9656             (substitute-command-keys
9657              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9658              ))))
9659          'undefined)
9660         (gnus-error 1 "Undefined key")
9661       (save-excursion
9662         (while articles
9663           (gnus-summary-goto-subject (setq article (pop articles)))
9664           (command-execute func)
9665           (gnus-summary-remove-process-mark article)))))
9666   (gnus-summary-position-point))
9667
9668 (defun gnus-summary-toggle-truncation (&optional arg)
9669   "Toggle truncation of summary lines.
9670 With arg, turn line truncation on iff arg is positive."
9671   (interactive "P")
9672   (setq truncate-lines
9673         (if (null arg) (not truncate-lines)
9674           (> (prefix-numeric-value arg) 0)))
9675   (redraw-display))
9676
9677 (defun gnus-summary-reselect-current-group (&optional all rescan)
9678   "Exit and then reselect the current newsgroup.
9679 The prefix argument ALL means to select all articles."
9680   (interactive "P")
9681   (gnus-set-global-variables)
9682   (let ((current-subject (gnus-summary-article-number))
9683         (group gnus-newsgroup-name))
9684     (setq gnus-newsgroup-begin nil)
9685     (gnus-summary-exit)
9686     ;; We have to adjust the point of group mode buffer because the
9687     ;; current point was moved to the next unread newsgroup by
9688     ;; exiting.
9689     (gnus-summary-jump-to-group group)
9690     (when rescan
9691       (save-excursion
9692         (gnus-group-get-new-news-this-group 1)))
9693     (gnus-group-read-group all t)
9694     (gnus-summary-goto-subject current-subject)))
9695
9696 (defun gnus-summary-rescan-group (&optional all)
9697   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9698   (interactive "P")
9699   (gnus-summary-reselect-current-group all t))
9700
9701 (defun gnus-summary-update-info ()
9702   (let* ((group gnus-newsgroup-name))
9703     (when gnus-newsgroup-kill-headers
9704       (setq gnus-newsgroup-killed
9705             (gnus-compress-sequence
9706              (nconc
9707               (gnus-set-sorted-intersection
9708                (gnus-uncompress-range gnus-newsgroup-killed)
9709                (setq gnus-newsgroup-unselected
9710                      (sort gnus-newsgroup-unselected '<)))
9711               (setq gnus-newsgroup-unreads
9712                     (sort gnus-newsgroup-unreads '<))) t)))
9713     (unless (listp (cdr gnus-newsgroup-killed))
9714       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9715     (let ((headers gnus-newsgroup-headers))
9716       (run-hooks 'gnus-exit-group-hook)
9717       (unless gnus-save-score
9718         (setq gnus-newsgroup-scored nil))
9719       ;; Set the new ranges of read articles.
9720       (gnus-update-read-articles
9721        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9722       ;; Set the current article marks.
9723       (gnus-update-marks)
9724       ;; Do the cross-ref thing.
9725       (when gnus-use-cross-reference
9726         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9727       ;; Do adaptive scoring, and possibly save score files.
9728       (when gnus-newsgroup-adaptive
9729         (gnus-score-adaptive))
9730       (when gnus-use-scoring
9731         (gnus-score-save))
9732       ;; Do not switch windows but change the buffer to work.
9733       (set-buffer gnus-group-buffer)
9734       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9735           (gnus-group-update-group group)))))
9736
9737 (defun gnus-summary-exit (&optional temporary)
9738   "Exit reading current newsgroup, and then return to group selection mode.
9739 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9740   (interactive)
9741   (gnus-set-global-variables)
9742   (gnus-kill-save-kill-buffer)
9743   (let* ((group gnus-newsgroup-name)
9744          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9745          (mode major-mode)
9746          (buf (current-buffer)))
9747     (run-hooks 'gnus-summary-prepare-exit-hook)
9748     ;; If we have several article buffers, we kill them at exit.
9749     (unless gnus-single-article-buffer
9750       (gnus-kill-buffer gnus-original-article-buffer)
9751       (setq gnus-article-current nil))
9752     (when gnus-use-cache
9753       (gnus-cache-possibly-remove-articles)
9754       (gnus-cache-save-buffers))
9755     (when gnus-use-trees
9756       (gnus-tree-close group))
9757     ;; Make all changes in this group permanent.
9758     (unless quit-config
9759       (gnus-summary-update-info))
9760     (gnus-close-group group)
9761     ;; Make sure where I was, and go to next newsgroup.
9762     (set-buffer gnus-group-buffer)
9763     (unless quit-config
9764       (gnus-group-jump-to-group group)
9765       (gnus-group-next-unread-group 1))
9766     (run-hooks 'gnus-summary-exit-hook)
9767     (if temporary
9768         nil                             ;Nothing to do.
9769       ;; If we have several article buffers, we kill them at exit.
9770       (unless gnus-single-article-buffer
9771         (gnus-kill-buffer gnus-article-buffer)
9772         (gnus-kill-buffer gnus-original-article-buffer)
9773         (setq gnus-article-current nil))
9774       (set-buffer buf)
9775       (if (not gnus-kill-summary-on-exit)
9776           (gnus-deaden-summary)
9777         ;; We set all buffer-local variables to nil.  It is unclear why
9778         ;; this is needed, but if we don't, buffer-local variables are
9779         ;; not garbage-collected, it seems.  This would the lead to en
9780         ;; ever-growing Emacs.
9781         (gnus-summary-clear-local-variables)
9782         (when (get-buffer gnus-article-buffer)
9783           (bury-buffer gnus-article-buffer))
9784         ;; We clear the global counterparts of the buffer-local
9785         ;; variables as well, just to be on the safe side.
9786         (gnus-configure-windows 'group 'force)
9787         (gnus-summary-clear-local-variables)
9788         ;; Return to group mode buffer.
9789         (if (eq mode 'gnus-summary-mode)
9790             (gnus-kill-buffer buf)))
9791       (setq gnus-current-select-method gnus-select-method)
9792       (pop-to-buffer gnus-group-buffer)
9793       ;; Clear the current group name.
9794       (if (not quit-config)
9795           (progn
9796             (gnus-group-jump-to-group group)
9797             (gnus-group-next-unread-group 1)
9798             (gnus-configure-windows 'group 'force))
9799         (if (not (buffer-name (car quit-config)))
9800             (gnus-configure-windows 'group 'force)
9801           (set-buffer (car quit-config))
9802           (and (eq major-mode 'gnus-summary-mode)
9803                (gnus-set-global-variables))
9804           (gnus-configure-windows (cdr quit-config))))
9805       (unless quit-config
9806         (setq gnus-newsgroup-name nil)))))
9807
9808 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9809 (defun gnus-summary-exit-no-update (&optional no-questions)
9810   "Quit reading current newsgroup without updating read article info."
9811   (interactive)
9812   (gnus-set-global-variables)
9813   (let* ((group gnus-newsgroup-name)
9814          (quit-config (gnus-group-quit-config group)))
9815     (when (or no-questions
9816               gnus-expert-user
9817               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9818       ;; If we have several article buffers, we kill them at exit.
9819       (unless gnus-single-article-buffer
9820         (gnus-kill-buffer gnus-article-buffer)
9821         (gnus-kill-buffer gnus-original-article-buffer)
9822         (setq gnus-article-current nil))
9823       (if (not gnus-kill-summary-on-exit)
9824           (gnus-deaden-summary)
9825         (gnus-close-group group)
9826         (gnus-summary-clear-local-variables)
9827         (set-buffer gnus-group-buffer)
9828         (gnus-summary-clear-local-variables)
9829         (when (get-buffer gnus-summary-buffer)
9830           (kill-buffer gnus-summary-buffer)))
9831       (unless gnus-single-article-buffer
9832         (setq gnus-article-current nil))
9833       (when gnus-use-trees
9834         (gnus-tree-close group))
9835       (when (get-buffer gnus-article-buffer)
9836         (bury-buffer gnus-article-buffer))
9837       ;; Return to the group buffer.
9838       (gnus-configure-windows 'group 'force)
9839       ;; Clear the current group name.
9840       (setq gnus-newsgroup-name nil)
9841       (when (equal (gnus-group-group-name) group)
9842         (gnus-group-next-unread-group 1))
9843       (when quit-config
9844         (if (not (buffer-name (car quit-config)))
9845             (gnus-configure-windows 'group 'force)
9846           (set-buffer (car quit-config))
9847           (when (eq major-mode 'gnus-summary-mode)
9848             (gnus-set-global-variables))
9849           (gnus-configure-windows (cdr quit-config)))))))
9850
9851 ;;; Dead summaries.
9852
9853 (defvar gnus-dead-summary-mode-map nil)
9854
9855 (if gnus-dead-summary-mode-map
9856     nil
9857   (setq gnus-dead-summary-mode-map (make-keymap))
9858   (suppress-keymap gnus-dead-summary-mode-map)
9859   (substitute-key-definition
9860    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9861   (let ((keys '("\C-d" "\r" "\177")))
9862     (while keys
9863       (define-key gnus-dead-summary-mode-map
9864         (pop keys) 'gnus-summary-wake-up-the-dead))))
9865
9866 (defvar gnus-dead-summary-mode nil
9867   "Minor mode for Gnus summary buffers.")
9868
9869 (defun gnus-dead-summary-mode (&optional arg)
9870   "Minor mode for Gnus summary buffers."
9871   (interactive "P")
9872   (when (eq major-mode 'gnus-summary-mode)
9873     (make-local-variable 'gnus-dead-summary-mode)
9874     (setq gnus-dead-summary-mode
9875           (if (null arg) (not gnus-dead-summary-mode)
9876             (> (prefix-numeric-value arg) 0)))
9877     (when gnus-dead-summary-mode
9878       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9879         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9880       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9881         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9882               minor-mode-map-alist)))))
9883
9884 (defun gnus-deaden-summary ()
9885   "Make the current summary buffer into a dead summary buffer."
9886   ;; Kill any previous dead summary buffer.
9887   (when (and gnus-dead-summary
9888              (buffer-name gnus-dead-summary))
9889     (save-excursion
9890       (set-buffer gnus-dead-summary)
9891       (when gnus-dead-summary-mode
9892         (kill-buffer (current-buffer)))))
9893   ;; Make this the current dead summary.
9894   (setq gnus-dead-summary (current-buffer))
9895   (gnus-dead-summary-mode 1)
9896   (let ((name (buffer-name)))
9897     (when (string-match "Summary" name)
9898       (rename-buffer
9899        (concat (substring name 0 (match-beginning 0)) "Dead "
9900                (substring name (match-beginning 0))) t))))
9901
9902 (defun gnus-kill-or-deaden-summary (buffer)
9903   "Kill or deaden the summary BUFFER."
9904   (when (and (buffer-name buffer)
9905              (not gnus-single-article-buffer))
9906     (save-excursion
9907       (set-buffer buffer)
9908       (gnus-kill-buffer gnus-article-buffer)
9909       (gnus-kill-buffer gnus-original-article-buffer)))
9910   (cond (gnus-kill-summary-on-exit
9911          (when (and gnus-use-trees
9912                     (and (get-buffer buffer)
9913                          (buffer-name (get-buffer buffer))))
9914            (save-excursion
9915              (set-buffer (get-buffer buffer))
9916              (gnus-tree-close gnus-newsgroup-name)))
9917          (gnus-kill-buffer buffer))
9918         ((and (get-buffer buffer)
9919               (buffer-name (get-buffer buffer)))
9920          (save-excursion
9921            (set-buffer buffer)
9922            (gnus-deaden-summary)))))
9923
9924 (defun gnus-summary-wake-up-the-dead (&rest args)
9925   "Wake up the dead summary buffer."
9926   (interactive)
9927   (gnus-dead-summary-mode -1)
9928   (let ((name (buffer-name)))
9929     (when (string-match "Dead " name)
9930       (rename-buffer
9931        (concat (substring name 0 (match-beginning 0))
9932                (substring name (match-end 0))) t)))
9933   (gnus-message 3 "This dead summary is now alive again"))
9934
9935 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9936 (defun gnus-summary-fetch-faq (&optional faq-dir)
9937   "Fetch the FAQ for the current group.
9938 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9939 in."
9940   (interactive
9941    (list
9942     (if current-prefix-arg
9943         (completing-read
9944          "Faq dir: " (and (listp gnus-group-faq-directory)
9945                           gnus-group-faq-directory)))))
9946   (let (gnus-faq-buffer)
9947     (and (setq gnus-faq-buffer
9948                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9949          (gnus-configure-windows 'summary-faq))))
9950
9951 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9952 (defun gnus-summary-describe-group (&optional force)
9953   "Describe the current newsgroup."
9954   (interactive "P")
9955   (gnus-group-describe-group force gnus-newsgroup-name))
9956
9957 (defun gnus-summary-describe-briefly ()
9958   "Describe summary mode commands briefly."
9959   (interactive)
9960   (gnus-message 6
9961                 (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")))
9962
9963 ;; Walking around group mode buffer from summary mode.
9964
9965 (defun gnus-summary-next-group (&optional no-article target-group backward)
9966   "Exit current newsgroup and then select next unread newsgroup.
9967 If prefix argument NO-ARTICLE is non-nil, no article is selected
9968 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9969 previous group instead."
9970   (interactive "P")
9971   (gnus-set-global-variables)
9972   (let ((current-group gnus-newsgroup-name)
9973         (current-buffer (current-buffer))
9974         entered)
9975     ;; First we semi-exit this group to update Xrefs and all variables.
9976     ;; We can't do a real exit, because the window conf must remain
9977     ;; the same in case the user is prompted for info, and we don't
9978     ;; want the window conf to change before that...
9979     (gnus-summary-exit t)
9980     (while (not entered)
9981       ;; Then we find what group we are supposed to enter.
9982       (set-buffer gnus-group-buffer)
9983       (gnus-group-jump-to-group current-group)
9984       (setq target-group
9985             (or target-group
9986                 (if (eq gnus-keep-same-level 'best)
9987                     (gnus-summary-best-group gnus-newsgroup-name)
9988                   (gnus-summary-search-group backward gnus-keep-same-level))))
9989       (if (not target-group)
9990           ;; There are no further groups, so we return to the group
9991           ;; buffer.
9992           (progn
9993             (gnus-message 5 "Returning to the group buffer")
9994             (setq entered t)
9995             (set-buffer current-buffer)
9996             (gnus-summary-exit))
9997         ;; We try to enter the target group.
9998         (gnus-group-jump-to-group target-group)
9999         (let ((unreads (gnus-group-group-unread)))
10000           (if (and (or (eq t unreads)
10001                        (and unreads (not (zerop unreads))))
10002                    (gnus-summary-read-group
10003                     target-group nil no-article current-buffer))
10004               (setq entered t)
10005             (setq current-group target-group
10006                   target-group nil)))))))
10007
10008 (defun gnus-summary-prev-group (&optional no-article)
10009   "Exit current newsgroup and then select previous unread newsgroup.
10010 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10011   (interactive "P")
10012   (gnus-summary-next-group no-article nil t))
10013
10014 ;; Walking around summary lines.
10015
10016 (defun gnus-summary-first-subject (&optional unread)
10017   "Go to the first unread subject.
10018 If UNREAD is non-nil, go to the first unread article.
10019 Returns the article selected or nil if there are no unread articles."
10020   (interactive "P")
10021   (prog1
10022       (cond
10023        ;; Empty summary.
10024        ((null gnus-newsgroup-data)
10025         (gnus-message 3 "No articles in the group")
10026         nil)
10027        ;; Pick the first article.
10028        ((not unread)
10029         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10030         (gnus-data-number (car gnus-newsgroup-data)))
10031        ;; No unread articles.
10032        ((null gnus-newsgroup-unreads)
10033         (gnus-message 3 "No more unread articles")
10034         nil)
10035        ;; Find the first unread article.
10036        (t
10037         (let ((data gnus-newsgroup-data))
10038           (while (and data
10039                       (not (gnus-data-unread-p (car data))))
10040             (setq data (cdr data)))
10041           (if data
10042               (progn
10043                 (goto-char (gnus-data-pos (car data)))
10044                 (gnus-data-number (car data)))))))
10045     (gnus-summary-position-point)))
10046
10047 (defun gnus-summary-next-subject (n &optional unread dont-display)
10048   "Go to next N'th summary line.
10049 If N is negative, go to the previous N'th subject line.
10050 If UNREAD is non-nil, only unread articles are selected.
10051 The difference between N and the actual number of steps taken is
10052 returned."
10053   (interactive "p")
10054   (let ((backward (< n 0))
10055         (n (abs n)))
10056     (while (and (> n 0)
10057                 (if backward
10058                     (gnus-summary-find-prev unread)
10059                   (gnus-summary-find-next unread)))
10060       (setq n (1- n)))
10061     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10062                                (if unread " unread" "")))
10063     (unless dont-display
10064       (gnus-summary-recenter)
10065       (gnus-summary-position-point))
10066     n))
10067
10068 (defun gnus-summary-next-unread-subject (n)
10069   "Go to next N'th unread summary line."
10070   (interactive "p")
10071   (gnus-summary-next-subject n t))
10072
10073 (defun gnus-summary-prev-subject (n &optional unread)
10074   "Go to previous N'th summary line.
10075 If optional argument UNREAD is non-nil, only unread article is selected."
10076   (interactive "p")
10077   (gnus-summary-next-subject (- n) unread))
10078
10079 (defun gnus-summary-prev-unread-subject (n)
10080   "Go to previous N'th unread summary line."
10081   (interactive "p")
10082   (gnus-summary-next-subject (- n) t))
10083
10084 (defun gnus-summary-goto-subject (article &optional force silent)
10085   "Go the subject line of ARTICLE.
10086 If FORCE, also allow jumping to articles not currently shown."
10087   (let ((b (point))
10088         (data (gnus-data-find article)))
10089     ;; We read in the article if we have to.
10090     (and (not data)
10091          force
10092          (gnus-summary-insert-subject article)
10093          (setq data (gnus-data-find article)))
10094     (goto-char b)
10095     (if (not data)
10096         (progn
10097           (unless silent
10098             (gnus-message 3 "Can't find article %d" article))
10099           nil)
10100       (goto-char (gnus-data-pos data))
10101       article)))
10102
10103 ;; Walking around summary lines with displaying articles.
10104
10105 (defun gnus-summary-expand-window (&optional arg)
10106   "Make the summary buffer take up the entire Emacs frame.
10107 Given a prefix, will force an `article' buffer configuration."
10108   (interactive "P")
10109   (gnus-set-global-variables)
10110   (if arg
10111       (gnus-configure-windows 'article 'force)
10112     (gnus-configure-windows 'summary 'force)))
10113
10114 (defun gnus-summary-display-article (article &optional all-header)
10115   "Display ARTICLE in article buffer."
10116   (gnus-set-global-variables)
10117   (if (null article)
10118       nil
10119     (prog1
10120         (if gnus-summary-display-article-function
10121             (funcall gnus-summary-display-article-function article all-header)
10122           (gnus-article-prepare article all-header))
10123       (run-hooks 'gnus-select-article-hook)
10124       (unless (zerop gnus-current-article)
10125         (gnus-summary-goto-subject gnus-current-article))
10126       (gnus-summary-recenter)
10127       (when gnus-use-trees
10128         (gnus-possibly-generate-tree article)
10129         (gnus-highlight-selected-tree article))
10130       ;; Successfully display article.
10131       (gnus-article-set-window-start
10132        (cdr (assq article gnus-newsgroup-bookmarks))))))
10133
10134 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10135   "Select the current article.
10136 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10137 non-nil, the article will be re-fetched even if it already present in
10138 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10139 be displayed."
10140   ;; Make sure we are in the summary buffer to work around bbdb bug.
10141   (unless (eq major-mode 'gnus-summary-mode)
10142     (set-buffer gnus-summary-buffer))
10143   (let ((article (or article (gnus-summary-article-number)))
10144         (all-headers (not (not all-headers))) ;Must be T or NIL.
10145         gnus-summary-display-article-function
10146         did)
10147     (and (not pseudo)
10148          (gnus-summary-article-pseudo-p article)
10149          (error "This is a pseudo-article."))
10150     (prog1
10151         (save-excursion
10152           (set-buffer gnus-summary-buffer)
10153           (if (or (and gnus-single-article-buffer
10154                        (or (null gnus-current-article)
10155                            (null gnus-article-current)
10156                            (null (get-buffer gnus-article-buffer))
10157                            (not (eq article (cdr gnus-article-current)))
10158                            (not (equal (car gnus-article-current)
10159                                        gnus-newsgroup-name))))
10160                   (and (not gnus-single-article-buffer)
10161                        (or (null gnus-current-article)
10162                            (not (eq gnus-current-article article))))
10163                   force)
10164               ;; The requested article is different from the current article.
10165               (prog1
10166                   (gnus-summary-display-article article all-headers)
10167                 (setq did article))
10168             (if (or all-headers gnus-show-all-headers)
10169                 (gnus-article-show-all-headers))
10170             'old))
10171       (if did
10172           (gnus-article-set-window-start
10173            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10174
10175 (defun gnus-summary-set-current-mark (&optional current-mark)
10176   "Obsolete function."
10177   nil)
10178
10179 (defun gnus-summary-next-article (&optional unread subject backward push)
10180   "Select the next article.
10181 If UNREAD, only unread articles are selected.
10182 If SUBJECT, only articles with SUBJECT are selected.
10183 If BACKWARD, the previous article is selected instead of the next."
10184   (interactive "P")
10185   (gnus-set-global-variables)
10186   (cond
10187    ;; Is there such an article?
10188    ((and (gnus-summary-search-forward unread subject backward)
10189          (or (gnus-summary-display-article (gnus-summary-article-number))
10190              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10191     (gnus-summary-position-point))
10192    ;; If not, we try the first unread, if that is wanted.
10193    ((and subject
10194          gnus-auto-select-same
10195          (or (gnus-summary-first-unread-article)
10196              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10197     (gnus-summary-position-point)
10198     (gnus-message 6 "Wrapped"))
10199    ;; Try to get next/previous article not displayed in this group.
10200    ((and gnus-auto-extend-newsgroup
10201          (not unread) (not subject))
10202     (gnus-summary-goto-article
10203      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10204      nil t))
10205    ;; Go to next/previous group.
10206    (t
10207     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10208         (gnus-summary-jump-to-group gnus-newsgroup-name))
10209     (let ((cmd last-command-char)
10210           (group
10211            (if (eq gnus-keep-same-level 'best)
10212                (gnus-summary-best-group gnus-newsgroup-name)
10213              (gnus-summary-search-group backward gnus-keep-same-level))))
10214       ;; For some reason, the group window gets selected.  We change
10215       ;; it back.
10216       (select-window (get-buffer-window (current-buffer)))
10217       ;; Select next unread newsgroup automagically.
10218       (cond
10219        ((not gnus-auto-select-next)
10220         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10221        ((or (eq gnus-auto-select-next 'quietly)
10222             (and (eq gnus-auto-select-next 'slightly-quietly)
10223                  push)
10224             (and (eq gnus-auto-select-next 'almost-quietly)
10225                  (gnus-summary-last-article-p)))
10226         ;; Select quietly.
10227         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10228             (gnus-summary-exit)
10229           (gnus-message 7 "No more%s articles (%s)..."
10230                         (if unread " unread" "")
10231                         (if group (concat "selecting " group)
10232                           "exiting"))
10233           (gnus-summary-next-group nil group backward)))
10234        (t
10235         (gnus-summary-walk-group-buffer
10236          gnus-newsgroup-name cmd unread backward)))))))
10237
10238 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10239   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10240                       (?\C-p (gnus-group-prev-unread-group 1))))
10241         keve key group ended)
10242     (save-excursion
10243       (set-buffer gnus-group-buffer)
10244       (gnus-summary-jump-to-group from-group)
10245       (setq group
10246             (if (eq gnus-keep-same-level 'best)
10247                 (gnus-summary-best-group gnus-newsgroup-name)
10248               (gnus-summary-search-group backward gnus-keep-same-level))))
10249     (while (not ended)
10250       (gnus-message
10251        5 "No more%s articles%s" (if unread " unread" "")
10252        (if (and group
10253                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10254            (format " (Type %s for %s [%s])"
10255                    (single-key-description cmd) group
10256                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10257          (format " (Type %s to exit %s)"
10258                  (single-key-description cmd)
10259                  gnus-newsgroup-name)))
10260       ;; Confirm auto selection.
10261       (setq key (car (setq keve (gnus-read-event-char))))
10262       (setq ended t)
10263       (cond
10264        ((assq key keystrokes)
10265         (let ((obuf (current-buffer)))
10266           (switch-to-buffer gnus-group-buffer)
10267           (and group
10268                (gnus-group-jump-to-group group))
10269           (eval (cadr (assq key keystrokes)))
10270           (setq group (gnus-group-group-name))
10271           (switch-to-buffer obuf))
10272         (setq ended nil))
10273        ((equal key cmd)
10274         (if (or (not group)
10275                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10276             (gnus-summary-exit)
10277           (gnus-summary-next-group nil group backward)))
10278        (t
10279         (push (cdr keve) unread-command-events))))))
10280
10281 (defun gnus-read-event-char ()
10282   "Get the next event."
10283   (let ((event (read-event)))
10284     (cons (and (numberp event) event) event)))
10285
10286 (defun gnus-summary-next-unread-article ()
10287   "Select unread article after current one."
10288   (interactive)
10289   (gnus-summary-next-article t (and gnus-auto-select-same
10290                                     (gnus-summary-article-subject))))
10291
10292 (defun gnus-summary-prev-article (&optional unread subject)
10293   "Select the article after the current one.
10294 If UNREAD is non-nil, only unread articles are selected."
10295   (interactive "P")
10296   (gnus-summary-next-article unread subject t))
10297
10298 (defun gnus-summary-prev-unread-article ()
10299   "Select unred article before current one."
10300   (interactive)
10301   (gnus-summary-prev-article t (and gnus-auto-select-same
10302                                     (gnus-summary-article-subject))))
10303
10304 (defun gnus-summary-next-page (&optional lines circular)
10305   "Show next page of the selected article.
10306 If at the end of the current article, select the next article.
10307 LINES says how many lines should be scrolled up.
10308
10309 If CIRCULAR is non-nil, go to the start of the article instead of
10310 selecting the next article when reaching the end of the current
10311 article."
10312   (interactive "P")
10313   (setq gnus-summary-buffer (current-buffer))
10314   (gnus-set-global-variables)
10315   (let ((article (gnus-summary-article-number))
10316         (endp nil))
10317     (gnus-configure-windows 'article)
10318     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10319         (if (and (eq gnus-summary-goto-unread 'never)
10320                  (not (gnus-summary-last-article-p article)))
10321             (gnus-summary-next-article)
10322           (gnus-summary-next-unread-article))
10323       (if (or (null gnus-current-article)
10324               (null gnus-article-current)
10325               (/= article (cdr gnus-article-current))
10326               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10327           ;; Selected subject is different from current article's.
10328           (gnus-summary-display-article article)
10329         (gnus-eval-in-buffer-window
10330          gnus-article-buffer
10331          (setq endp (gnus-article-next-page lines)))
10332         (if endp
10333             (cond (circular
10334                    (gnus-summary-beginning-of-article))
10335                   (lines
10336                    (gnus-message 3 "End of message"))
10337                   ((null lines)
10338                    (if (and (eq gnus-summary-goto-unread 'never)
10339                             (not (gnus-summary-last-article-p article)))
10340                        (gnus-summary-next-article)
10341                      (gnus-summary-next-unread-article)))))))
10342     (gnus-summary-recenter)
10343     (gnus-summary-position-point)))
10344
10345 (defun gnus-summary-prev-page (&optional lines)
10346   "Show previous page of selected article.
10347 Argument LINES specifies lines to be scrolled down."
10348   (interactive "P")
10349   (gnus-set-global-variables)
10350   (let ((article (gnus-summary-article-number)))
10351     (gnus-configure-windows 'article)
10352     (if (or (null gnus-current-article)
10353             (null gnus-article-current)
10354             (/= article (cdr gnus-article-current))
10355             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10356         ;; Selected subject is different from current article's.
10357         (gnus-summary-display-article article)
10358       (gnus-summary-recenter)
10359       (gnus-eval-in-buffer-window gnus-article-buffer
10360                                   (gnus-article-prev-page lines))))
10361   (gnus-summary-position-point))
10362
10363 (defun gnus-summary-scroll-up (lines)
10364   "Scroll up (or down) one line current article.
10365 Argument LINES specifies lines to be scrolled up (or down if negative)."
10366   (interactive "p")
10367   (gnus-set-global-variables)
10368   (gnus-configure-windows 'article)
10369   (gnus-summary-show-thread)
10370   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10371     (gnus-eval-in-buffer-window
10372      gnus-article-buffer
10373      (cond ((> lines 0)
10374             (if (gnus-article-next-page lines)
10375                 (gnus-message 3 "End of message")))
10376            ((< lines 0)
10377             (gnus-article-prev-page (- lines))))))
10378   (gnus-summary-recenter)
10379   (gnus-summary-position-point))
10380
10381 (defun gnus-summary-next-same-subject ()
10382   "Select next article which has the same subject as current one."
10383   (interactive)
10384   (gnus-set-global-variables)
10385   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10386
10387 (defun gnus-summary-prev-same-subject ()
10388   "Select previous article which has the same subject as current one."
10389   (interactive)
10390   (gnus-set-global-variables)
10391   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10392
10393 (defun gnus-summary-next-unread-same-subject ()
10394   "Select next unread article which has the same subject as current one."
10395   (interactive)
10396   (gnus-set-global-variables)
10397   (gnus-summary-next-article t (gnus-summary-article-subject)))
10398
10399 (defun gnus-summary-prev-unread-same-subject ()
10400   "Select previous unread article which has the same subject as current one."
10401   (interactive)
10402   (gnus-set-global-variables)
10403   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10404
10405 (defun gnus-summary-first-unread-article ()
10406   "Select the first unread article.
10407 Return nil if there are no unread articles."
10408   (interactive)
10409   (gnus-set-global-variables)
10410   (prog1
10411       (if (gnus-summary-first-subject t)
10412           (progn
10413             (gnus-summary-show-thread)
10414             (gnus-summary-first-subject t)
10415             (gnus-summary-display-article (gnus-summary-article-number))))
10416     (gnus-summary-position-point)))
10417
10418 (defun gnus-summary-best-unread-article ()
10419   "Select the unread article with the highest score."
10420   (interactive)
10421   (gnus-set-global-variables)
10422   (let ((best -1000000)
10423         (data gnus-newsgroup-data)
10424         article score)
10425     (while data
10426       (and (gnus-data-unread-p (car data))
10427            (> (setq score
10428                     (gnus-summary-article-score (gnus-data-number (car data))))
10429               best)
10430            (setq best score
10431                  article (gnus-data-number (car data))))
10432       (setq data (cdr data)))
10433     (prog1
10434         (if article
10435             (gnus-summary-goto-article article)
10436           (error "No unread articles"))
10437       (gnus-summary-position-point))))
10438
10439 (defun gnus-summary-last-subject ()
10440   "Go to the last displayed subject line in the group."
10441   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10442     (when article
10443       (gnus-summary-goto-subject article))))
10444
10445 (defun gnus-summary-goto-article (article &optional all-headers force)
10446   "Fetch ARTICLE and display it if it exists.
10447 If ALL-HEADERS is non-nil, no header lines are hidden."
10448   (interactive
10449    (list
10450     (string-to-int
10451      (completing-read
10452       "Article number: "
10453       (mapcar (lambda (number) (list (int-to-string number)))
10454               gnus-newsgroup-limit)))
10455     current-prefix-arg
10456     t))
10457   (prog1
10458       (if (gnus-summary-goto-subject article force)
10459           (gnus-summary-display-article article all-headers)
10460         (gnus-message 4 "Couldn't go to article %s" article) nil)
10461     (gnus-summary-position-point)))
10462
10463 (defun gnus-summary-goto-last-article ()
10464   "Go to the previously read article."
10465   (interactive)
10466   (prog1
10467       (and gnus-last-article
10468            (gnus-summary-goto-article gnus-last-article))
10469     (gnus-summary-position-point)))
10470
10471 (defun gnus-summary-pop-article (number)
10472   "Pop one article off the history and go to the previous.
10473 NUMBER articles will be popped off."
10474   (interactive "p")
10475   (let (to)
10476     (setq gnus-newsgroup-history
10477           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10478     (if to
10479         (gnus-summary-goto-article (car to))
10480       (error "Article history empty")))
10481   (gnus-summary-position-point))
10482
10483 ;; Summary commands and functions for limiting the summary buffer.
10484
10485 (defun gnus-summary-limit-to-articles (n)
10486   "Limit the summary buffer to the next N articles.
10487 If not given a prefix, use the process marked articles instead."
10488   (interactive "P")
10489   (gnus-set-global-variables)
10490   (prog1
10491       (let ((articles (gnus-summary-work-articles n)))
10492         (setq gnus-newsgroup-processable nil)
10493         (gnus-summary-limit articles))
10494     (gnus-summary-position-point)))
10495
10496 (defun gnus-summary-pop-limit (&optional total)
10497   "Restore the previous limit.
10498 If given a prefix, remove all limits."
10499   (interactive "P")
10500   (gnus-set-global-variables)
10501   (when total 
10502     (setq gnus-newsgroup-limits
10503           (list (mapcar (lambda (h) (mail-header-number h))
10504                         gnus-newsgroup-headers))))
10505   (unless gnus-newsgroup-limits
10506     (error "No limit to pop"))
10507   (prog1
10508       (gnus-summary-limit nil 'pop)
10509     (gnus-summary-position-point)))
10510
10511 (defun gnus-summary-limit-to-subject (subject &optional header)
10512   "Limit the summary buffer to articles that have subjects that match a regexp."
10513   (interactive "sRegexp: ")
10514   (unless header
10515     (setq header "subject"))
10516   (when (not (equal "" subject))
10517     (prog1
10518         (let ((articles (gnus-summary-find-matching
10519                          (or header "subject") subject 'all)))
10520           (or articles (error "Found no matches for \"%s\"" subject))
10521           (gnus-summary-limit articles))
10522       (gnus-summary-position-point))))
10523
10524 (defun gnus-summary-limit-to-author (from)
10525   "Limit the summary buffer to articles that have authors that match a regexp."
10526   (interactive "sRegexp: ")
10527   (gnus-summary-limit-to-subject from "from"))
10528
10529 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10530 (make-obsolete
10531  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10532
10533 (defun gnus-summary-limit-to-unread (&optional all)
10534   "Limit the summary buffer to articles that are not marked as read.
10535 If ALL is non-nil, limit strictly to unread articles."
10536   (interactive "P")
10537   (if all
10538       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10539     (gnus-summary-limit-to-marks
10540      ;; Concat all the marks that say that an article is read and have
10541      ;; those removed.
10542      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10543            gnus-killed-mark gnus-kill-file-mark
10544            gnus-low-score-mark gnus-expirable-mark
10545            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10546      'reverse)))
10547
10548 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10549 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10550
10551 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10552   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10553 If REVERSE, limit the summary buffer to articles that are not marked
10554 with MARKS.  MARKS can either be a string of marks or a list of marks.
10555 Returns how many articles were removed."
10556   (interactive "sMarks: ")
10557   (gnus-set-global-variables)
10558   (prog1
10559       (let ((data gnus-newsgroup-data)
10560             (marks (if (listp marks) marks
10561                      (append marks nil))) ; Transform to list.
10562             articles)
10563         (while data
10564           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10565                  (memq (gnus-data-mark (car data)) marks))
10566                (setq articles (cons (gnus-data-number (car data)) articles)))
10567           (setq data (cdr data)))
10568         (gnus-summary-limit articles))
10569     (gnus-summary-position-point)))
10570
10571 (defun gnus-summary-limit-to-score (&optional score)
10572   "Limit to articles with score at or above SCORE."
10573   (interactive "P")
10574   (gnus-set-global-variables)
10575   (setq score (if score
10576                   (prefix-numeric-value score)
10577                 (or gnus-summary-default-score 0)))
10578   (let ((data gnus-newsgroup-data)
10579         articles)
10580     (while data
10581       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10582                 score)
10583         (push (gnus-data-number (car data)) articles))
10584       (setq data (cdr data)))
10585     (prog1
10586         (gnus-summary-limit articles)
10587       (gnus-summary-position-point))))
10588
10589 (defun gnus-summary-limit-include-dormant ()
10590   "Display all the hidden articles that are marked as dormant."
10591   (interactive)
10592   (gnus-set-global-variables)
10593   (or gnus-newsgroup-dormant
10594       (error "There are no dormant articles in this group"))
10595   (prog1
10596       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10597     (gnus-summary-position-point)))
10598
10599 (defun gnus-summary-limit-exclude-dormant ()
10600   "Hide all dormant articles."
10601   (interactive)
10602   (gnus-set-global-variables)
10603   (prog1
10604       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10605     (gnus-summary-position-point)))
10606
10607 (defun gnus-summary-limit-exclude-childless-dormant ()
10608   "Hide all dormant articles that have no children."
10609   (interactive)
10610   (gnus-set-global-variables)
10611   (let ((data (gnus-data-list t))
10612         articles d children)
10613     ;; Find all articles that are either not dormant or have
10614     ;; children.
10615     (while (setq d (pop data))
10616       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10617                 (and (setq children 
10618                            (gnus-article-children (gnus-data-number d)))
10619                      (let (found)
10620                        (while children
10621                          (when (memq (car children) articles)
10622                            (setq children nil
10623                                  found t))
10624                          (pop children))
10625                        found)))
10626         (push (gnus-data-number d) articles)))
10627     ;; Do the limiting.
10628     (prog1
10629         (gnus-summary-limit articles)
10630       (gnus-summary-position-point))))
10631
10632 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10633   "Mark all unread excluded articles as read.
10634 If ALL, mark even excluded ticked and dormants as read."
10635   (interactive "P")
10636   (let ((articles (gnus-sorted-complement
10637                    (sort
10638                     (mapcar (lambda (h) (mail-header-number h))
10639                             gnus-newsgroup-headers)
10640                     '<)
10641                    (sort gnus-newsgroup-limit '<)))
10642         article)
10643     (setq gnus-newsgroup-unreads nil)
10644     (if all
10645         (setq gnus-newsgroup-dormant nil
10646               gnus-newsgroup-marked nil
10647               gnus-newsgroup-reads
10648               (nconc
10649                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10650                gnus-newsgroup-reads))
10651       (while (setq article (pop articles))
10652         (unless (or (memq article gnus-newsgroup-dormant)
10653                     (memq article gnus-newsgroup-marked))
10654           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10655
10656 (defun gnus-summary-limit (articles &optional pop)
10657   (if pop
10658       ;; We pop the previous limit off the stack and use that.
10659       (setq articles (car gnus-newsgroup-limits)
10660             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10661     ;; We use the new limit, so we push the old limit on the stack.
10662     (setq gnus-newsgroup-limits
10663           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10664   ;; Set the limit.
10665   (setq gnus-newsgroup-limit articles)
10666   (let ((total (length gnus-newsgroup-data))
10667         (data (gnus-data-find-list (gnus-summary-article-number)))
10668         found)
10669     ;; This will do all the work of generating the new summary buffer
10670     ;; according to the new limit.
10671     (gnus-summary-prepare)
10672     ;; Hide any threads, possibly.
10673     (and gnus-show-threads
10674          gnus-thread-hide-subtree
10675          (gnus-summary-hide-all-threads))
10676     ;; Try to return to the article you were at, or one in the
10677     ;; neighborhood.
10678     (if data
10679         ;; We try to find some article after the current one.
10680         (while data
10681           (and (gnus-summary-goto-subject
10682                 (gnus-data-number (car data)) nil t)
10683                (setq data nil
10684                      found t))
10685           (setq data (cdr data))))
10686     (or found
10687         ;; If there is no data, that means that we were after the last
10688         ;; article.  The same goes when we can't find any articles
10689         ;; after the current one.
10690         (progn
10691           (goto-char (point-max))
10692           (gnus-summary-find-prev)))
10693     ;; We return how many articles were removed from the summary
10694     ;; buffer as a result of the new limit.
10695     (- total (length gnus-newsgroup-data))))
10696
10697 (defsubst gnus-cut-thread (thread)
10698   "Go forwards in the thread until we find an article that we want to display."
10699   (when (eq gnus-fetch-old-headers 'some)
10700     ;; Deal with old-fetched headers.
10701     (while (and thread
10702                 (memq (mail-header-number (car thread)) 
10703                       gnus-newsgroup-ancient)
10704                 (<= (length (cdr thread)) 1))
10705       (setq thread (cadr thread))))
10706   ;; Deal with sparse threads.
10707   (when (or (eq gnus-build-sparse-threads 'some)
10708             (eq gnus-build-sparse-threads 'more))
10709     (while (and thread
10710                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10711                 (= (length (cdr thread)) 1))
10712       (setq thread (cadr thread))))
10713   thread)
10714
10715 (defun gnus-cut-threads (threads)
10716   "Cut off all uninteresting articles from the beginning of threads."
10717   (when (or (eq gnus-fetch-old-headers 'some)
10718             (eq gnus-build-sparse-threads 'some)
10719             (eq gnus-build-sparse-threads 'more))
10720     (let ((th threads))
10721       (while th
10722         (setcar th (gnus-cut-thread (car th)))
10723         (setq th (cdr th)))))
10724   ;; Remove nixed out threads.
10725   (delq nil threads))
10726
10727 (defun gnus-summary-initial-limit (&optional show-if-empty)
10728   "Figure out what the initial limit is supposed to be on group entry.
10729 This entails weeding out unwanted dormants, low-scored articles,
10730 fetch-old-headers verbiage, and so on."
10731   ;; Most groups have nothing to remove.
10732   (if (or gnus-inhibit-limiting
10733           (and (null gnus-newsgroup-dormant)
10734                (not (eq gnus-fetch-old-headers 'some))
10735                (null gnus-summary-expunge-below)
10736                (not (eq gnus-build-sparse-threads 'some))
10737                (not (eq gnus-build-sparse-threads 'more))
10738                (null gnus-thread-expunge-below)
10739                (not gnus-use-nocem)))
10740       () ; Do nothing.
10741     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10742     (setq gnus-newsgroup-limit nil)
10743     (mapatoms
10744      (lambda (node)
10745        (unless (car (symbol-value node))
10746          ;; These threads have no parents -- they are roots.
10747          (let ((nodes (cdr (symbol-value node)))
10748                thread)
10749            (while nodes
10750              (if (and gnus-thread-expunge-below
10751                       (< (gnus-thread-total-score (car nodes))
10752                          gnus-thread-expunge-below))
10753                  (gnus-expunge-thread (pop nodes))
10754                (setq thread (pop nodes))
10755                (gnus-summary-limit-children thread))))))
10756      gnus-newsgroup-dependencies)
10757     ;; If this limitation resulted in an empty group, we might
10758     ;; pop the previous limit and use it instead.
10759     (when (and (not gnus-newsgroup-limit)
10760                show-if-empty)
10761       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10762     gnus-newsgroup-limit))
10763
10764 (defun gnus-summary-limit-children (thread)
10765   "Return 1 if this subthread is visible and 0 if it is not."
10766   ;; First we get the number of visible children to this thread.  This
10767   ;; is done by recursing down the thread using this function, so this
10768   ;; will really go down to a leaf article first, before slowly
10769   ;; working its way up towards the root.
10770   (when thread
10771     (let ((children
10772            (if (cdr thread)
10773                (apply '+ (mapcar 'gnus-summary-limit-children
10774                                  (cdr thread)))
10775              0))
10776           (number (mail-header-number (car thread)))
10777           score)
10778       (if (or
10779            ;; If this article is dormant and has absolutely no visible
10780            ;; children, then this article isn't visible.
10781            (and (memq number gnus-newsgroup-dormant)
10782                 (= children 0))
10783            ;; If this is a "fetch-old-headered" and there is only one
10784            ;; visible child (or less), then we don't want this article.
10785            (and (eq gnus-fetch-old-headers 'some)
10786                 (memq number gnus-newsgroup-ancient)
10787                 (zerop children))
10788            ;; If this is a sparsely inserted article with no children,
10789            ;; we don't want it.
10790            (and (eq gnus-build-sparse-threads 'some)
10791                 (memq number gnus-newsgroup-sparse)
10792                 (zerop children))
10793            ;; If we use expunging, and this article is really
10794            ;; low-scored, then we don't want this article.
10795            (when (and gnus-summary-expunge-below
10796                       (< (setq score
10797                                (or (cdr (assq number gnus-newsgroup-scored))
10798                                    gnus-summary-default-score))
10799                          gnus-summary-expunge-below))
10800              ;; We increase the expunge-tally here, but that has
10801              ;; nothing to do with the limits, really.
10802              (incf gnus-newsgroup-expunged-tally)
10803              ;; We also mark as read here, if that's wanted.
10804              (when (and gnus-summary-mark-below
10805                         (< score gnus-summary-mark-below))
10806                (setq gnus-newsgroup-unreads
10807                      (delq number gnus-newsgroup-unreads))
10808                (if gnus-newsgroup-auto-expire
10809                    (push number gnus-newsgroup-expirable)
10810                  (push (cons number gnus-low-score-mark)
10811                        gnus-newsgroup-reads)))
10812              t)
10813            (and gnus-use-nocem
10814                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10815           ;; Nope, invisible article.
10816           0
10817         ;; Ok, this article is to be visible, so we add it to the limit
10818         ;; and return 1.
10819         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10820         1))))
10821
10822 (defun gnus-expunge-thread (thread)
10823   "Mark all articles in THREAD as read."
10824   (let* ((number (mail-header-number (car thread))))
10825     (incf gnus-newsgroup-expunged-tally)
10826     ;; We also mark as read here, if that's wanted.
10827     (setq gnus-newsgroup-unreads
10828           (delq number gnus-newsgroup-unreads))
10829     (if gnus-newsgroup-auto-expire
10830         (push number gnus-newsgroup-expirable)
10831       (push (cons number gnus-low-score-mark)
10832             gnus-newsgroup-reads)))
10833   ;; Go recursively through all subthreads.
10834   (mapcar 'gnus-expunge-thread (cdr thread)))
10835
10836 ;; Summary article oriented commands
10837
10838 (defun gnus-summary-refer-parent-article (n)
10839   "Refer parent article N times.
10840 The difference between N and the number of articles fetched is returned."
10841   (interactive "p")
10842   (gnus-set-global-variables)
10843   (while
10844       (and
10845        (> n 0)
10846        (let* ((header (gnus-summary-article-header))
10847               (ref
10848                ;; If we try to find the parent of the currently
10849                ;; displayed article, then we take a look at the actual
10850                ;; References header, since this is slightly more
10851                ;; reliable than the References field we got from the
10852                ;; server.
10853                (if (and (eq (mail-header-number header)
10854                             (cdr gnus-article-current))
10855                         (equal gnus-newsgroup-name
10856                                (car gnus-article-current)))
10857                    (save-excursion
10858                      (set-buffer gnus-original-article-buffer)
10859                      (nnheader-narrow-to-headers)
10860                      (prog1
10861                          (mail-fetch-field "references")
10862                        (widen)))
10863                  ;; It's not the current article, so we take a bet on
10864                  ;; the value we got from the server.
10865                  (mail-header-references header))))
10866          (if (setq ref (or ref (mail-header-references header)))
10867              (or (gnus-summary-refer-article (gnus-parent-id ref))
10868                  (gnus-message 1 "Couldn't find parent"))
10869            (gnus-message 1 "No references in article %d"
10870                          (gnus-summary-article-number))
10871            nil)))
10872     (setq n (1- n)))
10873   (gnus-summary-position-point)
10874   n)
10875
10876 (defun gnus-summary-refer-references ()
10877   "Fetch all articles mentioned in the References header.
10878 Return how many articles were fetched."
10879   (interactive)
10880   (gnus-set-global-variables)
10881   (let ((ref (mail-header-references (gnus-summary-article-header)))
10882         (current (gnus-summary-article-number))
10883         (n 0))
10884     ;; For each Message-ID in the References header...
10885     (while (string-match "<[^>]*>" ref)
10886       (incf n)
10887       ;; ... fetch that article.
10888       (gnus-summary-refer-article
10889        (prog1 (match-string 0 ref)
10890          (setq ref (substring ref (match-end 0))))))
10891     (gnus-summary-goto-subject current)
10892     (gnus-summary-position-point)
10893     n))
10894
10895 (defun gnus-summary-refer-article (message-id)
10896   "Fetch an article specified by MESSAGE-ID."
10897   (interactive "sMessage-ID: ")
10898   (when (and (stringp message-id)
10899              (not (zerop (length message-id))))
10900     ;; Construct the correct Message-ID if necessary.
10901     ;; Suggested by tale@pawl.rpi.edu.
10902     (unless (string-match "^<" message-id)
10903       (setq message-id (concat "<" message-id)))
10904     (unless (string-match ">$" message-id)
10905       (setq message-id (concat message-id ">")))
10906     (let ((header (car (gnus-gethash message-id
10907                                      gnus-newsgroup-dependencies))))
10908       (if header
10909           ;; The article is present in the buffer, to we just go to it.
10910           (gnus-summary-goto-article (mail-header-number header) nil t)
10911         ;; We fetch the article
10912         (let ((gnus-override-method 
10913                (and (gnus-news-group-p gnus-newsgroup-name)
10914                     gnus-refer-article-method))
10915               number)
10916           ;; Start the special refer-article method, if necessary.
10917           (when gnus-refer-article-method
10918             (gnus-check-server gnus-refer-article-method))
10919           ;; Fetch the header, and display the article.
10920           (if (setq number (gnus-summary-insert-subject message-id))
10921               (gnus-summary-select-article nil nil nil number)
10922             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10923
10924 (defun gnus-summary-enter-digest-group (&optional force)
10925   "Enter a digest group based on the current article."
10926   (interactive "P")
10927   (gnus-set-global-variables)
10928   (gnus-summary-select-article)
10929   (let ((name (format "%s-%d"
10930                       (gnus-group-prefixed-name
10931                        gnus-newsgroup-name (list 'nndoc ""))
10932                       gnus-current-article))
10933         (ogroup gnus-newsgroup-name)
10934         (case-fold-search t)
10935         (buf (current-buffer))
10936         dig)
10937     (save-excursion
10938       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10939       (insert-buffer-substring gnus-original-article-buffer)
10940       (narrow-to-region
10941        (goto-char (point-min))
10942        (or (search-forward "\n\n" nil t) (point)))
10943       (goto-char (point-min))
10944       (delete-matching-lines "^\\(Path\\):\\|^From ")
10945       (widen))
10946     (unwind-protect
10947         (if (gnus-group-read-ephemeral-group
10948              name `(nndoc ,name (nndoc-address
10949                                  ,(get-buffer dig))
10950                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10951             ;; Make all postings to this group go to the parent group.
10952             (nconc (gnus-info-params (gnus-get-info name))
10953                    (list (cons 'to-group ogroup)))
10954           ;; Couldn't select this doc group.
10955           (switch-to-buffer buf)
10956           (gnus-set-global-variables)
10957           (gnus-configure-windows 'summary)
10958           (gnus-message 3 "Article couldn't be entered?"))
10959       (kill-buffer dig))))
10960
10961 (defun gnus-summary-isearch-article (&optional regexp-p)
10962   "Do incremental search forward on the current article.
10963 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10964   (interactive "P")
10965   (gnus-set-global-variables)
10966   (gnus-summary-select-article)
10967   (gnus-configure-windows 'article)
10968   (gnus-eval-in-buffer-window
10969    gnus-article-buffer
10970    (goto-char (point-min))
10971    (isearch-forward regexp-p)))
10972
10973 (defun gnus-summary-search-article-forward (regexp &optional backward)
10974   "Search for an article containing REGEXP forward.
10975 If BACKWARD, search backward instead."
10976   (interactive
10977    (list (read-string
10978           (format "Search article %s (regexp%s): "
10979                   (if current-prefix-arg "backward" "forward")
10980                   (if gnus-last-search-regexp
10981                       (concat ", default " gnus-last-search-regexp)
10982                     "")))
10983          current-prefix-arg))
10984   (gnus-set-global-variables)
10985   (if (string-equal regexp "")
10986       (setq regexp (or gnus-last-search-regexp ""))
10987     (setq gnus-last-search-regexp regexp))
10988   (if (gnus-summary-search-article regexp backward)
10989       (gnus-article-set-window-start
10990        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10991     (error "Search failed: \"%s\"" regexp)))
10992
10993 (defun gnus-summary-search-article-backward (regexp)
10994   "Search for an article containing REGEXP backward."
10995   (interactive
10996    (list (read-string
10997           (format "Search article backward (regexp%s): "
10998                   (if gnus-last-search-regexp
10999                       (concat ", default " gnus-last-search-regexp)
11000                     "")))))
11001   (gnus-summary-search-article-forward regexp 'backward))
11002
11003 (defun gnus-summary-search-article (regexp &optional backward)
11004   "Search for an article containing REGEXP.
11005 Optional argument BACKWARD means do search for backward.
11006 gnus-select-article-hook is not called during the search."
11007   (let ((gnus-select-article-hook nil)  ;Disable hook.
11008         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11009         (re-search
11010          (if backward
11011              (function re-search-backward) (function re-search-forward)))
11012         (found nil)
11013         (last nil))
11014     ;; Hidden thread subtrees must be searched for ,too.
11015     (gnus-summary-show-all-threads)
11016     ;; First of all, search current article.
11017     ;; We don't want to read article again from NNTP server nor reset
11018     ;; current point.
11019     (gnus-summary-select-article)
11020     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11021     (setq last gnus-current-article)
11022     (gnus-eval-in-buffer-window
11023      gnus-article-buffer
11024      (save-restriction
11025        (widen)
11026        ;; Begin search from current point.
11027        (setq found (funcall re-search regexp nil t))))
11028     ;; Then search next articles.
11029     (while (and (not found)
11030                 (gnus-summary-display-article
11031                  (if backward (gnus-summary-find-prev)
11032                    (gnus-summary-find-next))))
11033       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11034       (gnus-eval-in-buffer-window
11035        gnus-article-buffer
11036        (save-restriction
11037          (widen)
11038          (goto-char (if backward (point-max) (point-min)))
11039          (setq found (funcall re-search regexp nil t)))))
11040     (message "")
11041     ;; Adjust article pointer.
11042     (or (eq last gnus-current-article)
11043         (setq gnus-last-article last))
11044     ;; Return T if found such article.
11045     found))
11046
11047 (defun gnus-summary-find-matching (header regexp &optional backward unread
11048                                           not-case-fold)
11049   "Return a list of all articles that match REGEXP on HEADER.
11050 The search stars on the current article and goes forwards unless
11051 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11052 If UNREAD is non-nil, only unread articles will
11053 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11054 in the comparisons."
11055   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11056                 (gnus-data-find-list
11057                  (gnus-summary-article-number) (gnus-data-list backward))))
11058         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11059         (case-fold-search (not not-case-fold))
11060         articles d)
11061     (or (fboundp (intern (concat "mail-header-" header)))
11062         (error "%s is not a valid header" header))
11063     (while data
11064       (setq d (car data))
11065       (and (or (not unread)             ; We want all articles...
11066                (gnus-data-unread-p d))  ; Or just unreads.
11067            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11068            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11069            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11070       (setq data (cdr data)))
11071     (nreverse articles)))
11072
11073 (defun gnus-summary-execute-command (header regexp command &optional backward)
11074   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11075 If HEADER is an empty string (or nil), the match is done on the entire
11076 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11077   (interactive
11078    (list (let ((completion-ignore-case t))
11079            (completing-read
11080             "Header name: "
11081             (mapcar (lambda (string) (list string))
11082                     '("Number" "Subject" "From" "Lines" "Date"
11083                       "Message-ID" "Xref" "References" "Body"))
11084             nil 'require-match))
11085          (read-string "Regexp: ")
11086          (read-key-sequence "Command: ")
11087          current-prefix-arg))
11088   (when (equal header "Body")
11089     (setq header ""))
11090   (gnus-set-global-variables)
11091   ;; Hidden thread subtrees must be searched as well.
11092   (gnus-summary-show-all-threads)
11093   ;; We don't want to change current point nor window configuration.
11094   (save-excursion
11095     (save-window-excursion
11096       (gnus-message 6 "Executing %s..." (key-description command))
11097       ;; We'd like to execute COMMAND interactively so as to give arguments.
11098       (gnus-execute header regexp
11099                     `(lambda () (call-interactively ',(key-binding command)))
11100                     backward)
11101       (gnus-message 6 "Executing %s...done" (key-description command)))))
11102
11103 (defun gnus-summary-beginning-of-article ()
11104   "Scroll the article back to the beginning."
11105   (interactive)
11106   (gnus-set-global-variables)
11107   (gnus-summary-select-article)
11108   (gnus-configure-windows 'article)
11109   (gnus-eval-in-buffer-window
11110    gnus-article-buffer
11111    (widen)
11112    (goto-char (point-min))
11113    (and gnus-break-pages (gnus-narrow-to-page))))
11114
11115 (defun gnus-summary-end-of-article ()
11116   "Scroll to the end of the article."
11117   (interactive)
11118   (gnus-set-global-variables)
11119   (gnus-summary-select-article)
11120   (gnus-configure-windows 'article)
11121   (gnus-eval-in-buffer-window
11122    gnus-article-buffer
11123    (widen)
11124    (goto-char (point-max))
11125    (recenter -3)
11126    (and gnus-break-pages (gnus-narrow-to-page))))
11127
11128 (defun gnus-summary-show-article (&optional arg)
11129   "Force re-fetching of the current article.
11130 If ARG (the prefix) is non-nil, show the raw article without any
11131 article massaging functions being run."
11132   (interactive "P")
11133   (gnus-set-global-variables)
11134   (if (not arg)
11135       ;; Select the article the normal way.
11136       (gnus-summary-select-article nil 'force)
11137     ;; Bind the article treatment functions to nil.
11138     (let ((gnus-have-all-headers t)
11139           gnus-article-display-hook
11140           gnus-article-prepare-hook
11141           gnus-break-pages
11142           gnus-visual)
11143       (gnus-summary-select-article nil 'force)))
11144   (gnus-summary-goto-subject gnus-current-article)
11145 ;  (gnus-configure-windows 'article)
11146   (gnus-summary-position-point))
11147
11148 (defun gnus-summary-verbose-headers (&optional arg)
11149   "Toggle permanent full header display.
11150 If ARG is a positive number, turn header display on.
11151 If ARG is a negative number, turn header display off."
11152   (interactive "P")
11153   (gnus-set-global-variables)
11154   (gnus-summary-toggle-header arg)
11155   (setq gnus-show-all-headers
11156         (cond ((or (not (numberp arg))
11157                    (zerop arg))
11158                (not gnus-show-all-headers))
11159               ((natnump arg)
11160                t))))
11161
11162 (defun gnus-summary-toggle-header (&optional arg)
11163   "Show the headers if they are hidden, or hide them if they are shown.
11164 If ARG is a positive number, show the entire header.
11165 If ARG is a negative number, hide the unwanted header lines."
11166   (interactive "P")
11167   (gnus-set-global-variables)
11168   (save-excursion
11169     (set-buffer gnus-article-buffer)
11170     (let* ((buffer-read-only nil)
11171            (inhibit-point-motion-hooks t)
11172            (hidden (text-property-any
11173                     (goto-char (point-min)) (search-forward "\n\n")
11174                     'invisible t))
11175            e)
11176       (goto-char (point-min))
11177       (when (search-forward "\n\n" nil t)
11178         (delete-region (point-min) (1- (point))))
11179       (goto-char (point-min))
11180       (save-excursion
11181         (set-buffer gnus-original-article-buffer)
11182         (goto-char (point-min))
11183         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11184       (insert-buffer-substring gnus-original-article-buffer 1 e)
11185       (let ((gnus-inhibit-hiding t))
11186         (run-hooks 'gnus-article-display-hook))
11187       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11188           (gnus-article-hide-headers)))))
11189
11190 (defun gnus-summary-show-all-headers ()
11191   "Make all header lines visible."
11192   (interactive)
11193   (gnus-set-global-variables)
11194   (gnus-article-show-all-headers))
11195
11196 (defun gnus-summary-toggle-mime (&optional arg)
11197   "Toggle MIME processing.
11198 If ARG is a positive number, turn MIME processing on."
11199   (interactive "P")
11200   (gnus-set-global-variables)
11201   (setq gnus-show-mime
11202         (if (null arg) (not gnus-show-mime)
11203           (> (prefix-numeric-value arg) 0)))
11204   (gnus-summary-select-article t 'force))
11205
11206 (defun gnus-summary-caesar-message (&optional arg)
11207   "Caesar rotate the current article by 13.
11208 The numerical prefix specifies how manu places to rotate each letter
11209 forward."
11210   (interactive "P")
11211   (gnus-set-global-variables)
11212   (gnus-summary-select-article)
11213   (let ((mail-header-separator ""))
11214     (gnus-eval-in-buffer-window
11215      gnus-article-buffer
11216      (save-restriction
11217        (widen)
11218        (let ((start (window-start)))
11219          (news-caesar-buffer-body arg)
11220          (set-window-start (get-buffer-window (current-buffer)) start))))))
11221
11222 (defun gnus-summary-stop-page-breaking ()
11223   "Stop page breaking in the current article."
11224   (interactive)
11225   (gnus-set-global-variables)
11226   (gnus-summary-select-article)
11227   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11228
11229 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11230   "Move the current article to a different newsgroup.
11231 If N is a positive number, move the N next articles.
11232 If N is a negative number, move the N previous articles.
11233 If N is nil and any articles have been marked with the process mark,
11234 move those articles instead.
11235 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11236 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11237 re-spool using this method.
11238
11239 For this function to work, both the current newsgroup and the
11240 newsgroup that you want to move to have to support the `request-move'
11241 and `request-accept' functions."
11242   (interactive "P")
11243   (unless action (setq action 'move))
11244   (gnus-set-global-variables)
11245   ;; Check whether the source group supports the required functions.
11246   (cond ((and (eq action 'move)
11247               (not (gnus-check-backend-function
11248                     'request-move-article gnus-newsgroup-name)))
11249          (error "The current group does not support article moving"))
11250         ((and (eq action 'crosspost)
11251               (not (gnus-check-backend-function
11252                     'request-replace-article gnus-newsgroup-name)))
11253          (error "The current group does not support article editing")))
11254   (let ((articles (gnus-summary-work-articles n))
11255         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11256         (names '((move "Move" "Moving")
11257                  (copy "Copy" "Copying")
11258                  (crosspost "Crosspost" "Crossposting")))
11259         (copy-buf (save-excursion
11260                     (nnheader-set-temp-buffer " *copy article*")))
11261         art-group to-method new-xref article to-groups)
11262     (unless (assq action names)
11263       (error "Unknown action %s" action))
11264     ;; Read the newsgroup name.
11265     (when (and (not to-newsgroup)
11266                (not select-method))
11267       (setq to-newsgroup
11268             (gnus-read-move-group-name
11269              (cadr (assq action names))
11270              (symbol-value (intern (format "gnus-current-%s-group" action)))
11271              articles prefix))
11272       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11273     (setq to-method (or select-method 
11274                         (gnus-find-method-for-group to-newsgroup)))
11275     ;; Check the method we are to move this article to...
11276     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11277         (error "%s does not support article copying" (car to-method)))
11278     (or (gnus-check-server to-method)
11279         (error "Can't open server %s" (car to-method)))
11280     (gnus-message 6 "%s to %s: %s..."
11281                   (caddr (assq action names))
11282                   (or (car select-method) to-newsgroup) articles)
11283     (while articles
11284       (setq article (pop articles))
11285       (setq
11286        art-group
11287        (cond
11288         ;; Move the article.
11289         ((eq action 'move)
11290          (gnus-request-move-article
11291           article                       ; Article to move
11292           gnus-newsgroup-name           ; From newsgrouo
11293           (nth 1 (gnus-find-method-for-group
11294                   gnus-newsgroup-name)) ; Server
11295           (list 'gnus-request-accept-article
11296                 to-newsgroup (list 'quote select-method)
11297                 (not articles))         ; Accept form
11298           (not articles)))              ; Only save nov last time
11299         ;; Copy the article.
11300         ((eq action 'copy)
11301          (save-excursion
11302            (set-buffer copy-buf)
11303            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11304            (gnus-request-accept-article
11305             to-newsgroup select-method (not articles))))
11306         ;; Crosspost the article.
11307         ((eq action 'crosspost)
11308          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11309            (setq new-xref (concat gnus-newsgroup-name ":" article))
11310            (if (and xref (not (string= xref "")))
11311                (progn
11312                  (when (string-match "^Xref: " xref)
11313                    (setq xref (substring xref (match-end 0))))
11314                  (setq new-xref (concat xref " " new-xref)))
11315              (setq new-xref (concat (system-name) " " new-xref)))
11316            (save-excursion
11317              (set-buffer copy-buf)
11318              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11319              (nnheader-replace-header "xref" new-xref)
11320              (gnus-request-accept-article
11321               to-newsgroup select-method (not articles)))))))
11322       (if (not art-group)
11323           (gnus-message 1 "Couldn't %s article %s"
11324                         (cadr (assq action names)) article)
11325         (let* ((entry
11326                 (or
11327                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11328                  (gnus-gethash
11329                   (gnus-group-prefixed-name
11330                    (car art-group)
11331                    (or select-method 
11332                        (gnus-find-method-for-group to-newsgroup)))
11333                   gnus-newsrc-hashtb)))
11334                (info (nth 2 entry))
11335                (to-group (gnus-info-group info)))
11336           ;; Update the group that has been moved to.
11337           (when (and info
11338                      (memq action '(move copy)))
11339             (unless (member to-group to-groups)
11340               (push to-group to-groups))
11341
11342             (unless (memq article gnus-newsgroup-unreads)
11343               (gnus-info-set-read
11344                info (gnus-add-to-range (gnus-info-read info)
11345                                        (list (cdr art-group)))))
11346
11347             ;; Copy any marks over to the new group.
11348             (let ((marks gnus-article-mark-lists)
11349                   (to-article (cdr art-group)))
11350
11351               ;; See whether the article is to be put in the cache.
11352               (when gnus-use-cache
11353                 (gnus-cache-possibly-enter-article
11354                  to-group to-article
11355                  (let ((header (copy-sequence
11356                                 (gnus-summary-article-header article))))
11357                    (mail-header-set-number header to-article)
11358                    header)
11359                  (memq article gnus-newsgroup-marked)
11360                  (memq article gnus-newsgroup-dormant)
11361                  (memq article gnus-newsgroup-unreads)))
11362
11363               (while marks
11364                 (when (memq article (symbol-value
11365                                      (intern (format "gnus-newsgroup-%s"
11366                                                      (caar marks)))))
11367                   ;; If the other group is the same as this group,
11368                   ;; then we have to add the mark to the list.
11369                   (when (equal to-group gnus-newsgroup-name)
11370                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11371                          (cons to-article
11372                                (symbol-value
11373                                 (intern (format "gnus-newsgroup-%s"
11374                                                 (caar marks)))))))
11375                   ;; Copy mark to other group.
11376                   (gnus-add-marked-articles
11377                    to-group (cdar marks) (list to-article) info))
11378                 (setq marks (cdr marks)))))
11379
11380           ;; Update the Xref header in this article to point to
11381           ;; the new crossposted article we have just created.
11382           (when (eq action 'crosspost)
11383             (save-excursion
11384               (set-buffer copy-buf)
11385               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11386               (nnheader-replace-header
11387                "xref" (concat new-xref " " (gnus-group-prefixed-name
11388                                             (car art-group) to-method)
11389                               ":" (cdr art-group)))
11390               (gnus-request-replace-article
11391                article gnus-newsgroup-name (current-buffer)))))
11392
11393         (gnus-summary-goto-subject article)
11394         (when (eq action 'move)
11395           (gnus-summary-mark-article article gnus-canceled-mark)))
11396       (gnus-summary-remove-process-mark article))
11397     ;; Re-activate all groups that have been moved to.
11398     (while to-groups
11399       (gnus-activate-group (pop to-groups)))
11400     
11401     (gnus-kill-buffer copy-buf)
11402     (gnus-summary-position-point)
11403     (gnus-set-mode-line 'summary)))
11404
11405 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11406   "Move the current article to a different newsgroup.
11407 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11408 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11409 re-spool using this method."
11410   (interactive "P")
11411   (gnus-summary-move-article n nil select-method 'copy))
11412
11413 (defun gnus-summary-crosspost-article (&optional n)
11414   "Crosspost the current article to some other group."
11415   (interactive "P")
11416   (gnus-summary-move-article n nil nil 'crosspost))
11417
11418 (defvar gnus-summary-respool-default-method nil
11419   "Default method for respooling an article.  
11420 If nil, use to the current newsgroup method.")
11421
11422 (defun gnus-summary-respool-article (&optional n method)
11423   "Respool the current article.
11424 The article will be squeezed through the mail spooling process again,
11425 which means that it will be put in some mail newsgroup or other
11426 depending on `nnmail-split-methods'.
11427 If N is a positive number, respool the N next articles.
11428 If N is a negative number, respool the N previous articles.
11429 If N is nil and any articles have been marked with the process mark,
11430 respool those articles instead.
11431
11432 Respooling can be done both from mail groups and \"real\" newsgroups.
11433 In the former case, the articles in question will be moved from the
11434 current group into whatever groups they are destined to.  In the
11435 latter case, they will be copied into the relevant groups."
11436   (interactive 
11437    (list current-prefix-arg
11438          (let* ((methods (gnus-methods-using 'respool))
11439                 (methname
11440                  (symbol-name (or gnus-summary-respool-default-method
11441                                   (car (gnus-find-method-for-group
11442                                         gnus-newsgroup-name)))))
11443                 (method
11444                  (gnus-completing-read 
11445                   methname "What backend do you want to use when? "
11446                   methods nil t nil 'gnus-method-history))
11447                 ms)
11448            (cond
11449             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11450              (list (intern method) ""))
11451             ((= 1 (length ms))
11452              (car ms))
11453             (t
11454              (cdr (completing-read 
11455                    "Server name: "
11456                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11457   (gnus-set-global-variables)
11458   (unless method
11459     (error "No method given for respooling"))
11460   (if (assoc (symbol-name
11461               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11462              (gnus-methods-using 'respool))
11463       (gnus-summary-move-article n nil method)
11464     (gnus-summary-copy-article n nil method)))
11465
11466 (defun gnus-summary-import-article (file)
11467   "Import a random file into a mail newsgroup."
11468   (interactive "fImport file: ")
11469   (gnus-set-global-variables)
11470   (let ((group gnus-newsgroup-name)
11471         (now (current-time))
11472         atts lines)
11473     (or (gnus-check-backend-function 'request-accept-article group)
11474         (error "%s does not support article importing" group))
11475     (or (file-readable-p file)
11476         (not (file-regular-p file))
11477         (error "Can't read %s" file))
11478     (save-excursion
11479       (set-buffer (get-buffer-create " *import file*"))
11480       (buffer-disable-undo (current-buffer))
11481       (erase-buffer)
11482       (insert-file-contents file)
11483       (goto-char (point-min))
11484       (unless (nnheader-article-p)
11485         ;; This doesn't look like an article, so we fudge some headers.
11486         (setq atts (file-attributes file)
11487               lines (count-lines (point-min) (point-max)))
11488         (insert "From: " (read-string "From: ") "\n"
11489                 "Subject: " (read-string "Subject: ") "\n"
11490                 "Date: " (timezone-make-date-arpa-standard
11491                           (current-time-string (nth 5 atts))
11492                           (current-time-zone now)
11493                           (current-time-zone now)) "\n"
11494                 "Message-ID: " (gnus-inews-message-id) "\n"
11495                 "Lines: " (int-to-string lines) "\n"
11496                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11497       (gnus-request-accept-article group nil t)
11498       (kill-buffer (current-buffer)))))
11499
11500 (defun gnus-summary-expire-articles ()
11501   "Expire all articles that are marked as expirable in the current group."
11502   (interactive)
11503   (gnus-set-global-variables)
11504   (when (gnus-check-backend-function
11505          'request-expire-articles gnus-newsgroup-name)
11506     ;; This backend supports expiry.
11507     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11508            (expirable (if total
11509                           (gnus-list-of-read-articles gnus-newsgroup-name)
11510                         (setq gnus-newsgroup-expirable
11511                               (sort gnus-newsgroup-expirable '<))))
11512            (expiry-wait (gnus-group-get-parameter
11513                          gnus-newsgroup-name 'expiry-wait))
11514            es)
11515       (when expirable
11516         ;; There are expirable articles in this group, so we run them
11517         ;; through the expiry process.
11518         (gnus-message 6 "Expiring articles...")
11519         ;; The list of articles that weren't expired is returned.
11520         (if expiry-wait
11521             (let ((nnmail-expiry-wait-function nil)
11522                   (nnmail-expiry-wait expiry-wait))
11523               (setq es (gnus-request-expire-articles
11524                         expirable gnus-newsgroup-name)))
11525           (setq es (gnus-request-expire-articles
11526                     expirable gnus-newsgroup-name)))
11527         (or total (setq gnus-newsgroup-expirable es))
11528         ;; We go through the old list of expirable, and mark all
11529         ;; really expired articles as nonexistent.
11530         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11531           (let ((gnus-use-cache nil))
11532             (while expirable
11533               (unless (memq (car expirable) es)
11534                 (when (gnus-data-find (car expirable))
11535                   (gnus-summary-mark-article
11536                    (car expirable) gnus-canceled-mark)))
11537               (setq expirable (cdr expirable)))))
11538         (gnus-message 6 "Expiring articles...done")))))
11539
11540 (defun gnus-summary-expire-articles-now ()
11541   "Expunge all expirable articles in the current group.
11542 This means that *all* articles that are marked as expirable will be
11543 deleted forever, right now."
11544   (interactive)
11545   (gnus-set-global-variables)
11546   (or gnus-expert-user
11547       (gnus-y-or-n-p
11548        "Are you really, really, really sure you want to delete all these messages? ")
11549       (error "Phew!"))
11550   (let ((nnmail-expiry-wait 'immediate)
11551         (nnmail-expiry-wait-function nil))
11552     (gnus-summary-expire-articles)))
11553
11554 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11555 (defun gnus-summary-delete-article (&optional n)
11556   "Delete the N next (mail) articles.
11557 This command actually deletes articles.  This is not a marking
11558 command.  The article will disappear forever from your life, never to
11559 return.
11560 If N is negative, delete backwards.
11561 If N is nil and articles have been marked with the process mark,
11562 delete these instead."
11563   (interactive "P")
11564   (gnus-set-global-variables)
11565   (or (gnus-check-backend-function 'request-expire-articles
11566                                    gnus-newsgroup-name)
11567       (error "The current newsgroup does not support article deletion."))
11568   ;; Compute the list of articles to delete.
11569   (let ((articles (gnus-summary-work-articles n))
11570         not-deleted)
11571     (if (and gnus-novice-user
11572              (not (gnus-y-or-n-p
11573                    (format "Do you really want to delete %s forever? "
11574                            (if (> (length articles) 1) "these articles"
11575                              "this article")))))
11576         ()
11577       ;; Delete the articles.
11578       (setq not-deleted (gnus-request-expire-articles
11579                          articles gnus-newsgroup-name 'force))
11580       (while articles
11581         (gnus-summary-remove-process-mark (car articles))
11582         ;; The backend might not have been able to delete the article
11583         ;; after all.
11584         (or (memq (car articles) not-deleted)
11585             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11586         (setq articles (cdr articles))))
11587     (gnus-summary-position-point)
11588     (gnus-set-mode-line 'summary)
11589     not-deleted))
11590
11591 (defun gnus-summary-edit-article (&optional force)
11592   "Enter into a buffer and edit the current article.
11593 This will have permanent effect only in mail groups.
11594 If FORCE is non-nil, allow editing of articles even in read-only
11595 groups."
11596   (interactive "P")
11597   (save-excursion
11598     (set-buffer gnus-summary-buffer)
11599     (gnus-set-global-variables)
11600     (when (and (not force)
11601                (gnus-group-read-only-p))
11602       (error "The current newsgroup does not support article editing."))
11603     (gnus-summary-select-article t nil t)
11604     (gnus-configure-windows 'article)
11605     (select-window (get-buffer-window gnus-article-buffer))
11606     (gnus-message 6 "C-c C-c to end edits")
11607     (setq buffer-read-only nil)
11608     (text-mode)
11609     (use-local-map (copy-keymap (current-local-map)))
11610     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11611     (buffer-enable-undo)
11612     (widen)
11613     (goto-char (point-min))
11614     (search-forward "\n\n" nil t)))
11615
11616 (defun gnus-summary-edit-article-done ()
11617   "Make edits to the current article permanent."
11618   (interactive)
11619   (if (gnus-group-read-only-p)
11620       (progn
11621         (gnus-summary-edit-article-postpone)
11622         (gnus-error
11623          1 "The current newsgroup does not support article editing."))
11624     (let ((buf (format "%s" (buffer-string))))
11625       (erase-buffer)
11626       (insert buf)
11627       (if (not (gnus-request-replace-article
11628                 (cdr gnus-article-current) (car gnus-article-current)
11629                 (current-buffer)))
11630           (error "Couldn't replace article.")
11631         (gnus-article-mode)
11632         (use-local-map gnus-article-mode-map)
11633         (setq buffer-read-only t)
11634         (buffer-disable-undo (current-buffer))
11635         (gnus-configure-windows 'summary)
11636         (gnus-summary-update-article (cdr gnus-article-current))
11637         (when gnus-use-cache
11638           (gnus-cache-update-article 
11639            (cdr gnus-article-current) (car gnus-article-current))))
11640       (save-excursion
11641         (when (get-buffer gnus-original-article-buffer)
11642           (set-buffer gnus-original-article-buffer)
11643           (setq gnus-original-article nil)))
11644       (setq gnus-article-current nil
11645             gnus-current-article nil)
11646       (run-hooks 'gnus-article-display-hook)
11647       (and (gnus-visual-p 'summary-highlight 'highlight)
11648            (run-hooks 'gnus-visual-mark-article-hook)))))
11649
11650 (defun gnus-summary-edit-article-postpone ()
11651   "Postpone changes to the current article."
11652   (interactive)
11653   (gnus-article-mode)
11654   (use-local-map gnus-article-mode-map)
11655   (setq buffer-read-only t)
11656   (buffer-disable-undo (current-buffer))
11657   (gnus-configure-windows 'summary)
11658   (and (gnus-visual-p 'summary-highlight 'highlight)
11659        (run-hooks 'gnus-visual-mark-article-hook)))
11660
11661 (defun gnus-summary-respool-query ()
11662   "Query where the respool algorithm would put this article."
11663   (interactive)
11664   (gnus-set-global-variables)
11665   (gnus-summary-select-article)
11666   (save-excursion
11667     (set-buffer gnus-article-buffer)
11668     (save-restriction
11669       (goto-char (point-min))
11670       (search-forward "\n\n")
11671       (narrow-to-region (point-min) (point))
11672       (pp-eval-expression
11673        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11674
11675 ;; Summary score commands.
11676
11677 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11678
11679 (defun gnus-summary-raise-score (n)
11680   "Raise the score of the current article by N."
11681   (interactive "p")
11682   (gnus-set-global-variables)
11683   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11684
11685 (defun gnus-summary-set-score (n)
11686   "Set the score of the current article to N."
11687   (interactive "p")
11688   (gnus-set-global-variables)
11689   (save-excursion
11690     (gnus-summary-show-thread)
11691     (let ((buffer-read-only nil))
11692       ;; Set score.
11693       (gnus-summary-update-mark
11694        (if (= n (or gnus-summary-default-score 0)) ? 
11695          (if (< n (or gnus-summary-default-score 0))
11696              gnus-score-below-mark gnus-score-over-mark)) 'score))
11697     (let* ((article (gnus-summary-article-number))
11698            (score (assq article gnus-newsgroup-scored)))
11699       (if score (setcdr score n)
11700         (setq gnus-newsgroup-scored
11701               (cons (cons article n) gnus-newsgroup-scored))))
11702     (gnus-summary-update-line)))
11703
11704 (defun gnus-summary-current-score ()
11705   "Return the score of the current article."
11706   (interactive)
11707   (gnus-set-global-variables)
11708   (gnus-message 1 "%s" (gnus-summary-article-score)))
11709
11710 ;; Summary marking commands.
11711
11712 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11713   "Mark articles which has the same subject as read, and then select the next.
11714 If UNMARK is positive, remove any kind of mark.
11715 If UNMARK is negative, tick articles."
11716   (interactive "P")
11717   (gnus-set-global-variables)
11718   (if unmark
11719       (setq unmark (prefix-numeric-value unmark)))
11720   (let ((count
11721          (gnus-summary-mark-same-subject
11722           (gnus-summary-article-subject) unmark)))
11723     ;; Select next unread article.  If auto-select-same mode, should
11724     ;; select the first unread article.
11725     (gnus-summary-next-article t (and gnus-auto-select-same
11726                                       (gnus-summary-article-subject)))
11727     (gnus-message 7 "%d article%s marked as %s"
11728                   count (if (= count 1) " is" "s are")
11729                   (if unmark "unread" "read"))))
11730
11731 (defun gnus-summary-kill-same-subject (&optional unmark)
11732   "Mark articles which has the same subject as read.
11733 If UNMARK is positive, remove any kind of mark.
11734 If UNMARK is negative, tick articles."
11735   (interactive "P")
11736   (gnus-set-global-variables)
11737   (if unmark
11738       (setq unmark (prefix-numeric-value unmark)))
11739   (let ((count
11740          (gnus-summary-mark-same-subject
11741           (gnus-summary-article-subject) unmark)))
11742     ;; If marked as read, go to next unread subject.
11743     (if (null unmark)
11744         ;; Go to next unread subject.
11745         (gnus-summary-next-subject 1 t))
11746     (gnus-message 7 "%d articles are marked as %s"
11747                   count (if unmark "unread" "read"))))
11748
11749 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11750   "Mark articles with same SUBJECT as read, and return marked number.
11751 If optional argument UNMARK is positive, remove any kinds of marks.
11752 If optional argument UNMARK is negative, mark articles as unread instead."
11753   (let ((count 1))
11754     (save-excursion
11755       (cond
11756        ((null unmark)                   ; Mark as read.
11757         (while (and
11758                 (progn
11759                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11760                   (gnus-summary-show-thread) t)
11761                 (gnus-summary-find-subject subject))
11762           (setq count (1+ count))))
11763        ((> unmark 0)                    ; Tick.
11764         (while (and
11765                 (progn
11766                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11767                   (gnus-summary-show-thread) t)
11768                 (gnus-summary-find-subject subject))
11769           (setq count (1+ count))))
11770        (t                               ; Mark as unread.
11771         (while (and
11772                 (progn
11773                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11774                   (gnus-summary-show-thread) t)
11775                 (gnus-summary-find-subject subject))
11776           (setq count (1+ count)))))
11777       (gnus-set-mode-line 'summary)
11778       ;; Return the number of marked articles.
11779       count)))
11780
11781 (defun gnus-summary-mark-as-processable (n &optional unmark)
11782   "Set the process mark on the next N articles.
11783 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11784 the process mark instead.  The difference between N and the actual
11785 number of articles marked is returned."
11786   (interactive "p")
11787   (gnus-set-global-variables)
11788   (let ((backward (< n 0))
11789         (n (abs n)))
11790     (while (and
11791             (> n 0)
11792             (if unmark
11793                 (gnus-summary-remove-process-mark
11794                  (gnus-summary-article-number))
11795               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11796             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11797       (setq n (1- n)))
11798     (if (/= 0 n) (gnus-message 7 "No more articles"))
11799     (gnus-summary-recenter)
11800     (gnus-summary-position-point)
11801     n))
11802
11803 (defun gnus-summary-unmark-as-processable (n)
11804   "Remove the process mark from the next N articles.
11805 If N is negative, mark backward instead.  The difference between N and
11806 the actual number of articles marked is returned."
11807   (interactive "p")
11808   (gnus-set-global-variables)
11809   (gnus-summary-mark-as-processable n t))
11810
11811 (defun gnus-summary-unmark-all-processable ()
11812   "Remove the process mark from all articles."
11813   (interactive)
11814   (gnus-set-global-variables)
11815   (save-excursion
11816     (while gnus-newsgroup-processable
11817       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11818   (gnus-summary-position-point))
11819
11820 (defun gnus-summary-mark-as-expirable (n)
11821   "Mark N articles forward as expirable.
11822 If N is negative, mark backward instead.  The difference between N and
11823 the actual number of articles marked is returned."
11824   (interactive "p")
11825   (gnus-set-global-variables)
11826   (gnus-summary-mark-forward n gnus-expirable-mark))
11827
11828 (defun gnus-summary-mark-article-as-replied (article)
11829   "Mark ARTICLE replied and update the summary line."
11830   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11831   (let ((buffer-read-only nil))
11832     (when (gnus-summary-goto-subject article)
11833       (gnus-summary-update-secondary-mark article))))
11834
11835 (defun gnus-summary-set-bookmark (article)
11836   "Set a bookmark in current article."
11837   (interactive (list (gnus-summary-article-number)))
11838   (gnus-set-global-variables)
11839   (if (or (not (get-buffer gnus-article-buffer))
11840           (not gnus-current-article)
11841           (not gnus-article-current)
11842           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11843       (error "No current article selected"))
11844   ;; Remove old bookmark, if one exists.
11845   (let ((old (assq article gnus-newsgroup-bookmarks)))
11846     (if old (setq gnus-newsgroup-bookmarks
11847                   (delq old gnus-newsgroup-bookmarks))))
11848   ;; Set the new bookmark, which is on the form
11849   ;; (article-number . line-number-in-body).
11850   (setq gnus-newsgroup-bookmarks
11851         (cons
11852          (cons article
11853                (save-excursion
11854                  (set-buffer gnus-article-buffer)
11855                  (count-lines
11856                   (min (point)
11857                        (save-excursion
11858                          (goto-char (point-min))
11859                          (search-forward "\n\n" nil t)
11860                          (point)))
11861                   (point))))
11862          gnus-newsgroup-bookmarks))
11863   (gnus-message 6 "A bookmark has been added to the current article."))
11864
11865 (defun gnus-summary-remove-bookmark (article)
11866   "Remove the bookmark from the current article."
11867   (interactive (list (gnus-summary-article-number)))
11868   (gnus-set-global-variables)
11869   ;; Remove old bookmark, if one exists.
11870   (let ((old (assq article gnus-newsgroup-bookmarks)))
11871     (if old
11872         (progn
11873           (setq gnus-newsgroup-bookmarks
11874                 (delq old gnus-newsgroup-bookmarks))
11875           (gnus-message 6 "Removed bookmark."))
11876       (gnus-message 6 "No bookmark in current article."))))
11877
11878 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11879 (defun gnus-summary-mark-as-dormant (n)
11880   "Mark N articles forward as dormant.
11881 If N is negative, mark backward instead.  The difference between N and
11882 the actual number of articles marked is returned."
11883   (interactive "p")
11884   (gnus-set-global-variables)
11885   (gnus-summary-mark-forward n gnus-dormant-mark))
11886
11887 (defun gnus-summary-set-process-mark (article)
11888   "Set the process mark on ARTICLE and update the summary line."
11889   (setq gnus-newsgroup-processable
11890         (cons article
11891               (delq article gnus-newsgroup-processable)))
11892   (when (gnus-summary-goto-subject article)
11893     (gnus-summary-show-thread)
11894     (gnus-summary-update-secondary-mark article)))
11895
11896 (defun gnus-summary-remove-process-mark (article)
11897   "Remove the process mark from ARTICLE and update the summary line."
11898   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11899   (when (gnus-summary-goto-subject article)
11900     (gnus-summary-show-thread)
11901     (gnus-summary-update-secondary-mark article)))
11902
11903 (defun gnus-summary-set-saved-mark (article)
11904   "Set the process mark on ARTICLE and update the summary line."
11905   (push article gnus-newsgroup-saved)
11906   (when (gnus-summary-goto-subject article)
11907     (gnus-summary-update-secondary-mark article)))
11908
11909 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11910   "Mark N articles as read forwards.
11911 If N is negative, mark backwards instead.
11912 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11913 marked as unread.
11914 The difference between N and the actual number of articles marked is
11915 returned."
11916   (interactive "p")
11917   (gnus-set-global-variables)
11918   (let ((backward (< n 0))
11919         (gnus-summary-goto-unread
11920          (and gnus-summary-goto-unread
11921               (not (eq gnus-summary-goto-unread 'never))
11922               (not (memq mark (list gnus-unread-mark
11923                                     gnus-ticked-mark gnus-dormant-mark)))))
11924         (n (abs n))
11925         (mark (or mark gnus-del-mark)))
11926     (while (and (> n 0)
11927                 (gnus-summary-mark-article nil mark no-expire)
11928                 (zerop (gnus-summary-next-subject
11929                         (if backward -1 1)
11930                         (and gnus-summary-goto-unread
11931                              (not (eq gnus-summary-goto-unread 'never)))
11932                         t)))
11933       (setq n (1- n)))
11934     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11935     (gnus-summary-recenter)
11936     (gnus-summary-position-point)
11937     (gnus-set-mode-line 'summary)
11938     n))
11939
11940 (defun gnus-summary-mark-article-as-read (mark)
11941   "Mark the current article quickly as read with MARK."
11942   (let ((article (gnus-summary-article-number)))
11943     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11944     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11945     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11946     (setq gnus-newsgroup-reads
11947           (cons (cons article mark) gnus-newsgroup-reads))
11948     ;; Possibly remove from cache, if that is used.
11949     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11950     ;; Allow the backend to change the mark.
11951     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11952     ;; Check for auto-expiry.
11953     (when (and gnus-newsgroup-auto-expire
11954                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11955                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11956                    (= mark gnus-ancient-mark)
11957                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11958       (setq mark gnus-expirable-mark)
11959       (push article gnus-newsgroup-expirable))
11960     ;; Set the mark in the buffer.
11961     (gnus-summary-update-mark mark 'unread)
11962     t))
11963
11964 (defun gnus-summary-mark-article-as-unread (mark)
11965   "Mark the current article quickly as unread with MARK."
11966   (let ((article (gnus-summary-article-number)))
11967     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11968     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11969     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11970     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11971     (cond ((= mark gnus-ticked-mark)
11972            (push article gnus-newsgroup-marked))
11973           ((= mark gnus-dormant-mark)
11974            (push article gnus-newsgroup-dormant))
11975           (t
11976            (push article gnus-newsgroup-unreads)))
11977     (setq gnus-newsgroup-reads
11978           (delq (assq article gnus-newsgroup-reads)
11979                 gnus-newsgroup-reads))
11980
11981     ;; See whether the article is to be put in the cache.
11982     (and gnus-use-cache
11983          (vectorp (gnus-summary-article-header article))
11984          (save-excursion
11985            (gnus-cache-possibly-enter-article
11986             gnus-newsgroup-name article
11987             (gnus-summary-article-header article)
11988             (= mark gnus-ticked-mark)
11989             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11990
11991     ;; Fix the mark.
11992     (gnus-summary-update-mark mark 'unread)
11993     t))
11994
11995 (defun gnus-summary-mark-article (&optional article mark no-expire)
11996   "Mark ARTICLE with MARK.  MARK can be any character.
11997 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11998 `??' (dormant) and `?E' (expirable).
11999 If MARK is nil, then the default character `?D' is used.
12000 If ARTICLE is nil, then the article on the current line will be
12001 marked."
12002   ;; The mark might be a string.
12003   (and (stringp mark)
12004        (setq mark (aref mark 0)))
12005   ;; If no mark is given, then we check auto-expiring.
12006   (and (not no-expire)
12007        gnus-newsgroup-auto-expire
12008        (or (not mark)
12009            (and (numberp mark)
12010                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12011                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12012                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12013        (setq mark gnus-expirable-mark))
12014   (let* ((mark (or mark gnus-del-mark))
12015          (article (or article (gnus-summary-article-number))))
12016     (or article (error "No article on current line"))
12017     (if (or (= mark gnus-unread-mark)
12018             (= mark gnus-ticked-mark)
12019             (= mark gnus-dormant-mark))
12020         (gnus-mark-article-as-unread article mark)
12021       (gnus-mark-article-as-read article mark))
12022
12023     ;; See whether the article is to be put in the cache.
12024     (and gnus-use-cache
12025          (not (= mark gnus-canceled-mark))
12026          (vectorp (gnus-summary-article-header article))
12027          (save-excursion
12028            (gnus-cache-possibly-enter-article
12029             gnus-newsgroup-name article
12030             (gnus-summary-article-header article)
12031             (= mark gnus-ticked-mark)
12032             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12033
12034     (if (gnus-summary-goto-subject article nil t)
12035         (let ((buffer-read-only nil))
12036           (gnus-summary-show-thread)
12037           ;; Fix the mark.
12038           (gnus-summary-update-mark mark 'unread)
12039           t))))
12040
12041 (defun gnus-summary-update-secondary-mark (article)
12042   "Update the secondary (read, process, cache) mark."
12043   (gnus-summary-update-mark
12044    (cond ((memq article gnus-newsgroup-processable)
12045           gnus-process-mark)
12046          ((memq article gnus-newsgroup-cached)
12047           gnus-cached-mark)
12048          ((memq article gnus-newsgroup-replied)
12049           gnus-replied-mark)
12050          ((memq article gnus-newsgroup-saved)
12051           gnus-saved-mark)
12052          (t gnus-unread-mark))
12053    'replied)
12054   (when (gnus-visual-p 'summary-highlight 'highlight)
12055     (run-hooks 'gnus-summary-update-hook))
12056   t)
12057
12058 (defun gnus-summary-update-mark (mark type)
12059   (beginning-of-line)
12060   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12061         (buffer-read-only nil))
12062     (when (and forward
12063                (<= (+ forward (point)) (point-max)))
12064       ;; Go to the right position on the line.
12065       (goto-char (+ forward (point)))
12066       ;; Replace the old mark with the new mark.
12067       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12068       ;; Optionally update the marks by some user rule.
12069       (when (eq type 'unread)
12070         (gnus-data-set-mark
12071          (gnus-data-find (gnus-summary-article-number)) mark)
12072         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12073
12074 (defun gnus-mark-article-as-read (article &optional mark)
12075   "Enter ARTICLE in the pertinent lists and remove it from others."
12076   ;; Make the article expirable.
12077   (let ((mark (or mark gnus-del-mark)))
12078     (if (= mark gnus-expirable-mark)
12079         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12080       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12081     ;; Remove from unread and marked lists.
12082     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12083     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12084     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12085     (push (cons article mark) gnus-newsgroup-reads)
12086     ;; Possibly remove from cache, if that is used.
12087     (when gnus-use-cache
12088       (gnus-cache-enter-remove-article article))))
12089
12090 (defun gnus-mark-article-as-unread (article &optional mark)
12091   "Enter ARTICLE in the pertinent lists and remove it from others."
12092   (let ((mark (or mark gnus-ticked-mark)))
12093     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12094     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12095     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12096     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12097     (cond ((= mark gnus-ticked-mark)
12098            (push article gnus-newsgroup-marked))
12099           ((= mark gnus-dormant-mark)
12100            (push article gnus-newsgroup-dormant))
12101           (t
12102            (push article gnus-newsgroup-unreads)))
12103     (setq gnus-newsgroup-reads
12104           (delq (assq article gnus-newsgroup-reads)
12105                 gnus-newsgroup-reads))))
12106
12107 (defalias 'gnus-summary-mark-as-unread-forward
12108   'gnus-summary-tick-article-forward)
12109 (make-obsolete 'gnus-summary-mark-as-unread-forward
12110                'gnus-summary-tick-article-forward)
12111 (defun gnus-summary-tick-article-forward (n)
12112   "Tick N articles forwards.
12113 If N is negative, tick backwards instead.
12114 The difference between N and the number of articles ticked is returned."
12115   (interactive "p")
12116   (gnus-summary-mark-forward n gnus-ticked-mark))
12117
12118 (defalias 'gnus-summary-mark-as-unread-backward
12119   'gnus-summary-tick-article-backward)
12120 (make-obsolete 'gnus-summary-mark-as-unread-backward
12121                'gnus-summary-tick-article-backward)
12122 (defun gnus-summary-tick-article-backward (n)
12123   "Tick N articles backwards.
12124 The difference between N and the number of articles ticked is returned."
12125   (interactive "p")
12126   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12127
12128 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12129 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12130 (defun gnus-summary-tick-article (&optional article clear-mark)
12131   "Mark current article as unread.
12132 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12133 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12134   (interactive)
12135   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12136                                        gnus-ticked-mark)))
12137
12138 (defun gnus-summary-mark-as-read-forward (n)
12139   "Mark N articles as read forwards.
12140 If N is negative, mark backwards instead.
12141 The difference between N and the actual number of articles marked is
12142 returned."
12143   (interactive "p")
12144   (gnus-summary-mark-forward n gnus-del-mark t))
12145
12146 (defun gnus-summary-mark-as-read-backward (n)
12147   "Mark the N articles as read backwards.
12148 The difference between N and the actual number of articles marked is
12149 returned."
12150   (interactive "p")
12151   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12152
12153 (defun gnus-summary-mark-as-read (&optional article mark)
12154   "Mark current article as read.
12155 ARTICLE specifies the article to be marked as read.
12156 MARK specifies a string to be inserted at the beginning of the line."
12157   (gnus-summary-mark-article article mark))
12158
12159 (defun gnus-summary-clear-mark-forward (n)
12160   "Clear marks from N articles forward.
12161 If N is negative, clear backward instead.
12162 The difference between N and the number of marks cleared is returned."
12163   (interactive "p")
12164   (gnus-summary-mark-forward n gnus-unread-mark))
12165
12166 (defun gnus-summary-clear-mark-backward (n)
12167   "Clear marks from N articles backward.
12168 The difference between N and the number of marks cleared is returned."
12169   (interactive "p")
12170   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12171
12172 (defun gnus-summary-mark-unread-as-read ()
12173   "Intended to be used by `gnus-summary-mark-article-hook'."
12174   (when (memq gnus-current-article gnus-newsgroup-unreads)
12175     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12176
12177 (defun gnus-summary-mark-read-and-unread-as-read ()
12178   "Intended to be used by `gnus-summary-mark-article-hook'."
12179   (let ((mark (gnus-summary-article-mark)))
12180     (when (or (gnus-unread-mark-p mark)
12181               (gnus-read-mark-p mark))
12182       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12183
12184 (defun gnus-summary-mark-region-as-read (point mark all)
12185   "Mark all unread articles between point and mark as read.
12186 If given a prefix, mark all articles between point and mark as read,
12187 even ticked and dormant ones."
12188   (interactive "r\nP")
12189   (save-excursion
12190     (let (article)
12191       (goto-char point)
12192       (beginning-of-line)
12193       (while (and
12194               (< (point) mark)
12195               (progn
12196                 (when (or all
12197                           (memq (setq article (gnus-summary-article-number))
12198                                 gnus-newsgroup-unreads))
12199                   (gnus-summary-mark-article article gnus-del-mark))
12200                 t)
12201               (gnus-summary-find-next))))))
12202
12203 (defun gnus-summary-mark-below (score mark)
12204   "Mark articles with score less than SCORE with MARK."
12205   (interactive "P\ncMark: ")
12206   (gnus-set-global-variables)
12207   (setq score (if score
12208                   (prefix-numeric-value score)
12209                 (or gnus-summary-default-score 0)))
12210   (save-excursion
12211     (set-buffer gnus-summary-buffer)
12212     (goto-char (point-min))
12213     (while 
12214         (progn
12215           (and (< (gnus-summary-article-score) score)
12216                (gnus-summary-mark-article nil mark))
12217           (gnus-summary-find-next)))))
12218
12219 (defun gnus-summary-kill-below (&optional score)
12220   "Mark articles with score below SCORE as read."
12221   (interactive "P")
12222   (gnus-set-global-variables)
12223   (gnus-summary-mark-below score gnus-killed-mark))
12224
12225 (defun gnus-summary-clear-above (&optional score)
12226   "Clear all marks from articles with score above SCORE."
12227   (interactive "P")
12228   (gnus-set-global-variables)
12229   (gnus-summary-mark-above score gnus-unread-mark))
12230
12231 (defun gnus-summary-tick-above (&optional score)
12232   "Tick all articles with score above SCORE."
12233   (interactive "P")
12234   (gnus-set-global-variables)
12235   (gnus-summary-mark-above score gnus-ticked-mark))
12236
12237 (defun gnus-summary-mark-above (score mark)
12238   "Mark articles with score over SCORE with MARK."
12239   (interactive "P\ncMark: ")
12240   (gnus-set-global-variables)
12241   (setq score (if score
12242                   (prefix-numeric-value score)
12243                 (or gnus-summary-default-score 0)))
12244   (save-excursion
12245     (set-buffer gnus-summary-buffer)
12246     (goto-char (point-min))
12247     (while (and (progn
12248                   (if (> (gnus-summary-article-score) score)
12249                       (gnus-summary-mark-article nil mark))
12250                   t)
12251                 (gnus-summary-find-next)))))
12252
12253 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12254 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12255 (defun gnus-summary-limit-include-expunged ()
12256   "Display all the hidden articles that were expunged for low scores."
12257   (interactive)
12258   (gnus-set-global-variables)
12259   (let ((buffer-read-only nil))
12260     (let ((scored gnus-newsgroup-scored)
12261           headers h)
12262       (while scored
12263         (or (gnus-summary-goto-subject (caar scored))
12264             (and (setq h (gnus-summary-article-header (caar scored)))
12265                  (< (cdar scored) gnus-summary-expunge-below)
12266                  (setq headers (cons h headers))))
12267         (setq scored (cdr scored)))
12268       (or headers (error "No expunged articles hidden."))
12269       (goto-char (point-min))
12270       (gnus-summary-prepare-unthreaded (nreverse headers)))
12271     (goto-char (point-min))
12272     (gnus-summary-position-point)))
12273
12274 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12275   "Mark all articles not marked as unread in this newsgroup as read.
12276 If prefix argument ALL is non-nil, all articles are marked as read.
12277 If QUIETLY is non-nil, no questions will be asked.
12278 If TO-HERE is non-nil, it should be a point in the buffer.  All
12279 articles before this point will be marked as read.
12280 The number of articles marked as read is returned."
12281   (interactive "P")
12282   (gnus-set-global-variables)
12283   (prog1
12284       (if (or quietly
12285               (not gnus-interactive-catchup) ;Without confirmation?
12286               gnus-expert-user
12287               (gnus-y-or-n-p
12288                (if all
12289                    "Mark absolutely all articles as read? "
12290                  "Mark all unread articles as read? ")))
12291           (if (and not-mark
12292                    (not gnus-newsgroup-adaptive)
12293                    (not gnus-newsgroup-auto-expire))
12294               (progn
12295                 (when all
12296                   (setq gnus-newsgroup-marked nil
12297                         gnus-newsgroup-dormant nil))
12298                 (setq gnus-newsgroup-unreads nil))
12299             ;; We actually mark all articles as canceled, which we
12300             ;; have to do when using auto-expiry or adaptive scoring.
12301             (gnus-summary-show-all-threads)
12302             (if (gnus-summary-first-subject (not all))
12303                 (while (and
12304                         (if to-here (< (point) to-here) t)
12305                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12306                         (gnus-summary-find-next (not all)))))
12307             (unless to-here
12308               (setq gnus-newsgroup-unreads nil))
12309             (gnus-set-mode-line 'summary)))
12310     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12311       (if (and (not to-here) (eq 'nnvirtual (car method)))
12312           (nnvirtual-catchup-group
12313            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12314     (gnus-summary-position-point)))
12315
12316 (defun gnus-summary-catchup-to-here (&optional all)
12317   "Mark all unticked articles before the current one as read.
12318 If ALL is non-nil, also mark ticked and dormant articles as read."
12319   (interactive "P")
12320   (gnus-set-global-variables)
12321   (save-excursion
12322     (let ((beg (point)))
12323       ;; We check that there are unread articles.
12324       (when (or all (gnus-summary-find-prev))
12325         (gnus-summary-catchup all t beg))))
12326   (gnus-summary-position-point))
12327
12328 (defun gnus-summary-catchup-all (&optional quietly)
12329   "Mark all articles in this newsgroup as read."
12330   (interactive "P")
12331   (gnus-set-global-variables)
12332   (gnus-summary-catchup t quietly))
12333
12334 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12335   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12336 If prefix argument ALL is non-nil, all articles are marked as read."
12337   (interactive "P")
12338   (gnus-set-global-variables)
12339   (gnus-summary-catchup all quietly nil 'fast)
12340   ;; Select next newsgroup or exit.
12341   (if (eq gnus-auto-select-next 'quietly)
12342       (gnus-summary-next-group nil)
12343     (gnus-summary-exit)))
12344
12345 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12346   "Mark all articles in this newsgroup as read, and then exit."
12347   (interactive "P")
12348   (gnus-set-global-variables)
12349   (gnus-summary-catchup-and-exit t quietly))
12350
12351 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12352 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12353   "Mark all articles in this group as read and select the next group.
12354 If given a prefix, mark all articles, unread as well as ticked, as
12355 read."
12356   (interactive "P")
12357   (gnus-set-global-variables)
12358   (save-excursion
12359     (gnus-summary-catchup all))
12360   (gnus-summary-next-article t nil nil t))
12361
12362 ;; Thread-based commands.
12363
12364 (defun gnus-summary-articles-in-thread (&optional article)
12365   "Return a list of all articles in the current thread.
12366 If ARTICLE is non-nil, return all articles in the thread that starts
12367 with that article."
12368   (let* ((article (or article (gnus-summary-article-number)))
12369          (data (gnus-data-find-list article))
12370          (top-level (gnus-data-level (car data)))
12371          (top-subject
12372           (cond ((null gnus-thread-operation-ignore-subject)
12373                  (gnus-simplify-subject-re
12374                   (mail-header-subject (gnus-data-header (car data)))))
12375                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12376                  (gnus-simplify-subject-fuzzy
12377                   (mail-header-subject (gnus-data-header (car data)))))
12378                 (t nil)))
12379          (end-point (save-excursion
12380                       (if (gnus-summary-go-to-next-thread) 
12381                           (point) (point-max))))
12382          articles)
12383     (while (and data
12384                 (< (gnus-data-pos (car data)) end-point))
12385       (when (or (not top-subject)
12386                 (string= top-subject
12387                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12388                              (gnus-simplify-subject-fuzzy
12389                               (mail-header-subject
12390                                (gnus-data-header (car data))))
12391                            (gnus-simplify-subject-re
12392                             (mail-header-subject
12393                              (gnus-data-header (car data)))))))
12394         (push (gnus-data-number (car data)) articles))
12395       (unless (and (setq data (cdr data))
12396                    (> (gnus-data-level (car data)) top-level))
12397         (setq data nil)))
12398     ;; Return the list of articles.
12399     (nreverse articles)))
12400
12401 (defun gnus-summary-rethread-current ()
12402   "Rethread the thread the current article is part of."
12403   (interactive)
12404   (gnus-set-global-variables)
12405   (let* ((gnus-show-threads t)
12406          (article (gnus-summary-article-number))
12407          (id (mail-header-id (gnus-summary-article-header)))
12408          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12409     (unless id
12410       (error "No article on the current line"))
12411     (gnus-rebuild-thread id)
12412     (gnus-summary-goto-subject article)))
12413
12414 (defun gnus-summary-reparent-thread ()
12415   "Make current article child of the marked (or previous) article.
12416
12417 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12418 is non-nil or the Subject: of both articles are the same."
12419   (interactive)
12420   (or (not (gnus-group-read-only-p))
12421       (error "The current newsgroup does not support article editing."))
12422   (or (<= (length gnus-newsgroup-processable) 1)
12423       (error "No more than one article may be marked."))
12424   (save-window-excursion
12425     (let ((gnus-article-buffer " *reparent*")
12426           (current-article (gnus-summary-article-number))
12427           ; first grab the marked article, otherwise one line up.
12428           (parent-article (if (not (null gnus-newsgroup-processable))
12429                               (car gnus-newsgroup-processable)
12430                             (save-excursion
12431                               (if (eq (forward-line -1) 0)
12432                                   (gnus-summary-article-number)
12433                                 (error "Beginning of summary buffer."))))))
12434       (or (not (eq current-article parent-article))
12435           (error "An article may not be self-referential."))
12436       (let ((message-id (mail-header-id 
12437                          (gnus-summary-article-header parent-article))))
12438         (or (and message-id (not (equal message-id "")))
12439             (error "No message-id in desired parent."))
12440         (gnus-summary-select-article t t nil current-article)
12441         (set-buffer gnus-article-buffer)
12442         (setq buffer-read-only nil)
12443         (let ((buf (format "%s" (buffer-string))))
12444           (erase-buffer)
12445           (insert buf))
12446         (goto-char (point-min))
12447         (if (search-forward-regexp "^References: " nil t)
12448             (insert message-id " " )
12449           (insert "References: " message-id "\n"))
12450         (or (gnus-request-replace-article current-article
12451                                           (car gnus-article-current)
12452                                           gnus-article-buffer)
12453             (error "Couldn't replace article."))
12454         (set-buffer gnus-summary-buffer)
12455         (gnus-summary-unmark-all-processable)
12456         (gnus-summary-rethread-current)
12457         (gnus-message 3 "Article %d is now the child of article %d."
12458                       current-article parent-article)))))
12459
12460 (defun gnus-summary-toggle-threads (&optional arg)
12461   "Toggle showing conversation threads.
12462 If ARG is positive number, turn showing conversation threads on."
12463   (interactive "P")
12464   (gnus-set-global-variables)
12465   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12466     (setq gnus-show-threads
12467           (if (null arg) (not gnus-show-threads)
12468             (> (prefix-numeric-value arg) 0)))
12469     (gnus-summary-prepare)
12470     (gnus-summary-goto-subject current)
12471     (gnus-summary-position-point)))
12472
12473 (defun gnus-summary-show-all-threads ()
12474   "Show all threads."
12475   (interactive)
12476   (gnus-set-global-variables)
12477   (save-excursion
12478     (let ((buffer-read-only nil))
12479       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12480   (gnus-summary-position-point))
12481
12482 (defun gnus-summary-show-thread ()
12483   "Show thread subtrees.
12484 Returns nil if no thread was there to be shown."
12485   (interactive)
12486   (gnus-set-global-variables)
12487   (let ((buffer-read-only nil)
12488         (orig (point))
12489         ;; first goto end then to beg, to have point at beg after let
12490         (end (progn (end-of-line) (point)))
12491         (beg (progn (beginning-of-line) (point))))
12492     (prog1
12493         ;; Any hidden lines here?
12494         (search-forward "\r" end t)
12495       (subst-char-in-region beg end ?\^M ?\n t)
12496       (goto-char orig)
12497       (gnus-summary-position-point))))
12498
12499 (defun gnus-summary-hide-all-threads ()
12500   "Hide all thread subtrees."
12501   (interactive)
12502   (gnus-set-global-variables)
12503   (save-excursion
12504     (goto-char (point-min))
12505     (gnus-summary-hide-thread)
12506     (while (zerop (gnus-summary-next-thread 1 t))
12507       (gnus-summary-hide-thread)))
12508   (gnus-summary-position-point))
12509
12510 (defun gnus-summary-hide-thread ()
12511   "Hide thread subtrees.
12512 Returns nil if no threads were there to be hidden."
12513   (interactive)
12514   (gnus-set-global-variables)
12515   (let ((buffer-read-only nil)
12516         (start (point))
12517         (article (gnus-summary-article-number)))
12518     (goto-char start)
12519     ;; Go forward until either the buffer ends or the subthread
12520     ;; ends.
12521     (when (and (not (eobp))
12522                (or (zerop (gnus-summary-next-thread 1 t))
12523                    (goto-char (point-max))))
12524       (prog1
12525           (if (and (> (point) start)
12526                    (search-backward "\n" start t))
12527               (progn
12528                 (subst-char-in-region start (point) ?\n ?\^M)
12529                 (gnus-summary-goto-subject article))
12530             (goto-char start)
12531             nil)
12532         ;;(gnus-summary-position-point)
12533         ))))
12534
12535 (defun gnus-summary-go-to-next-thread (&optional previous)
12536   "Go to the same level (or less) next thread.
12537 If PREVIOUS is non-nil, go to previous thread instead.
12538 Return the article number moved to, or nil if moving was impossible."
12539   (let ((level (gnus-summary-thread-level))
12540         (way (if previous -1 1))
12541         (beg (point)))
12542     (forward-line way)
12543     (while (and (not (eobp))
12544                 (< level (gnus-summary-thread-level)))
12545       (forward-line way))
12546     (if (eobp)
12547         (progn
12548           (goto-char beg)
12549           nil)
12550       (setq beg (point))
12551       (prog1
12552           (gnus-summary-article-number)
12553         (goto-char beg)))))
12554
12555 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12556   "Go to the same level (or less) next thread.
12557 If PREVIOUS is non-nil, go to previous thread instead.
12558 Return the article number moved to, or nil if moving was impossible."
12559   (if (and (eq gnus-summary-make-false-root 'dummy)
12560            (gnus-summary-article-intangible-p))
12561       (let ((beg (point)))
12562         (while (and (zerop (forward-line 1))
12563                     (not (gnus-summary-article-intangible-p))
12564                     (not (zerop (save-excursion 
12565                                   (gnus-summary-thread-level))))))
12566         (if (eobp)
12567             (progn
12568               (goto-char beg)
12569               nil)
12570           (point)))
12571     (let* ((level (gnus-summary-thread-level))
12572            (article (gnus-summary-article-number))
12573            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12574            oart)
12575       (while data
12576         (if (<= (gnus-data-level (car data)) level)
12577             (setq oart (gnus-data-number (car data))
12578                   data nil)
12579           (setq data (cdr data))))
12580       (and oart
12581            (gnus-summary-goto-subject oart)))))
12582
12583 (defun gnus-summary-next-thread (n &optional silent)
12584   "Go to the same level next N'th thread.
12585 If N is negative, search backward instead.
12586 Returns the difference between N and the number of skips actually
12587 done.
12588
12589 If SILENT, don't output messages."
12590   (interactive "p")
12591   (gnus-set-global-variables)
12592   (let ((backward (< n 0))
12593         (n (abs n))
12594         old dum int)
12595     (while (and (> n 0)
12596                 (gnus-summary-go-to-next-thread backward))
12597       (decf n))
12598     (unless silent 
12599       (gnus-summary-position-point))
12600     (when (and (not silent) (/= 0 n))
12601       (gnus-message 7 "No more threads"))
12602     n))
12603
12604 (defun gnus-summary-prev-thread (n)
12605   "Go to the same level previous N'th thread.
12606 Returns the difference between N and the number of skips actually
12607 done."
12608   (interactive "p")
12609   (gnus-set-global-variables)
12610   (gnus-summary-next-thread (- n)))
12611
12612 (defun gnus-summary-go-down-thread ()
12613   "Go down one level in the current thread."
12614   (let ((children (gnus-summary-article-children)))
12615     (and children
12616          (gnus-summary-goto-subject (car children)))))
12617
12618 (defun gnus-summary-go-up-thread ()
12619   "Go up one level in the current thread."
12620   (let ((parent (gnus-summary-article-parent)))
12621     (and parent
12622          (gnus-summary-goto-subject parent))))
12623
12624 (defun gnus-summary-down-thread (n)
12625   "Go down thread N steps.
12626 If N is negative, go up instead.
12627 Returns the difference between N and how many steps down that were
12628 taken."
12629   (interactive "p")
12630   (gnus-set-global-variables)
12631   (let ((up (< n 0))
12632         (n (abs n)))
12633     (while (and (> n 0)
12634                 (if up (gnus-summary-go-up-thread)
12635                   (gnus-summary-go-down-thread)))
12636       (setq n (1- n)))
12637     (gnus-summary-position-point)
12638     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12639     n))
12640
12641 (defun gnus-summary-up-thread (n)
12642   "Go up thread N steps.
12643 If N is negative, go up instead.
12644 Returns the difference between N and how many steps down that were
12645 taken."
12646   (interactive "p")
12647   (gnus-set-global-variables)
12648   (gnus-summary-down-thread (- n)))
12649
12650 (defun gnus-summary-top-thread ()
12651   "Go to the top of the thread."
12652   (interactive)
12653   (gnus-set-global-variables)
12654   (while (gnus-summary-go-up-thread))
12655   (gnus-summary-article-number))
12656
12657 (defun gnus-summary-kill-thread (&optional unmark)
12658   "Mark articles under current thread as read.
12659 If the prefix argument is positive, remove any kinds of marks.
12660 If the prefix argument is negative, tick articles instead."
12661   (interactive "P")
12662   (gnus-set-global-variables)
12663   (if unmark
12664       (setq unmark (prefix-numeric-value unmark)))
12665   (let ((articles (gnus-summary-articles-in-thread)))
12666     (save-excursion
12667       ;; Expand the thread.
12668       (gnus-summary-show-thread)
12669       ;; Mark all the articles.
12670       (while articles
12671         (gnus-summary-goto-subject (car articles))
12672         (cond ((null unmark)
12673                (gnus-summary-mark-article-as-read gnus-killed-mark))
12674               ((> unmark 0)
12675                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12676               (t
12677                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12678         (setq articles (cdr articles))))
12679     ;; Hide killed subtrees.
12680     (and (null unmark)
12681          gnus-thread-hide-killed
12682          (gnus-summary-hide-thread))
12683     ;; If marked as read, go to next unread subject.
12684     (if (null unmark)
12685         ;; Go to next unread subject.
12686         (gnus-summary-next-subject 1 t)))
12687   (gnus-set-mode-line 'summary))
12688
12689 ;; Summary sorting commands
12690
12691 (defun gnus-summary-sort-by-number (&optional reverse)
12692   "Sort summary buffer by article number.
12693 Argument REVERSE means reverse order."
12694   (interactive "P")
12695   (gnus-summary-sort 'number reverse))
12696
12697 (defun gnus-summary-sort-by-author (&optional reverse)
12698   "Sort summary buffer by author name alphabetically.
12699 If case-fold-search is non-nil, case of letters is ignored.
12700 Argument REVERSE means reverse order."
12701   (interactive "P")
12702   (gnus-summary-sort 'author reverse))
12703
12704 (defun gnus-summary-sort-by-subject (&optional reverse)
12705   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12706 If case-fold-search is non-nil, case of letters is ignored.
12707 Argument REVERSE means reverse order."
12708   (interactive "P")
12709   (gnus-summary-sort 'subject reverse))
12710
12711 (defun gnus-summary-sort-by-date (&optional reverse)
12712   "Sort summary buffer by date.
12713 Argument REVERSE means reverse order."
12714   (interactive "P")
12715   (gnus-summary-sort 'date reverse))
12716
12717 (defun gnus-summary-sort-by-score (&optional reverse)
12718   "Sort summary buffer by score.
12719 Argument REVERSE means reverse order."
12720   (interactive "P")
12721   (gnus-summary-sort 'score reverse))
12722
12723 (defun gnus-summary-sort (predicate reverse)
12724   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12725   (gnus-set-global-variables)
12726   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12727          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12728          (gnus-thread-sort-functions
12729           (list
12730            (if (not reverse)
12731                thread
12732              `(lambda (t1 t2)
12733                 (,thread t2 t1)))))
12734          (gnus-article-sort-functions
12735           (list
12736            (if (not reverse)
12737                article
12738              `(lambda (t1 t2)
12739                 (,article t2 t1)))))
12740          (buffer-read-only)
12741          (gnus-summary-prepare-hook nil))
12742     ;; We do the sorting by regenerating the threads.
12743     (gnus-summary-prepare)
12744     ;; Hide subthreads if needed.
12745     (when (and gnus-show-threads gnus-thread-hide-subtree)
12746       (gnus-summary-hide-all-threads)))
12747   ;; If in async mode, we send some info to the backend.
12748   (when gnus-newsgroup-async
12749     (gnus-request-asynchronous
12750      gnus-newsgroup-name gnus-newsgroup-data)))
12751
12752 (defun gnus-sortable-date (date)
12753   "Make sortable string by string-lessp from DATE.
12754 Timezone package is used."
12755   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12756          (year (aref date 0))
12757          (month (aref date 1))
12758          (day (aref date 2)))
12759     (timezone-make-sortable-date
12760      year month day
12761      (timezone-make-time-string
12762       (aref date 3) (aref date 4) (aref date 5)))))
12763
12764 ;; Summary saving commands.
12765
12766 (defun gnus-summary-save-article (&optional n not-saved)
12767   "Save the current article using the default saver function.
12768 If N is a positive number, save the N next articles.
12769 If N is a negative number, save the N previous articles.
12770 If N is nil and any articles have been marked with the process mark,
12771 save those articles instead.
12772 The variable `gnus-default-article-saver' specifies the saver function."
12773   (interactive "P")
12774   (gnus-set-global-variables)
12775   (let ((articles (gnus-summary-work-articles n))
12776         file header article)
12777     (while articles
12778       (setq header (gnus-summary-article-header
12779                     (setq article (pop articles))))
12780       (if (not (vectorp header))
12781           ;; This is a pseudo-article.
12782           (if (assq 'name header)
12783               (gnus-copy-file (cdr (assq 'name header)))
12784             (gnus-message 1 "Article %d is unsaveable" article))
12785         ;; This is a real article.
12786         (save-window-excursion
12787           (gnus-summary-select-article t nil nil article))
12788         (unless gnus-save-all-headers
12789           ;; Remove headers accoring to `gnus-saved-headers'.
12790           (let ((gnus-visible-headers
12791                  (or gnus-saved-headers gnus-visible-headers)))
12792             (gnus-article-hide-headers nil t)))
12793         ;; Remove any X-Gnus lines.
12794         (save-excursion
12795           (set-buffer gnus-article-buffer)
12796           (save-restriction
12797             (let ((buffer-read-only nil))
12798               (nnheader-narrow-to-headers)
12799               (while (re-search-forward "^X-Gnus" nil t)
12800                 (gnus-delete-line)))))
12801         (save-window-excursion
12802           (if (not gnus-default-article-saver)
12803               (error "No default saver is defined.")
12804             (setq file (funcall
12805                         gnus-default-article-saver
12806                         (cond
12807                          ((not gnus-prompt-before-saving)
12808                           'default)
12809                          ((eq gnus-prompt-before-saving 'always)
12810                           nil)
12811                          (t file))))))
12812         (gnus-summary-remove-process-mark article)
12813         (unless not-saved
12814           (gnus-summary-set-saved-mark article))))
12815     (gnus-summary-position-point)
12816     n))
12817
12818 (defun gnus-summary-pipe-output (&optional arg)
12819   "Pipe the current article to a subprocess.
12820 If N is a positive number, pipe the N next articles.
12821 If N is a negative number, pipe the N previous articles.
12822 If N is nil and any articles have been marked with the process mark,
12823 pipe those articles instead."
12824   (interactive "P")
12825   (gnus-set-global-variables)
12826   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12827     (gnus-summary-save-article arg t))
12828   (gnus-configure-windows 'pipe))
12829
12830 (defun gnus-summary-save-article-mail (&optional arg)
12831   "Append the current article to an mail file.
12832 If N is a positive number, save the N next articles.
12833 If N is a negative number, save the N previous articles.
12834 If N is nil and any articles have been marked with the process mark,
12835 save those articles instead."
12836   (interactive "P")
12837   (gnus-set-global-variables)
12838   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12839     (gnus-summary-save-article arg)))
12840
12841 (defun gnus-summary-save-article-rmail (&optional arg)
12842   "Append the current article to an rmail file.
12843 If N is a positive number, save the N next articles.
12844 If N is a negative number, save the N previous articles.
12845 If N is nil and any articles have been marked with the process mark,
12846 save those articles instead."
12847   (interactive "P")
12848   (gnus-set-global-variables)
12849   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12850     (gnus-summary-save-article arg)))
12851
12852 (defun gnus-summary-save-article-file (&optional arg)
12853   "Append the current article to a file.
12854 If N is a positive number, save the N next articles.
12855 If N is a negative number, save the N previous articles.
12856 If N is nil and any articles have been marked with the process mark,
12857 save those articles instead."
12858   (interactive "P")
12859   (gnus-set-global-variables)
12860   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12861     (gnus-summary-save-article arg)))
12862
12863 (defun gnus-summary-save-article-body-file (&optional arg)
12864   "Append the current article body to a file.
12865 If N is a positive number, save the N next articles.
12866 If N is a negative number, save the N previous articles.
12867 If N is nil and any articles have been marked with the process mark,
12868 save those articles instead."
12869   (interactive "P")
12870   (gnus-set-global-variables)
12871   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12872     (gnus-summary-save-article arg)))
12873
12874 (defun gnus-get-split-value (methods)
12875   "Return a value based on the split METHODS."
12876   (let (split-name method result match)
12877     (when methods
12878       (save-excursion
12879         (set-buffer gnus-original-article-buffer)
12880         (save-restriction
12881           (nnheader-narrow-to-headers)
12882           (while methods
12883             (goto-char (point-min))
12884             (setq method (pop methods))
12885             (setq match (car method))
12886             (when (cond
12887                    ((stringp match)
12888                     ;; Regular expression.
12889                     (condition-case ()
12890                         (re-search-forward match nil t)
12891                       (error nil)))
12892                    ((gnus-functionp match)
12893                     ;; Function.
12894                     (save-restriction
12895                       (widen)
12896                       (setq result (funcall match gnus-newsgroup-name))))
12897                    ((consp match)
12898                     ;; Form.
12899                     (save-restriction
12900                       (widen)
12901                       (setq result (eval match)))))
12902               (setq split-name (append (cdr method) split-name))
12903               (cond ((stringp result)
12904                      (push result split-name))
12905                     ((consp result)
12906                      (setq split-name (append result split-name)))))))))
12907     split-name))
12908
12909 (defun gnus-read-move-group-name (prompt default articles prefix)
12910   "Read a group name."
12911   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12912          group-map
12913          (dum (mapatoms
12914                (lambda (g) 
12915                  (and (boundp g)
12916                       (symbol-name g)
12917                       (memq 'respool
12918                             (assoc (symbol-name
12919                                     (car (gnus-find-method-for-group
12920                                           (symbol-name g))))
12921                                    gnus-valid-select-methods))
12922                       (push (list (symbol-name g)) group-map)))
12923                gnus-active-hashtb))
12924          (prom
12925           (format "%s %s to:"
12926                   prompt
12927                   (if (> (length articles) 1)
12928                       (format "these %d articles" (length articles))
12929                     "this article")))
12930          (to-newsgroup
12931           (cond
12932            ((null split-name)
12933             (gnus-completing-read default prom
12934                                   group-map nil nil prefix
12935                                   'gnus-group-history))
12936            ((= 1 (length split-name))
12937             (gnus-completing-read (car split-name) prom group-map
12938                                   nil nil nil
12939                                   'gnus-group-history))
12940            (t
12941             (gnus-completing-read nil prom 
12942                                   (mapcar (lambda (el) (list el))
12943                                           (nreverse split-name))
12944                                   nil nil nil
12945                                   'gnus-group-history)))))
12946     (when to-newsgroup
12947       (if (or (string= to-newsgroup "")
12948               (string= to-newsgroup prefix))
12949           (setq to-newsgroup (or default "")))
12950       (or (gnus-active to-newsgroup)
12951           (gnus-activate-group to-newsgroup)
12952           (error "No such group: %s" to-newsgroup)))
12953     to-newsgroup))
12954
12955 (defun gnus-read-save-file-name (prompt default-name)
12956   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12957          (file
12958           ;; Let the split methods have their say.
12959           (cond
12960            ;; No split name was found.
12961            ((null split-name)
12962             (read-file-name
12963              (concat prompt " (default "
12964                      (file-name-nondirectory default-name) ") ")
12965              (file-name-directory default-name)
12966              default-name))
12967            ;; A single split name was found
12968            ((= 1 (length split-name))
12969             (read-file-name
12970              (concat prompt " (default " (car split-name) ") ")
12971              gnus-article-save-directory
12972              (concat gnus-article-save-directory (car split-name))))
12973            ;; A list of splits was found.
12974            (t
12975             (setq split-name (nreverse split-name))
12976             (let (result)
12977               (let ((file-name-history (nconc split-name file-name-history)))
12978                 (setq result
12979                       (read-file-name
12980                        (concat prompt " (`M-p' for defaults) ")
12981                        gnus-article-save-directory
12982                        (car split-name))))
12983               (car (push result file-name-history)))))))
12984     ;; If we have read a directory, we append the default file name.
12985     (when (file-directory-p file)
12986       (setq file (concat (file-name-as-directory file)
12987                          (file-name-nondirectory default-name))))
12988     ;; Possibly translate some charaters.
12989     (nnheader-translate-file-chars file)))
12990
12991 (defun gnus-article-archive-name (group)
12992   "Return the first instance of an \"Archive-name\" in the current buffer."
12993   (let ((case-fold-search t))
12994     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12995       (match-string 1))))
12996
12997 (defun gnus-summary-save-in-rmail (&optional filename)
12998   "Append this article to Rmail file.
12999 Optional argument FILENAME specifies file name.
13000 Directory to save to is default to `gnus-article-save-directory' which
13001 is initialized from the SAVEDIR environment variable."
13002   (interactive)
13003   (gnus-set-global-variables)
13004   (let ((default-name
13005           (funcall gnus-rmail-save-name gnus-newsgroup-name
13006                    gnus-current-headers gnus-newsgroup-last-rmail)))
13007     (setq filename
13008           (cond ((eq filename 'default)
13009                  default-name)
13010                 (filename filename)
13011                 (t (gnus-read-save-file-name
13012                     "Save in rmail file:" default-name))))
13013     (gnus-make-directory (file-name-directory filename))
13014     (gnus-eval-in-buffer-window
13015      gnus-original-article-buffer
13016      (save-excursion
13017        (save-restriction
13018          (widen)
13019          (gnus-output-to-rmail filename))))
13020     ;; Remember the directory name to save articles
13021     (setq gnus-newsgroup-last-rmail filename)))
13022
13023 (defun gnus-summary-save-in-mail (&optional filename)
13024   "Append this article to Unix mail file.
13025 Optional argument FILENAME specifies file name.
13026 Directory to save to is default to `gnus-article-save-directory' which
13027 is initialized from the SAVEDIR environment variable."
13028   (interactive)
13029   (gnus-set-global-variables)
13030   (let ((default-name
13031           (funcall gnus-mail-save-name gnus-newsgroup-name
13032                    gnus-current-headers gnus-newsgroup-last-mail)))
13033     (setq filename
13034           (cond ((eq filename 'default)
13035                  default-name)
13036                 (filename filename)
13037                 (t (gnus-read-save-file-name
13038                     "Save in Unix mail file:" default-name))))
13039     (setq filename
13040           (expand-file-name filename
13041                             (and default-name
13042                                  (file-name-directory default-name))))
13043     (gnus-make-directory (file-name-directory filename))
13044     (gnus-eval-in-buffer-window
13045      gnus-original-article-buffer
13046      (save-excursion
13047        (save-restriction
13048          (widen)
13049          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13050              (gnus-output-to-rmail filename)
13051            (let ((mail-use-rfc822 t))
13052              (rmail-output filename 1 t t))))))
13053     ;; Remember the directory name to save articles.
13054     (setq gnus-newsgroup-last-mail filename)))
13055
13056 (defun gnus-summary-save-in-file (&optional filename)
13057   "Append this article to file.
13058 Optional argument FILENAME specifies file name.
13059 Directory to save to is default to `gnus-article-save-directory' which
13060 is initialized from the SAVEDIR environment variable."
13061   (interactive)
13062   (gnus-set-global-variables)
13063   (let ((default-name
13064           (funcall gnus-file-save-name gnus-newsgroup-name
13065                    gnus-current-headers gnus-newsgroup-last-file)))
13066     (setq filename
13067           (cond ((eq filename 'default)
13068                  default-name)
13069                 (filename filename)
13070                 (t (gnus-read-save-file-name
13071                     "Save in file:" default-name))))
13072     (gnus-make-directory (file-name-directory filename))
13073     (gnus-eval-in-buffer-window
13074      gnus-original-article-buffer
13075      (save-excursion
13076        (save-restriction
13077          (widen)
13078          (gnus-output-to-file filename))))
13079     ;; Remember the directory name to save articles.
13080     (setq gnus-newsgroup-last-file filename)))
13081
13082 (defun gnus-summary-save-body-in-file (&optional filename)
13083   "Append this article body to a file.
13084 Optional argument FILENAME specifies file name.
13085 The directory to save in defaults to `gnus-article-save-directory' which
13086 is initialized from the SAVEDIR environment variable."
13087   (interactive)
13088   (gnus-set-global-variables)
13089   (let ((default-name
13090           (funcall gnus-file-save-name gnus-newsgroup-name
13091                    gnus-current-headers gnus-newsgroup-last-file)))
13092     (setq filename
13093           (cond ((eq filename 'default)
13094                  default-name)
13095                 (filename filename)
13096                 (t (gnus-read-save-file-name
13097                     "Save body in file:" default-name))))
13098     (gnus-make-directory (file-name-directory filename))
13099     (gnus-eval-in-buffer-window
13100      gnus-article-buffer
13101      (save-excursion
13102        (save-restriction
13103          (widen)
13104          (goto-char (point-min))
13105          (and (search-forward "\n\n" nil t)
13106               (narrow-to-region (point) (point-max)))
13107          (gnus-output-to-file filename))))
13108     ;; Remember the directory name to save articles.
13109     (setq gnus-newsgroup-last-file filename)))
13110
13111 (defun gnus-summary-save-in-pipe (&optional command)
13112   "Pipe this article to subprocess."
13113   (interactive)
13114   (gnus-set-global-variables)
13115   (setq command
13116         (cond ((eq command 'default)
13117                gnus-last-shell-command)
13118               (command command)
13119               (t (read-string "Shell command on article: "
13120                               gnus-last-shell-command))))
13121   (if (string-equal command "")
13122       (setq command gnus-last-shell-command))
13123   (gnus-eval-in-buffer-window
13124    gnus-article-buffer
13125    (save-restriction
13126      (widen)
13127      (shell-command-on-region (point-min) (point-max) command nil)))
13128   (setq gnus-last-shell-command command))
13129
13130 ;; Summary extract commands
13131
13132 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13133   (let ((buffer-read-only nil)
13134         (article (gnus-summary-article-number))
13135         after-article b e)
13136     (or (gnus-summary-goto-subject article)
13137         (error (format "No such article: %d" article)))
13138     (gnus-summary-position-point)
13139     ;; If all commands are to be bunched up on one line, we collect
13140     ;; them here.
13141     (if gnus-view-pseudos-separately
13142         ()
13143       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13144             files action)
13145         (while ps
13146           (setq action (cdr (assq 'action (car ps))))
13147           (setq files (list (cdr (assq 'name (car ps)))))
13148           (while (and ps (cdr ps)
13149                       (string= (or action "1")
13150                                (or (cdr (assq 'action (cadr ps))) "2")))
13151             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13152             (setcdr ps (cddr ps)))
13153           (if (not files)
13154               ()
13155             (if (not (string-match "%s" action))
13156                 (setq files (cons " " files)))
13157             (setq files (cons " " files))
13158             (and (assq 'execute (car ps))
13159                  (setcdr (assq 'execute (car ps))
13160                          (funcall (if (string-match "%s" action)
13161                                       'format 'concat)
13162                                   action
13163                                   (mapconcat (lambda (f) f) files " ")))))
13164           (setq ps (cdr ps)))))
13165     (if (and gnus-view-pseudos (not not-view))
13166         (while pslist
13167           (and (assq 'execute (car pslist))
13168                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13169                                      (eq gnus-view-pseudos 'not-confirm)))
13170           (setq pslist (cdr pslist)))
13171       (save-excursion
13172         (while pslist
13173           (setq after-article (or (cdr (assq 'article (car pslist)))
13174                                   (gnus-summary-article-number)))
13175           (gnus-summary-goto-subject after-article)
13176           (forward-line 1)
13177           (setq b (point))
13178           (insert "    " (file-name-nondirectory
13179                                 (cdr (assq 'name (car pslist))))
13180                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13181           (setq e (point))
13182           (forward-line -1)             ; back to `b'
13183           (add-text-properties
13184            b e (list 'gnus-number gnus-reffed-article-number
13185                      gnus-mouse-face-prop gnus-mouse-face))
13186           (gnus-data-enter
13187            after-article gnus-reffed-article-number
13188            gnus-unread-mark b (car pslist) 0 (- e b))
13189           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13190           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13191           (setq pslist (cdr pslist)))))))
13192
13193 (defun gnus-pseudos< (p1 p2)
13194   (let ((c1 (cdr (assq 'action p1)))
13195         (c2 (cdr (assq 'action p2))))
13196     (and c1 c2 (string< c1 c2))))
13197
13198 (defun gnus-request-pseudo-article (props)
13199   (cond ((assq 'execute props)
13200          (gnus-execute-command (cdr (assq 'execute props)))))
13201   (let ((gnus-current-article (gnus-summary-article-number)))
13202     (run-hooks 'gnus-mark-article-hook)))
13203
13204 (defun gnus-execute-command (command &optional automatic)
13205   (save-excursion
13206     (gnus-article-setup-buffer)
13207     (set-buffer gnus-article-buffer)
13208     (setq buffer-read-only nil)
13209     (let ((command (if automatic command (read-string "Command: " command)))
13210           ;; Just binding this here doesn't help, because there might
13211           ;; be output from the process after exiting the scope of 
13212           ;; this `let'.
13213           ;; (buffer-read-only nil)
13214           )
13215       (erase-buffer)
13216       (insert "$ " command "\n\n")
13217       (if gnus-view-pseudo-asynchronously
13218           (start-process "gnus-execute" nil "sh" "-c" command)
13219         (call-process "sh" nil t nil "-c" command)))))
13220
13221 (defun gnus-copy-file (file &optional to)
13222   "Copy FILE to TO."
13223   (interactive
13224    (list (read-file-name "Copy file: " default-directory)
13225          (read-file-name "Copy file to: " default-directory)))
13226   (gnus-set-global-variables)
13227   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13228   (and (file-directory-p to)
13229        (setq to (concat (file-name-as-directory to)
13230                         (file-name-nondirectory file))))
13231   (copy-file file to))
13232
13233 ;; Summary kill commands.
13234
13235 (defun gnus-summary-edit-global-kill (article)
13236   "Edit the \"global\" kill file."
13237   (interactive (list (gnus-summary-article-number)))
13238   (gnus-set-global-variables)
13239   (gnus-group-edit-global-kill article))
13240
13241 (defun gnus-summary-edit-local-kill ()
13242   "Edit a local kill file applied to the current newsgroup."
13243   (interactive)
13244   (gnus-set-global-variables)
13245   (setq gnus-current-headers (gnus-summary-article-header))
13246   (gnus-set-global-variables)
13247   (gnus-group-edit-local-kill
13248    (gnus-summary-article-number) gnus-newsgroup-name))
13249
13250 \f
13251 ;;;
13252 ;;; Gnus article mode
13253 ;;;
13254
13255 (put 'gnus-article-mode 'mode-class 'special)
13256
13257 (if gnus-article-mode-map
13258     nil
13259   (setq gnus-article-mode-map (make-keymap))
13260   (suppress-keymap gnus-article-mode-map)
13261
13262   (gnus-define-keys gnus-article-mode-map
13263     " " gnus-article-goto-next-page
13264     "\177" gnus-article-goto-prev-page
13265     [delete] gnus-article-goto-prev-page
13266     "\C-c^" gnus-article-refer-article
13267     "h" gnus-article-show-summary
13268     "s" gnus-article-show-summary
13269     "\C-c\C-m" gnus-article-mail
13270     "?" gnus-article-describe-briefly
13271     gnus-mouse-2 gnus-article-push-button
13272     "\r" gnus-article-press-button
13273     "\t" gnus-article-next-button
13274     "\M-\t" gnus-article-prev-button
13275     "\C-c\C-b" gnus-bug)
13276
13277   (substitute-key-definition
13278    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13279
13280 (defun gnus-article-mode ()
13281   "Major mode for displaying an article.
13282
13283 All normal editing commands are switched off.
13284
13285 The following commands are available:
13286
13287 \\<gnus-article-mode-map>
13288 \\[gnus-article-next-page]\t Scroll the article one page forwards
13289 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13290 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13291 \\[gnus-article-show-summary]\t Display the summary buffer
13292 \\[gnus-article-mail]\t Send a reply to the address near point
13293 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13294 \\[gnus-info-find-node]\t Go to the Gnus info node"
13295   (interactive)
13296   (when (and menu-bar-mode
13297              (gnus-visual-p 'article-menu 'menu))
13298     (gnus-article-make-menu-bar))
13299   (kill-all-local-variables)
13300   (gnus-simplify-mode-line)
13301   (setq mode-name "Article")
13302   (setq major-mode 'gnus-article-mode)
13303   (make-local-variable 'minor-mode-alist)
13304   (or (assq 'gnus-show-mime minor-mode-alist)
13305       (setq minor-mode-alist
13306             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13307   (use-local-map gnus-article-mode-map)
13308   (make-local-variable 'page-delimiter)
13309   (setq page-delimiter gnus-page-delimiter)
13310   (buffer-disable-undo (current-buffer))
13311   (setq buffer-read-only t)             ;Disable modification
13312   (run-hooks 'gnus-article-mode-hook))
13313
13314 (defun gnus-article-setup-buffer ()
13315   "Initialize the article buffer."
13316   (let* ((name (if gnus-single-article-buffer "*Article*"
13317                  (concat "*Article " gnus-newsgroup-name "*")))
13318          (original
13319           (progn (string-match "\\*Article" name)
13320                  (concat " *Original Article"
13321                          (substring name (match-end 0))))))
13322     (setq gnus-article-buffer name)
13323     (setq gnus-original-article-buffer original)
13324     ;; This might be a variable local to the summary buffer.
13325     (unless gnus-single-article-buffer
13326       (save-excursion
13327         (set-buffer gnus-summary-buffer)
13328         (setq gnus-article-buffer name)
13329         (setq gnus-original-article-buffer original)
13330         (gnus-set-global-variables))
13331       (make-local-variable 'gnus-summary-buffer))
13332     ;; Init original article buffer.
13333     (save-excursion
13334       (set-buffer (get-buffer-create gnus-original-article-buffer))
13335       (buffer-disable-undo (current-buffer))
13336       (setq major-mode 'gnus-original-article-mode)
13337       (make-local-variable 'gnus-original-article))
13338     (if (get-buffer name)
13339         (save-excursion
13340           (set-buffer name)
13341           (buffer-disable-undo (current-buffer))
13342           (setq buffer-read-only t)
13343           (gnus-add-current-to-buffer-list)
13344           (or (eq major-mode 'gnus-article-mode)
13345               (gnus-article-mode))
13346           (current-buffer))
13347       (save-excursion
13348         (set-buffer (get-buffer-create name))
13349         (gnus-add-current-to-buffer-list)
13350         (gnus-article-mode)
13351         (current-buffer)))))
13352
13353 ;; Set article window start at LINE, where LINE is the number of lines
13354 ;; from the head of the article.
13355 (defun gnus-article-set-window-start (&optional line)
13356   (set-window-start
13357    (get-buffer-window gnus-article-buffer)
13358    (save-excursion
13359      (set-buffer gnus-article-buffer)
13360      (goto-char (point-min))
13361      (if (not line)
13362          (point-min)
13363        (gnus-message 6 "Moved to bookmark")
13364        (search-forward "\n\n" nil t)
13365        (forward-line line)
13366        (point)))))
13367
13368 (defun gnus-kill-all-overlays ()
13369   "Delete all overlays in the current buffer."
13370   (when (fboundp 'overlay-lists)
13371     (let* ((overlayss (overlay-lists))
13372            (buffer-read-only nil)
13373            (overlays (nconc (car overlayss) (cdr overlayss))))
13374       (while overlays
13375         (delete-overlay (pop overlays))))))
13376
13377 (defun gnus-request-article-this-buffer (article group)
13378   "Get an article and insert it into this buffer."
13379   (let (do-update-line)
13380     (prog1
13381         (save-excursion
13382           (erase-buffer)
13383           (gnus-kill-all-overlays)
13384           (setq group (or group gnus-newsgroup-name))
13385
13386           ;; Open server if it has closed.
13387           (gnus-check-server (gnus-find-method-for-group group))
13388
13389           ;; Using `gnus-request-article' directly will insert the article into
13390           ;; `nntp-server-buffer' - so we'll save some time by not having to
13391           ;; copy it from the server buffer into the article buffer.
13392
13393           ;; We only request an article by message-id when we do not have the
13394           ;; headers for it, so we'll have to get those.
13395           (when (stringp article)
13396             (let ((gnus-override-method gnus-refer-article-method))
13397               (gnus-read-header article)))
13398
13399           ;; If the article number is negative, that means that this article
13400           ;; doesn't belong in this newsgroup (possibly), so we find its
13401           ;; message-id and request it by id instead of number.
13402           (when (and (numberp article)
13403                      gnus-summary-buffer
13404                      (get-buffer gnus-summary-buffer)
13405                      (buffer-name (get-buffer gnus-summary-buffer)))
13406             (save-excursion
13407               (set-buffer gnus-summary-buffer)
13408               (let ((header (gnus-summary-article-header article)))
13409                 (if (< article 0)
13410                     (cond 
13411                      ((memq article gnus-newsgroup-sparse)
13412                       ;; This is a sparse gap article.
13413                       (setq do-update-line article)
13414                       (setq article (mail-header-id header))
13415                       (let ((gnus-override-method gnus-refer-article-method))
13416                         (gnus-read-header article))
13417                       (setq gnus-newsgroup-sparse
13418                             (delq article gnus-newsgroup-sparse)))
13419                      ((vectorp header)
13420                       ;; It's a real article.
13421                       (setq article (mail-header-id header)))
13422                      (t
13423                       ;; It is an extracted pseudo-article.
13424                       (setq article 'pseudo)
13425                       (gnus-request-pseudo-article header))))
13426                 
13427                 (let ((method (gnus-find-method-for-group 
13428                                gnus-newsgroup-name)))
13429                   (if (not (eq (car method) 'nneething))
13430                       ()
13431                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13432                                        (mail-header-subject header))))
13433                       (if (file-directory-p dir)
13434                           (progn
13435                             (setq article 'nneething)
13436                             (gnus-group-enter-directory dir)))))))))
13437
13438           (cond
13439            ;; Refuse to select canceled articles.
13440            ((and (numberp article)
13441                  gnus-summary-buffer
13442                  (get-buffer gnus-summary-buffer)
13443                  (buffer-name (get-buffer gnus-summary-buffer))
13444                  (eq (cdr (save-excursion
13445                             (set-buffer gnus-summary-buffer)
13446                             (assq article gnus-newsgroup-reads)))
13447                      gnus-canceled-mark))
13448             nil)
13449            ;; We first check `gnus-original-article-buffer'.
13450            ((and (get-buffer gnus-original-article-buffer)
13451                  (numberp article)
13452                  (save-excursion
13453                    (set-buffer gnus-original-article-buffer)
13454                    (and (equal (car gnus-original-article) group)
13455                         (eq (cdr gnus-original-article) article))))
13456             (insert-buffer-substring gnus-original-article-buffer)
13457             'article)
13458            ;; Check the backlog.
13459            ((and gnus-keep-backlog
13460                  (gnus-backlog-request-article group article (current-buffer)))
13461             'article)
13462            ;; Check the cache.
13463            ((and gnus-use-cache
13464                  (numberp article)
13465                  (gnus-cache-request-article article group))
13466             'article)
13467            ;; Get the article and put into the article buffer.
13468            ((or (stringp article) (numberp article))
13469             (let ((gnus-override-method
13470                    (and (stringp article) gnus-refer-article-method))
13471                   (buffer-read-only nil))
13472               (erase-buffer)
13473               (gnus-kill-all-overlays)
13474               (if (gnus-request-article article group (current-buffer))
13475                   (progn
13476                     (and gnus-keep-backlog
13477                          (numberp article)
13478                          (gnus-backlog-enter-article
13479                           group article (current-buffer)))
13480                     'article))))
13481            ;; It was a pseudo.
13482            (t article)))
13483
13484       ;; Take the article from the original article buffer
13485       ;; and place it in the buffer it's supposed to be in.
13486       (when (and (get-buffer gnus-article-buffer)
13487                  (numberp article)
13488                  (equal (buffer-name (current-buffer))
13489                         (buffer-name (get-buffer gnus-article-buffer))))
13490         (save-excursion
13491           (if (get-buffer gnus-original-article-buffer)
13492               (set-buffer (get-buffer gnus-original-article-buffer))
13493             (set-buffer (get-buffer-create gnus-original-article-buffer))
13494             (buffer-disable-undo (current-buffer))
13495             (setq major-mode 'gnus-original-article-mode)
13496             (setq buffer-read-only t)
13497             (gnus-add-current-to-buffer-list))
13498           (let (buffer-read-only)
13499             (erase-buffer)
13500             (insert-buffer-substring gnus-article-buffer))
13501           (setq gnus-original-article (cons group article))))
13502     
13503       ;; Update sparse articles.
13504       (when (and do-update-line
13505                  (or (numberp article)
13506                      (stringp article)))
13507         (let ((buf (current-buffer)))
13508           (set-buffer gnus-summary-buffer)
13509           (gnus-summary-update-article do-update-line)
13510           (gnus-summary-goto-subject do-update-line nil t)
13511           (set-window-point (get-buffer-window (current-buffer) t)
13512                             (point))
13513           (set-buffer buf))))))
13514
13515 (defun gnus-read-header (id &optional header)
13516   "Read the headers of article ID and enter them into the Gnus system."
13517   (let ((group gnus-newsgroup-name)
13518         (gnus-override-method 
13519          (and (gnus-news-group-p gnus-newsgroup-name)
13520               gnus-refer-article-method))       
13521         where)
13522     ;; First we check to see whether the header in question is already
13523     ;; fetched.
13524     (if (stringp id)
13525         ;; This is a Message-ID.
13526         (setq header (or header (gnus-id-to-header id)))
13527       ;; This is an article number.
13528       (setq header (or header (gnus-summary-article-header id))))
13529     (if (and header
13530              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13531         ;; We have found the header.
13532         header
13533       ;; We have to really fetch the header to this article.
13534       (when (setq where
13535                   (if (gnus-check-backend-function 'request-head group)
13536                       (gnus-request-head id group)
13537                     (gnus-request-article id group)))
13538         (save-excursion
13539           (set-buffer nntp-server-buffer)
13540           (and (search-forward "\n\n" nil t)
13541                (delete-region (1- (point)) (point-max)))
13542           (goto-char (point-max))
13543           (insert ".\n")
13544           (goto-char (point-min))
13545           (insert "211 ")
13546           (princ (cond
13547                   ((numberp id) id)
13548                   ((cdr where) (cdr where))
13549                   (header (mail-header-number header))
13550                   (t gnus-reffed-article-number))
13551                  (current-buffer))
13552           (insert " Article retrieved.\n"))
13553         ;(when (and header
13554         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13555         ;  (setcar (gnus-id-to-thread id) nil))
13556         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13557             ()                          ; Malformed head.
13558           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13559             (if (and (stringp id)
13560                      (not (string= (gnus-group-real-name group)
13561                                    (car where))))
13562                 ;; If we fetched by Message-ID and the article came
13563                 ;; from a different group, we fudge some bogus article
13564                 ;; numbers for this article.
13565                 (mail-header-set-number header gnus-reffed-article-number))
13566             (decf gnus-reffed-article-number)
13567             (push header gnus-newsgroup-headers)
13568             (setq gnus-current-headers header)
13569             (push (mail-header-number header) gnus-newsgroup-limit))
13570           header)))))
13571
13572 (defun gnus-article-prepare (article &optional all-headers header)
13573   "Prepare ARTICLE in article mode buffer.
13574 ARTICLE should either be an article number or a Message-ID.
13575 If ARTICLE is an id, HEADER should be the article headers.
13576 If ALL-HEADERS is non-nil, no headers are hidden."
13577   (save-excursion
13578     ;; Make sure we start in a summary buffer.
13579     (unless (eq major-mode 'gnus-summary-mode)
13580       (set-buffer gnus-summary-buffer))
13581     (setq gnus-summary-buffer (current-buffer))
13582     ;; Make sure the connection to the server is alive.
13583     (unless (gnus-server-opened
13584              (gnus-find-method-for-group gnus-newsgroup-name))
13585       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13586       (gnus-request-group gnus-newsgroup-name t))
13587     (let* ((article (if header (mail-header-number header) article))
13588            (summary-buffer (current-buffer))
13589            (internal-hook gnus-article-internal-prepare-hook)
13590            (group gnus-newsgroup-name)
13591            result)
13592       (save-excursion
13593         (gnus-article-setup-buffer)
13594         (set-buffer gnus-article-buffer)
13595         ;; Deactivate active regions.
13596         (when (and (boundp 'transient-mark-mode)
13597                    transient-mark-mode)
13598           (setq mark-active nil))
13599         (if (not (setq result (let ((buffer-read-only nil))
13600                                 (gnus-request-article-this-buffer
13601                                  article group))))
13602             ;; There is no such article.
13603             (save-excursion
13604               (when (and (numberp article)
13605                          (not (memq article gnus-newsgroup-sparse)))
13606                 (setq gnus-article-current
13607                       (cons gnus-newsgroup-name article))
13608                 (set-buffer gnus-summary-buffer)
13609                 (setq gnus-current-article article)
13610                 (gnus-summary-mark-article article gnus-canceled-mark))
13611               (unless (memq article gnus-newsgroup-sparse)
13612                 (gnus-error
13613                  1 "No such article (may have expired or been canceled)")))
13614           (if (or (eq result 'pseudo) (eq result 'nneething))
13615               (progn
13616                 (save-excursion
13617                   (set-buffer summary-buffer)
13618                   (setq gnus-last-article gnus-current-article
13619                         gnus-newsgroup-history (cons gnus-current-article
13620                                                      gnus-newsgroup-history)
13621                         gnus-current-article 0
13622                         gnus-current-headers nil
13623                         gnus-article-current nil)
13624                   (if (eq result 'nneething)
13625                       (gnus-configure-windows 'summary)
13626                     (gnus-configure-windows 'article))
13627                   (gnus-set-global-variables))
13628                 (gnus-set-mode-line 'article))
13629             ;; The result from the `request' was an actual article -
13630             ;; or at least some text that is now displayed in the
13631             ;; article buffer.
13632             (if (and (numberp article)
13633                      (not (eq article gnus-current-article)))
13634                 ;; Seems like a new article has been selected.
13635                 ;; `gnus-current-article' must be an article number.
13636                 (save-excursion
13637                   (set-buffer summary-buffer)
13638                   (setq gnus-last-article gnus-current-article
13639                         gnus-newsgroup-history (cons gnus-current-article
13640                                                      gnus-newsgroup-history)
13641                         gnus-current-article article
13642                         gnus-current-headers
13643                         (gnus-summary-article-header gnus-current-article)
13644                         gnus-article-current
13645                         (cons gnus-newsgroup-name gnus-current-article))
13646                   (unless (vectorp gnus-current-headers)
13647                     (setq gnus-current-headers nil))
13648                   (gnus-summary-show-thread)
13649                   (run-hooks 'gnus-mark-article-hook)
13650                   (gnus-set-mode-line 'summary)
13651                   (and (gnus-visual-p 'article-highlight 'highlight)
13652                        (run-hooks 'gnus-visual-mark-article-hook))
13653                   ;; Set the global newsgroup variables here.
13654                   ;; Suggested by Jim Sisolak
13655                   ;; <sisolak@trans4.neep.wisc.edu>.
13656                   (gnus-set-global-variables)
13657                   (setq gnus-have-all-headers
13658                         (or all-headers gnus-show-all-headers))
13659                   (and gnus-use-cache
13660                        (vectorp (gnus-summary-article-header article))
13661                        (gnus-cache-possibly-enter-article
13662                         group article
13663                         (gnus-summary-article-header article)
13664                         (memq article gnus-newsgroup-marked)
13665                         (memq article gnus-newsgroup-dormant)
13666                         (memq article gnus-newsgroup-unreads)))))
13667             (when (or (numberp article)
13668                       (stringp article))
13669               ;; Hooks for getting information from the article.
13670               ;; This hook must be called before being narrowed.
13671               (let (buffer-read-only)
13672                 (run-hooks 'internal-hook)
13673                 (run-hooks 'gnus-article-prepare-hook)
13674                 ;; Decode MIME message.
13675                 (if gnus-show-mime
13676                     (if (or (not gnus-strict-mime)
13677                             (gnus-fetch-field "Mime-Version"))
13678                         (funcall gnus-show-mime-method)
13679                       (funcall gnus-decode-encoded-word-method)))
13680                 ;; Perform the article display hooks.
13681                 (run-hooks 'gnus-article-display-hook))
13682               ;; Do page break.
13683               (goto-char (point-min))
13684               (and gnus-break-pages (gnus-narrow-to-page)))
13685             (gnus-set-mode-line 'article)
13686             (gnus-configure-windows 'article)
13687             (goto-char (point-min))
13688             t))))))
13689
13690 (defun gnus-article-show-all-headers ()
13691   "Show all article headers in article mode buffer."
13692   (save-excursion
13693     (gnus-article-setup-buffer)
13694     (set-buffer gnus-article-buffer)
13695     (let ((buffer-read-only nil))
13696       (gnus-unhide-text (point-min) (point-max)))))
13697
13698 (defun gnus-article-hide-headers-if-wanted ()
13699   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13700 Provided for backwards compatibility."
13701   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13702       gnus-inhibit-hiding
13703       (gnus-article-hide-headers)))
13704
13705 (defsubst gnus-article-header-rank ()
13706   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13707   (let ((list gnus-sorted-header-list)
13708         (i 0))
13709     (while list
13710       (when (looking-at (car list))
13711         (setq list nil))
13712       (setq list (cdr list))
13713       (incf i))
13714     i))
13715
13716 (defun gnus-article-hide-headers (&optional arg delete)
13717   "Toggle whether to hide unwanted headers and possibly sort them as well.
13718 If given a negative prefix, always show; if given a positive prefix,
13719 always hide."
13720   (interactive "P")
13721   (unless (gnus-article-check-hidden-text 'headers arg)
13722     ;; This function might be inhibited.
13723     (unless gnus-inhibit-hiding
13724       (save-excursion
13725         (set-buffer gnus-article-buffer)
13726         (save-restriction
13727           (let ((buffer-read-only nil)
13728                 (props (nconc (list 'gnus-type 'headers)
13729                               gnus-hidden-properties))
13730                 (max (1+ (length gnus-sorted-header-list)))
13731                 (ignored (when (not (stringp gnus-visible-headers))
13732                            (cond ((stringp gnus-ignored-headers)
13733                                   gnus-ignored-headers)
13734                                  ((listp gnus-ignored-headers)
13735                                   (mapconcat 'identity gnus-ignored-headers
13736                                              "\\|")))))
13737                 (visible
13738                  (cond ((stringp gnus-visible-headers)
13739                         gnus-visible-headers)
13740                        ((and gnus-visible-headers
13741                              (listp gnus-visible-headers))
13742                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13743                 want-list beg)
13744             ;; First we narrow to just the headers.
13745             (widen)
13746             (goto-char (point-min))
13747             ;; Hide any "From " lines at the beginning of (mail) articles.
13748             (while (looking-at "From ")
13749               (forward-line 1))
13750             (unless (bobp)
13751               (gnus-hide-text (point-min) (point) props))
13752             ;; Then treat the rest of the header lines.
13753             (narrow-to-region
13754              (point)
13755              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13756             ;; Then we use the two regular expressions
13757             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13758             ;; select which header lines is to remain visible in the
13759             ;; article buffer.
13760             (goto-char (point-min))
13761             (while (re-search-forward "^[^ \t]*:" nil t)
13762               (beginning-of-line)
13763               ;; We add the headers we want to keep to a list and delete
13764               ;; them from the buffer.
13765               (put-text-property 
13766                (point) (1+ (point)) 'message-rank
13767                (if (or (and visible (looking-at visible))
13768                        (and ignored
13769                             (not (looking-at ignored))))
13770                    (gnus-article-header-rank) 
13771                  (+ 2 max)))
13772               (forward-line 1))
13773             (message-sort-headers-1)
13774             (when (setq beg (text-property-any 
13775                              (point-min) (point-max) 'message-rank (+ 2 max)))
13776               ;; We make the unwanted headers invisible.
13777               (if delete
13778                   (delete-region beg (point-max))
13779                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13780                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13781
13782 (defun gnus-article-hide-boring-headers (&optional arg)
13783   "Toggle hiding of headers that aren't very interesting.
13784 If given a negative prefix, always show; if given a positive prefix,
13785 always hide."
13786   (interactive "P")
13787   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13788     (save-excursion
13789       (set-buffer gnus-article-buffer)
13790       (save-restriction
13791         (let ((buffer-read-only nil)
13792               (list gnus-boring-article-headers)
13793               (inhibit-point-motion-hooks t)
13794               elem)
13795           (nnheader-narrow-to-headers)
13796           (while list
13797             (setq elem (pop list))
13798             (goto-char (point-min))
13799             (cond
13800              ;; Hide empty headers.
13801              ((eq elem 'empty)
13802               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13803                 (forward-line -1)
13804                 (gnus-hide-text-type
13805                  (progn (beginning-of-line) (point))
13806                  (progn 
13807                    (end-of-line)
13808                    (if (re-search-forward "^[^ \t]" nil t)
13809                        (match-beginning 0)
13810                      (point-max)))
13811                  'boring-headers)))
13812              ;; Hide boring Newsgroups header.
13813              ((eq elem 'newsgroups)
13814               (when (equal (mail-fetch-field "newsgroups")
13815                            (gnus-group-real-name gnus-newsgroup-name))
13816                 (gnus-article-hide-header "newsgroups")))
13817              ((eq elem 'followup-to)
13818               (when (equal (mail-fetch-field "followup-to")
13819                            (mail-fetch-field "newsgroups"))
13820                 (gnus-article-hide-header "followup-to")))
13821              ((eq elem 'reply-to)
13822               (let ((from (mail-fetch-field "from"))
13823                     (reply-to (mail-fetch-field "reply-to")))
13824                 (when (and
13825                        from reply-to
13826                        (equal 
13827                         (nth 1 (funcall gnus-extract-address-components from))
13828                         (nth 1 (funcall gnus-extract-address-components
13829                                         reply-to))))
13830                   (gnus-article-hide-header "reply-to"))))
13831              ((eq elem 'date)
13832               (let ((date (mail-fetch-field "date")))
13833                 (when (and date
13834                            (< (gnus-days-between date (current-time-string))
13835                               4))
13836                   (gnus-article-hide-header "date")))))))))))
13837
13838 (defun gnus-article-hide-header (header)
13839   (save-excursion
13840     (goto-char (point-min))
13841     (when (re-search-forward (concat "^" header ":") nil t)
13842       (gnus-hide-text-type
13843        (progn (beginning-of-line) (point))
13844        (progn 
13845          (end-of-line)
13846          (if (re-search-forward "^[^ \t]" nil t)
13847              (match-beginning 0)
13848            (point-max)))
13849        'boring-headers))))
13850
13851 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13852 (defun gnus-article-treat-overstrike ()
13853   "Translate overstrikes into bold text."
13854   (interactive)
13855   (save-excursion
13856     (set-buffer gnus-article-buffer)
13857     (let ((buffer-read-only nil))
13858       (while (search-forward "\b" nil t)
13859         (let ((next (following-char))
13860               (previous (char-after (- (point) 2))))
13861           (cond ((eq next previous)
13862                  (put-text-property (- (point) 2) (point) 'invisible t)
13863                  (put-text-property (point) (1+ (point)) 'face 'bold))
13864                 ((eq next ?_)
13865                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13866                  (put-text-property
13867                   (- (point) 2) (1- (point)) 'face 'underline))
13868                 ((eq previous ?_)
13869                  (put-text-property (- (point) 2) (point) 'invisible t)
13870                  (put-text-property
13871                   (point) (1+ (point))  'face 'underline))))))))
13872
13873 (defun gnus-article-word-wrap ()
13874   "Format too long lines."
13875   (interactive)
13876   (save-excursion
13877     (set-buffer gnus-article-buffer)
13878     (let ((buffer-read-only nil))
13879       (widen)
13880       (goto-char (point-min))
13881       (search-forward "\n\n" nil t)
13882       (end-of-line 1)
13883       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13884             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13885             (adaptive-fill-mode t))
13886         (while (not (eobp))
13887           (and (>= (current-column) (min fill-column (window-width)))
13888                (/= (preceding-char) ?:)
13889                (fill-paragraph nil))
13890           (end-of-line 2))))))
13891
13892 (defun gnus-article-remove-cr ()
13893   "Remove carriage returns from an article."
13894   (interactive)
13895   (save-excursion
13896     (set-buffer gnus-article-buffer)
13897     (let ((buffer-read-only nil))
13898       (goto-char (point-min))
13899       (while (search-forward "\r" nil t)
13900         (replace-match "" t t)))))
13901
13902 (defun gnus-article-remove-trailing-blank-lines ()
13903   "Remove all trailing blank lines from the article."
13904   (interactive)
13905   (save-excursion
13906     (set-buffer gnus-article-buffer)
13907     (let ((buffer-read-only nil))
13908       (goto-char (point-max))
13909       (delete-region
13910        (point)
13911        (progn
13912          (while (looking-at "^[ \t]*$")
13913            (forward-line -1))
13914          (forward-line 1)
13915          (point))))))
13916
13917 (defun gnus-article-display-x-face (&optional force)
13918   "Look for an X-Face header and display it if present."
13919   (interactive (list 'force))
13920   (save-excursion
13921     (set-buffer gnus-article-buffer)
13922     ;; Delete the old process, if any.
13923     (when (process-status "gnus-x-face")
13924       (delete-process "gnus-x-face"))
13925     (let ((inhibit-point-motion-hooks t)
13926           (case-fold-search nil)
13927           from)
13928       (save-restriction
13929         (nnheader-narrow-to-headers)
13930         (setq from (mail-fetch-field "from"))
13931         (goto-char (point-min))
13932         (when (and gnus-article-x-face-command
13933                    (or force
13934                        ;; Check whether this face is censored.
13935                        (not gnus-article-x-face-too-ugly)
13936                        (and gnus-article-x-face-too-ugly from
13937                             (not (string-match gnus-article-x-face-too-ugly
13938                                                from))))
13939                    ;; Has to be present.
13940                    (re-search-forward "^X-Face: " nil t))
13941           ;; We now have the area of the buffer where the X-Face is stored.
13942           (let ((beg (point))
13943                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13944             ;; We display the face.
13945             (if (symbolp gnus-article-x-face-command)
13946                 ;; The command is a lisp function, so we call it.
13947                 (if (gnus-functionp gnus-article-x-face-command)
13948                     (funcall gnus-article-x-face-command beg end)
13949                   (error "%s is not a function" gnus-article-x-face-command))
13950               ;; The command is a string, so we interpret the command
13951               ;; as a, well, command, and fork it off.
13952               (let ((process-connection-type nil))
13953                 (process-kill-without-query
13954                  (start-process
13955                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13956                 (process-send-region "gnus-x-face" beg end)
13957                 (process-send-eof "gnus-x-face")))))))))
13958
13959 (defun gnus-headers-decode-quoted-printable ()
13960   "Hack to remove QP encoding from headers."
13961   (let ((case-fold-search t)
13962         (inhibit-point-motion-hooks t)
13963         (buffer-read-only nil)
13964         string)
13965     (goto-char (point-min))
13966     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13967       (setq string (match-string 1))
13968       (narrow-to-region (match-beginning 0) (match-end 0))
13969       (delete-region (point-min) (point-max))
13970       (insert string)
13971       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13972       (subst-char-in-region (point-min) (point-max) ?_ ? )
13973       (widen)
13974       (goto-char (point-min)))))
13975
13976 (defun gnus-article-de-quoted-unreadable (&optional force)
13977   "Do a naive translation of a quoted-printable-encoded article.
13978 This is in no way, shape or form meant as a replacement for real MIME
13979 processing, but is simply a stop-gap measure until MIME support is
13980 written.
13981 If FORCE, decode the article whether it is marked as quoted-printable
13982 or not."
13983   (interactive (list 'force))
13984   (save-excursion
13985     (set-buffer gnus-article-buffer)
13986     (let ((case-fold-search t)
13987           (buffer-read-only nil)
13988           (type (gnus-fetch-field "content-transfer-encoding")))
13989       (gnus-headers-decode-quoted-printable)
13990       (when (or force
13991                 (and type (string-match "quoted-printable" (downcase type))))
13992         (goto-char (point-min))
13993         (search-forward "\n\n" nil 'move)
13994         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13995
13996 (defun gnus-mime-decode-quoted-printable (from to)
13997   "Decode Quoted-Printable in the region between FROM and TO."
13998   (interactive "r")
13999   (goto-char from)
14000   (while (search-forward "=" to t)
14001     (cond ((eq (following-char) ?\n)
14002            (delete-char -1)
14003            (delete-char 1))
14004           ((looking-at "[0-9A-F][0-9A-F]")
14005            (subst-char-in-region
14006             (1- (point)) (point) ?=
14007             (hexl-hex-string-to-integer
14008              (buffer-substring (point) (+ 2 (point)))))
14009            (delete-char 2))
14010           ((looking-at "=")
14011            (delete-char 1))
14012           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14013
14014 (defun gnus-article-hide-pgp (&optional arg)
14015   "Toggle hiding of any PGP headers and signatures in the current article.
14016 If given a negative prefix, always show; if given a positive prefix,
14017 always hide."
14018   (interactive "P")
14019   (unless (gnus-article-check-hidden-text 'pgp arg)
14020     (save-excursion
14021       (set-buffer gnus-article-buffer)
14022       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14023             buffer-read-only beg end)
14024         (widen)
14025         (goto-char (point-min))
14026         ;; Hide the "header".
14027         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14028              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14029         (setq beg (point))
14030         ;; Hide the actual signature.
14031         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14032              (setq end (1+ (match-beginning 0)))
14033              (gnus-hide-text
14034               end
14035               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14036                   (match-end 0)
14037                 ;; Perhaps we shouldn't hide to the end of the buffer
14038                 ;; if there is no end to the signature?
14039                 (point-max))
14040               props))
14041         ;; Hide "- " PGP quotation markers.
14042         (when (and beg end)
14043           (narrow-to-region beg end)
14044           (goto-char (point-min))
14045           (while (re-search-forward "^- " nil t)
14046             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14047           (widen))))))
14048
14049 (defun gnus-article-hide-signature (&optional arg)
14050   "Hide the signature in the current article.
14051 If given a negative prefix, always show; if given a positive prefix,
14052 always hide."
14053   (interactive "P")
14054   (unless (gnus-article-check-hidden-text 'signature arg)
14055     (save-excursion
14056       (set-buffer gnus-article-buffer)
14057       (save-restriction
14058         (let ((buffer-read-only nil))
14059           (when (gnus-narrow-to-signature)
14060             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14061
14062 (defun gnus-article-strip-leading-blank-lines ()
14063   "Remove all blank lines from the beginning of the article."
14064   (interactive)
14065   (save-excursion
14066     (set-buffer gnus-article-buffer)
14067     (let (buffer-read-only)
14068       (goto-char (point-min))
14069       (when (search-forward "\n\n" nil t)
14070         (while (looking-at "[ \t]$")
14071           (gnus-delete-line))))))
14072
14073 (defun gnus-narrow-to-signature ()
14074   "Narrow to the signature."
14075   (widen)
14076   (goto-char (point-max))
14077   (when (re-search-backward gnus-signature-separator nil t)
14078     (forward-line 1)
14079     (when (or (null gnus-signature-limit)
14080               (and (numberp gnus-signature-limit)
14081                    (< (- (point-max) (point)) gnus-signature-limit))
14082               (and (gnus-functionp gnus-signature-limit)
14083                    (funcall gnus-signature-limit))
14084               (and (stringp gnus-signature-limit)
14085                    (not (re-search-forward gnus-signature-limit nil t))))
14086       (narrow-to-region (point) (point-max))
14087       t)))
14088
14089 (defun gnus-article-check-hidden-text (type arg)
14090   "Return nil if hiding is necessary."
14091   (save-excursion
14092     (set-buffer gnus-article-buffer)
14093     (let ((hide (gnus-article-hidden-text-p type)))
14094       (cond ((or (and (null arg) (eq hide 'hidden))
14095                  (and arg (< 0 (prefix-numeric-value arg))))
14096              (gnus-article-show-hidden-text type))
14097             ((eq hide 'shown)
14098              (gnus-article-show-hidden-text type t))
14099             (t nil)))))
14100
14101 (defun gnus-article-hidden-text-p (type)
14102   "Say whether the current buffer contains hidden text of type TYPE."
14103   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14104     (when pos
14105       (if (get-text-property pos 'invisible)
14106           'hidden
14107         'shown))))
14108
14109 (defun gnus-article-hide (&optional arg force)
14110   "Hide all the gruft in the current article.
14111 This means that PGP stuff, signatures, cited text and (some)
14112 headers will be hidden.
14113 If given a prefix, show the hidden text instead."
14114   (interactive (list current-prefix-arg 'force))
14115   (gnus-article-hide-headers arg)
14116   (gnus-article-hide-pgp arg)
14117   (gnus-article-hide-citation-maybe arg force)
14118   (gnus-article-hide-signature arg))
14119
14120 (defun gnus-article-show-hidden-text (type &optional hide)
14121   "Show all hidden text of type TYPE.
14122 If HIDE, hide the text instead."
14123   (save-excursion
14124     (set-buffer gnus-article-buffer)
14125     (let ((buffer-read-only nil)
14126           (inhibit-point-motion-hooks t)
14127           (beg (point-min)))
14128       (while (gnus-goto-char (text-property-any
14129                               beg (point-max) 'gnus-type type))
14130         (setq beg (point))
14131         (forward-char)
14132         (if hide
14133             (gnus-hide-text beg (point) gnus-hidden-properties)
14134           (gnus-unhide-text beg (point)))
14135         (setq beg (point)))
14136       t)))
14137
14138 (defvar gnus-article-time-units
14139   `((year . ,(* 365.25 24 60 60))
14140     (week . ,(* 7 24 60 60))
14141     (day . ,(* 24 60 60))
14142     (hour . ,(* 60 60))
14143     (minute . 60)
14144     (second . 1))
14145   "Mapping from time units to seconds.")
14146
14147 (defun gnus-article-date-ut (&optional type highlight)
14148   "Convert DATE date to universal time in the current article.
14149 If TYPE is `local', convert to local time; if it is `lapsed', output
14150 how much time has lapsed since DATE."
14151   (interactive (list 'ut t))
14152   (let* ((header (or gnus-current-headers
14153                      (gnus-summary-article-header) ""))
14154          (date (and (vectorp header) (mail-header-date header)))
14155          (date-regexp "^Date: \\|^X-Sent: ")
14156          (now (current-time))
14157          (inhibit-point-motion-hooks t)
14158          bface eface)
14159     (when (and date (not (string= date "")))
14160       (save-excursion
14161         (set-buffer gnus-article-buffer)
14162         (save-restriction
14163           (nnheader-narrow-to-headers)
14164           (let ((buffer-read-only nil))
14165             ;; Delete any old Date headers.
14166             (if (re-search-forward date-regexp nil t)
14167                 (progn
14168                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14169                         eface (get-text-property (1- (gnus-point-at-eol))
14170                                                  'face))
14171                   (message-remove-header date-regexp t)
14172                   (beginning-of-line))
14173               (goto-char (point-max)))
14174             (insert
14175              (cond
14176               ;; Convert to the local timezone.  We have to slap a
14177               ;; `condition-case' round the calls to the timezone
14178               ;; functions since they aren't particularly resistant to
14179               ;; buggy dates.
14180               ((eq type 'local)
14181                (concat "Date: " (condition-case ()
14182                                     (timezone-make-date-arpa-standard date)
14183                                   (error date))
14184                        "\n"))
14185               ;; Convert to Universal Time.
14186               ((eq type 'ut)
14187                (concat "Date: "
14188                        (condition-case ()
14189                            (timezone-make-date-arpa-standard date nil "UT")
14190                          (error date))
14191                        "\n"))
14192               ;; Get the original date from the article.
14193               ((eq type 'original)
14194                (concat "Date: " date "\n"))
14195               ;; Do an X-Sent lapsed format.
14196               ((eq type 'lapsed)
14197                ;; If the date is seriously mangled, the timezone
14198                ;; functions are liable to bug out, so we condition-case
14199                ;; the entire thing.
14200                (let* ((real-time
14201                        (condition-case ()
14202                            (gnus-time-minus
14203                             (gnus-encode-date
14204                              (timezone-make-date-arpa-standard
14205                               (current-time-string now)
14206                               (current-time-zone now) "UT"))
14207                             (gnus-encode-date
14208                              (timezone-make-date-arpa-standard
14209                               date nil "UT")))
14210                          (error '(0 0))))
14211                       (real-sec (+ (* (float (car real-time)) 65536)
14212                                    (cadr real-time)))
14213                       (sec (abs real-sec))
14214                       num prev)
14215                  (if (zerop sec)
14216                      "X-Sent: Now\n"
14217                    (concat
14218                     "X-Sent: "
14219                     ;; This is a bit convoluted, but basically we go
14220                     ;; through the time units for years, weeks, etc,
14221                     ;; and divide things to see whether that results
14222                     ;; in positive answers.
14223                     (mapconcat
14224                      (lambda (unit)
14225                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14226                            ;; The (remaining) seconds are too few to
14227                            ;; be divided into this time unit.
14228                            ""
14229                          ;; It's big enough, so we output it.
14230                          (setq sec (- sec (* num (cdr unit))))
14231                          (prog1
14232                              (concat (if prev ", " "") (int-to-string
14233                                                         (floor num))
14234                                      " " (symbol-name (car unit))
14235                                      (if (> num 1) "s" ""))
14236                            (setq prev t))))
14237                      gnus-article-time-units "")
14238                     ;; If dates are odd, then it might appear like the
14239                     ;; article was sent in the future.
14240                     (if (> real-sec 0)
14241                         " ago\n"
14242                       " in the future\n")))))
14243               (t
14244                (error "Unknown conversion type: %s" type))))
14245             ;; Do highlighting.
14246             (forward-line -1)
14247             (when (and (gnus-visual-p 'article-highlight 'highlight)
14248                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14249               (put-text-property (match-beginning 1) (match-end 1)
14250                                  'face bface)
14251               (put-text-property (match-beginning 2) (match-end 2)
14252                                  'face eface))))))))
14253
14254 (defun gnus-article-date-local (&optional highlight)
14255   "Convert the current article date to the local timezone."
14256   (interactive (list t))
14257   (gnus-article-date-ut 'local highlight))
14258
14259 (defun gnus-article-date-original (&optional highlight)
14260   "Convert the current article date to what it was originally.
14261 This is only useful if you have used some other date conversion
14262 function and want to see what the date was before converting."
14263   (interactive (list t))
14264   (gnus-article-date-ut 'original highlight))
14265
14266 (defun gnus-article-date-lapsed (&optional highlight)
14267   "Convert the current article date to time lapsed since it was sent."
14268   (interactive (list t))
14269   (gnus-article-date-ut 'lapsed highlight))
14270
14271 (defun gnus-article-maybe-highlight ()
14272   "Do some article highlighting if `gnus-visual' is non-nil."
14273   (if (gnus-visual-p 'article-highlight 'highlight)
14274       (gnus-article-highlight-some)))
14275
14276 ;; Article savers.
14277
14278 (defun gnus-output-to-rmail (file-name)
14279   "Append the current article to an Rmail file named FILE-NAME."
14280   (require 'rmail)
14281   ;; Most of these codes are borrowed from rmailout.el.
14282   (setq file-name (expand-file-name file-name))
14283   (setq rmail-default-rmail-file file-name)
14284   (let ((artbuf (current-buffer))
14285         (tmpbuf (get-buffer-create " *Gnus-output*")))
14286     (save-excursion
14287       (or (get-file-buffer file-name)
14288           (file-exists-p file-name)
14289           (if (gnus-yes-or-no-p
14290                (concat "\"" file-name "\" does not exist, create it? "))
14291               (let ((file-buffer (create-file-buffer file-name)))
14292                 (save-excursion
14293                   (set-buffer file-buffer)
14294                   (rmail-insert-rmail-file-header)
14295                   (let ((require-final-newline nil))
14296                     (write-region (point-min) (point-max) file-name t 1)))
14297                 (kill-buffer file-buffer))
14298             (error "Output file does not exist")))
14299       (set-buffer tmpbuf)
14300       (buffer-disable-undo (current-buffer))
14301       (erase-buffer)
14302       (insert-buffer-substring artbuf)
14303       (gnus-convert-article-to-rmail)
14304       ;; Decide whether to append to a file or to an Emacs buffer.
14305       (let ((outbuf (get-file-buffer file-name)))
14306         (if (not outbuf)
14307             (append-to-file (point-min) (point-max) file-name)
14308           ;; File has been visited, in buffer OUTBUF.
14309           (set-buffer outbuf)
14310           (let ((buffer-read-only nil)
14311                 (msg (and (boundp 'rmail-current-message)
14312                           (symbol-value 'rmail-current-message))))
14313             ;; If MSG is non-nil, buffer is in RMAIL mode.
14314             (if msg
14315                 (progn (widen)
14316                        (narrow-to-region (point-max) (point-max))))
14317             (insert-buffer-substring tmpbuf)
14318             (if msg
14319                 (progn
14320                   (goto-char (point-min))
14321                   (widen)
14322                   (search-backward "\^_")
14323                   (narrow-to-region (point) (point-max))
14324                   (goto-char (1+ (point-min)))
14325                   (rmail-count-new-messages t)
14326                   (rmail-show-message msg)))))))
14327     (kill-buffer tmpbuf)))
14328
14329 (defun gnus-output-to-file (file-name)
14330   "Append the current article to a file named FILE-NAME."
14331   (setq file-name (expand-file-name file-name))
14332   (let ((artbuf (current-buffer))
14333         (tmpbuf (get-buffer-create " *Gnus-output*")))
14334     (save-excursion
14335       (set-buffer tmpbuf)
14336       (buffer-disable-undo (current-buffer))
14337       (erase-buffer)
14338       (insert-buffer-substring artbuf)
14339       ;; Append newline at end of the buffer as separator, and then
14340       ;; save it to file.
14341       (goto-char (point-max))
14342       (insert "\n")
14343       (append-to-file (point-min) (point-max) file-name))
14344     (kill-buffer tmpbuf)))
14345
14346 (defun gnus-convert-article-to-rmail ()
14347   "Convert article in current buffer to Rmail message format."
14348   (let ((buffer-read-only nil))
14349     ;; Convert article directly into Babyl format.
14350     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14351     (goto-char (point-min))
14352     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14353     (while (search-forward "\n\^_" nil t) ;single char
14354       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14355     (goto-char (point-max))
14356     (insert "\^_")))
14357
14358 (defun gnus-narrow-to-page (&optional arg)
14359   "Narrow the article buffer to a page.
14360 If given a numerical ARG, move forward ARG pages."
14361   (interactive "P")
14362   (setq arg (if arg (prefix-numeric-value arg) 0))
14363   (save-excursion
14364     (set-buffer gnus-article-buffer)
14365     (goto-char (point-min))
14366     (widen)
14367     (when (gnus-visual-p 'page-marker)
14368       (let ((buffer-read-only nil))
14369         (gnus-remove-text-with-property 'gnus-prev)
14370         (gnus-remove-text-with-property 'gnus-next)))
14371     (when
14372         (cond ((< arg 0)
14373                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14374               ((> arg 0)
14375                (re-search-forward page-delimiter nil 'move arg)))
14376       (goto-char (match-end 0)))
14377     (narrow-to-region
14378      (point)
14379      (if (re-search-forward page-delimiter nil 'move)
14380          (match-beginning 0)
14381        (point)))
14382     (when (and (gnus-visual-p 'page-marker)
14383                (not (= (point-min) 1)))
14384       (save-excursion
14385         (goto-char (point-min))
14386         (gnus-insert-prev-page-button)))
14387     (when (and (gnus-visual-p 'page-marker)
14388                (not (= (1- (point-max)) (buffer-size))))
14389       (save-excursion
14390         (goto-char (point-max))
14391         (gnus-insert-next-page-button)))))
14392
14393 ;; Article mode commands
14394
14395 (defun gnus-article-goto-next-page ()
14396   "Show the next page of the article."
14397   (interactive)
14398   (when (gnus-article-next-page)
14399     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14400
14401 (defun gnus-article-goto-prev-page ()
14402   "Show the next page of the article."
14403   (interactive)
14404   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14405     (gnus-article-prev-page nil)))
14406
14407 (defun gnus-article-next-page (&optional lines)
14408   "Show the next page of the current article.
14409 If end of article, return non-nil.  Otherwise return nil.
14410 Argument LINES specifies lines to be scrolled up."
14411   (interactive "p")
14412   (move-to-window-line -1)
14413   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14414   (if (save-excursion
14415         (end-of-line)
14416         (and (pos-visible-in-window-p)  ;Not continuation line.
14417              (eobp)))
14418       ;; Nothing in this page.
14419       (if (or (not gnus-break-pages)
14420               (save-excursion
14421                 (save-restriction
14422                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14423           t                             ;Nothing more.
14424         (gnus-narrow-to-page 1)         ;Go to next page.
14425         nil)
14426     ;; More in this page.
14427     (condition-case ()
14428         (scroll-up lines)
14429       (end-of-buffer
14430        ;; Long lines may cause an end-of-buffer error.
14431        (goto-char (point-max))))
14432     (move-to-window-line 0)
14433     nil))
14434
14435 (defun gnus-article-prev-page (&optional lines)
14436   "Show previous page of current article.
14437 Argument LINES specifies lines to be scrolled down."
14438   (interactive "p")
14439   (move-to-window-line 0)
14440   (if (and gnus-break-pages
14441            (bobp)
14442            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14443       (progn
14444         (gnus-narrow-to-page -1)        ;Go to previous page.
14445         (goto-char (point-max))
14446         (recenter -1))
14447     (prog1
14448         (condition-case ()
14449             (scroll-down lines)
14450           (error nil))
14451       (move-to-window-line 0))))
14452
14453 (defun gnus-article-refer-article ()
14454   "Read article specified by message-id around point."
14455   (interactive)
14456   (let ((point (point)))
14457     (search-forward ">" nil t)          ;Move point to end of "<....>".
14458     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14459         (let ((message-id (match-string 1)))
14460           (goto-char point)
14461           (set-buffer gnus-summary-buffer)
14462           (gnus-summary-refer-article message-id))
14463       (goto-char (point))
14464       (error "No references around point"))))
14465
14466 (defun gnus-article-show-summary ()
14467   "Reconfigure windows to show summary buffer."
14468   (interactive)
14469   (gnus-configure-windows 'article)
14470   (gnus-summary-goto-subject gnus-current-article))
14471
14472 (defun gnus-article-describe-briefly ()
14473   "Describe article mode commands briefly."
14474   (interactive)
14475   (gnus-message 6
14476                 (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")))
14477
14478 (defun gnus-article-summary-command ()
14479   "Execute the last keystroke in the summary buffer."
14480   (interactive)
14481   (let ((obuf (current-buffer))
14482         (owin (current-window-configuration))
14483         func)
14484     (switch-to-buffer gnus-summary-buffer 'norecord)
14485     (setq func (lookup-key (current-local-map) (this-command-keys)))
14486     (call-interactively func)
14487     (set-buffer obuf)
14488     (set-window-configuration owin)
14489     (set-window-point (get-buffer-window (current-buffer)) (point))))
14490
14491 (defun gnus-article-summary-command-nosave ()
14492   "Execute the last keystroke in the summary buffer."
14493   (interactive)
14494   (let (func)
14495     (pop-to-buffer gnus-summary-buffer 'norecord)
14496     (setq func (lookup-key (current-local-map) (this-command-keys)))
14497     (call-interactively func)))
14498
14499 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14500   "Read a summary buffer key sequence and execute it from the article buffer."
14501   (interactive "P")
14502   (let ((nosaves
14503          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14504            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14505            "=" "^" "\M-^"))
14506         keys)
14507     (save-excursion
14508       (set-buffer gnus-summary-buffer)
14509       (push (or key last-command-event) unread-command-events)
14510       (setq keys (read-key-sequence nil)))
14511     (message "")
14512
14513     (if (member keys nosaves)
14514         (let (func)
14515           (pop-to-buffer gnus-summary-buffer 'norecord)
14516           (if (setq func (lookup-key (current-local-map) keys))
14517               (call-interactively func)
14518             (ding)))
14519       (let ((obuf (current-buffer))
14520             (owin (current-window-configuration))
14521             (opoint (point))
14522             func in-buffer)
14523         (if not-restore-window
14524             (pop-to-buffer gnus-summary-buffer 'norecord)
14525           (switch-to-buffer gnus-summary-buffer 'norecord))
14526         (setq in-buffer (current-buffer))
14527         (if (setq func (lookup-key (current-local-map) keys))
14528             (call-interactively func)
14529           (ding))
14530         (when (eq in-buffer (current-buffer))
14531           (set-buffer obuf)
14532           (unless not-restore-window
14533             (set-window-configuration owin))
14534           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14535
14536 \f
14537 ;;;
14538 ;;; Kill file handling.
14539 ;;;
14540
14541 ;;;###autoload
14542 (defalias 'gnus-batch-kill 'gnus-batch-score)
14543 ;;;###autoload
14544 (defun gnus-batch-score ()
14545   "Run batched scoring.
14546 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14547 Newsgroups is a list of strings in Bnews format.  If you want to score
14548 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14549 score the alt hierarchy, you'd say \"!alt.all\"."
14550   (interactive)
14551   (let* ((yes-and-no
14552           (gnus-newsrc-parse-options
14553            (apply (function concat)
14554                   (mapcar (lambda (g) (concat g " "))
14555                           command-line-args-left))))
14556          (gnus-expert-user t)
14557          (nnmail-spool-file nil)
14558          (gnus-use-dribble-file nil)
14559          (yes (car yes-and-no))
14560          (no (cdr yes-and-no))
14561          group newsrc entry
14562          ;; Disable verbose message.
14563          gnus-novice-user gnus-large-newsgroup)
14564     ;; Eat all arguments.
14565     (setq command-line-args-left nil)
14566     ;; Start Gnus.
14567     (gnus)
14568     ;; Apply kills to specified newsgroups in command line arguments.
14569     (setq newsrc (cdr gnus-newsrc-alist))
14570     (while newsrc
14571       (setq group (caar newsrc))
14572       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14573       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14574                (and (car entry)
14575                     (or (eq (car entry) t)
14576                         (not (zerop (car entry)))))
14577                (if yes (string-match yes group) t)
14578                (or (null no) (not (string-match no group))))
14579           (progn
14580             (gnus-summary-read-group group nil t nil t)
14581             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14582                  (gnus-summary-exit))))
14583       (setq newsrc (cdr newsrc)))
14584     ;; Exit Emacs.
14585     (switch-to-buffer gnus-group-buffer)
14586     (gnus-group-save-newsrc)))
14587
14588 (defun gnus-apply-kill-file ()
14589   "Apply a kill file to the current newsgroup.
14590 Returns the number of articles marked as read."
14591   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14592           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14593       (gnus-apply-kill-file-internal)
14594     0))
14595
14596 (defun gnus-kill-save-kill-buffer ()
14597   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14598     (when (get-file-buffer file)
14599       (save-excursion
14600         (set-buffer (get-file-buffer file))
14601         (and (buffer-modified-p) (save-buffer))
14602         (kill-buffer (current-buffer))))))
14603
14604 (defvar gnus-kill-file-name "KILL"
14605   "Suffix of the kill files.")
14606
14607 (defun gnus-newsgroup-kill-file (newsgroup)
14608   "Return the name of a kill file name for NEWSGROUP.
14609 If NEWSGROUP is nil, return the global kill file name instead."
14610   (cond 
14611    ;; The global KILL file is placed at top of the directory.
14612    ((or (null newsgroup)
14613         (string-equal newsgroup ""))
14614     (expand-file-name gnus-kill-file-name
14615                       (or gnus-kill-files-directory "~/News")))
14616    ;; Append ".KILL" to newsgroup name.
14617    ((gnus-use-long-file-name 'not-kill)
14618     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14619                               "." gnus-kill-file-name)
14620                       (or gnus-kill-files-directory "~/News")))
14621    ;; Place "KILL" under the hierarchical directory.
14622    (t
14623     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14624                               "/" gnus-kill-file-name)
14625                       (or gnus-kill-files-directory "~/News")))))
14626
14627 \f
14628 ;;;
14629 ;;; Dribble file
14630 ;;;
14631
14632 (defvar gnus-dribble-ignore nil)
14633 (defvar gnus-dribble-eval-file nil)
14634
14635 (defun gnus-dribble-file-name ()
14636   "Return the dribble file for the current .newsrc."
14637   (concat
14638    (if gnus-dribble-directory
14639        (concat (file-name-as-directory gnus-dribble-directory)
14640                (file-name-nondirectory gnus-current-startup-file))
14641      gnus-current-startup-file)
14642    "-dribble"))
14643
14644 (defun gnus-dribble-enter (string)
14645   "Enter STRING into the dribble buffer."
14646   (if (and (not gnus-dribble-ignore)
14647            gnus-dribble-buffer
14648            (buffer-name gnus-dribble-buffer))
14649       (let ((obuf (current-buffer)))
14650         (set-buffer gnus-dribble-buffer)
14651         (insert string "\n")
14652         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14653         (set-buffer obuf))))
14654
14655 (defun gnus-dribble-read-file ()
14656   "Read the dribble file from disk."
14657   (let ((dribble-file (gnus-dribble-file-name)))
14658     (save-excursion
14659       (set-buffer (setq gnus-dribble-buffer
14660                         (get-buffer-create
14661                          (file-name-nondirectory dribble-file))))
14662       (gnus-add-current-to-buffer-list)
14663       (erase-buffer)
14664       (setq buffer-file-name dribble-file)
14665       (auto-save-mode t)
14666       (buffer-disable-undo (current-buffer))
14667       (bury-buffer (current-buffer))
14668       (set-buffer-modified-p nil)
14669       (let ((auto (make-auto-save-file-name))
14670             (gnus-dribble-ignore t))
14671         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14672           ;; Load whichever file is newest -- the auto save file
14673           ;; or the "real" file.
14674           (if (file-newer-than-file-p auto dribble-file)
14675               (insert-file-contents auto)
14676             (insert-file-contents dribble-file))
14677           (unless (zerop (buffer-size))
14678             (set-buffer-modified-p t))
14679           ;; Set the file modes to reflect the .newsrc file modes.
14680           (save-buffer)
14681           (when (file-exists-p gnus-current-startup-file)
14682             (set-file-modes dribble-file
14683                             (file-modes gnus-current-startup-file)))
14684           ;; Possibly eval the file later.
14685           (when (gnus-y-or-n-p
14686                  "Auto-save file exists.  Do you want to read it? ")
14687             (setq gnus-dribble-eval-file t)))))))
14688
14689 (defun gnus-dribble-eval-file ()
14690   (if (not gnus-dribble-eval-file)
14691       ()
14692     (setq gnus-dribble-eval-file nil)
14693     (save-excursion
14694       (let ((gnus-dribble-ignore t))
14695         (set-buffer gnus-dribble-buffer)
14696         (eval-buffer (current-buffer))))))
14697
14698 (defun gnus-dribble-delete-file ()
14699   (if (file-exists-p (gnus-dribble-file-name))
14700       (delete-file (gnus-dribble-file-name)))
14701   (if gnus-dribble-buffer
14702       (save-excursion
14703         (set-buffer gnus-dribble-buffer)
14704         (let ((auto (make-auto-save-file-name)))
14705           (if (file-exists-p auto)
14706               (delete-file auto))
14707           (erase-buffer)
14708           (set-buffer-modified-p nil)))))
14709
14710 (defun gnus-dribble-save ()
14711   (if (and gnus-dribble-buffer
14712            (buffer-name gnus-dribble-buffer))
14713       (save-excursion
14714         (set-buffer gnus-dribble-buffer)
14715         (save-buffer))))
14716
14717 (defun gnus-dribble-clear ()
14718   (save-excursion
14719     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14720         (progn
14721           (set-buffer gnus-dribble-buffer)
14722           (erase-buffer)
14723           (set-buffer-modified-p nil)
14724           (setq buffer-saved-size (buffer-size))))))
14725
14726 \f
14727 ;;;
14728 ;;; Server Communication
14729 ;;;
14730
14731 (defun gnus-start-news-server (&optional confirm)
14732   "Open a method for getting news.
14733 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14734   (let (how)
14735     (if gnus-current-select-method
14736         ;; Stream is already opened.
14737         nil
14738       ;; Open NNTP server.
14739       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14740       (if confirm
14741           (progn
14742             ;; Read server name with completion.
14743             (setq gnus-nntp-server
14744                   (completing-read "NNTP server: "
14745                                    (mapcar (lambda (server) (list server))
14746                                            (cons (list gnus-nntp-server)
14747                                                  gnus-secondary-servers))
14748                                    nil nil gnus-nntp-server))))
14749
14750       (if (and gnus-nntp-server
14751                (stringp gnus-nntp-server)
14752                (not (string= gnus-nntp-server "")))
14753           (setq gnus-select-method
14754                 (cond ((or (string= gnus-nntp-server "")
14755                            (string= gnus-nntp-server "::"))
14756                        (list 'nnspool (system-name)))
14757                       ((string-match "^:" gnus-nntp-server)
14758                        (list 'nnmh gnus-nntp-server
14759                              (list 'nnmh-directory
14760                                    (file-name-as-directory
14761                                     (expand-file-name
14762                                      (concat "~/" (substring
14763                                                    gnus-nntp-server 1)))))
14764                              (list 'nnmh-get-new-mail nil)))
14765                       (t
14766                        (list 'nntp gnus-nntp-server)))))
14767
14768       (setq how (car gnus-select-method))
14769       (cond ((eq how 'nnspool)
14770              (require 'nnspool)
14771              (gnus-message 5 "Looking up local news spool..."))
14772             ((eq how 'nnmh)
14773              (require 'nnmh)
14774              (gnus-message 5 "Looking up mh spool..."))
14775             (t
14776              (require 'nntp)))
14777       (setq gnus-current-select-method gnus-select-method)
14778       (run-hooks 'gnus-open-server-hook)
14779       (or
14780        ;; gnus-open-server-hook might have opened it
14781        (gnus-server-opened gnus-select-method)
14782        (gnus-open-server gnus-select-method)
14783        (gnus-y-or-n-p
14784         (format
14785          "%s (%s) open error: '%s'.     Continue? "
14786          (car gnus-select-method) (cadr gnus-select-method)
14787          (gnus-status-message gnus-select-method)))
14788        (gnus-error 1 "Couldn't open server on %s"
14789                    (nth 1 gnus-select-method))))))
14790
14791 (defun gnus-check-group (group)
14792   "Try to make sure that the server where GROUP exists is alive."
14793   (let ((method (gnus-find-method-for-group group)))
14794     (or (gnus-server-opened method)
14795         (gnus-open-server method))))
14796
14797 (defun gnus-check-server (&optional method silent)
14798   "Check whether the connection to METHOD is down.
14799 If METHOD is nil, use `gnus-select-method'.
14800 If it is down, start it up (again)."
14801   (let ((method (or method gnus-select-method)))
14802     ;; Transform virtual server names into select methods.
14803     (when (stringp method)
14804       (setq method (gnus-server-to-method method)))
14805     (if (gnus-server-opened method)
14806         ;; The stream is already opened.
14807         t
14808       ;; Open the server.
14809       (unless silent
14810         (gnus-message 5 "Opening %s server%s..." (car method)
14811                       (if (equal (nth 1 method) "") ""
14812                         (format " on %s" (nth 1 method)))))
14813       (run-hooks 'gnus-open-server-hook)
14814       (prog1
14815           (gnus-open-server method)
14816         (unless silent
14817           (message ""))))))
14818
14819 (defun gnus-get-function (method function)
14820   "Return a function symbol based on METHOD and FUNCTION."
14821   ;; Translate server names into methods.
14822   (unless method
14823     (error "Attempted use of a nil select method"))
14824   (when (stringp method)
14825     (setq method (gnus-server-to-method method)))
14826   (let ((func (intern (format "%s-%s" (car method) function))))
14827     ;; If the functions isn't bound, we require the backend in
14828     ;; question.
14829     (unless (fboundp func)
14830       (require (car method))
14831       (unless (fboundp func)
14832         ;; This backend doesn't implement this function.
14833         (error "No such function: %s" func)))
14834     func))
14835
14836 \f
14837 ;;;
14838 ;;; Interface functions to the backends.
14839 ;;;
14840
14841 (defun gnus-open-server (method)
14842   "Open a connection to METHOD."
14843   (when (stringp method)
14844     (setq method (gnus-server-to-method method)))
14845   (let ((elem (assoc method gnus-opened-servers)))
14846     ;; If this method was previously denied, we just return nil.
14847     (if (eq (nth 1 elem) 'denied)
14848         (progn
14849           (gnus-message 1 "Denied server")
14850           nil)
14851       ;; Open the server.
14852       (let ((result
14853              (funcall (gnus-get-function method 'open-server)
14854                       (nth 1 method) (nthcdr 2 method))))
14855         ;; If this hasn't been opened before, we add it to the list.
14856         (unless elem
14857           (setq elem (list method nil)
14858                 gnus-opened-servers (cons elem gnus-opened-servers)))
14859         ;; Set the status of this server.
14860         (setcar (cdr elem) (if result 'ok 'denied))
14861         ;; Return the result from the "open" call.
14862         result))))
14863
14864 (defun gnus-close-server (method)
14865   "Close the connection to METHOD."
14866   (when (stringp method)
14867     (setq method (gnus-server-to-method method)))
14868   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14869
14870 (defun gnus-request-list (method)
14871   "Request the active file from METHOD."
14872   (when (stringp method)
14873     (setq method (gnus-server-to-method method)))
14874   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14875
14876 (defun gnus-request-list-newsgroups (method)
14877   "Request the newsgroups file from METHOD."
14878   (when (stringp method)
14879     (setq method (gnus-server-to-method method)))
14880   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14881
14882 (defun gnus-request-newgroups (date method)
14883   "Request all new groups since DATE from METHOD."
14884   (when (stringp method)
14885     (setq method (gnus-server-to-method method)))
14886   (funcall (gnus-get-function method 'request-newgroups)
14887            date (nth 1 method)))
14888
14889 (defun gnus-server-opened (method)
14890   "Check whether a connection to METHOD has been opened."
14891   (when (stringp method)
14892     (setq method (gnus-server-to-method method)))
14893   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14894
14895 (defun gnus-status-message (method)
14896   "Return the status message from METHOD.
14897 If METHOD is a string, it is interpreted as a group name.   The method
14898 this group uses will be queried."
14899   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14900                   method)))
14901     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14902
14903 (defun gnus-request-group (group &optional dont-check method)
14904   "Request GROUP.  If DONT-CHECK, no information is required."
14905   (let ((method (or method (gnus-find-method-for-group group))))
14906     (when (stringp method)
14907       (setq method (gnus-server-to-method method)))
14908     (funcall (gnus-get-function method 'request-group)
14909              (gnus-group-real-name group) (nth 1 method) dont-check)))
14910
14911 (defun gnus-request-asynchronous (group &optional articles)
14912   "Request that GROUP behave asynchronously.
14913 ARTICLES is the `data' of the group."
14914   (let ((method (gnus-find-method-for-group group)))
14915     (funcall (gnus-get-function method 'request-asynchronous)
14916              (gnus-group-real-name group) (nth 1 method) articles)))
14917
14918 (defun gnus-list-active-group (group)
14919   "Request active information on GROUP."
14920   (let ((method (gnus-find-method-for-group group))
14921         (func 'list-active-group))
14922     (when (gnus-check-backend-function func group)
14923       (funcall (gnus-get-function method func)
14924                (gnus-group-real-name group) (nth 1 method)))))
14925
14926 (defun gnus-request-group-description (group)
14927   "Request a description of GROUP."
14928   (let ((method (gnus-find-method-for-group group))
14929         (func 'request-group-description))
14930     (when (gnus-check-backend-function func group)
14931       (funcall (gnus-get-function method func)
14932                (gnus-group-real-name group) (nth 1 method)))))
14933
14934 (defun gnus-close-group (group)
14935   "Request the GROUP be closed."
14936   (let ((method (gnus-find-method-for-group group)))
14937     (funcall (gnus-get-function method 'close-group)
14938              (gnus-group-real-name group) (nth 1 method))))
14939
14940 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14941   "Request headers for ARTICLES in GROUP.
14942 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14943   (let ((method (gnus-find-method-for-group group)))
14944     (if (and gnus-use-cache (numberp (car articles)))
14945         (gnus-cache-retrieve-headers articles group fetch-old)
14946       (funcall (gnus-get-function method 'retrieve-headers)
14947                articles (gnus-group-real-name group) (nth 1 method)
14948                fetch-old))))
14949
14950 (defun gnus-retrieve-groups (groups method)
14951   "Request active information on GROUPS from METHOD."
14952   (when (stringp method)
14953     (setq method (gnus-server-to-method method)))
14954   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14955
14956 (defun gnus-request-type (group &optional article)
14957   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14958   (let ((method (gnus-find-method-for-group group)))
14959     (if (not (gnus-check-backend-function 'request-type (car method)))
14960         'unknown
14961       (funcall (gnus-get-function method 'request-type)
14962                (gnus-group-real-name group) article))))
14963
14964 (defun gnus-request-update-mark (group article mark)
14965   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14966   (let ((method (gnus-find-method-for-group group)))
14967     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14968         mark
14969       (funcall (gnus-get-function method 'request-update-mark)
14970                (gnus-group-real-name group) article mark))))
14971
14972 (defun gnus-request-article (article group &optional buffer)
14973   "Request the ARTICLE in GROUP.
14974 ARTICLE can either be an article number or an article Message-ID.
14975 If BUFFER, insert the article in that group."
14976   (let ((method (gnus-find-method-for-group group)))
14977     (funcall (gnus-get-function method 'request-article)
14978              article (gnus-group-real-name group) (nth 1 method) buffer)))
14979
14980 (defun gnus-request-head (article group)
14981   "Request the head of ARTICLE in GROUP."
14982   (let ((method (gnus-find-method-for-group group)))
14983     (funcall (gnus-get-function method 'request-head)
14984              article (gnus-group-real-name group) (nth 1 method))))
14985
14986 (defun gnus-request-body (article group)
14987   "Request the body of ARTICLE in GROUP."
14988   (let ((method (gnus-find-method-for-group group)))
14989     (funcall (gnus-get-function method 'request-body)
14990              article (gnus-group-real-name group) (nth 1 method))))
14991
14992 (defun gnus-request-post (method)
14993   "Post the current buffer using METHOD."
14994   (when (stringp method)
14995     (setq method (gnus-server-to-method method)))
14996   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14997
14998 (defun gnus-request-scan (group method)
14999   "Request a SCAN being performed in GROUP from METHOD.
15000 If GROUP is nil, all groups on METHOD are scanned."
15001   (let ((method (if group (gnus-find-method-for-group group) method)))
15002     (funcall (gnus-get-function method 'request-scan)
15003              (and group (gnus-group-real-name group)) (nth 1 method))))
15004
15005 (defsubst gnus-request-update-info (info method)
15006   "Request that METHOD update INFO."
15007   (when (stringp method)
15008     (setq method (gnus-server-to-method method)))
15009   (when (gnus-check-backend-function 'request-update-info (car method))
15010     (funcall (gnus-get-function method 'request-update-info)
15011              (gnus-group-real-name (gnus-info-group info))
15012              info (nth 1 method))))
15013
15014 (defun gnus-request-expire-articles (articles group &optional force)
15015   (let ((method (gnus-find-method-for-group group)))
15016     (funcall (gnus-get-function method 'request-expire-articles)
15017              articles (gnus-group-real-name group) (nth 1 method)
15018              force)))
15019
15020 (defun gnus-request-move-article
15021   (article group server accept-function &optional last)
15022   (let ((method (gnus-find-method-for-group group)))
15023     (funcall (gnus-get-function method 'request-move-article)
15024              article (gnus-group-real-name group)
15025              (nth 1 method) accept-function last)))
15026
15027 (defun gnus-request-accept-article (group method &optional last)
15028   ;; Make sure there's a newline at the end of the article.
15029   (when (stringp method)
15030     (setq method (gnus-server-to-method method)))
15031   (when (and (not method)
15032              (stringp group))
15033     (setq method (gnus-find-method-for-group group)))
15034   (goto-char (point-max))
15035   (unless (bolp)
15036     (insert "\n"))
15037   (let ((func (car (or method (gnus-find-method-for-group group)))))
15038     (funcall (intern (format "%s-request-accept-article" func))
15039              (if (stringp group) (gnus-group-real-name group) group)
15040              (cadr method)
15041              last)))
15042
15043 (defun gnus-request-replace-article (article group buffer)
15044   (let ((func (car (gnus-find-method-for-group group))))
15045     (funcall (intern (format "%s-request-replace-article" func))
15046              article (gnus-group-real-name group) buffer)))
15047
15048 (defun gnus-request-associate-buffer (group)
15049   (let ((method (gnus-find-method-for-group group)))
15050     (funcall (gnus-get-function method 'request-associate-buffer)
15051              (gnus-group-real-name group))))
15052
15053 (defun gnus-request-restore-buffer (article group)
15054   "Request a new buffer restored to the state of ARTICLE."
15055   (let ((method (gnus-find-method-for-group group)))
15056     (funcall (gnus-get-function method 'request-restore-buffer)
15057              article (gnus-group-real-name group) (nth 1 method))))
15058
15059 (defun gnus-request-create-group (group &optional method)
15060   (when (stringp method)
15061     (setq method (gnus-server-to-method method)))
15062   (let ((method (or method (gnus-find-method-for-group group))))
15063     (funcall (gnus-get-function method 'request-create-group)
15064              (gnus-group-real-name group) (nth 1 method))))
15065
15066 (defun gnus-request-delete-group (group &optional force)
15067   (let ((method (gnus-find-method-for-group group)))
15068     (funcall (gnus-get-function method 'request-delete-group)
15069              (gnus-group-real-name group) force (nth 1 method))))
15070
15071 (defun gnus-request-rename-group (group new-name)
15072   (let ((method (gnus-find-method-for-group group)))
15073     (funcall (gnus-get-function method 'request-rename-group)
15074              (gnus-group-real-name group)
15075              (gnus-group-real-name new-name) (nth 1 method))))
15076
15077 (defun gnus-member-of-valid (symbol group)
15078   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15079   (memq symbol (assoc
15080                 (symbol-name (car (gnus-find-method-for-group group)))
15081                 gnus-valid-select-methods)))
15082
15083 (defun gnus-method-option-p (method option)
15084   "Return non-nil if select METHOD has OPTION as a parameter."
15085   (when (stringp method)
15086     (setq method (gnus-server-to-method method)))
15087   (memq option (assoc (format "%s" (car method))
15088                       gnus-valid-select-methods)))
15089
15090 (defun gnus-server-extend-method (group method)
15091   ;; This function "extends" a virtual server.  If the server is
15092   ;; "hello", and the select method is ("hello" (my-var "something"))
15093   ;; in the group "alt.alt", this will result in a new virtual server
15094   ;; called "hello+alt.alt".
15095   (let ((entry
15096          (gnus-copy-sequence
15097           (if (equal (car method) "native") gnus-select-method
15098             (cdr (assoc (car method) gnus-server-alist))))))
15099     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15100     (nconc entry (cdr method))))
15101
15102 (defun gnus-find-method-for-group (group &optional info)
15103   "Find the select method that GROUP uses."
15104   (or gnus-override-method
15105       (and (not group)
15106            gnus-select-method)
15107       (let ((info (or info (gnus-get-info group)))
15108             method)
15109         (if (or (not info)
15110                 (not (setq method (gnus-info-method info)))
15111                 (equal method "native"))
15112             gnus-select-method
15113           (setq method
15114                 (cond ((stringp method)
15115                        (gnus-server-to-method method))
15116                       ((stringp (car method))
15117                        (gnus-server-extend-method group method))
15118                       (t
15119                        method)))
15120           (cond ((equal (cadr method) "")
15121                  method)
15122                 ((null (cadr method))
15123                  (list (car method) ""))
15124                 (t
15125                  (gnus-server-add-address method)))))))
15126
15127 (defun gnus-check-backend-function (func group)
15128   "Check whether GROUP supports function FUNC."
15129   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15130                   group)))
15131     (fboundp (intern (format "%s-%s" method func)))))
15132
15133 (defun gnus-methods-using (feature)
15134   "Find all methods that have FEATURE."
15135   (let ((valids gnus-valid-select-methods)
15136         outs)
15137     (while valids
15138       (if (memq feature (car valids))
15139           (setq outs (cons (car valids) outs)))
15140       (setq valids (cdr valids)))
15141     outs))
15142
15143 \f
15144 ;;;
15145 ;;; Active & Newsrc File Handling
15146 ;;;
15147
15148 (defun gnus-setup-news (&optional rawfile level dont-connect)
15149   "Setup news information.
15150 If RAWFILE is non-nil, the .newsrc file will also be read.
15151 If LEVEL is non-nil, the news will be set up at level LEVEL."
15152   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15153
15154     (when init 
15155       ;; Clear some variables to re-initialize news information.
15156       (setq gnus-newsrc-alist nil
15157             gnus-active-hashtb nil)
15158       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15159       (gnus-read-newsrc-file rawfile))
15160
15161     (when (and (not (assoc "archive" gnus-server-alist))
15162                gnus-message-archive-method)
15163       (push (cons "archive" gnus-message-archive-method)
15164             gnus-server-alist))
15165
15166     ;; If we don't read the complete active file, we fill in the
15167     ;; hashtb here.
15168     (if (or (null gnus-read-active-file)
15169             (eq gnus-read-active-file 'some))
15170         (gnus-update-active-hashtb-from-killed))
15171
15172     ;; Read the active file and create `gnus-active-hashtb'.
15173     ;; If `gnus-read-active-file' is nil, then we just create an empty
15174     ;; hash table.  The partial filling out of the hash table will be
15175     ;; done in `gnus-get-unread-articles'.
15176     (and gnus-read-active-file
15177          (not level)
15178          (gnus-read-active-file))
15179
15180     (or gnus-active-hashtb
15181         (setq gnus-active-hashtb (make-vector 4095 0)))
15182
15183     ;; Initialize the cache.
15184     (when gnus-use-cache
15185       (gnus-cache-open))
15186
15187     ;; Possibly eval the dribble file.
15188     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15189
15190     (gnus-update-format-specifications)
15191
15192     ;; See whether we need to read the description file.
15193     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15194              (not gnus-description-hashtb)
15195              (not dont-connect)
15196              gnus-read-active-file)
15197         (gnus-read-all-descriptions-files))
15198
15199     ;; Find new newsgroups and treat them.
15200     (if (and init gnus-check-new-newsgroups (not level)
15201              (gnus-check-server gnus-select-method))
15202         (gnus-find-new-newsgroups))
15203
15204     ;; We might read in new NoCeM messages here.
15205     (when (and gnus-use-nocem 
15206                (not level)
15207                (not dont-connect))
15208       (gnus-nocem-scan-groups))
15209
15210     ;; Find the number of unread articles in each non-dead group.
15211     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15212       (gnus-get-unread-articles level))
15213
15214     (if (and init gnus-check-bogus-newsgroups
15215              gnus-read-active-file (not level)
15216              (gnus-server-opened gnus-select-method))
15217         (gnus-check-bogus-newsgroups))))
15218
15219 (defun gnus-find-new-newsgroups (&optional arg)
15220   "Search for new newsgroups and add them.
15221 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15222 The `-n' option line from .newsrc is respected.
15223 If ARG (the prefix), use the `ask-server' method to query
15224 the server for new groups."
15225   (interactive "P")
15226   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15227                        (null gnus-read-active-file)
15228                        (eq gnus-read-active-file 'some))
15229                    'ask-server gnus-check-new-newsgroups)))
15230     (unless (gnus-check-first-time-used)
15231       (if (or (consp check)
15232               (eq check 'ask-server))
15233           ;; Ask the server for new groups.
15234           (gnus-ask-server-for-new-groups)
15235         ;; Go through the active hashtb and look for new groups.
15236         (let ((groups 0)
15237               group new-newsgroups)
15238           (gnus-message 5 "Looking for new newsgroups...")
15239           (unless gnus-have-read-active-file
15240             (gnus-read-active-file))
15241           (setq gnus-newsrc-last-checked-date (current-time-string))
15242           (unless gnus-killed-hashtb
15243             (gnus-make-hashtable-from-killed))
15244           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15245           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15246           (mapatoms
15247            (lambda (sym)
15248              (if (or (null (setq group (symbol-name sym)))
15249                      (not (boundp sym))
15250                      (null (symbol-value sym))
15251                      (gnus-gethash group gnus-killed-hashtb)
15252                      (gnus-gethash group gnus-newsrc-hashtb))
15253                  ()
15254                (let ((do-sub (gnus-matches-options-n group)))
15255                  (cond
15256                   ((eq do-sub 'subscribe)
15257                    (setq groups (1+ groups))
15258                    (gnus-sethash group group gnus-killed-hashtb)
15259                    (funcall gnus-subscribe-options-newsgroup-method group))
15260                   ((eq do-sub 'ignore)
15261                    nil)
15262                   (t
15263                    (setq groups (1+ groups))
15264                    (gnus-sethash group group gnus-killed-hashtb)
15265                    (if gnus-subscribe-hierarchical-interactive
15266                        (setq new-newsgroups (cons group new-newsgroups))
15267                      (funcall gnus-subscribe-newsgroup-method group)))))))
15268            gnus-active-hashtb)
15269           (when new-newsgroups
15270             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15271           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15272           (if (> groups 0)
15273               (gnus-message 6 "%d new newsgroup%s arrived."
15274                             groups (if (> groups 1) "s have" " has"))
15275             (gnus-message 6 "No new newsgroups.")))))))
15276
15277 (defun gnus-matches-options-n (group)
15278   ;; Returns `subscribe' if the group is to be unconditionally
15279   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15280   ;; no match for the group.
15281
15282   ;; First we check the two user variables.
15283   (cond
15284    ((and gnus-options-subscribe
15285          (string-match gnus-options-subscribe group))
15286     'subscribe)
15287    ((and gnus-auto-subscribed-groups
15288          (string-match gnus-auto-subscribed-groups group))
15289     'subscribe)
15290    ((and gnus-options-not-subscribe
15291          (string-match gnus-options-not-subscribe group))
15292     'ignore)
15293    ;; Then we go through the list that was retrieved from the .newsrc
15294    ;; file.  This list has elements on the form
15295    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15296    ;; is in the reverse order of the options line) is returned.
15297    (t
15298     (let ((regs gnus-newsrc-options-n))
15299       (while (and regs
15300                   (not (string-match (caar regs) group)))
15301         (setq regs (cdr regs)))
15302       (and regs (cdar regs))))))
15303
15304 (defun gnus-ask-server-for-new-groups ()
15305   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15306          (methods (cons gnus-select-method
15307                         (nconc
15308                          (when gnus-message-archive-method
15309                            (list "archive"))
15310                          (append
15311                           (and (consp gnus-check-new-newsgroups)
15312                                gnus-check-new-newsgroups)
15313                           gnus-secondary-select-methods))))
15314          (groups 0)
15315          (new-date (current-time-string))
15316          group new-newsgroups got-new method hashtb
15317          gnus-override-subscribe-method)
15318     ;; Go through both primary and secondary select methods and
15319     ;; request new newsgroups.
15320     (while (setq method (gnus-server-get-method nil (pop methods)))
15321       (setq new-newsgroups nil)
15322       (setq gnus-override-subscribe-method method)
15323       (when (and (gnus-check-server method)
15324                  (gnus-request-newgroups date method))
15325         (save-excursion
15326           (setq got-new t)
15327           (setq hashtb (gnus-make-hashtable 100))
15328           (set-buffer nntp-server-buffer)
15329           ;; Enter all the new groups into a hashtable.
15330           (gnus-active-to-gnus-format method hashtb 'ignore))
15331         ;; Now all new groups from `method' are in `hashtb'.
15332         (mapatoms
15333          (lambda (group-sym)
15334            (if (or (null (setq group (symbol-name group-sym)))
15335                    (not (boundp group-sym))
15336                    (null (symbol-value group-sym))
15337                    (gnus-gethash group gnus-newsrc-hashtb)
15338                    (member group gnus-zombie-list)
15339                    (member group gnus-killed-list))
15340                ;; The group is already known.
15341                ()
15342              ;; Make this group active.
15343              (when (symbol-value group-sym)
15344                (gnus-set-active group (symbol-value group-sym)))
15345              ;; Check whether we want it or not.
15346              (let ((do-sub (gnus-matches-options-n group)))
15347                (cond
15348                 ((eq do-sub 'subscribe)
15349                  (incf groups)
15350                  (gnus-sethash group group gnus-killed-hashtb)
15351                  (funcall gnus-subscribe-options-newsgroup-method group))
15352                 ((eq do-sub 'ignore)
15353                  nil)
15354                 (t
15355                  (incf groups)
15356                  (gnus-sethash group group gnus-killed-hashtb)
15357                  (if gnus-subscribe-hierarchical-interactive
15358                      (push group new-newsgroups)
15359                    (funcall gnus-subscribe-newsgroup-method group)))))))
15360          hashtb))
15361       (when new-newsgroups
15362         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15363     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15364     (when (> groups 0)
15365       (gnus-message 6 "%d new newsgroup%s arrived."
15366                     groups (if (> groups 1) "s have" " has")))
15367     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15368     got-new))
15369
15370 (defun gnus-check-first-time-used ()
15371   (if (or (> (length gnus-newsrc-alist) 1)
15372           (file-exists-p gnus-startup-file)
15373           (file-exists-p (concat gnus-startup-file ".el"))
15374           (file-exists-p (concat gnus-startup-file ".eld")))
15375       nil
15376     (gnus-message 6 "First time user; subscribing you to default groups")
15377     (unless (gnus-read-active-file-p)
15378       (gnus-read-active-file))
15379     (setq gnus-newsrc-last-checked-date (current-time-string))
15380     (let ((groups gnus-default-subscribed-newsgroups)
15381           group)
15382       (if (eq groups t)
15383           nil
15384         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15385         (mapatoms
15386          (lambda (sym)
15387            (if (null (setq group (symbol-name sym)))
15388                ()
15389              (let ((do-sub (gnus-matches-options-n group)))
15390                (cond
15391                 ((eq do-sub 'subscribe)
15392                  (gnus-sethash group group gnus-killed-hashtb)
15393                  (funcall gnus-subscribe-options-newsgroup-method group))
15394                 ((eq do-sub 'ignore)
15395                  nil)
15396                 (t
15397                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15398          gnus-active-hashtb)
15399         (while groups
15400           (if (gnus-active (car groups))
15401               (gnus-group-change-level
15402                (car groups) gnus-level-default-subscribed gnus-level-killed))
15403           (setq groups (cdr groups)))
15404         (gnus-group-make-help-group)
15405         (and gnus-novice-user
15406              (gnus-message 7 "`A k' to list killed groups"))))))
15407
15408 (defun gnus-subscribe-group (group previous &optional method)
15409   (gnus-group-change-level
15410    (if method
15411        (list t group gnus-level-default-subscribed nil nil method)
15412      group)
15413    gnus-level-default-subscribed gnus-level-killed previous t))
15414
15415 ;; `gnus-group-change-level' is the fundamental function for changing
15416 ;; subscription levels of newsgroups.  This might mean just changing
15417 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15418 ;; again, which subscribes/unsubscribes a group, which is equally
15419 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15420 ;; from 8-9 to 1-7 means that you remove the group from the list of
15421 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15422 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15423 ;; which is trivial.
15424 ;; ENTRY can either be a string (newsgroup name) or a list (if
15425 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15426 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15427 ;; entries.
15428 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15429 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15430 ;; after.
15431 (defun gnus-group-change-level (entry level &optional oldlevel
15432                                       previous fromkilled)
15433   (let (group info active num)
15434     ;; Glean what info we can from the arguments
15435     (if (consp entry)
15436         (if fromkilled (setq group (nth 1 entry))
15437           (setq group (car (nth 2 entry))))
15438       (setq group entry))
15439     (if (and (stringp entry)
15440              oldlevel
15441              (< oldlevel gnus-level-zombie))
15442         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15443     (if (and (not oldlevel)
15444              (consp entry))
15445         (setq oldlevel (gnus-info-level (nth 2 entry)))
15446       (setq oldlevel (or oldlevel 9)))
15447     (if (stringp previous)
15448         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15449
15450     (if (and (>= oldlevel gnus-level-zombie)
15451              (gnus-gethash group gnus-newsrc-hashtb))
15452         ;; We are trying to subscribe a group that is already
15453         ;; subscribed.
15454         ()                              ; Do nothing.
15455
15456       (or (gnus-ephemeral-group-p group)
15457           (gnus-dribble-enter
15458            (format "(gnus-group-change-level %S %S %S %S %S)"
15459                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15460
15461       ;; Then we remove the newgroup from any old structures, if needed.
15462       ;; If the group was killed, we remove it from the killed or zombie
15463       ;; list.  If not, and it is in fact going to be killed, we remove
15464       ;; it from the newsrc hash table and assoc.
15465       (cond
15466        ((>= oldlevel gnus-level-zombie)
15467         (if (= oldlevel gnus-level-zombie)
15468             (setq gnus-zombie-list (delete group gnus-zombie-list))
15469           (setq gnus-killed-list (delete group gnus-killed-list))))
15470        (t
15471         (if (and (>= level gnus-level-zombie)
15472                  entry)
15473             (progn
15474               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15475               (if (nth 3 entry)
15476                   (setcdr (gnus-gethash (car (nth 3 entry))
15477                                         gnus-newsrc-hashtb)
15478                           (cdr entry)))
15479               (setcdr (cdr entry) (cdddr entry))))))
15480
15481       ;; Finally we enter (if needed) the list where it is supposed to
15482       ;; go, and change the subscription level.  If it is to be killed,
15483       ;; we enter it into the killed or zombie list.
15484       (cond 
15485        ((>= level gnus-level-zombie)
15486         ;; Remove from the hash table.
15487         (gnus-sethash group nil gnus-newsrc-hashtb)
15488         ;; We do not enter foreign groups into the list of dead
15489         ;; groups.
15490         (unless (gnus-group-foreign-p group)
15491           (if (= level gnus-level-zombie)
15492               (setq gnus-zombie-list (cons group gnus-zombie-list))
15493             (setq gnus-killed-list (cons group gnus-killed-list)))))
15494        (t
15495         ;; If the list is to be entered into the newsrc assoc, and
15496         ;; it was killed, we have to create an entry in the newsrc
15497         ;; hashtb format and fix the pointers in the newsrc assoc.
15498         (if (< oldlevel gnus-level-zombie)
15499             ;; It was alive, and it is going to stay alive, so we
15500             ;; just change the level and don't change any pointers or
15501             ;; hash table entries.
15502             (setcar (cdaddr entry) level)
15503           (if (listp entry)
15504               (setq info (cdr entry)
15505                     num (car entry))
15506             (setq active (gnus-active group))
15507             (setq num
15508                   (if active (- (1+ (cdr active)) (car active)) t))
15509             ;; Check whether the group is foreign.  If so, the
15510             ;; foreign select method has to be entered into the
15511             ;; info.
15512             (let ((method (or gnus-override-subscribe-method
15513                               (gnus-group-method group))))
15514               (if (eq method gnus-select-method)
15515                   (setq info (list group level nil))
15516                 (setq info (list group level nil nil method)))))
15517           (unless previous
15518             (setq previous
15519                   (let ((p gnus-newsrc-alist))
15520                     (while (cddr p)
15521                       (setq p (cdr p)))
15522                     p)))
15523           (setq entry (cons info (cddr previous)))
15524           (if (cdr previous)
15525               (progn
15526                 (setcdr (cdr previous) entry)
15527                 (gnus-sethash group (cons num (cdr previous))
15528                               gnus-newsrc-hashtb))
15529             (setcdr previous entry)
15530             (gnus-sethash group (cons num previous)
15531                           gnus-newsrc-hashtb))
15532           (when (cdr entry)
15533             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15534       (when gnus-group-change-level-function
15535         (funcall gnus-group-change-level-function group level oldlevel)))))
15536
15537 (defun gnus-kill-newsgroup (newsgroup)
15538   "Obsolete function.  Kills a newsgroup."
15539   (gnus-group-change-level
15540    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15541
15542 (defun gnus-check-bogus-newsgroups (&optional confirm)
15543   "Remove bogus newsgroups.
15544 If CONFIRM is non-nil, the user has to confirm the deletion of every
15545 newsgroup."
15546   (let ((newsrc (cdr gnus-newsrc-alist))
15547         bogus group entry info)
15548     (gnus-message 5 "Checking bogus newsgroups...")
15549     (unless (gnus-read-active-file-p)
15550       (gnus-read-active-file))
15551     (when (gnus-read-active-file-p)
15552       ;; Find all bogus newsgroup that are subscribed.
15553       (while newsrc
15554         (setq info (pop newsrc)
15555               group (gnus-info-group info))
15556         (unless (or (gnus-active group) ; Active
15557                     (gnus-info-method info) ; Foreign
15558                     (and confirm
15559                          (not (gnus-y-or-n-p
15560                                (format "Remove bogus newsgroup: %s " group)))))
15561           ;; Found a bogus newsgroup.
15562           (push group bogus)))
15563       ;; Remove all bogus subscribed groups by first killing them, and
15564       ;; then removing them from the list of killed groups.
15565       (while bogus
15566         (when (setq entry (gnus-gethash (setq group (pop bogus))
15567                                         gnus-newsrc-hashtb))
15568           (gnus-group-change-level entry gnus-level-killed)
15569           (setq gnus-killed-list (delete group gnus-killed-list))))
15570       ;; Then we remove all bogus groups from the list of killed and
15571       ;; zombie groups.  They are are removed without confirmation.
15572       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15573             killed)
15574         (while dead-lists
15575           (setq killed (symbol-value (car dead-lists)))
15576           (while killed
15577             (unless (gnus-active (setq group (pop killed)))
15578               ;; The group is bogus.
15579               ;; !!!Slow as hell.
15580               (set (car dead-lists)
15581                    (delete group (symbol-value (car dead-lists))))))
15582           (setq dead-lists (cdr dead-lists))))
15583       (gnus-message 5 "Checking bogus newsgroups...done"))))
15584
15585 (defun gnus-check-duplicate-killed-groups ()
15586   "Remove duplicates from the list of killed groups."
15587   (interactive)
15588   (let ((killed gnus-killed-list))
15589     (while killed
15590       (gnus-message 9 "%d" (length killed))
15591       (setcdr killed (delete (car killed) (cdr killed)))
15592       (setq killed (cdr killed)))))
15593
15594 ;; We want to inline a function from gnus-cache, so we cheat here:
15595 (eval-when-compile
15596   (provide 'gnus)
15597   (require 'gnus-cache))
15598
15599 (defun gnus-get-unread-articles-in-group (info active &optional update)
15600   (when active
15601     ;; Allow the backend to update the info in the group.
15602     (when (and update 
15603                (gnus-request-update-info
15604                 info (gnus-find-method-for-group (gnus-info-group info))))
15605       (gnus-activate-group (gnus-info-group info) nil t))
15606     (let* ((range (gnus-info-read info))
15607            (num 0))
15608       ;; If a cache is present, we may have to alter the active info.
15609       (when (and gnus-use-cache info)
15610         (inline (gnus-cache-possibly-alter-active 
15611                  (gnus-info-group info) active)))
15612       ;; Modify the list of read articles according to what articles
15613       ;; are available; then tally the unread articles and add the
15614       ;; number to the group hash table entry.
15615       (cond
15616        ((zerop (cdr active))
15617         (setq num 0))
15618        ((not range)
15619         (setq num (- (1+ (cdr active)) (car active))))
15620        ((not (listp (cdr range)))
15621         ;; Fix a single (num . num) range according to the
15622         ;; active hash table.
15623         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15624         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15625         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15626         ;; Compute number of unread articles.
15627         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15628        (t
15629         ;; The read list is a list of ranges.  Fix them according to
15630         ;; the active hash table.
15631         ;; First peel off any elements that are below the lower
15632         ;; active limit.
15633         (while (and (cdr range)
15634                     (>= (car active)
15635                         (or (and (atom (cadr range)) (cadr range))
15636                             (caadr range))))
15637           (if (numberp (car range))
15638               (setcar range
15639                       (cons (car range)
15640                             (or (and (numberp (cadr range))
15641                                      (cadr range))
15642                                 (cdadr range))))
15643             (setcdr (car range)
15644                     (or (and (numberp (nth 1 range)) (nth 1 range))
15645                         (cdadr range))))
15646           (setcdr range (cddr range)))
15647         ;; Adjust the first element to be the same as the lower limit.
15648         (if (and (not (atom (car range)))
15649                  (< (cdar range) (car active)))
15650             (setcdr (car range) (1- (car active))))
15651         ;; Then we want to peel off any elements that are higher
15652         ;; than the upper active limit.
15653         (let ((srange range))
15654           ;; Go past all legal elements.
15655           (while (and (cdr srange)
15656                       (<= (or (and (atom (cadr srange))
15657                                    (cadr srange))
15658                               (caadr srange)) (cdr active)))
15659             (setq srange (cdr srange)))
15660           (if (cdr srange)
15661               ;; Nuke all remaining illegal elements.
15662               (setcdr srange nil))
15663
15664           ;; Adjust the final element.
15665           (if (and (not (atom (car srange)))
15666                    (> (cdar srange) (cdr active)))
15667               (setcdr (car srange) (cdr active))))
15668         ;; Compute the number of unread articles.
15669         (while range
15670           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15671                                       (cdar range)))
15672                               (or (and (atom (car range)) (car range))
15673                                   (caar range)))))
15674           (setq range (cdr range)))
15675         (setq num (max 0 (- (cdr active) num)))))
15676       ;; Set the number of unread articles.
15677       (when info
15678         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15679       num)))
15680
15681 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15682 ;; and compute how many unread articles there are in each group.
15683 (defun gnus-get-unread-articles (&optional level)
15684   (let* ((newsrc (cdr gnus-newsrc-alist))
15685          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15686          (foreign-level
15687           (min
15688            (cond ((and gnus-activate-foreign-newsgroups
15689                        (not (numberp gnus-activate-foreign-newsgroups)))
15690                   (1+ gnus-level-subscribed))
15691                  ((numberp gnus-activate-foreign-newsgroups)
15692                   gnus-activate-foreign-newsgroups)
15693                  (t 0))
15694            level))
15695          info group active method)
15696     (gnus-message 5 "Checking new news...")
15697
15698     (while newsrc
15699       (setq active (gnus-active (setq group (gnus-info-group
15700                                              (setq info (pop newsrc))))))
15701
15702       ;; Check newsgroups.  If the user doesn't want to check them, or
15703       ;; they can't be checked (for instance, if the news server can't
15704       ;; be reached) we just set the number of unread articles in this
15705       ;; newsgroup to t.  This means that Gnus thinks that there are
15706       ;; unread articles, but it has no idea how many.
15707       (if (and (setq method (gnus-info-method info))
15708                (not (gnus-server-equal
15709                      gnus-select-method
15710                      (setq method (gnus-server-get-method nil method))))
15711                (not (gnus-secondary-method-p method)))
15712           ;; These groups are foreign.  Check the level.
15713           (when (<= (gnus-info-level info) foreign-level)
15714             (setq active (gnus-activate-group group 'scan))
15715             (unless (inline (gnus-virtual-group-p group))
15716               (inline (gnus-close-group group))))
15717
15718         ;; These groups are native or secondary.
15719         (when (and (<= (gnus-info-level info) level)
15720                    (not gnus-read-active-file))
15721           (setq active (gnus-activate-group group 'scan))
15722           (inline (gnus-close-group group))))
15723
15724       (if active
15725           (inline (gnus-get-unread-articles-in-group 
15726                    info active
15727                    (and method
15728                         (fboundp (intern (concat (symbol-name (car method))
15729                                                  "-request-update-info"))))))
15730         ;; The group couldn't be reached, so we nix out the number of
15731         ;; unread articles and stuff.
15732         (gnus-set-active group nil)
15733         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15734
15735     (gnus-message 5 "Checking new news...done")))
15736
15737 ;; Create a hash table out of the newsrc alist.  The `car's of the
15738 ;; alist elements are used as keys.
15739 (defun gnus-make-hashtable-from-newsrc-alist ()
15740   (let ((alist gnus-newsrc-alist)
15741         (ohashtb gnus-newsrc-hashtb)
15742         prev)
15743     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15744     (setq alist
15745           (setq prev (setq gnus-newsrc-alist
15746                            (if (equal (caar gnus-newsrc-alist)
15747                                       "dummy.group")
15748                                gnus-newsrc-alist
15749                              (cons (list "dummy.group" 0 nil) alist)))))
15750     (while alist
15751       (gnus-sethash
15752        (caar alist)
15753        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15754              prev)
15755        gnus-newsrc-hashtb)
15756       (setq prev alist
15757             alist (cdr alist)))))
15758
15759 (defun gnus-make-hashtable-from-killed ()
15760   "Create a hash table from the killed and zombie lists."
15761   (let ((lists '(gnus-killed-list gnus-zombie-list))
15762         list)
15763     (setq gnus-killed-hashtb
15764           (gnus-make-hashtable
15765            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15766     (while (setq list (pop lists))
15767       (setq list (symbol-value list))
15768       (while list
15769         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15770
15771 (defun gnus-activate-group (group &optional scan dont-check)
15772   ;; Check whether a group has been activated or not.
15773   ;; If SCAN, request a scan of that group as well.
15774   (let ((method (gnus-find-method-for-group group))
15775         active)
15776     (and (gnus-check-server method)
15777          ;; We escape all bugs and quit here to make it possible to
15778          ;; continue if a group is so out-there that it reports bugs
15779          ;; and stuff.
15780          (progn
15781            (and scan
15782                 (gnus-check-backend-function 'request-scan (car method))
15783                 (gnus-request-scan group method))
15784            t)
15785          (condition-case ()
15786              (gnus-request-group group dont-check)
15787         ;   (error nil)
15788            (quit nil))
15789          (save-excursion
15790            (set-buffer nntp-server-buffer)
15791            (goto-char (point-min))
15792            ;; Parse the result we got from `gnus-request-group'.
15793            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15794                 (progn
15795                   (goto-char (match-beginning 1))
15796                   (gnus-set-active
15797                    group (setq active (cons (read (current-buffer))
15798                                             (read (current-buffer)))))
15799                   ;; Return the new active info.
15800                   active))))))
15801
15802 (defun gnus-update-read-articles (group unread)
15803   "Update the list of read and ticked articles in GROUP using the
15804 UNREAD and TICKED lists.
15805 Note: UNSELECTED has to be sorted over `<'.
15806 Returns whether the updating was successful."
15807   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15808          (entry (gnus-gethash group gnus-newsrc-hashtb))
15809          (info (nth 2 entry))
15810          (prev 1)
15811          (unread (sort (copy-sequence unread) '<))
15812          read)
15813     (if (or (not info) (not active))
15814         ;; There is no info on this group if it was, in fact,
15815         ;; killed.  Gnus stores no information on killed groups, so
15816         ;; there's nothing to be done.
15817         ;; One could store the information somewhere temporarily,
15818         ;; perhaps...  Hmmm...
15819         ()
15820       ;; Remove any negative articles numbers.
15821       (while (and unread (< (car unread) 0))
15822         (setq unread (cdr unread)))
15823       ;; Remove any expired article numbers
15824       (while (and unread (< (car unread) (car active)))
15825         (setq unread (cdr unread)))
15826       ;; Compute the ranges of read articles by looking at the list of
15827       ;; unread articles.
15828       (while unread
15829         (if (/= (car unread) prev)
15830             (setq read (cons (if (= prev (1- (car unread))) prev
15831                                (cons prev (1- (car unread)))) read)))
15832         (setq prev (1+ (car unread)))
15833         (setq unread (cdr unread)))
15834       (when (<= prev (cdr active))
15835         (setq read (cons (cons prev (cdr active)) read)))
15836       ;; Enter this list into the group info.
15837       (gnus-info-set-read
15838        info (if (> (length read) 1) (nreverse read) read))
15839       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15840       (gnus-get-unread-articles-in-group info (gnus-active group))
15841       t)))
15842
15843 (defun gnus-make-articles-unread (group articles)
15844   "Mark ARTICLES in GROUP as unread."
15845   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15846                           (gnus-gethash (gnus-group-real-name group)
15847                                         gnus-newsrc-hashtb))))
15848          (ranges (gnus-info-read info))
15849          news article)
15850     (while articles
15851       (when (gnus-member-of-range
15852              (setq article (pop articles)) ranges)
15853         (setq news (cons article news))))
15854     (when news
15855       (gnus-info-set-read
15856        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15857       (gnus-group-update-group group t))))
15858
15859 ;; Enter all dead groups into the hashtb.
15860 (defun gnus-update-active-hashtb-from-killed ()
15861   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15862         (lists (list gnus-killed-list gnus-zombie-list))
15863         killed)
15864     (while lists
15865       (setq killed (car lists))
15866       (while killed
15867         (gnus-sethash (car killed) nil hashtb)
15868         (setq killed (cdr killed)))
15869       (setq lists (cdr lists)))))
15870
15871 (defun gnus-get-killed-groups ()
15872   "Go through the active hashtb and all all unknown groups as killed."
15873   ;; First make sure active file has been read.
15874   (unless (gnus-read-active-file-p)
15875     (let ((gnus-read-active-file t))
15876       (gnus-read-active-file)))
15877   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15878   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15879   (mapatoms
15880    (lambda (sym)
15881      (let ((groups 0)
15882            (group (symbol-name sym)))
15883        (if (or (null group)
15884                (gnus-gethash group gnus-killed-hashtb)
15885                (gnus-gethash group gnus-newsrc-hashtb))
15886            ()
15887          (let ((do-sub (gnus-matches-options-n group)))
15888            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
15889                ()
15890              (setq groups (1+ groups))
15891              (setq gnus-killed-list
15892                    (cons group gnus-killed-list))
15893              (gnus-sethash group group gnus-killed-hashtb))))))
15894    gnus-active-hashtb))
15895
15896 ;; Get the active file(s) from the backend(s).
15897 (defun gnus-read-active-file ()
15898   (gnus-group-set-mode-line)
15899   (let ((methods 
15900          (append
15901           (if (gnus-check-server gnus-select-method)
15902               ;; The native server is available.
15903               (cons gnus-select-method gnus-secondary-select-methods)
15904             ;; The native server is down, so we just do the
15905             ;; secondary ones.
15906             gnus-secondary-select-methods)
15907           ;; Also read from the archive server.
15908           (when gnus-message-archive-method
15909             (list "archive"))))
15910         list-type)
15911     (setq gnus-have-read-active-file nil)
15912     (save-excursion
15913       (set-buffer nntp-server-buffer)
15914       (while methods
15915         (let* ((method (if (stringp (car methods))
15916                            (gnus-server-get-method nil (car methods))
15917                          (car methods)))
15918                (where (nth 1 method))
15919                (mesg (format "Reading active file%s via %s..."
15920                              (if (and where (not (zerop (length where))))
15921                                  (concat " from " where) "")
15922                              (car method))))
15923           (gnus-message 5 mesg)
15924           (when (gnus-check-server method)
15925             ;; Request that the backend scan its incoming messages.
15926             (and (gnus-check-backend-function 'request-scan (car method))
15927                  (gnus-request-scan nil method))
15928             (cond
15929              ((and (eq gnus-read-active-file 'some)
15930                    (gnus-check-backend-function 'retrieve-groups (car method)))
15931               (let ((newsrc (cdr gnus-newsrc-alist))
15932                     (gmethod (gnus-server-get-method nil method))
15933                     groups info)
15934                 (while (setq info (pop newsrc))
15935                   (when (gnus-server-equal
15936                          (gnus-find-method-for-group 
15937                           (gnus-info-group info) info)
15938                          gmethod)
15939                     (push (gnus-group-real-name (gnus-info-group info)) 
15940                           groups)))
15941                 (when groups
15942                   (gnus-check-server method)
15943                   (setq list-type (gnus-retrieve-groups groups method))
15944                   (cond
15945                    ((not list-type)
15946                     (gnus-error
15947                      1.2 "Cannot read partial active file from %s server."
15948                      (car method)))
15949                    ((eq list-type 'active)
15950                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15951                    (t
15952                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15953              (t
15954               (if (not (gnus-request-list method))
15955                   (unless (equal method gnus-message-archive-method)
15956                     (gnus-error 1 "Cannot read active file from %s server."
15957                                 (car method)))
15958                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15959                 ;; We mark this active file as read.
15960                 (push method gnus-have-read-active-file)
15961                 (gnus-message 5 "%sdone" mesg))))))
15962         (setq methods (cdr methods))))))
15963
15964 ;; Read an active file and place the results in `gnus-active-hashtb'.
15965 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15966   (unless method
15967     (setq method gnus-select-method))
15968   (let ((cur (current-buffer))
15969         (hashtb (or hashtb
15970                     (if (and gnus-active-hashtb
15971                              (not (equal method gnus-select-method)))
15972                         gnus-active-hashtb
15973                       (setq gnus-active-hashtb
15974                             (if (equal method gnus-select-method)
15975                                 (gnus-make-hashtable
15976                                  (count-lines (point-min) (point-max)))
15977                               (gnus-make-hashtable 4096)))))))
15978     ;; Delete unnecessary lines.
15979     (goto-char (point-min))
15980     (while (search-forward "\nto." nil t)
15981       (delete-region (1+ (match-beginning 0))
15982                      (progn (forward-line 1) (point))))
15983     (or (string= gnus-ignored-newsgroups "")
15984         (progn
15985           (goto-char (point-min))
15986           (delete-matching-lines gnus-ignored-newsgroups)))
15987     ;; Make the group names readable as a lisp expression even if they
15988     ;; contain special characters.
15989     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15990     (goto-char (point-max))
15991     (while (re-search-backward "[][';?()#]" nil t)
15992       (insert ?\\))
15993     ;; If these are groups from a foreign select method, we insert the
15994     ;; group prefix in front of the group names.
15995     (and method (not (gnus-server-equal
15996                       (gnus-server-get-method nil method)
15997                       (gnus-server-get-method nil gnus-select-method)))
15998          (let ((prefix (gnus-group-prefixed-name "" method)))
15999            (goto-char (point-min))
16000            (while (and (not (eobp))
16001                        (progn (insert prefix)
16002                               (zerop (forward-line 1)))))))
16003     ;; Store the active file in a hash table.
16004     (goto-char (point-min))
16005     (if (string-match "%[oO]" gnus-group-line-format)
16006         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16007         ;; If we want information on moderated groups, we use this
16008         ;; loop...
16009         (let* ((mod-hashtb (make-vector 7 0))
16010                (m (intern "m" mod-hashtb))
16011                group max min)
16012           (while (not (eobp))
16013             (condition-case nil
16014                 (progn
16015                   (narrow-to-region (point) (gnus-point-at-eol))
16016                   (setq group (let ((obarray hashtb)) (read cur)))
16017                   (if (and (numberp (setq max (read cur)))
16018                            (numberp (setq min (read cur)))
16019                            (progn
16020                              (skip-chars-forward " \t")
16021                              (not
16022                               (or (= (following-char) ?=)
16023                                   (= (following-char) ?x)
16024                                   (= (following-char) ?j)))))
16025                       (set group (cons min max))
16026                     (set group nil))
16027                   ;; Enter moderated groups into a list.
16028                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16029                       (setq gnus-moderated-list
16030                             (cons (symbol-name group) gnus-moderated-list))))
16031               (error
16032                (and group
16033                     (symbolp group)
16034                     (set group nil))))
16035             (widen)
16036             (forward-line 1)))
16037       ;; And if we do not care about moderation, we use this loop,
16038       ;; which is faster.
16039       (let (group max min)
16040         (while (not (eobp))
16041           (condition-case ()
16042               (progn
16043                 (narrow-to-region (point) (gnus-point-at-eol))
16044                 ;; group gets set to a symbol interned in the hash table
16045                 ;; (what a hack!!) - jwz
16046                 (setq group (let ((obarray hashtb)) (read cur)))
16047                 (if (and (numberp (setq max (read cur)))
16048                          (numberp (setq min (read cur)))
16049                          (progn
16050                            (skip-chars-forward " \t")
16051                            (not
16052                             (or (= (following-char) ?=)
16053                                 (= (following-char) ?x)
16054                                 (= (following-char) ?j)))))
16055                     (set group (cons min max))
16056                   (set group nil)))
16057             (error
16058              (progn
16059                (and group
16060                     (symbolp group)
16061                     (set group nil))
16062                (or ignore-errors
16063                    (gnus-message 3 "Warning - illegal active: %s"
16064                                  (buffer-substring
16065                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16066           (widen)
16067           (forward-line 1))))))
16068
16069 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16070   ;; Parse a "groups" active file.
16071   (let ((cur (current-buffer))
16072         (hashtb (or hashtb
16073                     (if (and method gnus-active-hashtb)
16074                         gnus-active-hashtb
16075                       (setq gnus-active-hashtb
16076                             (gnus-make-hashtable
16077                              (count-lines (point-min) (point-max)))))))
16078         (prefix (and method
16079                      (not (gnus-server-equal
16080                            (gnus-server-get-method nil method)
16081                            (gnus-server-get-method nil gnus-select-method)))
16082                      (gnus-group-prefixed-name "" method))))
16083
16084     (goto-char (point-min))
16085     ;; We split this into to separate loops, one with the prefix
16086     ;; and one without to speed the reading up somewhat.
16087     (if prefix
16088         (let (min max opoint group)
16089           (while (not (eobp))
16090             (condition-case ()
16091                 (progn
16092                   (read cur) (read cur)
16093                   (setq min (read cur)
16094                         max (read cur)
16095                         opoint (point))
16096                   (skip-chars-forward " \t")
16097                   (insert prefix)
16098                   (goto-char opoint)
16099                   (set (let ((obarray hashtb)) (read cur))
16100                        (cons min max)))
16101               (error (and group (symbolp group) (set group nil))))
16102             (forward-line 1)))
16103       (let (min max group)
16104         (while (not (eobp))
16105           (condition-case ()
16106               (if (= (following-char) ?2)
16107                   (progn
16108                     (read cur) (read cur)
16109                     (setq min (read cur)
16110                           max (read cur))
16111                     (set (setq group (let ((obarray hashtb)) (read cur)))
16112                          (cons min max))))
16113             (error (and group (symbolp group) (set group nil))))
16114           (forward-line 1))))))
16115
16116 (defun gnus-read-newsrc-file (&optional force)
16117   "Read startup file.
16118 If FORCE is non-nil, the .newsrc file is read."
16119   ;; Reset variables that might be defined in the .newsrc.eld file.
16120   (let ((variables gnus-variable-list))
16121     (while variables
16122       (set (car variables) nil)
16123       (setq variables (cdr variables))))
16124   (let* ((newsrc-file gnus-current-startup-file)
16125          (quick-file (concat newsrc-file ".el")))
16126     (save-excursion
16127       ;; We always load the .newsrc.eld file.  If always contains
16128       ;; much information that can not be gotten from the .newsrc
16129       ;; file (ticked articles, killed groups, foreign methods, etc.)
16130       (gnus-read-newsrc-el-file quick-file)
16131
16132       (if (and (file-exists-p gnus-current-startup-file)
16133                (or force
16134                    (and (file-newer-than-file-p newsrc-file quick-file)
16135                         (file-newer-than-file-p newsrc-file
16136                                                 (concat quick-file "d")))
16137                    (not gnus-newsrc-alist)))
16138           ;; We read the .newsrc file.  Note that if there if a
16139           ;; .newsrc.eld file exists, it has already been read, and
16140           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16141           ;; the .newsrc file, Gnus will only use the information it
16142           ;; can find there for changing the data already read -
16143           ;; ie. reading the .newsrc file will not trash the data
16144           ;; already read (except for read articles).
16145           (save-excursion
16146             (gnus-message 5 "Reading %s..." newsrc-file)
16147             (set-buffer (find-file-noselect newsrc-file))
16148             (buffer-disable-undo (current-buffer))
16149             (gnus-newsrc-to-gnus-format)
16150             (kill-buffer (current-buffer))
16151             (gnus-message 5 "Reading %s...done" newsrc-file)))
16152
16153       ;; Read any slave files.
16154       (unless gnus-slave
16155         (gnus-master-read-slave-newsrc))
16156       
16157       ;; Convert old to new.
16158       (gnus-convert-old-newsrc))))
16159
16160 (defun gnus-continuum-version (version)
16161   "Return VERSION as a floating point number."
16162   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16163     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16164            (number (match-string 2 version))
16165            major minor least)
16166       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16167       (setq major (string-to-number (match-string 1 number)))
16168       (setq minor (string-to-number (match-string 2 number)))
16169       (setq least (if (match-beginning 3)
16170                       (string-to-number (match-string 3 number))
16171                     0))
16172       (string-to-number
16173        (if (zerop major)
16174            (format "%s00%02d%02d"
16175                    (cond 
16176                     ((string= alpha "(ding)") "4.99")
16177                     ((string= alpha "September") "5.01")
16178                     ((string= alpha "Red") "5.03"))
16179                    minor least)
16180          (format "%d.%02d%20d" major minor least))))))
16181
16182 (defun gnus-convert-old-newsrc ()
16183   "Convert old newsrc into the new format, if needed."
16184   (let ((fcv (and gnus-newsrc-file-version
16185                   (gnus-continuum-version gnus-newsrc-file-version))))
16186     (cond
16187      ;; No .newsrc.eld file was loaded.
16188      ((null fcv) nil)
16189      ;; Gnus 5 .newsrc.eld was loaded.
16190      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16191       (gnus-convert-old-ticks)))))
16192
16193 (defun gnus-convert-old-ticks ()
16194   (let ((newsrc (cdr gnus-newsrc-alist))
16195         marks info dormant ticked)
16196     (while (setq info (pop newsrc))
16197       (when (setq marks (gnus-info-marks info))
16198         (setq dormant (cdr (assq 'dormant marks))
16199               ticked (cdr (assq 'tick marks)))
16200         (when (or dormant ticked)
16201           (gnus-info-set-read
16202            info
16203            (gnus-add-to-range
16204             (gnus-info-read info)
16205             (nconc (gnus-uncompress-range dormant)
16206                    (gnus-uncompress-range ticked)))))))))
16207
16208 (defun gnus-read-newsrc-el-file (file)
16209   (let ((ding-file (concat file "d")))
16210     ;; We always, always read the .eld file.
16211     (gnus-message 5 "Reading %s..." ding-file)
16212     (let (gnus-newsrc-assoc)
16213       (condition-case nil
16214           (load ding-file t t t)
16215         (error
16216          (gnus-error 1 "Error in %s" ding-file)))
16217       (when gnus-newsrc-assoc
16218         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16219     (gnus-make-hashtable-from-newsrc-alist)
16220     (when (file-newer-than-file-p file ding-file)
16221       ;; Old format quick file
16222       (gnus-message 5 "Reading %s..." file)
16223       ;; The .el file is newer than the .eld file, so we read that one
16224       ;; as well.
16225       (gnus-read-old-newsrc-el-file file))))
16226
16227 ;; Parse the old-style quick startup file
16228 (defun gnus-read-old-newsrc-el-file (file)
16229   (let (newsrc killed marked group m info)
16230     (prog1
16231         (let ((gnus-killed-assoc nil)
16232               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16233           (prog1
16234               (condition-case nil
16235                   (load file t t t)
16236                 (error nil))
16237             (setq newsrc gnus-newsrc-assoc
16238                   killed gnus-killed-assoc
16239                   marked gnus-marked-assoc)))
16240       (setq gnus-newsrc-alist nil)
16241       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16242         (if info
16243             (progn
16244               (gnus-info-set-read info (cddr group))
16245               (gnus-info-set-level
16246                info (if (nth 1 group) gnus-level-default-subscribed
16247                       gnus-level-default-unsubscribed))
16248               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16249           (push (setq info
16250                       (list (car group)
16251                             (if (nth 1 group) gnus-level-default-subscribed
16252                               gnus-level-default-unsubscribed)
16253                             (cddr group)))
16254                 gnus-newsrc-alist))
16255         ;; Copy marks into info.
16256         (when (setq m (assoc (car group) marked))
16257           (unless (nthcdr 3 info)
16258             (nconc info (list nil)))
16259           (gnus-info-set-marks
16260            info (list (cons 'tick (gnus-compress-sequence 
16261                                    (sort (cdr m) '<) t))))))
16262       (setq newsrc killed)
16263       (while newsrc
16264         (setcar newsrc (caar newsrc))
16265         (setq newsrc (cdr newsrc)))
16266       (setq gnus-killed-list killed))
16267     ;; The .el file version of this variable does not begin with
16268     ;; "options", while the .eld version does, so we just add it if it
16269     ;; isn't there.
16270     (and
16271      gnus-newsrc-options
16272      (progn
16273        (and (not (string-match "^ *options" gnus-newsrc-options))
16274             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16275        (and (not (string-match "\n$" gnus-newsrc-options))
16276             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16277        ;; Finally, if we read some options lines, we parse them.
16278        (or (string= gnus-newsrc-options "")
16279            (gnus-newsrc-parse-options gnus-newsrc-options))))
16280
16281     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16282     (gnus-make-hashtable-from-newsrc-alist)))
16283
16284 (defun gnus-make-newsrc-file (file)
16285   "Make server dependent file name by catenating FILE and server host name."
16286   (let* ((file (expand-file-name file nil))
16287          (real-file (concat file "-" (nth 1 gnus-select-method))))
16288     (if (or (file-exists-p real-file)
16289             (file-exists-p (concat real-file ".el"))
16290             (file-exists-p (concat real-file ".eld")))
16291         real-file file)))
16292
16293 (defun gnus-newsrc-to-gnus-format ()
16294   (setq gnus-newsrc-options "")
16295   (setq gnus-newsrc-options-n nil)
16296
16297   (or gnus-active-hashtb
16298       (setq gnus-active-hashtb (make-vector 4095 0)))
16299   (let ((buf (current-buffer))
16300         (already-read (> (length gnus-newsrc-alist) 1))
16301         group subscribed options-symbol newsrc Options-symbol
16302         symbol reads num1)
16303     (goto-char (point-min))
16304     ;; We intern the symbol `options' in the active hashtb so that we
16305     ;; can `eq' against it later.
16306     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16307     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16308
16309     (while (not (eobp))
16310       ;; We first read the first word on the line by narrowing and
16311       ;; then reading into `gnus-active-hashtb'.  Most groups will
16312       ;; already exist in that hashtb, so this will save some string
16313       ;; space.
16314       (narrow-to-region
16315        (point)
16316        (progn (skip-chars-forward "^ \t!:\n") (point)))
16317       (goto-char (point-min))
16318       (setq symbol
16319             (and (/= (point-min) (point-max))
16320                  (let ((obarray gnus-active-hashtb)) (read buf))))
16321       (widen)
16322       ;; Now, the symbol we have read is either `options' or a group
16323       ;; name.  If it is an options line, we just add it to a string.
16324       (cond
16325        ((or (eq symbol options-symbol)
16326             (eq symbol Options-symbol))
16327         (setq gnus-newsrc-options
16328               ;; This concating is quite inefficient, but since our
16329               ;; thorough studies show that approx 99.37% of all
16330               ;; .newsrc files only contain a single options line, we
16331               ;; don't give a damn, frankly, my dear.
16332               (concat gnus-newsrc-options
16333                       (buffer-substring
16334                        (gnus-point-at-bol)
16335                        ;; Options may continue on the next line.
16336                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16337                                 (progn (beginning-of-line) (point)))
16338                            (point)))))
16339         (forward-line -1))
16340        (symbol
16341         ;; Group names can be just numbers.  
16342         (when (numberp symbol) 
16343           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16344         (or (boundp symbol) (set symbol nil))
16345         ;; It was a group name.
16346         (setq subscribed (= (following-char) ?:)
16347               group (symbol-name symbol)
16348               reads nil)
16349         (if (eolp)
16350             ;; If the line ends here, this is clearly a buggy line, so
16351             ;; we put point a the beginning of line and let the cond
16352             ;; below do the error handling.
16353             (beginning-of-line)
16354           ;; We skip to the beginning of the ranges.
16355           (skip-chars-forward "!: \t"))
16356         ;; We are now at the beginning of the list of read articles.
16357         ;; We read them range by range.
16358         (while
16359             (cond
16360              ((looking-at "[0-9]+")
16361               ;; We narrow and read a number instead of buffer-substring/
16362               ;; string-to-int because it's faster.  narrow/widen is
16363               ;; faster than save-restriction/narrow, and save-restriction
16364               ;; produces a garbage object.
16365               (setq num1 (progn
16366                            (narrow-to-region (match-beginning 0) (match-end 0))
16367                            (read buf)))
16368               (widen)
16369               ;; If the next character is a dash, then this is a range.
16370               (if (= (following-char) ?-)
16371                   (progn
16372                     ;; We read the upper bound of the range.
16373                     (forward-char 1)
16374                     (if (not (looking-at "[0-9]+"))
16375                         ;; This is a buggy line, by we pretend that
16376                         ;; it's kinda OK.  Perhaps the user should be
16377                         ;; dinged?
16378                         (setq reads (cons num1 reads))
16379                       (setq reads
16380                             (cons
16381                              (cons num1
16382                                    (progn
16383                                      (narrow-to-region (match-beginning 0)
16384                                                        (match-end 0))
16385                                      (read buf)))
16386                              reads))
16387                       (widen)))
16388                 ;; It was just a simple number, so we add it to the
16389                 ;; list of ranges.
16390                 (setq reads (cons num1 reads)))
16391               ;; If the next char in ?\n, then we have reached the end
16392               ;; of the line and return nil.
16393               (/= (following-char) ?\n))
16394              ((= (following-char) ?\n)
16395               ;; End of line, so we end.
16396               nil)
16397              (t
16398               ;; Not numbers and not eol, so this might be a buggy
16399               ;; line...
16400               (or (eobp)
16401                   ;; If it was eob instead of ?\n, we allow it.
16402                   (progn
16403                     ;; The line was buggy.
16404                     (setq group nil)
16405                     (gnus-error 3.1 "Mangled line: %s"
16406                                 (buffer-substring (gnus-point-at-bol)
16407                                                   (gnus-point-at-eol)))))
16408               nil))
16409           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16410           ;; we allow them, because it's a common mistake to put a
16411           ;; space after the comma.
16412           (skip-chars-forward ", "))
16413
16414         ;; We have already read .newsrc.eld, so we gently update the
16415         ;; data in the hash table with the information we have just
16416         ;; read.
16417         (when group
16418           (let ((info (gnus-get-info group))
16419                 level)
16420             (if info
16421                 ;; There is an entry for this file in the alist.
16422                 (progn
16423                   (gnus-info-set-read info (nreverse reads))
16424                   ;; We update the level very gently.  In fact, we
16425                   ;; only change it if there's been a status change
16426                   ;; from subscribed to unsubscribed, or vice versa.
16427                   (setq level (gnus-info-level info))
16428                   (cond ((and (<= level gnus-level-subscribed)
16429                               (not subscribed))
16430                          (setq level (if reads
16431                                          gnus-level-default-unsubscribed
16432                                        (1+ gnus-level-default-unsubscribed))))
16433                         ((and (> level gnus-level-subscribed) subscribed)
16434                          (setq level gnus-level-default-subscribed)))
16435                   (gnus-info-set-level info level))
16436               ;; This is a new group.
16437               (setq info (list group
16438                                (if subscribed
16439                                    gnus-level-default-subscribed
16440                                  (if reads
16441                                      (1+ gnus-level-subscribed)
16442                                    gnus-level-default-unsubscribed))
16443                                (nreverse reads))))
16444             (setq newsrc (cons info newsrc))))))
16445       (forward-line 1))
16446
16447     (setq newsrc (nreverse newsrc))
16448
16449     (if (not already-read)
16450         ()
16451       ;; We now have two newsrc lists - `newsrc', which is what we
16452       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16453       ;; what we've read from .newsrc.eld.  We have to merge these
16454       ;; lists.  We do this by "attaching" any (foreign) groups in the
16455       ;; gnus-newsrc-alist to the (native) group that precedes them.
16456       (let ((rc (cdr gnus-newsrc-alist))
16457             (prev gnus-newsrc-alist)
16458             entry mentry)
16459         (while rc
16460           (or (null (nth 4 (car rc)))   ; It's a native group.
16461               (assoc (caar rc) newsrc) ; It's already in the alist.
16462               (if (setq entry (assoc (caar prev) newsrc))
16463                   (setcdr (setq mentry (memq entry newsrc))
16464                           (cons (car rc) (cdr mentry)))
16465                 (setq newsrc (cons (car rc) newsrc))))
16466           (setq prev rc
16467                 rc (cdr rc)))))
16468
16469     (setq gnus-newsrc-alist newsrc)
16470     ;; We make the newsrc hashtb.
16471     (gnus-make-hashtable-from-newsrc-alist)
16472
16473     ;; Finally, if we read some options lines, we parse them.
16474     (or (string= gnus-newsrc-options "")
16475         (gnus-newsrc-parse-options gnus-newsrc-options))))
16476
16477 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16478 ;; The return value will be a list on the form
16479 ;; ((regexp1 . ignore)
16480 ;;  (regexp2 . subscribe)...)
16481 ;; When handling new newsgroups, groups that match a `ignore' regexp
16482 ;; will be ignored, and groups that match a `subscribe' regexp will be
16483 ;; subscribed.  A line like
16484 ;; options -n !all rec.all
16485 ;; will lead to a list that looks like
16486 ;; (("^rec\\..+" . subscribe)
16487 ;;  ("^.+" . ignore))
16488 ;; So all "rec.*" groups will be subscribed, while all the other
16489 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16490 ;; different from "options -n rec.all !all".
16491 (defun gnus-newsrc-parse-options (options)
16492   (let (out eol)
16493     (save-excursion
16494       (gnus-set-work-buffer)
16495       (insert (regexp-quote options))
16496       ;; First we treat all continuation lines.
16497       (goto-char (point-min))
16498       (while (re-search-forward "\n[ \t]+" nil t)
16499         (replace-match " " t t))
16500       ;; Then we transform all "all"s into ".+"s.
16501       (goto-char (point-min))
16502       (while (re-search-forward "\\ball\\b" nil t)
16503         (replace-match ".+" t t))
16504       (goto-char (point-min))
16505       ;; We remove all other options than the "-n" ones.
16506       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16507         (replace-match " ")
16508         (forward-char -1))
16509       (goto-char (point-min))
16510
16511       ;; We are only interested in "options -n" lines - we
16512       ;; ignore the other option lines.
16513       (while (re-search-forward "[ \t]-n" nil t)
16514         (setq eol
16515               (or (save-excursion
16516                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16517                          (- (point) 2)))
16518                   (gnus-point-at-eol)))
16519         ;; Search for all "words"...
16520         (while (re-search-forward "[^ \t,\n]+" eol t)
16521           (if (= (char-after (match-beginning 0)) ?!)
16522               ;; If the word begins with a bang (!), this is a "not"
16523               ;; spec.  We put this spec (minus the bang) and the
16524               ;; symbol `ignore' into the list.
16525               (setq out (cons (cons (concat
16526                                      "^" (buffer-substring
16527                                           (1+ (match-beginning 0))
16528                                           (match-end 0)))
16529                                     'ignore) out))
16530             ;; There was no bang, so this is a "yes" spec.
16531             (setq out (cons (cons (concat "^" (match-string 0))
16532                                   'subscribe) out)))))
16533
16534       (setq gnus-newsrc-options-n out))))
16535
16536 (defun gnus-save-newsrc-file (&optional force)
16537   "Save .newsrc file."
16538   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16539   ;; from the variable gnus-newsrc-alist.
16540   (when (and (or gnus-newsrc-alist gnus-killed-list)
16541              gnus-current-startup-file)
16542     (save-excursion
16543       (if (and (or gnus-use-dribble-file gnus-slave)
16544                (not force)
16545                (or (not gnus-dribble-buffer)
16546                    (not (buffer-name gnus-dribble-buffer))
16547                    (zerop (save-excursion
16548                             (set-buffer gnus-dribble-buffer)
16549                             (buffer-size)))))
16550           (gnus-message 4 "(No changes need to be saved)")
16551         (run-hooks 'gnus-save-newsrc-hook)
16552         (if gnus-slave
16553             (gnus-slave-save-newsrc)
16554           ;; Save .newsrc.
16555           (when gnus-save-newsrc-file
16556             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16557             (gnus-gnus-to-newsrc-format)
16558             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16559           ;; Save .newsrc.eld.
16560           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16561           (make-local-variable 'version-control)
16562           (setq version-control 'never)
16563           (setq buffer-file-name
16564                 (concat gnus-current-startup-file ".eld"))
16565           (gnus-add-current-to-buffer-list)
16566           (buffer-disable-undo (current-buffer))
16567           (erase-buffer)
16568           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16569           (gnus-gnus-to-quick-newsrc-format)
16570           (run-hooks 'gnus-save-quick-newsrc-hook)
16571           (save-buffer)
16572           (kill-buffer (current-buffer))
16573           (gnus-message
16574            5 "Saving %s.eld...done" gnus-current-startup-file))
16575         (gnus-dribble-delete-file)))))
16576
16577 (defun gnus-gnus-to-quick-newsrc-format ()
16578   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16579   (insert ";; Gnus startup file.\n")
16580   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16581   (insert ";; to read .newsrc.\n")
16582   (insert "(setq gnus-newsrc-file-version "
16583           (prin1-to-string gnus-version) ")\n")
16584   (let ((variables
16585          (if gnus-save-killed-list gnus-variable-list
16586            ;; Remove the `gnus-killed-list' from the list of variables
16587            ;; to be saved, if required.
16588            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16589         ;; Peel off the "dummy" group.
16590         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16591         variable)
16592     ;; Insert the variables into the file.
16593     (while variables
16594       (when (and (boundp (setq variable (pop variables)))
16595                  (symbol-value variable))
16596         (insert "(setq " (symbol-name variable) " '")
16597         (prin1 (symbol-value variable) (current-buffer))
16598         (insert ")\n")))))
16599
16600 (defun gnus-gnus-to-newsrc-format ()
16601   ;; Generate and save the .newsrc file.
16602   (save-excursion
16603     (set-buffer (create-file-buffer gnus-current-startup-file))
16604     (let ((newsrc (cdr gnus-newsrc-alist))
16605           (standard-output (current-buffer))
16606           info ranges range method)
16607       (setq buffer-file-name gnus-current-startup-file)
16608       (buffer-disable-undo (current-buffer))
16609       (erase-buffer)
16610       ;; Write options.
16611       (if gnus-newsrc-options (insert gnus-newsrc-options))
16612       ;; Write subscribed and unsubscribed.
16613       (while (setq info (pop newsrc))
16614         ;; Don't write foreign groups to .newsrc.
16615         (when (or (null (setq method (gnus-info-method info)))
16616                   (equal method "native")
16617                   (gnus-server-equal method gnus-select-method))
16618           (insert (gnus-info-group info)
16619                   (if (> (gnus-info-level info) gnus-level-subscribed)
16620                       "!" ":"))
16621           (when (setq ranges (gnus-info-read info))
16622             (insert " ")
16623             (if (not (listp (cdr ranges)))
16624                 (if (= (car ranges) (cdr ranges))
16625                     (princ (car ranges))
16626                   (princ (car ranges))
16627                   (insert "-")
16628                   (princ (cdr ranges)))
16629               (while (setq range (pop ranges))
16630                 (if (or (atom range) (= (car range) (cdr range)))
16631                     (princ (or (and (atom range) range) (car range)))
16632                   (princ (car range))
16633                   (insert "-")
16634                   (princ (cdr range)))
16635                 (if ranges (insert ",")))))
16636           (insert "\n")))
16637       (make-local-variable 'version-control)
16638       (setq version-control 'never)
16639       ;; It has been reported that sometime the modtime on the .newsrc
16640       ;; file seems to be off.  We really do want to overwrite it, so
16641       ;; we clear the modtime here before saving.  It's a bit odd,
16642       ;; though...
16643       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16644       ;; delete the silly thing entirely first.  but this fails to provide
16645       ;; such niceties as .newsrc~ creation.
16646       (if gnus-modtime-botch
16647           (delete-file gnus-startup-file)
16648         (clear-visited-file-modtime))
16649       (run-hooks 'gnus-save-standard-newsrc-hook)
16650       (save-buffer)
16651       (kill-buffer (current-buffer)))))
16652
16653 \f
16654 ;;;
16655 ;;; Slave functions.
16656 ;;;
16657
16658 (defun gnus-slave-save-newsrc ()
16659   (save-excursion
16660     (set-buffer gnus-dribble-buffer)
16661     (let ((slave-name
16662            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16663       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16664
16665 (defun gnus-master-read-slave-newsrc ()
16666   (let ((slave-files
16667          (directory-files
16668           (file-name-directory gnus-current-startup-file)
16669           t (concat
16670              "^" (regexp-quote
16671                   (concat
16672                    (file-name-nondirectory gnus-current-startup-file)
16673                    "-slave-")))
16674           t))
16675         file)
16676     (if (not slave-files)
16677         ()                              ; There are no slave files to read.
16678       (gnus-message 7 "Reading slave newsrcs...")
16679       (save-excursion
16680         (set-buffer (get-buffer-create " *gnus slave*"))
16681         (buffer-disable-undo (current-buffer))
16682         (setq slave-files
16683               (sort (mapcar (lambda (file)
16684                               (list (nth 5 (file-attributes file)) file))
16685                             slave-files)
16686                     (lambda (f1 f2)
16687                       (or (< (caar f1) (caar f2))
16688                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16689         (while slave-files
16690           (erase-buffer)
16691           (setq file (nth 1 (car slave-files)))
16692           (insert-file-contents file)
16693           (if (condition-case ()
16694                   (progn
16695                     (eval-buffer (current-buffer))
16696                     t)
16697                 (error
16698                  (gnus-error 3.2 "Possible error in %s" file)
16699                  nil))
16700               (or gnus-slave ; Slaves shouldn't delete these files.
16701                   (condition-case ()
16702                       (delete-file file)
16703                     (error nil))))
16704           (setq slave-files (cdr slave-files))))
16705       (gnus-message 7 "Reading slave newsrcs...done"))))
16706
16707 \f
16708 ;;;
16709 ;;; Group description.
16710 ;;;
16711
16712 (defun gnus-read-all-descriptions-files ()
16713   (let ((methods (cons gnus-select-method 
16714                        (nconc
16715                         (when gnus-message-archive-method
16716                           (list "archive"))
16717                         gnus-secondary-select-methods))))
16718     (while methods
16719       (gnus-read-descriptions-file (car methods))
16720       (setq methods (cdr methods)))
16721     t))
16722
16723 (defun gnus-read-descriptions-file (&optional method)
16724   (let ((method (or method gnus-select-method)))
16725     (when (stringp method)
16726       (setq method (gnus-server-to-method method)))
16727     ;; We create the hashtable whether we manage to read the desc file
16728     ;; to avoid trying to re-read after a failed read.
16729     (or gnus-description-hashtb
16730         (setq gnus-description-hashtb
16731               (gnus-make-hashtable (length gnus-active-hashtb))))
16732     ;; Mark this method's desc file as read.
16733     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16734                   gnus-description-hashtb)
16735
16736     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16737     (cond
16738      ((not (gnus-check-server method))
16739       (gnus-message 1 "Couldn't open server")
16740       nil)
16741      ((not (gnus-request-list-newsgroups method))
16742       (gnus-message 1 "Couldn't read newsgroups descriptions")
16743       nil)
16744      (t
16745       (let (group)
16746         (save-excursion
16747           (save-restriction
16748             (set-buffer nntp-server-buffer)
16749             (goto-char (point-min))
16750             (if (or (search-forward "\n.\n" nil t)
16751                     (goto-char (point-max)))
16752                 (progn
16753                   (beginning-of-line)
16754                   (narrow-to-region (point-min) (point))))
16755             (goto-char (point-min))
16756             (while (not (eobp))
16757               ;; If we get an error, we set group to 0, which is not a
16758               ;; symbol...
16759               (setq group
16760                     (condition-case ()
16761                         (let ((obarray gnus-description-hashtb))
16762                           ;; Group is set to a symbol interned in this
16763                           ;; hash table.
16764                           (read nntp-server-buffer))
16765                       (error 0)))
16766               (skip-chars-forward " \t")
16767               ;; ...  which leads to this line being effectively ignored.
16768               (and (symbolp group)
16769                    (set group (buffer-substring
16770                                (point) (progn (end-of-line) (point)))))
16771               (forward-line 1))))
16772         (gnus-message 5 "Reading descriptions file...done")
16773         t)))))
16774
16775 (defun gnus-group-get-description (group)
16776   "Get the description of a group by sending XGTITLE to the server."
16777   (when (gnus-request-group-description group)
16778     (save-excursion
16779       (set-buffer nntp-server-buffer)
16780       (goto-char (point-min))
16781       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16782         (match-string 1)))))
16783
16784 \f
16785 ;;;
16786 ;;; Buffering of read articles.
16787 ;;;
16788
16789 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16790 (defvar gnus-backlog-articles nil)
16791 (defvar gnus-backlog-hashtb nil)
16792
16793 (defun gnus-backlog-buffer ()
16794   "Return the backlog buffer."
16795   (or (get-buffer gnus-backlog-buffer)
16796       (save-excursion
16797         (set-buffer (get-buffer-create gnus-backlog-buffer))
16798         (buffer-disable-undo (current-buffer))
16799         (setq buffer-read-only t)
16800         (gnus-add-current-to-buffer-list)
16801         (get-buffer gnus-backlog-buffer))))
16802
16803 (defun gnus-backlog-setup ()
16804   "Initialize backlog variables."
16805   (unless gnus-backlog-hashtb
16806     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16807
16808 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16809
16810 (defun gnus-backlog-shutdown ()
16811   "Clear all backlog variables and buffers."
16812   (when (get-buffer gnus-backlog-buffer)
16813     (kill-buffer gnus-backlog-buffer))
16814   (setq gnus-backlog-hashtb nil
16815         gnus-backlog-articles nil))
16816
16817 (defun gnus-backlog-enter-article (group number buffer)
16818   (gnus-backlog-setup)
16819   (let ((ident (intern (concat group ":" (int-to-string number))
16820                        gnus-backlog-hashtb))
16821         b)
16822     (if (memq ident gnus-backlog-articles)
16823         () ; It's already kept.
16824       ;; Remove the oldest article, if necessary.
16825       (and (numberp gnus-keep-backlog)
16826            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16827            (gnus-backlog-remove-oldest-article))
16828       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16829       ;; Insert the new article.
16830       (save-excursion
16831         (set-buffer (gnus-backlog-buffer))
16832         (let (buffer-read-only)
16833           (goto-char (point-max))
16834           (or (bolp) (insert "\n"))
16835           (setq b (point))
16836           (insert-buffer-substring buffer)
16837           ;; Tag the beginning of the article with the ident.
16838           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16839
16840 (defun gnus-backlog-remove-oldest-article ()
16841   (save-excursion
16842     (set-buffer (gnus-backlog-buffer))
16843     (goto-char (point-min))
16844     (if (zerop (buffer-size))
16845         () ; The buffer is empty.
16846       (let ((ident (get-text-property (point) 'gnus-backlog))
16847             buffer-read-only)
16848         ;; Remove the ident from the list of articles.
16849         (when ident
16850           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16851         ;; Delete the article itself.
16852         (delete-region
16853          (point) (next-single-property-change
16854                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16855
16856 (defun gnus-backlog-request-article (group number buffer)
16857   (when (numberp number)
16858     (gnus-backlog-setup)
16859     (let ((ident (intern (concat group ":" (int-to-string number))
16860                          gnus-backlog-hashtb))
16861           beg end)
16862       (when (memq ident gnus-backlog-articles)
16863         ;; It was in the backlog.
16864         (save-excursion
16865           (set-buffer (gnus-backlog-buffer))
16866           (if (not (setq beg (text-property-any
16867                               (point-min) (point-max) 'gnus-backlog
16868                               ident)))
16869               ;; It wasn't in the backlog after all.
16870               (ignore
16871                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16872             ;; Find the end (i. e., the beginning of the next article).
16873             (setq end
16874                   (next-single-property-change
16875                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16876         (let ((buffer-read-only nil))
16877           (erase-buffer)
16878           (insert-buffer-substring gnus-backlog-buffer beg end)
16879           t)))))
16880
16881 ;; Allow redefinition of Gnus functions.
16882
16883 (gnus-ems-redefine)
16884
16885 (provide 'gnus)
16886
16887 ;;; gnus.el ends here