*** 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 (require 'nnmail)
36
37 (eval-when-compile (require 'cl))
38
39 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
40   "*Directory variable from which all other Gnus file variables are derived.")
41
42 ;; Site dependent variables.  These variables should be defined in
43 ;; paths.el.
44
45 (defvar gnus-default-nntp-server nil
46   "Specify a default NNTP server.
47 This variable should be defined in paths.el, and should never be set
48 by the user.
49 If you want to change servers, you should use `gnus-select-method'.
50 See the documentation to that variable.")
51
52 (defvar gnus-backup-default-subscribed-newsgroups
53   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
54   "Default default new newsgroups the first time Gnus is run.
55 Should be set in paths.el, and shouldn't be touched by the user.")
56
57 (defvar gnus-local-domain nil
58   "Local domain name without a host name.
59 The DOMAINNAME environment variable is used instead if it is defined.
60 If the `system-name' function returns the full Internet name, there is
61 no need to set this variable.")
62
63 (defvar gnus-local-organization nil
64   "String with a description of what organization (if any) the user belongs to.
65 The ORGANIZATION environment variable is used instead if it is defined.
66 If this variable contains a function, this function will be called
67 with the current newsgroup name as the argument.  The function should
68 return a string.
69
70 In any case, if the string (either in the variable, in the environment
71 variable, or returned by the function) is a file name, the contents of
72 this file will be used as the organization.")
73
74 ;; Customization variables
75
76 ;; Don't touch this variable.
77 (defvar gnus-nntp-service "nntp"
78   "*NNTP service name (\"nntp\" or 119).
79 This is an obsolete variable, which is scarcely used.  If you use an
80 nntp server for your newsgroup and want to change the port number
81 used to 899, you would say something along these lines:
82
83  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
84
85 (defvar gnus-nntpserver-file "/etc/nntpserver"
86   "*A file with only the name of the nntp server in it.")
87
88 ;; This function is used to check both the environment variable
89 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
90 ;; an nntp server name default.
91 (defun gnus-getenv-nntpserver ()
92   (or (getenv "NNTPSERVER")
93       (and (file-readable-p gnus-nntpserver-file)
94            (save-excursion
95              (set-buffer (get-buffer-create " *gnus nntp*"))
96              (buffer-disable-undo (current-buffer))
97              (insert-file-contents gnus-nntpserver-file)
98              (let ((name (buffer-string)))
99                (prog1
100                    (if (string-match "^[ \t\n]*$" name)
101                        nil
102                      name)
103                  (kill-buffer (current-buffer))))))))
104
105 (defvar gnus-select-method
106   (nconc
107    (list 'nntp (or (condition-case ()
108                        (gnus-getenv-nntpserver)
109                      (error nil))
110                    (if (and gnus-default-nntp-server
111                             (not (string= gnus-default-nntp-server "")))
112                        gnus-default-nntp-server)
113                    (system-name)))
114    (if (or (null gnus-nntp-service)
115            (equal gnus-nntp-service "nntp"))
116        nil
117      (list gnus-nntp-service)))
118   "*Default method for selecting a newsgroup.
119 This variable should be a list, where the first element is how the
120 news is to be fetched, the second is the address.
121
122 For instance, if you want to get your news via NNTP from
123 \"flab.flab.edu\", you could say:
124
125 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
126
127 If you want to use your local spool, say:
128
129 (setq gnus-select-method (list 'nnspool (system-name)))
130
131 If you use this variable, you must set `gnus-nntp-server' to nil.
132
133 There is a lot more to know about select methods and virtual servers -
134 see the manual for details.")
135
136 (defvar gnus-message-archive-method 
137   `(nnfolder
138     "archive"
139     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
140     (nnfolder-active-file 
141      ,(nnheader-concat message-directory "archive/active"))
142     (nnfolder-get-new-mail nil)
143     (nnfolder-inhibit-expiry t))
144   "*Method used for archiving messages you've sent.
145 This should be a mail method.")
146
147 (defvar gnus-refer-article-method nil
148   "*Preferred method for fetching an article by Message-ID.
149 If you are reading news from the local spool (with nnspool), fetching
150 articles by Message-ID is painfully slow.  By setting this method to an
151 nntp method, you might get acceptable results.
152
153 The value of this variable must be a valid select method as discussed
154 in the documentation of `gnus-select-method'.")
155
156 (defvar gnus-secondary-select-methods nil
157   "*A list of secondary methods that will be used for reading news.
158 This is a list where each element is a complete select method (see
159 `gnus-select-method').
160
161 If, for instance, you want to read your mail with the nnml backend,
162 you could set this variable:
163
164 (setq gnus-secondary-select-methods '((nnml \"\")))")
165
166 (defvar gnus-secondary-servers nil
167   "*List of NNTP servers that the user can choose between interactively.
168 To make Gnus query you for a server, you have to give `gnus' a
169 non-numeric prefix - `C-u M-x gnus', in short.")
170
171 (defvar gnus-nntp-server nil
172   "*The name of the host running the NNTP server.
173 This variable is semi-obsolete.  Use the `gnus-select-method'
174 variable instead.")
175
176 (defvar gnus-startup-file "~/.newsrc"
177   "*Your `.newsrc' file.
178 `.newsrc-SERVER' will be used instead if that exists.")
179
180 (defvar gnus-init-file "~/.gnus"
181   "*Your Gnus elisp startup file.
182 If a file with the .el or .elc suffixes exist, it will be read
183 instead.")
184
185 (defvar gnus-group-faq-directory
186   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
187     "/ftp@sunsite.auc.dk:/pub/usenet/"
188     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
189     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
190     "/ftp@rtfm.mit.edu:/pub/usenet/"
191     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
192     "/ftp@ftp.sunet.se:/pub/usenet/"
193     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
194     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
195     "/ftp@ftp.hk.super.net:/mirror/faqs/")
196   "*Directory where the group FAQs are stored.
197 This will most commonly be on a remote machine, and the file will be
198 fetched by ange-ftp.
199
200 This variable can also be a list of directories.  In that case, the
201 first element in the list will be used by default, and the others will
202 be used as backup sites.
203
204 Note that Gnus uses an aol machine as the default directory.  If this
205 feels fundamentally unclean, just think of it as a way to finally get
206 something of value back from them.
207
208 If the default site is too slow, try one of these:
209
210    North America: mirrors.aol.com                /pub/rtfm/usenet
211                   ftp.seas.gwu.edu               /pub/rtfm
212                   rtfm.mit.edu                   /pub/usenet
213    Europe:        ftp.uni-paderborn.de           /pub/FAQ
214                   src.doc.ic.ac.uk               /usenet/news-FAQS
215                   ftp.sunet.se                   /pub/usenet
216                   sunsite.auc.dk                 /pub/usenet
217    Asia:          nctuccca.edu.tw                /USENET/FAQ
218                   hwarang.postech.ac.kr          /pub/usenet
219                   ftp.hk.super.net               /mirror/faqs")
220
221 (defvar gnus-group-archive-directory
222   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
223   "*The address of the (ding) archives.")
224
225 (defvar gnus-group-recent-archive-directory
226   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
227   "*The address of the most recent (ding) articles.")
228
229 (defvar gnus-default-subscribed-newsgroups nil
230   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
231 It should be a list of strings.
232 If it is `t', Gnus will not do anything special the first time it is
233 started; it'll just use the normal newsgroups subscription methods.")
234
235 (defvar gnus-use-cross-reference t
236   "*Non-nil means that cross referenced articles will be marked as read.
237 If nil, ignore cross references.  If t, mark articles as read in
238 subscribed newsgroups.  If neither t nor nil, mark as read in all
239 newsgroups.")
240
241 (defvar gnus-single-article-buffer t
242   "*If non-nil, display all articles in the same buffer.
243 If nil, each group will get its own article buffer.")
244
245 (defvar gnus-use-dribble-file t
246   "*Non-nil means that Gnus will use a dribble file to store user updates.
247 If Emacs should crash without saving the .newsrc files, complete
248 information can be restored from the dribble file.")
249
250 (defvar gnus-dribble-directory nil
251   "*The directory where dribble files will be saved.
252 If this variable is nil, the directory where the .newsrc files are
253 saved will be used.")
254
255 (defvar gnus-asynchronous nil
256   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
257
258 (defvar gnus-kill-summary-on-exit t
259   "*If non-nil, kill the summary buffer when you exit from it.
260 If nil, the summary will become a \"*Dead Summary*\" buffer, and
261 it will be killed sometime later.")
262
263 (defvar gnus-large-newsgroup 200
264   "*The number of articles which indicates a large newsgroup.
265 If the number of articles in a newsgroup is greater than this value,
266 confirmation is required for selecting the newsgroup.")
267
268 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
269 (defvar gnus-no-groups-message "No news is horrible news"
270   "*Message displayed by Gnus when no groups are available.")
271
272 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
273   "*Non-nil means that the default name of a file to save articles in is the group name.
274 If it's nil, the directory form of the group name is used instead.
275
276 If this variable is a list, and the list contains the element
277 `not-score', long file names will not be used for score files; if it
278 contains the element `not-save', long file names will not be used for
279 saving; and if it contains the element `not-kill', long file names
280 will not be used for kill files.")
281
282 (defvar gnus-article-save-directory gnus-directory
283   "*Name of the directory articles will be saved in (default \"~/News\").")
284
285 (defvar gnus-kill-files-directory gnus-directory
286   "*Name of the directory where kill files will be stored (default \"~/News\").")
287
288 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
289   "*A function to save articles in your favorite format.
290 The function must be interactively callable (in other words, it must
291 be an Emacs command).
292
293 Gnus provides the following functions:
294
295 * gnus-summary-save-in-rmail (Rmail format)
296 * gnus-summary-save-in-mail (Unix mail format)
297 * gnus-summary-save-in-folder (MH folder)
298 * gnus-summary-save-in-file (article format).
299 * gnus-summary-save-in-vm (use VM's folder format).")
300
301 (defvar gnus-prompt-before-saving 'always
302   "*This variable says how much prompting is to be done when saving articles.
303 If it is nil, no prompting will be done, and the articles will be
304 saved to the default files.  If this variable is `always', each and
305 every article that is saved will be preceded by a prompt, even when
306 saving large batches of articles.  If this variable is neither nil not
307 `always', there the user will be prompted once for a file name for
308 each invocation of the saving commands.")
309
310 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
311   "*A function generating a file name to save articles in Rmail format.
312 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
313
314 (defvar gnus-mail-save-name (function gnus-plain-save-name)
315   "*A function generating a file name to save articles in Unix mail format.
316 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
317
318 (defvar gnus-folder-save-name (function gnus-folder-save-name)
319   "*A function generating a file name to save articles in MH folder.
320 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
321
322 (defvar gnus-file-save-name (function gnus-numeric-save-name)
323   "*A function generating a file name to save articles in article format.
324 The function is called with NEWSGROUP, HEADERS, and optional
325 LAST-FILE.")
326
327 (defvar gnus-split-methods
328   '((gnus-article-archive-name))
329   "*Variable used to suggest where articles are to be saved.
330 For instance, if you would like to save articles related to Gnus in
331 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
332 you could set this variable to something like:
333
334  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
335    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
336
337 This variable is an alist where the where the key is the match and the
338 value is a list of possible files to save in if the match is non-nil.
339
340 If the match is a string, it is used as a regexp match on the
341 article.  If the match is a symbol, that symbol will be funcalled
342 from the buffer of the article to be saved with the newsgroup as the
343 parameter.  If it is a list, it will be evaled in the same buffer.
344
345 If this form or function returns a string, this string will be used as
346 a possible file name; and if it returns a non-nil list, that list will
347 be used as possible file names.")
348
349 (defvar gnus-move-split-methods nil
350   "*Variable used to suggest where articles are to be moved to.
351 It uses the same syntax as the `gnus-split-methods' variable.")
352
353 (defvar gnus-save-score nil
354   "*If non-nil, save group scoring info.")
355
356 (defvar gnus-use-adaptive-scoring nil
357   "*If non-nil, use some adaptive scoring scheme.")
358
359 (defvar gnus-use-cache 'passive
360   "*If nil, Gnus will ignore the article cache.
361 If `passive', it will allow entering (and reading) articles
362 explicitly entered into the cache.  If anything else, use the
363 cache to the full extent of the law.")
364
365 (defvar gnus-use-trees nil
366   "*If non-nil, display a thread tree buffer.")
367
368 (defvar gnus-use-grouplens nil
369   "*If non-nil, use GroupLens ratings.")
370
371 (defvar gnus-keep-backlog nil
372   "*If non-nil, Gnus will keep read articles for later re-retrieval.
373 If it is a number N, then Gnus will only keep the last N articles
374 read.  If it is neither nil nor a number, Gnus will keep all read
375 articles.  This is not a good idea.")
376
377 (defvar gnus-use-nocem nil
378   "*If non-nil, Gnus will read NoCeM cancel messages.")
379
380 (defvar gnus-use-demon nil
381   "If non-nil, Gnus might use some demons.")
382
383 (defvar gnus-use-scoring t
384   "*If non-nil, enable scoring.")
385
386 (defvar gnus-use-picons nil
387   "*If non-nil, display picons.")
388
389 (defvar gnus-fetch-old-headers nil
390   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
391 If an unread article in the group refers to an older, already read (or
392 just marked as read) article, the old article will not normally be
393 displayed in the Summary buffer.  If this variable is non-nil, Gnus
394 will attempt to grab the headers to the old articles, and thereby
395 build complete threads.  If it has the value `some', only enough
396 headers to connect otherwise loose threads will be displayed.
397 This variable can also be a number.  In that case, no more than that
398 number of old headers will be fetched.
399
400 The server has to support NOV for any of this to work.")
401
402 ;see gnus-cus.el
403 ;(defvar gnus-visual t
404 ;  "*If non-nil, will do various highlighting.
405 ;If nil, no mouse highlights (or any other highlights) will be
406 ;performed.  This might speed up Gnus some when generating large group
407 ;and summary buffers.")
408
409 (defvar gnus-novice-user t
410   "*Non-nil means that you are a usenet novice.
411 If non-nil, verbose messages may be displayed and confirmations may be
412 required.")
413
414 (defvar gnus-expert-user nil
415   "*Non-nil means that you will never be asked for confirmation about anything.
416 And that means *anything*.")
417
418 (defvar gnus-verbose 7
419   "*Integer that says how verbose Gnus should be.
420 The higher the number, the more messages Gnus will flash to say what
421 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
422 display most important messages; and at ten, Gnus will keep on
423 jabbering all the time.")
424
425 (defvar gnus-keep-same-level nil
426   "*Non-nil means that the next newsgroup after the current will be on the same level.
427 When you type, for instance, `n' after reading the last article in the
428 current newsgroup, you will go to the next newsgroup.  If this variable
429 is nil, the next newsgroup will be the next from the group
430 buffer.
431 If this variable is non-nil, Gnus will either put you in the
432 next newsgroup with the same level, or, if no such newsgroup is
433 available, the next newsgroup with the lowest possible level higher
434 than the current level.
435 If this variable is `best', Gnus will make the next newsgroup the one
436 with the best level.")
437
438 (defvar gnus-summary-make-false-root 'adopt
439   "*nil means that Gnus won't gather loose threads.
440 If the root of a thread has expired or been read in a previous
441 session, the information necessary to build a complete thread has been
442 lost.  Instead of having many small sub-threads from this original thread
443 scattered all over the summary buffer, Gnus can gather them.
444
445 If non-nil, Gnus will try to gather all loose sub-threads from an
446 original thread into one large thread.
447
448 If this variable is non-nil, it should be one of `none', `adopt',
449 `dummy' or `empty'.
450
451 If this variable is `none', Gnus will not make a false root, but just
452 present the sub-threads after another.
453 If this variable is `dummy', Gnus will create a dummy root that will
454 have all the sub-threads as children.
455 If this variable is `adopt', Gnus will make one of the \"children\"
456 the parent and mark all the step-children as such.
457 If this variable is `empty', the \"children\" are printed with empty
458 subject fields.  (Or rather, they will be printed with a string
459 given by the `gnus-summary-same-subject' variable.)")
460
461 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
462   "*A regexp to match subjects to be excluded from loose thread gathering.
463 As loose thread gathering is done on subjects only, that means that
464 there can be many false gatherings performed.  By rooting out certain
465 common subjects, gathering might become saner.")
466
467 (defvar gnus-summary-gather-subject-limit nil
468   "*Maximum length of subject comparisons when gathering loose threads.
469 Use nil to compare full subjects.  Setting this variable to a low
470 number will help gather threads that have been corrupted by
471 newsreaders chopping off subject lines, but it might also mean that
472 unrelated articles that have subject that happen to begin with the
473 same few characters will be incorrectly gathered.
474
475 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
476 comparing subjects.")
477
478 (defvar gnus-simplify-ignored-prefixes nil
479   "*Regexp, matches for which are removed from subject lines when simplifying.")
480
481 (defvar gnus-build-sparse-threads nil
482   "*If non-nil, fill in the gaps in threads.
483 If `some', only fill in the gaps that are needed to tie loose threads
484 together.  If `more', fill in all leaf nodes that Gnus can find.  If
485 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
486
487 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
488   "Function used for gathering loose threads.
489 There are two pre-defined functions: `gnus-gather-threads-by-subject',
490 which only takes Subjects into consideration; and
491 `gnus-gather-threads-by-references', which compared the References
492 headers of the articles to find matches.")
493
494 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
495 (defvar gnus-summary-same-subject ""
496   "*String indicating that the current article has the same subject as the previous.
497 This variable will only be used if the value of
498 `gnus-summary-make-false-root' is `empty'.")
499
500 (defvar gnus-summary-goto-unread t
501   "*If non-nil, marking commands will go to the next unread article.
502 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
503 whether it is read or not.")
504
505 (defvar gnus-group-goto-unread t
506   "*If non-nil, movement commands will go to the next unread and subscribed group.")
507
508 (defvar gnus-goto-next-group-when-activating t
509   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
510
511 (defvar gnus-check-new-newsgroups t
512   "*Non-nil means that Gnus will add new newsgroups at startup.
513 If this variable is `ask-server', Gnus will ask the server for new
514 groups since the last time it checked.  This means that the killed list
515 is no longer necessary, so you could set `gnus-save-killed-list' to
516 nil.
517
518 A variant is to have this variable be a list of select methods.  Gnus
519 will then use the `ask-server' method on all these select methods to
520 query for new groups from all those servers.
521
522 Eg.
523   (setq gnus-check-new-newsgroups
524         '((nntp \"some.server\") (nntp \"other.server\")))
525
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
528
529 (defvar gnus-check-bogus-newsgroups nil
530   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
531 If this variable is nil, then you have to tell Gnus explicitly to
532 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
533
534 (defvar gnus-read-active-file t
535   "*Non-nil means that Gnus will read the entire active file at startup.
536 If this variable is nil, Gnus will only know about the groups in your
537 `.newsrc' file.
538
539 If this variable is `some', Gnus will try to only read the relevant
540 parts of the active file from the server.  Not all servers support
541 this, and it might be quite slow with other servers, but this should
542 generally be faster than both the t and nil value.
543
544 If you set this variable to nil or `some', you probably still want to
545 be told about new newsgroups that arrive.  To do that, set
546 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
547 properly with all servers.")
548
549 (defvar gnus-level-subscribed 5
550   "*Groups with levels less than or equal to this variable are subscribed.")
551
552 (defvar gnus-level-unsubscribed 7
553   "*Groups with levels less than or equal to this variable are unsubscribed.
554 Groups with levels less than `gnus-level-subscribed', which should be
555 less than this variable, are subscribed.")
556
557 (defvar gnus-level-zombie 8
558   "*Groups with this level are zombie groups.")
559
560 (defvar gnus-level-killed 9
561   "*Groups with this level are killed.")
562
563 (defvar gnus-level-default-subscribed 3
564   "*New subscribed groups will be subscribed at this level.")
565
566 (defvar gnus-level-default-unsubscribed 6
567   "*New unsubscribed groups will be unsubscribed at this level.")
568
569 (defvar gnus-activate-level (1+ gnus-level-subscribed)
570   "*Groups higher than this level won't be activated on startup.
571 Setting this variable to something log might save lots of time when
572 you have many groups that you aren't interested in.")
573
574 (defvar gnus-activate-foreign-newsgroups 4
575   "*If nil, Gnus will not check foreign newsgroups at startup.
576 If it is non-nil, it should be a number between one and nine.  Foreign
577 newsgroups that have a level lower or equal to this number will be
578 activated on startup.  For instance, if you want to active all
579 subscribed newsgroups, but not the rest, you'd set this variable to
580 `gnus-level-subscribed'.
581
582 If you subscribe to lots of newsgroups from different servers, startup
583 might take a while.  By setting this variable to nil, you'll save time,
584 but you won't be told how many unread articles there are in the
585 groups.")
586
587 (defvar gnus-save-newsrc-file t
588   "*Non-nil means that Gnus will save the `.newsrc' file.
589 Gnus always saves its own startup file, which is called
590 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
591 be readily understood by other newsreaders.  If you don't plan on
592 using other newsreaders, set this variable to nil to save some time on
593 exit.")
594
595 (defvar gnus-save-killed-list t
596   "*If non-nil, save the list of killed groups to the startup file.
597 If you set this variable to nil, you'll save both time (when starting
598 and quitting) and space (both memory and disk), but it will also mean
599 that Gnus has no record of which groups are new and which are old, so
600 the automatic new newsgroups subscription methods become meaningless.
601
602 You should always set `gnus-check-new-newsgroups' to `ask-server' or
603 nil if you set this variable to nil.")
604
605 (defvar gnus-interactive-catchup t
606   "*If non-nil, require your confirmation when catching up a group.")
607
608 (defvar gnus-interactive-exit t
609   "*If non-nil, require your confirmation when exiting Gnus.")
610
611 (defvar gnus-kill-killed t
612   "*If non-nil, Gnus will apply kill files to already killed articles.
613 If it is nil, Gnus will never apply kill files to articles that have
614 already been through the scoring process, which might very well save lots
615 of time.")
616
617 (defvar gnus-extract-address-components 'gnus-extract-address-components
618   "*Function for extracting address components from a From header.
619 Two pre-defined function exist: `gnus-extract-address-components',
620 which is the default, quite fast, and too simplistic solution, and
621 `mail-extract-address-components', which works much better, but is
622 slower.")
623
624 (defvar gnus-summary-default-score 0
625   "*Default article score level.
626 If this variable is nil, scoring will be disabled.")
627
628 (defvar gnus-summary-zcore-fuzz 0
629   "*Fuzziness factor for the zcore in the summary buffer.
630 Articles with scores closer than this to `gnus-summary-default-score'
631 will not be marked.")
632
633 (defvar gnus-simplify-subject-fuzzy-regexp nil
634   "*Strings to be removed when doing fuzzy matches.
635 This can either be a regular expression or list of regular expressions
636 that will be removed from subject strings if fuzzy subject
637 simplification is selected.")
638
639 (defvar gnus-permanently-visible-groups nil
640   "*Regexp to match groups that should always be listed in the group buffer.
641 This means that they will still be listed when there are no unread
642 articles in the groups.")
643
644 (defvar gnus-list-groups-with-ticked-articles t
645   "*If non-nil, list groups that have only ticked articles.
646 If nil, only list groups that have unread articles.")
647
648 (defvar gnus-group-default-list-level gnus-level-subscribed
649   "*Default listing level.
650 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
651
652 (defvar gnus-group-use-permanent-levels nil
653   "*If non-nil, once you set a level, Gnus will use this level.")
654
655 (defvar gnus-group-list-inactive-groups t
656   "*If non-nil, inactive groups will be listed.")
657
658 (defvar gnus-show-mime nil
659   "*If non-nil, do mime processing of articles.
660 The articles will simply be fed to the function given by
661 `gnus-show-mime-method'.")
662
663 (defvar gnus-strict-mime t
664   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
665
666 (defvar gnus-show-mime-method 'metamail-buffer
667   "*Function to process a MIME message.
668 The function is called from the article buffer.")
669
670 (defvar gnus-decode-encoded-word-method (lambda ())
671   "*Function to decode a MIME encoded-words.
672 The function is called from the article buffer.")
673
674 (defvar gnus-show-threads t
675   "*If non-nil, display threads in summary mode.")
676
677 (defvar gnus-thread-hide-subtree nil
678   "*If non-nil, hide all threads initially.
679 If threads are hidden, you have to run the command
680 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
681 to expose hidden threads.")
682
683 (defvar gnus-thread-hide-killed t
684   "*If non-nil, hide killed threads automatically.")
685
686 (defvar gnus-thread-ignore-subject nil
687   "*If non-nil, ignore subjects and do all threading based on the Reference header.
688 If nil, which is the default, articles that have different subjects
689 from their parents will start separate threads.")
690
691 (defvar gnus-thread-operation-ignore-subject t
692   "*If non-nil, subjects will be ignored when doing thread commands.
693 This affects commands like `gnus-summary-kill-thread' and
694 `gnus-summary-lower-thread'.
695
696 If this variable is nil, articles in the same thread with different
697 subjects will not be included in the operation in question.  If this
698 variable is `fuzzy', only articles that have subjects that are fuzzily
699 equal will be included.")
700
701 (defvar gnus-thread-indent-level 4
702   "*Number that says how much each sub-thread should be indented.")
703
704 (defvar gnus-ignored-newsgroups
705   (purecopy (mapconcat 'identity
706                        '("^to\\."       ; not "real" groups
707                          "^[0-9. \t]+ " ; all digits in name
708                          "[][\"#'()]"   ; bogus characters
709                          )
710                        "\\|"))
711   "*A regexp to match uninteresting newsgroups in the active file.
712 Any lines in the active file matching this regular expression are
713 removed from the newsgroup list before anything else is done to it,
714 thus making them effectively non-existent.")
715
716 (defvar gnus-ignored-headers
717   "^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:"
718   "*All headers that match this regexp will be hidden.
719 This variable can also be a list of regexps of headers to be ignored.
720 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
721
722 (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-"
723   "*All headers that do not match this regexp will be hidden.
724 This variable can also be a list of regexp of headers to remain visible.
725 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
726
727 (defvar gnus-sorted-header-list
728   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
729     "^Cc:" "^Date:" "^Organization:")
730   "*This variable is a list of regular expressions.
731 If it is non-nil, headers that match the regular expressions will
732 be placed first in the article buffer in the sequence specified by
733 this list.")
734
735 (defvar gnus-boring-article-headers
736   '(empty followup-to reply-to)
737   "*Headers that are only to be displayed if they have interesting data.
738 Possible values in this list are `empty', `newsgroups', `followup-to',
739 `reply-to', and `date'.")
740
741 (defvar gnus-show-all-headers nil
742   "*If non-nil, don't hide any headers.")
743
744 (defvar gnus-save-all-headers t
745   "*If non-nil, don't remove any headers before saving.")
746
747 (defvar gnus-saved-headers gnus-visible-headers
748   "*Headers to keep if `gnus-save-all-headers' is nil.
749 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
750 If that variable is nil, however, all headers that match this regexp
751 will be kept while the rest will be deleted before saving.")
752
753 (defvar gnus-inhibit-startup-message nil
754   "*If non-nil, the startup message will not be displayed.")
755
756 (defvar gnus-signature-separator "^-- *$"
757   "Regexp matching signature separator.")
758
759 (defvar gnus-signature-limit nil
760   "Provide a limit to what is considered a signature.
761 If it is a number, no signature may not be longer (in characters) than
762 that number.  If it is a function, the function will be called without
763 any parameters, and if it returns nil, there is no signature in the
764 buffer.  If it is a string, it will be used as a regexp.  If it
765 matches, the text in question is not a signature.")
766
767 (defvar gnus-auto-extend-newsgroup t
768   "*If non-nil, extend newsgroup forward and backward when requested.")
769
770 (defvar gnus-auto-select-first t
771   "*If nil, don't select the first unread article when entering a group.
772 If this variable is `best', select the highest-scored unread article
773 in the group.  If neither nil nor `best', select the first unread
774 article.
775
776 If you want to prevent automatic selection of the first unread article
777 in some newsgroups, set the variable to nil in
778 `gnus-select-group-hook'.")
779
780 (defvar gnus-auto-select-next t
781   "*If non-nil, offer to go to the next group from the end of the previous.
782 If the value is t and the next newsgroup is empty, Gnus will exit
783 summary mode and go back to group mode.  If the value is neither nil
784 nor t, Gnus will select the following unread newsgroup.  In
785 particular, if the value is the symbol `quietly', the next unread
786 newsgroup will be selected without any confirmation, and if it is
787 `almost-quietly', the next group will be selected without any
788 confirmation if you are located on the last article in the group.
789 Finally, if this variable is `slightly-quietly', the `Z n' command
790 will go to the next group without confirmation.")
791
792 (defvar gnus-auto-select-same nil
793   "*If non-nil, select the next article with the same subject.")
794
795 (defvar gnus-summary-check-current nil
796   "*If non-nil, consider the current article when moving.
797 The \"unread\" movement commands will stay on the same line if the
798 current article is unread.")
799
800 (defvar gnus-auto-center-summary t
801   "*If non-nil, always center the current summary buffer.
802 In particular, if `vertical' do only vertical recentering.  If non-nil
803 and non-`vertical', do both horizontal and vertical recentering.")
804
805 (defvar gnus-break-pages t
806   "*If non-nil, do page breaking on articles.
807 The page delimiter is specified by the `gnus-page-delimiter'
808 variable.")
809
810 (defvar gnus-page-delimiter "^\^L"
811   "*Regexp describing what to use as article page delimiters.
812 The default value is \"^\^L\", which is a form linefeed at the
813 beginning of a line.")
814
815 (defvar gnus-use-full-window t
816   "*If non-nil, use the entire Emacs screen.")
817
818 (defvar gnus-window-configuration nil
819   "Obsolete variable.  See `gnus-buffer-configuration'.")
820
821 (defvar gnus-window-min-width 2
822   "*Minimum width of Gnus buffers.")
823
824 (defvar gnus-window-min-height 1
825   "*Minimum height of Gnus buffers.")
826
827 (defvar gnus-buffer-configuration
828   '((group
829      (vertical 1.0
830                (group 1.0 point)
831                (if gnus-carpal '(group-carpal 4))))
832     (summary
833      (vertical 1.0
834                (summary 1.0 point)
835                (if gnus-carpal '(summary-carpal 4))))
836     (article
837      (cond 
838       (gnus-use-picons
839        '(frame 1.0
840                (vertical 1.0
841                          (summary 0.25 point)
842                          (if gnus-carpal '(summary-carpal 4))
843                          (article 1.0))
844                (vertical ((height . 5) (width . 15)
845                           (user-position . t)
846                           (left . -1) (top . 1))
847                          (picons 1.0))))
848       (gnus-use-trees
849        '(vertical 1.0
850                   (summary 0.25 point)
851                   (tree 0.25)
852                   (article 1.0)))
853       (t
854        '(vertical 1.0
855                  (summary 0.25 point)
856                  (if gnus-carpal '(summary-carpal 4))
857                  (if gnus-use-trees '(tree 0.25))
858                  (article 1.0)))))
859     (server
860      (vertical 1.0
861                (server 1.0 point)
862                (if gnus-carpal '(server-carpal 2))))
863     (browse
864      (vertical 1.0
865                (browse 1.0 point)
866                (if gnus-carpal '(browse-carpal 2))))
867     (message
868      (vertical 1.0
869                (message 1.0 point)))
870     (pick
871      (vertical 1.0
872                (article 1.0 point)))
873     (info
874      (vertical 1.0
875                (info 1.0 point)))
876     (summary-faq
877      (vertical 1.0
878                (summary 0.25)
879                (faq 1.0 point)))
880     (edit-group
881      (vertical 1.0
882                (group 0.5)
883                (edit-group 1.0 point)))
884     (edit-server
885      (vertical 1.0
886                (server 0.5)
887                (edit-server 1.0 point)))
888     (edit-score
889      (vertical 1.0
890                (summary 0.25)
891                (edit-score 1.0 point)))
892     (post
893      (vertical 1.0
894                (post 1.0 point)))
895     (reply
896      (vertical 1.0
897                (article-copy 0.5)
898                (message 1.0 point)))
899     (forward
900      (vertical 1.0
901                (message 1.0 point)))
902     (reply-yank
903      (vertical 1.0
904                (message 1.0 point)))
905     (mail-bounce
906      (vertical 1.0
907                (article 0.5)
908                (message 1.0 point)))
909     (draft
910      (vertical 1.0
911                (draft 1.0 point)))
912     (pipe
913      (vertical 1.0
914                (summary 0.25 point)
915                (if gnus-carpal '(summary-carpal 4))
916                ("*Shell Command Output*" 1.0)))
917     (bug
918      (vertical 1.0
919                ("*Gnus Help Bug*" 0.5)
920                ("*Gnus Bug*" 1.0 point)))
921     (compose-bounce
922      (vertical 1.0
923                (article 0.5)
924                (message 1.0 point))))
925   "Window configuration for all possible Gnus buffers.
926 This variable is a list of lists.  Each of these lists has a NAME and
927 a RULE.  The NAMEs are commonsense names like `group', which names a
928 rule used when displaying the group buffer; `summary', which names a
929 rule for what happens when you enter a group and do not display an
930 article buffer; and so on.  See the value of this variable for a
931 complete list of NAMEs.
932
933 Each RULE is a list of vectors.  The first element in this vector is
934 the name of the buffer to be displayed; the second element is the
935 percentage of the screen this buffer is to occupy (a number in the
936 0.0-0.99 range); the optional third element is `point', which should
937 be present to denote which buffer point is to go to after making this
938 buffer configuration.")
939
940 (defvar gnus-window-to-buffer
941   '((group . gnus-group-buffer)
942     (summary . gnus-summary-buffer)
943     (article . gnus-article-buffer)
944     (server . gnus-server-buffer)
945     (browse . "*Gnus Browse Server*")
946     (edit-group . gnus-group-edit-buffer)
947     (edit-server . gnus-server-edit-buffer)
948     (group-carpal . gnus-carpal-group-buffer)
949     (summary-carpal . gnus-carpal-summary-buffer)
950     (server-carpal . gnus-carpal-server-buffer)
951     (browse-carpal . gnus-carpal-browse-buffer)
952     (edit-score . gnus-score-edit-buffer)
953     (message . gnus-message-buffer)
954     (mail . gnus-message-buffer)
955     (post-news . gnus-message-buffer)
956     (faq . gnus-faq-buffer)
957     (picons . "*Picons*")
958     (tree . gnus-tree-buffer)
959     (info . gnus-info-buffer)
960     (article-copy . gnus-article-copy)
961     (draft . gnus-draft-buffer))
962   "Mapping from short symbols to buffer names or buffer variables.")
963
964 (defvar gnus-carpal nil
965   "*If non-nil, display clickable icons.")
966
967 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
968   "*Function called with a group name when new group is detected.
969 A few pre-made functions are supplied: `gnus-subscribe-randomly'
970 inserts new groups at the beginning of the list of groups;
971 `gnus-subscribe-alphabetically' inserts new groups in strict
972 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
973 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
974 for your decision; `gnus-subscribe-killed' kills all new groups;
975 `gnus-subscribe-zombies' will make all new groups into zombies.")
976
977 ;; Suggested by a bug report by Hallvard B Furuseth.
978 ;; <h.b.furuseth@usit.uio.no>.
979 (defvar gnus-subscribe-options-newsgroup-method
980   (function gnus-subscribe-alphabetically)
981   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
982 If, for instance, you want to subscribe to all newsgroups in the
983 \"no\" and \"alt\" hierarchies, you'd put the following in your
984 .newsrc file:
985
986 options -n no.all alt.all
987
988 Gnus will the subscribe all new newsgroups in these hierarchies with
989 the subscription method in this variable.")
990
991 (defvar gnus-subscribe-hierarchical-interactive nil
992   "*If non-nil, Gnus will offer to subscribe hierarchically.
993 When a new hierarchy appears, Gnus will ask the user:
994
995 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
996
997 If the user pressed `d', Gnus will descend the hierarchy, `y' will
998 subscribe to all newsgroups in the hierarchy and `s' will skip this
999 hierarchy in its entirety.")
1000
1001 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1002   "*Function used for sorting the group buffer.
1003 This function will be called with group info entries as the arguments
1004 for the groups to be sorted.  Pre-made functions include
1005 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1006 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1007 `gnus-group-sort-by-rank'.
1008
1009 This variable can also be a list of sorting functions.  In that case,
1010 the most significant sort function should be the last function in the
1011 list.")
1012
1013 ;; Mark variables suggested by Thomas Michanek
1014 ;; <Thomas.Michanek@telelogic.se>.
1015 (defvar gnus-unread-mark ? 
1016   "*Mark used for unread articles.")
1017 (defvar gnus-ticked-mark ?!
1018   "*Mark used for ticked articles.")
1019 (defvar gnus-dormant-mark ??
1020   "*Mark used for dormant articles.")
1021 (defvar gnus-del-mark ?r
1022   "*Mark used for del'd articles.")
1023 (defvar gnus-read-mark ?R
1024   "*Mark used for read articles.")
1025 (defvar gnus-expirable-mark ?E
1026   "*Mark used for expirable articles.")
1027 (defvar gnus-killed-mark ?K
1028   "*Mark used for killed articles.")
1029 (defvar gnus-souped-mark ?F
1030   "*Mark used for killed articles.")
1031 (defvar gnus-kill-file-mark ?X
1032   "*Mark used for articles killed by kill files.")
1033 (defvar gnus-low-score-mark ?Y
1034   "*Mark used for articles with a low score.")
1035 (defvar gnus-catchup-mark ?C
1036   "*Mark used for articles that are caught up.")
1037 (defvar gnus-replied-mark ?A
1038   "*Mark used for articles that have been replied to.")
1039 (defvar gnus-cached-mark ?*
1040   "*Mark used for articles that are in the cache.")
1041 (defvar gnus-saved-mark ?S
1042   "*Mark used for articles that have been saved to.")
1043 (defvar gnus-process-mark ?#
1044   "*Process mark.")
1045 (defvar gnus-ancient-mark ?O
1046   "*Mark used for ancient articles.")
1047 (defvar gnus-sparse-mark ?Q
1048   "*Mark used for sparsely reffed articles.")
1049 (defvar gnus-canceled-mark ?G
1050   "*Mark used for canceled articles.")
1051 (defvar gnus-score-over-mark ?+
1052   "*Score mark used for articles with high scores.")
1053 (defvar gnus-score-below-mark ?-
1054   "*Score mark used for articles with low scores.")
1055 (defvar gnus-empty-thread-mark ? 
1056   "*There is no thread under the article.")
1057 (defvar gnus-not-empty-thread-mark ?=
1058   "*There is a thread under the article.")
1059
1060 (defvar gnus-view-pseudo-asynchronously nil
1061   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1062
1063 (defvar gnus-view-pseudos nil
1064   "*If `automatic', pseudo-articles will be viewed automatically.
1065 If `not-confirm', pseudos will be viewed automatically, and the user
1066 will not be asked to confirm the command.")
1067
1068 (defvar gnus-view-pseudos-separately t
1069   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1070 If nil, all files that use the same viewing command will be given as a
1071 list of parameters to that command.")
1072
1073 (defvar gnus-insert-pseudo-articles t
1074   "*If non-nil, insert pseudo-articles when decoding articles.")
1075
1076 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1077   "*Format of group lines.
1078 It works along the same lines as a normal formatting string,
1079 with some simple extensions.
1080
1081 %M    Only marked articles (character, \"*\" or \" \")
1082 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1083 %L    Level of subscribedness (integer)
1084 %N    Number of unread articles (integer)
1085 %I    Number of dormant articles (integer)
1086 %i    Number of ticked and dormant (integer)
1087 %T    Number of ticked articles (integer)
1088 %R    Number of read articles (integer)
1089 %t    Total number of articles (integer)
1090 %y    Number of unread, unticked articles (integer)
1091 %G    Group name (string)
1092 %g    Qualified group name (string)
1093 %D    Group description (string)
1094 %s    Select method (string)
1095 %o    Moderated group (char, \"m\")
1096 %p    Process mark (char)
1097 %O    Moderated group (string, \"(m)\" or \"\")
1098 %P    Topic indentation (string)
1099 %l    Whether there are GroupLens predictions for this group (string)
1100 %n    Select from where (string)
1101 %z    A string that look like `<%s:%n>' if a foreign select method is used
1102 %u    User defined specifier.  The next character in the format string should
1103       be a letter.  Gnus will call the function gnus-user-format-function-X,
1104       where X is the letter following %u.  The function will be passed the
1105       current header as argument.  The function should return a string, which
1106       will be inserted into the buffer just like information from any other
1107       group specifier.
1108
1109 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1110 the mouse point move inside the area.  There can only be one such area.
1111
1112 Note that this format specification is not always respected.  For
1113 reasons of efficiency, when listing killed groups, this specification
1114 is ignored altogether.  If the spec is changed considerably, your
1115 output may end up looking strange when listing both alive and killed
1116 groups.
1117
1118 If you use %o or %O, reading the active file will be slower and quite
1119 a bit of extra memory will be used. %D will also worsen performance.
1120 Also note that if you change the format specification to include any
1121 of these specs, you must probably re-start Gnus to see them go into
1122 effect.")
1123
1124 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1125   "*The format specification of the lines in the summary buffer.
1126
1127 It works along the same lines as a normal formatting string,
1128 with some simple extensions.
1129
1130 %N   Article number, left padded with spaces (string)
1131 %S   Subject (string)
1132 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1133 %n   Name of the poster (string)
1134 %a   Extracted name of the poster (string)
1135 %A   Extracted address of the poster (string)
1136 %F   Contents of the From: header (string)
1137 %x   Contents of the Xref: header (string)
1138 %D   Date of the article (string)
1139 %d   Date of the article (string) in DD-MMM format
1140 %M   Message-id of the article (string)
1141 %r   References of the article (string)
1142 %c   Number of characters in the article (integer)
1143 %L   Number of lines in the article (integer)
1144 %I   Indentation based on thread level (a string of spaces)
1145 %T   A string with two possible values: 80 spaces if the article
1146      is on thread level two or larger and 0 spaces on level one
1147 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1148 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1149 %[   Opening bracket (character, \"[\" or \"<\")
1150 %]   Closing bracket (character, \"]\" or \">\")
1151 %>   Spaces of length thread-level (string)
1152 %<   Spaces of length (- 20 thread-level) (string)
1153 %i   Article score (number)
1154 %z   Article zcore (character)
1155 %t   Number of articles under the current thread (number).
1156 %e   Whether the thread is empty or not (character).
1157 %l   GroupLens score (string).
1158 %u   User defined specifier.  The next character in the format string should
1159      be a letter.  Gnus will call the function gnus-user-format-function-X,
1160      where X is the letter following %u.  The function will be passed the
1161      current header as argument.  The function should return a string, which
1162      will be inserted into the summary just like information from any other
1163      summary specifier.
1164
1165 Text between %( and %) will be highlighted with `gnus-mouse-face'
1166 when the mouse point is placed inside the area.  There can only be one
1167 such area.
1168
1169 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1170 with care.  For reasons of efficiency, Gnus will compute what column
1171 these characters will end up in, and \"hard-code\" that.  This means that
1172 it is illegal to have these specs after a variable-length spec.  Well,
1173 you might not be arrested, but your summary buffer will look strange,
1174 which is bad enough.
1175
1176 The smart choice is to have these specs as for to the left as
1177 possible.
1178
1179 This restriction may disappear in later versions of Gnus.")
1180
1181 (defvar gnus-summary-dummy-line-format
1182   "*  %(:                          :%) %S\n"
1183   "*The format specification for the dummy roots in the summary buffer.
1184 It works along the same lines as a normal formatting string,
1185 with some simple extensions.
1186
1187 %S  The subject")
1188
1189 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1190   "*The format specification for the summary mode line.
1191 It works along the same lines as a normal formatting string,
1192 with some simple extensions:
1193
1194 %G  Group name
1195 %p  Unprefixed group name
1196 %A  Current article number
1197 %V  Gnus version
1198 %U  Number of unread articles in the group
1199 %e  Number of unselected articles in the group
1200 %Z  A string with unread/unselected article counts
1201 %g  Shortish group name
1202 %S  Subject of the current article
1203 %u  User-defined spec
1204 %s  Current score file name
1205 %d  Number of dormant articles
1206 %r  Number of articles that have been marked as read in this session
1207 %E  Number of articles expunged by the score files")
1208
1209 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1210   "*The format specification for the article mode line.
1211 See `gnus-summary-mode-line-format' for a closer description.")
1212
1213 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1214   "*The format specification for the group mode line.
1215 It works along the same lines as a normal formatting string,
1216 with some simple extensions:
1217
1218 %S   The native news server.
1219 %M   The native select method.
1220 %:   \":\" if %S isn't \"\".")
1221
1222 (defvar gnus-valid-select-methods
1223   '(("nntp" post address prompt-address)
1224     ("nnspool" post address)
1225     ("nnvirtual" post-mail virtual prompt-address)
1226     ("nnmbox" mail respool address)
1227     ("nnml" mail respool address)
1228     ("nnmh" mail respool address)
1229     ("nndir" post-mail prompt-address address)
1230     ("nneething" none address prompt-address)
1231     ("nndoc" none address prompt-address)
1232     ("nnbabyl" mail address respool)
1233     ("nnkiboze" post address virtual)
1234     ("nnsoup" post-mail address)
1235     ("nndraft" post-mail)
1236     ("nnfolder" mail respool address))
1237   "An alist of valid select methods.
1238 The first element of each list lists should be a string with the name
1239 of the select method.  The other elements may be be the category of
1240 this method (ie. `post', `mail', `none' or whatever) or other
1241 properties that this method has (like being respoolable).
1242 If you implement a new select method, all you should have to change is
1243 this variable.  I think.")
1244
1245 (defvar gnus-updated-mode-lines '(group article summary tree)
1246   "*List of buffers that should update their mode lines.
1247 The list may contain the symbols `group', `article' and `summary'.  If
1248 the corresponding symbol is present, Gnus will keep that mode line
1249 updated with information that may be pertinent.
1250 If this variable is nil, screen refresh may be quicker.")
1251
1252 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1253 (defvar gnus-mode-non-string-length nil
1254   "*Max length of mode-line non-string contents.
1255 If this is nil, Gnus will take space as is needed, leaving the rest
1256 of the modeline intact.")
1257
1258 ;see gnus-cus.el
1259 ;(defvar gnus-mouse-face 'highlight
1260 ;  "*Face used for mouse highlighting in Gnus.
1261 ;No mouse highlights will be done if `gnus-visual' is nil.")
1262
1263 (defvar gnus-summary-mark-below 0
1264   "*Mark all articles with a score below this variable as read.
1265 This variable is local to each summary buffer and usually set by the
1266 score file.")
1267
1268 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1269   "*List of functions used for sorting articles in the summary buffer.
1270 This variable is only used when not using a threaded display.")
1271
1272 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1273   "*List of functions used for sorting threads in the summary buffer.
1274 By default, threads are sorted by article number.
1275
1276 Each function takes two threads and return non-nil if the first thread
1277 should be sorted before the other.  If you use more than one function,
1278 the primary sort function should be the last.  You should probably
1279 always include `gnus-thread-sort-by-number' in the list of sorting
1280 functions -- preferably first.
1281
1282 Ready-mady functions include `gnus-thread-sort-by-number',
1283 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1284 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1285 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1286
1287 (defvar gnus-thread-score-function '+
1288   "*Function used for calculating the total score of a thread.
1289
1290 The function is called with the scores of the article and each
1291 subthread and should then return the score of the thread.
1292
1293 Some functions you can use are `+', `max', or `min'.")
1294
1295 (defvar gnus-summary-expunge-below nil
1296   "All articles that have a score less than this variable will be expunged.")
1297
1298 (defvar gnus-thread-expunge-below nil
1299   "All threads that have a total score less than this variable will be expunged.
1300 See `gnus-thread-score-function' for en explanation of what a
1301 \"thread score\" is.")
1302
1303 (defvar gnus-auto-subscribed-groups
1304   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1305   "*All new groups that match this regexp will be subscribed automatically.
1306 Note that this variable only deals with new groups.  It has no effect
1307 whatsoever on old groups.")
1308
1309 (defvar gnus-options-subscribe nil
1310   "*All new groups matching this regexp will be subscribed unconditionally.
1311 Note that this variable deals only with new newsgroups.  This variable
1312 does not affect old newsgroups.")
1313
1314 (defvar gnus-options-not-subscribe nil
1315   "*All new groups matching this regexp will be ignored.
1316 Note that this variable deals only with new newsgroups.  This variable
1317 does not affect old (already subscribed) newsgroups.")
1318
1319 (defvar gnus-auto-expirable-newsgroups nil
1320   "*Groups in which to automatically mark read articles as expirable.
1321 If non-nil, this should be a regexp that should match all groups in
1322 which to perform auto-expiry.  This only makes sense for mail groups.")
1323
1324 (defvar gnus-total-expirable-newsgroups nil
1325   "*Groups in which to perform expiry of all read articles.
1326 Use with extreme caution.  All groups that match this regexp will be
1327 expiring - which means that all read articles will be deleted after
1328 (say) one week.  (This only goes for mail groups and the like, of
1329 course.)")
1330
1331 (defvar gnus-group-uncollapsed-levels 1
1332   "Number of group name elements to leave alone when making a short group name.")
1333
1334 (defvar gnus-hidden-properties '(invisible t intangible t)
1335   "Property list to use for hiding text.")
1336
1337 (defvar gnus-modtime-botch nil
1338   "*Non-nil means .newsrc should be deleted prior to save.  
1339 Its use is due to the bogus appearance that .newsrc was modified on
1340 disc.")
1341
1342 ;; Hooks.
1343
1344 (defvar gnus-group-mode-hook nil
1345   "*A hook for Gnus group mode.")
1346
1347 (defvar gnus-summary-mode-hook nil
1348   "*A hook for Gnus summary mode.
1349 This hook is run before any variables are set in the summary buffer.")
1350
1351 (defvar gnus-article-mode-hook nil
1352   "*A hook for Gnus article mode.")
1353
1354 (defvar gnus-summary-prepare-exit-hook nil
1355   "*A hook called when preparing to exit from the summary buffer.
1356 It calls `gnus-summary-expire-articles' by default.")
1357 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1358
1359 (defvar gnus-summary-exit-hook nil
1360   "*A hook called on exit from the summary buffer.")
1361
1362 (defvar gnus-group-catchup-group-hook nil
1363   "*A hook run when catching up a group from the group buffer.")
1364
1365 (defvar gnus-open-server-hook nil
1366   "*A hook called just before opening connection to the news server.")
1367
1368 (defvar gnus-load-hook nil
1369   "*A hook run while Gnus is loaded.")
1370
1371 (defvar gnus-startup-hook nil
1372   "*A hook called at startup.
1373 This hook is called after Gnus is connected to the NNTP server.")
1374
1375 (defvar gnus-get-new-news-hook nil
1376   "*A hook run just before Gnus checks for new news.")
1377
1378 (defvar gnus-after-getting-new-news-hook nil
1379   "*A hook run after Gnus checks for new news.")
1380
1381 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1382   "*A function that is called to generate the group buffer.
1383 The function is called with three arguments: The first is a number;
1384 all group with a level less or equal to that number should be listed,
1385 if the second is non-nil, empty groups should also be displayed.  If
1386 the third is non-nil, it is a number.  No groups with a level lower
1387 than this number should be displayed.
1388
1389 The only current function implemented is `gnus-group-prepare-flat'.")
1390
1391 (defvar gnus-group-prepare-hook nil
1392   "*A hook called after the group buffer has been generated.
1393 If you want to modify the group buffer, you can use this hook.")
1394
1395 (defvar gnus-summary-prepare-hook nil
1396   "*A hook called after the summary buffer has been generated.
1397 If you want to modify the summary buffer, you can use this hook.")
1398
1399 (defvar gnus-summary-generate-hook nil
1400   "*A hook run just before generating the summary buffer.
1401 This hook is commonly used to customize threading variables and the
1402 like.")
1403
1404 (defvar gnus-article-prepare-hook nil
1405   "*A hook called after an article has been prepared in the article buffer.
1406 If you want to run a special decoding program like nkf, use this hook.")
1407
1408 ;(defvar gnus-article-display-hook nil
1409 ;  "*A hook called after the article is displayed in the article buffer.
1410 ;The hook is designed to change the contents of the article
1411 ;buffer.  Typical functions that this hook may contain are
1412 ;`gnus-article-hide-headers' (hide selected headers),
1413 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1414 ;`gnus-article-hide-signature' (hide signature) and
1415 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1416 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1417 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1418 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1419
1420 (defvar gnus-article-x-face-command
1421   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1422   "String or function to be executed to display an X-Face header.
1423 If it is a string, the command will be executed in a sub-shell
1424 asynchronously.  The compressed face will be piped to this command.")
1425
1426 (defvar gnus-article-x-face-too-ugly nil
1427   "Regexp matching posters whose face shouldn't be shown automatically.")
1428
1429 (defvar gnus-select-group-hook nil
1430   "*A hook called when a newsgroup is selected.
1431
1432 If you'd like to simplify subjects like the
1433 `gnus-summary-next-same-subject' command does, you can use the
1434 following hook:
1435
1436  (setq gnus-select-group-hook
1437       (list
1438         (lambda ()
1439           (mapcar (lambda (header)
1440                      (mail-header-set-subject
1441                       header
1442                       (gnus-simplify-subject
1443                        (mail-header-subject header) 're-only)))
1444                   gnus-newsgroup-headers))))")
1445
1446 (defvar gnus-select-article-hook nil
1447   "*A hook called when an article is selected.")
1448
1449 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1450   "*A hook called to apply kill files to a group.
1451 This hook is intended to apply a kill file to the selected newsgroup.
1452 The function `gnus-apply-kill-file' is called by default.
1453
1454 Since a general kill file is too heavy to use only for a few
1455 newsgroups, I recommend you to use a lighter hook function.  For
1456 example, if you'd like to apply a kill file to articles which contains
1457 a string `rmgroup' in subject in newsgroup `control', you can use the
1458 following hook:
1459
1460  (setq gnus-apply-kill-hook
1461       (list
1462         (lambda ()
1463           (cond ((string-match \"control\" gnus-newsgroup-name)
1464                  (gnus-kill \"Subject\" \"rmgroup\")
1465                  (gnus-expunge \"X\"))))))")
1466
1467 (defvar gnus-visual-mark-article-hook
1468   (list 'gnus-highlight-selected-summary)
1469   "*Hook run after selecting an article in the summary buffer.
1470 It is meant to be used for highlighting the article in some way.  It
1471 is not run if `gnus-visual' is nil.")
1472
1473 (defvar gnus-parse-headers-hook nil
1474   "*A hook called before parsing the headers.")
1475 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1476
1477 (defvar gnus-exit-group-hook nil
1478   "*A hook called when exiting (not quitting) summary mode.")
1479
1480 (defvar gnus-suspend-gnus-hook nil
1481   "*A hook called when suspending (not exiting) Gnus.")
1482
1483 (defvar gnus-exit-gnus-hook nil
1484   "*A hook called when exiting Gnus.")
1485
1486 (defvar gnus-after-exiting-gnus-hook nil
1487   "*A hook called after exiting Gnus.")
1488
1489 (defvar gnus-save-newsrc-hook nil
1490   "*A hook called before saving any of the newsrc files.")
1491
1492 (defvar gnus-save-quick-newsrc-hook nil
1493   "*A hook called just before saving the quick newsrc file.
1494 Can be used to turn version control on or off.")
1495
1496 (defvar gnus-save-standard-newsrc-hook nil
1497   "*A hook called just before saving the standard newsrc file.
1498 Can be used to turn version control on or off.")
1499
1500 (defvar gnus-summary-update-hook
1501   (list 'gnus-summary-highlight-line)
1502   "*A hook called when a summary line is changed.
1503 The hook will not be called if `gnus-visual' is nil.
1504
1505 The default function `gnus-summary-highlight-line' will
1506 highlight the line according to the `gnus-summary-highlight'
1507 variable.")
1508
1509 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1510   "*A hook called when a group line is changed.
1511 The hook will not be called if `gnus-visual' is nil.
1512
1513 The default function `gnus-group-highlight-line' will
1514 highlight the line according to the `gnus-group-highlight'
1515 variable.")
1516
1517 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1518   "*A hook called when an article is selected for the first time.
1519 The hook is intended to mark an article as read (or unread)
1520 automatically when it is selected.")
1521
1522 (defvar gnus-group-change-level-function nil
1523   "Function run when a group level is changed.
1524 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1525
1526 ;; Remove any hilit infestation.
1527 (add-hook 'gnus-startup-hook
1528           (lambda ()
1529             (remove-hook 'gnus-summary-prepare-hook
1530                          'hilit-rehighlight-buffer-quietly)
1531             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1532             (setq gnus-mark-article-hook
1533                   '(gnus-summary-mark-read-and-unread-as-read))
1534             (remove-hook 'gnus-article-prepare-hook
1535                          'hilit-rehighlight-buffer-quietly)))
1536
1537 \f
1538 ;; Internal variables
1539
1540 (defvar gnus-tree-buffer "*Tree*"
1541   "Buffer where Gnus thread trees are displayed.")
1542
1543 ;; Dummy variable.
1544 (defvar gnus-use-generic-from nil)
1545
1546 (defvar gnus-thread-indent-array nil)
1547 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1548
1549 (defvar gnus-newsrc-file-version nil)
1550
1551 (defvar gnus-method-history nil)
1552 ;; Variable holding the user answers to all method prompts.
1553
1554 (defvar gnus-group-history nil)
1555 ;; Variable holding the user answers to all group prompts.
1556
1557 (defvar gnus-server-alist nil
1558   "List of available servers.")
1559
1560 (defvar gnus-group-indentation-function nil)
1561
1562 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1563
1564 (defvar gnus-goto-missing-group-function nil)
1565
1566 (defvar gnus-override-subscribe-method nil)
1567
1568 (defvar gnus-group-goto-next-group-function nil
1569   "Function to override finding the next group after listing groups.")
1570
1571 (defconst gnus-article-mark-lists
1572   '((marked . tick) (replied . reply)
1573     (expirable . expire) (killed . killed)
1574     (bookmarks . bookmark) (dormant . dormant)
1575     (scored . score) (saved . save)
1576     (cached . cache)
1577     ))
1578
1579 ;; Avoid highlighting in kill files.
1580 (defvar gnus-summary-inhibit-highlight nil)
1581 (defvar gnus-newsgroup-selected-overlay nil)
1582
1583 (defvar gnus-inhibit-hiding nil)
1584 (defvar gnus-group-indentation "")
1585 (defvar gnus-inhibit-limiting nil)
1586 (defvar gnus-created-frames nil)
1587
1588 (defvar gnus-article-mode-map nil)
1589 (defvar gnus-dribble-buffer nil)
1590 (defvar gnus-headers-retrieved-by nil)
1591 (defvar gnus-article-reply nil)
1592 (defvar gnus-override-method nil)
1593 (defvar gnus-article-check-size nil)
1594
1595 (defvar gnus-current-score-file nil)
1596 (defvar gnus-newsgroup-adaptive-score-file nil)
1597 (defvar gnus-scores-exclude-files nil)
1598
1599 (defvar gnus-opened-servers nil)
1600
1601 (defvar gnus-current-move-group nil)
1602 (defvar gnus-current-copy-group nil)
1603 (defvar gnus-current-crosspost-group nil)
1604
1605 (defvar gnus-newsgroup-dependencies nil)
1606 (defvar gnus-newsgroup-async nil)
1607 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1608
1609 (defvar gnus-newsgroup-adaptive nil)
1610
1611 (defvar gnus-summary-display-table nil)
1612 (defvar gnus-summary-display-article-function nil)
1613
1614 (defvar gnus-summary-highlight-line-function nil
1615   "Function called after highlighting a summary line.")
1616
1617 (defvar gnus-group-line-format-alist
1618   `((?M gnus-tmp-marked-mark ?c)
1619     (?S gnus-tmp-subscribed ?c)
1620     (?L gnus-tmp-level ?d)
1621     (?N (cond ((eq number t) "*" )
1622               ((numberp number) 
1623                (int-to-string
1624                 (+ number
1625                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1626                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1627               (t number)) ?s)
1628     (?R gnus-tmp-number-of-read ?s)
1629     (?t gnus-tmp-number-total ?d)
1630     (?y gnus-tmp-number-of-unread ?s)
1631     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1632     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1633     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1634            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1635     (?g gnus-tmp-group ?s)
1636     (?G gnus-tmp-qualified-group ?s)
1637     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1638     (?D gnus-tmp-newsgroup-description ?s)
1639     (?o gnus-tmp-moderated ?c)
1640     (?O gnus-tmp-moderated-string ?s)
1641     (?p gnus-tmp-process-marked ?c)
1642     (?s gnus-tmp-news-server ?s)
1643     (?n gnus-tmp-news-method ?s)
1644     (?P gnus-group-indentation ?s)
1645     (?l gnus-tmp-grouplens ?s)
1646     (?z gnus-tmp-news-method-string ?s)
1647     (?u gnus-tmp-user-defined ?s)))
1648
1649 (defvar gnus-summary-line-format-alist
1650   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1651     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1652     (?s gnus-tmp-subject-or-nil ?s)
1653     (?n gnus-tmp-name ?s)
1654     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1655         ?s)
1656     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1657             gnus-tmp-from) ?s)
1658     (?F gnus-tmp-from ?s)
1659     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1660     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1661     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1662     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1663     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1664     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1665     (?L gnus-tmp-lines ?d)
1666     (?I gnus-tmp-indentation ?s)
1667     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1668     (?R gnus-tmp-replied ?c)
1669     (?\[ gnus-tmp-opening-bracket ?c)
1670     (?\] gnus-tmp-closing-bracket ?c)
1671     (?\> (make-string gnus-tmp-level ? ) ?s)
1672     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1673     (?i gnus-tmp-score ?d)
1674     (?z gnus-tmp-score-char ?c)
1675     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1676     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1677     (?U gnus-tmp-unread ?c)
1678     (?t (gnus-summary-number-of-articles-in-thread
1679          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1680         ?d)
1681     (?e (gnus-summary-number-of-articles-in-thread
1682          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1683         ?c)
1684     (?u gnus-tmp-user-defined ?s))
1685   "An alist of format specifications that can appear in summary lines,
1686 and what variables they correspond with, along with the type of the
1687 variable (string, integer, character, etc).")
1688
1689 (defvar gnus-summary-dummy-line-format-alist
1690   `((?S gnus-tmp-subject ?s)
1691     (?N gnus-tmp-number ?d)
1692     (?u gnus-tmp-user-defined ?s)))
1693
1694 (defvar gnus-summary-mode-line-format-alist
1695   `((?G gnus-tmp-group-name ?s)
1696     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1697     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1698     (?A gnus-tmp-article-number ?d)
1699     (?Z gnus-tmp-unread-and-unselected ?s)
1700     (?V gnus-version ?s)
1701     (?U gnus-tmp-unread ?d)
1702     (?S gnus-tmp-subject ?s)
1703     (?e gnus-tmp-unselected ?d)
1704     (?u gnus-tmp-user-defined ?s)
1705     (?d (length gnus-newsgroup-dormant) ?d)
1706     (?t (length gnus-newsgroup-marked) ?d)
1707     (?r (length gnus-newsgroup-reads) ?d)
1708     (?E gnus-newsgroup-expunged-tally ?d)
1709     (?s (gnus-current-score-file-nondirectory) ?s)))
1710
1711 (defvar gnus-article-mode-line-format-alist
1712   gnus-summary-mode-line-format-alist)
1713
1714 (defvar gnus-group-mode-line-format-alist
1715   `((?S gnus-tmp-news-server ?s)
1716     (?M gnus-tmp-news-method ?s)
1717     (?u gnus-tmp-user-defined ?s)
1718     (?: gnus-tmp-colon ?s)))
1719
1720 (defvar gnus-have-read-active-file nil)
1721
1722 (defconst gnus-maintainer
1723   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1724   "The mail address of the Gnus maintainers.")
1725
1726 (defconst gnus-version "Gnus v5.2.1"
1727   "Version number for this version of Gnus.")
1728
1729 (defvar gnus-info-nodes
1730   '((gnus-group-mode "(gnus)The Group Buffer")
1731     (gnus-summary-mode "(gnus)The Summary Buffer")
1732     (gnus-article-mode "(gnus)The Article Buffer"))
1733   "Assoc list of major modes and related Info nodes.")
1734
1735 (defvar gnus-group-buffer "*Group*")
1736 (defvar gnus-summary-buffer "*Summary*")
1737 (defvar gnus-article-buffer "*Article*")
1738 (defvar gnus-server-buffer "*Server*")
1739
1740 (defvar gnus-work-buffer " *gnus work*")
1741
1742 (defvar gnus-original-article-buffer " *Original Article*")
1743 (defvar gnus-original-article nil)
1744
1745 (defvar gnus-buffer-list nil
1746   "Gnus buffers that should be killed on exit.")
1747
1748 (defvar gnus-slave nil
1749   "Whether this Gnus is a slave or not.")
1750
1751 (defvar gnus-variable-list
1752   '(gnus-newsrc-options gnus-newsrc-options-n
1753     gnus-newsrc-last-checked-date
1754     gnus-newsrc-alist gnus-server-alist
1755     gnus-killed-list gnus-zombie-list
1756     gnus-topic-topology gnus-topic-alist
1757     gnus-format-specs)
1758   "Gnus variables saved in the quick startup file.")
1759
1760 (defvar gnus-newsrc-options nil
1761   "Options line in the .newsrc file.")
1762
1763 (defvar gnus-newsrc-options-n nil
1764   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1765
1766 (defvar gnus-newsrc-last-checked-date nil
1767   "Date Gnus last asked server for new newsgroups.")
1768
1769 (defvar gnus-topic-topology nil
1770   "The complete topic hierarchy.")
1771
1772 (defvar gnus-topic-alist nil
1773   "The complete topic-group alist.")
1774
1775 (defvar gnus-newsrc-alist nil
1776   "Assoc list of read articles.
1777 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1778
1779 (defvar gnus-newsrc-hashtb nil
1780   "Hashtable of gnus-newsrc-alist.")
1781
1782 (defvar gnus-killed-list nil
1783   "List of killed newsgroups.")
1784
1785 (defvar gnus-killed-hashtb nil
1786   "Hash table equivalent of gnus-killed-list.")
1787
1788 (defvar gnus-zombie-list nil
1789   "List of almost dead newsgroups.")
1790
1791 (defvar gnus-description-hashtb nil
1792   "Descriptions of newsgroups.")
1793
1794 (defvar gnus-list-of-killed-groups nil
1795   "List of newsgroups that have recently been killed by the user.")
1796
1797 (defvar gnus-active-hashtb nil
1798   "Hashtable of active articles.")
1799
1800 (defvar gnus-moderated-list nil
1801   "List of moderated newsgroups.")
1802
1803 (defvar gnus-group-marked nil)
1804
1805 (defvar gnus-current-startup-file nil
1806   "Startup file for the current host.")
1807
1808 (defvar gnus-last-search-regexp nil
1809   "Default regexp for article search command.")
1810
1811 (defvar gnus-last-shell-command nil
1812   "Default shell command on article.")
1813
1814 (defvar gnus-current-select-method nil
1815   "The current method for selecting a newsgroup.")
1816
1817 (defvar gnus-group-list-mode nil)
1818
1819 (defvar gnus-article-internal-prepare-hook nil)
1820
1821 (defvar gnus-newsgroup-name nil)
1822 (defvar gnus-newsgroup-begin nil)
1823 (defvar gnus-newsgroup-end nil)
1824 (defvar gnus-newsgroup-last-rmail nil)
1825 (defvar gnus-newsgroup-last-mail nil)
1826 (defvar gnus-newsgroup-last-folder nil)
1827 (defvar gnus-newsgroup-last-file nil)
1828 (defvar gnus-newsgroup-auto-expire nil)
1829 (defvar gnus-newsgroup-active nil)
1830
1831 (defvar gnus-newsgroup-data nil)
1832 (defvar gnus-newsgroup-data-reverse nil)
1833 (defvar gnus-newsgroup-limit nil)
1834 (defvar gnus-newsgroup-limits nil)
1835
1836 (defvar gnus-newsgroup-unreads nil
1837   "List of unread articles in the current newsgroup.")
1838
1839 (defvar gnus-newsgroup-unselected nil
1840   "List of unselected unread articles in the current newsgroup.")
1841
1842 (defvar gnus-newsgroup-reads nil
1843   "Alist of read articles and article marks in the current newsgroup.")
1844
1845 (defvar gnus-newsgroup-expunged-tally nil)
1846
1847 (defvar gnus-newsgroup-marked nil
1848   "List of ticked articles in the current newsgroup (a subset of unread art).")
1849
1850 (defvar gnus-newsgroup-killed nil
1851   "List of ranges of articles that have been through the scoring process.")
1852
1853 (defvar gnus-newsgroup-cached nil
1854   "List of articles that come from the article cache.")
1855
1856 (defvar gnus-newsgroup-saved nil
1857   "List of articles that have been saved.")
1858
1859 (defvar gnus-newsgroup-kill-headers nil)
1860
1861 (defvar gnus-newsgroup-replied nil
1862   "List of articles that have been replied to in the current newsgroup.")
1863
1864 (defvar gnus-newsgroup-expirable nil
1865   "List of articles in the current newsgroup that can be expired.")
1866
1867 (defvar gnus-newsgroup-processable nil
1868   "List of articles in the current newsgroup that can be processed.")
1869
1870 (defvar gnus-newsgroup-bookmarks nil
1871   "List of articles in the current newsgroup that have bookmarks.")
1872
1873 (defvar gnus-newsgroup-dormant nil
1874   "List of dormant articles in the current newsgroup.")
1875
1876 (defvar gnus-newsgroup-scored nil
1877   "List of scored articles in the current newsgroup.")
1878
1879 (defvar gnus-newsgroup-headers nil
1880   "List of article headers in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-threads nil)
1883
1884 (defvar gnus-newsgroup-prepared nil
1885   "Whether the current group has been prepared properly.")
1886
1887 (defvar gnus-newsgroup-ancient nil
1888   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1889
1890 (defvar gnus-newsgroup-sparse nil)
1891
1892 (defvar gnus-current-article nil)
1893 (defvar gnus-article-current nil)
1894 (defvar gnus-current-headers nil)
1895 (defvar gnus-have-all-headers nil)
1896 (defvar gnus-last-article nil)
1897 (defvar gnus-newsgroup-history nil)
1898 (defvar gnus-current-kill-article nil)
1899
1900 ;; Save window configuration.
1901 (defvar gnus-prev-winconf nil)
1902
1903 (defvar gnus-summary-mark-positions nil)
1904 (defvar gnus-group-mark-positions nil)
1905
1906 (defvar gnus-reffed-article-number nil)
1907
1908 ;;; Let the byte-compiler know that we know about this variable.
1909 (defvar rmail-default-rmail-file)
1910
1911 (defvar gnus-cache-removable-articles nil)
1912
1913 (defvar gnus-dead-summary nil)
1914
1915 (defconst gnus-summary-local-variables
1916   '(gnus-newsgroup-name
1917     gnus-newsgroup-begin gnus-newsgroup-end
1918     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1919     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1920     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1921     gnus-newsgroup-unselected gnus-newsgroup-marked
1922     gnus-newsgroup-reads gnus-newsgroup-saved
1923     gnus-newsgroup-replied gnus-newsgroup-expirable
1924     gnus-newsgroup-processable gnus-newsgroup-killed
1925     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1926     gnus-newsgroup-headers gnus-newsgroup-threads
1927     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1928     gnus-current-article gnus-current-headers gnus-have-all-headers
1929     gnus-last-article gnus-article-internal-prepare-hook
1930     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1931     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1932     gnus-newsgroup-async gnus-thread-expunge-below
1933     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1934     (gnus-summary-mark-below . 0)
1935     gnus-newsgroup-active gnus-scores-exclude-files
1936     gnus-newsgroup-history gnus-newsgroup-ancient
1937     gnus-newsgroup-sparse
1938     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1939     gnus-newsgroup-adaptive-score-file
1940     (gnus-newsgroup-expunged-tally . 0)
1941     gnus-cache-removable-articles gnus-newsgroup-cached
1942     gnus-newsgroup-data gnus-newsgroup-data-reverse
1943     gnus-newsgroup-limit gnus-newsgroup-limits)
1944   "Variables that are buffer-local to the summary buffers.")
1945
1946 (defconst gnus-bug-message
1947   "Sending a bug report to the Gnus Towers.
1948 ========================================
1949
1950 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1951 be sent to the Gnus Bug Exterminators.
1952
1953 At the bottom of the buffer you'll see lots of variable settings.
1954 Please do not delete those.  They will tell the Bug People what your
1955 environment is, so that it will be easier to locate the bugs.
1956
1957 If you have found a bug that makes Emacs go \"beep\", set
1958 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1959 and include the backtrace in your bug report.
1960
1961 Please describe the bug in annoying, painstaking detail.
1962
1963 Thank you for your help in stamping out bugs.
1964 ")
1965
1966 ;;; End of variables.
1967
1968 ;; Define some autoload functions Gnus might use.
1969 (eval-and-compile
1970
1971   ;; This little mapcar goes through the list below and marks the
1972   ;; symbols in question as autoloaded functions.
1973   (mapcar
1974    (lambda (package)
1975      (let ((interactive (nth 1 (memq ':interactive package))))
1976        (mapcar
1977         (lambda (function)
1978           (let (keymap)
1979             (when (consp function)
1980               (setq keymap (car (memq 'keymap function)))
1981               (setq function (car function)))
1982             (autoload function (car package) nil interactive keymap)))
1983         (if (eq (nth 1 package) ':interactive)
1984             (cdddr package)
1985           (cdr package)))))
1986    '(("metamail" metamail-buffer)
1987      ("info" Info-goto-node)
1988      ("hexl" hexl-hex-string-to-integer)
1989      ("pp" pp pp-to-string pp-eval-expression)
1990      ("mail-extr" mail-extract-address-components)
1991      ("nnmail" nnmail-split-fancy nnmail-article-group)
1992      ("nnvirtual" nnvirtual-catchup-group)
1993      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1994       timezone-make-sortable-date timezone-make-time-string)
1995      ("sendmail" mail-position-on-field mail-setup)
1996      ("rmailout" rmail-output)
1997      ("rnewspost" news-mail-other-window news-reply-yank-original
1998       news-caesar-buffer-body)
1999      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2000       rmail-show-message)
2001      ("gnus-soup" :interactive t
2002       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2003       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2004      ("nnsoup" nnsoup-pack-replies)
2005      ("gnus-scomo" :interactive t gnus-score-mode)
2006      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2007       gnus-Folder-save-name gnus-folder-save-name)
2008      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2009      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2010       gnus-server-make-menu-bar gnus-article-make-menu-bar
2011       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2012       gnus-summary-highlight-line gnus-carpal-setup-buffer
2013       gnus-group-highlight-line
2014       gnus-article-add-button gnus-insert-next-page-button
2015       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2016      ("gnus-vis" :interactive t
2017       gnus-article-push-button gnus-article-press-button
2018       gnus-article-highlight gnus-article-highlight-some
2019       gnus-article-highlight-headers gnus-article-highlight-signature
2020       gnus-article-add-buttons gnus-article-add-buttons-to-head
2021       gnus-article-next-button gnus-article-prev-button)
2022      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2023       gnus-demon-add-disconnection gnus-demon-add-handler
2024       gnus-demon-remove-handler)
2025      ("gnus-demon" :interactive t
2026       gnus-demon-init gnus-demon-cancel)
2027      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2028       gnus-tree-open gnus-tree-close)
2029      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2030       gnus-nocem-unwanted-article-p)
2031      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2032      ("gnus-srvr" gnus-browse-foreign-server)
2033      ("gnus-cite" :interactive t
2034       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2035       gnus-article-hide-citation gnus-article-fill-cited-article
2036       gnus-article-hide-citation-in-followups)
2037      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2038       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2039       gnus-execute gnus-expunge)
2040      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2041       gnus-cache-possibly-remove-articles gnus-cache-request-article
2042       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2043       gnus-cache-enter-remove-article gnus-cached-article-p
2044       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2045      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2046       gnus-cache-remove-article)
2047      ("gnus-score" :interactive t
2048       gnus-summary-increase-score gnus-summary-lower-score
2049       gnus-score-flush-cache gnus-score-close
2050       gnus-score-raise-same-subject-and-select
2051       gnus-score-raise-same-subject gnus-score-default
2052       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2053       gnus-score-lower-same-subject gnus-score-lower-thread
2054       gnus-possibly-score-headers gnus-summary-raise-score 
2055       gnus-summary-set-score gnus-summary-current-score)
2056      ("gnus-score"
2057       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2058       gnus-current-score-file-nondirectory gnus-score-adaptive
2059       gnus-score-find-trace gnus-score-file-name)
2060      ("gnus-edit" :interactive t gnus-score-customize)
2061      ("gnus-topic" :interactive t gnus-topic-mode)
2062      ("gnus-topic" gnus-topic-remove-group)
2063      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2064      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2065      ("gnus-uu" :interactive t
2066       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2067       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2068       gnus-uu-mark-by-regexp gnus-uu-mark-all
2069       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2070       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2071       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2072       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2073       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2074       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2075       gnus-uu-decode-binhex-view)
2076      ("gnus-msg" (gnus-summary-send-map keymap)
2077       gnus-mail-yank-original gnus-mail-send-and-exit
2078       gnus-sendmail-setup-mail gnus-article-mail
2079       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2080      ("gnus-msg" :interactive t
2081       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2082       gnus-summary-followup gnus-summary-followup-with-original
2083       gnus-summary-followup-and-reply
2084       gnus-summary-followup-and-reply-with-original
2085       gnus-summary-cancel-article gnus-summary-supersede-article
2086       gnus-post-news gnus-inews-news gnus-cancel-news
2087       gnus-summary-reply gnus-summary-reply-with-original
2088       gnus-summary-mail-forward gnus-summary-mail-other-window
2089       gnus-bug)
2090      ("gnus-picon" :interactive t gnus-article-display-picons
2091       gnus-group-display-picons gnus-picons-article-display-x-face)
2092      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2093       gnus-grouplens-mode)
2094      ("gnus-vm" gnus-vm-mail-setup)
2095      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2096       gnus-summary-save-article-vm gnus-yank-article))))
2097
2098 \f
2099
2100 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2101 ;; If you want the cursor to go somewhere else, set these two
2102 ;; functions in some startup hook to whatever you want.
2103 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2104 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2105
2106 ;;; Various macros and substs.
2107
2108 (defun gnus-header-from (header)
2109   (mail-header-from header))
2110
2111 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2112   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2113   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2114         (w (make-symbol "w"))
2115         (buf (make-symbol "buf")))
2116     `(let* ((,tempvar (selected-window))
2117             (,buf ,buffer)
2118             (,w (get-buffer-window ,buf 'visible)))
2119        (unwind-protect
2120            (progn
2121              (if ,w
2122                  (select-window ,w)
2123                (pop-to-buffer ,buf))
2124              ,@forms)
2125          (select-window ,tempvar)))))
2126
2127 (defmacro gnus-gethash (string hashtable)
2128   "Get hash value of STRING in HASHTABLE."
2129   `(symbol-value (intern-soft ,string ,hashtable)))
2130
2131 (defmacro gnus-sethash (string value hashtable)
2132   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2133   `(set (intern ,string ,hashtable) ,value))
2134
2135 (defmacro gnus-intern-safe (string hashtable)
2136   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2137   `(let ((symbol (intern ,string ,hashtable)))
2138      (or (boundp symbol)
2139          (set symbol nil))
2140      symbol))
2141
2142 (defmacro gnus-group-unread (group)
2143   "Get the currently computed number of unread articles in GROUP."
2144   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2145
2146 (defmacro gnus-group-entry (group)
2147   "Get the newsrc entry for GROUP."
2148   `(gnus-gethash ,group gnus-newsrc-hashtb))
2149
2150 (defmacro gnus-active (group)
2151   "Get active info on GROUP."
2152   `(gnus-gethash ,group gnus-active-hashtb))
2153
2154 (defmacro gnus-set-active (group active)
2155   "Set GROUP's active info."
2156   `(gnus-sethash ,group ,active gnus-active-hashtb))
2157
2158 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2159 ;;   function `substring' might cut on a middle of multi-octet
2160 ;;   character.
2161 (defun gnus-truncate-string (str width)
2162   (substring str 0 width))
2163
2164 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2165 ;; to limit the length of a string.  This function is necessary since
2166 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2167 (defsubst gnus-limit-string (str width)
2168   (if (> (length str) width)
2169       (substring str 0 width)
2170     str))
2171
2172 (defsubst gnus-simplify-subject-re (subject)
2173   "Remove \"Re:\" from subject lines."
2174   (if (string-match "^[Rr][Ee]: *" subject)
2175       (substring subject (match-end 0))
2176     subject))
2177
2178 (defsubst gnus-functionp (form)
2179   "Return non-nil if FORM is funcallable."
2180   (or (and (symbolp form) (fboundp form))
2181       (and (listp form) (eq (car form) 'lambda))))
2182
2183 (defsubst gnus-goto-char (point)
2184   (and point (goto-char point)))
2185
2186 (defmacro gnus-buffer-exists-p (buffer)
2187   `(let ((buffer ,buffer))
2188      (and buffer
2189           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2190                    buffer))))
2191
2192 (defmacro gnus-kill-buffer (buffer)
2193   `(let ((buf ,buffer))
2194      (if (gnus-buffer-exists-p buf)
2195          (kill-buffer buf))))
2196
2197 (defsubst gnus-point-at-bol ()
2198   "Return point at the beginning of the line."
2199   (let ((p (point)))
2200     (beginning-of-line)
2201     (prog1
2202         (point)
2203       (goto-char p))))
2204
2205 (defsubst gnus-point-at-eol ()
2206   "Return point at the end of the line."
2207   (let ((p (point)))
2208     (end-of-line)
2209     (prog1
2210         (point)
2211       (goto-char p))))
2212
2213 (defun gnus-alive-p ()
2214   "Say whether Gnus is running or not."
2215   (and gnus-group-buffer
2216        (get-buffer gnus-group-buffer)))
2217
2218 ;; Delete the current line (and the next N lines.);
2219 (defmacro gnus-delete-line (&optional n)
2220   `(delete-region (progn (beginning-of-line) (point))
2221                   (progn (forward-line ,(or n 1)) (point))))
2222
2223 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2224 (defvar gnus-init-inhibit nil)
2225 (defun gnus-read-init-file (&optional inhibit-next)
2226   (if gnus-init-inhibit
2227       (setq gnus-init-inhibit nil)
2228     (setq gnus-init-inhibit inhibit-next)
2229     (and gnus-init-file
2230          (or (and (file-exists-p gnus-init-file)
2231                   ;; Don't try to load a directory.
2232                   (not (file-directory-p gnus-init-file)))
2233              (file-exists-p (concat gnus-init-file ".el"))
2234              (file-exists-p (concat gnus-init-file ".elc")))
2235          (condition-case var
2236              (load gnus-init-file nil t)
2237            (error
2238             (error "Error in %s: %s" gnus-init-file var))))))
2239
2240 ;; Info access macros.
2241
2242 (defmacro gnus-info-group (info)
2243   `(nth 0 ,info))
2244 (defmacro gnus-info-rank (info)
2245   `(nth 1 ,info))
2246 (defmacro gnus-info-read (info)
2247   `(nth 2 ,info))
2248 (defmacro gnus-info-marks (info)
2249   `(nth 3 ,info))
2250 (defmacro gnus-info-method (info)
2251   `(nth 4 ,info))
2252 (defmacro gnus-info-params (info)
2253   `(nth 5 ,info))
2254
2255 (defmacro gnus-info-level (info)
2256   `(let ((rank (gnus-info-rank ,info)))
2257      (if (consp rank)
2258          (car rank)
2259        rank)))
2260 (defmacro gnus-info-score (info)
2261   `(let ((rank (gnus-info-rank ,info)))
2262      (or (and (consp rank) (cdr rank)) 0)))
2263
2264 (defmacro gnus-info-set-group (info group)
2265   `(setcar ,info ,group))
2266 (defmacro gnus-info-set-rank (info rank)
2267   `(setcar (nthcdr 1 ,info) ,rank))
2268 (defmacro gnus-info-set-read (info read)
2269   `(setcar (nthcdr 2 ,info) ,read))
2270 (defmacro gnus-info-set-marks (info marks)
2271   `(setcar (nthcdr 3 ,info) ,marks))
2272 (defmacro gnus-info-set-method (info method)
2273   `(setcar (nthcdr 4 ,info) ,method))
2274 (defmacro gnus-info-set-params (info params)
2275   `(setcar (nthcdr 5 ,info) ,params))
2276
2277 (defmacro gnus-info-set-level (info level)
2278   `(let ((rank (cdr ,info)))
2279      (if (consp (car rank))
2280          (setcar (car rank) ,level)
2281        (setcar rank ,level))))
2282 (defmacro gnus-info-set-score (info score)
2283   `(let ((rank (cdr ,info)))
2284      (if (consp (car rank))
2285          (setcdr (car rank) ,score)
2286        (setcar rank (cons (car rank) ,score)))))
2287
2288 (defmacro gnus-get-info (group)
2289   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2290
2291 (defun gnus-byte-code (func)
2292   "Return a form that can be `eval'ed based on FUNC."
2293   (let ((fval (symbol-function func)))
2294     (if (byte-code-function-p fval)
2295         (let ((flist (append fval nil)))
2296           (setcar flist 'byte-code)
2297           flist)
2298       (cons 'progn (cddr fval)))))
2299
2300 ;;; Load the compatability functions.
2301
2302 (require 'gnus-cus)
2303 (require 'gnus-ems)
2304
2305 \f
2306 ;;;
2307 ;;; Shutdown
2308 ;;;
2309
2310 (defvar gnus-shutdown-alist nil)
2311
2312 (defun gnus-add-shutdown (function &rest symbols)
2313   "Run FUNCTION whenever one of SYMBOLS is shut down."
2314   (push (cons function symbols) gnus-shutdown-alist))
2315
2316 (defun gnus-shutdown (symbol)
2317   "Shut down everything that waits for SYMBOL."
2318   (let ((alist gnus-shutdown-alist)
2319         entry)
2320     (while (setq entry (pop alist))
2321       (when (memq symbol (cdr entry))
2322         (funcall (car entry))))))
2323
2324 \f
2325
2326 ;; Format specs.  The chunks below are the machine-generated forms
2327 ;; that are to be evaled as the result of the default format strings.
2328 ;; We write them in here to get them byte-compiled.  That way the
2329 ;; default actions will be quite fast, while still retaining the full
2330 ;; flexibility of the user-defined format specs.
2331
2332 ;; First we have lots of dummy defvars to let the compiler know these
2333 ;; are really dynamic variables.
2334
2335 (defvar gnus-tmp-unread)
2336 (defvar gnus-tmp-replied)
2337 (defvar gnus-tmp-score-char)
2338 (defvar gnus-tmp-indentation)
2339 (defvar gnus-tmp-opening-bracket)
2340 (defvar gnus-tmp-lines)
2341 (defvar gnus-tmp-name)
2342 (defvar gnus-tmp-closing-bracket)
2343 (defvar gnus-tmp-subject-or-nil)
2344 (defvar gnus-tmp-subject)
2345 (defvar gnus-tmp-marked)
2346 (defvar gnus-tmp-marked-mark)
2347 (defvar gnus-tmp-subscribed)
2348 (defvar gnus-tmp-process-marked)
2349 (defvar gnus-tmp-number-of-unread)
2350 (defvar gnus-tmp-group-name)
2351 (defvar gnus-tmp-group)
2352 (defvar gnus-tmp-article-number)
2353 (defvar gnus-tmp-unread-and-unselected)
2354 (defvar gnus-tmp-news-method)
2355 (defvar gnus-tmp-news-server)
2356 (defvar gnus-tmp-article-number)
2357 (defvar gnus-mouse-face)
2358 (defvar gnus-mouse-face-prop)
2359
2360 (defun gnus-summary-line-format-spec ()
2361   (insert gnus-tmp-unread gnus-tmp-replied
2362           gnus-tmp-score-char gnus-tmp-indentation)
2363   (gnus-put-text-property
2364    (point)
2365    (progn
2366      (insert
2367       gnus-tmp-opening-bracket
2368       (format "%4d: %-20s"
2369               gnus-tmp-lines
2370               (if (> (length gnus-tmp-name) 20)
2371                   (substring gnus-tmp-name 0 20)
2372                 gnus-tmp-name))
2373       gnus-tmp-closing-bracket)
2374      (point))
2375    gnus-mouse-face-prop gnus-mouse-face)
2376   (insert " " gnus-tmp-subject-or-nil "\n"))
2377
2378 (defvar gnus-summary-line-format-spec
2379   (gnus-byte-code 'gnus-summary-line-format-spec))
2380
2381 (defun gnus-summary-dummy-line-format-spec ()
2382   (insert "*  ")
2383   (gnus-put-text-property
2384    (point)
2385    (progn
2386      (insert ":                          :")
2387      (point))
2388    gnus-mouse-face-prop gnus-mouse-face)
2389   (insert " " gnus-tmp-subject "\n"))
2390
2391 (defvar gnus-summary-dummy-line-format-spec
2392   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2393
2394 (defun gnus-group-line-format-spec ()
2395   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2396           gnus-tmp-process-marked
2397           gnus-group-indentation
2398           (format "%5s: " gnus-tmp-number-of-unread))
2399   (gnus-put-text-property
2400    (point)
2401    (progn
2402      (insert gnus-tmp-group "\n")
2403      (1- (point)))
2404    gnus-mouse-face-prop gnus-mouse-face))
2405 (defvar gnus-group-line-format-spec
2406   (gnus-byte-code 'gnus-group-line-format-spec))
2407
2408 (defvar gnus-format-specs
2409   `((version . ,emacs-version)
2410     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2411     (summary-dummy ,gnus-summary-dummy-line-format
2412                    ,gnus-summary-dummy-line-format-spec)
2413     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2414
2415 (defvar gnus-article-mode-line-format-spec nil)
2416 (defvar gnus-summary-mode-line-format-spec nil)
2417 (defvar gnus-group-mode-line-format-spec nil)
2418
2419 ;;; Phew.  All that gruft is over, fortunately.
2420
2421 \f
2422 ;;;
2423 ;;; Gnus Utility Functions
2424 ;;;
2425
2426 (defun gnus-extract-address-components (from)
2427   (let (name address)
2428     ;; First find the address - the thing with the @ in it.  This may
2429     ;; not be accurate in mail addresses, but does the trick most of
2430     ;; the time in news messages.
2431     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2432         (setq address (substring from (match-beginning 0) (match-end 0))))
2433     ;; Then we check whether the "name <address>" format is used.
2434     (and address
2435          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2436          ;; Linear white space is not required.
2437          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2438          (and (setq name (substring from 0 (match-beginning 0)))
2439               ;; Strip any quotes from the name.
2440               (string-match "\".*\"" name)
2441               (setq name (substring name 1 (1- (match-end 0))))))
2442     ;; If not, then "address (name)" is used.
2443     (or name
2444         (and (string-match "(.+)" from)
2445              (setq name (substring from (1+ (match-beginning 0))
2446                                    (1- (match-end 0)))))
2447         (and (string-match "()" from)
2448              (setq name address))
2449         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2450         ;; XOVER might not support folded From headers.
2451         (and (string-match "(.*" from)
2452              (setq name (substring from (1+ (match-beginning 0))
2453                                    (match-end 0)))))
2454     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2455     (list (or name from) (or address from))))
2456
2457 (defun gnus-fetch-field (field)
2458   "Return the value of the header FIELD of current article."
2459   (save-excursion
2460     (save-restriction
2461       (let ((case-fold-search t)
2462             (inhibit-point-motion-hooks t))
2463         (nnheader-narrow-to-headers)
2464         (message-fetch-field field)))))
2465
2466 (defun gnus-goto-colon ()
2467   (beginning-of-line)
2468   (search-forward ":" (gnus-point-at-eol) t))
2469
2470 ;;;###autoload
2471 (defun gnus-update-format (var)
2472   "Update the format specification near point."
2473   (interactive
2474    (list
2475     (save-excursion
2476       (eval-defun nil)
2477       ;; Find the end of the current word.
2478       (re-search-forward "[ \t\n]" nil t)
2479       ;; Search backward.
2480       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2481         (match-string 1)))))
2482   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2483                               (match-string 1 var))))
2484          (entry (assq type gnus-format-specs))
2485          value spec)
2486     (when entry
2487       (setq gnus-format-specs (delq entry gnus-format-specs)))
2488     (set
2489      (intern (format "%s-spec" var))
2490      (gnus-parse-format (setq value (symbol-value (intern var)))
2491                         (symbol-value (intern (format "%s-alist" var)))
2492                         (not (string-match "mode" var))))
2493     (setq spec (symbol-value (intern (format "%s-spec" var))))
2494     (push (list type value spec) gnus-format-specs)
2495
2496     (pop-to-buffer "*Gnus Format*")
2497     (erase-buffer)
2498     (lisp-interaction-mode)
2499     (insert (pp-to-string spec))))
2500
2501 (defun gnus-update-format-specifications (&optional force)
2502   "Update all (necessary) format specifications."
2503   ;; Make the indentation array.
2504   (gnus-make-thread-indent-array)
2505
2506   ;; See whether all the stored info needs to be flushed.
2507   (when (or force
2508             (not (equal emacs-version
2509                         (cdr (assq 'version gnus-format-specs)))))
2510     (setq gnus-format-specs nil))
2511
2512   ;; Go through all the formats and see whether they need updating.
2513   (let ((types '(summary summary-dummy group
2514                          summary-mode group-mode article-mode))
2515         new-format entry type val)
2516     (while (setq type (pop types))
2517       ;; Jump to the proper buffer to find out the value of
2518       ;; the variable, if possible.  (It may be buffer-local.)
2519       (save-excursion
2520         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2521               val)
2522           (when (and (boundp buffer)
2523                      (setq val (symbol-value buffer))
2524                      (get-buffer val)
2525                      (buffer-name (get-buffer val)))
2526             (set-buffer (get-buffer val)))
2527           (setq new-format (symbol-value
2528                             (intern (format "gnus-%s-line-format" type))))))
2529       (setq entry (cdr (assq type gnus-format-specs)))
2530       (if (and entry
2531                (equal (car entry) new-format))
2532           ;; Use the old format.
2533           (set (intern (format "gnus-%s-line-format-spec" type))
2534                (cadr entry))
2535         ;; This is a new format.
2536         (setq val
2537               (if (not (stringp new-format))
2538                   ;; This is a function call or something.
2539                   new-format
2540                 ;; This is a "real" format.
2541                 (gnus-parse-format
2542                  new-format
2543                  (symbol-value
2544                   (intern (format "gnus-%s-line-format-alist"
2545                                   (if (eq type 'article-mode)
2546                                       'summary-mode type))))
2547                  (not (string-match "mode$" (symbol-name type))))))
2548         ;; Enter the new format spec into the list.
2549         (if entry
2550             (progn
2551               (setcar (cdr entry) val)
2552               (setcar entry new-format))
2553           (push (list type new-format val) gnus-format-specs))
2554         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2555
2556   (unless (assq 'version gnus-format-specs)
2557     (push (cons 'version emacs-version) gnus-format-specs))
2558
2559   (gnus-update-group-mark-positions)
2560   (gnus-update-summary-mark-positions))
2561
2562 (defun gnus-update-summary-mark-positions ()
2563   "Compute where the summary marks are to go."
2564   (save-excursion
2565     (when (and gnus-summary-buffer
2566                (get-buffer gnus-summary-buffer)
2567                (buffer-name (get-buffer gnus-summary-buffer)))
2568       (set-buffer gnus-summary-buffer))
2569     (let ((gnus-replied-mark 129)
2570           (gnus-score-below-mark 130)
2571           (gnus-score-over-mark 130)
2572           (thread nil)
2573           (gnus-visual nil)
2574           (spec gnus-summary-line-format-spec)
2575           pos)
2576       (save-excursion
2577         (gnus-set-work-buffer)
2578         (let ((gnus-summary-line-format-spec spec))
2579           (gnus-summary-insert-line
2580            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2581           (goto-char (point-min))
2582           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2583                                              (- (point) 2)))))
2584           (goto-char (point-min))
2585           (push (cons 'replied (and (search-forward "\201" nil t) 
2586                                     (- (point) 2)))
2587                 pos)
2588           (goto-char (point-min))
2589           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2590                 pos)))
2591       (setq gnus-summary-mark-positions pos))))
2592
2593 (defun gnus-update-group-mark-positions ()
2594   (save-excursion
2595     (let ((gnus-process-mark 128)
2596           (gnus-group-marked '("dummy.group"))
2597           (gnus-active-hashtb (make-vector 10 0)))
2598       (gnus-set-active "dummy.group" '(0 . 0))
2599       (gnus-set-work-buffer)
2600       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2601       (goto-char (point-min))
2602       (setq gnus-group-mark-positions
2603             (list (cons 'process (and (search-forward "\200" nil t)
2604                                       (- (point) 2))))))))
2605
2606 (defvar gnus-mouse-face-0 'highlight)
2607 (defvar gnus-mouse-face-1 'highlight)
2608 (defvar gnus-mouse-face-2 'highlight)
2609 (defvar gnus-mouse-face-3 'highlight)
2610 (defvar gnus-mouse-face-4 'highlight)
2611
2612 (defun gnus-mouse-face-function (form type)
2613   `(gnus-put-text-property
2614     (point) (progn ,@form (point))
2615     gnus-mouse-face-prop
2616     ,(if (equal type 0)
2617          'gnus-mouse-face
2618        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2619
2620 (defvar gnus-face-0 'bold)
2621 (defvar gnus-face-1 'italic)
2622 (defvar gnus-face-2 'bold-italic)
2623 (defvar gnus-face-3 'bold)
2624 (defvar gnus-face-4 'bold)
2625
2626 (defun gnus-face-face-function (form type)
2627   `(gnus-put-text-property
2628     (point) (progn ,@form (point))
2629     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2630
2631 (defun gnus-max-width-function (el max-width)
2632   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2633   (if (symbolp el)
2634       `(if (> (length ,el) ,max-width)
2635            (substring ,el 0 ,max-width)
2636          ,el)
2637     `(let ((val (eval ,el)))
2638        (if (numberp val)
2639            (setq val (int-to-string val)))
2640        (if (> (length val) ,max-width)
2641            (substring val 0 ,max-width)
2642          val))))
2643
2644 (defun gnus-parse-format (format spec-alist &optional insert)
2645   ;; This function parses the FORMAT string with the help of the
2646   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2647   ;; string.  If the FORMAT string contains the specifiers %( and %)
2648   ;; the text between them will have the mouse-face text property.
2649   (if (string-match
2650        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2651        format)
2652       (gnus-parse-complex-format format spec-alist)
2653     ;; This is a simple format.
2654     (gnus-parse-simple-format format spec-alist insert)))
2655
2656 (defun gnus-parse-complex-format (format spec-alist)
2657   (save-excursion
2658     (gnus-set-work-buffer)
2659     (insert format)
2660     (goto-char (point-min))
2661     (while (re-search-forward "\"" nil t)
2662       (replace-match "\\\"" nil t))
2663     (goto-char (point-min))
2664     (insert "(\"")
2665     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2666       (let ((number (if (match-beginning 1)
2667                         (match-string 1) "0"))
2668             (delim (aref (match-string 2) 0)))
2669         (if (or (= delim ?\() (= delim ?\{))
2670             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2671                                    " " number " \""))
2672           (replace-match "\")\""))))
2673     (goto-char (point-max))
2674     (insert "\")")
2675     (goto-char (point-min))
2676     (let ((form (read (current-buffer))))
2677       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2678
2679 (defun gnus-complex-form-to-spec (form spec-alist)
2680   (delq nil
2681         (mapcar
2682          (lambda (sform)
2683            (if (stringp sform)
2684                (gnus-parse-simple-format sform spec-alist t)
2685              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2686                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2687                       (nth 1 sform))))
2688          form)))
2689
2690 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2691   ;; This function parses the FORMAT string with the help of the
2692   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2693   ;; string.
2694   (let ((max-width 0)
2695         spec flist fstring newspec elem beg result dontinsert)
2696     (save-excursion
2697       (gnus-set-work-buffer)
2698       (insert format)
2699       (goto-char (point-min))
2700       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2701                                 nil t)
2702         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2703               (setq newspec "%"
2704                     beg (1+ (match-beginning 0)))
2705           ;; First check if there are any specs that look anything like
2706           ;; "%12,12A", ie. with a "max width specification".  These have
2707           ;; to be treated specially.
2708           (if (setq beg (match-beginning 1))
2709               (setq max-width
2710                     (string-to-int
2711                      (buffer-substring
2712                       (1+ (match-beginning 1)) (match-end 1))))
2713             (setq max-width 0)
2714             (setq beg (match-beginning 2)))
2715           ;; Find the specification from `spec-alist'.
2716           (unless (setq elem (cdr (assq spec spec-alist)))
2717             (setq elem '("*" ?s)))
2718           ;; Treat user defined format specifiers specially.
2719           (when (eq (car elem) 'gnus-tmp-user-defined)
2720             (setq elem
2721                   (list
2722                    (list (intern (concat "gnus-user-format-function-"
2723                                          (match-string 3)))
2724                          'gnus-tmp-header) ?s))
2725             (delete-region (match-beginning 3) (match-end 3)))
2726           (if (not (zerop max-width))
2727               (let ((el (car elem)))
2728                 (cond ((= (cadr elem) ?c)
2729                        (setq el (list 'char-to-string el)))
2730                       ((= (cadr elem) ?d)
2731                        (setq el (list 'int-to-string el))))
2732                 (setq flist (cons (gnus-max-width-function el max-width)
2733                                   flist))
2734                 (setq newspec ?s))
2735             (progn
2736               (setq flist (cons (car elem) flist))
2737               (setq newspec (cadr elem)))))
2738         ;; Remove the old specification (and possibly a ",12" string).
2739         (delete-region beg (match-end 2))
2740         ;; Insert the new specification.
2741         (goto-char beg)
2742         (insert newspec))
2743       (setq fstring (buffer-substring 1 (point-max))))
2744     ;; Do some postprocessing to increase efficiency.
2745     (setq
2746      result
2747      (cond
2748       ;; Emptyness.
2749       ((string= fstring "")
2750        nil)
2751       ;; Not a format string.
2752       ((not (string-match "%" fstring))
2753        (list fstring))
2754       ;; A format string with just a single string spec.
2755       ((string= fstring "%s")
2756        (list (car flist)))
2757       ;; A single character.
2758       ((string= fstring "%c")
2759        (list (car flist)))
2760       ;; A single number.
2761       ((string= fstring "%d")
2762        (setq dontinsert)
2763        (if insert
2764            (list `(princ ,(car flist)))
2765          (list `(int-to-string ,(car flist)))))
2766       ;; Just lots of chars and strings.
2767       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2768        (nreverse flist))
2769       ;; A single string spec at the beginning of the spec.
2770       ((string-match "\\`%[sc][^%]+\\'" fstring)
2771        (list (car flist) (substring fstring 2)))
2772       ;; A single string spec in the middle of the spec.
2773       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2774        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2775       ;; A single string spec in the end of the spec.
2776       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2777        (list (match-string 1 fstring) (car flist)))
2778       ;; A more complex spec.
2779       (t
2780        (list (cons 'format (cons fstring (nreverse flist)))))))
2781
2782     (if insert
2783         (when result
2784           (if dontinsert
2785               result
2786             (cons 'insert result)))
2787       (cond ((stringp result)
2788              result)
2789             ((consp result)
2790              (cons 'concat result))
2791             (t "")))))
2792
2793 (defun gnus-eval-format (format &optional alist props)
2794   "Eval the format variable FORMAT, using ALIST.
2795 If PROPS, insert the result."
2796   (let ((form (gnus-parse-format format alist props)))
2797     (if props
2798         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2799       (eval form))))
2800
2801 (defun gnus-remove-text-with-property (prop)
2802   "Delete all text in the current buffer with text property PROP."
2803   (save-excursion
2804     (goto-char (point-min))
2805     (while (not (eobp))
2806       (while (get-text-property (point) prop)
2807         (delete-char 1))
2808       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2809
2810 (defun gnus-set-work-buffer ()
2811   (if (get-buffer gnus-work-buffer)
2812       (progn
2813         (set-buffer gnus-work-buffer)
2814         (erase-buffer))
2815     (set-buffer (get-buffer-create gnus-work-buffer))
2816     (kill-all-local-variables)
2817     (buffer-disable-undo (current-buffer))
2818     (gnus-add-current-to-buffer-list)))
2819
2820 ;; Article file names when saving.
2821
2822 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2823   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2824 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2825 Otherwise, it is like ~/News/news/group/num."
2826   (let ((default
2827           (expand-file-name
2828            (concat (if (gnus-use-long-file-name 'not-save)
2829                        (gnus-capitalize-newsgroup newsgroup)
2830                      (gnus-newsgroup-directory-form newsgroup))
2831                    "/" (int-to-string (mail-header-number headers)))
2832            gnus-article-save-directory)))
2833     (if (and last-file
2834              (string-equal (file-name-directory default)
2835                            (file-name-directory last-file))
2836              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2837         default
2838       (or last-file default))))
2839
2840 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2841   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2842 If variable `gnus-use-long-file-name' is non-nil, it is
2843 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2844   (let ((default
2845           (expand-file-name
2846            (concat (if (gnus-use-long-file-name 'not-save)
2847                        newsgroup
2848                      (gnus-newsgroup-directory-form newsgroup))
2849                    "/" (int-to-string (mail-header-number headers)))
2850            gnus-article-save-directory)))
2851     (if (and last-file
2852              (string-equal (file-name-directory default)
2853                            (file-name-directory last-file))
2854              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2855         default
2856       (or last-file default))))
2857
2858 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2859   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2860 If variable `gnus-use-long-file-name' is non-nil, it is
2861 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2862   (or last-file
2863       (expand-file-name
2864        (if (gnus-use-long-file-name 'not-save)
2865            (gnus-capitalize-newsgroup newsgroup)
2866          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2867        gnus-article-save-directory)))
2868
2869 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2870   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2871 If variable `gnus-use-long-file-name' is non-nil, it is
2872 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2873   (or last-file
2874       (expand-file-name
2875        (if (gnus-use-long-file-name 'not-save)
2876            newsgroup
2877          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2878        gnus-article-save-directory)))
2879
2880 ;; For subscribing new newsgroup
2881
2882 (defun gnus-subscribe-hierarchical-interactive (groups)
2883   (let ((groups (sort groups 'string<))
2884         prefixes prefix start ans group starts)
2885     (while groups
2886       (setq prefixes (list "^"))
2887       (while (and groups prefixes)
2888         (while (not (string-match (car prefixes) (car groups)))
2889           (setq prefixes (cdr prefixes)))
2890         (setq prefix (car prefixes))
2891         (setq start (1- (length prefix)))
2892         (if (and (string-match "[^\\.]\\." (car groups) start)
2893                  (cdr groups)
2894                  (setq prefix
2895                        (concat "^" (substring (car groups) 0 (match-end 0))))
2896                  (string-match prefix (cadr groups)))
2897             (progn
2898               (setq prefixes (cons prefix prefixes))
2899               (message "Descend hierarchy %s? ([y]nsq): "
2900                        (substring prefix 1 (1- (length prefix))))
2901               (setq ans (read-char))
2902               (cond ((= ans ?n)
2903                      (while (and groups
2904                                  (string-match prefix
2905                                                (setq group (car groups))))
2906                        (setq gnus-killed-list
2907                              (cons group gnus-killed-list))
2908                        (gnus-sethash group group gnus-killed-hashtb)
2909                        (setq groups (cdr groups)))
2910                      (setq starts (cdr starts)))
2911                     ((= ans ?s)
2912                      (while (and groups
2913                                  (string-match prefix
2914                                                (setq group (car groups))))
2915                        (gnus-sethash group group gnus-killed-hashtb)
2916                        (gnus-subscribe-alphabetically (car groups))
2917                        (setq groups (cdr groups)))
2918                      (setq starts (cdr starts)))
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 nil)))
2926           (message "Subscribe %s? ([n]yq)" (car groups))
2927           (setq ans (read-char))
2928           (setq group (car groups))
2929           (cond ((= ans ?y)
2930                  (gnus-subscribe-alphabetically (car groups))
2931                  (gnus-sethash group group gnus-killed-hashtb))
2932                 ((= ans ?q)
2933                  (while groups
2934                    (setq group (car groups))
2935                    (setq gnus-killed-list (cons group gnus-killed-list))
2936                    (gnus-sethash group group gnus-killed-hashtb)
2937                    (setq groups (cdr groups))))
2938                 (t
2939                  (setq gnus-killed-list (cons group gnus-killed-list))
2940                  (gnus-sethash group group gnus-killed-hashtb)))
2941           (setq groups (cdr groups)))))))
2942
2943 (defun gnus-subscribe-randomly (newsgroup)
2944   "Subscribe new NEWSGROUP by making it the first newsgroup."
2945   (gnus-subscribe-newsgroup newsgroup))
2946
2947 (defun gnus-subscribe-alphabetically (newgroup)
2948   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2949   (let ((groups (cdr gnus-newsrc-alist))
2950         before)
2951     (while (and (not before) groups)
2952       (if (string< newgroup (caar groups))
2953           (setq before (caar groups))
2954         (setq groups (cdr groups))))
2955     (gnus-subscribe-newsgroup newgroup before)))
2956
2957 (defun gnus-subscribe-hierarchically (newgroup)
2958   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2959   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2960   (save-excursion
2961     (set-buffer (find-file-noselect gnus-current-startup-file))
2962     (let ((groupkey newgroup)
2963           before)
2964       (while (and (not before) groupkey)
2965         (goto-char (point-min))
2966         (let ((groupkey-re
2967                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2968           (while (and (re-search-forward groupkey-re nil t)
2969                       (progn
2970                         (setq before (match-string 1))
2971                         (string< before newgroup)))))
2972         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2973         (setq groupkey
2974               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2975                   (substring groupkey (match-beginning 1) (match-end 1)))))
2976       (gnus-subscribe-newsgroup newgroup before))))
2977
2978 (defun gnus-subscribe-interactively (group)
2979   "Subscribe the new GROUP interactively.
2980 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2981 it is killed."
2982   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2983       (gnus-subscribe-hierarchically group)
2984     (push group gnus-killed-list)))
2985
2986 (defun gnus-subscribe-zombies (group)
2987   "Make the new GROUP into a zombie group."
2988   (push group gnus-zombie-list))
2989
2990 (defun gnus-subscribe-killed (group)
2991   "Make the new GROUP a killed group."
2992   (push group gnus-killed-list))
2993
2994 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2995   "Subscribe new NEWSGROUP.
2996 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2997 the first newsgroup."
2998   ;; We subscribe the group by changing its level to `subscribed'.
2999   (gnus-group-change-level
3000    newsgroup gnus-level-default-subscribed
3001    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3002   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3003
3004 ;; For directories
3005
3006 (defun gnus-newsgroup-directory-form (newsgroup)
3007   "Make hierarchical directory name from NEWSGROUP name."
3008   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3009         (len (length newsgroup))
3010         idx)
3011     ;; If this is a foreign group, we don't want to translate the
3012     ;; entire name.
3013     (if (setq idx (string-match ":" newsgroup))
3014         (aset newsgroup idx ?/)
3015       (setq idx 0))
3016     ;; Replace all occurrences of `.' with `/'.
3017     (while (< idx len)
3018       (if (= (aref newsgroup idx) ?.)
3019           (aset newsgroup idx ?/))
3020       (setq idx (1+ idx)))
3021     newsgroup))
3022
3023 (defun gnus-newsgroup-savable-name (group)
3024   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3025   ;; with dots.
3026   (nnheader-replace-chars-in-string group ?/ ?.))
3027
3028 (defun gnus-make-directory (dir)
3029   "Make DIRECTORY recursively."
3030   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3031   ;; of the many mysteries of the universe.
3032   (let* ((dir (expand-file-name dir default-directory))
3033          dirs err)
3034     (if (string-match "/$" dir)
3035         (setq dir (substring dir 0 (match-beginning 0))))
3036     ;; First go down the path until we find a directory that exists.
3037     (while (not (file-exists-p dir))
3038       (setq dirs (cons dir dirs))
3039       (string-match "/[^/]+$" dir)
3040       (setq dir (substring dir 0 (match-beginning 0))))
3041     ;; Then create all the subdirs.
3042     (while (and dirs (not err))
3043       (condition-case ()
3044           (make-directory (car dirs))
3045         (error (setq err t)))
3046       (setq dirs (cdr dirs)))
3047     ;; We return whether we were successful or not.
3048     (not dirs)))
3049
3050 (defun gnus-capitalize-newsgroup (newsgroup)
3051   "Capitalize NEWSGROUP name."
3052   (and (not (zerop (length newsgroup)))
3053        (concat (char-to-string (upcase (aref newsgroup 0)))
3054                (substring newsgroup 1))))
3055
3056 ;; Various... things.
3057
3058 (defun gnus-simplify-subject (subject &optional re-only)
3059   "Remove `Re:' and words in parentheses.
3060 If RE-ONLY is non-nil, strip leading `Re:'s only."
3061   (let ((case-fold-search t))           ;Ignore case.
3062     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3063     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3064       (setq subject (substring subject (match-end 0))))
3065     ;; Remove uninteresting prefixes.
3066     (if (and (not re-only)
3067              gnus-simplify-ignored-prefixes
3068              (string-match gnus-simplify-ignored-prefixes subject))
3069         (setq subject (substring subject (match-end 0))))
3070     ;; Remove words in parentheses from end.
3071     (unless re-only
3072       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3073         (setq subject (substring subject 0 (match-beginning 0)))))
3074     ;; Return subject string.
3075     subject))
3076
3077 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3078 ;; all whitespace.
3079 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3080 (defun gnus-simplify-buffer-fuzzy ()
3081   (goto-char (point-min))
3082   (while (search-forward "\t" nil t)
3083     (replace-match " " t t))
3084   (goto-char (point-min))
3085   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3086   (goto-char (match-beginning 0))
3087   (while (or
3088           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3089           (looking-at "^[[].*: .*[]]$"))
3090     (goto-char (point-min))
3091     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3092                               nil t)
3093       (replace-match "" t t))
3094     (goto-char (point-min))
3095     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3096       (goto-char (match-end 0))
3097       (delete-char -1)
3098       (delete-region
3099        (progn (goto-char (match-beginning 0)))
3100        (re-search-forward ":"))))
3101   (goto-char (point-min))
3102   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3103     (replace-match "" t t))
3104   (goto-char (point-min))
3105   (while (re-search-forward "  +" nil t)
3106     (replace-match " " t t))
3107   (goto-char (point-min))
3108   (while (re-search-forward " $" nil t)
3109     (replace-match "" t t))
3110   (goto-char (point-min))
3111   (while (re-search-forward "^ +" nil t)
3112     (replace-match "" t t))
3113   (goto-char (point-min))
3114   (when gnus-simplify-subject-fuzzy-regexp
3115     (if (listp gnus-simplify-subject-fuzzy-regexp)
3116         (let ((list gnus-simplify-subject-fuzzy-regexp))
3117           (while list
3118             (goto-char (point-min))
3119             (while (re-search-forward (car list) nil t)
3120               (replace-match "" t t))
3121             (setq list (cdr list))))
3122       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3123         (replace-match "" t t)))))
3124
3125 (defun gnus-simplify-subject-fuzzy (subject)
3126   "Siplify a subject string fuzzily."
3127   (save-excursion
3128     (gnus-set-work-buffer)
3129     (let ((case-fold-search t))
3130       (insert subject)
3131       (inline (gnus-simplify-buffer-fuzzy))
3132       (buffer-string))))
3133
3134 ;; Add the current buffer to the list of buffers to be killed on exit.
3135 (defun gnus-add-current-to-buffer-list ()
3136   (or (memq (current-buffer) gnus-buffer-list)
3137       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3138
3139 (defun gnus-string> (s1 s2)
3140   (not (or (string< s1 s2)
3141            (string= s1 s2))))
3142
3143 (defun gnus-read-active-file-p ()
3144   "Say whether the active file has been read from `gnus-select-method'."
3145   (memq gnus-select-method gnus-have-read-active-file))
3146
3147 ;;; General various misc type functions.
3148
3149 (defun gnus-clear-system ()
3150   "Clear all variables and buffers."
3151   ;; Clear Gnus variables.
3152   (let ((variables gnus-variable-list))
3153     (while variables
3154       (set (car variables) nil)
3155       (setq variables (cdr variables))))
3156   ;; Clear other internal variables.
3157   (setq gnus-list-of-killed-groups nil
3158         gnus-have-read-active-file nil
3159         gnus-newsrc-alist nil
3160         gnus-newsrc-hashtb nil
3161         gnus-killed-list nil
3162         gnus-zombie-list nil
3163         gnus-killed-hashtb nil
3164         gnus-active-hashtb nil
3165         gnus-moderated-list nil
3166         gnus-description-hashtb nil
3167         gnus-current-headers nil
3168         gnus-thread-indent-array nil
3169         gnus-newsgroup-headers nil
3170         gnus-newsgroup-name nil
3171         gnus-server-alist nil
3172         gnus-group-list-mode nil
3173         gnus-opened-servers nil
3174         gnus-current-select-method nil)
3175   (gnus-shutdown 'gnus)
3176   ;; Kill the startup file.
3177   (and gnus-current-startup-file
3178        (get-file-buffer gnus-current-startup-file)
3179        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3180   ;; Clear the dribble buffer.
3181   (gnus-dribble-clear)
3182   ;; Kill global KILL file buffer.
3183   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3184     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3185   (gnus-kill-buffer nntp-server-buffer)
3186   ;; Kill Gnus buffers.
3187   (while gnus-buffer-list
3188     (gnus-kill-buffer (pop gnus-buffer-list)))
3189   ;; Remove Gnus frames.
3190   (while gnus-created-frames
3191     (when (frame-live-p (car gnus-created-frames))
3192       ;; We slap a condition-case around this `delete-frame' to ensure 
3193       ;; agains errors if we try do delete the single frame that's left.
3194       (condition-case ()
3195           (delete-frame (car gnus-created-frames))
3196         (error nil)))
3197     (pop gnus-created-frames)))
3198
3199 (defun gnus-windows-old-to-new (setting)
3200   ;; First we take care of the really, really old Gnus 3 actions.
3201   (when (symbolp setting)
3202     (setq setting
3203           ;; Take care of ooold GNUS 3.x values.
3204           (cond ((eq setting 'SelectArticle) 'article)
3205                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3206                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3207                 (t setting))))
3208   (if (or (listp setting)
3209           (not (and gnus-window-configuration
3210                     (memq setting '(group summary article)))))
3211       setting
3212     (let* ((setting (if (eq setting 'group)
3213                         (if (assq 'newsgroup gnus-window-configuration)
3214                             'newsgroup
3215                           'newsgroups) setting))
3216            (elem (cadr (assq setting gnus-window-configuration)))
3217            (total (apply '+ elem))
3218            (types '(group summary article))
3219            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3220            (i 0)
3221            perc
3222            out)
3223       (while (< i 3)
3224         (or (not (numberp (nth i elem)))
3225             (zerop (nth i elem))
3226             (progn
3227               (setq perc  (/ (float (nth 0 elem)) total))
3228               (setq out (cons (if (eq pbuf (nth i types))
3229                                   (vector (nth i types) perc 'point)
3230                                 (vector (nth i types) perc))
3231                               out))))
3232         (setq i (1+ i)))
3233       (list (nreverse out)))))
3234
3235 ;;;###autoload
3236 (defun gnus-add-configuration (conf)
3237   "Add the window configuration CONF to `gnus-buffer-configuration'."
3238   (setq gnus-buffer-configuration
3239         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3240                          gnus-buffer-configuration))))
3241
3242 (defvar gnus-frame-list nil)
3243
3244 (defun gnus-configure-frame (split &optional window)
3245   "Split WINDOW according to SPLIT."
3246   (unless window
3247     (setq window (get-buffer-window (current-buffer))))
3248   (select-window window)
3249   ;; This might be an old-stylee buffer config.
3250   (when (vectorp split)
3251     (setq split (append split nil)))
3252   (when (or (consp (car split))
3253             (vectorp (car split)))
3254     (push 1.0 split)
3255     (push 'vertical split))
3256   ;; The SPLIT might be something that is to be evaled to
3257   ;; return a new SPLIT.
3258   (while (and (not (assq (car split) gnus-window-to-buffer))
3259               (gnus-functionp (car split)))
3260     (setq split (eval split)))
3261   (let* ((type (car split))
3262          (subs (cddr split))
3263          (len (if (eq type 'horizontal) (window-width) (window-height)))
3264          (total 0)
3265          (window-min-width (or gnus-window-min-width window-min-width))
3266          (window-min-height (or gnus-window-min-height window-min-height))
3267          s result new-win rest comp-subs size sub)
3268     (cond
3269      ;; Nothing to do here.
3270      ((null split))
3271      ;; Don't switch buffers.
3272      ((null type)
3273       (and (memq 'point split) window))
3274      ;; This is a buffer to be selected.
3275      ((not (memq type '(frame horizontal vertical)))
3276       (let ((buffer (cond ((stringp type) type)
3277                           (t (cdr (assq type gnus-window-to-buffer)))))
3278             buf)
3279         (unless buffer
3280           (error "Illegal buffer type: %s" type))
3281         (unless (setq buf (get-buffer (if (symbolp buffer)
3282                                           (symbol-value buffer) buffer)))
3283           (setq buf (get-buffer-create (if (symbolp buffer)
3284                                            (symbol-value buffer) buffer))))
3285         (switch-to-buffer buf)
3286         ;; We return the window if it has the `point' spec.
3287         (and (memq 'point split) window)))
3288      ;; This is a frame split.
3289      ((eq type 'frame)
3290       (unless gnus-frame-list
3291         (setq gnus-frame-list (list (window-frame
3292                                      (get-buffer-window (current-buffer))))))
3293       (let ((i 0)
3294             params frame fresult)
3295         (while (< i (length subs))
3296           ;; Frame parameter is gotten from the sub-split.
3297           (setq params (cadr (elt subs i)))
3298           ;; It should be a list.
3299           (unless (listp params)
3300             (setq params nil))
3301           ;; Create a new frame?
3302           (unless (setq frame (elt gnus-frame-list i))
3303             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3304             (push frame gnus-created-frames))
3305           ;; Is the old frame still alive?
3306           (unless (frame-live-p frame)
3307             (setcar (nthcdr i gnus-frame-list)
3308                     (setq frame (make-frame params))))
3309           ;; Select the frame in question and do more splits there.
3310           (select-frame frame)
3311           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3312           (incf i))
3313         ;; Select the frame that has the selected buffer.
3314         (when fresult
3315           (select-frame (window-frame fresult)))))
3316      ;; This is a normal split.
3317      (t
3318       (when (> (length subs) 0)
3319         ;; First we have to compute the sizes of all new windows.
3320         (while subs
3321           (setq sub (append (pop subs) nil))
3322           (while (and (not (assq (car sub) gnus-window-to-buffer))
3323                       (gnus-functionp (car sub)))
3324             (setq sub (eval sub)))
3325           (when sub
3326             (push sub comp-subs)
3327             (setq size (cadar comp-subs))
3328             (cond ((equal size 1.0)
3329                    (setq rest (car comp-subs))
3330                    (setq s 0))
3331                   ((floatp size)
3332                    (setq s (floor (* size len))))
3333                   ((integerp size)
3334                    (setq s size))
3335                   (t
3336                    (error "Illegal size: %s" size)))
3337             ;; Try to make sure that we are inside the safe limits.
3338             (cond ((zerop s))
3339                   ((eq type 'horizontal)
3340                    (setq s (max s window-min-width)))
3341                   ((eq type 'vertical)
3342                    (setq s (max s window-min-height))))
3343             (setcar (cdar comp-subs) s)
3344             (incf total s)))
3345         ;; Take care of the "1.0" spec.
3346         (if rest
3347             (setcar (cdr rest) (- len total))
3348           (error "No 1.0 specs in %s" split))
3349         ;; The we do the actual splitting in a nice recursive
3350         ;; fashion.
3351         (setq comp-subs (nreverse comp-subs))
3352         (while comp-subs
3353           (if (null (cdr comp-subs))
3354               (setq new-win window)
3355             (setq new-win
3356                   (split-window window (cadar comp-subs)
3357                                 (eq type 'horizontal))))
3358           (setq result (or (gnus-configure-frame
3359                             (car comp-subs) window) result))
3360           (select-window new-win)
3361           (setq window new-win)
3362           (setq comp-subs (cdr comp-subs))))
3363       ;; Return the proper window, if any.
3364       (when result
3365         (select-window result))))))
3366
3367 (defvar gnus-frame-split-p nil)
3368
3369 (defun gnus-configure-windows (setting &optional force)
3370   (setq setting (gnus-windows-old-to-new setting))
3371   (let ((split (if (symbolp setting)
3372                    (cadr (assq setting gnus-buffer-configuration))
3373                  setting))
3374         all-visible)
3375
3376     (setq gnus-frame-split-p nil)
3377
3378     (unless split
3379       (error "No such setting: %s" setting))
3380
3381     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3382              (not force))
3383         ;; All the windows mentioned are already visible, so we just
3384         ;; put point in the assigned buffer, and do not touch the
3385         ;; winconf.
3386         (select-window all-visible)
3387
3388       ;; Either remove all windows or just remove all Gnus windows.
3389       (let ((frame (selected-frame)))
3390         (unwind-protect
3391             (if gnus-use-full-window
3392                 ;; We want to remove all other windows.
3393                 (if (not gnus-frame-split-p)
3394                     ;; This is not a `frame' split, so we ignore the
3395                     ;; other frames.  
3396                     (delete-other-windows)
3397                   ;; This is a `frame' split, so we delete all windows
3398                   ;; on all frames.
3399                   (mapcar 
3400                    (lambda (frame)
3401                      (unless (eq (cdr (assq 'minibuffer
3402                                             (frame-parameters frame)))
3403                                  'only)
3404                        (select-frame frame)
3405                        (delete-other-windows)))
3406                    (frame-list)))
3407               ;; Just remove some windows.
3408               (gnus-remove-some-windows)
3409               (switch-to-buffer nntp-server-buffer))
3410           (select-frame frame)))
3411
3412       (switch-to-buffer nntp-server-buffer)
3413       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3414
3415 (defun gnus-all-windows-visible-p (split)
3416   "Say whether all buffers in SPLIT are currently visible.
3417 In particular, the value returned will be the window that
3418 should have point."
3419   (let ((stack (list split))
3420         (all-visible t)
3421         type buffer win buf)
3422     (while (and (setq split (pop stack))
3423                 all-visible)
3424       ;; Be backwards compatible.
3425       (when (vectorp split)
3426         (setq split (append split nil)))
3427       (when (or (consp (car split))
3428                 (vectorp (car split)))
3429         (push 1.0 split)
3430         (push 'vertical split))
3431       ;; The SPLIT might be something that is to be evaled to
3432       ;; return a new SPLIT.
3433       (while (and (not (assq (car split) gnus-window-to-buffer))
3434                   (gnus-functionp (car split)))
3435         (setq split (eval split)))
3436
3437       (setq type (elt split 0))
3438       (cond
3439        ;; Nothing here.
3440        ((null split) t)
3441        ;; A buffer.
3442        ((not (memq type '(horizontal vertical frame)))
3443         (setq buffer (cond ((stringp type) type)
3444                            (t (cdr (assq type gnus-window-to-buffer)))))
3445         (unless buffer
3446           (error "Illegal buffer type: %s" type))
3447         (when (setq buf (get-buffer (if (symbolp buffer)
3448                                         (symbol-value buffer)
3449                                       buffer)))
3450           (setq win (get-buffer-window buf t)))
3451         (if win
3452             (when (memq 'point split)
3453                 (setq all-visible win))
3454           (setq all-visible nil)))
3455        (t
3456         (when (eq type 'frame)
3457           (setq gnus-frame-split-p t))
3458         (setq stack (append (cddr split) stack)))))
3459     (unless (eq all-visible t)
3460       all-visible)))
3461
3462 (defun gnus-window-top-edge (&optional window)
3463   (nth 1 (window-edges window)))
3464
3465 (defun gnus-remove-some-windows ()
3466   (let ((buffers gnus-window-to-buffer)
3467         buf bufs lowest-buf lowest)
3468     (save-excursion
3469       ;; Remove windows on all known Gnus buffers.
3470       (while buffers
3471         (setq buf (cdar buffers))
3472         (if (symbolp buf)
3473             (setq buf (and (boundp buf) (symbol-value buf))))
3474         (and buf
3475              (get-buffer-window buf)
3476              (progn
3477                (setq bufs (cons buf bufs))
3478                (pop-to-buffer buf)
3479                (if (or (not lowest)
3480                        (< (gnus-window-top-edge) lowest))
3481                    (progn
3482                      (setq lowest (gnus-window-top-edge))
3483                      (setq lowest-buf buf)))))
3484         (setq buffers (cdr buffers)))
3485       ;; Remove windows on *all* summary buffers.
3486       (walk-windows
3487        (lambda (win)
3488          (let ((buf (window-buffer win)))
3489            (if (string-match    "^\\*Summary" (buffer-name buf))
3490                (progn
3491                  (setq bufs (cons buf bufs))
3492                  (pop-to-buffer buf)
3493                  (if (or (not lowest)
3494                          (< (gnus-window-top-edge) lowest))
3495                      (progn
3496                        (setq lowest-buf buf)
3497                        (setq lowest (gnus-window-top-edge)))))))))
3498       (and lowest-buf
3499            (progn
3500              (pop-to-buffer lowest-buf)
3501              (switch-to-buffer nntp-server-buffer)))
3502       (while bufs
3503         (and (not (eq (car bufs) lowest-buf))
3504              (delete-windows-on (car bufs)))
3505         (setq bufs (cdr bufs))))))
3506
3507 (defun gnus-version ()
3508   "Version numbers of this version of Gnus."
3509   (interactive)
3510   (let ((methods gnus-valid-select-methods)
3511         (mess gnus-version)
3512         meth)
3513     ;; Go through all the legal select methods and add their version
3514     ;; numbers to the total version string.  Only the backends that are
3515     ;; currently in use will have their message numbers taken into
3516     ;; consideration.
3517     (while methods
3518       (setq meth (intern (concat (caar methods) "-version")))
3519       (and (boundp meth)
3520            (stringp (symbol-value meth))
3521            (setq mess (concat mess "; " (symbol-value meth))))
3522       (setq methods (cdr methods)))
3523     (gnus-message 2 mess)))
3524
3525 (defun gnus-info-find-node ()
3526   "Find Info documentation of Gnus."
3527   (interactive)
3528   ;; Enlarge info window if needed.
3529   (let ((mode major-mode)
3530         gnus-info-buffer)
3531     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3532     (setq gnus-info-buffer (current-buffer))
3533     (gnus-configure-windows 'info)))
3534
3535 (defun gnus-days-between (date1 date2)
3536   ;; Return the number of days between date1 and date2.
3537   (- (gnus-day-number date1) (gnus-day-number date2)))
3538
3539 (defun gnus-day-number (date)
3540   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3541                      (timezone-parse-date date))))
3542     (timezone-absolute-from-gregorian
3543      (nth 1 dat) (nth 2 dat) (car dat))))
3544
3545 (defun gnus-encode-date (date)
3546   "Convert DATE to internal time."
3547   (let* ((parse (timezone-parse-date date))
3548          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3549          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3550     (encode-time (caddr time) (cadr time) (car time)
3551                  (caddr date) (cadr date) (car date) (nth 4 date))))
3552
3553 (defun gnus-time-minus (t1 t2)
3554   "Subtract two internal times."
3555   (let ((borrow (< (cadr t1) (cadr t2))))
3556     (list (- (car t1) (car t2) (if borrow 1 0))
3557           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3558
3559 (defun gnus-file-newer-than (file date)
3560   (let ((fdate (nth 5 (file-attributes file))))
3561     (or (> (car fdate) (car date))
3562         (and (= (car fdate) (car date))
3563              (> (nth 1 fdate) (nth 1 date))))))
3564
3565 (defmacro gnus-local-set-keys (&rest plist)
3566   "Set the keys in PLIST in the current keymap."
3567   `(gnus-define-keys-1 (current-local-map) ',plist))
3568
3569 (defmacro gnus-define-keys (keymap &rest plist)
3570   "Define all keys in PLIST in KEYMAP."
3571   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3572
3573 (put 'gnus-define-keys 'lisp-indent-function 1)
3574 (put 'gnus-define-keys 'lisp-indent-hook 1)
3575 (put 'gnus-define-keymap 'lisp-indent-function 1)
3576 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3577
3578 (defmacro gnus-define-keymap (keymap &rest plist)
3579   "Define all keys in PLIST in KEYMAP."
3580   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3581
3582 (defun gnus-define-keys-1 (keymap plist)
3583   (when (null keymap)
3584     (error "Can't set keys in a null keymap"))
3585   (cond ((symbolp keymap)
3586          (setq keymap (symbol-value keymap)))
3587         ((keymapp keymap))
3588         ((listp keymap)
3589          (set (car keymap) nil)
3590          (define-prefix-command (car keymap))
3591          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3592          (setq keymap (symbol-value (car keymap)))))
3593   (let (key)
3594     (while plist
3595       (when (symbolp (setq key (pop plist)))
3596         (setq key (symbol-value key)))
3597       (define-key keymap key (pop plist)))))
3598
3599 (defun gnus-group-read-only-p (&optional group)
3600   "Check whether GROUP supports editing or not.
3601 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3602 that that variable is buffer-local to the summary buffers."
3603   (let ((group (or group gnus-newsgroup-name)))
3604     (not (gnus-check-backend-function 'request-replace-article group))))
3605
3606 (defun gnus-group-total-expirable-p (group)
3607   "Check whether GROUP is total-expirable or not."
3608   (let ((params (gnus-info-params (gnus-get-info group))))
3609     (or (memq 'total-expire params)
3610         (cdr (assq 'total-expire params)) ; (total-expire . t)
3611         (and gnus-total-expirable-newsgroups ; Check var.
3612              (string-match gnus-total-expirable-newsgroups group)))))
3613
3614 (defun gnus-group-auto-expirable-p (group)
3615   "Check whether GROUP is total-expirable or not."
3616   (let ((params (gnus-info-params (gnus-get-info group))))
3617     (or (memq 'auto-expire params)
3618         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3619         (and gnus-auto-expirable-newsgroups ; Check var.
3620              (string-match gnus-auto-expirable-newsgroups group)))))
3621
3622 (defun gnus-virtual-group-p (group)
3623   "Say whether GROUP is virtual or not."
3624   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3625                         gnus-valid-select-methods)))
3626
3627 (defun gnus-news-group-p (group &optional article)
3628   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3629   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3630       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3631            (eq (gnus-request-type group article) 'news))))
3632
3633 (defsubst gnus-simplify-subject-fully (subject)
3634   "Simplify a subject string according to the user's wishes."
3635   (cond
3636    ((null gnus-summary-gather-subject-limit)
3637     (gnus-simplify-subject-re subject))
3638    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3639     (gnus-simplify-subject-fuzzy subject))
3640    ((numberp gnus-summary-gather-subject-limit)
3641     (gnus-limit-string (gnus-simplify-subject-re subject)
3642                        gnus-summary-gather-subject-limit))
3643    (t
3644     subject)))
3645
3646 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3647   "Check whether two subjects are equal.  If optional argument
3648 simple-first is t, first argument is already simplified."
3649   (cond
3650    ((null simple-first)
3651     (equal (gnus-simplify-subject-fully s1)
3652            (gnus-simplify-subject-fully s2)))
3653    (t
3654     (equal s1
3655            (gnus-simplify-subject-fully s2)))))
3656
3657 ;; Returns a list of writable groups.
3658 (defun gnus-writable-groups ()
3659   (let ((alist gnus-newsrc-alist)
3660         groups group)
3661     (while (setq group (car (pop alist)))
3662       (unless (gnus-group-read-only-p group)
3663         (push group groups)))
3664     (nreverse groups)))
3665
3666 (defun gnus-completing-read (default prompt &rest args)
3667   ;; Like `completing-read', except that DEFAULT is the default argument.
3668   (let* ((prompt (if default 
3669                      (concat prompt " (default " default ") ")
3670                    (concat prompt " ")))
3671          (answer (apply 'completing-read prompt args)))
3672     (if (or (null answer) (zerop (length answer)))
3673         default
3674       answer)))
3675
3676 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3677 ;; the echo area.
3678 (defun gnus-y-or-n-p (prompt)
3679   (prog1
3680       (y-or-n-p prompt)
3681     (message "")))
3682
3683 (defun gnus-yes-or-no-p (prompt)
3684   (prog1
3685       (yes-or-no-p prompt)
3686     (message "")))
3687
3688 ;; Check whether to use long file names.
3689 (defun gnus-use-long-file-name (symbol)
3690   ;; The variable has to be set...
3691   (and gnus-use-long-file-name
3692        ;; If it isn't a list, then we return t.
3693        (or (not (listp gnus-use-long-file-name))
3694            ;; If it is a list, and the list contains `symbol', we
3695            ;; return nil.
3696            (not (memq symbol gnus-use-long-file-name)))))
3697
3698 ;; I suspect there's a better way, but I haven't taken the time to do
3699 ;; it yet. -erik selberg@cs.washington.edu
3700 (defun gnus-dd-mmm (messy-date)
3701   "Return a string like DD-MMM from a big messy string"
3702   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3703                    (error nil))))
3704     (if (not datevec)
3705         "??-???"
3706       (format "%2s-%s"
3707               (condition-case ()
3708                   ;; Make sure leading zeroes are stripped.
3709                   (number-to-string (string-to-number (aref datevec 2)))
3710                 (error "??"))
3711               (capitalize
3712                (or (car
3713                     (nth (1- (string-to-number (aref datevec 1)))
3714                          timezone-months-assoc))
3715                    "???"))))))
3716
3717 (defun gnus-mode-string-quote (string)
3718   "Quote all \"%\" in STRING."
3719   (save-excursion
3720     (gnus-set-work-buffer)
3721     (insert string)
3722     (goto-char (point-min))
3723     (while (search-forward "%" nil t)
3724       (insert "%"))
3725     (buffer-string)))
3726
3727 ;; Make a hash table (default and minimum size is 255).
3728 ;; Optional argument HASHSIZE specifies the table size.
3729 (defun gnus-make-hashtable (&optional hashsize)
3730   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3731
3732 ;; Make a number that is suitable for hashing; bigger than MIN and one
3733 ;; less than 2^x.
3734 (defun gnus-create-hash-size (min)
3735   (let ((i 1))
3736     (while (< i min)
3737       (setq i (* 2 i)))
3738     (1- i)))
3739
3740 ;; Show message if message has a lower level than `gnus-verbose'.
3741 ;; Guideline for numbers:
3742 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3743 ;; for things that take a long time, 7 - not very important messages
3744 ;; on stuff, 9 - messages inside loops.
3745 (defun gnus-message (level &rest args)
3746   (if (<= level gnus-verbose)
3747       (apply 'message args)
3748     ;; We have to do this format thingy here even if the result isn't
3749     ;; shown - the return value has to be the same as the return value
3750     ;; from `message'.
3751     (apply 'format args)))
3752
3753 (defun gnus-error (level &rest args)
3754   "Beep an error if `gnus-verbose' is on LEVEL or less."
3755   (when (<= (floor level) gnus-verbose)
3756     (apply 'message args)
3757     (ding)
3758     (let (duration)
3759       (when (and (floatp level)
3760                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3761         (sit-for duration))))
3762   nil)
3763
3764 ;; Generate a unique new group name.
3765 (defun gnus-generate-new-group-name (leaf)
3766   (let ((name leaf)
3767         (num 0))
3768     (while (gnus-gethash name gnus-newsrc-hashtb)
3769       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3770     name))
3771
3772 (defsubst gnus-hide-text (b e props)
3773   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3774   (gnus-add-text-properties b e props)
3775   (when (memq 'intangible props)
3776     (gnus-put-text-property (max (1- b) (point-min))
3777                        b 'intangible (cddr (memq 'intangible props)))))
3778
3779 (defsubst gnus-unhide-text (b e)
3780   "Remove hidden text properties from region between B and E."
3781   (remove-text-properties b e gnus-hidden-properties)
3782   (when (memq 'intangible gnus-hidden-properties)
3783     (gnus-put-text-property (max (1- b) (point-min))
3784                        b 'intangible nil)))
3785
3786 (defun gnus-hide-text-type (b e type)
3787   "Hide text of TYPE between B and E."
3788   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3789
3790 ;; Find out whether the gnus-visual TYPE is wanted.
3791 (defun gnus-visual-p (&optional type class)
3792   (and gnus-visual                      ; Has to be non-nil, at least.
3793        (if (not type)                   ; We don't care about type.
3794            gnus-visual
3795          (if (listp gnus-visual)        ; It's a list, so we check it.
3796              (or (memq type gnus-visual)
3797                  (memq class gnus-visual))
3798            t))))
3799
3800 (defun gnus-parent-headers (headers &optional generation)
3801   "Return the headers of the GENERATIONeth parent of HEADERS."
3802   (unless generation 
3803     (setq generation 1))
3804   (let (references parent)
3805     (while (and headers (not (zerop generation)))
3806       (setq references (mail-header-references headers))
3807       (when (and references
3808                  (setq parent (gnus-parent-id references))
3809                  (setq headers (car (gnus-id-to-thread parent))))
3810         (decf generation)))
3811     headers))
3812
3813 (defun gnus-parent-id (references)
3814   "Return the last Message-ID in REFERENCES."
3815   (when (and references
3816              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3817     (substring references (match-beginning 1) (match-end 1))))
3818
3819 (defun gnus-split-references (references)
3820   "Return a list of Message-IDs in REFERENCES."
3821   (let ((beg 0)
3822         ids)
3823     (while (string-match "<[^>]+>" references beg)
3824       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3825             ids))
3826     (nreverse ids)))
3827
3828 (defun gnus-buffer-live-p (buffer)
3829   "Say whether BUFFER is alive or not."
3830   (and buffer
3831        (get-buffer buffer)
3832        (buffer-name (get-buffer buffer))))
3833
3834 (defun gnus-ephemeral-group-p (group)
3835   "Say whether GROUP is ephemeral or not."
3836   (gnus-group-get-parameter group 'quit-config))
3837
3838 (defun gnus-group-quit-config (group)
3839   "Return the quit-config of GROUP."
3840   (gnus-group-get-parameter group 'quit-config))
3841
3842 (defun gnus-simplify-mode-line ()
3843   "Make mode lines a bit simpler."
3844   (setq mode-line-modified "-- ")
3845   (when (listp mode-line-format)
3846     (make-local-variable 'mode-line-format)
3847     (setq mode-line-format (copy-sequence mode-line-format))
3848     (when (equal (nth 3 mode-line-format) "   ")
3849       (setcar (nthcdr 3 mode-line-format) " "))))
3850
3851 ;;; List and range functions
3852
3853 (defun gnus-last-element (list)
3854   "Return last element of LIST."
3855   (while (cdr list)
3856     (setq list (cdr list)))
3857   (car list))
3858
3859 (defun gnus-copy-sequence (list)
3860   "Do a complete, total copy of a list."
3861   (if (and (consp list) (not (consp (cdr list))))
3862       (cons (car list) (cdr list))
3863     (mapcar (lambda (elem) (if (consp elem)
3864                                (if (consp (cdr elem))
3865                                    (gnus-copy-sequence elem)
3866                                  (cons (car elem) (cdr elem)))
3867                              elem))
3868             list)))
3869
3870 (defun gnus-set-difference (list1 list2)
3871   "Return a list of elements of LIST1 that do not appear in LIST2."
3872   (let ((list1 (copy-sequence list1)))
3873     (while list2
3874       (setq list1 (delq (car list2) list1))
3875       (setq list2 (cdr list2)))
3876     list1))
3877
3878 (defun gnus-sorted-complement (list1 list2)
3879   "Return a list of elements of LIST1 that do not appear in LIST2.
3880 Both lists have to be sorted over <."
3881   (let (out)
3882     (if (or (null list1) (null list2))
3883         (or list1 list2)
3884       (while (and list1 list2)
3885         (cond ((= (car list1) (car list2))
3886                (setq list1 (cdr list1)
3887                      list2 (cdr list2)))
3888               ((< (car list1) (car list2))
3889                (setq out (cons (car list1) out))
3890                (setq list1 (cdr list1)))
3891               (t
3892                (setq out (cons (car list2) out))
3893                (setq list2 (cdr list2)))))
3894       (nconc (nreverse out) (or list1 list2)))))
3895
3896 (defun gnus-intersection (list1 list2)
3897   (let ((result nil))
3898     (while list2
3899       (if (memq (car list2) list1)
3900           (setq result (cons (car list2) result)))
3901       (setq list2 (cdr list2)))
3902     result))
3903
3904 (defun gnus-sorted-intersection (list1 list2)
3905   ;; LIST1 and LIST2 have to be sorted over <.
3906   (let (out)
3907     (while (and list1 list2)
3908       (cond ((= (car list1) (car list2))
3909              (setq out (cons (car list1) out)
3910                    list1 (cdr list1)
3911                    list2 (cdr list2)))
3912             ((< (car list1) (car list2))
3913              (setq list1 (cdr list1)))
3914             (t
3915              (setq list2 (cdr list2)))))
3916     (nreverse out)))
3917
3918 (defun gnus-set-sorted-intersection (list1 list2)
3919   ;; LIST1 and LIST2 have to be sorted over <.
3920   ;; This function modifies LIST1.
3921   (let* ((top (cons nil list1))
3922          (prev top))
3923     (while (and list1 list2)
3924       (cond ((= (car list1) (car list2))
3925              (setq prev list1
3926                    list1 (cdr list1)
3927                    list2 (cdr list2)))
3928             ((< (car list1) (car list2))
3929              (setcdr prev (cdr list1))
3930              (setq list1 (cdr list1)))
3931             (t
3932              (setq list2 (cdr list2)))))
3933     (setcdr prev nil)
3934     (cdr top)))
3935
3936 (defun gnus-compress-sequence (numbers &optional always-list)
3937   "Convert list of numbers to a list of ranges or a single range.
3938 If ALWAYS-LIST is non-nil, this function will always release a list of
3939 ranges."
3940   (let* ((first (car numbers))
3941          (last (car numbers))
3942          result)
3943     (if (null numbers)
3944         nil
3945       (if (not (listp (cdr numbers)))
3946           numbers
3947         (while numbers
3948           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3949                 ((= (1+ last) (car numbers)) ;Still in sequence
3950                  (setq last (car numbers)))
3951                 (t                      ;End of one sequence
3952                  (setq result
3953                        (cons (if (= first last) first
3954                                (cons first last)) result))
3955                  (setq first (car numbers))
3956                  (setq last  (car numbers))))
3957           (setq numbers (cdr numbers)))
3958         (if (and (not always-list) (null result))
3959             (if (= first last) (list first) (cons first last))
3960           (nreverse (cons (if (= first last) first (cons first last))
3961                           result)))))))
3962
3963 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3964 (defun gnus-uncompress-range (ranges)
3965   "Expand a list of ranges into a list of numbers.
3966 RANGES is either a single range on the form `(num . num)' or a list of
3967 these ranges."
3968   (let (first last result)
3969     (cond
3970      ((null ranges)
3971       nil)
3972      ((not (listp (cdr ranges)))
3973       (setq first (car ranges))
3974       (setq last (cdr ranges))
3975       (while (<= first last)
3976         (setq result (cons first result))
3977         (setq first (1+ first)))
3978       (nreverse result))
3979      (t
3980       (while ranges
3981         (if (atom (car ranges))
3982             (if (numberp (car ranges))
3983                 (setq result (cons (car ranges) result)))
3984           (setq first (caar ranges))
3985           (setq last  (cdar ranges))
3986           (while (<= first last)
3987             (setq result (cons first result))
3988             (setq first (1+ first))))
3989         (setq ranges (cdr ranges)))
3990       (nreverse result)))))
3991
3992 (defun gnus-add-to-range (ranges list)
3993   "Return a list of ranges that has all articles from both RANGES and LIST.
3994 Note: LIST has to be sorted over `<'."
3995   (if (not ranges)
3996       (gnus-compress-sequence list t)
3997     (setq list (copy-sequence list))
3998     (or (listp (cdr ranges))
3999         (setq ranges (list ranges)))
4000     (let ((out ranges)
4001           ilist lowest highest temp)
4002       (while (and ranges list)
4003         (setq ilist list)
4004         (setq lowest (or (and (atom (car ranges)) (car ranges))
4005                          (caar ranges)))
4006         (while (and list (cdr list) (< (cadr list) lowest))
4007           (setq list (cdr list)))
4008         (if (< (car ilist) lowest)
4009             (progn
4010               (setq temp list)
4011               (setq list (cdr list))
4012               (setcdr temp nil)
4013               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4014         (setq highest (or (and (atom (car ranges)) (car ranges))
4015                           (cdar ranges)))
4016         (while (and list (<= (car list) highest))
4017           (setq list (cdr list)))
4018         (setq ranges (cdr ranges)))
4019       (if list
4020           (setq out (nconc (gnus-compress-sequence list t) out)))
4021       (setq out (sort out (lambda (r1 r2)
4022                             (< (or (and (atom r1) r1) (car r1))
4023                                (or (and (atom r2) r2) (car r2))))))
4024       (setq ranges out)
4025       (while ranges
4026         (if (atom (car ranges))
4027             (if (cdr ranges)
4028                 (if (atom (cadr ranges))
4029                     (if (= (1+ (car ranges)) (cadr ranges))
4030                         (progn
4031                           (setcar ranges (cons (car ranges)
4032                                                (cadr ranges)))
4033                           (setcdr ranges (cddr ranges))))
4034                   (if (= (1+ (car ranges)) (caadr ranges))
4035                       (progn
4036                         (setcar (cadr ranges) (car ranges))
4037                         (setcar ranges (cadr ranges))
4038                         (setcdr ranges (cddr ranges))))))
4039           (if (cdr ranges)
4040               (if (atom (cadr ranges))
4041                   (if (= (1+ (cdar ranges)) (cadr ranges))
4042                       (progn
4043                         (setcdr (car ranges) (cadr ranges))
4044                         (setcdr ranges (cddr ranges))))
4045                 (if (= (1+ (cdar ranges)) (caadr ranges))
4046                     (progn
4047                       (setcdr (car ranges) (cdadr ranges))
4048                       (setcdr ranges (cddr ranges)))))))
4049         (setq ranges (cdr ranges)))
4050       out)))
4051
4052 (defun gnus-remove-from-range (ranges list)
4053   "Return a list of ranges that has all articles from LIST removed from RANGES.
4054 Note: LIST has to be sorted over `<'."
4055   ;; !!! This function shouldn't look like this, but I've got a headache.
4056   (gnus-compress-sequence
4057    (gnus-sorted-complement
4058     (gnus-uncompress-range ranges) list)))
4059
4060 (defun gnus-member-of-range (number ranges)
4061   (if (not (listp (cdr ranges)))
4062       (and (>= number (car ranges))
4063            (<= number (cdr ranges)))
4064     (let ((not-stop t))
4065       (while (and ranges
4066                   (if (numberp (car ranges))
4067                       (>= number (car ranges))
4068                     (>= number (caar ranges)))
4069                   not-stop)
4070         (if (if (numberp (car ranges))
4071                 (= number (car ranges))
4072               (and (>= number (caar ranges))
4073                    (<= number (cdar ranges))))
4074             (setq not-stop nil))
4075         (setq ranges (cdr ranges)))
4076       (not not-stop))))
4077
4078 (defun gnus-range-length (range)
4079   "Return the length RANGE would have if uncompressed."
4080   (length (gnus-uncompress-range range)))
4081
4082 (defun gnus-sublist-p (list sublist)
4083   "Test whether all elements in SUBLIST are members of LIST."
4084   (let ((sublistp t))
4085     (while sublist
4086       (unless (memq (pop sublist) list)
4087         (setq sublistp nil
4088               sublist nil)))
4089     sublistp))
4090
4091 \f
4092 ;;;
4093 ;;; Gnus group mode
4094 ;;;
4095
4096 (defvar gnus-group-mode-map nil)
4097 (put 'gnus-group-mode 'mode-class 'special)
4098
4099 (unless gnus-group-mode-map
4100   (setq gnus-group-mode-map (make-keymap))
4101   (suppress-keymap gnus-group-mode-map)
4102
4103   (gnus-define-keys gnus-group-mode-map
4104     " " gnus-group-read-group
4105     "=" gnus-group-select-group
4106     "\r" gnus-group-select-group
4107     "\M-\r" gnus-group-quick-select-group
4108     "j" gnus-group-jump-to-group
4109     "n" gnus-group-next-unread-group
4110     "p" gnus-group-prev-unread-group
4111     "\177" gnus-group-prev-unread-group
4112     [delete] gnus-group-prev-unread-group
4113     "N" gnus-group-next-group
4114     "P" gnus-group-prev-group
4115     "\M-n" gnus-group-next-unread-group-same-level
4116     "\M-p" gnus-group-prev-unread-group-same-level
4117     "," gnus-group-best-unread-group
4118     "." gnus-group-first-unread-group
4119     "u" gnus-group-unsubscribe-current-group
4120     "U" gnus-group-unsubscribe-group
4121     "c" gnus-group-catchup-current
4122     "C" gnus-group-catchup-current-all
4123     "l" gnus-group-list-groups
4124     "L" gnus-group-list-all-groups
4125     "m" gnus-group-mail
4126     "g" gnus-group-get-new-news
4127     "\M-g" gnus-group-get-new-news-this-group
4128     "R" gnus-group-restart
4129     "r" gnus-group-read-init-file
4130     "B" gnus-group-browse-foreign-server
4131     "b" gnus-group-check-bogus-groups
4132     "F" gnus-find-new-newsgroups
4133     "\C-c\C-d" gnus-group-describe-group
4134     "\M-d" gnus-group-describe-all-groups
4135     "\C-c\C-a" gnus-group-apropos
4136     "\C-c\M-\C-a" gnus-group-description-apropos
4137     "a" gnus-group-post-news
4138     "\ek" gnus-group-edit-local-kill
4139     "\eK" gnus-group-edit-global-kill
4140     "\C-k" gnus-group-kill-group
4141     "\C-y" gnus-group-yank-group
4142     "\C-w" gnus-group-kill-region
4143     "\C-x\C-t" gnus-group-transpose-groups
4144     "\C-c\C-l" gnus-group-list-killed
4145     "\C-c\C-x" gnus-group-expire-articles
4146     "\C-c\M-\C-x" gnus-group-expire-all-groups
4147     "V" gnus-version
4148     "s" gnus-group-save-newsrc
4149     "z" gnus-group-suspend
4150     "Z" gnus-group-clear-dribble
4151     "q" gnus-group-exit
4152     "Q" gnus-group-quit
4153     "?" gnus-group-describe-briefly
4154     "\C-c\C-i" gnus-info-find-node
4155     "\M-e" gnus-group-edit-group-method
4156     "^" gnus-group-enter-server-mode
4157     gnus-mouse-2 gnus-mouse-pick-group
4158     "<" beginning-of-buffer
4159     ">" end-of-buffer
4160     "\C-c\C-b" gnus-bug
4161     "\C-c\C-s" gnus-group-sort-groups
4162     "t" gnus-topic-mode
4163     "\C-c\M-g" gnus-activate-all-groups
4164     "\M-&" gnus-group-universal-argument
4165     "#" gnus-group-mark-group
4166     "\M-#" gnus-group-unmark-group)
4167
4168   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4169     "m" gnus-group-mark-group
4170     "u" gnus-group-unmark-group
4171     "w" gnus-group-mark-region
4172     "m" gnus-group-mark-buffer
4173     "r" gnus-group-mark-regexp
4174     "U" gnus-group-unmark-all-groups)
4175
4176   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4177     "d" gnus-group-make-directory-group
4178     "h" gnus-group-make-help-group
4179     "a" gnus-group-make-archive-group
4180     "k" gnus-group-make-kiboze-group
4181     "m" gnus-group-make-group
4182     "E" gnus-group-edit-group
4183     "e" gnus-group-edit-group-method
4184     "p" gnus-group-edit-group-parameters
4185     "v" gnus-group-add-to-virtual
4186     "V" gnus-group-make-empty-virtual
4187     "D" gnus-group-enter-directory
4188     "f" gnus-group-make-doc-group
4189     "r" gnus-group-rename-group
4190     "\177" gnus-group-delete-group
4191     [delete] gnus-group-delete-group)
4192
4193    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4194      "b" gnus-group-brew-soup
4195      "w" gnus-soup-save-areas
4196      "s" gnus-soup-send-replies
4197      "p" gnus-soup-pack-packet
4198      "r" nnsoup-pack-replies)
4199
4200    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4201      "s" gnus-group-sort-groups
4202      "a" gnus-group-sort-groups-by-alphabet
4203      "u" gnus-group-sort-groups-by-unread
4204      "l" gnus-group-sort-groups-by-level
4205      "v" gnus-group-sort-groups-by-score
4206      "r" gnus-group-sort-groups-by-rank
4207      "m" gnus-group-sort-groups-by-method)
4208
4209    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4210      "k" gnus-group-list-killed
4211      "z" gnus-group-list-zombies
4212      "s" gnus-group-list-groups
4213      "u" gnus-group-list-all-groups
4214      "A" gnus-group-list-active
4215      "a" gnus-group-apropos
4216      "d" gnus-group-description-apropos
4217      "m" gnus-group-list-matching
4218      "M" gnus-group-list-all-matching
4219      "l" gnus-group-list-level)
4220
4221    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4222      "f" gnus-score-flush-cache)
4223
4224    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4225      "f" gnus-group-fetch-faq)
4226
4227    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4228      "l" gnus-group-set-current-level
4229      "t" gnus-group-unsubscribe-current-group
4230      "s" gnus-group-unsubscribe-group
4231      "k" gnus-group-kill-group
4232      "y" gnus-group-yank-group
4233      "w" gnus-group-kill-region
4234      "\C-k" gnus-group-kill-level
4235      "z" gnus-group-kill-all-zombies))
4236
4237 (defun gnus-group-mode ()
4238   "Major mode for reading news.
4239
4240 All normal editing commands are switched off.
4241 \\<gnus-group-mode-map>
4242 The group buffer lists (some of) the groups available.  For instance,
4243 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4244 lists all zombie groups.
4245
4246 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4247 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4248
4249 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4250
4251 The following commands are available:
4252
4253 \\{gnus-group-mode-map}"
4254   (interactive)
4255   (when (and menu-bar-mode
4256              (gnus-visual-p 'group-menu 'menu))
4257     (gnus-group-make-menu-bar))
4258   (kill-all-local-variables)
4259   (gnus-simplify-mode-line)
4260   (setq major-mode 'gnus-group-mode)
4261   (setq mode-name "Group")
4262   (gnus-group-set-mode-line)
4263   (setq mode-line-process nil)
4264   (use-local-map gnus-group-mode-map)
4265   (buffer-disable-undo (current-buffer))
4266   (setq truncate-lines t)
4267   (setq buffer-read-only t)
4268   (gnus-make-local-hook 'post-command-hook)
4269   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved)
4270   (run-hooks 'gnus-group-mode-hook))
4271
4272 (defun gnus-clear-inboxes-moved ()
4273   (setq nnmail-moved-inboxes nil))
4274
4275 (defun gnus-mouse-pick-group (e)
4276   "Enter the group under the mouse pointer."
4277   (interactive "e")
4278   (mouse-set-point e)
4279   (gnus-group-read-group nil))
4280
4281 ;; Look at LEVEL and find out what the level is really supposed to be.
4282 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4283 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4284 (defun gnus-group-default-level (&optional level number-or-nil)
4285   (cond
4286    (gnus-group-use-permanent-levels
4287     (or (setq gnus-group-use-permanent-levels
4288               (or level (if (numberp gnus-group-use-permanent-levels)
4289                             gnus-group-use-permanent-levels
4290                           (or gnus-group-default-list-level
4291                               gnus-level-subscribed))))
4292         gnus-group-default-list-level gnus-level-subscribed))
4293    (number-or-nil
4294     level)
4295    (t
4296     (or level gnus-group-default-list-level gnus-level-subscribed))))
4297
4298 ;;;###autoload
4299 (defun gnus-slave-no-server (&optional arg)
4300   "Read network news as a slave, without connecting to local server"
4301   (interactive "P")
4302   (gnus-no-server arg t))
4303
4304 ;;;###autoload
4305 (defun gnus-no-server (&optional arg slave)
4306   "Read network news.
4307 If ARG is a positive number, Gnus will use that as the
4308 startup level.  If ARG is nil, Gnus will be started at level 2.
4309 If ARG is non-nil and not a positive number, Gnus will
4310 prompt the user for the name of an NNTP server to use.
4311 As opposed to `gnus', this command will not connect to the local server."
4312   (interactive "P")
4313   (let ((val (or arg (1- gnus-level-default-subscribed))))
4314     (gnus val t slave)
4315     (make-local-variable 'gnus-group-use-permanent-levels)
4316     (setq gnus-group-use-permanent-levels val)))
4317
4318 ;;;###autoload
4319 (defun gnus-slave (&optional arg)
4320   "Read news as a slave."
4321   (interactive "P")
4322   (gnus arg nil 'slave))
4323
4324 ;;;###autoload
4325 (defun gnus-other-frame (&optional arg)
4326   "Pop up a frame to read news."
4327   (interactive "P")
4328   (if (get-buffer gnus-group-buffer)
4329       (let ((pop-up-frames t))
4330         (gnus arg))
4331     (select-frame (make-frame))
4332     (gnus arg)))
4333
4334 ;;;###autoload
4335 (defun gnus (&optional arg dont-connect slave)
4336   "Read network news.
4337 If ARG is non-nil and a positive number, Gnus will use that as the
4338 startup level.  If ARG is non-nil and not a positive number, Gnus will
4339 prompt the user for the name of an NNTP server to use."
4340   (interactive "P")
4341
4342   (if (get-buffer gnus-group-buffer)
4343       (progn
4344         (switch-to-buffer gnus-group-buffer)
4345         (gnus-group-get-new-news))
4346
4347     (gnus-clear-system)
4348     (nnheader-init-server-buffer)
4349     (gnus-read-init-file)
4350     (setq gnus-slave slave)
4351
4352     (gnus-group-setup-buffer)
4353     (let ((buffer-read-only nil))
4354       (erase-buffer)
4355       (if (not gnus-inhibit-startup-message)
4356           (progn
4357             (gnus-group-startup-message)
4358             (sit-for 0))))
4359
4360     (let ((level (and (numberp arg) (> arg 0) arg))
4361           did-connect)
4362       (unwind-protect
4363           (progn
4364             (or dont-connect
4365                 (setq did-connect
4366                       (gnus-start-news-server (and arg (not level))))))
4367         (if (and (not dont-connect)
4368                  (not did-connect))
4369             (gnus-group-quit)
4370           (run-hooks 'gnus-startup-hook)
4371           ;; NNTP server is successfully open.
4372
4373           ;; Find the current startup file name.
4374           (setq gnus-current-startup-file
4375                 (gnus-make-newsrc-file gnus-startup-file))
4376
4377           ;; Read the dribble file.
4378           (when (or gnus-slave gnus-use-dribble-file)
4379             (gnus-dribble-read-file))
4380
4381           ;; Allow using GroupLens predictions.
4382           (when gnus-use-grouplens
4383             (bbb-login)
4384             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4385
4386           (gnus-summary-make-display-table)
4387           ;; Do the actual startup.
4388           (gnus-setup-news nil level dont-connect)
4389           ;; Generate the group buffer.
4390           (gnus-group-list-groups level)
4391           (gnus-group-first-unread-group)
4392           (gnus-configure-windows 'group)
4393           (gnus-group-set-mode-line))))))
4394
4395 (defun gnus-unload ()
4396   "Unload all Gnus features."
4397   (interactive)
4398   (or (boundp 'load-history)
4399       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4400   (let ((history load-history)
4401         feature)
4402     (while history
4403       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4404            (setq feature (cdr (assq 'provide (car history))))
4405            (unload-feature feature 'force))
4406       (setq history (cdr history)))))
4407
4408 (defun gnus-compile ()
4409   "Byte-compile the user-defined format specs."
4410   (interactive)
4411   (let ((entries gnus-format-specs)
4412         entry gnus-tmp-func)
4413     (save-excursion
4414       (gnus-message 7 "Compiling format specs...")
4415
4416       (while entries
4417         (setq entry (pop entries))
4418         (if (eq (car entry) 'version)
4419             (setq gnus-format-specs (delq entry gnus-format-specs))
4420           (when (and (listp (caddr entry))
4421                      (not (eq 'byte-code (caaddr entry))))
4422             (fset 'gnus-tmp-func
4423                   `(lambda () ,(caddr entry)))
4424             (byte-compile 'gnus-tmp-func)
4425             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4426
4427       (push (cons 'version emacs-version) gnus-format-specs)
4428
4429       (gnus-message 7 "Compiling user specs...done"))))
4430
4431 (defun gnus-indent-rigidly (start end arg)
4432   "Indent rigidly using only spaces and no tabs."
4433   (save-excursion
4434     (save-restriction
4435       (narrow-to-region start end)
4436       (indent-rigidly start end arg)
4437       (goto-char (point-min))
4438       (while (search-forward "\t" nil t)
4439         (replace-match "        " t t)))))
4440
4441 (defun gnus-group-startup-message (&optional x y)
4442   "Insert startup message in current buffer."
4443   ;; Insert the message.
4444   (erase-buffer)
4445   (insert
4446    (format "              %s
4447           _    ___ _             _
4448           _ ___ __ ___  __    _ ___
4449           __   _     ___    __  ___
4450               _           ___     _
4451              _  _ __             _
4452              ___   __            _
4453                    __           _
4454                     _      _   _
4455                    _      _    _
4456                       _  _    _
4457                   __  ___
4458                  _   _ _     _
4459                 _   _
4460               _    _
4461              _    _
4462             _
4463           __
4464
4465 "
4466            ""))
4467   ;; And then hack it.
4468   (gnus-indent-rigidly (point-min) (point-max)
4469                        (/ (max (- (window-width) (or x 46)) 0) 2))
4470   (goto-char (point-min))
4471   (forward-line 1)
4472   (let* ((pheight (count-lines (point-min) (point-max)))
4473          (wheight (window-height))
4474          (rest (- wheight pheight)))
4475     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4476   ;; Fontify some.
4477   (goto-char (point-min))
4478   (and (search-forward "Praxis" nil t)
4479        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4480   (goto-char (point-min))
4481   (let* ((mode-string (gnus-group-set-mode-line)))
4482     (setq mode-line-buffer-identification
4483           (list (concat gnus-version (substring (car mode-string) 4))))
4484     (set-buffer-modified-p t)))
4485
4486 (defun gnus-group-setup-buffer ()
4487   (or (get-buffer gnus-group-buffer)
4488       (progn
4489         (switch-to-buffer gnus-group-buffer)
4490         (gnus-add-current-to-buffer-list)
4491         (gnus-group-mode)
4492         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4493
4494 (defun gnus-group-list-groups (&optional level unread lowest)
4495   "List newsgroups with level LEVEL or lower that have unread articles.
4496 Default is all subscribed groups.
4497 If argument UNREAD is non-nil, groups with no unread articles are also
4498 listed."
4499   (interactive (list (if current-prefix-arg
4500                          (prefix-numeric-value current-prefix-arg)
4501                        (or
4502                         (gnus-group-default-level nil t)
4503                         gnus-group-default-list-level
4504                         gnus-level-subscribed))))
4505   (or level
4506       (setq level (car gnus-group-list-mode)
4507             unread (cdr gnus-group-list-mode)))
4508   (setq level (gnus-group-default-level level))
4509   (gnus-group-setup-buffer)             ;May call from out of group buffer
4510   (gnus-update-format-specifications)
4511   (let ((case-fold-search nil)
4512         (props (text-properties-at (gnus-point-at-bol)))
4513         (group (gnus-group-group-name)))
4514     (set-buffer gnus-group-buffer)
4515     (funcall gnus-group-prepare-function level unread lowest)
4516     (if (zerop (buffer-size))
4517         (gnus-message 5 gnus-no-groups-message)
4518       (goto-char (point-max))
4519       (when (or (not gnus-group-goto-next-group-function)
4520                 (not (funcall gnus-group-goto-next-group-function 
4521                               group props)))
4522         (if (not group)
4523             ;; Go to the first group with unread articles.
4524             (gnus-group-search-forward t)
4525           ;; Find the right group to put point on.  If the current group
4526           ;; has disappeared in the new listing, try to find the next
4527           ;; one.        If no next one can be found, just leave point at the
4528           ;; first newsgroup in the buffer.
4529           (if (not (gnus-goto-char
4530                     (text-property-any
4531                      (point-min) (point-max)
4532                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4533               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4534                 (while (and newsrc
4535                             (not (gnus-goto-char
4536                                   (text-property-any
4537                                    (point-min) (point-max) 'gnus-group
4538                                    (gnus-intern-safe
4539                                     (caar newsrc) gnus-active-hashtb)))))
4540                   (setq newsrc (cdr newsrc)))
4541                 (or newsrc (progn (goto-char (point-max))
4542                                   (forward-line -1)))))))
4543       ;; Adjust cursor point.
4544       (gnus-group-position-point))))
4545
4546 (defun gnus-group-list-level (level &optional all)
4547   "List groups on LEVEL.
4548 If ALL (the prefix), also list groups that have no unread articles."
4549   (interactive "nList groups on level: \nP")
4550   (gnus-group-list-groups level all level))
4551
4552 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4553   "List all newsgroups with unread articles of level LEVEL or lower.
4554 If ALL is non-nil, list groups that have no unread articles.
4555 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4556 If REGEXP, only list groups matching REGEXP."
4557   (set-buffer gnus-group-buffer)
4558   (let ((buffer-read-only nil)
4559         (newsrc (cdr gnus-newsrc-alist))
4560         (lowest (or lowest 1))
4561         info clevel unread group params)
4562     (erase-buffer)
4563     (if (< lowest gnus-level-zombie)
4564         ;; List living groups.
4565         (while newsrc
4566           (setq info (car newsrc)
4567                 group (gnus-info-group info)
4568                 params (gnus-info-params info)
4569                 newsrc (cdr newsrc)
4570                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4571           (and unread                   ; This group might be bogus
4572                (or (not regexp)
4573                    (string-match regexp group))
4574                (<= (setq clevel (gnus-info-level info)) level)
4575                (>= clevel lowest)
4576                (or all                  ; We list all groups?
4577                    (if (eq unread t)    ; Unactivated?
4578                        gnus-group-list-inactive-groups ; We list unactivated 
4579                      (> unread 0))      ; We list groups with unread articles
4580                    (and gnus-list-groups-with-ticked-articles
4581                         (cdr (assq 'tick (gnus-info-marks info))))
4582                                         ; And groups with tickeds
4583                    ;; Check for permanent visibility.
4584                    (and gnus-permanently-visible-groups
4585                         (string-match gnus-permanently-visible-groups
4586                                       group))
4587                    (memq 'visible params)
4588                    (cdr (assq 'visible params)))
4589                (gnus-group-insert-group-line
4590                 group (gnus-info-level info)
4591                 (gnus-info-marks info) unread (gnus-info-method info)))))
4592
4593     ;; List dead groups.
4594     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4595          (gnus-group-prepare-flat-list-dead
4596           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4597           gnus-level-zombie ?Z
4598           regexp))
4599     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4600          (gnus-group-prepare-flat-list-dead
4601           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4602           gnus-level-killed ?K regexp))
4603
4604     (gnus-group-set-mode-line)
4605     (setq gnus-group-list-mode (cons level all))
4606     (run-hooks 'gnus-group-prepare-hook)))
4607
4608 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4609   ;; List zombies and killed lists somewhat faster, which was
4610   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4611   ;; this by ignoring the group format specification altogether.
4612   (let (group)
4613     (if regexp
4614         ;; This loop is used when listing groups that match some
4615         ;; regexp.
4616         (while groups
4617           (setq group (pop groups))
4618           (when (string-match regexp group)
4619             (gnus-add-text-properties
4620              (point) (prog1 (1+ (point))
4621                        (insert " " mark "     *: " group "\n"))
4622              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4623                    'gnus-unread t
4624                    'gnus-level level))))
4625       ;; This loop is used when listing all groups.
4626       (while groups
4627         (gnus-add-text-properties
4628          (point) (prog1 (1+ (point))
4629                    (insert " " mark "     *: "
4630                            (setq group (pop groups)) "\n"))
4631          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4632                'gnus-unread t
4633                'gnus-level level))))))
4634
4635 (defmacro gnus-group-real-name (group)
4636   "Find the real name of a foreign newsgroup."
4637   `(let ((gname ,group))
4638      (if (string-match ":[^:]+$" gname)
4639          (substring gname (1+ (match-beginning 0)))
4640        gname)))
4641
4642 (defsubst gnus-server-add-address (method)
4643   (let ((method-name (symbol-name (car method))))
4644     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4645              (not (assq (intern (concat method-name "-address")) method)))
4646         (append method (list (list (intern (concat method-name "-address"))
4647                                    (nth 1 method))))
4648       method)))
4649
4650 (defsubst gnus-server-get-method (group method)
4651   ;; Input either a server name, and extended server name, or a
4652   ;; select method, and return a select method.
4653   (cond ((stringp method)
4654          (gnus-server-to-method method))
4655         ((equal method gnus-select-method)
4656          gnus-select-method)
4657         ((and (stringp (car method)) group)
4658          (gnus-server-extend-method group method))
4659         ((and method (not group)
4660               (equal (cadr method) ""))
4661          method)
4662         (t
4663          (gnus-server-add-address method))))
4664
4665 (defun gnus-server-to-method (server)
4666   "Map virtual server names to select methods."
4667   (or 
4668    ;; Is this a method, perhaps?
4669    (and server (listp server) server)
4670    ;; Perhaps this is the native server?
4671    (and (equal server "native") gnus-select-method)
4672    ;; It should be in the server alist.
4673    (cdr (assoc server gnus-server-alist))
4674    ;; If not, we look through all the opened server
4675    ;; to see whether we can find it there.
4676    (let ((opened gnus-opened-servers))
4677      (while (and opened
4678                  (not (equal server (format "%s:%s" (caaar opened)
4679                                             (cadaar opened)))))
4680        (pop opened))
4681      (caar opened))))
4682
4683 (defmacro gnus-method-equal (ss1 ss2)
4684   "Say whether two servers are equal."
4685   `(let ((s1 ,ss1)
4686          (s2 ,ss2))
4687      (or (equal s1 s2)
4688          (and (= (length s1) (length s2))
4689               (progn
4690                 (while (and s1 (member (car s1) s2))
4691                   (setq s1 (cdr s1)))
4692                 (null s1))))))
4693
4694 (defun gnus-server-equal (m1 m2)
4695   "Say whether two methods are equal."
4696   (let ((m1 (cond ((null m1) gnus-select-method)
4697                   ((stringp m1) (gnus-server-to-method m1))
4698                   (t m1)))
4699         (m2 (cond ((null m2) gnus-select-method)
4700                   ((stringp m2) (gnus-server-to-method m2))
4701                   (t m2))))
4702     (gnus-method-equal m1 m2)))
4703
4704 (defun gnus-servers-using-backend (backend)
4705   "Return a list of known servers using BACKEND."
4706   (let ((opened gnus-opened-servers)
4707         out)
4708     (while opened
4709       (when (eq backend (caaar opened))
4710         (push (caar opened) out))
4711       (pop opened))
4712     out))
4713
4714 (defun gnus-group-prefixed-name (group method)
4715   "Return the whole name from GROUP and METHOD."
4716   (and (stringp method) (setq method (gnus-server-to-method method)))
4717   (concat (format "%s" (car method))
4718           (if (and
4719                (or (assoc (format "%s" (car method)) 
4720                           (gnus-methods-using 'address))
4721                    (gnus-server-equal method gnus-message-archive-method))
4722                (nth 1 method)
4723                (not (string= (nth 1 method) "")))
4724               (concat "+" (nth 1 method)))
4725           ":" group))
4726
4727 (defun gnus-group-real-prefix (group)
4728   "Return the prefix of the current group name."
4729   (if (string-match "^[^:]+:" group)
4730       (substring group 0 (match-end 0))
4731     ""))
4732
4733 (defun gnus-group-method (group)
4734   "Return the server or method used for selecting GROUP."
4735   (let ((prefix (gnus-group-real-prefix group)))
4736     (if (equal prefix "")
4737         gnus-select-method
4738       (let ((servers gnus-opened-servers)
4739             (server "")
4740             backend possible found)
4741         (if (string-match "^[^\\+]+\\+" prefix)
4742             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4743                   server (substring prefix (match-end 0) (1- (length prefix))))
4744           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4745         (while servers
4746           (when (eq (caaar servers) backend)
4747             (setq possible (caar servers))
4748             (when (equal (cadaar servers) server)
4749               (setq found (caar servers))))
4750           (pop servers))
4751         (or (car (rassoc found gnus-server-alist))
4752             found
4753             (car (rassoc possible gnus-server-alist))
4754             possible
4755             (list backend server))))))
4756
4757 (defsubst gnus-secondary-method-p (method)
4758   "Return whether METHOD is a secondary select method."
4759   (let ((methods gnus-secondary-select-methods)
4760         (gmethod (gnus-server-get-method nil method)))
4761     (while (and methods
4762                 (not (equal (gnus-server-get-method nil (car methods))
4763                             gmethod)))
4764       (setq methods (cdr methods)))
4765     methods))
4766
4767 (defun gnus-group-foreign-p (group)
4768   "Say whether a group is foreign or not."
4769   (and (not (gnus-group-native-p group))
4770        (not (gnus-group-secondary-p group))))
4771
4772 (defun gnus-group-native-p (group)
4773   "Say whether the group is native or not."
4774   (not (string-match ":" group)))
4775
4776 (defun gnus-group-secondary-p (group)
4777   "Say whether the group is secondary or not."
4778   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4779
4780 (defun gnus-group-get-parameter (group &optional symbol)
4781   "Returns the group parameters for GROUP.
4782 If SYMBOL, return the value of that symbol in the group parameters."
4783   (let ((params (gnus-info-params (gnus-get-info group))))
4784     (if symbol
4785         (gnus-group-parameter-value params symbol)
4786       params)))
4787
4788 (defun gnus-group-parameter-value (params symbol)
4789   "Return the value of SYMBOL in group PARAMS."
4790   (or (car (memq symbol params))        ; It's either a simple symbol
4791       (cdr (assq symbol params))))      ; or a cons.
4792
4793 (defun gnus-group-add-parameter (group param)
4794   "Add parameter PARAM to GROUP."
4795   (let ((info (gnus-get-info group)))
4796     (if (not info)
4797         () ; This is a dead group.  We just ignore it.
4798       ;; Cons the new param to the old one and update.
4799       (gnus-group-set-info (cons param (gnus-info-params info))
4800                            group 'params))))
4801
4802 (defun gnus-group-set-parameter (group name value)
4803   "Set parameter NAME to VALUE in GROUP."
4804   (let ((info (gnus-get-info group)))
4805     (if (not info)
4806         () ; This is a dead group.  We just ignore it.
4807       (let ((old-params (gnus-info-params info))
4808             (new-params (list (cons name value))))
4809         (while old-params
4810           (if (or (not (listp (car old-params)))
4811                   (not (eq (caar old-params) name)))
4812               (setq new-params (append new-params (list (car old-params)))))
4813           (setq old-params (cdr old-params)))
4814         (gnus-group-set-info new-params group 'params)))))
4815
4816 (defun gnus-group-add-score (group &optional score)
4817   "Add SCORE to the GROUP score.
4818 If SCORE is nil, add 1 to the score of GROUP."
4819   (let ((info (gnus-get-info group)))
4820     (when info
4821       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4822
4823 (defun gnus-summary-bubble-group ()
4824   "Increase the score of the current group.
4825 This is a handy function to add to `gnus-summary-exit-hook' to
4826 increase the score of each group you read."
4827   (gnus-group-add-score gnus-newsgroup-name))
4828
4829 (defun gnus-group-set-info (info &optional method-only-group part)
4830   (let* ((entry (gnus-gethash
4831                  (or method-only-group (gnus-info-group info))
4832                  gnus-newsrc-hashtb))
4833          (part-info info)
4834          (info (if method-only-group (nth 2 entry) info))
4835          method)
4836     (when method-only-group
4837       (unless entry
4838         (error "Trying to change non-existent group %s" method-only-group))
4839       ;; We have received parts of the actual group info - either the
4840       ;; select method or the group parameters.  We first check
4841       ;; whether we have to extend the info, and if so, do that.
4842       (let ((len (length info))
4843             (total (if (eq part 'method) 5 6)))
4844         (when (< len total)
4845           (setcdr (nthcdr (1- len) info)
4846                   (make-list (- total len) nil)))
4847         ;; Then we enter the new info.
4848         (setcar (nthcdr (1- total) info) part-info)))
4849     (unless entry
4850       ;; This is a new group, so we just create it.
4851       (save-excursion
4852         (set-buffer gnus-group-buffer)
4853         (setq method (gnus-info-method info))
4854         (when (gnus-server-equal method "native")
4855           (setq method nil))
4856         (save-excursion
4857           (set-buffer gnus-group-buffer)
4858           (if method
4859               ;; It's a foreign group...
4860               (gnus-group-make-group
4861                (gnus-group-real-name (gnus-info-group info))
4862                (if (stringp method) method
4863                  (prin1-to-string (car method)))
4864                (and (consp method)
4865                     (nth 1 (gnus-info-method info))))
4866             ;; It's a native group.
4867             (gnus-group-make-group (gnus-info-group info))))
4868         (gnus-message 6 "Note: New group created")
4869         (setq entry
4870               (gnus-gethash (gnus-group-prefixed-name
4871                              (gnus-group-real-name (gnus-info-group info))
4872                              (or (gnus-info-method info) gnus-select-method))
4873                             gnus-newsrc-hashtb))))
4874     ;; Whether it was a new group or not, we now have the entry, so we
4875     ;; can do the update.
4876     (if entry
4877         (progn
4878           (setcar (nthcdr 2 entry) info)
4879           (when (and (not (eq (car entry) t))
4880                      (gnus-active (gnus-info-group info)))
4881             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4882       (error "No such group: %s" (gnus-info-group info)))))
4883
4884 (defun gnus-group-set-method-info (group select-method)
4885   (gnus-group-set-info select-method group 'method))
4886
4887 (defun gnus-group-set-params-info (group params)
4888   (gnus-group-set-info params group 'params))
4889
4890 (defun gnus-group-update-group-line ()
4891   "Update the current line in the group buffer."
4892   (let* ((buffer-read-only nil)
4893          (group (gnus-group-group-name))
4894          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4895          gnus-group-indentation)
4896     (and entry
4897          (not (gnus-ephemeral-group-p group))
4898          (gnus-dribble-enter
4899           (concat "(gnus-group-set-info '"
4900                   (prin1-to-string (nth 2 entry)) ")")))
4901     (setq gnus-group-indentation (gnus-group-group-indentation))
4902     (gnus-delete-line)
4903     (gnus-group-insert-group-line-info group)
4904     (forward-line -1)
4905     (gnus-group-position-point)))
4906
4907 (defun gnus-group-insert-group-line-info (group)
4908   "Insert GROUP on the current line."
4909   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4910         active info)
4911     (if entry
4912         (progn
4913           ;; (Un)subscribed group.
4914           (setq info (nth 2 entry))
4915           (gnus-group-insert-group-line
4916            group (gnus-info-level info) (gnus-info-marks info)
4917            (or (car entry) t) (gnus-info-method info)))
4918       ;; This group is dead.
4919       (gnus-group-insert-group-line
4920        group
4921        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4922        nil
4923        (if (setq active (gnus-active group))
4924            (- (1+ (cdr active)) (car active)) 0)
4925        nil))))
4926
4927 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4928                                                     gnus-tmp-marked number
4929                                                     gnus-tmp-method)
4930   "Insert a group line in the group buffer."
4931   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4932          (gnus-tmp-number-total
4933           (if gnus-tmp-active
4934               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4935             0))
4936          (gnus-tmp-number-of-unread
4937           (if (numberp number) (int-to-string (max 0 number))
4938             "*"))
4939          (gnus-tmp-number-of-read
4940           (if (numberp number)
4941               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4942             "*"))
4943          (gnus-tmp-subscribed
4944           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4945                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4946                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4947                 (t ?K)))
4948          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4949          (gnus-tmp-newsgroup-description
4950           (if gnus-description-hashtb
4951               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4952             ""))
4953          (gnus-tmp-moderated
4954           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4955          (gnus-tmp-moderated-string
4956           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4957          (gnus-tmp-method
4958           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4959          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4960          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4961          (gnus-tmp-news-method-string
4962           (if gnus-tmp-method
4963               (format "(%s:%s)" (car gnus-tmp-method)
4964                       (cadr gnus-tmp-method)) ""))
4965          (gnus-tmp-marked-mark
4966           (if (and (numberp number)
4967                    (zerop number)
4968                    (cdr (assq 'tick gnus-tmp-marked)))
4969               ?* ? ))
4970          (gnus-tmp-process-marked
4971           (if (member gnus-tmp-group gnus-group-marked)
4972               gnus-process-mark ? ))
4973          (gnus-tmp-grouplens
4974           (or (and gnus-use-grouplens
4975                    (bbb-grouplens-group-p gnus-tmp-group))
4976               ""))
4977          (buffer-read-only nil)
4978          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4979     (beginning-of-line)
4980     (gnus-add-text-properties
4981      (point)
4982      (prog1 (1+ (point))
4983        ;; Insert the text.
4984        (eval gnus-group-line-format-spec))
4985      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4986        gnus-unread ,(if (numberp number)
4987                         (string-to-int gnus-tmp-number-of-unread)
4988                       t)
4989        gnus-marked ,gnus-tmp-marked-mark
4990        gnus-indentation ,gnus-group-indentation
4991        gnus-level ,gnus-tmp-level))
4992     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4993       (forward-line -1)
4994       (run-hooks 'gnus-group-update-hook)
4995       (forward-line))
4996     ;; Allow XEmacs to remove front-sticky text properties.
4997     (gnus-group-remove-excess-properties)))
4998
4999 (defun gnus-group-update-group (group &optional visible-only)
5000   "Update all lines where GROUP appear.
5001 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5002 already."
5003   (save-excursion
5004     (set-buffer gnus-group-buffer)
5005     ;; The buffer may be narrowed.
5006     (save-restriction
5007       (widen)
5008       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5009             (loc (point-min))
5010             found buffer-read-only)
5011         ;; Enter the current status into the dribble buffer.
5012         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5013           (if (and entry (not (gnus-ephemeral-group-p group)))
5014               (gnus-dribble-enter
5015                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5016                        ")"))))
5017         ;; Find all group instances.  If topics are in use, each group
5018         ;; may be listed in more than once.
5019         (while (setq loc (text-property-any
5020                           loc (point-max) 'gnus-group ident))
5021           (setq found t)
5022           (goto-char loc)
5023           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5024             (gnus-delete-line)
5025             (gnus-group-insert-group-line-info group))
5026           (setq loc (1+ loc)))
5027         (unless (or found visible-only)
5028           ;; No such line in the buffer, find out where it's supposed to
5029           ;; go, and insert it there (or at the end of the buffer).
5030           (if gnus-goto-missing-group-function
5031               (funcall gnus-goto-missing-group-function group)
5032             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5033               (while (and entry (car entry)
5034                           (not
5035                            (gnus-goto-char
5036                             (text-property-any
5037                              (point-min) (point-max)
5038                              'gnus-group (gnus-intern-safe
5039                                           (caar entry) gnus-active-hashtb)))))
5040                 (setq entry (cdr entry)))
5041               (or entry (goto-char (point-max)))))
5042           ;; Finally insert the line.
5043           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5044             (gnus-group-insert-group-line-info group)))
5045         (gnus-group-set-mode-line)))))
5046
5047 (defun gnus-group-set-mode-line ()
5048   "Update the mode line in the group buffer."
5049   (when (memq 'group gnus-updated-mode-lines)
5050     ;; Yes, we want to keep this mode line updated.
5051     (save-excursion
5052       (set-buffer gnus-group-buffer)
5053       (let* ((gformat (or gnus-group-mode-line-format-spec
5054                           (setq gnus-group-mode-line-format-spec
5055                                 (gnus-parse-format
5056                                  gnus-group-mode-line-format
5057                                  gnus-group-mode-line-format-alist))))
5058              (gnus-tmp-news-server (cadr gnus-select-method))
5059              (gnus-tmp-news-method (car gnus-select-method))
5060              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5061              (max-len 60)
5062              gnus-tmp-header            ;Dummy binding for user-defined formats
5063              ;; Get the resulting string.
5064              (mode-string (eval gformat)))
5065         ;; Say whether the dribble buffer has been modified.
5066         (setq mode-line-modified
5067               (if (and gnus-dribble-buffer
5068                        (buffer-name gnus-dribble-buffer)
5069                        (buffer-modified-p gnus-dribble-buffer)
5070                        (save-excursion
5071                          (set-buffer gnus-dribble-buffer)
5072                          (not (zerop (buffer-size)))))
5073                   "---*- " "----- "))
5074         ;; If the line is too long, we chop it off.
5075         (when (> (length mode-string) max-len)
5076           (setq mode-string (substring mode-string 0 (- max-len 4))))
5077         (prog1
5078             (setq mode-line-buffer-identification 
5079                   (list mode-string))
5080           (set-buffer-modified-p t))))))
5081
5082 (defun gnus-group-group-name ()
5083   "Get the name of the newsgroup on the current line."
5084   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5085     (and group (symbol-name group))))
5086
5087 (defun gnus-group-group-level ()
5088   "Get the level of the newsgroup on the current line."
5089   (get-text-property (gnus-point-at-bol) 'gnus-level))
5090
5091 (defun gnus-group-group-indentation ()
5092   "Get the indentation of the newsgroup on the current line."
5093   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5094       (and gnus-group-indentation-function
5095            (funcall gnus-group-indentation-function))
5096       ""))
5097
5098 (defun gnus-group-group-unread ()
5099   "Get the number of unread articles of the newsgroup on the current line."
5100   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5101
5102 (defun gnus-group-search-forward (&optional backward all level first-too)
5103   "Find the next newsgroup with unread articles.
5104 If BACKWARD is non-nil, find the previous newsgroup instead.
5105 If ALL is non-nil, just find any newsgroup.
5106 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5107 group exists.
5108 If FIRST-TOO, the current line is also eligible as a target."
5109   (let ((way (if backward -1 1))
5110         (low gnus-level-killed)
5111         (beg (point))
5112         pos found lev)
5113     (if (and backward (progn (beginning-of-line)) (bobp))
5114         nil
5115       (or first-too (forward-line way))
5116       (while (and
5117               (not (eobp))
5118               (not (setq
5119                     found
5120                     (and (or all
5121                              (and
5122                               (let ((unread
5123                                      (get-text-property (point) 'gnus-unread)))
5124                                 (and (numberp unread) (> unread 0)))
5125                               (setq lev (get-text-property (point)
5126                                                            'gnus-level))
5127                               (<= lev gnus-level-subscribed)))
5128                          (or (not level)
5129                              (and (setq lev (get-text-property (point)
5130                                                                'gnus-level))
5131                                   (or (= lev level)
5132                                       (and (< lev low)
5133                                            (< level lev)
5134                                            (progn
5135                                              (setq low lev)
5136                                              (setq pos (point))
5137                                              nil))))))))
5138               (zerop (forward-line way)))))
5139     (if found
5140         (progn (gnus-group-position-point) t)
5141       (goto-char (or pos beg))
5142       (and pos t))))
5143
5144 ;;; Gnus group mode commands
5145
5146 ;; Group marking.
5147
5148 (defun gnus-group-mark-group (n &optional unmark no-advance)
5149   "Mark the current group."
5150   (interactive "p")
5151   (let ((buffer-read-only nil)
5152         group)
5153     (while (and (> n 0)
5154                 (not (eobp)))
5155       (when (setq group (gnus-group-group-name))
5156         ;; Update the mark.
5157         (beginning-of-line)
5158         (forward-char
5159          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5160         (delete-char 1)
5161         (if unmark
5162             (progn
5163               (insert " ")
5164               (setq gnus-group-marked (delete group gnus-group-marked)))
5165           (insert "#")
5166           (setq gnus-group-marked
5167                 (cons group (delete group gnus-group-marked)))))
5168       (or no-advance (gnus-group-next-group 1))
5169       (decf n))
5170     (gnus-summary-position-point)
5171     n))
5172
5173 (defun gnus-group-unmark-group (n)
5174   "Remove the mark from the current group."
5175   (interactive "p")
5176   (gnus-group-mark-group n 'unmark)
5177   (gnus-group-position-point))
5178
5179 (defun gnus-group-unmark-all-groups ()
5180   "Unmark all groups."
5181   (interactive)
5182   (let ((groups gnus-group-marked))
5183     (save-excursion
5184       (while groups
5185         (gnus-group-remove-mark (pop groups)))))
5186   (gnus-group-position-point))
5187
5188 (defun gnus-group-mark-region (unmark beg end)
5189   "Mark all groups between point and mark.
5190 If UNMARK, remove the mark instead."
5191   (interactive "P\nr")
5192   (let ((num (count-lines beg end)))
5193     (save-excursion
5194       (goto-char beg)
5195       (- num (gnus-group-mark-group num unmark)))))
5196
5197 (defun gnus-group-mark-buffer (&optional unmark)
5198   "Mark all groups in the buffer.
5199 If UNMARK, remove the mark instead."
5200   (interactive "P")
5201   (gnus-group-mark-region unmark (point-min) (point-max)))
5202
5203 (defun gnus-group-mark-regexp (regexp)
5204   "Mark all groups that match some regexp."
5205   (interactive "sMark (regexp): ")
5206   (let ((alist (cdr gnus-newsrc-alist))
5207         group)
5208     (while alist
5209       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5210         (gnus-group-set-mark group))))
5211   (gnus-group-position-point))
5212
5213 (defun gnus-group-remove-mark (group)
5214   "Remove the process mark from GROUP and move point there.
5215 Return nil if the group isn't displayed."
5216   (if (gnus-group-goto-group group)
5217       (save-excursion
5218         (gnus-group-mark-group 1 'unmark t)
5219         t)
5220     (setq gnus-group-marked
5221           (delete group gnus-group-marked))
5222     nil))
5223
5224 (defun gnus-group-set-mark (group)
5225   "Set the process mark on GROUP."
5226   (if (gnus-group-goto-group group) 
5227       (save-excursion
5228         (gnus-group-mark-group 1 nil t))
5229     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5230
5231 (defun gnus-group-universal-argument (arg &optional groups func)
5232   "Perform any command on all groups accoring to the process/prefix convention."
5233   (interactive "P")
5234   (let ((groups (or groups (gnus-group-process-prefix arg)))
5235         group func)
5236     (if (eq (setq func (or func
5237                            (key-binding
5238                             (read-key-sequence
5239                              (substitute-command-keys
5240                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5241             'undefined)
5242         (gnus-error 1 "Undefined key")
5243       (while groups
5244         (gnus-group-remove-mark (setq group (pop groups)))
5245         (command-execute func))))
5246   (gnus-group-position-point))
5247
5248 (defun gnus-group-process-prefix (n)
5249   "Return a list of groups to work on.
5250 Take into consideration N (the prefix) and the list of marked groups."
5251   (cond
5252    (n
5253     (setq n (prefix-numeric-value n))
5254     ;; There is a prefix, so we return a list of the N next
5255     ;; groups.
5256     (let ((way (if (< n 0) -1 1))
5257           (n (abs n))
5258           group groups)
5259       (save-excursion
5260         (while (and (> n 0)
5261                     (setq group (gnus-group-group-name)))
5262           (setq groups (cons group groups))
5263           (setq n (1- n))
5264           (gnus-group-next-group way)))
5265       (nreverse groups)))
5266    ((and (boundp 'transient-mark-mode)
5267          transient-mark-mode
5268          (boundp 'mark-active)
5269          mark-active)
5270     ;; Work on the region between point and mark.
5271     (let ((max (max (point) (mark)))
5272           groups)
5273       (save-excursion
5274         (goto-char (min (point) (mark)))
5275         (while
5276             (and
5277              (push (gnus-group-group-name) groups)
5278              (zerop (gnus-group-next-group 1))
5279              (< (point) max)))
5280         (nreverse groups))))
5281    (gnus-group-marked
5282     ;; No prefix, but a list of marked articles.
5283     (reverse gnus-group-marked))
5284    (t
5285     ;; Neither marked articles or a prefix, so we return the
5286     ;; current group.
5287     (let ((group (gnus-group-group-name)))
5288       (and group (list group))))))
5289
5290 ;; Selecting groups.
5291
5292 (defun gnus-group-read-group (&optional all no-article group)
5293   "Read news in this newsgroup.
5294 If the prefix argument ALL is non-nil, already read articles become
5295 readable.  IF ALL is a number, fetch this number of articles.  If the
5296 optional argument NO-ARTICLE is non-nil, no article will be
5297 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5298 group."
5299   (interactive "P")
5300   (let ((group (or group (gnus-group-group-name)))
5301         number active marked entry)
5302     (or group (error "No group on current line"))
5303     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5304                                             group gnus-newsrc-hashtb)))))
5305     ;; This group might be a dead group.  In that case we have to get
5306     ;; the number of unread articles from `gnus-active-hashtb'.
5307     (setq number
5308           (cond ((numberp all) all)
5309                 (entry (car entry))
5310                 ((setq active (gnus-active group))
5311                  (- (1+ (cdr active)) (car active)))))
5312     (gnus-summary-read-group
5313      group (or all (and (numberp number)
5314                         (zerop (+ number (length (cdr (assq 'tick marked)))
5315                                   (length (cdr (assq 'dormant marked)))))))
5316      no-article)))
5317
5318 (defun gnus-group-select-group (&optional all)
5319   "Select this newsgroup.
5320 No article is selected automatically.
5321 If ALL is non-nil, already read articles become readable.
5322 If ALL is a number, fetch this number of articles."
5323   (interactive "P")
5324   (gnus-group-read-group all t))
5325
5326 (defun gnus-group-quick-select-group (&optional all)
5327   "Select the current group \"quickly\".
5328 This means that no highlighting or scoring will be performed."
5329   (interactive "P")
5330   (let (gnus-visual
5331         gnus-score-find-score-files-function
5332         gnus-apply-kill-hook
5333         gnus-summary-expunge-below)
5334     (gnus-group-read-group all t)))
5335
5336 (defun gnus-group-visible-select-group (&optional all)
5337   "Select the current group without hiding any articles."
5338   (interactive "P")
5339   (let ((gnus-inhibit-limiting t))
5340     (gnus-group-read-group all t)))
5341
5342 ;;;###autoload
5343 (defun gnus-fetch-group (group)
5344   "Start Gnus if necessary and enter GROUP.
5345 Returns whether the fetching was successful or not."
5346   (interactive "sGroup name: ")
5347   (or (get-buffer gnus-group-buffer)
5348       (gnus))
5349   (gnus-group-read-group nil nil group))
5350
5351 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5352 ;; if selection was successful.
5353 (defun gnus-group-read-ephemeral-group
5354   (group method &optional activate quit-config)
5355   (let ((group (if (gnus-group-foreign-p group) group
5356                  (gnus-group-prefixed-name group method))))
5357     (gnus-sethash
5358      group
5359      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5360                      ((quit-config . ,(if quit-config quit-config
5361                                         (cons (current-buffer) 'summary))))))
5362      gnus-newsrc-hashtb)
5363     (set-buffer gnus-group-buffer)
5364     (or (gnus-check-server method)
5365         (error "Unable to contact server: %s" (gnus-status-message method)))
5366     (if activate (or (gnus-request-group group)
5367                      (error "Couldn't request group")))
5368     (condition-case ()
5369         (gnus-group-read-group t t group)
5370       (error nil)
5371       (quit nil))))
5372
5373 (defun gnus-group-jump-to-group (group)
5374   "Jump to newsgroup GROUP."
5375   (interactive
5376    (list (completing-read
5377           "Group: " gnus-active-hashtb nil
5378           (gnus-read-active-file-p)
5379           nil
5380           'gnus-group-history)))
5381
5382   (when (equal group "")
5383     (error "Empty group name"))
5384
5385   (when (string-match "[\000-\032]" group)
5386     (error "Control characters in group: %s" group))
5387
5388   (let ((b (text-property-any
5389             (point-min) (point-max)
5390             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5391     (unless (gnus-ephemeral-group-p group)
5392       (if b
5393           ;; Either go to the line in the group buffer...
5394           (goto-char b)
5395         ;; ... or insert the line.
5396         (or
5397          (gnus-active group)
5398          (gnus-activate-group group)
5399          (error "%s error: %s" group (gnus-status-message group)))
5400
5401         (gnus-group-update-group group)
5402         (goto-char (text-property-any
5403                     (point-min) (point-max)
5404                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5405     ;; Adjust cursor point.
5406     (gnus-group-position-point)))
5407
5408 (defun gnus-group-goto-group (group)
5409   "Goto to newsgroup GROUP."
5410   (when group
5411     (let ((b (text-property-any (point-min) (point-max)
5412                                 'gnus-group (gnus-intern-safe
5413                                              group gnus-active-hashtb))))
5414       (and b (goto-char b)))))
5415
5416 (defun gnus-group-next-group (n)
5417   "Go to next N'th newsgroup.
5418 If N is negative, search backward instead.
5419 Returns the difference between N and the number of skips actually
5420 done."
5421   (interactive "p")
5422   (gnus-group-next-unread-group n t))
5423
5424 (defun gnus-group-next-unread-group (n &optional all level)
5425   "Go to next N'th unread newsgroup.
5426 If N is negative, search backward instead.
5427 If ALL is non-nil, choose any newsgroup, unread or not.
5428 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5429 such group can be found, the next group with a level higher than
5430 LEVEL.
5431 Returns the difference between N and the number of skips actually
5432 made."
5433   (interactive "p")
5434   (let ((backward (< n 0))
5435         (n (abs n)))
5436     (while (and (> n 0)
5437                 (gnus-group-search-forward
5438                  backward (or (not gnus-group-goto-unread) all) level))
5439       (setq n (1- n)))
5440     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5441                                (if level " on this level or higher" "")))
5442     n))
5443
5444 (defun gnus-group-prev-group (n)
5445   "Go to previous N'th newsgroup.
5446 Returns the difference between N and the number of skips actually
5447 done."
5448   (interactive "p")
5449   (gnus-group-next-unread-group (- n) t))
5450
5451 (defun gnus-group-prev-unread-group (n)
5452   "Go to previous N'th unread newsgroup.
5453 Returns the difference between N and the number of skips actually
5454 done."
5455   (interactive "p")
5456   (gnus-group-next-unread-group (- n)))
5457
5458 (defun gnus-group-next-unread-group-same-level (n)
5459   "Go to next N'th unread newsgroup on the same level.
5460 If N is negative, search backward instead.
5461 Returns the difference between N and the number of skips actually
5462 done."
5463   (interactive "p")
5464   (gnus-group-next-unread-group n t (gnus-group-group-level))
5465   (gnus-group-position-point))
5466
5467 (defun gnus-group-prev-unread-group-same-level (n)
5468   "Go to next N'th unread newsgroup on the same level.
5469 Returns the difference between N and the number of skips actually
5470 done."
5471   (interactive "p")
5472   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5473   (gnus-group-position-point))
5474
5475 (defun gnus-group-best-unread-group (&optional exclude-group)
5476   "Go to the group with the highest level.
5477 If EXCLUDE-GROUP, do not go to that group."
5478   (interactive)
5479   (goto-char (point-min))
5480   (let ((best 100000)
5481         unread best-point)
5482     (while (not (eobp))
5483       (setq unread (get-text-property (point) 'gnus-unread))
5484       (if (and (numberp unread) (> unread 0))
5485           (progn
5486             (if (and (get-text-property (point) 'gnus-level)
5487                      (< (get-text-property (point) 'gnus-level) best)
5488                      (or (not exclude-group)
5489                          (not (equal exclude-group (gnus-group-group-name)))))
5490                 (progn
5491                   (setq best (get-text-property (point) 'gnus-level))
5492                   (setq best-point (point))))))
5493       (forward-line 1))
5494     (if best-point (goto-char best-point))
5495     (gnus-summary-position-point)
5496     (and best-point (gnus-group-group-name))))
5497
5498 (defun gnus-group-first-unread-group ()
5499   "Go to the first group with unread articles."
5500   (interactive)
5501   (prog1
5502       (let ((opoint (point))
5503             unread)
5504         (goto-char (point-min))
5505         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5506                 (and (numberp unread)   ; Not a topic.
5507                      (not (zerop unread))) ; Has unread articles.
5508                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5509             (point)                     ; Success.
5510           (goto-char opoint)
5511           nil))                         ; Not success.
5512     (gnus-group-position-point)))
5513
5514 (defun gnus-group-enter-server-mode ()
5515   "Jump to the server buffer."
5516   (interactive)
5517   (gnus-enter-server-buffer))
5518
5519 (defun gnus-group-make-group (name &optional method address)
5520   "Add a new newsgroup.
5521 The user will be prompted for a NAME, for a select METHOD, and an
5522 ADDRESS."
5523   (interactive
5524    (cons
5525     (read-string "Group name: ")
5526     (let ((method
5527            (completing-read
5528             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5529             nil t nil 'gnus-method-history)))
5530       (cond ((assoc method gnus-valid-select-methods)
5531              (list method
5532                    (if (memq 'prompt-address
5533                              (assoc method gnus-valid-select-methods))
5534                        (read-string "Address: ")
5535                      "")))
5536             ((assoc method gnus-server-alist)
5537              (list method))
5538             (t
5539              (list method ""))))))
5540
5541   (let* ((meth (and method (if address (list (intern method) address)
5542                              method)))
5543          (nname (if method (gnus-group-prefixed-name name meth) name))
5544          backend info)
5545     (when (gnus-gethash nname gnus-newsrc-hashtb)
5546       (error "Group %s already exists" nname))
5547     ;; Subscribe to the new group.
5548     (gnus-group-change-level
5549      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5550      gnus-level-default-subscribed gnus-level-killed
5551      (and (gnus-group-group-name)
5552           (gnus-gethash (gnus-group-group-name)
5553                         gnus-newsrc-hashtb))
5554      t)
5555     ;; Make it active.
5556     (gnus-set-active nname (cons 1 0))
5557     (or (gnus-ephemeral-group-p name)
5558         (gnus-dribble-enter
5559          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5560     ;; Insert the line.
5561     (gnus-group-insert-group-line-info nname)
5562     (forward-line -1)
5563     (gnus-group-position-point)
5564
5565     ;; Load the backend and try to make the backend create
5566     ;; the group as well.
5567     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5568                                                   nil meth))))
5569                  gnus-valid-select-methods)
5570       (require backend))
5571     (gnus-check-server meth)
5572     (and (gnus-check-backend-function 'request-create-group nname)
5573          (gnus-request-create-group nname))
5574     t))
5575
5576 (defun gnus-group-delete-group (group &optional force)
5577   "Delete the current group.
5578 If FORCE (the prefix) is non-nil, all the articles in the group will
5579 be deleted.  This is \"deleted\" as in \"removed forever from the face
5580 of the Earth\".  There is no undo."
5581   (interactive
5582    (list (gnus-group-group-name)
5583          current-prefix-arg))
5584   (or group (error "No group to rename"))
5585   (or (gnus-check-backend-function 'request-delete-group group)
5586       (error "This backend does not support group deletion"))
5587   (prog1
5588       (if (not (gnus-yes-or-no-p
5589                 (format
5590                  "Do you really want to delete %s%s? "
5591                  group (if force " and all its contents" ""))))
5592           () ; Whew!
5593         (gnus-message 6 "Deleting group %s..." group)
5594         (if (not (gnus-request-delete-group group force))
5595             (gnus-error 3 "Couldn't delete group %s" group)
5596           (gnus-message 6 "Deleting group %s...done" group)
5597           (gnus-group-goto-group group)
5598           (gnus-group-kill-group 1 t)
5599           (gnus-sethash group nil gnus-active-hashtb)
5600           t))
5601     (gnus-group-position-point)))
5602
5603 (defun gnus-group-rename-group (group new-name)
5604   (interactive
5605    (list
5606     (gnus-group-group-name)
5607     (progn
5608       (or (gnus-check-backend-function
5609            'request-rename-group (gnus-group-group-name))
5610           (error "This backend does not support renaming groups"))
5611       (read-string "New group name: "))))
5612
5613   (or (gnus-check-backend-function 'request-rename-group group)
5614       (error "This backend does not support renaming groups"))
5615
5616   (or group (error "No group to rename"))
5617   (and (string-match "^[ \t]*$" new-name)
5618        (error "Not a valid group name"))
5619
5620   ;; We find the proper prefixed name.
5621   (setq new-name
5622         (gnus-group-prefixed-name
5623          (gnus-group-real-name new-name)
5624          (gnus-info-method (gnus-get-info group))))
5625
5626   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5627   (prog1
5628       (if (not (gnus-request-rename-group group new-name))
5629           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5630         ;; We rename the group internally by killing it...
5631         (gnus-group-goto-group group)
5632         (gnus-group-kill-group)
5633         ;; ... changing its name ...
5634         (setcar (cdar gnus-list-of-killed-groups) new-name)
5635         ;; ... and then yanking it.  Magic!
5636         (gnus-group-yank-group)
5637         (gnus-set-active new-name (gnus-active group))
5638         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5639         new-name)
5640     (gnus-group-position-point)))
5641
5642 (defun gnus-group-edit-group (group &optional part)
5643   "Edit the group on the current line."
5644   (interactive (list (gnus-group-group-name)))
5645   (let* ((part (or part 'info))
5646          (done-func `(lambda ()
5647                        "Exit editing mode and update the information."
5648                        (interactive)
5649                        (gnus-group-edit-group-done ',part ,group)))
5650          (winconf (current-window-configuration))
5651          info)
5652     (or group (error "No group on current line"))
5653     (or (setq info (gnus-get-info group))
5654         (error "Killed group; can't be edited"))
5655     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5656     (gnus-configure-windows 'edit-group)
5657     (gnus-add-current-to-buffer-list)
5658     (emacs-lisp-mode)
5659     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5660     (use-local-map (copy-keymap emacs-lisp-mode-map))
5661     (local-set-key "\C-c\C-c" done-func)
5662     (make-local-variable 'gnus-prev-winconf)
5663     (setq gnus-prev-winconf winconf)
5664     (erase-buffer)
5665     (insert
5666      (cond
5667       ((eq part 'method)
5668        ";; Type `C-c C-c' after editing the select method.\n\n")
5669       ((eq part 'params)
5670        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5671       ((eq part 'info)
5672        ";; Type `C-c C-c' after editing the group info.\n\n")))
5673     (insert
5674      (pp-to-string
5675       (cond ((eq part 'method)
5676              (or (gnus-info-method info) "native"))
5677             ((eq part 'params)
5678              (gnus-info-params info))
5679             (t info)))
5680      "\n")))
5681
5682 (defun gnus-group-edit-group-method (group)
5683   "Edit the select method of GROUP."
5684   (interactive (list (gnus-group-group-name)))
5685   (gnus-group-edit-group group 'method))
5686
5687 (defun gnus-group-edit-group-parameters (group)
5688   "Edit the group parameters of GROUP."
5689   (interactive (list (gnus-group-group-name)))
5690   (gnus-group-edit-group group 'params))
5691
5692 (defun gnus-group-edit-group-done (part group)
5693   "Get info from buffer, update variables and jump to the group buffer."
5694   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5695   (goto-char (point-min))
5696   (let* ((form (read (current-buffer)))
5697          (winconf gnus-prev-winconf)
5698          (method (cond ((eq part 'info) (nth 4 form))
5699                        ((eq part 'method) form)
5700                        (t nil)))
5701          (info (cond ((eq part 'info) form)
5702                      ((eq part 'method) (gnus-get-info group))
5703                      (t nil)))
5704          (new-group (if info
5705                       (if (or (not method)
5706                               (gnus-server-equal
5707                                gnus-select-method method))
5708                           (gnus-group-real-name (car info))
5709                         (gnus-group-prefixed-name
5710                          (gnus-group-real-name (car info)) method))
5711                       nil)))
5712     (when (and new-group
5713                (not (equal new-group group)))
5714       (when (gnus-group-goto-group group)
5715         (gnus-group-kill-group 1))
5716       (gnus-activate-group new-group))
5717     ;; Set the info.
5718     (if (and info new-group)
5719         (progn
5720           (setq info (gnus-copy-sequence info))
5721           (setcar info new-group)
5722           (unless (gnus-server-equal method "native")
5723             (unless (nthcdr 3 info)
5724               (nconc info (list nil nil)))
5725             (unless (nthcdr 4 info)
5726               (nconc info (list nil)))
5727             (gnus-info-set-method info method))
5728           (gnus-group-set-info info))
5729       (gnus-group-set-info form (or new-group group) part))
5730     (kill-buffer (current-buffer))
5731     (and winconf (set-window-configuration winconf))
5732     (set-buffer gnus-group-buffer)
5733     (gnus-group-update-group (or new-group group))
5734     (gnus-group-position-point)))
5735
5736 (defun gnus-group-make-help-group ()
5737   "Create the Gnus documentation group."
5738   (interactive)
5739   (let ((path load-path)
5740         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5741         file dir)
5742     (and (gnus-gethash name gnus-newsrc-hashtb)
5743          (error "Documentation group already exists"))
5744     (while path
5745       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5746             file nil)
5747       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5748                 (file-exists-p
5749                  (setq file (concat (file-name-directory
5750                                      (directory-file-name dir))
5751                                     "etc/gnus-tut.txt"))))
5752         (setq path nil)))
5753     (if (not file)
5754         (gnus-message 1 "Couldn't find doc group")
5755       (gnus-group-make-group
5756        (gnus-group-real-name name)
5757        (list 'nndoc "gnus-help"
5758              (list 'nndoc-address file)
5759              (list 'nndoc-article-type 'mbox)))))
5760   (gnus-group-position-point))
5761
5762 (defun gnus-group-make-doc-group (file type)
5763   "Create a group that uses a single file as the source."
5764   (interactive
5765    (list (read-file-name "File name: ")
5766          (and current-prefix-arg 'ask)))
5767   (when (eq type 'ask)
5768     (let ((err "")
5769           char found)
5770       (while (not found)
5771         (message
5772          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5773          err)
5774         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5775                           ((= char ?b) 'babyl)
5776                           ((= char ?d) 'digest)
5777                           ((= char ?f) 'forward)
5778                           ((= char ?a) 'mmfd)
5779                           (t (setq err (format "%c unknown. " char))
5780                              nil))))
5781       (setq type found)))
5782   (let* ((file (expand-file-name file))
5783          (name (gnus-generate-new-group-name
5784                 (gnus-group-prefixed-name
5785                  (file-name-nondirectory file) '(nndoc "")))))
5786     (gnus-group-make-group
5787      (gnus-group-real-name name)
5788      (list 'nndoc (file-name-nondirectory file)
5789            (list 'nndoc-address file)
5790            (list 'nndoc-article-type (or type 'guess))))))
5791
5792 (defun gnus-group-make-archive-group (&optional all)
5793   "Create the (ding) Gnus archive group of the most recent articles.
5794 Given a prefix, create a full group."
5795   (interactive "P")
5796   (let ((group (gnus-group-prefixed-name
5797                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5798     (and (gnus-gethash group gnus-newsrc-hashtb)
5799          (error "Archive group already exists"))
5800     (gnus-group-make-group
5801      (gnus-group-real-name group)
5802      (list 'nndir (if all "hpc" "edu")
5803            (list 'nndir-directory
5804                  (if all gnus-group-archive-directory
5805                    gnus-group-recent-archive-directory))))))
5806
5807 (defun gnus-group-make-directory-group (dir)
5808   "Create an nndir group.
5809 The user will be prompted for a directory.  The contents of this
5810 directory will be used as a newsgroup.  The directory should contain
5811 mail messages or news articles in files that have numeric names."
5812   (interactive
5813    (list (read-file-name "Create group from directory: ")))
5814   (or (file-exists-p dir) (error "No such directory"))
5815   (or (file-directory-p dir) (error "Not a directory"))
5816   (let ((ext "")
5817         (i 0)
5818         group)
5819     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5820       (setq group
5821             (gnus-group-prefixed-name
5822              (concat (file-name-as-directory (directory-file-name dir))
5823                      ext)
5824              '(nndir "")))
5825       (setq ext (format "<%d>" (setq i (1+ i)))))
5826     (gnus-group-make-group
5827      (gnus-group-real-name group)
5828      (list 'nndir group (list 'nndir-directory dir)))))
5829
5830 (defun gnus-group-make-kiboze-group (group address scores)
5831   "Create an nnkiboze group.
5832 The user will be prompted for a name, a regexp to match groups, and
5833 score file entries for articles to include in the group."
5834   (interactive
5835    (list
5836     (read-string "nnkiboze group name: ")
5837     (read-string "Source groups (regexp): ")
5838     (let ((headers (mapcar (lambda (group) (list group))
5839                            '("subject" "from" "number" "date" "message-id"
5840                              "references" "chars" "lines" "xref"
5841                              "followup" "all" "body" "head")))
5842           scores header regexp regexps)
5843       (while (not (equal "" (setq header (completing-read
5844                                           "Match on header: " headers nil t))))
5845         (setq regexps nil)
5846         (while (not (equal "" (setq regexp (read-string
5847                                             (format "Match on %s (string): "
5848                                                     header)))))
5849           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5850         (setq scores (cons (cons header regexps) scores)))
5851       scores)))
5852   (gnus-group-make-group group "nnkiboze" address)
5853   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5854     (let (emacs-lisp-mode-hook)
5855       (pp scores (current-buffer)))))
5856
5857 (defun gnus-group-add-to-virtual (n vgroup)
5858   "Add the current group to a virtual group."
5859   (interactive
5860    (list current-prefix-arg
5861          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5862                           "nnvirtual:")))
5863   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5864       (error "%s is not an nnvirtual group" vgroup))
5865   (let* ((groups (gnus-group-process-prefix n))
5866          (method (gnus-info-method (gnus-get-info vgroup))))
5867     (setcar (cdr method)
5868             (concat
5869              (nth 1 method) "\\|"
5870              (mapconcat
5871               (lambda (s)
5872                 (gnus-group-remove-mark s)
5873                 (concat "\\(^" (regexp-quote s) "$\\)"))
5874               groups "\\|"))))
5875   (gnus-group-position-point))
5876
5877 (defun gnus-group-make-empty-virtual (group)
5878   "Create a new, fresh, empty virtual group."
5879   (interactive "sCreate new, empty virtual group: ")
5880   (let* ((method (list 'nnvirtual "^$"))
5881          (pgroup (gnus-group-prefixed-name group method)))
5882     ;; Check whether it exists already.
5883     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5884          (error "Group %s already exists." pgroup))
5885     ;; Subscribe the new group after the group on the current line.
5886     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5887     (gnus-group-update-group pgroup)
5888     (forward-line -1)
5889     (gnus-group-position-point)))
5890
5891 (defun gnus-group-enter-directory (dir)
5892   "Enter an ephemeral nneething group."
5893   (interactive "DDirectory to read: ")
5894   (let* ((method (list 'nneething dir))
5895          (leaf (gnus-group-prefixed-name
5896                 (file-name-nondirectory (directory-file-name dir))
5897                 method))
5898          (name (gnus-generate-new-group-name leaf)))
5899     (let ((nneething-read-only t))
5900       (or (gnus-group-read-ephemeral-group
5901            name method t
5902            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5903                                       'summary 'group)))
5904           (error "Couldn't enter %s" dir)))))
5905
5906 ;; Group sorting commands
5907 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5908
5909 (defun gnus-group-sort-groups (func &optional reverse)
5910   "Sort the group buffer according to FUNC.
5911 If REVERSE, reverse the sorting order."
5912   (interactive (list gnus-group-sort-function
5913                      current-prefix-arg))
5914   (let ((func (cond 
5915                ((not (listp func)) func)
5916                ((null func) func)
5917                ((= 1 (length func)) (car func))
5918                (t `(lambda (t1 t2)
5919                      ,(gnus-make-sort-function 
5920                        (reverse func)))))))
5921     ;; We peel off the dummy group from the alist.
5922     (when func
5923       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5924         (pop gnus-newsrc-alist))
5925       ;; Do the sorting.
5926       (setq gnus-newsrc-alist
5927             (sort gnus-newsrc-alist func))
5928       (when reverse
5929         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5930       ;; Regenerate the hash table.
5931       (gnus-make-hashtable-from-newsrc-alist)
5932       (gnus-group-list-groups))))
5933
5934 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5935   "Sort the group buffer alphabetically by group name.
5936 If REVERSE, sort in reverse order."
5937   (interactive "P")
5938   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5939
5940 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5941   "Sort the group buffer by number of unread articles.
5942 If REVERSE, sort in reverse order."
5943   (interactive "P")
5944   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5945
5946 (defun gnus-group-sort-groups-by-level (&optional reverse)
5947   "Sort the group buffer by group level.
5948 If REVERSE, sort in reverse order."
5949   (interactive "P")
5950   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5951
5952 (defun gnus-group-sort-groups-by-score (&optional reverse)
5953   "Sort the group buffer by group score.
5954 If REVERSE, sort in reverse order."
5955   (interactive "P")
5956   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5957
5958 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5959   "Sort the group buffer by group rank.
5960 If REVERSE, sort in reverse order."
5961   (interactive "P")
5962   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5963
5964 (defun gnus-group-sort-groups-by-method (&optional reverse)
5965   "Sort the group buffer alphabetically by backend name.
5966 If REVERSE, sort in reverse order."
5967   (interactive "P")
5968   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5969
5970 (defun gnus-group-sort-by-alphabet (info1 info2)
5971   "Sort alphabetically."
5972   (string< (gnus-info-group info1) (gnus-info-group info2)))
5973
5974 (defun gnus-group-sort-by-unread (info1 info2)
5975   "Sort by number of unread articles."
5976   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5977         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5978     (< (or (and (numberp n1) n1) 0)
5979        (or (and (numberp n2) n2) 0))))
5980
5981 (defun gnus-group-sort-by-level (info1 info2)
5982   "Sort by level."
5983   (< (gnus-info-level info1) (gnus-info-level info2)))
5984
5985 (defun gnus-group-sort-by-method (info1 info2)
5986   "Sort alphabetically by backend name."
5987   (string< (symbol-name (car (gnus-find-method-for-group
5988                               (gnus-info-group info1) info1)))
5989            (symbol-name (car (gnus-find-method-for-group
5990                               (gnus-info-group info2) info2)))))
5991
5992 (defun gnus-group-sort-by-score (info1 info2)
5993   "Sort by group score."
5994   (< (gnus-info-score info1) (gnus-info-score info2)))
5995
5996 (defun gnus-group-sort-by-rank (info1 info2)
5997   "Sort by level and score."
5998   (let ((level1 (gnus-info-level info1))
5999         (level2 (gnus-info-level info2)))
6000     (or (< level1 level2)
6001         (and (= level1 level2)
6002              (> (gnus-info-score info1) (gnus-info-score info2))))))
6003
6004 ;; Group catching up.
6005
6006 (defun gnus-group-clear-data (n)
6007   "Clear all marks and read ranges from the current group."
6008   (interactive "P")
6009   (let ((groups (gnus-group-process-prefix n))
6010         group info)
6011     (while (setq group (pop groups))
6012       (setq info (gnus-get-info group))
6013       (gnus-info-set-read info nil)
6014       (when (gnus-info-marks info)
6015         (gnus-info-set-marks info nil))
6016       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6017       (when (gnus-group-goto-group group)
6018         (gnus-group-remove-mark group)
6019         (gnus-group-update-group-line)))))
6020
6021 (defun gnus-group-catchup-current (&optional n all)
6022   "Mark all articles not marked as unread in current newsgroup as read.
6023 If prefix argument N is numeric, the ARG next newsgroups will be
6024 caught up.  If ALL is non-nil, marked articles will also be marked as
6025 read.  Cross references (Xref: header) of articles are ignored.
6026 The difference between N and actual number of newsgroups that were
6027 caught up is returned."
6028   (interactive "P")
6029   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6030                gnus-expert-user
6031                (gnus-y-or-n-p
6032                 (if all
6033                     "Do you really want to mark all articles as read? "
6034                   "Mark all unread articles as read? "))))
6035       n
6036     (let ((groups (gnus-group-process-prefix n))
6037           (ret 0))
6038       (while groups
6039         ;; Virtual groups have to be given special treatment.
6040         (let ((method (gnus-find-method-for-group (car groups))))
6041           (if (eq 'nnvirtual (car method))
6042               (nnvirtual-catchup-group
6043                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6044         (gnus-group-remove-mark (car groups))
6045         (if (>= (gnus-group-group-level) gnus-level-zombie)
6046             (gnus-message 2 "Dead groups can't be caught up")
6047           (if (prog1
6048                   (gnus-group-goto-group (car groups))
6049                 (gnus-group-catchup (car groups) all))
6050               (gnus-group-update-group-line)
6051             (setq ret (1+ ret))))
6052         (setq groups (cdr groups)))
6053       (gnus-group-next-unread-group 1)
6054       ret)))
6055
6056 (defun gnus-group-catchup-current-all (&optional n)
6057   "Mark all articles in current newsgroup as read.
6058 Cross references (Xref: header) of articles are ignored."
6059   (interactive "P")
6060   (gnus-group-catchup-current n 'all))
6061
6062 (defun gnus-group-catchup (group &optional all)
6063   "Mark all articles in GROUP as read.
6064 If ALL is non-nil, all articles are marked as read.
6065 The return value is the number of articles that were marked as read,
6066 or nil if no action could be taken."
6067   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6068          (num (car entry)))
6069     ;; Do the updating only if the newsgroup isn't killed.
6070     (if (not (numberp (car entry)))
6071         (gnus-message 1 "Can't catch up; non-active group")
6072       ;; Do auto-expirable marks if that's required.
6073       (when (gnus-group-auto-expirable-p group)
6074         (gnus-add-marked-articles
6075          group 'expire (gnus-list-of-unread-articles group))
6076         (when all
6077           (let ((marks (nth 3 (nth 2 entry))))
6078             (gnus-add-marked-articles
6079              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6080             (gnus-add-marked-articles
6081              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6082       (when entry
6083         (gnus-update-read-articles group nil)
6084         ;; Also nix out the lists of marks and dormants.
6085         (when all
6086           (gnus-add-marked-articles group 'tick nil nil 'force)
6087           (gnus-add-marked-articles group 'dormant nil nil 'force))
6088         (run-hooks 'gnus-group-catchup-group-hook)
6089         num))))
6090
6091 (defun gnus-group-expire-articles (&optional n)
6092   "Expire all expirable articles in the current newsgroup."
6093   (interactive "P")
6094   (let ((groups (gnus-group-process-prefix n))
6095         group)
6096     (unless groups
6097       (error "No groups to expire"))
6098     (while (setq group (pop groups))
6099       (gnus-group-remove-mark group)
6100       (when (gnus-check-backend-function 'request-expire-articles group)
6101         (gnus-message 6 "Expiring articles in %s..." group)
6102         (let* ((info (gnus-get-info group))
6103                (expirable (if (gnus-group-total-expirable-p group)
6104                               (cons nil (gnus-list-of-read-articles group))
6105                             (assq 'expire (gnus-info-marks info))))
6106                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6107           (when expirable
6108             (setcdr
6109              expirable
6110              (gnus-compress-sequence
6111               (if expiry-wait
6112                   ;; We set the expiry variables to the groupp
6113                   ;; parameter. 
6114                   (let ((nnmail-expiry-wait-function nil)
6115                         (nnmail-expiry-wait expiry-wait))
6116                     (gnus-request-expire-articles
6117                      (gnus-uncompress-sequence (cdr expirable)) group))
6118                 ;; Just expire using the normal expiry values.
6119                 (gnus-request-expire-articles
6120                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6121           (gnus-message 6 "Expiring articles in %s...done" group)))
6122       (gnus-group-position-point))))
6123
6124 (defun gnus-group-expire-all-groups ()
6125   "Expire all expirable articles in all newsgroups."
6126   (interactive)
6127   (save-excursion
6128     (gnus-message 5 "Expiring...")
6129     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6130                                      (cdr gnus-newsrc-alist))))
6131       (gnus-group-expire-articles nil)))
6132   (gnus-group-position-point)
6133   (gnus-message 5 "Expiring...done"))
6134
6135 (defun gnus-group-set-current-level (n level)
6136   "Set the level of the next N groups to LEVEL."
6137   (interactive
6138    (list
6139     current-prefix-arg
6140     (string-to-int
6141      (let ((s (read-string
6142                (format "Level (default %s): "
6143                        (or (gnus-group-group-level) 
6144                            gnus-level-default-subscribed)))))
6145        (if (string-match "^\\s-*$" s)
6146            (int-to-string (or (gnus-group-group-level) 
6147                               gnus-level-default-subscribed))
6148          s)))))
6149   (or (and (>= level 1) (<= level gnus-level-killed))
6150       (error "Illegal level: %d" level))
6151   (let ((groups (gnus-group-process-prefix n))
6152         group)
6153     (while (setq group (pop groups))
6154       (gnus-group-remove-mark group)
6155       (gnus-message 6 "Changed level of %s from %d to %d"
6156                     group (or (gnus-group-group-level) gnus-level-killed)
6157                     level)
6158       (gnus-group-change-level
6159        group level (or (gnus-group-group-level) gnus-level-killed))
6160       (gnus-group-update-group-line)))
6161   (gnus-group-position-point))
6162
6163 (defun gnus-group-unsubscribe-current-group (&optional n)
6164   "Toggle subscription of the current group.
6165 If given numerical prefix, toggle the N next groups."
6166   (interactive "P")
6167   (let ((groups (gnus-group-process-prefix n))
6168         group)
6169     (while groups
6170       (setq group (car groups)
6171             groups (cdr groups))
6172       (gnus-group-remove-mark group)
6173       (gnus-group-unsubscribe-group
6174        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6175                  gnus-level-default-unsubscribed
6176                gnus-level-default-subscribed) t)
6177       (gnus-group-update-group-line))
6178     (gnus-group-next-group 1)))
6179
6180 (defun gnus-group-unsubscribe-group (group &optional level silent)
6181   "Toggle subscription to GROUP.
6182 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6183 group line."
6184   (interactive
6185    (list (completing-read
6186           "Group: " gnus-active-hashtb nil
6187           (gnus-read-active-file-p)
6188           nil 
6189           'gnus-group-history)))
6190   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6191     (cond
6192      ((string-match "^[ \t]$" group)
6193       (error "Empty group name"))
6194      (newsrc
6195       ;; Toggle subscription flag.
6196       (gnus-group-change-level
6197        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6198                                       gnus-level-subscribed)
6199                                   (1+ gnus-level-subscribed)
6200                                 gnus-level-default-subscribed)))
6201       (unless silent
6202         (gnus-group-update-group group)))
6203      ((and (stringp group)
6204            (or (not (gnus-read-active-file-p))
6205                (gnus-active group)))
6206       ;; Add new newsgroup.
6207       (gnus-group-change-level
6208        group
6209        (if level level gnus-level-default-subscribed)
6210        (or (and (member group gnus-zombie-list)
6211                 gnus-level-zombie)
6212            gnus-level-killed)
6213        (and (gnus-group-group-name)
6214             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6215       (unless silent
6216         (gnus-group-update-group group)))
6217      (t (error "No such newsgroup: %s" group)))
6218     (gnus-group-position-point)))
6219
6220 (defun gnus-group-transpose-groups (n)
6221   "Move the current newsgroup up N places.
6222 If given a negative prefix, move down instead.  The difference between
6223 N and the number of steps taken is returned."
6224   (interactive "p")
6225   (or (gnus-group-group-name)
6226       (error "No group on current line"))
6227   (gnus-group-kill-group 1)
6228   (prog1
6229       (forward-line (- n))
6230     (gnus-group-yank-group)
6231     (gnus-group-position-point)))
6232
6233 (defun gnus-group-kill-all-zombies ()
6234   "Kill all zombie newsgroups."
6235   (interactive)
6236   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6237   (setq gnus-zombie-list nil)
6238   (gnus-group-list-groups))
6239
6240 (defun gnus-group-kill-region (begin end)
6241   "Kill newsgroups in current region (excluding current point).
6242 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6243   (interactive "r")
6244   (let ((lines
6245          ;; Count lines.
6246          (save-excursion
6247            (count-lines
6248             (progn
6249               (goto-char begin)
6250               (beginning-of-line)
6251               (point))
6252             (progn
6253               (goto-char end)
6254               (beginning-of-line)
6255               (point))))))
6256     (goto-char begin)
6257     (beginning-of-line)                 ;Important when LINES < 1
6258     (gnus-group-kill-group lines)))
6259
6260 (defun gnus-group-kill-group (&optional n discard)
6261   "Kill the next N groups.
6262 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6263 However, only groups that were alive can be yanked; already killed
6264 groups or zombie groups can't be yanked.
6265 The return value is the name of the group that was killed, or a list
6266 of groups killed."
6267   (interactive "P")
6268   (let ((buffer-read-only nil)
6269         (groups (gnus-group-process-prefix n))
6270         group entry level out)
6271     (if (< (length groups) 10)
6272         ;; This is faster when there are few groups.
6273         (while groups
6274           (push (setq group (pop groups)) out)
6275           (gnus-group-remove-mark group)
6276           (setq level (gnus-group-group-level))
6277           (gnus-delete-line)
6278           (when (and (not discard)
6279                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6280             (push (cons (car entry) (nth 2 entry))
6281                   gnus-list-of-killed-groups))
6282           (gnus-group-change-level
6283            (if entry entry group) gnus-level-killed (if entry nil level)))
6284       ;; If there are lots and lots of groups to be killed, we use
6285       ;; this thing instead.
6286       (let (entry)
6287         (setq groups (nreverse groups))
6288         (while groups
6289           (gnus-group-remove-mark (setq group (pop groups)))
6290           (gnus-delete-line)
6291           (cond
6292            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6293             (push (cons (car entry) (nth 2 entry))
6294                   gnus-list-of-killed-groups)
6295             (setcdr (cdr entry) (cdddr entry)))
6296            ((member group gnus-zombie-list)
6297             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6298         (gnus-make-hashtable-from-newsrc-alist)))
6299
6300     (gnus-group-position-point)
6301     (if (< (length out) 2) (car out) (nreverse out))))
6302
6303 (defun gnus-group-yank-group (&optional arg)
6304   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6305 inserting it before the current newsgroup.  The numeric ARG specifies
6306 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6307 is returned, or (if several groups are yanked) a list of yanked groups
6308 is returned."
6309   (interactive "p")
6310   (setq arg (or arg 1))
6311   (let (info group prev out)
6312     (while (>= (decf arg) 0)
6313       (if (not (setq info (pop gnus-list-of-killed-groups)))
6314           (error "No more newsgroups to yank"))
6315       (push (setq group (nth 1 info)) out)
6316       ;; Find which newsgroup to insert this one before - search
6317       ;; backward until something suitable is found.  If there are no
6318       ;; other newsgroups in this buffer, just make this newsgroup the
6319       ;; first newsgroup.
6320       (setq prev (gnus-group-group-name))
6321       (gnus-group-change-level
6322        info (gnus-info-level (cdr info)) gnus-level-killed
6323        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6324        t)
6325       (gnus-group-insert-group-line-info group))
6326     (forward-line -1)
6327     (gnus-group-position-point)
6328     (if (< (length out) 2) (car out) (nreverse out))))
6329
6330 (defun gnus-group-kill-level (level)
6331   "Kill all groups that is on a certain LEVEL."
6332   (interactive "nKill all groups on level: ")
6333   (cond
6334    ((= level gnus-level-zombie)
6335     (setq gnus-killed-list
6336           (nconc gnus-zombie-list gnus-killed-list))
6337     (setq gnus-zombie-list nil))
6338    ((and (< level gnus-level-zombie)
6339          (> level 0)
6340          (or gnus-expert-user
6341              (gnus-yes-or-no-p
6342               (format
6343                "Do you really want to kill all groups on level %d? "
6344                level))))
6345     (let* ((prev gnus-newsrc-alist)
6346            (alist (cdr prev)))
6347       (while alist
6348         (if (= (gnus-info-level level) level)
6349             (setcdr prev (cdr alist))
6350           (setq prev alist))
6351         (setq alist (cdr alist)))
6352       (gnus-make-hashtable-from-newsrc-alist)
6353       (gnus-group-list-groups)))
6354    (t
6355     (error "Can't kill; illegal level: %d" level))))
6356
6357 (defun gnus-group-list-all-groups (&optional arg)
6358   "List all newsgroups with level ARG or lower.
6359 Default is gnus-level-unsubscribed, which lists all subscribed and most
6360 unsubscribed groups."
6361   (interactive "P")
6362   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6363
6364 ;; Redefine this to list ALL killed groups if prefix arg used.
6365 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6366 (defun gnus-group-list-killed (&optional arg)
6367   "List all killed newsgroups in the group buffer.
6368 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6369 entail asking the server for the groups."
6370   (interactive "P")
6371   ;; Find all possible killed newsgroups if arg.
6372   (when arg
6373     (gnus-get-killed-groups))
6374   (if (not gnus-killed-list)
6375       (gnus-message 6 "No killed groups")
6376     (let (gnus-group-list-mode)
6377       (funcall gnus-group-prepare-function
6378                gnus-level-killed t gnus-level-killed))
6379     (goto-char (point-min)))
6380   (gnus-group-position-point))
6381
6382 (defun gnus-group-list-zombies ()
6383   "List all zombie newsgroups in the group buffer."
6384   (interactive)
6385   (if (not gnus-zombie-list)
6386       (gnus-message 6 "No zombie groups")
6387     (let (gnus-group-list-mode)
6388       (funcall gnus-group-prepare-function
6389                gnus-level-zombie t gnus-level-zombie))
6390     (goto-char (point-min)))
6391   (gnus-group-position-point))
6392
6393 (defun gnus-group-list-active ()
6394   "List all groups that are available from the server(s)."
6395   (interactive)
6396   ;; First we make sure that we have really read the active file.
6397   (unless (gnus-read-active-file-p)
6398     (let ((gnus-read-active-file t))
6399       (gnus-read-active-file)))
6400   ;; Find all groups and sort them.
6401   (let ((groups
6402          (sort
6403           (let (list)
6404             (mapatoms
6405              (lambda (sym)
6406                (and (boundp sym)
6407                     (symbol-value sym)
6408                     (setq list (cons (symbol-name sym) list))))
6409              gnus-active-hashtb)
6410             list)
6411           'string<))
6412         (buffer-read-only nil))
6413     (erase-buffer)
6414     (while groups
6415       (gnus-group-insert-group-line-info (pop groups)))
6416     (goto-char (point-min))))
6417
6418 (defun gnus-activate-all-groups (level)
6419   "Activate absolutely all groups."
6420   (interactive (list 7))
6421   (let ((gnus-activate-level level)
6422         (gnus-activate-foreign-newsgroups level))
6423     (gnus-group-get-new-news)))
6424
6425 (defun gnus-group-get-new-news (&optional arg)
6426   "Get newly arrived articles.
6427 If ARG is a number, it specifies which levels you are interested in
6428 re-scanning.  If ARG is non-nil and not a number, this will force
6429 \"hard\" re-reading of the active files from all servers."
6430   (interactive "P")
6431   (run-hooks 'gnus-get-new-news-hook)
6432   ;; We might read in new NoCeM messages here.
6433   (when (and gnus-use-nocem 
6434              (null arg))
6435     (gnus-nocem-scan-groups))
6436   ;; If ARG is not a number, then we read the active file.
6437   (when (and arg (not (numberp arg)))
6438     (let ((gnus-read-active-file t))
6439       (gnus-read-active-file))
6440     (setq arg nil))
6441
6442   (setq arg (gnus-group-default-level arg t))
6443   (if (and gnus-read-active-file (not arg))
6444       (progn
6445         (gnus-read-active-file)
6446         (gnus-get-unread-articles arg))
6447     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6448       (gnus-get-unread-articles arg)))
6449   (run-hooks 'gnus-after-getting-new-news-hook)
6450   (gnus-group-list-groups))
6451
6452 (defun gnus-group-get-new-news-this-group (&optional n)
6453   "Check for newly arrived news in the current group (and the N-1 next groups).
6454 The difference between N and the number of newsgroup checked is returned.
6455 If N is negative, this group and the N-1 previous groups will be checked."
6456   (interactive "P")
6457   (let* ((groups (gnus-group-process-prefix n))
6458          (ret (if (numberp n) (- n (length groups)) 0))
6459          (beg (unless n (point)))
6460          group)
6461     (while (setq group (pop groups))
6462       (gnus-group-remove-mark group)
6463       (if (gnus-activate-group group 'scan)
6464           (progn
6465             (gnus-get-unread-articles-in-group
6466              (gnus-get-info group) (gnus-active group) t)
6467             (unless (gnus-virtual-group-p group)
6468               (gnus-close-group group))
6469             (gnus-group-update-group group))
6470         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6471     (when beg (goto-char beg))
6472     (when gnus-goto-next-group-when-activating
6473       (gnus-group-next-unread-group 1 t))
6474     (gnus-summary-position-point)
6475     ret))
6476
6477 (defun gnus-group-fetch-faq (group &optional faq-dir)
6478   "Fetch the FAQ for the current group."
6479   (interactive
6480    (list
6481     (gnus-group-real-name (gnus-group-group-name))
6482     (cond (current-prefix-arg
6483            (completing-read
6484             "Faq dir: " (and (listp gnus-group-faq-directory)
6485                              gnus-group-faq-directory))))))
6486   (or faq-dir
6487       (setq faq-dir (if (listp gnus-group-faq-directory)
6488                         (car gnus-group-faq-directory)
6489                       gnus-group-faq-directory)))
6490   (or group (error "No group name given"))
6491   (let ((file (concat (file-name-as-directory faq-dir)
6492                       (gnus-group-real-name group))))
6493     (if (not (file-exists-p file))
6494         (error "No such file: %s" file)
6495       (find-file file))))
6496
6497 (defun gnus-group-describe-group (force &optional group)
6498   "Display a description of the current newsgroup."
6499   (interactive (list current-prefix-arg (gnus-group-group-name)))
6500   (and force (setq gnus-description-hashtb nil))
6501   (let ((method (gnus-find-method-for-group group))
6502         desc)
6503     (or group (error "No group name given"))
6504     (and (or (and gnus-description-hashtb
6505                   ;; We check whether this group's method has been
6506                   ;; queried for a description file.
6507                   (gnus-gethash
6508                    (gnus-group-prefixed-name "" method)
6509                    gnus-description-hashtb))
6510              (setq desc (gnus-group-get-description group))
6511              (gnus-read-descriptions-file method))
6512          (gnus-message 1
6513           (or desc (gnus-gethash group gnus-description-hashtb)
6514               "No description available")))))
6515
6516 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6517 (defun gnus-group-describe-all-groups (&optional force)
6518   "Pop up a buffer with descriptions of all newsgroups."
6519   (interactive "P")
6520   (and force (setq gnus-description-hashtb nil))
6521   (if (not (or gnus-description-hashtb
6522                (gnus-read-all-descriptions-files)))
6523       (error "Couldn't request descriptions file"))
6524   (let ((buffer-read-only nil)
6525         b)
6526     (erase-buffer)
6527     (mapatoms
6528      (lambda (group)
6529        (setq b (point))
6530        (insert (format "      *: %-20s %s\n" (symbol-name group)
6531                        (symbol-value group)))
6532        (gnus-add-text-properties
6533         b (1+ b) (list 'gnus-group group
6534                        'gnus-unread t 'gnus-marked nil
6535                        'gnus-level (1+ gnus-level-subscribed))))
6536      gnus-description-hashtb)
6537     (goto-char (point-min))
6538     (gnus-group-position-point)))
6539
6540 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6541 (defun gnus-group-apropos (regexp &optional search-description)
6542   "List all newsgroups that have names that match a regexp."
6543   (interactive "sGnus apropos (regexp): ")
6544   (let ((prev "")
6545         (obuf (current-buffer))
6546         groups des)
6547     ;; Go through all newsgroups that are known to Gnus.
6548     (mapatoms
6549      (lambda (group)
6550        (and (symbol-name group)
6551             (string-match regexp (symbol-name group))
6552             (setq groups (cons (symbol-name group) groups))))
6553      gnus-active-hashtb)
6554     ;; Also go through all descriptions that are known to Gnus.
6555     (when search-description
6556       (mapatoms
6557        (lambda (group)
6558          (and (string-match regexp (symbol-value group))
6559               (gnus-active (symbol-name group))
6560               (setq groups (cons (symbol-name group) groups))))
6561        gnus-description-hashtb))
6562     (if (not groups)
6563         (gnus-message 3 "No groups matched \"%s\"." regexp)
6564       ;; Print out all the groups.
6565       (save-excursion
6566         (pop-to-buffer "*Gnus Help*")
6567         (buffer-disable-undo (current-buffer))
6568         (erase-buffer)
6569         (setq groups (sort groups 'string<))
6570         (while groups
6571           ;; Groups may be entered twice into the list of groups.
6572           (if (not (string= (car groups) prev))
6573               (progn
6574                 (insert (setq prev (car groups)) "\n")
6575                 (if (and gnus-description-hashtb
6576                          (setq des (gnus-gethash (car groups)
6577                                                  gnus-description-hashtb)))
6578                     (insert "  " des "\n"))))
6579           (setq groups (cdr groups)))
6580         (goto-char (point-min))))
6581     (pop-to-buffer obuf)))
6582
6583 (defun gnus-group-description-apropos (regexp)
6584   "List all newsgroups that have names or descriptions that match a regexp."
6585   (interactive "sGnus description apropos (regexp): ")
6586   (if (not (or gnus-description-hashtb
6587                (gnus-read-all-descriptions-files)))
6588       (error "Couldn't request descriptions file"))
6589   (gnus-group-apropos regexp t))
6590
6591 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6592 (defun gnus-group-list-matching (level regexp &optional all lowest)
6593   "List all groups with unread articles that match REGEXP.
6594 If the prefix LEVEL is non-nil, it should be a number that says which
6595 level to cut off listing groups.
6596 If ALL, also list groups with no unread articles.
6597 If LOWEST, don't list groups with level lower than LOWEST.
6598
6599 This command may read the active file."
6600   (interactive "P\nsList newsgroups matching: ")
6601   ;; First make sure active file has been read.
6602   (when (and level
6603              (>= (prefix-numeric-value level) gnus-level-killed))
6604     (gnus-get-killed-groups))
6605   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6606                            all (or lowest 1) regexp)
6607   (goto-char (point-min))
6608   (gnus-group-position-point))
6609
6610 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6611   "List all groups that match REGEXP.
6612 If the prefix LEVEL is non-nil, it should be a number that says which
6613 level to cut off listing groups.
6614 If LOWEST, don't list groups with level lower than LOWEST."
6615   (interactive "P\nsList newsgroups matching: ")
6616   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6617
6618 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6619 (defun gnus-group-save-newsrc (&optional force)
6620   "Save the Gnus startup files.
6621 If FORCE, force saving whether it is necessary or not."
6622   (interactive "P")
6623   (gnus-save-newsrc-file force))
6624
6625 (defun gnus-group-restart (&optional arg)
6626   "Force Gnus to read the .newsrc file."
6627   (interactive "P")
6628   (when (gnus-yes-or-no-p
6629          (format "Are you sure you want to read %s? "
6630                  gnus-current-startup-file))
6631     (gnus-save-newsrc-file)
6632     (gnus-setup-news 'force)
6633     (gnus-group-list-groups arg)))
6634
6635 (defun gnus-group-read-init-file ()
6636   "Read the Gnus elisp init file."
6637   (interactive)
6638   (gnus-read-init-file))
6639
6640 (defun gnus-group-check-bogus-groups (&optional silent)
6641   "Check bogus newsgroups.
6642 If given a prefix, don't ask for confirmation before removing a bogus
6643 group."
6644   (interactive "P")
6645   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6646   (gnus-group-list-groups))
6647
6648 (defun gnus-group-edit-global-kill (&optional article group)
6649   "Edit the global kill file.
6650 If GROUP, edit that local kill file instead."
6651   (interactive "P")
6652   (setq gnus-current-kill-article article)
6653   (gnus-kill-file-edit-file group)
6654   (gnus-message
6655    6
6656    (substitute-command-keys
6657     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6658             (if group "local" "global")))))
6659
6660 (defun gnus-group-edit-local-kill (article group)
6661   "Edit a local kill file."
6662   (interactive (list nil (gnus-group-group-name)))
6663   (gnus-group-edit-global-kill article group))
6664
6665 (defun gnus-group-force-update ()
6666   "Update `.newsrc' file."
6667   (interactive)
6668   (gnus-save-newsrc-file))
6669
6670 (defun gnus-group-suspend ()
6671   "Suspend the current Gnus session.
6672 In fact, cleanup buffers except for group mode buffer.
6673 The hook gnus-suspend-gnus-hook is called before actually suspending."
6674   (interactive)
6675   (run-hooks 'gnus-suspend-gnus-hook)
6676   ;; Kill Gnus buffers except for group mode buffer.
6677   (let ((group-buf (get-buffer gnus-group-buffer)))
6678     ;; Do this on a separate list in case the user does a ^G before we finish
6679     (let ((gnus-buffer-list
6680            (delq group-buf (delq gnus-dribble-buffer
6681                                  (append gnus-buffer-list nil)))))
6682       (while gnus-buffer-list
6683         (gnus-kill-buffer (car gnus-buffer-list))
6684         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6685     (if group-buf
6686         (progn
6687           (setq gnus-buffer-list (list group-buf))
6688           (bury-buffer group-buf)
6689           (delete-windows-on group-buf t)))))
6690
6691 (defun gnus-group-clear-dribble ()
6692   "Clear all information from the dribble buffer."
6693   (interactive)
6694   (gnus-dribble-clear)
6695   (gnus-message 7 "Cleared dribble buffer"))
6696
6697 (defun gnus-group-exit ()
6698   "Quit reading news after updating .newsrc.eld and .newsrc.
6699 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6700   (interactive)
6701   (when 
6702       (or noninteractive                ;For gnus-batch-kill
6703           (not gnus-interactive-exit)   ;Without confirmation
6704           gnus-expert-user
6705           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6706     (run-hooks 'gnus-exit-gnus-hook)
6707     ;; Offer to save data from non-quitted summary buffers.
6708     (gnus-offer-save-summaries)
6709     ;; Save the newsrc file(s).
6710     (gnus-save-newsrc-file)
6711     ;; Kill-em-all.
6712     (gnus-close-backends)
6713     ;; Reset everything.
6714     (gnus-clear-system)
6715     ;; Allow the user to do things after cleaning up.
6716     (run-hooks 'gnus-after-exiting-gnus-hook)))
6717
6718 (defun gnus-close-backends ()
6719   ;; Send a close request to all backends that support such a request.
6720   (let ((methods gnus-valid-select-methods)
6721         func)
6722     (while methods
6723       (if (fboundp (setq func (intern (concat (caar methods)
6724                                               "-request-close"))))
6725           (funcall func))
6726       (setq methods (cdr methods)))))
6727
6728 (defun gnus-group-quit ()
6729   "Quit reading news without updating .newsrc.eld or .newsrc.
6730 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6731   (interactive)
6732   (when (or noninteractive              ;For gnus-batch-kill
6733             (zerop (buffer-size))
6734             (not (gnus-server-opened gnus-select-method))
6735             gnus-expert-user
6736             (not gnus-current-startup-file)
6737             (gnus-yes-or-no-p
6738              (format "Quit reading news without saving %s? "
6739                      (file-name-nondirectory gnus-current-startup-file))))
6740     (run-hooks 'gnus-exit-gnus-hook)
6741     (if gnus-use-full-window
6742         (delete-other-windows)
6743       (gnus-remove-some-windows))
6744     (gnus-dribble-save)
6745     (gnus-close-backends)
6746     (gnus-clear-system)
6747     ;; Allow the user to do things after cleaning up.
6748     (run-hooks 'gnus-after-exiting-gnus-hook)))
6749
6750 (defun gnus-offer-save-summaries ()
6751   "Offer to save all active summary buffers."
6752   (save-excursion
6753     (let ((buflist (buffer-list))
6754           buffers bufname)
6755       ;; Go through all buffers and find all summaries.
6756       (while buflist
6757         (and (setq bufname (buffer-name (car buflist)))
6758              (string-match "Summary" bufname)
6759              (save-excursion
6760                (set-buffer bufname)
6761                ;; We check that this is, indeed, a summary buffer.
6762                (and (eq major-mode 'gnus-summary-mode)
6763                     ;; Also make sure this isn't bogus.
6764                     gnus-newsgroup-prepared))
6765              (push bufname buffers))
6766         (setq buflist (cdr buflist)))
6767       ;; Go through all these summary buffers and offer to save them.
6768       (when buffers
6769         (map-y-or-n-p
6770          "Update summary buffer %s? "
6771          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6772          buffers)))))
6773
6774 (defun gnus-group-describe-briefly ()
6775   "Give a one line description of the group mode commands."
6776   (interactive)
6777   (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")))
6778
6779 (defun gnus-group-browse-foreign-server (method)
6780   "Browse a foreign news server.
6781 If called interactively, this function will ask for a select method
6782  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6783 If not, METHOD should be a list where the first element is the method
6784 and the second element is the address."
6785   (interactive
6786    (list (let ((how (completing-read
6787                      "Which backend: "
6788                      (append gnus-valid-select-methods gnus-server-alist)
6789                      nil t (cons "nntp" 0) 'gnus-method-history)))
6790            ;; We either got a backend name or a virtual server name.
6791            ;; If the first, we also need an address.
6792            (if (assoc how gnus-valid-select-methods)
6793                (list (intern how)
6794                      ;; Suggested by mapjph@bath.ac.uk.
6795                      (completing-read
6796                       "Address: "
6797                       (mapcar (lambda (server) (list server))
6798                               gnus-secondary-servers)))
6799              ;; We got a server name, so we find the method.
6800              (gnus-server-to-method how)))))
6801   (gnus-browse-foreign-server method))
6802
6803 \f
6804 ;;;
6805 ;;; Gnus summary mode
6806 ;;;
6807
6808 (defvar gnus-summary-mode-map nil)
6809
6810 (put 'gnus-summary-mode 'mode-class 'special)
6811
6812 (unless gnus-summary-mode-map
6813   (setq gnus-summary-mode-map (make-keymap))
6814   (suppress-keymap gnus-summary-mode-map)
6815
6816   ;; Non-orthogonal keys
6817
6818   (gnus-define-keys gnus-summary-mode-map
6819     " " gnus-summary-next-page
6820     "\177" gnus-summary-prev-page
6821     [delete] gnus-summary-prev-page
6822     "\r" gnus-summary-scroll-up
6823     "n" gnus-summary-next-unread-article
6824     "p" gnus-summary-prev-unread-article
6825     "N" gnus-summary-next-article
6826     "P" gnus-summary-prev-article
6827     "\M-\C-n" gnus-summary-next-same-subject
6828     "\M-\C-p" gnus-summary-prev-same-subject
6829     "\M-n" gnus-summary-next-unread-subject
6830     "\M-p" gnus-summary-prev-unread-subject
6831     "." gnus-summary-first-unread-article
6832     "," gnus-summary-best-unread-article
6833     "\M-s" gnus-summary-search-article-forward
6834     "\M-r" gnus-summary-search-article-backward
6835     "<" gnus-summary-beginning-of-article
6836     ">" gnus-summary-end-of-article
6837     "j" gnus-summary-goto-article
6838     "^" gnus-summary-refer-parent-article
6839     "\M-^" gnus-summary-refer-article
6840     "u" gnus-summary-tick-article-forward
6841     "!" gnus-summary-tick-article-forward
6842     "U" gnus-summary-tick-article-backward
6843     "d" gnus-summary-mark-as-read-forward
6844     "D" gnus-summary-mark-as-read-backward
6845     "E" gnus-summary-mark-as-expirable
6846     "\M-u" gnus-summary-clear-mark-forward
6847     "\M-U" gnus-summary-clear-mark-backward
6848     "k" gnus-summary-kill-same-subject-and-select
6849     "\C-k" gnus-summary-kill-same-subject
6850     "\M-\C-k" gnus-summary-kill-thread
6851     "\M-\C-l" gnus-summary-lower-thread
6852     "e" gnus-summary-edit-article
6853     "#" gnus-summary-mark-as-processable
6854     "\M-#" gnus-summary-unmark-as-processable
6855     "\M-\C-t" gnus-summary-toggle-threads
6856     "\M-\C-s" gnus-summary-show-thread
6857     "\M-\C-h" gnus-summary-hide-thread
6858     "\M-\C-f" gnus-summary-next-thread
6859     "\M-\C-b" gnus-summary-prev-thread
6860     "\M-\C-u" gnus-summary-up-thread
6861     "\M-\C-d" gnus-summary-down-thread
6862     "&" gnus-summary-execute-command
6863     "c" gnus-summary-catchup-and-exit
6864     "\C-w" gnus-summary-mark-region-as-read
6865     "\C-t" gnus-summary-toggle-truncation
6866     "?" gnus-summary-mark-as-dormant
6867     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6868     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6869     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6870     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6871     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6872     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6873     "=" gnus-summary-expand-window
6874     "\C-x\C-s" gnus-summary-reselect-current-group
6875     "\M-g" gnus-summary-rescan-group
6876     "w" gnus-summary-stop-page-breaking
6877     "\C-c\C-r" gnus-summary-caesar-message
6878     "\M-t" gnus-summary-toggle-mime
6879     "f" gnus-summary-followup
6880     "F" gnus-summary-followup-with-original
6881     "C" gnus-summary-cancel-article
6882     "r" gnus-summary-reply
6883     "R" gnus-summary-reply-with-original
6884     "\C-c\C-f" gnus-summary-mail-forward
6885     "o" gnus-summary-save-article
6886     "\C-o" gnus-summary-save-article-mail
6887     "|" gnus-summary-pipe-output
6888     "\M-k" gnus-summary-edit-local-kill
6889     "\M-K" gnus-summary-edit-global-kill
6890     "V" gnus-version
6891     "\C-c\C-d" gnus-summary-describe-group
6892     "q" gnus-summary-exit
6893     "Q" gnus-summary-exit-no-update
6894     "\C-c\C-i" gnus-info-find-node
6895     gnus-mouse-2 gnus-mouse-pick-article
6896     "m" gnus-summary-mail-other-window
6897     "a" gnus-summary-post-news
6898     "x" gnus-summary-limit-to-unread
6899     "s" gnus-summary-isearch-article
6900     "t" gnus-article-hide-headers
6901     "g" gnus-summary-show-article
6902     "l" gnus-summary-goto-last-article
6903     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6904     "\C-d" gnus-summary-enter-digest-group
6905     "\C-c\C-b" gnus-bug
6906     "*" gnus-cache-enter-article
6907     "\M-*" gnus-cache-remove-article
6908     "\M-&" gnus-summary-universal-argument
6909     "\C-l" gnus-recenter
6910     "I" gnus-summary-increase-score
6911     "L" gnus-summary-lower-score
6912
6913     "V" gnus-summary-score-map
6914     "X" gnus-uu-extract-map
6915     "S" gnus-summary-send-map)
6916
6917   ;; Sort of orthogonal keymap
6918   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6919     "t" gnus-summary-tick-article-forward
6920     "!" gnus-summary-tick-article-forward
6921     "d" gnus-summary-mark-as-read-forward
6922     "r" gnus-summary-mark-as-read-forward
6923     "c" gnus-summary-clear-mark-forward
6924     " " gnus-summary-clear-mark-forward
6925     "e" gnus-summary-mark-as-expirable
6926     "x" gnus-summary-mark-as-expirable
6927     "?" gnus-summary-mark-as-dormant
6928     "b" gnus-summary-set-bookmark
6929     "B" gnus-summary-remove-bookmark
6930     "#" gnus-summary-mark-as-processable
6931     "\M-#" gnus-summary-unmark-as-processable
6932     "S" gnus-summary-limit-include-expunged
6933     "C" gnus-summary-catchup
6934     "H" gnus-summary-catchup-to-here
6935     "\C-c" gnus-summary-catchup-all
6936     "k" gnus-summary-kill-same-subject-and-select
6937     "K" gnus-summary-kill-same-subject
6938     "P" gnus-uu-mark-map)
6939
6940   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6941     "c" gnus-summary-clear-above
6942     "u" gnus-summary-tick-above
6943     "m" gnus-summary-mark-above
6944     "k" gnus-summary-kill-below)
6945
6946   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6947     "/" gnus-summary-limit-to-subject
6948     "n" gnus-summary-limit-to-articles
6949     "w" gnus-summary-pop-limit
6950     "s" gnus-summary-limit-to-subject
6951     "a" gnus-summary-limit-to-author
6952     "u" gnus-summary-limit-to-unread
6953     "m" gnus-summary-limit-to-marks
6954     "v" gnus-summary-limit-to-score
6955     "D" gnus-summary-limit-include-dormant
6956     "d" gnus-summary-limit-exclude-dormant
6957     ;;  "t" gnus-summary-limit-exclude-thread
6958     "E" gnus-summary-limit-include-expunged
6959     "c" gnus-summary-limit-exclude-childless-dormant
6960     "C" gnus-summary-limit-mark-excluded-as-read)
6961
6962   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6963     "n" gnus-summary-next-unread-article
6964     "p" gnus-summary-prev-unread-article
6965     "N" gnus-summary-next-article
6966     "P" gnus-summary-prev-article
6967     "\C-n" gnus-summary-next-same-subject
6968     "\C-p" gnus-summary-prev-same-subject
6969     "\M-n" gnus-summary-next-unread-subject
6970     "\M-p" gnus-summary-prev-unread-subject
6971     "f" gnus-summary-first-unread-article
6972     "b" gnus-summary-best-unread-article
6973     "j" gnus-summary-goto-article
6974     "g" gnus-summary-goto-subject
6975     "l" gnus-summary-goto-last-article
6976     "p" gnus-summary-pop-article)
6977
6978   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6979     "k" gnus-summary-kill-thread
6980     "l" gnus-summary-lower-thread
6981     "i" gnus-summary-raise-thread
6982     "T" gnus-summary-toggle-threads
6983     "t" gnus-summary-rethread-current
6984     "^" gnus-summary-reparent-thread
6985     "s" gnus-summary-show-thread
6986     "S" gnus-summary-show-all-threads
6987     "h" gnus-summary-hide-thread
6988     "H" gnus-summary-hide-all-threads
6989     "n" gnus-summary-next-thread
6990     "p" gnus-summary-prev-thread
6991     "u" gnus-summary-up-thread
6992     "o" gnus-summary-top-thread
6993     "d" gnus-summary-down-thread
6994     "#" gnus-uu-mark-thread
6995     "\M-#" gnus-uu-unmark-thread)
6996
6997   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6998     "c" gnus-summary-catchup-and-exit
6999     "C" gnus-summary-catchup-all-and-exit
7000     "E" gnus-summary-exit-no-update
7001     "Q" gnus-summary-exit
7002     "Z" gnus-summary-exit
7003     "n" gnus-summary-catchup-and-goto-next-group
7004     "R" gnus-summary-reselect-current-group
7005     "G" gnus-summary-rescan-group
7006     "N" gnus-summary-next-group
7007     "P" gnus-summary-prev-group)
7008
7009   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7010     " " gnus-summary-next-page
7011     "n" gnus-summary-next-page
7012     "\177" gnus-summary-prev-page
7013     [delete] gnus-summary-prev-page
7014     "p" gnus-summary-prev-page
7015     "\r" gnus-summary-scroll-up
7016     "<" gnus-summary-beginning-of-article
7017     ">" gnus-summary-end-of-article
7018     "b" gnus-summary-beginning-of-article
7019     "e" gnus-summary-end-of-article
7020     "^" gnus-summary-refer-parent-article
7021     "r" gnus-summary-refer-parent-article
7022     "R" gnus-summary-refer-references
7023     "g" gnus-summary-show-article
7024     "s" gnus-summary-isearch-article)
7025
7026   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7027     "b" gnus-article-add-buttons
7028     "B" gnus-article-add-buttons-to-head
7029     "o" gnus-article-treat-overstrike
7030     ;;  "w" gnus-article-word-wrap
7031     "w" gnus-article-fill-cited-article
7032     "c" gnus-article-remove-cr
7033     "L" gnus-article-remove-trailing-blank-lines
7034     "q" gnus-article-de-quoted-unreadable
7035     "f" gnus-article-display-x-face
7036     "l" gnus-summary-stop-page-breaking
7037     "r" gnus-summary-caesar-message
7038     "t" gnus-article-hide-headers
7039     "v" gnus-summary-verbose-headers
7040     "m" gnus-summary-toggle-mime)
7041
7042   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7043     "a" gnus-article-hide
7044     "h" gnus-article-hide-headers
7045     "b" gnus-article-hide-boring-headers
7046     "s" gnus-article-hide-signature
7047     "c" gnus-article-hide-citation
7048     "p" gnus-article-hide-pgp
7049     "\C-c" gnus-article-hide-citation-maybe)
7050
7051   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7052     "a" gnus-article-highlight
7053     "h" gnus-article-highlight-headers
7054     "c" gnus-article-highlight-citation
7055     "s" gnus-article-highlight-signature)
7056
7057   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7058     "z" gnus-article-date-ut
7059     "u" gnus-article-date-ut
7060     "l" gnus-article-date-local
7061     "e" gnus-article-date-lapsed
7062     "o" gnus-article-date-original)
7063
7064   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7065     "v" gnus-version
7066     "f" gnus-summary-fetch-faq
7067     "d" gnus-summary-describe-group
7068     "h" gnus-summary-describe-briefly
7069     "i" gnus-info-find-node)
7070
7071   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7072     "e" gnus-summary-expire-articles
7073     "\M-\C-e" gnus-summary-expire-articles-now
7074     "\177" gnus-summary-delete-article
7075     [delete] gnus-summary-delete-article
7076     "m" gnus-summary-move-article
7077     "r" gnus-summary-respool-article
7078     "w" gnus-summary-edit-article
7079     "c" gnus-summary-copy-article
7080     "B" gnus-summary-crosspost-article
7081     "q" gnus-summary-respool-query
7082     "i" gnus-summary-import-article)
7083
7084   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7085     "o" gnus-summary-save-article
7086     "m" gnus-summary-save-article-mail
7087     "r" gnus-summary-save-article-rmail
7088     "f" gnus-summary-save-article-file
7089     "b" gnus-summary-save-article-body-file
7090     "h" gnus-summary-save-article-folder
7091     "v" gnus-summary-save-article-vm
7092     "p" gnus-summary-pipe-output
7093     "s" gnus-soup-add-article)
7094   )
7095
7096 \f
7097
7098 (defun gnus-summary-mode (&optional group)
7099   "Major mode for reading articles.
7100
7101 All normal editing commands are switched off.
7102 \\<gnus-summary-mode-map>
7103 Each line in this buffer represents one article.  To read an
7104 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7105 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7106 respectively.
7107
7108 You can also post articles and send mail from this buffer.  To
7109 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7110 of an article, type `\\[gnus-summary-reply]'.
7111
7112 There are approx. one gazillion commands you can execute in this
7113 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7114
7115 The following commands are available:
7116
7117 \\{gnus-summary-mode-map}"
7118   (interactive)
7119   (when (and menu-bar-mode
7120              (gnus-visual-p 'summary-menu 'menu))
7121     (gnus-summary-make-menu-bar))
7122   (kill-all-local-variables)
7123   (let ((locals gnus-summary-local-variables))
7124     (while locals
7125       (if (consp (car locals))
7126           (progn
7127             (make-local-variable (caar locals))
7128             (set (caar locals) (eval (cdar locals))))
7129         (make-local-variable (car locals))
7130         (set (car locals) nil))
7131       (setq locals (cdr locals))))
7132   (gnus-make-thread-indent-array)
7133   (gnus-simplify-mode-line)
7134   (setq major-mode 'gnus-summary-mode)
7135   (setq mode-name "Summary")
7136   (make-local-variable 'minor-mode-alist)
7137   (use-local-map gnus-summary-mode-map)
7138   (buffer-disable-undo (current-buffer))
7139   (setq buffer-read-only t)             ;Disable modification
7140   (setq truncate-lines t)
7141   (setq selective-display t)
7142   (setq selective-display-ellipses t)   ;Display `...'
7143   (setq buffer-display-table gnus-summary-display-table)
7144   (setq gnus-newsgroup-name group)
7145   (make-local-variable 'gnus-summary-line-format)
7146   (make-local-variable 'gnus-summary-line-format-spec)
7147   (make-local-variable 'gnus-summary-mark-positions)
7148   (run-hooks 'gnus-summary-mode-hook))
7149
7150 (defun gnus-summary-make-display-table ()
7151   ;; Change the display table.  Odd characters have a tendency to mess
7152   ;; up nicely formatted displays - we make all possible glyphs
7153   ;; display only a single character.
7154
7155   ;; We start from the standard display table, if any.
7156   (setq gnus-summary-display-table
7157         (or (copy-sequence standard-display-table)
7158             (make-display-table)))
7159   ;; Nix out all the control chars...
7160   (let ((i 32))
7161     (while (>= (setq i (1- i)) 0)
7162       (aset gnus-summary-display-table i [??])))
7163   ;; ... but not newline and cr, of course. (cr is necessary for the
7164   ;; selective display).
7165   (aset gnus-summary-display-table ?\n nil)
7166   (aset gnus-summary-display-table ?\r nil)
7167   ;; We nix out any glyphs over 126 that are not set already.
7168   (let ((i 256))
7169     (while (>= (setq i (1- i)) 127)
7170       ;; Only modify if the entry is nil.
7171       (or (aref gnus-summary-display-table i)
7172           (aset gnus-summary-display-table i [??])))))
7173
7174 (defun gnus-summary-clear-local-variables ()
7175   (let ((locals gnus-summary-local-variables))
7176     (while locals
7177       (if (consp (car locals))
7178           (and (vectorp (caar locals))
7179                (set (caar locals) nil))
7180         (and (vectorp (car locals))
7181              (set (car locals) nil)))
7182       (setq locals (cdr locals)))))
7183
7184 ;; Summary data functions.
7185
7186 (defmacro gnus-data-number (data)
7187   `(car ,data))
7188
7189 (defmacro gnus-data-set-number (data number)
7190   `(setcar ,data ,number))
7191
7192 (defmacro gnus-data-mark (data)
7193   `(nth 1 ,data))
7194
7195 (defmacro gnus-data-set-mark (data mark)
7196   `(setcar (nthcdr 1 ,data) ,mark))
7197
7198 (defmacro gnus-data-pos (data)
7199   `(nth 2 ,data))
7200
7201 (defmacro gnus-data-set-pos (data pos)
7202   `(setcar (nthcdr 2 ,data) ,pos))
7203
7204 (defmacro gnus-data-header (data)
7205   `(nth 3 ,data))
7206
7207 (defmacro gnus-data-level (data)
7208   `(nth 4 ,data))
7209
7210 (defmacro gnus-data-unread-p (data)
7211   `(= (nth 1 ,data) gnus-unread-mark))
7212
7213 (defmacro gnus-data-pseudo-p (data)
7214   `(consp (nth 3 ,data)))
7215
7216 (defmacro gnus-data-find (number)
7217   `(assq ,number gnus-newsgroup-data))
7218
7219 (defmacro gnus-data-find-list (number &optional data)
7220   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7221      (memq (assq ,number bdata)
7222            bdata)))
7223
7224 (defmacro gnus-data-make (number mark pos header level)
7225   `(list ,number ,mark ,pos ,header ,level))
7226
7227 (defun gnus-data-enter (after-article number mark pos header level offset)
7228   (let ((data (gnus-data-find-list after-article)))
7229     (or data (error "No such article: %d" after-article))
7230     (setcdr data (cons (gnus-data-make number mark pos header level)
7231                        (cdr data)))
7232     (setq gnus-newsgroup-data-reverse nil)
7233     (gnus-data-update-list (cddr data) offset)))
7234
7235 (defun gnus-data-enter-list (after-article list &optional offset)
7236   (when list
7237     (let ((data (and after-article (gnus-data-find-list after-article)))
7238           (ilist list))
7239       (or data (not after-article) (error "No such article: %d" after-article))
7240       ;; Find the last element in the list to be spliced into the main
7241       ;; list.
7242       (while (cdr list)
7243         (setq list (cdr list)))
7244       (if (not data)
7245           (progn
7246             (setcdr list gnus-newsgroup-data)
7247             (setq gnus-newsgroup-data ilist)
7248             (and offset (gnus-data-update-list (cdr list) offset)))
7249         (setcdr list (cdr data))
7250         (setcdr data ilist)
7251         (and offset (gnus-data-update-list (cdr data) offset)))
7252       (setq gnus-newsgroup-data-reverse nil))))
7253
7254 (defun gnus-data-remove (article &optional offset)
7255   (let ((data gnus-newsgroup-data))
7256     (if (= (gnus-data-number (car data)) article)
7257         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7258               gnus-newsgroup-data-reverse nil)
7259       (while (cdr data)
7260         (and (= (gnus-data-number (cadr data)) article)
7261              (progn
7262                (setcdr data (cddr data))
7263                (and offset (gnus-data-update-list (cdr data) offset))
7264                (setq data nil
7265                      gnus-newsgroup-data-reverse nil)))
7266         (setq data (cdr data))))))
7267
7268 (defmacro gnus-data-list (backward)
7269   `(if ,backward
7270        (or gnus-newsgroup-data-reverse
7271            (setq gnus-newsgroup-data-reverse
7272                  (reverse gnus-newsgroup-data)))
7273      gnus-newsgroup-data))
7274
7275 (defun gnus-data-update-list (data offset)
7276   "Add OFFSET to the POS of all data entries in DATA."
7277   (while data
7278     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7279     (setq data (cdr data))))
7280
7281 (defun gnus-data-compute-positions ()
7282   "Compute the positions of all articles."
7283   (let ((data gnus-newsgroup-data)
7284         pos)
7285     (while data
7286       (when (setq pos (text-property-any
7287                        (point-min) (point-max)
7288                        'gnus-number (gnus-data-number (car data))))
7289         (gnus-data-set-pos (car data) (+ pos 3)))
7290       (setq data (cdr data)))))
7291
7292 (defun gnus-summary-article-pseudo-p (article)
7293   "Say whether this article is a pseudo article or not."
7294   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7295
7296 (defun gnus-article-parent-p (number)
7297   "Say whether this article is a parent or not."
7298   (let ((data (gnus-data-find-list number)))
7299     (and (cdr data)                     ; There has to be an article after...
7300          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7301             (gnus-data-level (nth 1 data))))))
7302
7303 (defun gnus-article-children (number)
7304   "Return a list of all children to NUMBER."
7305   (let* ((data (gnus-data-find-list number))
7306          (level (gnus-data-level (car data)))
7307          children)
7308     (setq data (cdr data))
7309     (while (and data            
7310                 (= (gnus-data-level (car data)) (1+ level)))
7311       (push (gnus-data-number (car data)) children)
7312       (setq data (cdr data)))
7313     children))
7314
7315 (defmacro gnus-summary-skip-intangible ()
7316   "If the current article is intangible, then jump to a different article."
7317   '(let ((to (get-text-property (point) 'gnus-intangible)))
7318     (and to (gnus-summary-goto-subject to))))
7319
7320 (defmacro gnus-summary-article-intangible-p ()
7321   "Say whether this article is intangible or not."
7322   '(get-text-property (point) 'gnus-intangible))
7323
7324 ;; Some summary mode macros.
7325
7326 (defmacro gnus-summary-article-number ()
7327   "The article number of the article on the current line.
7328 If there isn's an article number here, then we return the current
7329 article number."
7330   '(progn
7331      (gnus-summary-skip-intangible)
7332      (or (get-text-property (point) 'gnus-number)
7333          (gnus-summary-last-subject))))
7334
7335 (defmacro gnus-summary-article-header (&optional number)
7336   `(gnus-data-header (gnus-data-find
7337                       ,(or number '(gnus-summary-article-number)))))
7338
7339 (defmacro gnus-summary-thread-level (&optional number)
7340   `(if (and (eq gnus-summary-make-false-root 'dummy)
7341             (get-text-property (point) 'gnus-intangible))
7342        0
7343      (gnus-data-level (gnus-data-find
7344                        ,(or number '(gnus-summary-article-number))))))
7345
7346 (defmacro gnus-summary-article-mark (&optional number)
7347   `(gnus-data-mark (gnus-data-find
7348                     ,(or number '(gnus-summary-article-number)))))
7349
7350 (defmacro gnus-summary-article-pos (&optional number)
7351   `(gnus-data-pos (gnus-data-find
7352                    ,(or number '(gnus-summary-article-number)))))
7353
7354 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7355 (defmacro gnus-summary-article-subject (&optional number)
7356   "Return current subject string or nil if nothing."
7357   `(let ((headers
7358           ,(if number
7359                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7360              '(gnus-data-header (assq (gnus-summary-article-number)
7361                                       gnus-newsgroup-data)))))
7362      (and headers
7363           (vectorp headers)
7364           (mail-header-subject headers))))
7365
7366 (defmacro gnus-summary-article-score (&optional number)
7367   "Return current article score."
7368   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7369                   gnus-newsgroup-scored))
7370        gnus-summary-default-score 0))
7371
7372 (defun gnus-summary-article-children (&optional number)
7373   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7374          (level (gnus-data-level (car data)))
7375          l children)
7376     (while (and (setq data (cdr data))
7377                 (> (setq l (gnus-data-level (car data))) level))
7378       (and (= (1+ level) l)
7379            (setq children (cons (gnus-data-number (car data))
7380                                 children))))
7381     (nreverse children)))
7382
7383 (defun gnus-summary-article-parent (&optional number)
7384   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7385                                     (gnus-data-list t)))
7386          (level (gnus-data-level (car data))))
7387     (if (zerop level)
7388         () ; This is a root.
7389       ;; We search until we find an article with a level less than
7390       ;; this one.  That function has to be the parent.
7391       (while (and (setq data (cdr data))
7392                   (not (< (gnus-data-level (car data)) level))))
7393       (and data (gnus-data-number (car data))))))
7394
7395 (defun gnus-unread-mark-p (mark)
7396   "Say whether MARK is the unread mark."
7397   (= mark gnus-unread-mark))
7398
7399 (defun gnus-read-mark-p (mark)
7400   "Say whether MARK is one of the marks that mark as read.
7401 This is all marks except unread, ticked, dormant, and expirable."
7402   (not (or (= mark gnus-unread-mark)
7403            (= mark gnus-ticked-mark)
7404            (= mark gnus-dormant-mark)
7405            (= mark gnus-expirable-mark))))
7406
7407 ;; Saving hidden threads.
7408
7409 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7410 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7411 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7412
7413 (defmacro gnus-save-hidden-threads (&rest forms)
7414   "Save hidden threads, eval FORMS, and restore the hidden threads."
7415   (let ((config (make-symbol "config")))
7416     `(let ((,config (gnus-hidden-threads-configuration)))
7417        (unwind-protect
7418            (progn
7419              ,@forms)
7420          (gnus-restore-hidden-threads-configuration ,config)))))
7421
7422 (defun gnus-hidden-threads-configuration ()
7423   "Return the current hidden threads configuration."
7424   (save-excursion
7425     (let (config)
7426       (goto-char (point-min))
7427       (while (search-forward "\r" nil t)
7428         (push (1- (point)) config))
7429       config)))
7430
7431 (defun gnus-restore-hidden-threads-configuration (config)
7432   "Restore hidden threads configuration from CONFIG."
7433   (let (point buffer-read-only)
7434     (while (setq point (pop config))
7435       (when (and (< point (point-max))
7436                  (goto-char point)
7437                  (= (following-char) ?\n))
7438         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7439
7440 ;; Various summary mode internalish functions.
7441
7442 (defun gnus-mouse-pick-article (e)
7443   (interactive "e")
7444   (mouse-set-point e)
7445   (gnus-summary-next-page nil t))
7446
7447 (defun gnus-summary-setup-buffer (group)
7448   "Initialize summary buffer."
7449   (let ((buffer (concat "*Summary " group "*")))
7450     (if (get-buffer buffer)
7451         (progn
7452           (set-buffer buffer)
7453           (setq gnus-summary-buffer (current-buffer))
7454           (not gnus-newsgroup-prepared))
7455       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7456       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7457       (gnus-add-current-to-buffer-list)
7458       (gnus-summary-mode group)
7459       (when gnus-carpal
7460         (gnus-carpal-setup-buffer 'summary))
7461       (unless gnus-single-article-buffer
7462         (make-local-variable 'gnus-article-buffer)
7463         (make-local-variable 'gnus-article-current)
7464         (make-local-variable 'gnus-original-article-buffer))
7465       (setq gnus-newsgroup-name group)
7466       t)))
7467
7468 (defun gnus-set-global-variables ()
7469   ;; Set the global equivalents of the summary buffer-local variables
7470   ;; to the latest values they had.  These reflect the summary buffer
7471   ;; that was in action when the last article was fetched.
7472   (when (eq major-mode 'gnus-summary-mode)
7473     (setq gnus-summary-buffer (current-buffer))
7474     (let ((name gnus-newsgroup-name)
7475           (marked gnus-newsgroup-marked)
7476           (unread gnus-newsgroup-unreads)
7477           (headers gnus-current-headers)
7478           (data gnus-newsgroup-data)
7479           (summary gnus-summary-buffer)
7480           (article-buffer gnus-article-buffer)
7481           (original gnus-original-article-buffer)
7482           (gac gnus-article-current)
7483           (score-file gnus-current-score-file))
7484       (save-excursion
7485         (set-buffer gnus-group-buffer)
7486         (setq gnus-newsgroup-name name)
7487         (setq gnus-newsgroup-marked marked)
7488         (setq gnus-newsgroup-unreads unread)
7489         (setq gnus-current-headers headers)
7490         (setq gnus-newsgroup-data data)
7491         (setq gnus-article-current gac)
7492         (setq gnus-summary-buffer summary)
7493         (setq gnus-article-buffer article-buffer)
7494         (setq gnus-original-article-buffer original)
7495         (setq gnus-current-score-file score-file)))))
7496
7497 (defun gnus-summary-last-article-p (&optional article)
7498   "Return whether ARTICLE is the last article in the buffer."
7499   (if (not (setq article (or article (gnus-summary-article-number))))
7500       t ; All non-existant numbers are the last article. :-)
7501     (not (cdr (gnus-data-find-list article)))))
7502
7503 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7504   "Insert a dummy root in the summary buffer."
7505   (beginning-of-line)
7506   (gnus-add-text-properties
7507    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7508    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7509
7510 (defun gnus-make-thread-indent-array ()
7511   (let ((n 200))
7512     (unless (and gnus-thread-indent-array
7513                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7514       (setq gnus-thread-indent-array (make-vector 201 "")
7515             gnus-thread-indent-array-level gnus-thread-indent-level)
7516       (while (>= n 0)
7517         (aset gnus-thread-indent-array n
7518               (make-string (* n gnus-thread-indent-level) ? ))
7519         (setq n (1- n))))))
7520
7521 (defun gnus-summary-insert-line
7522   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7523                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7524                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7525   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7526          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7527          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7528          (gnus-tmp-score-char
7529           (if (or (null gnus-summary-default-score)
7530                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7531                       gnus-summary-zcore-fuzz)) ? 
7532             (if (< gnus-tmp-score gnus-summary-default-score)
7533                 gnus-score-below-mark gnus-score-over-mark)))
7534          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7535                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7536                                   gnus-cached-mark)
7537                                  (gnus-tmp-replied gnus-replied-mark)
7538                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7539                                   gnus-saved-mark)
7540                                  (t gnus-unread-mark)))
7541          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7542          (gnus-tmp-name
7543           (cond
7544            ((string-match "(.+)" gnus-tmp-from)
7545             (substring gnus-tmp-from
7546                        (1+ (match-beginning 0)) (1- (match-end 0))))
7547            ((string-match "<[^>]+> *$" gnus-tmp-from)
7548             (let ((beg (match-beginning 0)))
7549               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7550                        (substring gnus-tmp-from (1+ (match-beginning 0))
7551                                   (1- (match-end 0))))
7552                   (substring gnus-tmp-from 0 beg))))
7553            (t gnus-tmp-from)))
7554          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7555          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7556          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7557          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7558          (buffer-read-only nil))
7559     (when (string= gnus-tmp-name "")
7560       (setq gnus-tmp-name gnus-tmp-from))
7561     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7562     (gnus-put-text-property
7563      (point)
7564      (progn (eval gnus-summary-line-format-spec) (point))
7565      'gnus-number gnus-tmp-number)
7566     (when (gnus-visual-p 'summary-highlight 'highlight)
7567       (forward-line -1)
7568       (run-hooks 'gnus-summary-update-hook)
7569       (forward-line 1))))
7570
7571 (defun gnus-summary-update-line (&optional dont-update)
7572   ;; Update summary line after change.
7573   (when (and gnus-summary-default-score
7574              (not gnus-summary-inhibit-highlight))
7575     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7576            (article (gnus-summary-article-number))
7577            (score (gnus-summary-article-score article)))
7578       (unless dont-update
7579         (if (and gnus-summary-mark-below
7580                  (< (gnus-summary-article-score)
7581                     gnus-summary-mark-below))
7582             ;; This article has a low score, so we mark it as read.
7583             (when (memq article gnus-newsgroup-unreads)
7584               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7585           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7586             ;; This article was previously marked as read on account
7587             ;; of a low score, but now it has risen, so we mark it as
7588             ;; unread.
7589             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7590         (gnus-summary-update-mark
7591          (if (or (null gnus-summary-default-score)
7592                  (<= (abs (- score gnus-summary-default-score))
7593                      gnus-summary-zcore-fuzz)) ? 
7594            (if (< score gnus-summary-default-score)
7595                gnus-score-below-mark gnus-score-over-mark)) 'score))
7596       ;; Do visual highlighting.
7597       (when (gnus-visual-p 'summary-highlight 'highlight)
7598         (run-hooks 'gnus-summary-update-hook)))))
7599
7600 (defvar gnus-tmp-new-adopts nil)
7601
7602 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7603   ;; Sum up all elements (and sub-elements) in a list.
7604   (let* ((number
7605           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7606           (cond
7607            ((and (consp thread) (cdr thread))
7608             (apply
7609              '+ 1 (mapcar
7610                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7611            ((null thread)
7612             1)
7613            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7614             1)
7615            (t 0))))
7616     (when (and level (zerop level) gnus-tmp-new-adopts)
7617       (incf number
7618             (apply '+ (mapcar
7619                        'gnus-summary-number-of-articles-in-thread
7620                        gnus-tmp-new-adopts))))
7621     (if char
7622         (if (> number 1) gnus-not-empty-thread-mark
7623           gnus-empty-thread-mark)
7624       number)))
7625
7626 (defun gnus-summary-set-local-parameters (group)
7627  "Go through the local params of GROUP and set all variable specs in that list."
7628   (let ((params (gnus-info-params (gnus-get-info group)))
7629         elem)
7630     (while params
7631       (setq elem (car params)
7632             params (cdr params))
7633       (and (consp elem)                 ; Has to be a cons.
7634            (consp (cdr elem))           ; The cdr has to be a list.
7635            (symbolp (car elem))         ; Has to be a symbol in there.
7636            (not (memq (car elem) 
7637                       '(quit-config to-address to-list to-group)))
7638            (progn                       ; So we set it.
7639              (make-local-variable (car elem))
7640              (set (car elem) (eval (nth 1 elem))))))))
7641
7642 (defun gnus-summary-read-group (group &optional show-all no-article
7643                                       kill-buffer no-display)
7644   "Start reading news in newsgroup GROUP.
7645 If SHOW-ALL is non-nil, already read articles are also listed.
7646 If NO-ARTICLE is non-nil, no article is selected initially.
7647 If NO-DISPLAY, don't generate a summary buffer."
7648   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7649   (let* ((new-group (gnus-summary-setup-buffer group))
7650          (quit-config (gnus-group-quit-config group))
7651          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7652     (cond
7653      ;; This summary buffer exists already, so we just select it.
7654      ((not new-group)
7655       (gnus-set-global-variables)
7656       (when kill-buffer
7657         (gnus-kill-or-deaden-summary kill-buffer))
7658       (gnus-configure-windows 'summary 'force)
7659       (gnus-set-mode-line 'summary)
7660       (gnus-summary-position-point)
7661       (message "")
7662       t)
7663      ;; We couldn't select this group.
7664      ((null did-select)
7665       (when (and (eq major-mode 'gnus-summary-mode)
7666                  (not (equal (current-buffer) kill-buffer)))
7667         (kill-buffer (current-buffer))
7668         (if (not quit-config)
7669             (progn
7670               (set-buffer gnus-group-buffer)
7671               (gnus-group-jump-to-group group)
7672               (gnus-group-next-unread-group 1))
7673           (if (not (buffer-name (car quit-config)))
7674               (gnus-configure-windows 'group 'force)
7675             (set-buffer (car quit-config))
7676             (and (eq major-mode 'gnus-summary-mode)
7677                  (gnus-set-global-variables))
7678             (gnus-configure-windows (cdr quit-config)))))
7679       (gnus-message 3 "Can't select group")
7680       nil)
7681      ;; The user did a `C-g' while prompting for number of articles,
7682      ;; so we exit this group.
7683      ((eq did-select 'quit)
7684       (and (eq major-mode 'gnus-summary-mode)
7685            (not (equal (current-buffer) kill-buffer))
7686            (kill-buffer (current-buffer)))
7687       (when kill-buffer
7688         (gnus-kill-or-deaden-summary kill-buffer))
7689       (if (not quit-config)
7690           (progn
7691             (set-buffer gnus-group-buffer)
7692             (gnus-group-jump-to-group group)
7693             (gnus-group-next-unread-group 1)
7694             (gnus-configure-windows 'group 'force))
7695         (if (not (buffer-name (car quit-config)))
7696             (gnus-configure-windows 'group 'force)
7697           (set-buffer (car quit-config))
7698           (and (eq major-mode 'gnus-summary-mode)
7699                (gnus-set-global-variables))
7700           (gnus-configure-windows (cdr quit-config))))
7701       ;; Finally signal the quit.
7702       (signal 'quit nil))
7703      ;; The group was successfully selected.
7704      (t
7705       (gnus-set-global-variables)
7706       ;; Save the active value in effect when the group was entered.
7707       (setq gnus-newsgroup-active
7708             (gnus-copy-sequence
7709              (gnus-active gnus-newsgroup-name)))
7710       ;; You can change the summary buffer in some way with this hook.
7711       (run-hooks 'gnus-select-group-hook)
7712       ;; Set any local variables in the group parameters.
7713       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7714       (gnus-update-format-specifications)
7715       ;; Do score processing.
7716       (when gnus-use-scoring
7717         (gnus-possibly-score-headers))
7718       ;; Check whether to fill in the gaps in the threads.
7719       (when gnus-build-sparse-threads
7720         (gnus-build-sparse-threads))
7721       ;; Find the initial limit.
7722       (if gnus-show-threads
7723           (if show-all
7724               (let ((gnus-newsgroup-dormant nil))
7725                 (gnus-summary-initial-limit show-all))
7726             (gnus-summary-initial-limit show-all))
7727         (setq gnus-newsgroup-limit 
7728               (mapcar 
7729                (lambda (header) (mail-header-number header))
7730                gnus-newsgroup-headers)))
7731       ;; Generate the summary buffer.
7732       (unless no-display
7733         (gnus-summary-prepare))
7734       (when gnus-use-trees
7735         (gnus-tree-open group)
7736         (setq gnus-summary-highlight-line-function
7737               'gnus-tree-highlight-article))
7738       ;; If the summary buffer is empty, but there are some low-scored
7739       ;; articles or some excluded dormants, we include these in the
7740       ;; buffer.
7741       (when (and (zerop (buffer-size))
7742                  (not no-display))
7743         (cond (gnus-newsgroup-dormant
7744                (gnus-summary-limit-include-dormant))
7745               ((and gnus-newsgroup-scored show-all)
7746                (gnus-summary-limit-include-expunged))))
7747       ;; Function `gnus-apply-kill-file' must be called in this hook.
7748       (run-hooks 'gnus-apply-kill-hook)
7749       (if (and (zerop (buffer-size))
7750                (not no-display))
7751           (progn
7752             ;; This newsgroup is empty.
7753             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7754             (gnus-message 6 "No unread news")
7755             (when kill-buffer
7756               (gnus-kill-or-deaden-summary kill-buffer))
7757             ;; Return nil from this function.
7758             nil)
7759         ;; Hide conversation thread subtrees.  We cannot do this in
7760         ;; gnus-summary-prepare-hook since kill processing may not
7761         ;; work with hidden articles.
7762         (and gnus-show-threads
7763              gnus-thread-hide-subtree
7764              (gnus-summary-hide-all-threads))
7765         ;; Show first unread article if requested.
7766         (if (and (not no-article)
7767                  (not no-display)
7768                  gnus-newsgroup-unreads
7769                  gnus-auto-select-first)
7770             (unless (if (eq gnus-auto-select-first 'best)
7771                         (gnus-summary-best-unread-article)
7772                       (gnus-summary-first-unread-article))
7773               (gnus-configure-windows 'summary))
7774           ;; Don't select any articles, just move point to the first
7775           ;; article in the group.
7776           (goto-char (point-min))
7777           (gnus-summary-position-point)
7778           (gnus-set-mode-line 'summary)
7779           (gnus-configure-windows 'summary 'force))
7780         ;; If we are in async mode, we send some info to the backend.
7781         (when gnus-newsgroup-async
7782           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7783         (when kill-buffer
7784           (gnus-kill-or-deaden-summary kill-buffer))
7785         (when (get-buffer-window gnus-group-buffer t)
7786           ;; Gotta use windows, because recenter does wierd stuff if
7787           ;; the current buffer ain't the displayed window.
7788           (let ((owin (selected-window)))
7789             (select-window (get-buffer-window gnus-group-buffer t))
7790             (when (gnus-group-goto-group group)
7791               (recenter))
7792             (select-window owin))))
7793       ;; Mark this buffer as "prepared".
7794       (setq gnus-newsgroup-prepared t)
7795       t))))
7796
7797 (defun gnus-summary-prepare ()
7798   "Generate the summary buffer."
7799   (let ((buffer-read-only nil))
7800     (erase-buffer)
7801     (setq gnus-newsgroup-data nil
7802           gnus-newsgroup-data-reverse nil)
7803     (run-hooks 'gnus-summary-generate-hook)
7804     ;; Generate the buffer, either with threads or without.
7805     (when gnus-newsgroup-headers
7806       (gnus-summary-prepare-threads
7807        (if gnus-show-threads
7808            (gnus-sort-gathered-threads
7809             (funcall gnus-summary-thread-gathering-function
7810                      (gnus-sort-threads
7811                       (gnus-cut-threads (gnus-make-threads)))))
7812          ;; Unthreaded display.
7813          (gnus-sort-articles gnus-newsgroup-headers))))
7814     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7815     ;; Call hooks for modifying summary buffer.
7816     (goto-char (point-min))
7817     (run-hooks 'gnus-summary-prepare-hook)))
7818
7819 (defun gnus-gather-threads-by-subject (threads)
7820   "Gather threads by looking at Subject headers."
7821   (if (not gnus-summary-make-false-root)
7822       threads
7823     (let ((hashtb (gnus-make-hashtable 1023))
7824           (prev threads)
7825           (result threads)
7826           subject hthread whole-subject)
7827       (while threads
7828         (setq whole-subject (mail-header-subject (caar threads)))
7829         (setq subject
7830               (cond
7831                ;; Truncate the subject.
7832                ((numberp gnus-summary-gather-subject-limit)
7833                 (setq subject (gnus-simplify-subject-re whole-subject))
7834                 (if (> (length subject) gnus-summary-gather-subject-limit)
7835                     (substring subject 0 gnus-summary-gather-subject-limit)
7836                   subject))
7837                ;; Fuzzily simplify it.
7838                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7839                 (gnus-simplify-subject-fuzzy whole-subject))
7840                ;; Just remove the leading "Re:".
7841                (t
7842                 (gnus-simplify-subject-re whole-subject))))
7843
7844         (if (and gnus-summary-gather-exclude-subject
7845                  (string-match gnus-summary-gather-exclude-subject
7846                                subject))
7847             ()          ; We don't want to do anything with this article.
7848           ;; We simplify the subject before looking it up in the
7849           ;; hash table.
7850
7851           (if (setq hthread (gnus-gethash subject hashtb))
7852               (progn
7853                 ;; We enter a dummy root into the thread, if we
7854                 ;; haven't done that already.
7855                 (unless (stringp (caar hthread))
7856                   (setcar hthread (list whole-subject (car hthread))))
7857                 ;; We add this new gathered thread to this gathered
7858                 ;; thread.
7859                 (setcdr (car hthread)
7860                         (nconc (cdar hthread) (list (car threads))))
7861                 ;; Remove it from the list of threads.
7862                 (setcdr prev (cdr threads))
7863                 (setq threads prev))
7864             ;; Enter this thread into the hash table.
7865             (gnus-sethash subject threads hashtb)))
7866         (setq prev threads)
7867         (setq threads (cdr threads)))
7868       result)))
7869
7870 (defun gnus-gather-threads-by-references (threads)
7871   "Gather threads by looking at References headers."
7872   (let ((idhashtb (gnus-make-hashtable 1023))
7873         (thhashtb (gnus-make-hashtable 1023))
7874         (prev threads)
7875         (result threads)
7876         ids references id gthread gid entered)
7877     (while threads
7878       (when (setq references (mail-header-references (caar threads)))
7879         (setq id (mail-header-id (caar threads)))
7880         (setq ids (gnus-split-references references))
7881         (setq entered nil)
7882         (while ids
7883           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7884               (progn
7885                 (gnus-sethash (car ids) id idhashtb)
7886                 (gnus-sethash id threads thhashtb))
7887             (setq gthread (gnus-gethash gid thhashtb))
7888             (unless entered
7889               ;; We enter a dummy root into the thread, if we
7890               ;; haven't done that already.
7891               (unless (stringp (caar gthread))
7892                 (setcar gthread (list (mail-header-subject (caar gthread))
7893                                       (car gthread))))
7894               ;; We add this new gathered thread to this gathered
7895               ;; thread.
7896               (setcdr (car gthread)
7897                       (nconc (cdar gthread) (list (car threads)))))
7898             ;; Add it into the thread hash table.
7899             (gnus-sethash id gthread thhashtb)
7900             (setq entered t)
7901             ;; Remove it from the list of threads.
7902             (setcdr prev (cdr threads))
7903             (setq threads prev))
7904           (setq ids (cdr ids))))
7905       (setq prev threads)
7906       (setq threads (cdr threads)))
7907     result))
7908
7909 (defun gnus-sort-gathered-threads (threads)
7910   "Sort subtreads inside each gathered thread by article number."
7911   (let ((result threads))
7912     (while threads
7913       (when (stringp (caar threads))
7914         (setcdr (car threads)
7915                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7916       (setq threads (cdr threads)))
7917     result))
7918
7919 (defun gnus-make-threads ()
7920   "Go through the dependency hashtb and find the roots.  Return all threads."
7921   (let (threads)
7922     (mapatoms
7923      (lambda (refs)
7924        (unless (car (symbol-value refs))
7925          ;; These threads do not refer back to any other articles,
7926          ;; so they're roots.
7927          (setq threads (append (cdr (symbol-value refs)) threads))))
7928      gnus-newsgroup-dependencies)
7929     threads))
7930
7931 (defun gnus-build-sparse-threads ()
7932   (let ((headers gnus-newsgroup-headers)
7933         (deps gnus-newsgroup-dependencies)
7934         header references generation relations 
7935         cthread subject child end pthread relation)
7936     ;; First we create an alist of generations/relations, where 
7937     ;; generations is how much we trust the ralation, and the relation
7938     ;; is parent/child.
7939     (gnus-message 7 "Making sparse threads...")
7940     (save-excursion
7941       (nnheader-set-temp-buffer " *gnus sparse threads*")
7942       (while (setq header (pop headers))
7943         (when (and (setq references (mail-header-references header))
7944                    (not (string= references "")))
7945           (insert references)
7946           (setq child (mail-header-id header)
7947                 subject (mail-header-subject header))
7948           (setq generation 0)
7949           (while (search-backward ">" nil t)
7950             (setq end (1+ (point)))
7951             (when (search-backward "<" nil t)
7952               (push (list (incf generation) 
7953                           child (setq child (buffer-substring (point) end))
7954                           subject)
7955                     relations)))
7956           (push (list (1+ generation) child nil subject) relations)
7957           (erase-buffer)))
7958       (kill-buffer (current-buffer)))
7959     ;; Sort over trustworthiness.
7960     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7961     (while (setq relation (pop relations))
7962       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7963                 (unless (car (symbol-value cthread))
7964                   ;; Make this article the parent of these threads.
7965                   (setcar (symbol-value cthread)
7966                           (vector gnus-reffed-article-number 
7967                                   (cadddr relation) 
7968                                   "" ""
7969                                   (cadr relation) 
7970                                   (or (caddr relation) "") 0 0 "")))
7971               (set cthread (list (vector gnus-reffed-article-number
7972                                          (cadddr relation) 
7973                                          "" "" (cadr relation) 
7974                                          (or (caddr relation) "") 0 0 ""))))
7975         (push gnus-reffed-article-number gnus-newsgroup-limit)
7976         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7977         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7978               gnus-newsgroup-reads)
7979         (decf gnus-reffed-article-number)
7980         ;; Make this new thread the child of its parent.
7981         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7982             (setcdr (symbol-value pthread)
7983                     (nconc (cdr (symbol-value pthread))
7984                            (list (symbol-value cthread))))
7985           (set pthread (list nil (symbol-value cthread))))))
7986     (gnus-message 7 "Making sparse threads...done")))
7987
7988 (defun gnus-build-old-threads ()
7989   ;; Look at all the articles that refer back to old articles, and
7990   ;; fetch the headers for the articles that aren't there.  This will
7991   ;; build complete threads - if the roots haven't been expired by the
7992   ;; server, that is.
7993   (let (id heads)
7994     (mapatoms
7995      (lambda (refs)
7996        (when (not (car (symbol-value refs)))
7997          (setq heads (cdr (symbol-value refs)))
7998          (while heads
7999            (if (memq (mail-header-number (caar heads))
8000                      gnus-newsgroup-dormant)
8001                (setq heads (cdr heads))
8002              (setq id (symbol-name refs))
8003              (while (and (setq id (gnus-build-get-header id))
8004                          (not (car (gnus-gethash
8005                                     id gnus-newsgroup-dependencies)))))
8006              (setq heads nil)))))
8007      gnus-newsgroup-dependencies)))
8008
8009 (defun gnus-build-get-header (id)
8010   ;; Look through the buffer of NOV lines and find the header to
8011   ;; ID.  Enter this line into the dependencies hash table, and return
8012   ;; the id of the parent article (if any).
8013   (let ((deps gnus-newsgroup-dependencies)
8014         found header)
8015     (prog1
8016         (save-excursion
8017           (set-buffer nntp-server-buffer)
8018           (goto-char (point-min))
8019           (while (and (not found) (search-forward id nil t))
8020             (beginning-of-line)
8021             (setq found (looking-at
8022                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8023                                  (regexp-quote id))))
8024             (or found (beginning-of-line 2)))
8025           (when found
8026             (beginning-of-line)
8027             (and
8028              (setq header (gnus-nov-parse-line
8029                            (read (current-buffer)) deps))
8030              (gnus-parent-id (mail-header-references header)))))
8031       (when header
8032         (let ((number (mail-header-number header)))
8033           (push number gnus-newsgroup-limit)
8034           (push header gnus-newsgroup-headers)
8035           (if (memq number gnus-newsgroup-unselected)
8036               (progn
8037                 (push number gnus-newsgroup-unreads)
8038                 (setq gnus-newsgroup-unselected
8039                       (delq number gnus-newsgroup-unselected)))
8040             (push number gnus-newsgroup-ancient)))))))
8041
8042 (defun gnus-summary-update-article (article &optional header)
8043   "Update ARTICLE in the summary buffer."
8044   (set-buffer gnus-summary-buffer)
8045   (let* ((header (or header (gnus-summary-article-header article)))
8046          (id (mail-header-id header))
8047          (data (gnus-data-find article))
8048          (thread (gnus-id-to-thread id))
8049          (parent
8050           (gnus-id-to-thread (or (gnus-parent-id 
8051                                   (mail-header-references header))
8052                                  "tull")))
8053          (buffer-read-only nil)
8054          (old (car thread))
8055          (number (mail-header-number header))
8056          pos)
8057     (when thread
8058       (setcar thread nil)
8059       (when parent
8060         (delq thread parent))
8061       (if (gnus-summary-insert-subject id header)
8062           ;; Set the (possibly) new article number in the data structure.
8063           (gnus-data-set-number data (gnus-id-to-article id))
8064         (setcar thread old)
8065         nil))))
8066
8067 (defun gnus-rebuild-thread (id)
8068   "Rebuild the thread containing ID."
8069   (let ((buffer-read-only nil)
8070         current thread data)
8071     (if (not gnus-show-threads)
8072         (setq thread (list (car (gnus-id-to-thread id))))
8073       ;; Get the thread this article is part of.
8074       (setq thread (gnus-remove-thread id)))
8075     (setq current (save-excursion
8076                     (and (zerop (forward-line -1))
8077                          (gnus-summary-article-number))))
8078     ;; If this is a gathered thread, we have to go some re-gathering.
8079     (when (stringp (car thread))
8080       (let ((subject (car thread))
8081             roots thr)
8082         (setq thread (cdr thread))
8083         (while thread
8084           (unless (memq (setq thr (gnus-id-to-thread
8085                                       (gnus-root-id
8086                                        (mail-header-id (caar thread)))))
8087                         roots)
8088             (push thr roots))
8089           (setq thread (cdr thread)))
8090         ;; We now have all (unique) roots.
8091         (if (= (length roots) 1)
8092             ;; All the loose roots are now one solid root.
8093             (setq thread (car roots))
8094           (setq thread (cons subject (gnus-sort-threads roots))))))
8095     (let (threads)
8096       ;; We then insert this thread into the summary buffer.
8097       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8098         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8099         (setq data (nreverse gnus-newsgroup-data))
8100         (setq threads gnus-newsgroup-threads))
8101       ;; We splice the new data into the data structure.
8102       (gnus-data-enter-list current data)
8103       (gnus-data-compute-positions)
8104       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8105
8106 (defun gnus-id-to-thread (id)
8107   "Return the (sub-)thread where ID appears."
8108   (gnus-gethash id gnus-newsgroup-dependencies))
8109
8110 (defun gnus-id-to-article (id)
8111   "Return the article number of ID."
8112   (let ((thread (gnus-id-to-thread id)))
8113     (when (and thread
8114                (car thread))
8115       (mail-header-number (car thread)))))
8116
8117 (defun gnus-id-to-header (id)
8118   "Return the article headers of ID."
8119   (car (gnus-id-to-thread id)))
8120
8121 (defun gnus-article-displayed-root-p (article)
8122   "Say whether ARTICLE is a root(ish) article."
8123   (let ((level (gnus-summary-thread-level article))
8124         (refs (mail-header-references  (gnus-summary-article-header article)))
8125         particle)
8126     (cond 
8127      ((null level) nil)
8128      ((zerop level) t)
8129      ((null refs) t)
8130      ((null (gnus-parent-id refs)) t)
8131      ((and (= 1 level)
8132            (null (setq particle (gnus-id-to-article
8133                                  (gnus-parent-id refs))))
8134            (null (gnus-summary-thread-level particle)))))))
8135
8136 (defun gnus-root-id (id)
8137   "Return the id of the root of the thread where ID appears."
8138   (let (last-id prev)
8139     (while (and id (setq prev (car (gnus-gethash 
8140                                     id gnus-newsgroup-dependencies))))
8141       (setq last-id id
8142             id (gnus-parent-id (mail-header-references prev))))
8143     last-id))
8144
8145 (defun gnus-remove-thread (id &optional dont-remove)
8146   "Remove the thread that has ID in it."
8147   (let ((dep gnus-newsgroup-dependencies)
8148         headers thread last-id)
8149     ;; First go up in this thread until we find the root.
8150     (setq last-id (gnus-root-id id))
8151     (setq headers (list (car (gnus-id-to-thread last-id))
8152                         (caadr (gnus-id-to-thread last-id))))
8153     ;; We have now found the real root of this thread.  It might have
8154     ;; been gathered into some loose thread, so we have to search
8155     ;; through the threads to find the thread we wanted.
8156     (let ((threads gnus-newsgroup-threads)
8157           sub)
8158       (while threads
8159         (setq sub (car threads))
8160         (if (stringp (car sub))
8161             ;; This is a gathered threads, so we look at the roots
8162             ;; below it to find whether this article in in this
8163             ;; gathered root.
8164             (progn
8165               (setq sub (cdr sub))
8166               (while sub
8167                 (when (member (caar sub) headers)
8168                   (setq thread (car threads)
8169                         threads nil
8170                         sub nil))
8171                 (setq sub (cdr sub))))
8172           ;; It's an ordinary thread, so we check it.
8173           (when (eq (car sub) (car headers))
8174             (setq thread sub
8175                   threads nil)))
8176         (setq threads (cdr threads)))
8177       ;; If this article is in no thread, then it's a root.
8178       (if thread
8179           (unless dont-remove
8180             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8181         (setq thread (gnus-gethash last-id dep)))
8182       (when thread
8183         (prog1
8184             thread ; We return this thread.
8185           (unless dont-remove
8186             (if (stringp (car thread))
8187                 (progn
8188                   ;; If we use dummy roots, then we have to remove the
8189                   ;; dummy root as well.
8190                   (when (eq gnus-summary-make-false-root 'dummy)
8191                     ;; Uhm.
8192                     )
8193                   (setq thread (cdr thread))
8194                   (while thread
8195                     (gnus-remove-thread-1 (car thread))
8196                     (setq thread (cdr thread))))
8197               (gnus-remove-thread-1 thread))))))))
8198
8199 (defun gnus-remove-thread-1 (thread)
8200   "Remove the thread THREAD recursively."
8201   (let ((number (mail-header-number (car thread)))
8202         pos)
8203     (when (setq pos (text-property-any
8204                      (point-min) (point-max) 'gnus-number number))
8205       (goto-char pos)
8206       (gnus-delete-line)
8207       (gnus-data-remove number))
8208     (setq thread (cdr thread))
8209     (while thread
8210       (gnus-remove-thread-1 (pop thread)))))
8211
8212 (defun gnus-sort-threads (threads)
8213   "Sort THREADS."
8214   (if (not gnus-thread-sort-functions)
8215       threads
8216     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8217                     (car gnus-thread-sort-functions)
8218                   `(lambda (t1 t2)
8219                      ,(gnus-make-sort-function 
8220                        (reverse gnus-thread-sort-functions))))))
8221       (gnus-message 7 "Sorting threads...")
8222       (prog1
8223           (sort threads func)
8224         (gnus-message 7 "Sorting threads...done")))))
8225
8226 (defun gnus-sort-articles (articles)
8227   "Sort ARTICLES."
8228   (when gnus-article-sort-functions
8229     (let ((func (if (= 1 (length gnus-article-sort-functions))
8230                     (car gnus-article-sort-functions)
8231                   `(lambda (t1 t2)
8232                      ,(gnus-make-sort-function 
8233                        (reverse gnus-article-sort-functions))))))
8234       (gnus-message 7 "Sorting articles...")
8235       (prog1
8236           (setq gnus-newsgroup-headers (sort articles func))
8237         (gnus-message 7 "Sorting articles...done")))))
8238
8239 (defun gnus-make-sort-function (funs)
8240   "Return a composite sort condition based on the functions in FUNC."
8241   (if (cdr funs)
8242       `(or (,(car funs) t1 t2)
8243            (and (not (,(car funs) t2 t1))
8244                 ,(gnus-make-sort-function (cdr funs))))
8245     `(,(car funs) t1 t2)))
8246                  
8247 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8248 (defmacro gnus-thread-header (thread)
8249   ;; Return header of first article in THREAD.
8250   ;; Note that THREAD must never, ever be anything else than a variable -
8251   ;; using some other form will lead to serious barfage.
8252   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8253   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8254   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8255         (vector thread) 2))
8256
8257 (defsubst gnus-article-sort-by-number (h1 h2)
8258   "Sort articles by article number."
8259   (< (mail-header-number h1)
8260      (mail-header-number h2)))
8261
8262 (defun gnus-thread-sort-by-number (h1 h2)
8263   "Sort threads by root article number."
8264   (gnus-article-sort-by-number
8265    (gnus-thread-header h1) (gnus-thread-header h2)))
8266
8267 (defsubst gnus-article-sort-by-author (h1 h2)
8268   "Sort articles by root author."
8269   (string-lessp
8270    (let ((extract (funcall
8271                    gnus-extract-address-components
8272                    (mail-header-from h1))))
8273      (or (car extract) (cdr extract)))
8274    (let ((extract (funcall
8275                    gnus-extract-address-components
8276                    (mail-header-from h2))))
8277      (or (car extract) (cdr extract)))))
8278
8279 (defun gnus-thread-sort-by-author (h1 h2)
8280   "Sort threads by root author."
8281   (gnus-article-sort-by-author
8282    (gnus-thread-header h1)  (gnus-thread-header h2)))
8283
8284 (defsubst gnus-article-sort-by-subject (h1 h2)
8285   "Sort articles by root subject."
8286   (string-lessp
8287    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8288    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8289
8290 (defun gnus-thread-sort-by-subject (h1 h2)
8291   "Sort threads by root subject."
8292   (gnus-article-sort-by-subject
8293    (gnus-thread-header h1) (gnus-thread-header h2)))
8294
8295 (defsubst gnus-article-sort-by-date (h1 h2)
8296   "Sort articles by root article date."
8297   (string-lessp
8298    (inline (gnus-sortable-date (mail-header-date h1)))
8299    (inline (gnus-sortable-date (mail-header-date h2)))))
8300
8301 (defun gnus-thread-sort-by-date (h1 h2)
8302   "Sort threads by root article date."
8303   (gnus-article-sort-by-date
8304    (gnus-thread-header h1) (gnus-thread-header h2)))
8305
8306 (defsubst gnus-article-sort-by-score (h1 h2)
8307   "Sort articles by root article score.
8308 Unscored articles will be counted as having a score of zero."
8309   (> (or (cdr (assq (mail-header-number h1)
8310                     gnus-newsgroup-scored))
8311          gnus-summary-default-score 0)
8312      (or (cdr (assq (mail-header-number h2)
8313                     gnus-newsgroup-scored))
8314          gnus-summary-default-score 0)))
8315
8316 (defun gnus-thread-sort-by-score (h1 h2)
8317   "Sort threads by root article score."
8318   (gnus-article-sort-by-score
8319    (gnus-thread-header h1) (gnus-thread-header h2)))
8320
8321 (defun gnus-thread-sort-by-total-score (h1 h2)
8322   "Sort threads by the sum of all scores in the thread.
8323 Unscored articles will be counted as having a score of zero."
8324   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8325
8326 (defun gnus-thread-total-score (thread)
8327   ;;  This function find the total score of THREAD.
8328   (if (consp thread)
8329       (if (stringp (car thread))
8330           (apply gnus-thread-score-function 0
8331                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8332         (gnus-thread-total-score-1 thread))
8333     (gnus-thread-total-score-1 (list thread))))
8334
8335 (defun gnus-thread-total-score-1 (root)
8336   ;; This function find the total score of the thread below ROOT.
8337   (setq root (car root))
8338   (apply gnus-thread-score-function
8339          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8340              gnus-summary-default-score 0)
8341          (mapcar 'gnus-thread-total-score
8342                  (cdr (gnus-gethash (mail-header-id root)
8343                                     gnus-newsgroup-dependencies)))))
8344
8345 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8346 (defvar gnus-tmp-prev-subject nil)
8347 (defvar gnus-tmp-false-parent nil)
8348 (defvar gnus-tmp-root-expunged nil)
8349 (defvar gnus-tmp-dummy-line nil)
8350
8351 (defun gnus-summary-prepare-threads (threads)
8352   "Prepare summary buffer from THREADS and indentation LEVEL.
8353 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8354 or a straight list of headers."
8355   (gnus-message 7 "Generating summary...")
8356
8357   (setq gnus-newsgroup-threads threads)
8358   (beginning-of-line)
8359
8360   (let ((gnus-tmp-level 0)
8361         (default-score (or gnus-summary-default-score 0))
8362         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8363         thread number subject stack state gnus-tmp-gathered beg-match
8364         new-roots gnus-tmp-new-adopts thread-end
8365         gnus-tmp-header gnus-tmp-unread
8366         gnus-tmp-replied gnus-tmp-subject-or-nil
8367         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8368         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8369         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8370
8371     (setq gnus-tmp-prev-subject nil)
8372
8373     (if (vectorp (car threads))
8374         ;; If this is a straight (sic) list of headers, then a
8375         ;; threaded summary display isn't required, so we just create
8376         ;; an unthreaded one.
8377         (gnus-summary-prepare-unthreaded threads)
8378
8379       ;; Do the threaded display.
8380
8381       (while (or threads stack gnus-tmp-new-adopts new-roots)
8382
8383         (if (and (= gnus-tmp-level 0)
8384                  (not (setq gnus-tmp-dummy-line nil))
8385                  (or (not stack)
8386                      (= (caar stack) 0))
8387                  (not gnus-tmp-false-parent)
8388                  (or gnus-tmp-new-adopts new-roots))
8389             (if gnus-tmp-new-adopts
8390                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8391                       thread (list (car gnus-tmp-new-adopts))
8392                       gnus-tmp-header (caar thread)
8393                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8394               (if new-roots
8395                   (setq thread (list (car new-roots))
8396                         gnus-tmp-header (caar thread)
8397                         new-roots (cdr new-roots))))
8398
8399           (if threads
8400               ;; If there are some threads, we do them before the
8401               ;; threads on the stack.
8402               (setq thread threads
8403                     gnus-tmp-header (caar thread))
8404             ;; There were no current threads, so we pop something off
8405             ;; the stack.
8406             (setq state (car stack)
8407                   gnus-tmp-level (car state)
8408                   thread (cdr state)
8409                   stack (cdr stack)
8410                   gnus-tmp-header (caar thread))))
8411
8412         (setq gnus-tmp-false-parent nil)
8413         (setq gnus-tmp-root-expunged nil)
8414         (setq thread-end nil)
8415
8416         (if (stringp gnus-tmp-header)
8417             ;; The header is a dummy root.
8418             (cond
8419              ((eq gnus-summary-make-false-root 'adopt)
8420               ;; We let the first article adopt the rest.
8421               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8422                                                (cddar thread)))
8423               (setq gnus-tmp-gathered
8424                     (nconc (mapcar
8425                             (lambda (h) (mail-header-number (car h)))
8426                             (cddar thread))
8427                            gnus-tmp-gathered))
8428               (setq thread (cons (list (caar thread)
8429                                        (cadar thread))
8430                                  (cdr thread)))
8431               (setq gnus-tmp-level -1
8432                     gnus-tmp-false-parent t))
8433              ((eq gnus-summary-make-false-root 'empty)
8434               ;; We print adopted articles with empty subject fields.
8435               (setq gnus-tmp-gathered
8436                     (nconc (mapcar
8437                             (lambda (h) (mail-header-number (car h)))
8438                             (cddar thread))
8439                            gnus-tmp-gathered))
8440               (setq gnus-tmp-level -1))
8441              ((eq gnus-summary-make-false-root 'dummy)
8442               ;; We remember that we probably want to output a dummy
8443               ;; root.
8444               (setq gnus-tmp-dummy-line gnus-tmp-header)
8445               (setq gnus-tmp-prev-subject gnus-tmp-header))
8446              (t
8447               ;; We do not make a root for the gathered
8448               ;; sub-threads at all.
8449               (setq gnus-tmp-level -1)))
8450
8451           (setq number (mail-header-number gnus-tmp-header)
8452                 subject (mail-header-subject gnus-tmp-header))
8453
8454           (cond
8455            ;; If the thread has changed subject, we might want to make
8456            ;; this subthread into a root.
8457            ((and (null gnus-thread-ignore-subject)
8458                  (not (zerop gnus-tmp-level))
8459                  gnus-tmp-prev-subject
8460                  (not (inline
8461                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8462             (setq new-roots (nconc new-roots (list (car thread)))
8463                   thread-end t
8464                   gnus-tmp-header nil))
8465            ;; If the article lies outside the current limit,
8466            ;; then we do not display it.
8467            ((and (not (memq number gnus-newsgroup-limit))
8468                  (not gnus-tmp-dummy-line))
8469             (setq gnus-tmp-gathered
8470                   (nconc (mapcar
8471                           (lambda (h) (mail-header-number (car h)))
8472                           (cdar thread))
8473                          gnus-tmp-gathered))
8474             (setq gnus-tmp-new-adopts (if (cdar thread)
8475                                           (append gnus-tmp-new-adopts
8476                                                   (cdar thread))
8477                                         gnus-tmp-new-adopts)
8478                   thread-end t
8479                   gnus-tmp-header nil)
8480             (when (zerop gnus-tmp-level)
8481               (setq gnus-tmp-root-expunged t)))
8482            ;; Perhaps this article is to be marked as read?
8483            ((and gnus-summary-mark-below
8484                  (< (or (cdr (assq number gnus-newsgroup-scored))
8485                         default-score)
8486                     gnus-summary-mark-below)
8487                  ;; Don't touch sparse articles.
8488                  (not (memq number gnus-newsgroup-sparse))
8489                  (not (memq number gnus-newsgroup-ancient)))
8490             (setq gnus-newsgroup-unreads
8491                   (delq number gnus-newsgroup-unreads))
8492             (if gnus-newsgroup-auto-expire
8493                 (push number gnus-newsgroup-expirable)
8494               (push (cons number gnus-low-score-mark)
8495                     gnus-newsgroup-reads))))
8496
8497           (when gnus-tmp-header
8498             ;; We may have an old dummy line to output before this
8499             ;; article.
8500             (when gnus-tmp-dummy-line
8501               (gnus-summary-insert-dummy-line
8502                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8503               (setq gnus-tmp-dummy-line nil))
8504
8505             ;; Compute the mark.
8506             (setq
8507              gnus-tmp-unread
8508              (cond
8509               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8510               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8511               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8512               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8513               (t (or (cdr (assq number gnus-newsgroup-reads))
8514                      gnus-ancient-mark))))
8515
8516             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8517                                   gnus-tmp-header gnus-tmp-level)
8518                   gnus-newsgroup-data)
8519
8520             ;; Actually insert the line.
8521             (setq
8522              gnus-tmp-subject-or-nil
8523              (cond
8524               ((and gnus-thread-ignore-subject
8525                     gnus-tmp-prev-subject
8526                     (not (inline (gnus-subject-equal
8527                                   gnus-tmp-prev-subject subject))))
8528                subject)
8529               ((zerop gnus-tmp-level)
8530                (if (and (eq gnus-summary-make-false-root 'empty)
8531                         (memq number gnus-tmp-gathered)
8532                         gnus-tmp-prev-subject
8533                         (inline (gnus-subject-equal
8534                                  gnus-tmp-prev-subject subject)))
8535                    gnus-summary-same-subject
8536                  subject))
8537               (t gnus-summary-same-subject)))
8538             (if (and (eq gnus-summary-make-false-root 'adopt)
8539                      (= gnus-tmp-level 1)
8540                      (memq number gnus-tmp-gathered))
8541                 (setq gnus-tmp-opening-bracket ?\<
8542                       gnus-tmp-closing-bracket ?\>)
8543               (setq gnus-tmp-opening-bracket ?\[
8544                     gnus-tmp-closing-bracket ?\]))
8545             (setq
8546              gnus-tmp-indentation
8547              (aref gnus-thread-indent-array gnus-tmp-level)
8548              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8549              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8550                                 gnus-summary-default-score 0)
8551              gnus-tmp-score-char
8552              (if (or (null gnus-summary-default-score)
8553                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8554                          gnus-summary-zcore-fuzz)) ? 
8555                (if (< gnus-tmp-score gnus-summary-default-score)
8556                    gnus-score-below-mark gnus-score-over-mark))
8557              gnus-tmp-replied
8558              (cond ((memq number gnus-newsgroup-processable)
8559                     gnus-process-mark)
8560                    ((memq number gnus-newsgroup-cached)
8561                     gnus-cached-mark)
8562                    ((memq number gnus-newsgroup-replied)
8563                     gnus-replied-mark)
8564                    (t gnus-unread-mark))
8565              gnus-tmp-from (mail-header-from gnus-tmp-header)
8566              gnus-tmp-name
8567              (cond
8568               ((string-match "(.+)" gnus-tmp-from)
8569                (substring gnus-tmp-from
8570                           (1+ (match-beginning 0)) (1- (match-end 0))))
8571               ((string-match "<[^>]+> *$" gnus-tmp-from)
8572                (setq beg-match (match-beginning 0))
8573                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8574                         (substring gnus-tmp-from (1+ (match-beginning 0))
8575                                    (1- (match-end 0))))
8576                    (substring gnus-tmp-from 0 beg-match)))
8577               (t gnus-tmp-from)))
8578             (when (string= gnus-tmp-name "")
8579               (setq gnus-tmp-name gnus-tmp-from))
8580             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8581             (gnus-put-text-property
8582              (point)
8583              (progn (eval gnus-summary-line-format-spec) (point))
8584              'gnus-number number)
8585             (when gnus-visual-p
8586               (forward-line -1)
8587               (run-hooks 'gnus-summary-update-hook)
8588               (forward-line 1))
8589
8590             (setq gnus-tmp-prev-subject subject)))
8591
8592         (when (nth 1 thread)
8593           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8594         (incf gnus-tmp-level)
8595         (setq threads (if thread-end nil (cdar thread)))
8596         (unless threads
8597           (setq gnus-tmp-level 0)))))
8598   (gnus-message 7 "Generating summary...done"))
8599
8600 (defun gnus-summary-prepare-unthreaded (headers)
8601   "Generate an unthreaded summary buffer based on HEADERS."
8602   (let (header number mark)
8603
8604     (while headers
8605       ;; We may have to root out some bad articles...
8606       (when (memq (setq number (mail-header-number
8607                                 (setq header (pop headers))))
8608                   gnus-newsgroup-limit)
8609         ;; Mark article as read when it has a low score.
8610         (when (and gnus-summary-mark-below
8611                    (< (or (cdr (assq number gnus-newsgroup-scored))
8612                           gnus-summary-default-score 0)
8613                       gnus-summary-mark-below)
8614                    (not (memq number gnus-newsgroup-ancient)))
8615           (setq gnus-newsgroup-unreads
8616                 (delq number gnus-newsgroup-unreads))
8617           (if gnus-newsgroup-auto-expire
8618               (push number gnus-newsgroup-expirable)
8619             (push (cons number gnus-low-score-mark)
8620                   gnus-newsgroup-reads)))
8621
8622         (setq mark
8623               (cond
8624                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8625                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8626                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8627                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8628                (t (or (cdr (assq number gnus-newsgroup-reads))
8629                       gnus-ancient-mark))))
8630         (setq gnus-newsgroup-data
8631               (cons (gnus-data-make number mark (1+ (point)) header 0)
8632                     gnus-newsgroup-data))
8633         (gnus-summary-insert-line
8634          header 0 nil mark (memq number gnus-newsgroup-replied)
8635          (memq number gnus-newsgroup-expirable)
8636          (mail-header-subject header) nil
8637          (cdr (assq number gnus-newsgroup-scored))
8638          (memq number gnus-newsgroup-processable))))))
8639
8640 (defun gnus-select-newsgroup (group &optional read-all)
8641   "Select newsgroup GROUP.
8642 If READ-ALL is non-nil, all articles in the group are selected."
8643   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8644          (info (nth 2 entry))
8645          articles fetched-articles cached)
8646
8647     (or (gnus-check-server
8648          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8649         (error "Couldn't open server"))
8650
8651     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8652         (gnus-activate-group group)     ; Or we can activate it...
8653         (progn                          ; Or we bug out.
8654           (when (equal major-mode 'gnus-summary-mode)
8655             (kill-buffer (current-buffer)))
8656           (error "Couldn't request group %s: %s"
8657                  group (gnus-status-message group))))
8658
8659     (unless (gnus-request-group group t)
8660       (when (equal major-mode 'gnus-summary-mode)
8661         (kill-buffer (current-buffer)))
8662       (error "Couldn't request group %s: %s"
8663              group (gnus-status-message group)))      
8664
8665     (setq gnus-newsgroup-name group)
8666     (setq gnus-newsgroup-unselected nil)
8667     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8668
8669     (and gnus-asynchronous
8670          (gnus-check-backend-function
8671           'request-asynchronous gnus-newsgroup-name)
8672          (setq gnus-newsgroup-async
8673                (gnus-request-asynchronous gnus-newsgroup-name)))
8674
8675     ;; Adjust and set lists of article marks.
8676     (when info
8677       (gnus-adjust-marked-articles info))
8678
8679     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8680     (when (gnus-virtual-group-p group)
8681       (setq cached gnus-newsgroup-cached))
8682
8683     (setq gnus-newsgroup-unreads
8684           (gnus-set-difference
8685            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8686            gnus-newsgroup-dormant))
8687
8688     (setq gnus-newsgroup-processable nil)
8689
8690     (setq articles (gnus-articles-to-read group read-all))
8691
8692     (cond
8693      ((null articles)
8694       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8695       'quit)
8696      ((eq articles 0) nil)
8697      (t
8698       ;; Init the dependencies hash table.
8699       (setq gnus-newsgroup-dependencies
8700             (gnus-make-hashtable (length articles)))
8701       ;; Retrieve the headers and read them in.
8702       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8703       (setq gnus-newsgroup-headers
8704             (if (eq 'nov
8705                     (setq gnus-headers-retrieved-by
8706                           (gnus-retrieve-headers
8707                            articles gnus-newsgroup-name
8708                            ;; We might want to fetch old headers, but
8709                            ;; not if there is only 1 article.
8710                            (and gnus-fetch-old-headers
8711                                 (or (and
8712                                      (not (eq gnus-fetch-old-headers 'some))
8713                                      (not (numberp gnus-fetch-old-headers)))
8714                                     (> (length articles) 1))))))
8715                 (gnus-get-newsgroup-headers-xover articles)
8716               (gnus-get-newsgroup-headers)))
8717       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8718
8719       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8720       (when cached
8721         (setq gnus-newsgroup-cached cached))
8722
8723       ;; Set the initial limit.
8724       (setq gnus-newsgroup-limit (copy-sequence articles))
8725       ;; Remove canceled articles from the list of unread articles.
8726       (setq gnus-newsgroup-unreads
8727             (gnus-set-sorted-intersection
8728              gnus-newsgroup-unreads
8729              (setq fetched-articles
8730                    (mapcar (lambda (headers) (mail-header-number headers))
8731                            gnus-newsgroup-headers))))
8732       ;; Removed marked articles that do not exist.
8733       (gnus-update-missing-marks
8734        (gnus-sorted-complement fetched-articles articles))
8735       ;; We might want to build some more threads first.
8736       (and gnus-fetch-old-headers
8737            (eq gnus-headers-retrieved-by 'nov)
8738            (gnus-build-old-threads))
8739       ;; Check whether auto-expire is to be done in this group.
8740       (setq gnus-newsgroup-auto-expire
8741             (gnus-group-auto-expirable-p group))
8742       ;; Set up the article buffer now, if necessary.
8743       (unless gnus-single-article-buffer
8744         (gnus-article-setup-buffer))
8745       ;; First and last article in this newsgroup.
8746       (and gnus-newsgroup-headers
8747            (setq gnus-newsgroup-begin
8748                  (mail-header-number (car gnus-newsgroup-headers)))
8749            (setq gnus-newsgroup-end
8750                  (mail-header-number
8751                   (gnus-last-element gnus-newsgroup-headers))))
8752       (setq gnus-reffed-article-number -1)
8753       ;; GROUP is successfully selected.
8754       (or gnus-newsgroup-headers t)))))
8755
8756 (defun gnus-articles-to-read (group read-all)
8757   ;; Find out what articles the user wants to read.
8758   (let* ((articles
8759           ;; Select all articles if `read-all' is non-nil, or if there
8760           ;; are no unread articles.
8761           (if (or read-all
8762                   (and (zerop (length gnus-newsgroup-marked))
8763                        (zerop (length gnus-newsgroup-unreads))))
8764               (gnus-uncompress-range (gnus-active group))
8765             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8766                           (copy-sequence gnus-newsgroup-unreads))
8767                   '<)))
8768          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8769          (scored (length scored-list))
8770          (number (length articles))
8771          (marked (+ (length gnus-newsgroup-marked)
8772                     (length gnus-newsgroup-dormant)))
8773          (select
8774           (cond
8775            ((numberp read-all)
8776             read-all)
8777            (t
8778             (condition-case ()
8779                 (cond
8780                  ((and (or (<= scored marked) (= scored number))
8781                        (numberp gnus-large-newsgroup)
8782                        (> number gnus-large-newsgroup))
8783                   (let ((input
8784                          (read-string
8785                           (format
8786                            "How many articles from %s (default %d): "
8787                            gnus-newsgroup-name number))))
8788                     (if (string-match "^[ \t]*$" input) number input)))
8789                  ((and (> scored marked) (< scored number))
8790                   (let ((input
8791                          (read-string
8792                           (format "%s %s (%d scored, %d total): "
8793                                   "How many articles from"
8794                                   group scored number))))
8795                     (if (string-match "^[ \t]*$" input)
8796                         number input)))
8797                  (t number))
8798               (quit nil))))))
8799     (setq select (if (stringp select) (string-to-number select) select))
8800     (if (or (null select) (zerop select))
8801         select
8802       (if (and (not (zerop scored)) (<= (abs select) scored))
8803           (progn
8804             (setq articles (sort scored-list '<))
8805             (setq number (length articles)))
8806         (setq articles (copy-sequence articles)))
8807
8808       (if (< (abs select) number)
8809           (if (< select 0)
8810               ;; Select the N oldest articles.
8811               (setcdr (nthcdr (1- (abs select)) articles) nil)
8812             ;; Select the N most recent articles.
8813             (setq articles (nthcdr (- number select) articles))))
8814       (setq gnus-newsgroup-unselected
8815             (gnus-sorted-intersection
8816              gnus-newsgroup-unreads
8817              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8818       articles)))
8819
8820 (defun gnus-killed-articles (killed articles)
8821   (let (out)
8822     (while articles
8823       (if (inline (gnus-member-of-range (car articles) killed))
8824           (setq out (cons (car articles) out)))
8825       (setq articles (cdr articles)))
8826     out))
8827
8828 (defun gnus-uncompress-marks (marks)
8829   "Uncompress the mark ranges in MARKS."
8830   (let ((uncompressed '(score bookmark))
8831         out)
8832     (while marks
8833       (if (memq (caar marks) uncompressed)
8834           (push (car marks) out)
8835         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8836       (setq marks (cdr marks)))
8837     out))
8838
8839 (defun gnus-adjust-marked-articles (info)
8840   "Set all article lists and remove all marks that are no longer legal."
8841   (let* ((marked-lists (gnus-info-marks info))
8842          (active (gnus-active (gnus-info-group info)))
8843          (min (car active))
8844          (max (cdr active))
8845          (types gnus-article-mark-lists)
8846          (uncompressed '(score bookmark))
8847          marks var articles article mark)
8848
8849     (while marked-lists
8850       (setq marks (pop marked-lists))
8851       (set (setq var (intern (format "gnus-newsgroup-%s"
8852                                      (car (rassq (setq mark (car marks))
8853                                                  types)))))
8854            (if (memq (car marks) uncompressed) (cdr marks)
8855              (gnus-uncompress-range (cdr marks))))
8856
8857       (setq articles (symbol-value var))
8858
8859       ;; All articles have to be subsets of the active articles.
8860       (cond
8861        ;; Adjust "simple" lists.
8862        ((memq mark '(tick dormant expirable reply killed save))
8863         (while articles
8864           (when (or (< (setq article (pop articles)) min) (> article max))
8865             (set var (delq article (symbol-value var))))))
8866        ;; Adjust assocs.
8867        ((memq mark '(score bookmark))
8868         (while articles
8869           (when (or (< (car (setq article (pop articles))) min)
8870                     (> (car article) max))
8871             (set var (delq article (symbol-value var))))))))))
8872
8873 (defun gnus-update-missing-marks (missing)
8874   "Go through the list of MISSING articles and remove them mark lists."
8875   (when missing
8876     (let ((types gnus-article-mark-lists)
8877           var m)
8878       ;; Go through all types.
8879       (while types
8880         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8881         (when (symbol-value var)
8882           ;; This list has articles.  So we delete all missing articles
8883           ;; from it.
8884           (setq m missing)
8885           (while m
8886             (set var (delq (pop m) (symbol-value var)))))))))
8887
8888 (defun gnus-update-marks ()
8889   "Enter the various lists of marked articles into the newsgroup info list."
8890   (let ((types gnus-article-mark-lists)
8891         (info (gnus-get-info gnus-newsgroup-name))
8892         (uncompressed '(score bookmark killed))
8893         type list newmarked symbol)
8894     (when info
8895       ;; Add all marks lists that are non-nil to the list of marks lists.
8896       (while types
8897         (setq type (pop types))
8898         (when (setq list (symbol-value
8899                           (setq symbol
8900                                 (intern (format "gnus-newsgroup-%s"
8901                                                 (car type))))))
8902           (push (cons (cdr type)
8903                       (if (memq (cdr type) uncompressed) list
8904                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8905                 newmarked)))
8906
8907       ;; Enter these new marks into the info of the group.
8908       (if (nthcdr 3 info)
8909           (setcar (nthcdr 3 info) newmarked)
8910         ;; Add the marks lists to the end of the info.
8911         (when newmarked
8912           (setcdr (nthcdr 2 info) (list newmarked))))
8913
8914       ;; Cut off the end of the info if there's nothing else there.
8915       (let ((i 5))
8916         (while (and (> i 2)
8917                     (not (nth i info)))
8918           (when (nthcdr (decf i) info)
8919             (setcdr (nthcdr i info) nil)))))))
8920
8921 (defun gnus-add-marked-articles (group type articles &optional info force)
8922   ;; Add ARTICLES of TYPE to the info of GROUP.
8923   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8924   ;; add, but replace marked articles of TYPE with ARTICLES.
8925   (let ((info (or info (gnus-get-info group)))
8926         (uncompressed '(score bookmark killed))
8927         marked m)
8928     (or (not info)
8929         (and (not (setq marked (nthcdr 3 info)))
8930              (or (null articles)
8931                  (setcdr (nthcdr 2 info)
8932                          (list (list (cons type (gnus-compress-sequence
8933                                                  articles t)))))))
8934         (and (not (setq m (assq type (car marked))))
8935              (or (null articles)
8936                  (setcar marked
8937                          (cons (cons type (gnus-compress-sequence articles t) )
8938                                (car marked)))))
8939         (if force
8940             (if (null articles)
8941                 (setcar (nthcdr 3 info)
8942                         (delq (assq type (car marked)) (car marked)))
8943               (setcdr m (gnus-compress-sequence articles t)))
8944           (setcdr m (gnus-compress-sequence
8945                      (sort (nconc (gnus-uncompress-range (cdr m))
8946                                   (copy-sequence articles)) '<) t))))))
8947
8948 (defun gnus-set-mode-line (where)
8949   "This function sets the mode line of the article or summary buffers.
8950 If WHERE is `summary', the summary mode line format will be used."
8951   ;; Is this mode line one we keep updated?
8952   (when (memq where gnus-updated-mode-lines)
8953     (let (mode-string)
8954       (save-excursion
8955         ;; We evaluate this in the summary buffer since these
8956         ;; variables are buffer-local to that buffer.
8957         (set-buffer gnus-summary-buffer)
8958         ;; We bind all these variables that are used in the `eval' form
8959         ;; below.
8960         (let* ((mformat (symbol-value
8961                          (intern
8962                           (format "gnus-%s-mode-line-format-spec" where))))
8963                (gnus-tmp-group-name gnus-newsgroup-name)
8964                (gnus-tmp-article-number (or gnus-current-article 0))
8965                (gnus-tmp-unread gnus-newsgroup-unreads)
8966                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8967                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8968                (gnus-tmp-unread-and-unselected
8969                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8970                             (zerop gnus-tmp-unselected)) "")
8971                       ((zerop gnus-tmp-unselected)
8972                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8973                       (t (format "{%d(+%d) more}"
8974                                  gnus-tmp-unread-and-unticked
8975                                  gnus-tmp-unselected))))
8976                (gnus-tmp-subject
8977                 (if (and gnus-current-headers
8978                          (vectorp gnus-current-headers))
8979                     (gnus-mode-string-quote
8980                      (mail-header-subject gnus-current-headers)) ""))
8981                max-len
8982                gnus-tmp-header);; passed as argument to any user-format-funcs
8983           (setq mode-string (eval mformat))
8984           (setq max-len (max 4 (if gnus-mode-non-string-length
8985                                    (- (window-width)
8986                                       gnus-mode-non-string-length)
8987                                  (length mode-string))))
8988           ;; We might have to chop a bit of the string off...
8989           (when (> (length mode-string) max-len)
8990             (setq mode-string
8991                   (concat (gnus-truncate-string mode-string (- max-len 3))
8992                           "...")))
8993           ;; Pad the mode string a bit.
8994           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8995       ;; Update the mode line.
8996       (setq mode-line-buffer-identification (list mode-string))
8997       (set-buffer-modified-p t))))
8998
8999 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9000   "Go through the HEADERS list and add all Xrefs to a hash table.
9001 The resulting hash table is returned, or nil if no Xrefs were found."
9002   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9003          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9004          (xref-hashtb (make-vector 63 0))
9005          start group entry number xrefs header)
9006     (while headers
9007       (setq header (pop headers))
9008       (when (and (setq xrefs (mail-header-xref header))
9009                  (not (memq (setq number (mail-header-number header))
9010                             unreads)))
9011         (setq start 0)
9012         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9013           (setq start (match-end 0))
9014           (setq group (if prefix
9015                           (concat prefix (substring xrefs (match-beginning 1)
9016                                                     (match-end 1)))
9017                         (substring xrefs (match-beginning 1) (match-end 1))))
9018           (setq number
9019                 (string-to-int (substring xrefs (match-beginning 2)
9020                                           (match-end 2))))
9021           (if (setq entry (gnus-gethash group xref-hashtb))
9022               (setcdr entry (cons number (cdr entry)))
9023             (gnus-sethash group (cons number nil) xref-hashtb)))))
9024     (and start xref-hashtb)))
9025
9026 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9027   "Look through all the headers and mark the Xrefs as read."
9028   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9029         name entry info xref-hashtb idlist method nth4)
9030     (save-excursion
9031       (set-buffer gnus-group-buffer)
9032       (when (setq xref-hashtb
9033                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9034         (mapatoms
9035          (lambda (group)
9036            (unless (string= from-newsgroup (setq name (symbol-name group)))
9037              (setq idlist (symbol-value group))
9038              ;; Dead groups are not updated.
9039              (and (prog1
9040                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9041                             info (nth 2 entry))
9042                     (if (stringp (setq nth4 (gnus-info-method info)))
9043                         (setq nth4 (gnus-server-to-method nth4))))
9044                   ;; Only do the xrefs if the group has the same
9045                   ;; select method as the group we have just read.
9046                   (or (gnus-methods-equal-p
9047                        nth4 (gnus-find-method-for-group from-newsgroup))
9048                       virtual
9049                       (equal nth4 (setq method (gnus-find-method-for-group
9050                                                 from-newsgroup)))
9051                       (and (equal (car nth4) (car method))
9052                            (equal (nth 1 nth4) (nth 1 method))))
9053                   gnus-use-cross-reference
9054                   (or (not (eq gnus-use-cross-reference t))
9055                       virtual
9056                       ;; Only do cross-references on subscribed
9057                       ;; groups, if that is what is wanted.
9058                       (<= (gnus-info-level info) gnus-level-subscribed))
9059                   (gnus-group-make-articles-read name idlist))))
9060          xref-hashtb)))))
9061
9062 (defun gnus-group-make-articles-read (group articles)
9063   (let* ((num 0)
9064          (entry (gnus-gethash group gnus-newsrc-hashtb))
9065          (info (nth 2 entry))
9066          (active (gnus-active group))
9067          range)
9068     ;; First peel off all illegal article numbers.
9069     (if active
9070         (let ((ids articles)
9071               id first)
9072           (while ids
9073             (setq id (car ids))
9074             (if (and first (> id (cdr active)))
9075                 (progn
9076                   ;; We'll end up in this situation in one particular
9077                   ;; obscure situation.  If you re-scan a group and get
9078                   ;; a new article that is cross-posted to a different
9079                   ;; group that has not been re-scanned, you might get
9080                   ;; crossposted article that has a higher number than
9081                   ;; Gnus believes possible.  So we re-activate this
9082                   ;; group as well.  This might mean doing the
9083                   ;; crossposting thingy will *increase* the number
9084                   ;; of articles in some groups.  Tsk, tsk.
9085                   (setq active (or (gnus-activate-group group) active))))
9086             (if (or (> id (cdr active))
9087                     (< id (car active)))
9088                 (setq articles (delq id articles)))
9089             (setq ids (cdr ids)))))
9090     ;; If the read list is nil, we init it.
9091     (and active
9092          (null (gnus-info-read info))
9093          (> (car active) 1)
9094          (gnus-info-set-read info (cons 1 (1- (car active)))))
9095     ;; Then we add the read articles to the range.
9096     (gnus-info-set-read
9097      info
9098      (setq range
9099            (gnus-add-to-range
9100             (gnus-info-read info) (setq articles (sort articles '<)))))
9101     ;; Then we have to re-compute how many unread
9102     ;; articles there are in this group.
9103     (if active
9104         (progn
9105           (cond
9106            ((not range)
9107             (setq num (- (1+ (cdr active)) (car active))))
9108            ((not (listp (cdr range)))
9109             (setq num (- (cdr active) (- (1+ (cdr range))
9110                                          (car range)))))
9111            (t
9112             (while range
9113               (if (numberp (car range))
9114                   (setq num (1+ num))
9115                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9116               (setq range (cdr range)))
9117             (setq num (- (cdr active) num))))
9118           ;; Update the number of unread articles.
9119           (setcar entry num)
9120           ;; Update the group buffer.
9121           (gnus-group-update-group group t)))))
9122
9123 (defun gnus-methods-equal-p (m1 m2)
9124   (let ((m1 (or m1 gnus-select-method))
9125         (m2 (or m2 gnus-select-method)))
9126     (or (equal m1 m2)
9127         (and (eq (car m1) (car m2))
9128              (or (not (memq 'address (assoc (symbol-name (car m1))
9129                                             gnus-valid-select-methods)))
9130                  (equal (nth 1 m1) (nth 1 m2)))))))
9131
9132 (defsubst gnus-header-value ()
9133   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9134
9135 (defvar gnus-newsgroup-none-id 0)
9136
9137 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9138   (let ((cur nntp-server-buffer)
9139         (dependencies
9140          (or dependencies
9141              (save-excursion (set-buffer gnus-summary-buffer)
9142                              gnus-newsgroup-dependencies)))
9143         headers id id-dep ref-dep end ref)
9144     (save-excursion
9145       (set-buffer nntp-server-buffer)
9146       (run-hooks 'gnus-parse-headers-hook)
9147       (let ((case-fold-search t)
9148             in-reply-to header p lines)
9149         (goto-char (point-min))
9150         ;; Search to the beginning of the next header.  Error messages
9151         ;; do not begin with 2 or 3.
9152         (while (re-search-forward "^[23][0-9]+ " nil t)
9153           (setq id nil
9154                 ref nil)
9155           ;; This implementation of this function, with nine
9156           ;; search-forwards instead of the one re-search-forward and
9157           ;; a case (which basically was the old function) is actually
9158           ;; about twice as fast, even though it looks messier.  You
9159           ;; can't have everything, I guess.  Speed and elegance
9160           ;; doesn't always go hand in hand.
9161           (setq
9162            header
9163            (vector
9164             ;; Number.
9165             (prog1
9166                 (read cur)
9167               (end-of-line)
9168               (setq p (point))
9169               (narrow-to-region (point)
9170                                 (or (and (search-forward "\n.\n" nil t)
9171                                          (- (point) 2))
9172                                     (point))))
9173             ;; Subject.
9174             (progn
9175               (goto-char p)
9176               (if (search-forward "\nsubject: " nil t)
9177                   (gnus-header-value) "(none)"))
9178             ;; From.
9179             (progn
9180               (goto-char p)
9181               (if (search-forward "\nfrom: " nil t)
9182                   (gnus-header-value) "(nobody)"))
9183             ;; Date.
9184             (progn
9185               (goto-char p)
9186               (if (search-forward "\ndate: " nil t)
9187                   (gnus-header-value) ""))
9188             ;; Message-ID.
9189             (progn
9190               (goto-char p)
9191               (if (search-forward "\nmessage-id: " nil t)
9192                   (setq id (gnus-header-value))
9193                 ;; If there was no message-id, we just fake one to make
9194                 ;; subsequent routines simpler.
9195                 (setq id (concat "none+"
9196                                  (int-to-string
9197                                   (setq gnus-newsgroup-none-id
9198                                         (1+ gnus-newsgroup-none-id)))))))
9199             ;; References.
9200             (progn
9201               (goto-char p)
9202               (if (search-forward "\nreferences: " nil t)
9203                   (prog1
9204                       (gnus-header-value)
9205                     (setq end (match-end 0))
9206                     (save-excursion
9207                       (setq ref
9208                             (buffer-substring
9209                              (progn
9210                                (end-of-line)
9211                                (search-backward ">" end t)
9212                                (1+ (point)))
9213                              (progn
9214                                (search-backward "<" end t)
9215                                (point))))))
9216                 ;; Get the references from the in-reply-to header if there
9217                 ;; were no references and the in-reply-to header looks
9218                 ;; promising.
9219                 (if (and (search-forward "\nin-reply-to: " nil t)
9220                          (setq in-reply-to (gnus-header-value))
9221                          (string-match "<[^>]+>" in-reply-to))
9222                     (setq ref (substring in-reply-to (match-beginning 0)
9223                                          (match-end 0)))
9224                   (setq ref ""))))
9225             ;; Chars.
9226             0
9227             ;; Lines.
9228             (progn
9229               (goto-char p)
9230               (if (search-forward "\nlines: " nil t)
9231                   (if (numberp (setq lines (read cur)))
9232                       lines 0)
9233                 0))
9234             ;; Xref.
9235             (progn
9236               (goto-char p)
9237               (and (search-forward "\nxref: " nil t)
9238                    (gnus-header-value)))))
9239           ;; We do the threading while we read the headers.  The
9240           ;; message-id and the last reference are both entered into
9241           ;; the same hash table.  Some tippy-toeing around has to be
9242           ;; done in case an article has arrived before the article
9243           ;; which it refers to.
9244           (if (boundp (setq id-dep (intern id dependencies)))
9245               (if (and (car (symbol-value id-dep))
9246                        (not force-new))
9247                   ;; An article with this Message-ID has already
9248                   ;; been seen, so we ignore this one, except we add
9249                   ;; any additional Xrefs (in case the two articles
9250                   ;; came from different servers).
9251                   (progn
9252                     (mail-header-set-xref
9253                      (car (symbol-value id-dep))
9254                      (concat (or (mail-header-xref
9255                                   (car (symbol-value id-dep))) "")
9256                              (or (mail-header-xref header) "")))
9257                     (setq header nil))
9258                 (setcar (symbol-value id-dep) header))
9259             (set id-dep (list header)))
9260           (when header
9261             (if (boundp (setq ref-dep (intern ref dependencies)))
9262                 (setcdr (symbol-value ref-dep)
9263                         (nconc (cdr (symbol-value ref-dep))
9264                                (list (symbol-value id-dep))))
9265               (set ref-dep (list nil (symbol-value id-dep))))
9266             (setq headers (cons header headers)))
9267           (goto-char (point-max))
9268           (widen))
9269         (nreverse headers)))))
9270
9271 ;; The following macros and functions were written by Felix Lee
9272 ;; <flee@cse.psu.edu>.
9273
9274 (defmacro gnus-nov-read-integer ()
9275   '(prog1
9276        (if (= (following-char) ?\t)
9277            0
9278          (let ((num (condition-case nil (read buffer) (error nil))))
9279            (if (numberp num) num 0)))
9280      (or (eobp) (forward-char 1))))
9281
9282 (defmacro gnus-nov-skip-field ()
9283   '(search-forward "\t" eol 'move))
9284
9285 (defmacro gnus-nov-field ()
9286   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9287
9288 ;; Goes through the xover lines and returns a list of vectors
9289 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9290                                                   force-new dependencies)
9291   "Parse the news overview data in the server buffer, and return a
9292 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9293   ;; Get the Xref when the users reads the articles since most/some
9294   ;; NNTP servers do not include Xrefs when using XOVER.
9295   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9296   (let ((cur nntp-server-buffer)
9297         (dependencies (or dependencies gnus-newsgroup-dependencies))
9298         number headers header)
9299     (save-excursion
9300       (set-buffer nntp-server-buffer)
9301       ;; Allow the user to mangle the headers before parsing them.
9302       (run-hooks 'gnus-parse-headers-hook)
9303       (goto-char (point-min))
9304       (while (and sequence (not (eobp)))
9305         (setq number (read cur))
9306         (while (and sequence (< (car sequence) number))
9307           (setq sequence (cdr sequence)))
9308         (and sequence
9309              (eq number (car sequence))
9310              (progn
9311                (setq sequence (cdr sequence))
9312                (if (setq header
9313                          (inline (gnus-nov-parse-line
9314                                   number dependencies force-new)))
9315                    (setq headers (cons header headers)))))
9316         (forward-line 1))
9317       (setq headers (nreverse headers)))
9318     headers))
9319
9320 ;; This function has to be called with point after the article number
9321 ;; on the beginning of the line.
9322 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9323   (let ((none 0)
9324         (eol (gnus-point-at-eol))
9325         (buffer (current-buffer))
9326         header ref id id-dep ref-dep)
9327
9328     ;; overview: [num subject from date id refs chars lines misc]
9329     (narrow-to-region (point) eol)
9330     (or (eobp) (forward-char))
9331
9332     (condition-case nil
9333         (setq header
9334               (vector
9335                number                   ; number
9336                (gnus-nov-field)         ; subject
9337                (gnus-nov-field)         ; from
9338                (gnus-nov-field)         ; date
9339                (setq id (or (gnus-nov-field)
9340                             (concat "none+"
9341                                     (int-to-string
9342                                      (setq none (1+ none)))))) ; id
9343                (progn
9344                  (save-excursion
9345                    (let ((beg (point)))
9346                      (search-forward "\t" eol)
9347                      (if (search-backward ">" beg t)
9348                          (setq ref
9349                                (buffer-substring
9350                                 (1+ (point))
9351                                 (search-backward "<" beg t)))
9352                        (setq ref nil))))
9353                  (gnus-nov-field))      ; refs
9354                (gnus-nov-read-integer)  ; chars
9355                (gnus-nov-read-integer)  ; lines
9356                (if (= (following-char) ?\n)
9357                    nil
9358                  (gnus-nov-field))      ; misc
9359                ))
9360       (error (progn
9361                (gnus-error 4 "Strange nov line")
9362                (setq header nil)
9363                (goto-char eol))))
9364
9365     (widen)
9366
9367     ;; We build the thread tree.
9368     (when header
9369       (if (boundp (setq id-dep (intern id dependencies)))
9370           (if (and (car (symbol-value id-dep))
9371                    (not force-new))
9372               ;; An article with this Message-ID has already been seen,
9373               ;; so we ignore this one, except we add any additional
9374               ;; Xrefs (in case the two articles came from different
9375               ;; servers.
9376               (progn
9377                 (mail-header-set-xref
9378                  (car (symbol-value id-dep))
9379                  (concat (or (mail-header-xref
9380                               (car (symbol-value id-dep))) "")
9381                          (or (mail-header-xref header) "")))
9382                 (setq header nil))
9383             (setcar (symbol-value id-dep) header))
9384         (set id-dep (list header))))
9385     (if header
9386         (progn
9387           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9388               (setcdr (symbol-value ref-dep)
9389                       (nconc (cdr (symbol-value ref-dep))
9390                              (list (symbol-value id-dep))))
9391             (set ref-dep (list nil (symbol-value id-dep))))))
9392     header))
9393
9394 (defun gnus-article-get-xrefs ()
9395   "Fill in the Xref value in `gnus-current-headers', if necessary.
9396 This is meant to be called in `gnus-article-internal-prepare-hook'."
9397   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9398                                  gnus-current-headers)))
9399     (or (not gnus-use-cross-reference)
9400         (not headers)
9401         (and (mail-header-xref headers)
9402              (not (string= (mail-header-xref headers) "")))
9403         (let ((case-fold-search t)
9404               xref)
9405           (save-restriction
9406             (nnheader-narrow-to-headers)
9407             (goto-char (point-min))
9408             (if (or (and (eq (downcase (following-char)) ?x)
9409                          (looking-at "Xref:"))
9410                     (search-forward "\nXref:" nil t))
9411                 (progn
9412                   (goto-char (1+ (match-end 0)))
9413                   (setq xref (buffer-substring (point)
9414                                                (progn (end-of-line) (point))))
9415                   (mail-header-set-xref headers xref))))))))
9416
9417 (defun gnus-summary-insert-subject (id &optional old-header)
9418   "Find article ID and insert the summary line for that article."
9419   (let ((header (or old-header (gnus-read-header id)))
9420         (number (and (numberp id) id))
9421         pos)
9422     (when header
9423       ;; Rebuild the thread that this article is part of and go to the
9424       ;; article we have fetched.
9425       (when old-header
9426         (when (setq pos (text-property-any
9427                          (point-min) (point-max) 'gnus-number 
9428                          (mail-header-number old-header)))
9429           (goto-char pos)
9430           (gnus-delete-line)
9431           (gnus-data-remove (mail-header-number old-header))))
9432       (when old-header
9433         (mail-header-set-number header (mail-header-number old-header)))
9434       (setq gnus-newsgroup-sparse
9435             (delq (setq number (mail-header-number header)) 
9436                   gnus-newsgroup-sparse))
9437       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9438       (gnus-rebuild-thread (mail-header-id header))
9439       (gnus-summary-goto-subject number nil t))
9440     (when (and (numberp number)
9441                (> number 0))
9442       ;; We have to update the boundaries even if we can't fetch the
9443       ;; article if ID is a number -- so that the next `P' or `N'
9444       ;; command will fetch the previous (or next) article even
9445       ;; if the one we tried to fetch this time has been canceled.
9446       (and (> number gnus-newsgroup-end)
9447            (setq gnus-newsgroup-end number))
9448       (and (< number gnus-newsgroup-begin)
9449            (setq gnus-newsgroup-begin number))
9450       (setq gnus-newsgroup-unselected
9451             (delq number gnus-newsgroup-unselected)))
9452     ;; Report back a success?
9453     (and header (mail-header-number header))))
9454
9455 (defun gnus-summary-work-articles (n)
9456   "Return a list of articles to be worked upon.  The prefix argument,
9457 the list of process marked articles, and the current article will be
9458 taken into consideration."
9459   (cond
9460    ((and n (numberp n))
9461     ;; A numerical prefix has been given.
9462     (let ((backward (< n 0))
9463           (n (abs n))
9464           articles article)
9465       (save-excursion
9466         (while
9467             (and (> n 0)
9468                  (push (setq article (gnus-summary-article-number))
9469                        articles)
9470                  (if backward
9471                      (gnus-summary-find-prev nil article)
9472                    (gnus-summary-find-next nil article)))
9473           (decf n)))
9474       (nreverse articles)))
9475    ((and (boundp 'transient-mark-mode)
9476          transient-mark-mode
9477          mark-active)
9478     ;; Work on the region between point and mark.
9479     (let ((max (max (point) (mark)))
9480           articles article)
9481       (save-excursion
9482         (goto-char (min (point) (mark)))
9483         (while
9484             (and
9485              (push (setq article (gnus-summary-article-number)) articles)
9486              (gnus-summary-find-next nil article)
9487              (< (point) max)))
9488         (nreverse articles))))
9489    (gnus-newsgroup-processable
9490     ;; There are process-marked articles present.
9491     (reverse gnus-newsgroup-processable))
9492    (t
9493     ;; Just return the current article.
9494     (list (gnus-summary-article-number)))))
9495
9496 (defun gnus-summary-search-group (&optional backward use-level)
9497   "Search for next unread newsgroup.
9498 If optional argument BACKWARD is non-nil, search backward instead."
9499   (save-excursion
9500     (set-buffer gnus-group-buffer)
9501     (if (gnus-group-search-forward
9502          backward nil (if use-level (gnus-group-group-level) nil))
9503         (gnus-group-group-name))))
9504
9505 (defun gnus-summary-best-group (&optional exclude-group)
9506   "Find the name of the best unread group.
9507 If EXCLUDE-GROUP, do not go to this group."
9508   (save-excursion
9509     (set-buffer gnus-group-buffer)
9510     (save-excursion
9511       (gnus-group-best-unread-group exclude-group))))
9512
9513 (defun gnus-summary-find-next (&optional unread article backward)
9514   (if backward (gnus-summary-find-prev)
9515     (let* ((dummy (gnus-summary-article-intangible-p))
9516            (article (or article (gnus-summary-article-number)))
9517            (arts (gnus-data-find-list article))
9518            result)
9519       (when (and (not dummy)
9520                  (or (not gnus-summary-check-current)
9521                      (not unread)
9522                      (not (gnus-data-unread-p (car arts)))))
9523         (setq arts (cdr arts)))
9524       (when (setq result
9525                   (if unread
9526                       (progn
9527                         (while arts
9528                           (when (gnus-data-unread-p (car arts))
9529                             (setq result (car arts)
9530                                   arts nil))
9531                           (setq arts (cdr arts)))
9532                         result)
9533                     (car arts)))
9534         (goto-char (gnus-data-pos result))
9535         (gnus-data-number result)))))
9536
9537 (defun gnus-summary-find-prev (&optional unread article)
9538   (let* ((eobp (eobp))
9539          (article (or article (gnus-summary-article-number)))
9540          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9541          result)
9542     (when (and (not eobp)
9543                (or (not gnus-summary-check-current)
9544                    (not unread)
9545                    (not (gnus-data-unread-p (car arts)))))
9546       (setq arts (cdr arts)))
9547     (if (setq result
9548               (if unread
9549                   (progn
9550                     (while arts
9551                       (and (gnus-data-unread-p (car arts))
9552                            (setq result (car arts)
9553                                  arts nil))
9554                       (setq arts (cdr arts)))
9555                     result)
9556                 (car arts)))
9557         (progn
9558           (goto-char (gnus-data-pos result))
9559           (gnus-data-number result)))))
9560
9561 (defun gnus-summary-find-subject (subject &optional unread backward article)
9562   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9563          (article (or article (gnus-summary-article-number)))
9564          (articles (gnus-data-list backward))
9565          (arts (gnus-data-find-list article articles))
9566          result)
9567     (when (or (not gnus-summary-check-current)
9568               (not unread)
9569               (not (gnus-data-unread-p (car arts))))
9570       (setq arts (cdr arts)))
9571     (while arts
9572       (and (or (not unread)
9573                (gnus-data-unread-p (car arts)))
9574            (vectorp (gnus-data-header (car arts)))
9575            (gnus-subject-equal
9576             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9577            (setq result (car arts)
9578                  arts nil))
9579       (setq arts (cdr arts)))
9580     (and result
9581          (goto-char (gnus-data-pos result))
9582          (gnus-data-number result))))
9583
9584 (defun gnus-summary-search-forward (&optional unread subject backward)
9585   "Search forward for an article.
9586 If UNREAD, look for unread articles.  If SUBJECT, look for
9587 articles with that subject.  If BACKWARD, search backward instead."
9588   (cond (subject (gnus-summary-find-subject subject unread backward))
9589         (backward (gnus-summary-find-prev unread))
9590         (t (gnus-summary-find-next unread))))
9591
9592 (defun gnus-recenter (&optional n)
9593   "Center point in window and redisplay frame.
9594 Also do horizontal recentering."
9595   (interactive "P")
9596   (when (and gnus-auto-center-summary
9597              (not (eq gnus-auto-center-summary 'vertical)))
9598     (gnus-horizontal-recenter))
9599   (recenter n))
9600
9601 (defun gnus-summary-recenter ()
9602   "Center point in the summary window.
9603 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9604 displayed, no centering will be performed."
9605   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9606   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9607   (let* ((top (cond ((< (window-height) 4) 0)
9608                     ((< (window-height) 7) 1)
9609                     (t 2)))
9610          (height (1- (window-height)))
9611          (bottom (save-excursion (goto-char (point-max))
9612                                  (forward-line (- height))
9613                                  (point)))
9614          (window (get-buffer-window (current-buffer))))
9615     ;; The user has to want it.
9616     (when gnus-auto-center-summary
9617       (when (get-buffer-window gnus-article-buffer)
9618        ;; Only do recentering when the article buffer is displayed,
9619        ;; Set the window start to either `bottom', which is the biggest
9620        ;; possible valid number, or the second line from the top,
9621        ;; whichever is the least.
9622        (set-window-start
9623         window (min bottom (save-excursion 
9624                              (forward-line (- top)) (point)))))
9625       ;; Do horizontal recentering while we're at it.
9626       (when (and (get-buffer-window (current-buffer) t)
9627                  (not (eq gnus-auto-center-summary 'vertical)))
9628         (let ((selected (selected-window)))
9629           (select-window (get-buffer-window (current-buffer) t))
9630           (gnus-summary-position-point)
9631           (gnus-horizontal-recenter)
9632           (select-window selected))))))
9633
9634 (defun gnus-horizontal-recenter ()
9635   "Recenter the current buffer horizontally."
9636   (if (< (current-column) (/ (window-width) 2))
9637       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9638     (let* ((orig (point))
9639            (end (window-end (get-buffer-window (current-buffer) t)))
9640            (max 0))
9641       ;; Find the longest line currently displayed in the window.
9642       (goto-char (window-start))
9643       (while (and (not (eobp)) 
9644                   (< (point) end))
9645         (end-of-line)
9646         (setq max (max max (current-column)))
9647         (forward-line 1))
9648       (goto-char orig)
9649       ;; Scroll horizontally to center (sort of) the point.
9650       (if (> max (window-width))
9651           (set-window-hscroll 
9652            (get-buffer-window (current-buffer) t)
9653            (min (- (current-column) (/ (window-width) 3))
9654                 (+ 2 (- max (window-width)))))
9655         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9656       max)))
9657
9658 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9659 (defun gnus-short-group-name (group &optional levels)
9660   "Collapse GROUP name LEVELS."
9661   (let* ((name "") 
9662          (foreign "")
9663          (depth 0) 
9664          (skip 1)
9665          (levels (or levels
9666                      (progn
9667                        (while (string-match "\\." group skip)
9668                          (setq skip (match-end 0)
9669                                depth (+ depth 1)))
9670                        depth))))
9671     (if (string-match ":" group)
9672         (setq foreign (substring group 0 (match-end 0))
9673               group (substring group (match-end 0))))
9674     (while group
9675       (if (and (string-match "\\." group)
9676                (> levels (- gnus-group-uncollapsed-levels 1)))
9677           (setq name (concat name (substring group 0 1))
9678                 group (substring group (match-end 0))
9679                 levels (- levels 1)
9680                 name (concat name "."))
9681         (setq name (concat foreign name group)
9682               group nil)))
9683     name))
9684
9685 (defun gnus-summary-jump-to-group (newsgroup)
9686   "Move point to NEWSGROUP in group mode buffer."
9687   ;; Keep update point of group mode buffer if visible.
9688   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9689       (save-window-excursion
9690         ;; Take care of tree window mode.
9691         (if (get-buffer-window gnus-group-buffer)
9692             (pop-to-buffer gnus-group-buffer))
9693         (gnus-group-jump-to-group newsgroup))
9694     (save-excursion
9695       ;; Take care of tree window mode.
9696       (if (get-buffer-window gnus-group-buffer)
9697           (pop-to-buffer gnus-group-buffer)
9698         (set-buffer gnus-group-buffer))
9699       (gnus-group-jump-to-group newsgroup))))
9700
9701 ;; This function returns a list of article numbers based on the
9702 ;; difference between the ranges of read articles in this group and
9703 ;; the range of active articles.
9704 (defun gnus-list-of-unread-articles (group)
9705   (let* ((read (gnus-info-read (gnus-get-info group)))
9706          (active (gnus-active group))
9707          (last (cdr active))
9708          first nlast unread)
9709     ;; If none are read, then all are unread.
9710     (if (not read)
9711         (setq first (car active))
9712       ;; If the range of read articles is a single range, then the
9713       ;; first unread article is the article after the last read
9714       ;; article.  Sounds logical, doesn't it?
9715       (if (not (listp (cdr read)))
9716           (setq first (1+ (cdr read)))
9717         ;; `read' is a list of ranges.
9718         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9719                                 (caar read))) 1)
9720             (setq first 1))
9721         (while read
9722           (if first
9723               (while (< first nlast)
9724                 (setq unread (cons first unread))
9725                 (setq first (1+ first))))
9726           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9727           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9728           (setq read (cdr read)))))
9729     ;; And add the last unread articles.
9730     (while (<= first last)
9731       (setq unread (cons first unread))
9732       (setq first (1+ first)))
9733     ;; Return the list of unread articles.
9734     (nreverse unread)))
9735
9736 (defun gnus-list-of-read-articles (group)
9737   "Return a list of unread, unticked and non-dormant articles."
9738   (let* ((info (gnus-get-info group))
9739          (marked (gnus-info-marks info))
9740          (active (gnus-active group)))
9741     (and info active
9742          (gnus-set-difference
9743           (gnus-sorted-complement
9744            (gnus-uncompress-range active)
9745            (gnus-list-of-unread-articles group))
9746           (append
9747            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9748            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9749
9750 ;; Various summary commands
9751
9752 (defun gnus-summary-universal-argument (arg)
9753   "Perform any operation on all articles that are process/prefixed."
9754   (interactive "P")
9755   (gnus-set-global-variables)
9756   (let ((articles (gnus-summary-work-articles arg))
9757         func article)
9758     (if (eq
9759          (setq
9760           func
9761           (key-binding
9762            (read-key-sequence
9763             (substitute-command-keys
9764              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9765              ))))
9766          'undefined)
9767         (gnus-error 1 "Undefined key")
9768       (save-excursion
9769         (while articles
9770           (gnus-summary-goto-subject (setq article (pop articles)))
9771           (command-execute func)
9772           (gnus-summary-remove-process-mark article)))))
9773   (gnus-summary-position-point))
9774
9775 (defun gnus-summary-toggle-truncation (&optional arg)
9776   "Toggle truncation of summary lines.
9777 With arg, turn line truncation on iff arg is positive."
9778   (interactive "P")
9779   (setq truncate-lines
9780         (if (null arg) (not truncate-lines)
9781           (> (prefix-numeric-value arg) 0)))
9782   (redraw-display))
9783
9784 (defun gnus-summary-reselect-current-group (&optional all rescan)
9785   "Exit and then reselect the current newsgroup.
9786 The prefix argument ALL means to select all articles."
9787   (interactive "P")
9788   (gnus-set-global-variables)
9789   (let ((current-subject (gnus-summary-article-number))
9790         (group gnus-newsgroup-name))
9791     (setq gnus-newsgroup-begin nil)
9792     (gnus-summary-exit)
9793     ;; We have to adjust the point of group mode buffer because the
9794     ;; current point was moved to the next unread newsgroup by
9795     ;; exiting.
9796     (gnus-summary-jump-to-group group)
9797     (when rescan
9798       (save-excursion
9799         (gnus-group-get-new-news-this-group 1)))
9800     (gnus-group-read-group all t)
9801     (gnus-summary-goto-subject current-subject)))
9802
9803 (defun gnus-summary-rescan-group (&optional all)
9804   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9805   (interactive "P")
9806   (gnus-summary-reselect-current-group all t))
9807
9808 (defun gnus-summary-update-info ()
9809   (let* ((group gnus-newsgroup-name))
9810     (when gnus-newsgroup-kill-headers
9811       (setq gnus-newsgroup-killed
9812             (gnus-compress-sequence
9813              (nconc
9814               (gnus-set-sorted-intersection
9815                (gnus-uncompress-range gnus-newsgroup-killed)
9816                (setq gnus-newsgroup-unselected
9817                      (sort gnus-newsgroup-unselected '<)))
9818               (setq gnus-newsgroup-unreads
9819                     (sort gnus-newsgroup-unreads '<))) t)))
9820     (unless (listp (cdr gnus-newsgroup-killed))
9821       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9822     (let ((headers gnus-newsgroup-headers))
9823       (run-hooks 'gnus-exit-group-hook)
9824       (unless gnus-save-score
9825         (setq gnus-newsgroup-scored nil))
9826       ;; Set the new ranges of read articles.
9827       (gnus-update-read-articles
9828        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9829       ;; Set the current article marks.
9830       (gnus-update-marks)
9831       ;; Do the cross-ref thing.
9832       (when gnus-use-cross-reference
9833         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9834       ;; Do adaptive scoring, and possibly save score files.
9835       (when gnus-newsgroup-adaptive
9836         (gnus-score-adaptive))
9837       (when gnus-use-scoring
9838         (gnus-score-save))
9839       ;; Do not switch windows but change the buffer to work.
9840       (set-buffer gnus-group-buffer)
9841       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9842           (gnus-group-update-group group)))))
9843
9844 (defun gnus-summary-exit (&optional temporary)
9845   "Exit reading current newsgroup, and then return to group selection mode.
9846 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9847   (interactive)
9848   (gnus-set-global-variables)
9849   (gnus-kill-save-kill-buffer)
9850   (let* ((group gnus-newsgroup-name)
9851          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9852          (mode major-mode)
9853          (buf (current-buffer)))
9854     (run-hooks 'gnus-summary-prepare-exit-hook)
9855     ;; If we have several article buffers, we kill them at exit.
9856     (unless gnus-single-article-buffer
9857       (gnus-kill-buffer gnus-original-article-buffer)
9858       (setq gnus-article-current nil))
9859     (when gnus-use-cache
9860       (gnus-cache-possibly-remove-articles)
9861       (gnus-cache-save-buffers))
9862     (when gnus-use-trees
9863       (gnus-tree-close group))
9864     ;; Make all changes in this group permanent.
9865     (unless quit-config
9866       (gnus-summary-update-info))
9867     (gnus-close-group group)
9868     ;; Make sure where I was, and go to next newsgroup.
9869     (set-buffer gnus-group-buffer)
9870     (unless quit-config
9871       (gnus-group-jump-to-group group))
9872     (run-hooks 'gnus-summary-exit-hook)
9873     (unless quit-config
9874       (gnus-group-next-unread-group 1))
9875     (if temporary
9876         nil                             ;Nothing to do.
9877       ;; If we have several article buffers, we kill them at exit.
9878       (unless gnus-single-article-buffer
9879         (gnus-kill-buffer gnus-article-buffer)
9880         (gnus-kill-buffer gnus-original-article-buffer)
9881         (setq gnus-article-current nil))
9882       (set-buffer buf)
9883       (if (not gnus-kill-summary-on-exit)
9884           (gnus-deaden-summary)
9885         ;; We set all buffer-local variables to nil.  It is unclear why
9886         ;; this is needed, but if we don't, buffer-local variables are
9887         ;; not garbage-collected, it seems.  This would the lead to en
9888         ;; ever-growing Emacs.
9889         (gnus-summary-clear-local-variables)
9890         (when (get-buffer gnus-article-buffer)
9891           (bury-buffer gnus-article-buffer))
9892         ;; We clear the global counterparts of the buffer-local
9893         ;; variables as well, just to be on the safe side.
9894         (gnus-configure-windows 'group 'force)
9895         (gnus-summary-clear-local-variables)
9896         ;; Return to group mode buffer.
9897         (if (eq mode 'gnus-summary-mode)
9898             (gnus-kill-buffer buf)))
9899       (setq gnus-current-select-method gnus-select-method)
9900       (pop-to-buffer gnus-group-buffer)
9901       ;; Clear the current group name.
9902       (if (not quit-config)
9903           (progn
9904             (gnus-group-jump-to-group group)
9905             (gnus-group-next-unread-group 1)
9906             (gnus-configure-windows 'group 'force))
9907         (if (not (buffer-name (car quit-config)))
9908             (gnus-configure-windows 'group 'force)
9909           (set-buffer (car quit-config))
9910           (and (eq major-mode 'gnus-summary-mode)
9911                (gnus-set-global-variables))
9912           (gnus-configure-windows (cdr quit-config))))
9913       (unless quit-config
9914         (setq gnus-newsgroup-name nil)))))
9915
9916 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9917 (defun gnus-summary-exit-no-update (&optional no-questions)
9918   "Quit reading current newsgroup without updating read article info."
9919   (interactive)
9920   (gnus-set-global-variables)
9921   (let* ((group gnus-newsgroup-name)
9922          (quit-config (gnus-group-quit-config group)))
9923     (when (or no-questions
9924               gnus-expert-user
9925               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9926       ;; If we have several article buffers, we kill them at exit.
9927       (unless gnus-single-article-buffer
9928         (gnus-kill-buffer gnus-article-buffer)
9929         (gnus-kill-buffer gnus-original-article-buffer)
9930         (setq gnus-article-current nil))
9931       (if (not gnus-kill-summary-on-exit)
9932           (gnus-deaden-summary)
9933         (gnus-close-group group)
9934         (gnus-summary-clear-local-variables)
9935         (set-buffer gnus-group-buffer)
9936         (gnus-summary-clear-local-variables)
9937         (when (get-buffer gnus-summary-buffer)
9938           (kill-buffer gnus-summary-buffer)))
9939       (unless gnus-single-article-buffer
9940         (setq gnus-article-current nil))
9941       (when gnus-use-trees
9942         (gnus-tree-close group))
9943       (when (get-buffer gnus-article-buffer)
9944         (bury-buffer gnus-article-buffer))
9945       ;; Return to the group buffer.
9946       (gnus-configure-windows 'group 'force)
9947       ;; Clear the current group name.
9948       (setq gnus-newsgroup-name nil)
9949       (when (equal (gnus-group-group-name) group)
9950         (gnus-group-next-unread-group 1))
9951       (when quit-config
9952         (if (not (buffer-name (car quit-config)))
9953             (gnus-configure-windows 'group 'force)
9954           (set-buffer (car quit-config))
9955           (when (eq major-mode 'gnus-summary-mode)
9956             (gnus-set-global-variables))
9957           (gnus-configure-windows (cdr quit-config)))))))
9958
9959 ;;; Dead summaries.
9960
9961 (defvar gnus-dead-summary-mode-map nil)
9962
9963 (if gnus-dead-summary-mode-map
9964     nil
9965   (setq gnus-dead-summary-mode-map (make-keymap))
9966   (suppress-keymap gnus-dead-summary-mode-map)
9967   (substitute-key-definition
9968    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9969   (let ((keys '("\C-d" "\r" "\177")))
9970     (while keys
9971       (define-key gnus-dead-summary-mode-map
9972         (pop keys) 'gnus-summary-wake-up-the-dead))))
9973
9974 (defvar gnus-dead-summary-mode nil
9975   "Minor mode for Gnus summary buffers.")
9976
9977 (defun gnus-dead-summary-mode (&optional arg)
9978   "Minor mode for Gnus summary buffers."
9979   (interactive "P")
9980   (when (eq major-mode 'gnus-summary-mode)
9981     (make-local-variable 'gnus-dead-summary-mode)
9982     (setq gnus-dead-summary-mode
9983           (if (null arg) (not gnus-dead-summary-mode)
9984             (> (prefix-numeric-value arg) 0)))
9985     (when gnus-dead-summary-mode
9986       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9987         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9988       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9989         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9990               minor-mode-map-alist)))))
9991
9992 (defun gnus-deaden-summary ()
9993   "Make the current summary buffer into a dead summary buffer."
9994   ;; Kill any previous dead summary buffer.
9995   (when (and gnus-dead-summary
9996              (buffer-name gnus-dead-summary))
9997     (save-excursion
9998       (set-buffer gnus-dead-summary)
9999       (when gnus-dead-summary-mode
10000         (kill-buffer (current-buffer)))))
10001   ;; Make this the current dead summary.
10002   (setq gnus-dead-summary (current-buffer))
10003   (gnus-dead-summary-mode 1)
10004   (let ((name (buffer-name)))
10005     (when (string-match "Summary" name)
10006       (rename-buffer
10007        (concat (substring name 0 (match-beginning 0)) "Dead "
10008                (substring name (match-beginning 0))) t))))
10009
10010 (defun gnus-kill-or-deaden-summary (buffer)
10011   "Kill or deaden the summary BUFFER."
10012   (when (and (buffer-name buffer)
10013              (not gnus-single-article-buffer))
10014     (save-excursion
10015       (set-buffer buffer)
10016       (gnus-kill-buffer gnus-article-buffer)
10017       (gnus-kill-buffer gnus-original-article-buffer)))
10018   (cond (gnus-kill-summary-on-exit
10019          (when (and gnus-use-trees
10020                     (and (get-buffer buffer)
10021                          (buffer-name (get-buffer buffer))))
10022            (save-excursion
10023              (set-buffer (get-buffer buffer))
10024              (gnus-tree-close gnus-newsgroup-name)))
10025          (gnus-kill-buffer buffer))
10026         ((and (get-buffer buffer)
10027               (buffer-name (get-buffer buffer)))
10028          (save-excursion
10029            (set-buffer buffer)
10030            (gnus-deaden-summary)))))
10031
10032 (defun gnus-summary-wake-up-the-dead (&rest args)
10033   "Wake up the dead summary buffer."
10034   (interactive)
10035   (gnus-dead-summary-mode -1)
10036   (let ((name (buffer-name)))
10037     (when (string-match "Dead " name)
10038       (rename-buffer
10039        (concat (substring name 0 (match-beginning 0))
10040                (substring name (match-end 0))) t)))
10041   (gnus-message 3 "This dead summary is now alive again"))
10042
10043 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10044 (defun gnus-summary-fetch-faq (&optional faq-dir)
10045   "Fetch the FAQ for the current group.
10046 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10047 in."
10048   (interactive
10049    (list
10050     (if current-prefix-arg
10051         (completing-read
10052          "Faq dir: " (and (listp gnus-group-faq-directory)
10053                           gnus-group-faq-directory)))))
10054   (let (gnus-faq-buffer)
10055     (and (setq gnus-faq-buffer
10056                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10057          (gnus-configure-windows 'summary-faq))))
10058
10059 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10060 (defun gnus-summary-describe-group (&optional force)
10061   "Describe the current newsgroup."
10062   (interactive "P")
10063   (gnus-group-describe-group force gnus-newsgroup-name))
10064
10065 (defun gnus-summary-describe-briefly ()
10066   "Describe summary mode commands briefly."
10067   (interactive)
10068   (gnus-message 6
10069                 (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")))
10070
10071 ;; Walking around group mode buffer from summary mode.
10072
10073 (defun gnus-summary-next-group (&optional no-article target-group backward)
10074   "Exit current newsgroup and then select next unread newsgroup.
10075 If prefix argument NO-ARTICLE is non-nil, no article is selected
10076 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10077 previous group instead."
10078   (interactive "P")
10079   (gnus-set-global-variables)
10080   (let ((current-group gnus-newsgroup-name)
10081         (current-buffer (current-buffer))
10082         entered)
10083     ;; First we semi-exit this group to update Xrefs and all variables.
10084     ;; We can't do a real exit, because the window conf must remain
10085     ;; the same in case the user is prompted for info, and we don't
10086     ;; want the window conf to change before that...
10087     (gnus-summary-exit t)
10088     (while (not entered)
10089       ;; Then we find what group we are supposed to enter.
10090       (set-buffer gnus-group-buffer)
10091       (gnus-group-jump-to-group current-group)
10092       (setq target-group
10093             (or target-group
10094                 (if (eq gnus-keep-same-level 'best)
10095                     (gnus-summary-best-group gnus-newsgroup-name)
10096                   (gnus-summary-search-group backward gnus-keep-same-level))))
10097       (if (not target-group)
10098           ;; There are no further groups, so we return to the group
10099           ;; buffer.
10100           (progn
10101             (gnus-message 5 "Returning to the group buffer")
10102             (setq entered t)
10103             (set-buffer current-buffer)
10104             (gnus-summary-exit))
10105         ;; We try to enter the target group.
10106         (gnus-group-jump-to-group target-group)
10107         (let ((unreads (gnus-group-group-unread)))
10108           (if (and (or (eq t unreads)
10109                        (and unreads (not (zerop unreads))))
10110                    (gnus-summary-read-group
10111                     target-group nil no-article current-buffer))
10112               (setq entered t)
10113             (setq current-group target-group
10114                   target-group nil)))))))
10115
10116 (defun gnus-summary-prev-group (&optional no-article)
10117   "Exit current newsgroup and then select previous unread newsgroup.
10118 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10119   (interactive "P")
10120   (gnus-summary-next-group no-article nil t))
10121
10122 ;; Walking around summary lines.
10123
10124 (defun gnus-summary-first-subject (&optional unread)
10125   "Go to the first unread subject.
10126 If UNREAD is non-nil, go to the first unread article.
10127 Returns the article selected or nil if there are no unread articles."
10128   (interactive "P")
10129   (prog1
10130       (cond
10131        ;; Empty summary.
10132        ((null gnus-newsgroup-data)
10133         (gnus-message 3 "No articles in the group")
10134         nil)
10135        ;; Pick the first article.
10136        ((not unread)
10137         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10138         (gnus-data-number (car gnus-newsgroup-data)))
10139        ;; No unread articles.
10140        ((null gnus-newsgroup-unreads)
10141         (gnus-message 3 "No more unread articles")
10142         nil)
10143        ;; Find the first unread article.
10144        (t
10145         (let ((data gnus-newsgroup-data))
10146           (while (and data
10147                       (not (gnus-data-unread-p (car data))))
10148             (setq data (cdr data)))
10149           (if data
10150               (progn
10151                 (goto-char (gnus-data-pos (car data)))
10152                 (gnus-data-number (car data)))))))
10153     (gnus-summary-position-point)))
10154
10155 (defun gnus-summary-next-subject (n &optional unread dont-display)
10156   "Go to next N'th summary line.
10157 If N is negative, go to the previous N'th subject line.
10158 If UNREAD is non-nil, only unread articles are selected.
10159 The difference between N and the actual number of steps taken is
10160 returned."
10161   (interactive "p")
10162   (let ((backward (< n 0))
10163         (n (abs n)))
10164     (while (and (> n 0)
10165                 (if backward
10166                     (gnus-summary-find-prev unread)
10167                   (gnus-summary-find-next unread)))
10168       (setq n (1- n)))
10169     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10170                                (if unread " unread" "")))
10171     (unless dont-display
10172       (gnus-summary-recenter)
10173       (gnus-summary-position-point))
10174     n))
10175
10176 (defun gnus-summary-next-unread-subject (n)
10177   "Go to next N'th unread summary line."
10178   (interactive "p")
10179   (gnus-summary-next-subject n t))
10180
10181 (defun gnus-summary-prev-subject (n &optional unread)
10182   "Go to previous N'th summary line.
10183 If optional argument UNREAD is non-nil, only unread article is selected."
10184   (interactive "p")
10185   (gnus-summary-next-subject (- n) unread))
10186
10187 (defun gnus-summary-prev-unread-subject (n)
10188   "Go to previous N'th unread summary line."
10189   (interactive "p")
10190   (gnus-summary-next-subject (- n) t))
10191
10192 (defun gnus-summary-goto-subject (article &optional force silent)
10193   "Go the subject line of ARTICLE.
10194 If FORCE, also allow jumping to articles not currently shown."
10195   (let ((b (point))
10196         (data (gnus-data-find article)))
10197     ;; We read in the article if we have to.
10198     (and (not data)
10199          force
10200          (gnus-summary-insert-subject article (and (vectorp force) force))
10201          (setq data (gnus-data-find article)))
10202     (goto-char b)
10203     (if (not data)
10204         (progn
10205           (unless silent
10206             (gnus-message 3 "Can't find article %d" article))
10207           nil)
10208       (goto-char (gnus-data-pos data))
10209       article)))
10210
10211 ;; Walking around summary lines with displaying articles.
10212
10213 (defun gnus-summary-expand-window (&optional arg)
10214   "Make the summary buffer take up the entire Emacs frame.
10215 Given a prefix, will force an `article' buffer configuration."
10216   (interactive "P")
10217   (gnus-set-global-variables)
10218   (if arg
10219       (gnus-configure-windows 'article 'force)
10220     (gnus-configure-windows 'summary 'force)))
10221
10222 (defun gnus-summary-display-article (article &optional all-header)
10223   "Display ARTICLE in article buffer."
10224   (gnus-set-global-variables)
10225   (if (null article)
10226       nil
10227     (prog1
10228         (if gnus-summary-display-article-function
10229             (funcall gnus-summary-display-article-function article all-header)
10230           (gnus-article-prepare article all-header))
10231       (run-hooks 'gnus-select-article-hook)
10232       (unless (zerop gnus-current-article)
10233         (gnus-summary-goto-subject gnus-current-article))
10234       (gnus-summary-recenter)
10235       (when gnus-use-trees
10236         (gnus-possibly-generate-tree article)
10237         (gnus-highlight-selected-tree article))
10238       ;; Successfully display article.
10239       (gnus-article-set-window-start
10240        (cdr (assq article gnus-newsgroup-bookmarks))))))
10241
10242 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10243   "Select the current article.
10244 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10245 non-nil, the article will be re-fetched even if it already present in
10246 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10247 be displayed."
10248   ;; Make sure we are in the summary buffer to work around bbdb bug.
10249   (unless (eq major-mode 'gnus-summary-mode)
10250     (set-buffer gnus-summary-buffer))
10251   (let ((article (or article (gnus-summary-article-number)))
10252         (all-headers (not (not all-headers))) ;Must be T or NIL.
10253         gnus-summary-display-article-function
10254         did)
10255     (and (not pseudo)
10256          (gnus-summary-article-pseudo-p article)
10257          (error "This is a pseudo-article."))
10258     (prog1
10259         (save-excursion
10260           (set-buffer gnus-summary-buffer)
10261           (if (or (and gnus-single-article-buffer
10262                        (or (null gnus-current-article)
10263                            (null gnus-article-current)
10264                            (null (get-buffer gnus-article-buffer))
10265                            (not (eq article (cdr gnus-article-current)))
10266                            (not (equal (car gnus-article-current)
10267                                        gnus-newsgroup-name))))
10268                   (and (not gnus-single-article-buffer)
10269                        (or (null gnus-current-article)
10270                            (not (eq gnus-current-article article))))
10271                   force)
10272               ;; The requested article is different from the current article.
10273               (prog1
10274                   (gnus-summary-display-article article all-headers)
10275                 (setq did article))
10276             (if (or all-headers gnus-show-all-headers)
10277                 (gnus-article-show-all-headers))
10278             'old))
10279       (if did
10280           (gnus-article-set-window-start
10281            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10282
10283 (defun gnus-summary-set-current-mark (&optional current-mark)
10284   "Obsolete function."
10285   nil)
10286
10287 (defun gnus-summary-next-article (&optional unread subject backward push)
10288   "Select the next article.
10289 If UNREAD, only unread articles are selected.
10290 If SUBJECT, only articles with SUBJECT are selected.
10291 If BACKWARD, the previous article is selected instead of the next."
10292   (interactive "P")
10293   (gnus-set-global-variables)
10294   (cond
10295    ;; Is there such an article?
10296    ((and (gnus-summary-search-forward unread subject backward)
10297          (or (gnus-summary-display-article (gnus-summary-article-number))
10298              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10299     (gnus-summary-position-point))
10300    ;; If not, we try the first unread, if that is wanted.
10301    ((and subject
10302          gnus-auto-select-same
10303          (or (gnus-summary-first-unread-article)
10304              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10305     (gnus-summary-position-point)
10306     (gnus-message 6 "Wrapped"))
10307    ;; Try to get next/previous article not displayed in this group.
10308    ((and gnus-auto-extend-newsgroup
10309          (not unread) (not subject))
10310     (gnus-summary-goto-article
10311      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10312      nil t))
10313    ;; Go to next/previous group.
10314    (t
10315     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10316         (gnus-summary-jump-to-group gnus-newsgroup-name))
10317     (let ((cmd last-command-char)
10318           (group
10319            (if (eq gnus-keep-same-level 'best)
10320                (gnus-summary-best-group gnus-newsgroup-name)
10321              (gnus-summary-search-group backward gnus-keep-same-level))))
10322       ;; For some reason, the group window gets selected.  We change
10323       ;; it back.
10324       (select-window (get-buffer-window (current-buffer)))
10325       ;; Select next unread newsgroup automagically.
10326       (cond
10327        ((not gnus-auto-select-next)
10328         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10329        ((or (eq gnus-auto-select-next 'quietly)
10330             (and (eq gnus-auto-select-next 'slightly-quietly)
10331                  push)
10332             (and (eq gnus-auto-select-next 'almost-quietly)
10333                  (gnus-summary-last-article-p)))
10334         ;; Select quietly.
10335         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10336             (gnus-summary-exit)
10337           (gnus-message 7 "No more%s articles (%s)..."
10338                         (if unread " unread" "")
10339                         (if group (concat "selecting " group)
10340                           "exiting"))
10341           (gnus-summary-next-group nil group backward)))
10342        (t
10343         (gnus-summary-walk-group-buffer
10344          gnus-newsgroup-name cmd unread backward)))))))
10345
10346 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10347   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10348                       (?\C-p (gnus-group-prev-unread-group 1))))
10349         keve key group ended)
10350     (save-excursion
10351       (set-buffer gnus-group-buffer)
10352       (gnus-summary-jump-to-group from-group)
10353       (setq group
10354             (if (eq gnus-keep-same-level 'best)
10355                 (gnus-summary-best-group gnus-newsgroup-name)
10356               (gnus-summary-search-group backward gnus-keep-same-level))))
10357     (while (not ended)
10358       (gnus-message
10359        5 "No more%s articles%s" (if unread " unread" "")
10360        (if (and group
10361                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10362            (format " (Type %s for %s [%s])"
10363                    (single-key-description cmd) group
10364                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10365          (format " (Type %s to exit %s)"
10366                  (single-key-description cmd)
10367                  gnus-newsgroup-name)))
10368       ;; Confirm auto selection.
10369       (setq key (car (setq keve (gnus-read-event-char))))
10370       (setq ended t)
10371       (cond
10372        ((assq key keystrokes)
10373         (let ((obuf (current-buffer)))
10374           (switch-to-buffer gnus-group-buffer)
10375           (and group
10376                (gnus-group-jump-to-group group))
10377           (eval (cadr (assq key keystrokes)))
10378           (setq group (gnus-group-group-name))
10379           (switch-to-buffer obuf))
10380         (setq ended nil))
10381        ((equal key cmd)
10382         (if (or (not group)
10383                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10384             (gnus-summary-exit)
10385           (gnus-summary-next-group nil group backward)))
10386        (t
10387         (push (cdr keve) unread-command-events))))))
10388
10389 (defun gnus-read-event-char ()
10390   "Get the next event."
10391   (let ((event (read-event)))
10392     (cons (and (numberp event) event) event)))
10393
10394 (defun gnus-summary-next-unread-article ()
10395   "Select unread article after current one."
10396   (interactive)
10397   (gnus-summary-next-article t (and gnus-auto-select-same
10398                                     (gnus-summary-article-subject))))
10399
10400 (defun gnus-summary-prev-article (&optional unread subject)
10401   "Select the article after the current one.
10402 If UNREAD is non-nil, only unread articles are selected."
10403   (interactive "P")
10404   (gnus-summary-next-article unread subject t))
10405
10406 (defun gnus-summary-prev-unread-article ()
10407   "Select unred article before current one."
10408   (interactive)
10409   (gnus-summary-prev-article t (and gnus-auto-select-same
10410                                     (gnus-summary-article-subject))))
10411
10412 (defun gnus-summary-next-page (&optional lines circular)
10413   "Show next page of the selected article.
10414 If at the end of the current article, select the next article.
10415 LINES says how many lines should be scrolled up.
10416
10417 If CIRCULAR is non-nil, go to the start of the article instead of
10418 selecting the next article when reaching the end of the current
10419 article."
10420   (interactive "P")
10421   (setq gnus-summary-buffer (current-buffer))
10422   (gnus-set-global-variables)
10423   (let ((article (gnus-summary-article-number))
10424         (endp nil))
10425     (gnus-configure-windows 'article)
10426     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10427         (if (and (eq gnus-summary-goto-unread 'never)
10428                  (not (gnus-summary-last-article-p article)))
10429             (gnus-summary-next-article)
10430           (gnus-summary-next-unread-article))
10431       (if (or (null gnus-current-article)
10432               (null gnus-article-current)
10433               (/= article (cdr gnus-article-current))
10434               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10435           ;; Selected subject is different from current article's.
10436           (gnus-summary-display-article article)
10437         (gnus-eval-in-buffer-window
10438          gnus-article-buffer
10439          (setq endp (gnus-article-next-page lines)))
10440         (if endp
10441             (cond (circular
10442                    (gnus-summary-beginning-of-article))
10443                   (lines
10444                    (gnus-message 3 "End of message"))
10445                   ((null lines)
10446                    (if (and (eq gnus-summary-goto-unread 'never)
10447                             (not (gnus-summary-last-article-p article)))
10448                        (gnus-summary-next-article)
10449                      (gnus-summary-next-unread-article)))))))
10450     (gnus-summary-recenter)
10451     (gnus-summary-position-point)))
10452
10453 (defun gnus-summary-prev-page (&optional lines)
10454   "Show previous page of selected article.
10455 Argument LINES specifies lines to be scrolled down."
10456   (interactive "P")
10457   (gnus-set-global-variables)
10458   (let ((article (gnus-summary-article-number)))
10459     (gnus-configure-windows 'article)
10460     (if (or (null gnus-current-article)
10461             (null gnus-article-current)
10462             (/= article (cdr gnus-article-current))
10463             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10464         ;; Selected subject is different from current article's.
10465         (gnus-summary-display-article article)
10466       (gnus-summary-recenter)
10467       (gnus-eval-in-buffer-window gnus-article-buffer
10468                                   (gnus-article-prev-page lines))))
10469   (gnus-summary-position-point))
10470
10471 (defun gnus-summary-scroll-up (lines)
10472   "Scroll up (or down) one line current article.
10473 Argument LINES specifies lines to be scrolled up (or down if negative)."
10474   (interactive "p")
10475   (gnus-set-global-variables)
10476   (gnus-configure-windows 'article)
10477   (gnus-summary-show-thread)
10478   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10479     (gnus-eval-in-buffer-window
10480      gnus-article-buffer
10481      (cond ((> lines 0)
10482             (if (gnus-article-next-page lines)
10483                 (gnus-message 3 "End of message")))
10484            ((< lines 0)
10485             (gnus-article-prev-page (- lines))))))
10486   (gnus-summary-recenter)
10487   (gnus-summary-position-point))
10488
10489 (defun gnus-summary-next-same-subject ()
10490   "Select next article which has the same subject as current one."
10491   (interactive)
10492   (gnus-set-global-variables)
10493   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10494
10495 (defun gnus-summary-prev-same-subject ()
10496   "Select previous article which has the same subject as current one."
10497   (interactive)
10498   (gnus-set-global-variables)
10499   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10500
10501 (defun gnus-summary-next-unread-same-subject ()
10502   "Select next unread article which has the same subject as current one."
10503   (interactive)
10504   (gnus-set-global-variables)
10505   (gnus-summary-next-article t (gnus-summary-article-subject)))
10506
10507 (defun gnus-summary-prev-unread-same-subject ()
10508   "Select previous unread article which has the same subject as current one."
10509   (interactive)
10510   (gnus-set-global-variables)
10511   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10512
10513 (defun gnus-summary-first-unread-article ()
10514   "Select the first unread article.
10515 Return nil if there are no unread articles."
10516   (interactive)
10517   (gnus-set-global-variables)
10518   (prog1
10519       (if (gnus-summary-first-subject t)
10520           (progn
10521             (gnus-summary-show-thread)
10522             (gnus-summary-first-subject t)
10523             (gnus-summary-display-article (gnus-summary-article-number))))
10524     (gnus-summary-position-point)))
10525
10526 (defun gnus-summary-best-unread-article ()
10527   "Select the unread article with the highest score."
10528   (interactive)
10529   (gnus-set-global-variables)
10530   (let ((best -1000000)
10531         (data gnus-newsgroup-data)
10532         article score)
10533     (while data
10534       (and (gnus-data-unread-p (car data))
10535            (> (setq score
10536                     (gnus-summary-article-score (gnus-data-number (car data))))
10537               best)
10538            (setq best score
10539                  article (gnus-data-number (car data))))
10540       (setq data (cdr data)))
10541     (prog1
10542         (if article
10543             (gnus-summary-goto-article article)
10544           (error "No unread articles"))
10545       (gnus-summary-position-point))))
10546
10547 (defun gnus-summary-last-subject ()
10548   "Go to the last displayed subject line in the group."
10549   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10550     (when article
10551       (gnus-summary-goto-subject article))))
10552
10553 (defun gnus-summary-goto-article (article &optional all-headers force)
10554   "Fetch ARTICLE and display it if it exists.
10555 If ALL-HEADERS is non-nil, no header lines are hidden."
10556   (interactive
10557    (list
10558     (string-to-int
10559      (completing-read
10560       "Article number: "
10561       (mapcar (lambda (number) (list (int-to-string number)))
10562               gnus-newsgroup-limit)))
10563     current-prefix-arg
10564     t))
10565   (prog1
10566       (if (gnus-summary-goto-subject article force)
10567           (gnus-summary-display-article article all-headers)
10568         (gnus-message 4 "Couldn't go to article %s" article) nil)
10569     (gnus-summary-position-point)))
10570
10571 (defun gnus-summary-goto-last-article ()
10572   "Go to the previously read article."
10573   (interactive)
10574   (prog1
10575       (and gnus-last-article
10576            (gnus-summary-goto-article gnus-last-article))
10577     (gnus-summary-position-point)))
10578
10579 (defun gnus-summary-pop-article (number)
10580   "Pop one article off the history and go to the previous.
10581 NUMBER articles will be popped off."
10582   (interactive "p")
10583   (let (to)
10584     (setq gnus-newsgroup-history
10585           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10586     (if to
10587         (gnus-summary-goto-article (car to))
10588       (error "Article history empty")))
10589   (gnus-summary-position-point))
10590
10591 ;; Summary commands and functions for limiting the summary buffer.
10592
10593 (defun gnus-summary-limit-to-articles (n)
10594   "Limit the summary buffer to the next N articles.
10595 If not given a prefix, use the process marked articles instead."
10596   (interactive "P")
10597   (gnus-set-global-variables)
10598   (prog1
10599       (let ((articles (gnus-summary-work-articles n)))
10600         (setq gnus-newsgroup-processable nil)
10601         (gnus-summary-limit articles))
10602     (gnus-summary-position-point)))
10603
10604 (defun gnus-summary-pop-limit (&optional total)
10605   "Restore the previous limit.
10606 If given a prefix, remove all limits."
10607   (interactive "P")
10608   (gnus-set-global-variables)
10609   (when total 
10610     (setq gnus-newsgroup-limits
10611           (list (mapcar (lambda (h) (mail-header-number h))
10612                         gnus-newsgroup-headers))))
10613   (unless gnus-newsgroup-limits
10614     (error "No limit to pop"))
10615   (prog1
10616       (gnus-summary-limit nil 'pop)
10617     (gnus-summary-position-point)))
10618
10619 (defun gnus-summary-limit-to-subject (subject &optional header)
10620   "Limit the summary buffer to articles that have subjects that match a regexp."
10621   (interactive "sRegexp: ")
10622   (unless header
10623     (setq header "subject"))
10624   (when (not (equal "" subject))
10625     (prog1
10626         (let ((articles (gnus-summary-find-matching
10627                          (or header "subject") subject 'all)))
10628           (or articles (error "Found no matches for \"%s\"" subject))
10629           (gnus-summary-limit articles))
10630       (gnus-summary-position-point))))
10631
10632 (defun gnus-summary-limit-to-author (from)
10633   "Limit the summary buffer to articles that have authors that match a regexp."
10634   (interactive "sRegexp: ")
10635   (gnus-summary-limit-to-subject from "from"))
10636
10637 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10638 (make-obsolete
10639  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10640
10641 (defun gnus-summary-limit-to-unread (&optional all)
10642   "Limit the summary buffer to articles that are not marked as read.
10643 If ALL is non-nil, limit strictly to unread articles."
10644   (interactive "P")
10645   (if all
10646       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10647     (gnus-summary-limit-to-marks
10648      ;; Concat all the marks that say that an article is read and have
10649      ;; those removed.
10650      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10651            gnus-killed-mark gnus-kill-file-mark
10652            gnus-low-score-mark gnus-expirable-mark
10653            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10654      'reverse)))
10655
10656 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10657 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10658
10659 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10660   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10661 If REVERSE, limit the summary buffer to articles that are not marked
10662 with MARKS.  MARKS can either be a string of marks or a list of marks.
10663 Returns how many articles were removed."
10664   (interactive "sMarks: ")
10665   (gnus-set-global-variables)
10666   (prog1
10667       (let ((data gnus-newsgroup-data)
10668             (marks (if (listp marks) marks
10669                      (append marks nil))) ; Transform to list.
10670             articles)
10671         (while data
10672           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10673                  (memq (gnus-data-mark (car data)) marks))
10674                (setq articles (cons (gnus-data-number (car data)) articles)))
10675           (setq data (cdr data)))
10676         (gnus-summary-limit articles))
10677     (gnus-summary-position-point)))
10678
10679 (defun gnus-summary-limit-to-score (&optional score)
10680   "Limit to articles with score at or above SCORE."
10681   (interactive "P")
10682   (gnus-set-global-variables)
10683   (setq score (if score
10684                   (prefix-numeric-value score)
10685                 (or gnus-summary-default-score 0)))
10686   (let ((data gnus-newsgroup-data)
10687         articles)
10688     (while data
10689       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10690                 score)
10691         (push (gnus-data-number (car data)) articles))
10692       (setq data (cdr data)))
10693     (prog1
10694         (gnus-summary-limit articles)
10695       (gnus-summary-position-point))))
10696
10697 (defun gnus-summary-limit-include-dormant ()
10698   "Display all the hidden articles that are marked as dormant."
10699   (interactive)
10700   (gnus-set-global-variables)
10701   (or gnus-newsgroup-dormant
10702       (error "There are no dormant articles in this group"))
10703   (prog1
10704       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10705     (gnus-summary-position-point)))
10706
10707 (defun gnus-summary-limit-exclude-dormant ()
10708   "Hide all dormant articles."
10709   (interactive)
10710   (gnus-set-global-variables)
10711   (prog1
10712       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10713     (gnus-summary-position-point)))
10714
10715 (defun gnus-summary-limit-exclude-childless-dormant ()
10716   "Hide all dormant articles that have no children."
10717   (interactive)
10718   (gnus-set-global-variables)
10719   (let ((data (gnus-data-list t))
10720         articles d children)
10721     ;; Find all articles that are either not dormant or have
10722     ;; children.
10723     (while (setq d (pop data))
10724       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10725                 (and (setq children 
10726                            (gnus-article-children (gnus-data-number d)))
10727                      (let (found)
10728                        (while children
10729                          (when (memq (car children) articles)
10730                            (setq children nil
10731                                  found t))
10732                          (pop children))
10733                        found)))
10734         (push (gnus-data-number d) articles)))
10735     ;; Do the limiting.
10736     (prog1
10737         (gnus-summary-limit articles)
10738       (gnus-summary-position-point))))
10739
10740 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10741   "Mark all unread excluded articles as read.
10742 If ALL, mark even excluded ticked and dormants as read."
10743   (interactive "P")
10744   (let ((articles (gnus-sorted-complement
10745                    (sort
10746                     (mapcar (lambda (h) (mail-header-number h))
10747                             gnus-newsgroup-headers)
10748                     '<)
10749                    (sort gnus-newsgroup-limit '<)))
10750         article)
10751     (setq gnus-newsgroup-unreads nil)
10752     (if all
10753         (setq gnus-newsgroup-dormant nil
10754               gnus-newsgroup-marked nil
10755               gnus-newsgroup-reads
10756               (nconc
10757                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10758                gnus-newsgroup-reads))
10759       (while (setq article (pop articles))
10760         (unless (or (memq article gnus-newsgroup-dormant)
10761                     (memq article gnus-newsgroup-marked))
10762           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10763
10764 (defun gnus-summary-limit (articles &optional pop)
10765   (if pop
10766       ;; We pop the previous limit off the stack and use that.
10767       (setq articles (car gnus-newsgroup-limits)
10768             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10769     ;; We use the new limit, so we push the old limit on the stack.
10770     (setq gnus-newsgroup-limits
10771           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10772   ;; Set the limit.
10773   (setq gnus-newsgroup-limit articles)
10774   (let ((total (length gnus-newsgroup-data))
10775         (data (gnus-data-find-list (gnus-summary-article-number)))
10776         found)
10777     ;; This will do all the work of generating the new summary buffer
10778     ;; according to the new limit.
10779     (gnus-summary-prepare)
10780     ;; Hide any threads, possibly.
10781     (and gnus-show-threads
10782          gnus-thread-hide-subtree
10783          (gnus-summary-hide-all-threads))
10784     ;; Try to return to the article you were at, or one in the
10785     ;; neighborhood.
10786     (if data
10787         ;; We try to find some article after the current one.
10788         (while data
10789           (and (gnus-summary-goto-subject
10790                 (gnus-data-number (car data)) nil t)
10791                (setq data nil
10792                      found t))
10793           (setq data (cdr data))))
10794     (or found
10795         ;; If there is no data, that means that we were after the last
10796         ;; article.  The same goes when we can't find any articles
10797         ;; after the current one.
10798         (progn
10799           (goto-char (point-max))
10800           (gnus-summary-find-prev)))
10801     ;; We return how many articles were removed from the summary
10802     ;; buffer as a result of the new limit.
10803     (- total (length gnus-newsgroup-data))))
10804
10805 (defsubst gnus-invisible-cut-children (threads)
10806   (let ((num 0))
10807     (while threads
10808       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10809         (incf num))
10810       (pop threads))
10811     (< num 2)))
10812
10813 (defsubst gnus-cut-thread (thread)
10814   "Go forwards in the thread until we find an article that we want to display."
10815   (when (or (eq gnus-fetch-old-headers 'some)
10816             (eq gnus-build-sparse-threads 'some)
10817             (eq gnus-build-sparse-threads 'more))
10818     ;; Deal with old-fetched headers and sparse threads.
10819     (while (and
10820             thread
10821             (or
10822              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10823              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10824             (or (<= (length (cdr thread)) 1)
10825                 (gnus-invisible-cut-children (cdr thread))))
10826       (setq thread (cadr thread))))
10827   thread)
10828
10829 (defun gnus-cut-threads (threads)
10830   "Cut off all uninteresting articles from the beginning of threads."
10831   (when (or (eq gnus-fetch-old-headers 'some)
10832             (eq gnus-build-sparse-threads 'some)
10833             (eq gnus-build-sparse-threads 'more))
10834     (let ((th threads))
10835       (while th
10836         (setcar th (gnus-cut-thread (car th)))
10837         (setq th (cdr th)))))
10838   ;; Remove nixed out threads.
10839   (delq nil threads))
10840
10841 (defun gnus-summary-initial-limit (&optional show-if-empty)
10842   "Figure out what the initial limit is supposed to be on group entry.
10843 This entails weeding out unwanted dormants, low-scored articles,
10844 fetch-old-headers verbiage, and so on."
10845   ;; Most groups have nothing to remove.
10846   (if (or gnus-inhibit-limiting
10847           (and (null gnus-newsgroup-dormant)
10848                (not (eq gnus-fetch-old-headers 'some))
10849                (null gnus-summary-expunge-below)
10850                (not (eq gnus-build-sparse-threads 'some))
10851                (not (eq gnus-build-sparse-threads 'more))
10852                (null gnus-thread-expunge-below)
10853                (not gnus-use-nocem)))
10854       () ; Do nothing.
10855     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10856     (setq gnus-newsgroup-limit nil)
10857     (mapatoms
10858      (lambda (node)
10859        (unless (car (symbol-value node))
10860          ;; These threads have no parents -- they are roots.
10861          (let ((nodes (cdr (symbol-value node)))
10862                thread)
10863            (while nodes
10864              (if (and gnus-thread-expunge-below
10865                       (< (gnus-thread-total-score (car nodes))
10866                          gnus-thread-expunge-below))
10867                  (gnus-expunge-thread (pop nodes))
10868                (setq thread (pop nodes))
10869                (gnus-summary-limit-children thread))))))
10870      gnus-newsgroup-dependencies)
10871     ;; If this limitation resulted in an empty group, we might
10872     ;; pop the previous limit and use it instead.
10873     (when (and (not gnus-newsgroup-limit)
10874                show-if-empty)
10875       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10876     gnus-newsgroup-limit))
10877
10878 (defun gnus-summary-limit-children (thread)
10879   "Return 1 if this subthread is visible and 0 if it is not."
10880   ;; First we get the number of visible children to this thread.  This
10881   ;; is done by recursing down the thread using this function, so this
10882   ;; will really go down to a leaf article first, before slowly
10883   ;; working its way up towards the root.
10884   (when thread
10885     (let ((children
10886            (if (cdr thread)
10887                (apply '+ (mapcar 'gnus-summary-limit-children
10888                                  (cdr thread)))
10889              0))
10890           (number (mail-header-number (car thread)))
10891           score)
10892       (if (or
10893            ;; If this article is dormant and has absolutely no visible
10894            ;; children, then this article isn't visible.
10895            (and (memq number gnus-newsgroup-dormant)
10896                 (= children 0))
10897            ;; If this is "fetch-old-headered" and there is only one
10898            ;; visible child (or less), then we don't want this article.
10899            (and (eq gnus-fetch-old-headers 'some)
10900                 (memq number gnus-newsgroup-ancient)
10901                 (zerop children))
10902            ;; If this is a sparsely inserted article with no children,
10903            ;; we don't want it.
10904            (and (eq gnus-build-sparse-threads 'some)
10905                 (memq number gnus-newsgroup-sparse)
10906                 (zerop children))
10907            ;; If we use expunging, and this article is really
10908            ;; low-scored, then we don't want this article.
10909            (when (and gnus-summary-expunge-below
10910                       (< (setq score
10911                                (or (cdr (assq number gnus-newsgroup-scored))
10912                                    gnus-summary-default-score))
10913                          gnus-summary-expunge-below))
10914              ;; We increase the expunge-tally here, but that has
10915              ;; nothing to do with the limits, really.
10916              (incf gnus-newsgroup-expunged-tally)
10917              ;; We also mark as read here, if that's wanted.
10918              (when (and gnus-summary-mark-below
10919                         (< score gnus-summary-mark-below))
10920                (setq gnus-newsgroup-unreads
10921                      (delq number gnus-newsgroup-unreads))
10922                (if gnus-newsgroup-auto-expire
10923                    (push number gnus-newsgroup-expirable)
10924                  (push (cons number gnus-low-score-mark)
10925                        gnus-newsgroup-reads)))
10926              t)
10927            (and gnus-use-nocem
10928                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10929           ;; Nope, invisible article.
10930           0
10931         ;; Ok, this article is to be visible, so we add it to the limit
10932         ;; and return 1.
10933         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10934         1))))
10935
10936 (defun gnus-expunge-thread (thread)
10937   "Mark all articles in THREAD as read."
10938   (let* ((number (mail-header-number (car thread))))
10939     (incf gnus-newsgroup-expunged-tally)
10940     ;; We also mark as read here, if that's wanted.
10941     (setq gnus-newsgroup-unreads
10942           (delq number gnus-newsgroup-unreads))
10943     (if gnus-newsgroup-auto-expire
10944         (push number gnus-newsgroup-expirable)
10945       (push (cons number gnus-low-score-mark)
10946             gnus-newsgroup-reads)))
10947   ;; Go recursively through all subthreads.
10948   (mapcar 'gnus-expunge-thread (cdr thread)))
10949
10950 ;; Summary article oriented commands
10951
10952 (defun gnus-summary-refer-parent-article (n)
10953   "Refer parent article N times.
10954 The difference between N and the number of articles fetched is returned."
10955   (interactive "p")
10956   (gnus-set-global-variables)
10957   (while
10958       (and
10959        (> n 0)
10960        (let* ((header (gnus-summary-article-header))
10961               (ref
10962                ;; If we try to find the parent of the currently
10963                ;; displayed article, then we take a look at the actual
10964                ;; References header, since this is slightly more
10965                ;; reliable than the References field we got from the
10966                ;; server.
10967                (if (and (eq (mail-header-number header)
10968                             (cdr gnus-article-current))
10969                         (equal gnus-newsgroup-name
10970                                (car gnus-article-current)))
10971                    (save-excursion
10972                      (set-buffer gnus-original-article-buffer)
10973                      (nnheader-narrow-to-headers)
10974                      (prog1
10975                          (message-fetch-field "references")
10976                        (widen)))
10977                  ;; It's not the current article, so we take a bet on
10978                  ;; the value we got from the server.
10979                  (mail-header-references header))))
10980          (if (setq ref (or ref (mail-header-references header)))
10981              (or (gnus-summary-refer-article (gnus-parent-id ref))
10982                  (gnus-message 1 "Couldn't find parent"))
10983            (gnus-message 1 "No references in article %d"
10984                          (gnus-summary-article-number))
10985            nil)))
10986     (setq n (1- n)))
10987   (gnus-summary-position-point)
10988   n)
10989
10990 (defun gnus-summary-refer-references ()
10991   "Fetch all articles mentioned in the References header.
10992 Return how many articles were fetched."
10993   (interactive)
10994   (gnus-set-global-variables)
10995   (let ((ref (mail-header-references (gnus-summary-article-header)))
10996         (current (gnus-summary-article-number))
10997         (n 0))
10998     ;; For each Message-ID in the References header...
10999     (while (string-match "<[^>]*>" ref)
11000       (incf n)
11001       ;; ... fetch that article.
11002       (gnus-summary-refer-article
11003        (prog1 (match-string 0 ref)
11004          (setq ref (substring ref (match-end 0))))))
11005     (gnus-summary-goto-subject current)
11006     (gnus-summary-position-point)
11007     n))
11008
11009 (defun gnus-summary-refer-article (message-id)
11010   "Fetch an article specified by MESSAGE-ID."
11011   (interactive "sMessage-ID: ")
11012   (when (and (stringp message-id)
11013              (not (zerop (length message-id))))
11014     ;; Construct the correct Message-ID if necessary.
11015     ;; Suggested by tale@pawl.rpi.edu.
11016     (unless (string-match "^<" message-id)
11017       (setq message-id (concat "<" message-id)))
11018     (unless (string-match ">$" message-id)
11019       (setq message-id (concat message-id ">")))
11020     (let ((header (gnus-id-to-header message-id)))
11021       (if header
11022           ;; The article is present in the buffer, to we just go to it.
11023           (gnus-summary-goto-article (mail-header-number header) nil header)
11024         ;; We fetch the article
11025         (let ((gnus-override-method 
11026                (and (gnus-news-group-p gnus-newsgroup-name)
11027                     gnus-refer-article-method))
11028               number)
11029           ;; Start the special refer-article method, if necessary.
11030           (when (and gnus-refer-article-method
11031                      (gnus-news-group-p gnus-newsgroup-name))
11032             (gnus-check-server gnus-refer-article-method))
11033           ;; Fetch the header, and display the article.
11034           (if (setq number (gnus-summary-insert-subject message-id))
11035               (gnus-summary-select-article nil nil nil number)
11036             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11037
11038 (defun gnus-summary-enter-digest-group (&optional force)
11039   "Enter a digest group based on the current article."
11040   (interactive "P")
11041   (gnus-set-global-variables)
11042   (gnus-summary-select-article)
11043   (let ((name (format "%s-%d"
11044                       (gnus-group-prefixed-name
11045                        gnus-newsgroup-name (list 'nndoc ""))
11046                       gnus-current-article))
11047         (ogroup gnus-newsgroup-name)
11048         (case-fold-search t)
11049         (buf (current-buffer))
11050         dig)
11051     (save-excursion
11052       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11053       (insert-buffer-substring gnus-original-article-buffer)
11054       (narrow-to-region
11055        (goto-char (point-min))
11056        (or (search-forward "\n\n" nil t) (point)))
11057       (goto-char (point-min))
11058       (delete-matching-lines "^\\(Path\\):\\|^From ")
11059       (widen))
11060     (unwind-protect
11061         (if (gnus-group-read-ephemeral-group
11062              name `(nndoc ,name (nndoc-address
11063                                  ,(get-buffer dig))
11064                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11065             ;; Make all postings to this group go to the parent group.
11066             (nconc (gnus-info-params (gnus-get-info name))
11067                    (list (cons 'to-group ogroup)))
11068           ;; Couldn't select this doc group.
11069           (switch-to-buffer buf)
11070           (gnus-set-global-variables)
11071           (gnus-configure-windows 'summary)
11072           (gnus-message 3 "Article couldn't be entered?"))
11073       (kill-buffer dig))))
11074
11075 (defun gnus-summary-isearch-article (&optional regexp-p)
11076   "Do incremental search forward on the current article.
11077 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11078   (interactive "P")
11079   (gnus-set-global-variables)
11080   (gnus-summary-select-article)
11081   (gnus-configure-windows 'article)
11082   (gnus-eval-in-buffer-window
11083    gnus-article-buffer
11084    (goto-char (point-min))
11085    (isearch-forward regexp-p)))
11086
11087 (defun gnus-summary-search-article-forward (regexp &optional backward)
11088   "Search for an article containing REGEXP forward.
11089 If BACKWARD, search backward instead."
11090   (interactive
11091    (list (read-string
11092           (format "Search article %s (regexp%s): "
11093                   (if current-prefix-arg "backward" "forward")
11094                   (if gnus-last-search-regexp
11095                       (concat ", default " gnus-last-search-regexp)
11096                     "")))
11097          current-prefix-arg))
11098   (gnus-set-global-variables)
11099   (if (string-equal regexp "")
11100       (setq regexp (or gnus-last-search-regexp ""))
11101     (setq gnus-last-search-regexp regexp))
11102   (unless (gnus-summary-search-article regexp backward)
11103     (error "Search failed: \"%s\"" regexp)))
11104
11105 (defun gnus-summary-search-article-backward (regexp)
11106   "Search for an article containing REGEXP backward."
11107   (interactive
11108    (list (read-string
11109           (format "Search article backward (regexp%s): "
11110                   (if gnus-last-search-regexp
11111                       (concat ", default " gnus-last-search-regexp)
11112                     "")))))
11113   (gnus-summary-search-article-forward regexp 'backward))
11114
11115 (defun gnus-summary-search-article (regexp &optional backward)
11116   "Search for an article containing REGEXP.
11117 Optional argument BACKWARD means do search for backward.
11118 `gnus-select-article-hook' is not called during the search."
11119   (let ((gnus-select-article-hook nil)  ;Disable hook.
11120         (gnus-article-display-hook nil)
11121         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11122         (re-search
11123          (if backward
11124              're-search-backward 're-search-forward))
11125         (sum (current-buffer))
11126         (found nil))
11127     (gnus-save-hidden-threads
11128       (gnus-summary-select-article)
11129       (set-buffer gnus-article-buffer)
11130       (while (not found)
11131         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11132         (if (if backward
11133                 (re-search-backward regexp nil t)
11134               (re-search-forward regexp nil t))
11135             ;; We found the regexp.
11136             (progn
11137               (setq found 'found)
11138               (beginning-of-line)
11139               (set-window-start
11140                (get-buffer-window (current-buffer))
11141                (point))
11142               (forward-line 1)
11143               (set-buffer sum))
11144           ;; We didn't find it, so we go to the next article.
11145           (set-buffer sum)
11146           (if (not (if backward (gnus-summary-find-prev)
11147                      (gnus-summary-find-next)))
11148               ;; No more articles.
11149               (setq found t)
11150             ;; Select the next article and adjust point.
11151             (gnus-summary-select-article)
11152             (set-buffer gnus-article-buffer)
11153             (widen)
11154             (goto-char (if backward (point-max) (point-min))))))
11155       (gnus-message 7 ""))
11156     ;; Return whether we found the regexp.
11157     (when (eq found 'found)
11158       (gnus-summary-show-thread)
11159       (gnus-summary-goto-subject gnus-current-article)
11160       (gnus-summary-position-point)
11161       t)))
11162
11163 (defun gnus-summary-find-matching (header regexp &optional backward unread
11164                                           not-case-fold)
11165   "Return a list of all articles that match REGEXP on HEADER.
11166 The search stars on the current article and goes forwards unless
11167 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11168 If UNREAD is non-nil, only unread articles will
11169 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11170 in the comparisons."
11171   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11172                 (gnus-data-find-list
11173                  (gnus-summary-article-number) (gnus-data-list backward))))
11174         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11175         (case-fold-search (not not-case-fold))
11176         articles d)
11177     (or (fboundp (intern (concat "mail-header-" header)))
11178         (error "%s is not a valid header" header))
11179     (while data
11180       (setq d (car data))
11181       (and (or (not unread)             ; We want all articles...
11182                (gnus-data-unread-p d))  ; Or just unreads.
11183            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11184            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11185            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11186       (setq data (cdr data)))
11187     (nreverse articles)))
11188
11189 (defun gnus-summary-execute-command (header regexp command &optional backward)
11190   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11191 If HEADER is an empty string (or nil), the match is done on the entire
11192 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11193   (interactive
11194    (list (let ((completion-ignore-case t))
11195            (completing-read
11196             "Header name: "
11197             (mapcar (lambda (string) (list string))
11198                     '("Number" "Subject" "From" "Lines" "Date"
11199                       "Message-ID" "Xref" "References" "Body"))
11200             nil 'require-match))
11201          (read-string "Regexp: ")
11202          (read-key-sequence "Command: ")
11203          current-prefix-arg))
11204   (when (equal header "Body")
11205     (setq header ""))
11206   (gnus-set-global-variables)
11207   ;; Hidden thread subtrees must be searched as well.
11208   (gnus-summary-show-all-threads)
11209   ;; We don't want to change current point nor window configuration.
11210   (save-excursion
11211     (save-window-excursion
11212       (gnus-message 6 "Executing %s..." (key-description command))
11213       ;; We'd like to execute COMMAND interactively so as to give arguments.
11214       (gnus-execute header regexp
11215                     `(lambda () (call-interactively ',(key-binding command)))
11216                     backward)
11217       (gnus-message 6 "Executing %s...done" (key-description command)))))
11218
11219 (defun gnus-summary-beginning-of-article ()
11220   "Scroll the article back to the beginning."
11221   (interactive)
11222   (gnus-set-global-variables)
11223   (gnus-summary-select-article)
11224   (gnus-configure-windows 'article)
11225   (gnus-eval-in-buffer-window
11226    gnus-article-buffer
11227    (widen)
11228    (goto-char (point-min))
11229    (and gnus-break-pages (gnus-narrow-to-page))))
11230
11231 (defun gnus-summary-end-of-article ()
11232   "Scroll to the end of the article."
11233   (interactive)
11234   (gnus-set-global-variables)
11235   (gnus-summary-select-article)
11236   (gnus-configure-windows 'article)
11237   (gnus-eval-in-buffer-window
11238    gnus-article-buffer
11239    (widen)
11240    (goto-char (point-max))
11241    (recenter -3)
11242    (and gnus-break-pages (gnus-narrow-to-page))))
11243
11244 (defun gnus-summary-show-article (&optional arg)
11245   "Force re-fetching of the current article.
11246 If ARG (the prefix) is non-nil, show the raw article without any
11247 article massaging functions being run."
11248   (interactive "P")
11249   (gnus-set-global-variables)
11250   (if (not arg)
11251       ;; Select the article the normal way.
11252       (gnus-summary-select-article nil 'force)
11253     ;; Bind the article treatment functions to nil.
11254     (let ((gnus-have-all-headers t)
11255           gnus-article-display-hook
11256           gnus-article-prepare-hook
11257           gnus-break-pages
11258           gnus-visual)
11259       (gnus-summary-select-article nil 'force)))
11260   (gnus-summary-goto-subject gnus-current-article)
11261 ;  (gnus-configure-windows 'article)
11262   (gnus-summary-position-point))
11263
11264 (defun gnus-summary-verbose-headers (&optional arg)
11265   "Toggle permanent full header display.
11266 If ARG is a positive number, turn header display on.
11267 If ARG is a negative number, turn header display off."
11268   (interactive "P")
11269   (gnus-set-global-variables)
11270   (gnus-summary-toggle-header arg)
11271   (setq gnus-show-all-headers
11272         (cond ((or (not (numberp arg))
11273                    (zerop arg))
11274                (not gnus-show-all-headers))
11275               ((natnump arg)
11276                t))))
11277
11278 (defun gnus-summary-toggle-header (&optional arg)
11279   "Show the headers if they are hidden, or hide them if they are shown.
11280 If ARG is a positive number, show the entire header.
11281 If ARG is a negative number, hide the unwanted header lines."
11282   (interactive "P")
11283   (gnus-set-global-variables)
11284   (save-excursion
11285     (set-buffer gnus-article-buffer)
11286     (let* ((buffer-read-only nil)
11287            (inhibit-point-motion-hooks t)
11288            (hidden (text-property-any
11289                     (goto-char (point-min)) (search-forward "\n\n")
11290                     'invisible t))
11291            e)
11292       (goto-char (point-min))
11293       (when (search-forward "\n\n" nil t)
11294         (delete-region (point-min) (1- (point))))
11295       (goto-char (point-min))
11296       (save-excursion
11297         (set-buffer gnus-original-article-buffer)
11298         (goto-char (point-min))
11299         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11300       (insert-buffer-substring gnus-original-article-buffer 1 e)
11301       (let ((gnus-inhibit-hiding t))
11302         (run-hooks 'gnus-article-display-hook))
11303       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11304           (gnus-article-hide-headers)))))
11305
11306 (defun gnus-summary-show-all-headers ()
11307   "Make all header lines visible."
11308   (interactive)
11309   (gnus-set-global-variables)
11310   (gnus-article-show-all-headers))
11311
11312 (defun gnus-summary-toggle-mime (&optional arg)
11313   "Toggle MIME processing.
11314 If ARG is a positive number, turn MIME processing on."
11315   (interactive "P")
11316   (gnus-set-global-variables)
11317   (setq gnus-show-mime
11318         (if (null arg) (not gnus-show-mime)
11319           (> (prefix-numeric-value arg) 0)))
11320   (gnus-summary-select-article t 'force))
11321
11322 (defun gnus-summary-caesar-message (&optional arg)
11323   "Caesar rotate the current article by 13.
11324 The numerical prefix specifies how manu places to rotate each letter
11325 forward."
11326   (interactive "P")
11327   (gnus-set-global-variables)
11328   (gnus-summary-select-article)
11329   (let ((mail-header-separator ""))
11330     (gnus-eval-in-buffer-window
11331      gnus-article-buffer
11332      (save-restriction
11333        (widen)
11334        (let ((start (window-start)))
11335          (news-caesar-buffer-body arg)
11336          (set-window-start (get-buffer-window (current-buffer)) start))))))
11337
11338 (defun gnus-summary-stop-page-breaking ()
11339   "Stop page breaking in the current article."
11340   (interactive)
11341   (gnus-set-global-variables)
11342   (gnus-summary-select-article)
11343   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11344
11345 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11346   "Move the current article to a different newsgroup.
11347 If N is a positive number, move the N next articles.
11348 If N is a negative number, move the N previous articles.
11349 If N is nil and any articles have been marked with the process mark,
11350 move those articles instead.
11351 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11352 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11353 re-spool using this method.
11354
11355 For this function to work, both the current newsgroup and the
11356 newsgroup that you want to move to have to support the `request-move'
11357 and `request-accept' functions."
11358   (interactive "P")
11359   (unless action (setq action 'move))
11360   (gnus-set-global-variables)
11361   ;; Check whether the source group supports the required functions.
11362   (cond ((and (eq action 'move)
11363               (not (gnus-check-backend-function
11364                     'request-move-article gnus-newsgroup-name)))
11365          (error "The current group does not support article moving"))
11366         ((and (eq action 'crosspost)
11367               (not (gnus-check-backend-function
11368                     'request-replace-article gnus-newsgroup-name)))
11369          (error "The current group does not support article editing")))
11370   (let ((articles (gnus-summary-work-articles n))
11371         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11372         (names '((move "Move" "Moving")
11373                  (copy "Copy" "Copying")
11374                  (crosspost "Crosspost" "Crossposting")))
11375         (copy-buf (save-excursion
11376                     (nnheader-set-temp-buffer " *copy article*")))
11377         art-group to-method new-xref article to-groups)
11378     (unless (assq action names)
11379       (error "Unknown action %s" action))
11380     ;; Read the newsgroup name.
11381     (when (and (not to-newsgroup)
11382                (not select-method))
11383       (setq to-newsgroup
11384             (gnus-read-move-group-name
11385              (cadr (assq action names))
11386              (symbol-value (intern (format "gnus-current-%s-group" action)))
11387              articles prefix))
11388       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11389     (setq to-method (or select-method 
11390                         (gnus-find-method-for-group to-newsgroup)))
11391     ;; Check the method we are to move this article to...
11392     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11393         (error "%s does not support article copying" (car to-method)))
11394     (or (gnus-check-server to-method)
11395         (error "Can't open server %s" (car to-method)))
11396     (gnus-message 6 "%s to %s: %s..."
11397                   (caddr (assq action names))
11398                   (or (car select-method) to-newsgroup) articles)
11399     (while articles
11400       (setq article (pop articles))
11401       (setq
11402        art-group
11403        (cond
11404         ;; Move the article.
11405         ((eq action 'move)
11406          (gnus-request-move-article
11407           article                       ; Article to move
11408           gnus-newsgroup-name           ; From newsgrouo
11409           (nth 1 (gnus-find-method-for-group
11410                   gnus-newsgroup-name)) ; Server
11411           (list 'gnus-request-accept-article
11412                 to-newsgroup (list 'quote select-method)
11413                 (not articles))         ; Accept form
11414           (not articles)))              ; Only save nov last time
11415         ;; Copy the article.
11416         ((eq action 'copy)
11417          (save-excursion
11418            (set-buffer copy-buf)
11419            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11420            (gnus-request-accept-article
11421             to-newsgroup select-method (not articles))))
11422         ;; Crosspost the article.
11423         ((eq action 'crosspost)
11424          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11425            (setq new-xref (concat gnus-newsgroup-name ":" article))
11426            (if (and xref (not (string= xref "")))
11427                (progn
11428                  (when (string-match "^Xref: " xref)
11429                    (setq xref (substring xref (match-end 0))))
11430                  (setq new-xref (concat xref " " new-xref)))
11431              (setq new-xref (concat (system-name) " " new-xref)))
11432            (save-excursion
11433              (set-buffer copy-buf)
11434              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11435              (nnheader-replace-header "xref" new-xref)
11436              (gnus-request-accept-article
11437               to-newsgroup select-method (not articles)))))))
11438       (if (not art-group)
11439           (gnus-message 1 "Couldn't %s article %s"
11440                         (cadr (assq action names)) article)
11441         (let* ((entry
11442                 (or
11443                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11444                  (gnus-gethash
11445                   (gnus-group-prefixed-name
11446                    (car art-group)
11447                    (or select-method 
11448                        (gnus-find-method-for-group to-newsgroup)))
11449                   gnus-newsrc-hashtb)))
11450                (info (nth 2 entry))
11451                (to-group (gnus-info-group info)))
11452           ;; Update the group that has been moved to.
11453           (when (and info
11454                      (memq action '(move copy)))
11455             (unless (member to-group to-groups)
11456               (push to-group to-groups))
11457
11458             (unless (memq article gnus-newsgroup-unreads)
11459               (gnus-info-set-read
11460                info (gnus-add-to-range (gnus-info-read info)
11461                                        (list (cdr art-group)))))
11462
11463             ;; Copy any marks over to the new group.
11464             (let ((marks gnus-article-mark-lists)
11465                   (to-article (cdr art-group)))
11466
11467               ;; See whether the article is to be put in the cache.
11468               (when gnus-use-cache
11469                 (gnus-cache-possibly-enter-article
11470                  to-group to-article
11471                  (let ((header (copy-sequence
11472                                 (gnus-summary-article-header article))))
11473                    (mail-header-set-number header to-article)
11474                    header)
11475                  (memq article gnus-newsgroup-marked)
11476                  (memq article gnus-newsgroup-dormant)
11477                  (memq article gnus-newsgroup-unreads)))
11478
11479               (while marks
11480                 (when (memq article (symbol-value
11481                                      (intern (format "gnus-newsgroup-%s"
11482                                                      (caar marks)))))
11483                   ;; If the other group is the same as this group,
11484                   ;; then we have to add the mark to the list.
11485                   (when (equal to-group gnus-newsgroup-name)
11486                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11487                          (cons to-article
11488                                (symbol-value
11489                                 (intern (format "gnus-newsgroup-%s"
11490                                                 (caar marks)))))))
11491                   ;; Copy mark to other group.
11492                   (gnus-add-marked-articles
11493                    to-group (cdar marks) (list to-article) info))
11494                 (setq marks (cdr marks)))))
11495
11496           ;; Update the Xref header in this article to point to
11497           ;; the new crossposted article we have just created.
11498           (when (eq action 'crosspost)
11499             (save-excursion
11500               (set-buffer copy-buf)
11501               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11502               (nnheader-replace-header
11503                "xref" (concat new-xref " " (gnus-group-prefixed-name
11504                                             (car art-group) to-method)
11505                               ":" (cdr art-group)))
11506               (gnus-request-replace-article
11507                article gnus-newsgroup-name (current-buffer)))))
11508
11509         (gnus-summary-goto-subject article)
11510         (when (eq action 'move)
11511           (gnus-summary-mark-article article gnus-canceled-mark)))
11512       (gnus-summary-remove-process-mark article))
11513     ;; Re-activate all groups that have been moved to.
11514     (while to-groups
11515       (gnus-activate-group (pop to-groups)))
11516     
11517     (gnus-kill-buffer copy-buf)
11518     (gnus-summary-position-point)
11519     (gnus-set-mode-line 'summary)))
11520
11521 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11522   "Move the current article to a different newsgroup.
11523 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11524 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11525 re-spool using this method."
11526   (interactive "P")
11527   (gnus-summary-move-article n nil select-method 'copy))
11528
11529 (defun gnus-summary-crosspost-article (&optional n)
11530   "Crosspost the current article to some other group."
11531   (interactive "P")
11532   (gnus-summary-move-article n nil nil 'crosspost))
11533
11534 (defvar gnus-summary-respool-default-method nil
11535   "Default method for respooling an article.  
11536 If nil, use to the current newsgroup method.")
11537
11538 (defun gnus-summary-respool-article (&optional n method)
11539   "Respool the current article.
11540 The article will be squeezed through the mail spooling process again,
11541 which means that it will be put in some mail newsgroup or other
11542 depending on `nnmail-split-methods'.
11543 If N is a positive number, respool the N next articles.
11544 If N is a negative number, respool the N previous articles.
11545 If N is nil and any articles have been marked with the process mark,
11546 respool those articles instead.
11547
11548 Respooling can be done both from mail groups and \"real\" newsgroups.
11549 In the former case, the articles in question will be moved from the
11550 current group into whatever groups they are destined to.  In the
11551 latter case, they will be copied into the relevant groups."
11552   (interactive 
11553    (list current-prefix-arg
11554          (let* ((methods (gnus-methods-using 'respool))
11555                 (methname
11556                  (symbol-name (or gnus-summary-respool-default-method
11557                                   (car (gnus-find-method-for-group
11558                                         gnus-newsgroup-name)))))
11559                 (method
11560                  (gnus-completing-read 
11561                   methname "What backend do you want to use when? "
11562                   methods nil t nil 'gnus-method-history))
11563                 ms)
11564            (cond
11565             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11566              (list (intern method) ""))
11567             ((= 1 (length ms))
11568              (car ms))
11569             (t
11570              (cdr (completing-read 
11571                    "Server name: "
11572                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11573   (gnus-set-global-variables)
11574   (unless method
11575     (error "No method given for respooling"))
11576   (if (assoc (symbol-name
11577               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11578              (gnus-methods-using 'respool))
11579       (gnus-summary-move-article n nil method)
11580     (gnus-summary-copy-article n nil method)))
11581
11582 (defun gnus-summary-import-article (file)
11583   "Import a random file into a mail newsgroup."
11584   (interactive "fImport file: ")
11585   (gnus-set-global-variables)
11586   (let ((group gnus-newsgroup-name)
11587         (now (current-time))
11588         atts lines)
11589     (or (gnus-check-backend-function 'request-accept-article group)
11590         (error "%s does not support article importing" group))
11591     (or (file-readable-p file)
11592         (not (file-regular-p file))
11593         (error "Can't read %s" file))
11594     (save-excursion
11595       (set-buffer (get-buffer-create " *import file*"))
11596       (buffer-disable-undo (current-buffer))
11597       (erase-buffer)
11598       (insert-file-contents file)
11599       (goto-char (point-min))
11600       (unless (nnheader-article-p)
11601         ;; This doesn't look like an article, so we fudge some headers.
11602         (setq atts (file-attributes file)
11603               lines (count-lines (point-min) (point-max)))
11604         (insert "From: " (read-string "From: ") "\n"
11605                 "Subject: " (read-string "Subject: ") "\n"
11606                 "Date: " (timezone-make-date-arpa-standard
11607                           (current-time-string (nth 5 atts))
11608                           (current-time-zone now)
11609                           (current-time-zone now)) "\n"
11610                 "Message-ID: " (gnus-inews-message-id) "\n"
11611                 "Lines: " (int-to-string lines) "\n"
11612                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11613       (gnus-request-accept-article group nil t)
11614       (kill-buffer (current-buffer)))))
11615
11616 (defun gnus-summary-expire-articles (&optional now)
11617   "Expire all articles that are marked as expirable in the current group."
11618   (interactive)
11619   (gnus-set-global-variables)
11620   (when (gnus-check-backend-function
11621          'request-expire-articles gnus-newsgroup-name)
11622     ;; This backend supports expiry.
11623     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11624            (expirable (if total
11625                           (gnus-list-of-read-articles gnus-newsgroup-name)
11626                         (setq gnus-newsgroup-expirable
11627                               (sort gnus-newsgroup-expirable '<))))
11628            (expiry-wait (if now 'immediate
11629                           (gnus-group-get-parameter
11630                            gnus-newsgroup-name 'expiry-wait)))
11631            es)
11632       (when expirable
11633         ;; There are expirable articles in this group, so we run them
11634         ;; through the expiry process.
11635         (gnus-message 6 "Expiring articles...")
11636         ;; The list of articles that weren't expired is returned.
11637         (if expiry-wait
11638             (let ((nnmail-expiry-wait-function nil)
11639                   (nnmail-expiry-wait expiry-wait))
11640               (setq es (gnus-request-expire-articles
11641                         expirable gnus-newsgroup-name)))
11642           (setq es (gnus-request-expire-articles
11643                     expirable gnus-newsgroup-name)))
11644         (or total (setq gnus-newsgroup-expirable es))
11645         ;; We go through the old list of expirable, and mark all
11646         ;; really expired articles as nonexistent.
11647         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11648           (let ((gnus-use-cache nil))
11649             (while expirable
11650               (unless (memq (car expirable) es)
11651                 (when (gnus-data-find (car expirable))
11652                   (gnus-summary-mark-article
11653                    (car expirable) gnus-canceled-mark)))
11654               (setq expirable (cdr expirable)))))
11655         (gnus-message 6 "Expiring articles...done")))))
11656
11657 (defun gnus-summary-expire-articles-now ()
11658   "Expunge all expirable articles in the current group.
11659 This means that *all* articles that are marked as expirable will be
11660 deleted forever, right now."
11661   (interactive)
11662   (gnus-set-global-variables)
11663   (or gnus-expert-user
11664       (gnus-y-or-n-p
11665        "Are you really, really, really sure you want to delete all these messages? ")
11666       (error "Phew!"))
11667   (gnus-summary-expire-articles t))
11668
11669 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11670 (defun gnus-summary-delete-article (&optional n)
11671   "Delete the N next (mail) articles.
11672 This command actually deletes articles.  This is not a marking
11673 command.  The article will disappear forever from your life, never to
11674 return.
11675 If N is negative, delete backwards.
11676 If N is nil and articles have been marked with the process mark,
11677 delete these instead."
11678   (interactive "P")
11679   (gnus-set-global-variables)
11680   (or (gnus-check-backend-function 'request-expire-articles
11681                                    gnus-newsgroup-name)
11682       (error "The current newsgroup does not support article deletion."))
11683   ;; Compute the list of articles to delete.
11684   (let ((articles (gnus-summary-work-articles n))
11685         not-deleted)
11686     (if (and gnus-novice-user
11687              (not (gnus-y-or-n-p
11688                    (format "Do you really want to delete %s forever? "
11689                            (if (> (length articles) 1) 
11690                                (format "these %s articles" (length articles))
11691                              "this article")))))
11692         ()
11693       ;; Delete the articles.
11694       (setq not-deleted (gnus-request-expire-articles
11695                          articles gnus-newsgroup-name 'force))
11696       (while articles
11697         (gnus-summary-remove-process-mark (car articles))
11698         ;; The backend might not have been able to delete the article
11699         ;; after all.
11700         (or (memq (car articles) not-deleted)
11701             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11702         (setq articles (cdr articles))))
11703     (gnus-summary-position-point)
11704     (gnus-set-mode-line 'summary)
11705     not-deleted))
11706
11707 (defun gnus-summary-edit-article (&optional force)
11708   "Enter into a buffer and edit the current article.
11709 This will have permanent effect only in mail groups.
11710 If FORCE is non-nil, allow editing of articles even in read-only
11711 groups."
11712   (interactive "P")
11713   (save-excursion
11714     (set-buffer gnus-summary-buffer)
11715     (gnus-set-global-variables)
11716     (when (and (not force)
11717                (gnus-group-read-only-p))
11718       (error "The current newsgroup does not support article editing."))
11719     (gnus-summary-select-article t nil t)
11720     (gnus-configure-windows 'article)
11721     (select-window (get-buffer-window gnus-article-buffer))
11722     (gnus-message 6 "C-c C-c to end edits")
11723     (setq buffer-read-only nil)
11724     (text-mode)
11725     (use-local-map (copy-keymap (current-local-map)))
11726     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11727     (buffer-enable-undo)
11728     (widen)
11729     (goto-char (point-min))
11730     (search-forward "\n\n" nil t)))
11731
11732 (defun gnus-summary-edit-article-done ()
11733   "Make edits to the current article permanent."
11734   (interactive)
11735   (if (gnus-group-read-only-p)
11736       (progn
11737         (gnus-summary-edit-article-postpone)
11738         (gnus-error
11739          1 "The current newsgroup does not support article editing."))
11740     (let ((buf (format "%s" (buffer-string))))
11741       (erase-buffer)
11742       (insert buf)
11743       (if (not (gnus-request-replace-article
11744                 (cdr gnus-article-current) (car gnus-article-current)
11745                 (current-buffer)))
11746           (error "Couldn't replace article.")
11747         (gnus-article-mode)
11748         (use-local-map gnus-article-mode-map)
11749         (setq buffer-read-only t)
11750         (buffer-disable-undo (current-buffer))
11751         (gnus-configure-windows 'summary)
11752         (gnus-summary-update-article (cdr gnus-article-current))
11753         (when gnus-use-cache
11754           (gnus-cache-update-article 
11755            (cdr gnus-article-current) (car gnus-article-current)))
11756         (when gnus-keep-backlog
11757           (gnus-backlog-remove-article 
11758            (car gnus-article-current) (cdr gnus-article-current))))
11759       (save-excursion
11760         (when (get-buffer gnus-original-article-buffer)
11761           (set-buffer gnus-original-article-buffer)
11762           (setq gnus-original-article nil)))
11763       (setq gnus-article-current nil
11764             gnus-current-article nil)
11765       (run-hooks 'gnus-article-display-hook)
11766       (and (gnus-visual-p 'summary-highlight 'highlight)
11767            (run-hooks 'gnus-visual-mark-article-hook)))))
11768
11769 (defun gnus-summary-edit-article-postpone ()
11770   "Postpone changes to the current article."
11771   (interactive)
11772   (gnus-article-mode)
11773   (use-local-map gnus-article-mode-map)
11774   (setq buffer-read-only t)
11775   (buffer-disable-undo (current-buffer))
11776   (gnus-configure-windows 'summary)
11777   (and (gnus-visual-p 'summary-highlight 'highlight)
11778        (run-hooks 'gnus-visual-mark-article-hook)))
11779
11780 (defun gnus-summary-respool-query ()
11781   "Query where the respool algorithm would put this article."
11782   (interactive)
11783   (gnus-set-global-variables)
11784   (gnus-summary-select-article)
11785   (save-excursion
11786     (set-buffer gnus-article-buffer)
11787     (save-restriction
11788       (goto-char (point-min))
11789       (search-forward "\n\n")
11790       (narrow-to-region (point-min) (point))
11791       (pp-eval-expression
11792        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11793
11794 ;; Summary marking commands.
11795
11796 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11797   "Mark articles which has the same subject as read, and then select the next.
11798 If UNMARK is positive, remove any kind of mark.
11799 If UNMARK is negative, tick articles."
11800   (interactive "P")
11801   (gnus-set-global-variables)
11802   (if unmark
11803       (setq unmark (prefix-numeric-value unmark)))
11804   (let ((count
11805          (gnus-summary-mark-same-subject
11806           (gnus-summary-article-subject) unmark)))
11807     ;; Select next unread article.  If auto-select-same mode, should
11808     ;; select the first unread article.
11809     (gnus-summary-next-article t (and gnus-auto-select-same
11810                                       (gnus-summary-article-subject)))
11811     (gnus-message 7 "%d article%s marked as %s"
11812                   count (if (= count 1) " is" "s are")
11813                   (if unmark "unread" "read"))))
11814
11815 (defun gnus-summary-kill-same-subject (&optional unmark)
11816   "Mark articles which has the same subject as read.
11817 If UNMARK is positive, remove any kind of mark.
11818 If UNMARK is negative, tick articles."
11819   (interactive "P")
11820   (gnus-set-global-variables)
11821   (if unmark
11822       (setq unmark (prefix-numeric-value unmark)))
11823   (let ((count
11824          (gnus-summary-mark-same-subject
11825           (gnus-summary-article-subject) unmark)))
11826     ;; If marked as read, go to next unread subject.
11827     (if (null unmark)
11828         ;; Go to next unread subject.
11829         (gnus-summary-next-subject 1 t))
11830     (gnus-message 7 "%d articles are marked as %s"
11831                   count (if unmark "unread" "read"))))
11832
11833 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11834   "Mark articles with same SUBJECT as read, and return marked number.
11835 If optional argument UNMARK is positive, remove any kinds of marks.
11836 If optional argument UNMARK is negative, mark articles as unread instead."
11837   (let ((count 1))
11838     (save-excursion
11839       (cond
11840        ((null unmark)                   ; Mark as read.
11841         (while (and
11842                 (progn
11843                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11844                   (gnus-summary-show-thread) t)
11845                 (gnus-summary-find-subject subject))
11846           (setq count (1+ count))))
11847        ((> unmark 0)                    ; Tick.
11848         (while (and
11849                 (progn
11850                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11851                   (gnus-summary-show-thread) t)
11852                 (gnus-summary-find-subject subject))
11853           (setq count (1+ count))))
11854        (t                               ; Mark as unread.
11855         (while (and
11856                 (progn
11857                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11858                   (gnus-summary-show-thread) t)
11859                 (gnus-summary-find-subject subject))
11860           (setq count (1+ count)))))
11861       (gnus-set-mode-line 'summary)
11862       ;; Return the number of marked articles.
11863       count)))
11864
11865 (defun gnus-summary-mark-as-processable (n &optional unmark)
11866   "Set the process mark on the next N articles.
11867 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11868 the process mark instead.  The difference between N and the actual
11869 number of articles marked is returned."
11870   (interactive "p")
11871   (gnus-set-global-variables)
11872   (let ((backward (< n 0))
11873         (n (abs n)))
11874     (while (and
11875             (> n 0)
11876             (if unmark
11877                 (gnus-summary-remove-process-mark
11878                  (gnus-summary-article-number))
11879               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11880             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11881       (setq n (1- n)))
11882     (if (/= 0 n) (gnus-message 7 "No more articles"))
11883     (gnus-summary-recenter)
11884     (gnus-summary-position-point)
11885     n))
11886
11887 (defun gnus-summary-unmark-as-processable (n)
11888   "Remove the process mark from the next N articles.
11889 If N is negative, mark backward instead.  The difference between N and
11890 the actual number of articles marked is returned."
11891   (interactive "p")
11892   (gnus-set-global-variables)
11893   (gnus-summary-mark-as-processable n t))
11894
11895 (defun gnus-summary-unmark-all-processable ()
11896   "Remove the process mark from all articles."
11897   (interactive)
11898   (gnus-set-global-variables)
11899   (save-excursion
11900     (while gnus-newsgroup-processable
11901       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11902   (gnus-summary-position-point))
11903
11904 (defun gnus-summary-mark-as-expirable (n)
11905   "Mark N articles forward as expirable.
11906 If N is negative, mark backward instead.  The difference between N and
11907 the actual number of articles marked is returned."
11908   (interactive "p")
11909   (gnus-set-global-variables)
11910   (gnus-summary-mark-forward n gnus-expirable-mark))
11911
11912 (defun gnus-summary-mark-article-as-replied (article)
11913   "Mark ARTICLE replied and update the summary line."
11914   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11915   (let ((buffer-read-only nil))
11916     (when (gnus-summary-goto-subject article)
11917       (gnus-summary-update-secondary-mark article))))
11918
11919 (defun gnus-summary-set-bookmark (article)
11920   "Set a bookmark in current article."
11921   (interactive (list (gnus-summary-article-number)))
11922   (gnus-set-global-variables)
11923   (if (or (not (get-buffer gnus-article-buffer))
11924           (not gnus-current-article)
11925           (not gnus-article-current)
11926           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11927       (error "No current article selected"))
11928   ;; Remove old bookmark, if one exists.
11929   (let ((old (assq article gnus-newsgroup-bookmarks)))
11930     (if old (setq gnus-newsgroup-bookmarks
11931                   (delq old gnus-newsgroup-bookmarks))))
11932   ;; Set the new bookmark, which is on the form
11933   ;; (article-number . line-number-in-body).
11934   (setq gnus-newsgroup-bookmarks
11935         (cons
11936          (cons article
11937                (save-excursion
11938                  (set-buffer gnus-article-buffer)
11939                  (count-lines
11940                   (min (point)
11941                        (save-excursion
11942                          (goto-char (point-min))
11943                          (search-forward "\n\n" nil t)
11944                          (point)))
11945                   (point))))
11946          gnus-newsgroup-bookmarks))
11947   (gnus-message 6 "A bookmark has been added to the current article."))
11948
11949 (defun gnus-summary-remove-bookmark (article)
11950   "Remove the bookmark from the current article."
11951   (interactive (list (gnus-summary-article-number)))
11952   (gnus-set-global-variables)
11953   ;; Remove old bookmark, if one exists.
11954   (let ((old (assq article gnus-newsgroup-bookmarks)))
11955     (if old
11956         (progn
11957           (setq gnus-newsgroup-bookmarks
11958                 (delq old gnus-newsgroup-bookmarks))
11959           (gnus-message 6 "Removed bookmark."))
11960       (gnus-message 6 "No bookmark in current article."))))
11961
11962 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11963 (defun gnus-summary-mark-as-dormant (n)
11964   "Mark N articles forward as dormant.
11965 If N is negative, mark backward instead.  The difference between N and
11966 the actual number of articles marked is returned."
11967   (interactive "p")
11968   (gnus-set-global-variables)
11969   (gnus-summary-mark-forward n gnus-dormant-mark))
11970
11971 (defun gnus-summary-set-process-mark (article)
11972   "Set the process mark on ARTICLE and update the summary line."
11973   (setq gnus-newsgroup-processable
11974         (cons article
11975               (delq article gnus-newsgroup-processable)))
11976   (when (gnus-summary-goto-subject article)
11977     (gnus-summary-show-thread)
11978     (gnus-summary-update-secondary-mark article)))
11979
11980 (defun gnus-summary-remove-process-mark (article)
11981   "Remove the process mark from ARTICLE and update the summary line."
11982   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11983   (when (gnus-summary-goto-subject article)
11984     (gnus-summary-show-thread)
11985     (gnus-summary-update-secondary-mark article)))
11986
11987 (defun gnus-summary-set-saved-mark (article)
11988   "Set the process mark on ARTICLE and update the summary line."
11989   (push article gnus-newsgroup-saved)
11990   (when (gnus-summary-goto-subject article)
11991     (gnus-summary-update-secondary-mark article)))
11992
11993 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11994   "Mark N articles as read forwards.
11995 If N is negative, mark backwards instead.
11996 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11997 marked as unread.
11998 The difference between N and the actual number of articles marked is
11999 returned."
12000   (interactive "p")
12001   (gnus-set-global-variables)
12002   (let ((backward (< n 0))
12003         (gnus-summary-goto-unread
12004          (and gnus-summary-goto-unread
12005               (not (eq gnus-summary-goto-unread 'never))
12006               (not (memq mark (list gnus-unread-mark
12007                                     gnus-ticked-mark gnus-dormant-mark)))))
12008         (n (abs n))
12009         (mark (or mark gnus-del-mark)))
12010     (while (and (> n 0)
12011                 (gnus-summary-mark-article nil mark no-expire)
12012                 (zerop (gnus-summary-next-subject
12013                         (if backward -1 1)
12014                         (and gnus-summary-goto-unread
12015                              (not (eq gnus-summary-goto-unread 'never)))
12016                         t)))
12017       (setq n (1- n)))
12018     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12019     (gnus-summary-recenter)
12020     (gnus-summary-position-point)
12021     (gnus-set-mode-line 'summary)
12022     n))
12023
12024 (defun gnus-summary-mark-article-as-read (mark)
12025   "Mark the current article quickly as read with MARK."
12026   (let ((article (gnus-summary-article-number)))
12027     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12028     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12029     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12030     (setq gnus-newsgroup-reads
12031           (cons (cons article mark) gnus-newsgroup-reads))
12032     ;; Possibly remove from cache, if that is used.
12033     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12034     ;; Allow the backend to change the mark.
12035     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12036     ;; Check for auto-expiry.
12037     (when (and gnus-newsgroup-auto-expire
12038                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12039                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12040                    (= mark gnus-ancient-mark)
12041                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12042       (setq mark gnus-expirable-mark)
12043       (push article gnus-newsgroup-expirable))
12044     ;; Set the mark in the buffer.
12045     (gnus-summary-update-mark mark 'unread)
12046     t))
12047
12048 (defun gnus-summary-mark-article-as-unread (mark)
12049   "Mark the current article quickly as unread with MARK."
12050   (let ((article (gnus-summary-article-number)))
12051     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12052     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12053     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12054     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12055     (cond ((= mark gnus-ticked-mark)
12056            (push article gnus-newsgroup-marked))
12057           ((= mark gnus-dormant-mark)
12058            (push article gnus-newsgroup-dormant))
12059           (t
12060            (push article gnus-newsgroup-unreads)))
12061     (setq gnus-newsgroup-reads
12062           (delq (assq article gnus-newsgroup-reads)
12063                 gnus-newsgroup-reads))
12064
12065     ;; See whether the article is to be put in the cache.
12066     (and gnus-use-cache
12067          (vectorp (gnus-summary-article-header article))
12068          (save-excursion
12069            (gnus-cache-possibly-enter-article
12070             gnus-newsgroup-name article
12071             (gnus-summary-article-header article)
12072             (= mark gnus-ticked-mark)
12073             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12074
12075     ;; Fix the mark.
12076     (gnus-summary-update-mark mark 'unread)
12077     t))
12078
12079 (defun gnus-summary-mark-article (&optional article mark no-expire)
12080   "Mark ARTICLE with MARK.  MARK can be any character.
12081 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12082 `??' (dormant) and `?E' (expirable).
12083 If MARK is nil, then the default character `?D' is used.
12084 If ARTICLE is nil, then the article on the current line will be
12085 marked."
12086   ;; The mark might be a string.
12087   (and (stringp mark)
12088        (setq mark (aref mark 0)))
12089   ;; If no mark is given, then we check auto-expiring.
12090   (and (not no-expire)
12091        gnus-newsgroup-auto-expire
12092        (or (not mark)
12093            (and (numberp mark)
12094                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12095                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12096                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12097        (setq mark gnus-expirable-mark))
12098   (let* ((mark (or mark gnus-del-mark))
12099          (article (or article (gnus-summary-article-number))))
12100     (or article (error "No article on current line"))
12101     (if (or (= mark gnus-unread-mark)
12102             (= mark gnus-ticked-mark)
12103             (= mark gnus-dormant-mark))
12104         (gnus-mark-article-as-unread article mark)
12105       (gnus-mark-article-as-read article mark))
12106
12107     ;; See whether the article is to be put in the cache.
12108     (and gnus-use-cache
12109          (not (= mark gnus-canceled-mark))
12110          (vectorp (gnus-summary-article-header article))
12111          (save-excursion
12112            (gnus-cache-possibly-enter-article
12113             gnus-newsgroup-name article
12114             (gnus-summary-article-header article)
12115             (= mark gnus-ticked-mark)
12116             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12117
12118     (if (gnus-summary-goto-subject article nil t)
12119         (let ((buffer-read-only nil))
12120           (gnus-summary-show-thread)
12121           ;; Fix the mark.
12122           (gnus-summary-update-mark mark 'unread)
12123           t))))
12124
12125 (defun gnus-summary-update-secondary-mark (article)
12126   "Update the secondary (read, process, cache) mark."
12127   (gnus-summary-update-mark
12128    (cond ((memq article gnus-newsgroup-processable)
12129           gnus-process-mark)
12130          ((memq article gnus-newsgroup-cached)
12131           gnus-cached-mark)
12132          ((memq article gnus-newsgroup-replied)
12133           gnus-replied-mark)
12134          ((memq article gnus-newsgroup-saved)
12135           gnus-saved-mark)
12136          (t gnus-unread-mark))
12137    'replied)
12138   (when (gnus-visual-p 'summary-highlight 'highlight)
12139     (run-hooks 'gnus-summary-update-hook))
12140   t)
12141
12142 (defun gnus-summary-update-mark (mark type)
12143   (beginning-of-line)
12144   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12145         (buffer-read-only nil))
12146     (when (and forward
12147                (<= (+ forward (point)) (point-max)))
12148       ;; Go to the right position on the line.
12149       (goto-char (+ forward (point)))
12150       ;; Replace the old mark with the new mark.
12151       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12152       ;; Optionally update the marks by some user rule.
12153       (when (eq type 'unread)
12154         (gnus-data-set-mark
12155          (gnus-data-find (gnus-summary-article-number)) mark)
12156         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12157
12158 (defun gnus-mark-article-as-read (article &optional mark)
12159   "Enter ARTICLE in the pertinent lists and remove it from others."
12160   ;; Make the article expirable.
12161   (let ((mark (or mark gnus-del-mark)))
12162     (if (= mark gnus-expirable-mark)
12163         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12164       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12165     ;; Remove from unread and marked lists.
12166     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12167     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12168     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12169     (push (cons article mark) gnus-newsgroup-reads)
12170     ;; Possibly remove from cache, if that is used.
12171     (when gnus-use-cache
12172       (gnus-cache-enter-remove-article article))))
12173
12174 (defun gnus-mark-article-as-unread (article &optional mark)
12175   "Enter ARTICLE in the pertinent lists and remove it from others."
12176   (let ((mark (or mark gnus-ticked-mark)))
12177     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12178     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12179     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12180     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12181     (cond ((= mark gnus-ticked-mark)
12182            (push article gnus-newsgroup-marked))
12183           ((= mark gnus-dormant-mark)
12184            (push article gnus-newsgroup-dormant))
12185           (t
12186            (push article gnus-newsgroup-unreads)))
12187     (setq gnus-newsgroup-reads
12188           (delq (assq article gnus-newsgroup-reads)
12189                 gnus-newsgroup-reads))))
12190
12191 (defalias 'gnus-summary-mark-as-unread-forward
12192   'gnus-summary-tick-article-forward)
12193 (make-obsolete 'gnus-summary-mark-as-unread-forward
12194                'gnus-summary-tick-article-forward)
12195 (defun gnus-summary-tick-article-forward (n)
12196   "Tick N articles forwards.
12197 If N is negative, tick backwards instead.
12198 The difference between N and the number of articles ticked is returned."
12199   (interactive "p")
12200   (gnus-summary-mark-forward n gnus-ticked-mark))
12201
12202 (defalias 'gnus-summary-mark-as-unread-backward
12203   'gnus-summary-tick-article-backward)
12204 (make-obsolete 'gnus-summary-mark-as-unread-backward
12205                'gnus-summary-tick-article-backward)
12206 (defun gnus-summary-tick-article-backward (n)
12207   "Tick N articles backwards.
12208 The difference between N and the number of articles ticked is returned."
12209   (interactive "p")
12210   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12211
12212 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12213 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12214 (defun gnus-summary-tick-article (&optional article clear-mark)
12215   "Mark current article as unread.
12216 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12217 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12218   (interactive)
12219   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12220                                        gnus-ticked-mark)))
12221
12222 (defun gnus-summary-mark-as-read-forward (n)
12223   "Mark N articles as read forwards.
12224 If N is negative, mark backwards instead.
12225 The difference between N and the actual number of articles marked is
12226 returned."
12227   (interactive "p")
12228   (gnus-summary-mark-forward n gnus-del-mark t))
12229
12230 (defun gnus-summary-mark-as-read-backward (n)
12231   "Mark the N articles as read backwards.
12232 The difference between N and the actual number of articles marked is
12233 returned."
12234   (interactive "p")
12235   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12236
12237 (defun gnus-summary-mark-as-read (&optional article mark)
12238   "Mark current article as read.
12239 ARTICLE specifies the article to be marked as read.
12240 MARK specifies a string to be inserted at the beginning of the line."
12241   (gnus-summary-mark-article article mark))
12242
12243 (defun gnus-summary-clear-mark-forward (n)
12244   "Clear marks from N articles forward.
12245 If N is negative, clear backward instead.
12246 The difference between N and the number of marks cleared is returned."
12247   (interactive "p")
12248   (gnus-summary-mark-forward n gnus-unread-mark))
12249
12250 (defun gnus-summary-clear-mark-backward (n)
12251   "Clear marks from N articles backward.
12252 The difference between N and the number of marks cleared is returned."
12253   (interactive "p")
12254   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12255
12256 (defun gnus-summary-mark-unread-as-read ()
12257   "Intended to be used by `gnus-summary-mark-article-hook'."
12258   (when (memq gnus-current-article gnus-newsgroup-unreads)
12259     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12260
12261 (defun gnus-summary-mark-read-and-unread-as-read ()
12262   "Intended to be used by `gnus-summary-mark-article-hook'."
12263   (let ((mark (gnus-summary-article-mark)))
12264     (when (or (gnus-unread-mark-p mark)
12265               (gnus-read-mark-p mark))
12266       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12267
12268 (defun gnus-summary-mark-region-as-read (point mark all)
12269   "Mark all unread articles between point and mark as read.
12270 If given a prefix, mark all articles between point and mark as read,
12271 even ticked and dormant ones."
12272   (interactive "r\nP")
12273   (save-excursion
12274     (let (article)
12275       (goto-char point)
12276       (beginning-of-line)
12277       (while (and
12278               (< (point) mark)
12279               (progn
12280                 (when (or all
12281                           (memq (setq article (gnus-summary-article-number))
12282                                 gnus-newsgroup-unreads))
12283                   (gnus-summary-mark-article article gnus-del-mark))
12284                 t)
12285               (gnus-summary-find-next))))))
12286
12287 (defun gnus-summary-mark-below (score mark)
12288   "Mark articles with score less than SCORE with MARK."
12289   (interactive "P\ncMark: ")
12290   (gnus-set-global-variables)
12291   (setq score (if score
12292                   (prefix-numeric-value score)
12293                 (or gnus-summary-default-score 0)))
12294   (save-excursion
12295     (set-buffer gnus-summary-buffer)
12296     (goto-char (point-min))
12297     (while 
12298         (progn
12299           (and (< (gnus-summary-article-score) score)
12300                (gnus-summary-mark-article nil mark))
12301           (gnus-summary-find-next)))))
12302
12303 (defun gnus-summary-kill-below (&optional score)
12304   "Mark articles with score below SCORE as read."
12305   (interactive "P")
12306   (gnus-set-global-variables)
12307   (gnus-summary-mark-below score gnus-killed-mark))
12308
12309 (defun gnus-summary-clear-above (&optional score)
12310   "Clear all marks from articles with score above SCORE."
12311   (interactive "P")
12312   (gnus-set-global-variables)
12313   (gnus-summary-mark-above score gnus-unread-mark))
12314
12315 (defun gnus-summary-tick-above (&optional score)
12316   "Tick all articles with score above SCORE."
12317   (interactive "P")
12318   (gnus-set-global-variables)
12319   (gnus-summary-mark-above score gnus-ticked-mark))
12320
12321 (defun gnus-summary-mark-above (score mark)
12322   "Mark articles with score over SCORE with MARK."
12323   (interactive "P\ncMark: ")
12324   (gnus-set-global-variables)
12325   (setq score (if score
12326                   (prefix-numeric-value score)
12327                 (or gnus-summary-default-score 0)))
12328   (save-excursion
12329     (set-buffer gnus-summary-buffer)
12330     (goto-char (point-min))
12331     (while (and (progn
12332                   (if (> (gnus-summary-article-score) score)
12333                       (gnus-summary-mark-article nil mark))
12334                   t)
12335                 (gnus-summary-find-next)))))
12336
12337 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12338 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12339 (defun gnus-summary-limit-include-expunged ()
12340   "Display all the hidden articles that were expunged for low scores."
12341   (interactive)
12342   (gnus-set-global-variables)
12343   (let ((buffer-read-only nil))
12344     (let ((scored gnus-newsgroup-scored)
12345           headers h)
12346       (while scored
12347         (or (gnus-summary-goto-subject (caar scored))
12348             (and (setq h (gnus-summary-article-header (caar scored)))
12349                  (< (cdar scored) gnus-summary-expunge-below)
12350                  (setq headers (cons h headers))))
12351         (setq scored (cdr scored)))
12352       (or headers (error "No expunged articles hidden."))
12353       (goto-char (point-min))
12354       (gnus-summary-prepare-unthreaded (nreverse headers)))
12355     (goto-char (point-min))
12356     (gnus-summary-position-point)))
12357
12358 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12359   "Mark all articles not marked as unread in this newsgroup as read.
12360 If prefix argument ALL is non-nil, all articles are marked as read.
12361 If QUIETLY is non-nil, no questions will be asked.
12362 If TO-HERE is non-nil, it should be a point in the buffer.  All
12363 articles before this point will be marked as read.
12364 The number of articles marked as read is returned."
12365   (interactive "P")
12366   (gnus-set-global-variables)
12367   (prog1
12368       (if (or quietly
12369               (not gnus-interactive-catchup) ;Without confirmation?
12370               gnus-expert-user
12371               (gnus-y-or-n-p
12372                (if all
12373                    "Mark absolutely all articles as read? "
12374                  "Mark all unread articles as read? ")))
12375           (if (and not-mark
12376                    (not gnus-newsgroup-adaptive)
12377                    (not gnus-newsgroup-auto-expire))
12378               (progn
12379                 (when all
12380                   (setq gnus-newsgroup-marked nil
12381                         gnus-newsgroup-dormant nil))
12382                 (setq gnus-newsgroup-unreads nil))
12383             ;; We actually mark all articles as canceled, which we
12384             ;; have to do when using auto-expiry or adaptive scoring.
12385             (gnus-summary-show-all-threads)
12386             (if (gnus-summary-first-subject (not all))
12387                 (while (and
12388                         (if to-here (< (point) to-here) t)
12389                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12390                         (gnus-summary-find-next (not all)))))
12391             (unless to-here
12392               (setq gnus-newsgroup-unreads nil))
12393             (gnus-set-mode-line 'summary)))
12394     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12395       (if (and (not to-here) (eq 'nnvirtual (car method)))
12396           (nnvirtual-catchup-group
12397            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12398     (gnus-summary-position-point)))
12399
12400 (defun gnus-summary-catchup-to-here (&optional all)
12401   "Mark all unticked articles before the current one as read.
12402 If ALL is non-nil, also mark ticked and dormant articles as read."
12403   (interactive "P")
12404   (gnus-set-global-variables)
12405   (save-excursion
12406     (gnus-save-hidden-threads
12407       (let ((beg (point)))
12408         ;; We check that there are unread articles.
12409         (when (or all (gnus-summary-find-prev))
12410           (gnus-summary-catchup all t beg)))))
12411   (gnus-summary-position-point))
12412
12413 (defun gnus-summary-catchup-all (&optional quietly)
12414   "Mark all articles in this newsgroup as read."
12415   (interactive "P")
12416   (gnus-set-global-variables)
12417   (gnus-summary-catchup t quietly))
12418
12419 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12420   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12421 If prefix argument ALL is non-nil, all articles are marked as read."
12422   (interactive "P")
12423   (gnus-set-global-variables)
12424   (gnus-summary-catchup all quietly nil 'fast)
12425   ;; Select next newsgroup or exit.
12426   (if (eq gnus-auto-select-next 'quietly)
12427       (gnus-summary-next-group nil)
12428     (gnus-summary-exit)))
12429
12430 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12431   "Mark all articles in this newsgroup as read, and then exit."
12432   (interactive "P")
12433   (gnus-set-global-variables)
12434   (gnus-summary-catchup-and-exit t quietly))
12435
12436 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12437 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12438   "Mark all articles in this group as read and select the next group.
12439 If given a prefix, mark all articles, unread as well as ticked, as
12440 read."
12441   (interactive "P")
12442   (gnus-set-global-variables)
12443   (save-excursion
12444     (gnus-summary-catchup all))
12445   (gnus-summary-next-article t nil nil t))
12446
12447 ;; Thread-based commands.
12448
12449 (defun gnus-summary-articles-in-thread (&optional article)
12450   "Return a list of all articles in the current thread.
12451 If ARTICLE is non-nil, return all articles in the thread that starts
12452 with that article."
12453   (let* ((article (or article (gnus-summary-article-number)))
12454          (data (gnus-data-find-list article))
12455          (top-level (gnus-data-level (car data)))
12456          (top-subject
12457           (cond ((null gnus-thread-operation-ignore-subject)
12458                  (gnus-simplify-subject-re
12459                   (mail-header-subject (gnus-data-header (car data)))))
12460                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12461                  (gnus-simplify-subject-fuzzy
12462                   (mail-header-subject (gnus-data-header (car data)))))
12463                 (t nil)))
12464          (end-point (save-excursion
12465                       (if (gnus-summary-go-to-next-thread) 
12466                           (point) (point-max))))
12467          articles)
12468     (while (and data
12469                 (< (gnus-data-pos (car data)) end-point))
12470       (when (or (not top-subject)
12471                 (string= top-subject
12472                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12473                              (gnus-simplify-subject-fuzzy
12474                               (mail-header-subject
12475                                (gnus-data-header (car data))))
12476                            (gnus-simplify-subject-re
12477                             (mail-header-subject
12478                              (gnus-data-header (car data)))))))
12479         (push (gnus-data-number (car data)) articles))
12480       (unless (and (setq data (cdr data))
12481                    (> (gnus-data-level (car data)) top-level))
12482         (setq data nil)))
12483     ;; Return the list of articles.
12484     (nreverse articles)))
12485
12486 (defun gnus-summary-rethread-current ()
12487   "Rethread the thread the current article is part of."
12488   (interactive)
12489   (gnus-set-global-variables)
12490   (let* ((gnus-show-threads t)
12491          (article (gnus-summary-article-number))
12492          (id (mail-header-id (gnus-summary-article-header)))
12493          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12494     (unless id
12495       (error "No article on the current line"))
12496     (gnus-rebuild-thread id)
12497     (gnus-summary-goto-subject article)))
12498
12499 (defun gnus-summary-reparent-thread ()
12500   "Make current article child of the marked (or previous) article.
12501
12502 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12503 is non-nil or the Subject: of both articles are the same."
12504   (interactive)
12505   (or (not (gnus-group-read-only-p))
12506       (error "The current newsgroup does not support article editing."))
12507   (or (<= (length gnus-newsgroup-processable) 1)
12508       (error "No more than one article may be marked."))
12509   (save-window-excursion
12510     (let ((gnus-article-buffer " *reparent*")
12511           (current-article (gnus-summary-article-number))
12512           ; first grab the marked article, otherwise one line up.
12513           (parent-article (if (not (null gnus-newsgroup-processable))
12514                               (car gnus-newsgroup-processable)
12515                             (save-excursion
12516                               (if (eq (forward-line -1) 0)
12517                                   (gnus-summary-article-number)
12518                                 (error "Beginning of summary buffer."))))))
12519       (or (not (eq current-article parent-article))
12520           (error "An article may not be self-referential."))
12521       (let ((message-id (mail-header-id 
12522                          (gnus-summary-article-header parent-article))))
12523         (or (and message-id (not (equal message-id "")))
12524             (error "No message-id in desired parent."))
12525         (gnus-summary-select-article t t nil current-article)
12526         (set-buffer gnus-article-buffer)
12527         (setq buffer-read-only nil)
12528         (let ((buf (format "%s" (buffer-string))))
12529           (erase-buffer)
12530           (insert buf))
12531         (goto-char (point-min))
12532         (if (search-forward-regexp "^References: " nil t)
12533             (insert message-id " " )
12534           (insert "References: " message-id "\n"))
12535         (or (gnus-request-replace-article current-article
12536                                           (car gnus-article-current)
12537                                           gnus-article-buffer)
12538             (error "Couldn't replace article."))
12539         (set-buffer gnus-summary-buffer)
12540         (gnus-summary-unmark-all-processable)
12541         (gnus-summary-rethread-current)
12542         (gnus-message 3 "Article %d is now the child of article %d."
12543                       current-article parent-article)))))
12544
12545 (defun gnus-summary-toggle-threads (&optional arg)
12546   "Toggle showing conversation threads.
12547 If ARG is positive number, turn showing conversation threads on."
12548   (interactive "P")
12549   (gnus-set-global-variables)
12550   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12551     (setq gnus-show-threads
12552           (if (null arg) (not gnus-show-threads)
12553             (> (prefix-numeric-value arg) 0)))
12554     (gnus-summary-prepare)
12555     (gnus-summary-goto-subject current)
12556     (gnus-summary-position-point)))
12557
12558 (defun gnus-summary-show-all-threads ()
12559   "Show all threads."
12560   (interactive)
12561   (gnus-set-global-variables)
12562   (save-excursion
12563     (let ((buffer-read-only nil))
12564       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12565   (gnus-summary-position-point))
12566
12567 (defun gnus-summary-show-thread ()
12568   "Show thread subtrees.
12569 Returns nil if no thread was there to be shown."
12570   (interactive)
12571   (gnus-set-global-variables)
12572   (let ((buffer-read-only nil)
12573         (orig (point))
12574         ;; first goto end then to beg, to have point at beg after let
12575         (end (progn (end-of-line) (point)))
12576         (beg (progn (beginning-of-line) (point))))
12577     (prog1
12578         ;; Any hidden lines here?
12579         (search-forward "\r" end t)
12580       (subst-char-in-region beg end ?\^M ?\n t)
12581       (goto-char orig)
12582       (gnus-summary-position-point))))
12583
12584 (defun gnus-summary-hide-all-threads ()
12585   "Hide all thread subtrees."
12586   (interactive)
12587   (gnus-set-global-variables)
12588   (save-excursion
12589     (goto-char (point-min))
12590     (gnus-summary-hide-thread)
12591     (while (zerop (gnus-summary-next-thread 1 t))
12592       (gnus-summary-hide-thread)))
12593   (gnus-summary-position-point))
12594
12595 (defun gnus-summary-hide-thread ()
12596   "Hide thread subtrees.
12597 Returns nil if no threads were there to be hidden."
12598   (interactive)
12599   (gnus-set-global-variables)
12600   (let ((buffer-read-only nil)
12601         (start (point))
12602         (article (gnus-summary-article-number)))
12603     (goto-char start)
12604     ;; Go forward until either the buffer ends or the subthread
12605     ;; ends.
12606     (when (and (not (eobp))
12607                (or (zerop (gnus-summary-next-thread 1 t))
12608                    (goto-char (point-max))))
12609       (prog1
12610           (if (and (> (point) start)
12611                    (search-backward "\n" start t))
12612               (progn
12613                 (subst-char-in-region start (point) ?\n ?\^M)
12614                 (gnus-summary-goto-subject article))
12615             (goto-char start)
12616             nil)
12617         ;;(gnus-summary-position-point)
12618         ))))
12619
12620 (defun gnus-summary-go-to-next-thread (&optional previous)
12621   "Go to the same level (or less) next thread.
12622 If PREVIOUS is non-nil, go to previous thread instead.
12623 Return the article number moved to, or nil if moving was impossible."
12624   (let ((level (gnus-summary-thread-level))
12625         (way (if previous -1 1))
12626         (beg (point)))
12627     (forward-line way)
12628     (while (and (not (eobp))
12629                 (< level (gnus-summary-thread-level)))
12630       (forward-line way))
12631     (if (eobp)
12632         (progn
12633           (goto-char beg)
12634           nil)
12635       (setq beg (point))
12636       (prog1
12637           (gnus-summary-article-number)
12638         (goto-char beg)))))
12639
12640 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12641   "Go to the same level (or less) next thread.
12642 If PREVIOUS is non-nil, go to previous thread instead.
12643 Return the article number moved to, or nil if moving was impossible."
12644   (if (and (eq gnus-summary-make-false-root 'dummy)
12645            (gnus-summary-article-intangible-p))
12646       (let ((beg (point)))
12647         (while (and (zerop (forward-line 1))
12648                     (not (gnus-summary-article-intangible-p))
12649                     (not (zerop (save-excursion 
12650                                   (gnus-summary-thread-level))))))
12651         (if (eobp)
12652             (progn
12653               (goto-char beg)
12654               nil)
12655           (point)))
12656     (let* ((level (gnus-summary-thread-level))
12657            (article (gnus-summary-article-number))
12658            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12659            oart)
12660       (while data
12661         (if (<= (gnus-data-level (car data)) level)
12662             (setq oart (gnus-data-number (car data))
12663                   data nil)
12664           (setq data (cdr data))))
12665       (and oart
12666            (gnus-summary-goto-subject oart)))))
12667
12668 (defun gnus-summary-next-thread (n &optional silent)
12669   "Go to the same level next N'th thread.
12670 If N is negative, search backward instead.
12671 Returns the difference between N and the number of skips actually
12672 done.
12673
12674 If SILENT, don't output messages."
12675   (interactive "p")
12676   (gnus-set-global-variables)
12677   (let ((backward (< n 0))
12678         (n (abs n))
12679         old dum int)
12680     (while (and (> n 0)
12681                 (gnus-summary-go-to-next-thread backward))
12682       (decf n))
12683     (unless silent 
12684       (gnus-summary-position-point))
12685     (when (and (not silent) (/= 0 n))
12686       (gnus-message 7 "No more threads"))
12687     n))
12688
12689 (defun gnus-summary-prev-thread (n)
12690   "Go to the same level previous N'th thread.
12691 Returns the difference between N and the number of skips actually
12692 done."
12693   (interactive "p")
12694   (gnus-set-global-variables)
12695   (gnus-summary-next-thread (- n)))
12696
12697 (defun gnus-summary-go-down-thread ()
12698   "Go down one level in the current thread."
12699   (let ((children (gnus-summary-article-children)))
12700     (and children
12701          (gnus-summary-goto-subject (car children)))))
12702
12703 (defun gnus-summary-go-up-thread ()
12704   "Go up one level in the current thread."
12705   (let ((parent (gnus-summary-article-parent)))
12706     (and parent
12707          (gnus-summary-goto-subject parent))))
12708
12709 (defun gnus-summary-down-thread (n)
12710   "Go down thread N steps.
12711 If N is negative, go up instead.
12712 Returns the difference between N and how many steps down that were
12713 taken."
12714   (interactive "p")
12715   (gnus-set-global-variables)
12716   (let ((up (< n 0))
12717         (n (abs n)))
12718     (while (and (> n 0)
12719                 (if up (gnus-summary-go-up-thread)
12720                   (gnus-summary-go-down-thread)))
12721       (setq n (1- n)))
12722     (gnus-summary-position-point)
12723     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12724     n))
12725
12726 (defun gnus-summary-up-thread (n)
12727   "Go up thread N steps.
12728 If N is negative, go up instead.
12729 Returns the difference between N and how many steps down that were
12730 taken."
12731   (interactive "p")
12732   (gnus-set-global-variables)
12733   (gnus-summary-down-thread (- n)))
12734
12735 (defun gnus-summary-top-thread ()
12736   "Go to the top of the thread."
12737   (interactive)
12738   (gnus-set-global-variables)
12739   (while (gnus-summary-go-up-thread))
12740   (gnus-summary-article-number))
12741
12742 (defun gnus-summary-kill-thread (&optional unmark)
12743   "Mark articles under current thread as read.
12744 If the prefix argument is positive, remove any kinds of marks.
12745 If the prefix argument is negative, tick articles instead."
12746   (interactive "P")
12747   (gnus-set-global-variables)
12748   (if unmark
12749       (setq unmark (prefix-numeric-value unmark)))
12750   (let ((articles (gnus-summary-articles-in-thread)))
12751     (save-excursion
12752       ;; Expand the thread.
12753       (gnus-summary-show-thread)
12754       ;; Mark all the articles.
12755       (while articles
12756         (gnus-summary-goto-subject (car articles))
12757         (cond ((null unmark)
12758                (gnus-summary-mark-article-as-read gnus-killed-mark))
12759               ((> unmark 0)
12760                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12761               (t
12762                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12763         (setq articles (cdr articles))))
12764     ;; Hide killed subtrees.
12765     (and (null unmark)
12766          gnus-thread-hide-killed
12767          (gnus-summary-hide-thread))
12768     ;; If marked as read, go to next unread subject.
12769     (if (null unmark)
12770         ;; Go to next unread subject.
12771         (gnus-summary-next-subject 1 t)))
12772   (gnus-set-mode-line 'summary))
12773
12774 ;; Summary sorting commands
12775
12776 (defun gnus-summary-sort-by-number (&optional reverse)
12777   "Sort summary buffer by article number.
12778 Argument REVERSE means reverse order."
12779   (interactive "P")
12780   (gnus-summary-sort 'number reverse))
12781
12782 (defun gnus-summary-sort-by-author (&optional reverse)
12783   "Sort summary buffer by author name alphabetically.
12784 If case-fold-search is non-nil, case of letters is ignored.
12785 Argument REVERSE means reverse order."
12786   (interactive "P")
12787   (gnus-summary-sort 'author reverse))
12788
12789 (defun gnus-summary-sort-by-subject (&optional reverse)
12790   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12791 If case-fold-search is non-nil, case of letters is ignored.
12792 Argument REVERSE means reverse order."
12793   (interactive "P")
12794   (gnus-summary-sort 'subject reverse))
12795
12796 (defun gnus-summary-sort-by-date (&optional reverse)
12797   "Sort summary buffer by date.
12798 Argument REVERSE means reverse order."
12799   (interactive "P")
12800   (gnus-summary-sort 'date reverse))
12801
12802 (defun gnus-summary-sort-by-score (&optional reverse)
12803   "Sort summary buffer by score.
12804 Argument REVERSE means reverse order."
12805   (interactive "P")
12806   (gnus-summary-sort 'score reverse))
12807
12808 (defun gnus-summary-sort (predicate reverse)
12809   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12810   (gnus-set-global-variables)
12811   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12812          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12813          (gnus-thread-sort-functions
12814           (list
12815            (if (not reverse)
12816                thread
12817              `(lambda (t1 t2)
12818                 (,thread t2 t1)))))
12819          (gnus-article-sort-functions
12820           (list
12821            (if (not reverse)
12822                article
12823              `(lambda (t1 t2)
12824                 (,article t2 t1)))))
12825          (buffer-read-only)
12826          (gnus-summary-prepare-hook nil))
12827     ;; We do the sorting by regenerating the threads.
12828     (gnus-summary-prepare)
12829     ;; Hide subthreads if needed.
12830     (when (and gnus-show-threads gnus-thread-hide-subtree)
12831       (gnus-summary-hide-all-threads)))
12832   ;; If in async mode, we send some info to the backend.
12833   (when gnus-newsgroup-async
12834     (gnus-request-asynchronous
12835      gnus-newsgroup-name gnus-newsgroup-data)))
12836
12837 (defun gnus-sortable-date (date)
12838   "Make sortable string by string-lessp from DATE.
12839 Timezone package is used."
12840   (condition-case ()
12841       (progn
12842         (setq date (inline (timezone-fix-time 
12843                             date nil 
12844                             (aref (inline (timezone-parse-date date)) 4))))
12845         (inline
12846           (timezone-make-sortable-date
12847            (aref date 0) (aref date 1) (aref date 2)
12848            (inline
12849              (timezone-make-time-string
12850               (aref date 3) (aref date 4) (aref date 5))))))
12851     (error "")))
12852   
12853 ;; Summary saving commands.
12854
12855 (defun gnus-summary-save-article (&optional n not-saved)
12856   "Save the current article using the default saver function.
12857 If N is a positive number, save the N next articles.
12858 If N is a negative number, save the N previous articles.
12859 If N is nil and any articles have been marked with the process mark,
12860 save those articles instead.
12861 The variable `gnus-default-article-saver' specifies the saver function."
12862   (interactive "P")
12863   (gnus-set-global-variables)
12864   (let ((articles (gnus-summary-work-articles n))
12865         file header article)
12866     (while articles
12867       (setq header (gnus-summary-article-header
12868                     (setq article (pop articles))))
12869       (if (not (vectorp header))
12870           ;; This is a pseudo-article.
12871           (if (assq 'name header)
12872               (gnus-copy-file (cdr (assq 'name header)))
12873             (gnus-message 1 "Article %d is unsaveable" article))
12874         ;; This is a real article.
12875         (save-window-excursion
12876           (gnus-summary-select-article t nil nil article))
12877         (unless gnus-save-all-headers
12878           ;; Remove headers accoring to `gnus-saved-headers'.
12879           (let ((gnus-visible-headers
12880                  (or gnus-saved-headers gnus-visible-headers)))
12881             (gnus-article-hide-headers nil t)))
12882         ;; Remove any X-Gnus lines.
12883         (save-excursion
12884           (set-buffer gnus-article-buffer)
12885           (save-restriction
12886             (let ((buffer-read-only nil))
12887               (nnheader-narrow-to-headers)
12888               (while (re-search-forward "^X-Gnus" nil t)
12889                 (gnus-delete-line)))))
12890         (save-window-excursion
12891           (if (not gnus-default-article-saver)
12892               (error "No default saver is defined.")
12893             (setq file (funcall
12894                         gnus-default-article-saver
12895                         (cond
12896                          ((not gnus-prompt-before-saving)
12897                           'default)
12898                          ((eq gnus-prompt-before-saving 'always)
12899                           nil)
12900                          (t file))))))
12901         (gnus-summary-remove-process-mark article)
12902         (unless not-saved
12903           (gnus-summary-set-saved-mark article))))
12904     (gnus-summary-position-point)
12905     n))
12906
12907 (defun gnus-summary-pipe-output (&optional arg)
12908   "Pipe the current article to a subprocess.
12909 If N is a positive number, pipe the N next articles.
12910 If N is a negative number, pipe the N previous articles.
12911 If N is nil and any articles have been marked with the process mark,
12912 pipe those articles instead."
12913   (interactive "P")
12914   (gnus-set-global-variables)
12915   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12916     (gnus-summary-save-article arg t))
12917   (gnus-configure-windows 'pipe))
12918
12919 (defun gnus-summary-save-article-mail (&optional arg)
12920   "Append the current article to an mail file.
12921 If N is a positive number, save the N next articles.
12922 If N is a negative number, save the N previous articles.
12923 If N is nil and any articles have been marked with the process mark,
12924 save those articles instead."
12925   (interactive "P")
12926   (gnus-set-global-variables)
12927   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12928     (gnus-summary-save-article arg)))
12929
12930 (defun gnus-summary-save-article-rmail (&optional arg)
12931   "Append the current article to an rmail file.
12932 If N is a positive number, save the N next articles.
12933 If N is a negative number, save the N previous articles.
12934 If N is nil and any articles have been marked with the process mark,
12935 save those articles instead."
12936   (interactive "P")
12937   (gnus-set-global-variables)
12938   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12939     (gnus-summary-save-article arg)))
12940
12941 (defun gnus-summary-save-article-file (&optional arg)
12942   "Append the current article to a file.
12943 If N is a positive number, save the N next articles.
12944 If N is a negative number, save the N previous articles.
12945 If N is nil and any articles have been marked with the process mark,
12946 save those articles instead."
12947   (interactive "P")
12948   (gnus-set-global-variables)
12949   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12950     (gnus-summary-save-article arg)))
12951
12952 (defun gnus-summary-save-article-body-file (&optional arg)
12953   "Append the current article body to a file.
12954 If N is a positive number, save the N next articles.
12955 If N is a negative number, save the N previous articles.
12956 If N is nil and any articles have been marked with the process mark,
12957 save those articles instead."
12958   (interactive "P")
12959   (gnus-set-global-variables)
12960   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12961     (gnus-summary-save-article arg)))
12962
12963 (defun gnus-get-split-value (methods)
12964   "Return a value based on the split METHODS."
12965   (let (split-name method result match)
12966     (when methods
12967       (save-excursion
12968         (set-buffer gnus-original-article-buffer)
12969         (save-restriction
12970           (nnheader-narrow-to-headers)
12971           (while methods
12972             (goto-char (point-min))
12973             (setq method (pop methods))
12974             (setq match (car method))
12975             (when (cond
12976                    ((stringp match)
12977                     ;; Regular expression.
12978                     (condition-case ()
12979                         (re-search-forward match nil t)
12980                       (error nil)))
12981                    ((gnus-functionp match)
12982                     ;; Function.
12983                     (save-restriction
12984                       (widen)
12985                       (setq result (funcall match gnus-newsgroup-name))))
12986                    ((consp match)
12987                     ;; Form.
12988                     (save-restriction
12989                       (widen)
12990                       (setq result (eval match)))))
12991               (setq split-name (append (cdr method) split-name))
12992               (cond ((stringp result)
12993                      (push result split-name))
12994                     ((consp result)
12995                      (setq split-name (append result split-name)))))))))
12996     split-name))
12997
12998 (defun gnus-read-move-group-name (prompt default articles prefix)
12999   "Read a group name."
13000   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13001          (minibuffer-confirm-incomplete nil) ; XEmacs
13002          group-map
13003          (dum (mapatoms
13004                (lambda (g) 
13005                  (and (boundp g)
13006                       (symbol-name g)
13007                       (memq 'respool
13008                             (assoc (symbol-name
13009                                     (car (gnus-find-method-for-group
13010                                           (symbol-name g))))
13011                                    gnus-valid-select-methods))
13012                       (push (list (symbol-name g)) group-map)))
13013                gnus-active-hashtb))
13014          (prom
13015           (format "%s %s to:"
13016                   prompt
13017                   (if (> (length articles) 1)
13018                       (format "these %d articles" (length articles))
13019                     "this article")))
13020          (to-newsgroup
13021           (cond
13022            ((null split-name)
13023             (gnus-completing-read default prom
13024                                   group-map nil nil prefix
13025                                   'gnus-group-history))
13026            ((= 1 (length split-name))
13027             (gnus-completing-read (car split-name) prom group-map
13028                                   nil nil nil
13029                                   'gnus-group-history))
13030            (t
13031             (gnus-completing-read nil prom 
13032                                   (mapcar (lambda (el) (list el))
13033                                           (nreverse split-name))
13034                                   nil nil nil
13035                                   'gnus-group-history)))))
13036     (when to-newsgroup
13037       (if (or (string= to-newsgroup "")
13038               (string= to-newsgroup prefix))
13039           (setq to-newsgroup (or default "")))
13040       (or (gnus-active to-newsgroup)
13041           (gnus-activate-group to-newsgroup)
13042           (error "No such group: %s" to-newsgroup)))
13043     to-newsgroup))
13044
13045 (defun gnus-read-save-file-name (prompt default-name)
13046   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13047          (file
13048           ;; Let the split methods have their say.
13049           (cond
13050            ;; No split name was found.
13051            ((null split-name)
13052             (read-file-name
13053              (concat prompt " (default "
13054                      (file-name-nondirectory default-name) ") ")
13055              (file-name-directory default-name)
13056              default-name))
13057            ;; A single split name was found
13058            ((= 1 (length split-name))
13059             (read-file-name
13060              (concat prompt " (default " (car split-name) ") ")
13061              gnus-article-save-directory
13062              (concat gnus-article-save-directory (car split-name))))
13063            ;; A list of splits was found.
13064            (t
13065             (setq split-name (nreverse split-name))
13066             (let (result)
13067               (let ((file-name-history (nconc split-name file-name-history)))
13068                 (setq result
13069                       (read-file-name
13070                        (concat prompt " (`M-p' for defaults) ")
13071                        gnus-article-save-directory
13072                        (car split-name))))
13073               (car (push result file-name-history)))))))
13074     ;; If we have read a directory, we append the default file name.
13075     (when (file-directory-p file)
13076       (setq file (concat (file-name-as-directory file)
13077                          (file-name-nondirectory default-name))))
13078     ;; Possibly translate some charaters.
13079     (nnheader-translate-file-chars file)))
13080
13081 (defun gnus-article-archive-name (group)
13082   "Return the first instance of an \"Archive-name\" in the current buffer."
13083   (let ((case-fold-search t))
13084     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13085       (match-string 1))))
13086
13087 (defun gnus-summary-save-in-rmail (&optional filename)
13088   "Append this article to Rmail file.
13089 Optional argument FILENAME specifies file name.
13090 Directory to save to is default to `gnus-article-save-directory'."
13091   (interactive)
13092   (gnus-set-global-variables)
13093   (let ((default-name
13094           (funcall gnus-rmail-save-name gnus-newsgroup-name
13095                    gnus-current-headers gnus-newsgroup-last-rmail)))
13096     (setq filename
13097           (cond ((eq filename 'default)
13098                  default-name)
13099                 (filename filename)
13100                 (t (gnus-read-save-file-name
13101                     "Save in rmail file:" default-name))))
13102     (gnus-make-directory (file-name-directory filename))
13103     (gnus-eval-in-buffer-window
13104      gnus-original-article-buffer
13105      (save-excursion
13106        (save-restriction
13107          (widen)
13108          (gnus-output-to-rmail filename))))
13109     ;; Remember the directory name to save articles
13110     (setq gnus-newsgroup-last-rmail filename)))
13111
13112 (defun gnus-summary-save-in-mail (&optional filename)
13113   "Append this article to Unix mail file.
13114 Optional argument FILENAME specifies file name.
13115 Directory to save to is default to `gnus-article-save-directory'."
13116   (interactive)
13117   (gnus-set-global-variables)
13118   (let ((default-name
13119           (funcall gnus-mail-save-name gnus-newsgroup-name
13120                    gnus-current-headers gnus-newsgroup-last-mail)))
13121     (setq filename
13122           (cond ((eq filename 'default)
13123                  default-name)
13124                 (filename filename)
13125                 (t (gnus-read-save-file-name
13126                     "Save in Unix mail file:" default-name))))
13127     (setq filename
13128           (expand-file-name filename
13129                             (and default-name
13130                                  (file-name-directory default-name))))
13131     (gnus-make-directory (file-name-directory filename))
13132     (gnus-eval-in-buffer-window
13133      gnus-original-article-buffer
13134      (save-excursion
13135        (save-restriction
13136          (widen)
13137          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13138              (gnus-output-to-rmail filename)
13139            (let ((mail-use-rfc822 t))
13140              (rmail-output filename 1 t t))))))
13141     ;; Remember the directory name to save articles.
13142     (setq gnus-newsgroup-last-mail filename)))
13143
13144 (defun gnus-summary-save-in-file (&optional filename)
13145   "Append this article to file.
13146 Optional argument FILENAME specifies file name.
13147 Directory to save to is default to `gnus-article-save-directory'."
13148   (interactive)
13149   (gnus-set-global-variables)
13150   (let ((default-name
13151           (funcall gnus-file-save-name gnus-newsgroup-name
13152                    gnus-current-headers gnus-newsgroup-last-file)))
13153     (setq filename
13154           (cond ((eq filename 'default)
13155                  default-name)
13156                 (filename filename)
13157                 (t (gnus-read-save-file-name
13158                     "Save in file:" default-name))))
13159     (gnus-make-directory (file-name-directory filename))
13160     (gnus-eval-in-buffer-window
13161      gnus-original-article-buffer
13162      (save-excursion
13163        (save-restriction
13164          (widen)
13165          (gnus-output-to-file filename))))
13166     ;; Remember the directory name to save articles.
13167     (setq gnus-newsgroup-last-file filename)))
13168
13169 (defun gnus-summary-save-body-in-file (&optional filename)
13170   "Append this article body to a file.
13171 Optional argument FILENAME specifies file name.
13172 The directory to save in defaults to `gnus-article-save-directory'."
13173   (interactive)
13174   (gnus-set-global-variables)
13175   (let ((default-name
13176           (funcall gnus-file-save-name gnus-newsgroup-name
13177                    gnus-current-headers gnus-newsgroup-last-file)))
13178     (setq filename
13179           (cond ((eq filename 'default)
13180                  default-name)
13181                 (filename filename)
13182                 (t (gnus-read-save-file-name
13183                     "Save body in file:" default-name))))
13184     (gnus-make-directory (file-name-directory filename))
13185     (gnus-eval-in-buffer-window
13186      gnus-article-buffer
13187      (save-excursion
13188        (save-restriction
13189          (widen)
13190          (goto-char (point-min))
13191          (and (search-forward "\n\n" nil t)
13192               (narrow-to-region (point) (point-max)))
13193          (gnus-output-to-file filename))))
13194     ;; Remember the directory name to save articles.
13195     (setq gnus-newsgroup-last-file filename)))
13196
13197 (defun gnus-summary-save-in-pipe (&optional command)
13198   "Pipe this article to subprocess."
13199   (interactive)
13200   (gnus-set-global-variables)
13201   (setq command
13202         (cond ((eq command 'default)
13203                gnus-last-shell-command)
13204               (command command)
13205               (t (read-string "Shell command on article: "
13206                               gnus-last-shell-command))))
13207   (if (string-equal command "")
13208       (setq command gnus-last-shell-command))
13209   (gnus-eval-in-buffer-window
13210    gnus-article-buffer
13211    (save-restriction
13212      (widen)
13213      (shell-command-on-region (point-min) (point-max) command nil)))
13214   (setq gnus-last-shell-command command))
13215
13216 ;; Summary extract commands
13217
13218 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13219   (let ((buffer-read-only nil)
13220         (article (gnus-summary-article-number))
13221         after-article b e)
13222     (or (gnus-summary-goto-subject article)
13223         (error (format "No such article: %d" article)))
13224     (gnus-summary-position-point)
13225     ;; If all commands are to be bunched up on one line, we collect
13226     ;; them here.
13227     (if gnus-view-pseudos-separately
13228         ()
13229       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13230             files action)
13231         (while ps
13232           (setq action (cdr (assq 'action (car ps))))
13233           (setq files (list (cdr (assq 'name (car ps)))))
13234           (while (and ps (cdr ps)
13235                       (string= (or action "1")
13236                                (or (cdr (assq 'action (cadr ps))) "2")))
13237             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13238             (setcdr ps (cddr ps)))
13239           (if (not files)
13240               ()
13241             (if (not (string-match "%s" action))
13242                 (setq files (cons " " files)))
13243             (setq files (cons " " files))
13244             (and (assq 'execute (car ps))
13245                  (setcdr (assq 'execute (car ps))
13246                          (funcall (if (string-match "%s" action)
13247                                       'format 'concat)
13248                                   action
13249                                   (mapconcat (lambda (f) f) files " ")))))
13250           (setq ps (cdr ps)))))
13251     (if (and gnus-view-pseudos (not not-view))
13252         (while pslist
13253           (and (assq 'execute (car pslist))
13254                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13255                                      (eq gnus-view-pseudos 'not-confirm)))
13256           (setq pslist (cdr pslist)))
13257       (save-excursion
13258         (while pslist
13259           (setq after-article (or (cdr (assq 'article (car pslist)))
13260                                   (gnus-summary-article-number)))
13261           (gnus-summary-goto-subject after-article)
13262           (forward-line 1)
13263           (setq b (point))
13264           (insert "    " (file-name-nondirectory
13265                                 (cdr (assq 'name (car pslist))))
13266                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13267           (setq e (point))
13268           (forward-line -1)             ; back to `b'
13269           (gnus-add-text-properties
13270            b (1- e) (list 'gnus-number gnus-reffed-article-number
13271                           gnus-mouse-face-prop gnus-mouse-face))
13272           (gnus-data-enter
13273            after-article gnus-reffed-article-number
13274            gnus-unread-mark b (car pslist) 0 (- e b))
13275           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13276           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13277           (setq pslist (cdr pslist)))))))
13278
13279 (defun gnus-pseudos< (p1 p2)
13280   (let ((c1 (cdr (assq 'action p1)))
13281         (c2 (cdr (assq 'action p2))))
13282     (and c1 c2 (string< c1 c2))))
13283
13284 (defun gnus-request-pseudo-article (props)
13285   (cond ((assq 'execute props)
13286          (gnus-execute-command (cdr (assq 'execute props)))))
13287   (let ((gnus-current-article (gnus-summary-article-number)))
13288     (run-hooks 'gnus-mark-article-hook)))
13289
13290 (defun gnus-execute-command (command &optional automatic)
13291   (save-excursion
13292     (gnus-article-setup-buffer)
13293     (set-buffer gnus-article-buffer)
13294     (setq buffer-read-only nil)
13295     (let ((command (if automatic command (read-string "Command: " command)))
13296           ;; Just binding this here doesn't help, because there might
13297           ;; be output from the process after exiting the scope of 
13298           ;; this `let'.
13299           ;; (buffer-read-only nil)
13300           )
13301       (erase-buffer)
13302       (insert "$ " command "\n\n")
13303       (if gnus-view-pseudo-asynchronously
13304           (start-process "gnus-execute" nil "sh" "-c" command)
13305         (call-process "sh" nil t nil "-c" command)))))
13306
13307 (defun gnus-copy-file (file &optional to)
13308   "Copy FILE to TO."
13309   (interactive
13310    (list (read-file-name "Copy file: " default-directory)
13311          (read-file-name "Copy file to: " default-directory)))
13312   (gnus-set-global-variables)
13313   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13314   (and (file-directory-p to)
13315        (setq to (concat (file-name-as-directory to)
13316                         (file-name-nondirectory file))))
13317   (copy-file file to))
13318
13319 ;; Summary kill commands.
13320
13321 (defun gnus-summary-edit-global-kill (article)
13322   "Edit the \"global\" kill file."
13323   (interactive (list (gnus-summary-article-number)))
13324   (gnus-set-global-variables)
13325   (gnus-group-edit-global-kill article))
13326
13327 (defun gnus-summary-edit-local-kill ()
13328   "Edit a local kill file applied to the current newsgroup."
13329   (interactive)
13330   (gnus-set-global-variables)
13331   (setq gnus-current-headers (gnus-summary-article-header))
13332   (gnus-set-global-variables)
13333   (gnus-group-edit-local-kill
13334    (gnus-summary-article-number) gnus-newsgroup-name))
13335
13336 \f
13337 ;;;
13338 ;;; Gnus article mode
13339 ;;;
13340
13341 (put 'gnus-article-mode 'mode-class 'special)
13342
13343 (if gnus-article-mode-map
13344     nil
13345   (setq gnus-article-mode-map (make-keymap))
13346   (suppress-keymap gnus-article-mode-map)
13347
13348   (gnus-define-keys gnus-article-mode-map
13349     " " gnus-article-goto-next-page
13350     "\177" gnus-article-goto-prev-page
13351     [delete] gnus-article-goto-prev-page
13352     "\C-c^" gnus-article-refer-article
13353     "h" gnus-article-show-summary
13354     "s" gnus-article-show-summary
13355     "\C-c\C-m" gnus-article-mail
13356     "?" gnus-article-describe-briefly
13357     gnus-mouse-2 gnus-article-push-button
13358     "\r" gnus-article-press-button
13359     "\t" gnus-article-next-button
13360     "\M-\t" gnus-article-prev-button
13361     "\C-c\C-b" gnus-bug)
13362
13363   (substitute-key-definition
13364    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13365
13366 (defun gnus-article-mode ()
13367   "Major mode for displaying an article.
13368
13369 All normal editing commands are switched off.
13370
13371 The following commands are available:
13372
13373 \\<gnus-article-mode-map>
13374 \\[gnus-article-next-page]\t Scroll the article one page forwards
13375 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13376 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13377 \\[gnus-article-show-summary]\t Display the summary buffer
13378 \\[gnus-article-mail]\t Send a reply to the address near point
13379 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13380 \\[gnus-info-find-node]\t Go to the Gnus info node"
13381   (interactive)
13382   (when (and menu-bar-mode
13383              (gnus-visual-p 'article-menu 'menu))
13384     (gnus-article-make-menu-bar))
13385   (kill-all-local-variables)
13386   (gnus-simplify-mode-line)
13387   (setq mode-name "Article")
13388   (setq major-mode 'gnus-article-mode)
13389   (make-local-variable 'minor-mode-alist)
13390   (or (assq 'gnus-show-mime minor-mode-alist)
13391       (setq minor-mode-alist
13392             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13393   (use-local-map gnus-article-mode-map)
13394   (make-local-variable 'page-delimiter)
13395   (setq page-delimiter gnus-page-delimiter)
13396   (buffer-disable-undo (current-buffer))
13397   (setq buffer-read-only t)             ;Disable modification
13398   (run-hooks 'gnus-article-mode-hook))
13399
13400 (defun gnus-article-setup-buffer ()
13401   "Initialize the article buffer."
13402   (let* ((name (if gnus-single-article-buffer "*Article*"
13403                  (concat "*Article " gnus-newsgroup-name "*")))
13404          (original
13405           (progn (string-match "\\*Article" name)
13406                  (concat " *Original Article"
13407                          (substring name (match-end 0))))))
13408     (setq gnus-article-buffer name)
13409     (setq gnus-original-article-buffer original)
13410     ;; This might be a variable local to the summary buffer.
13411     (unless gnus-single-article-buffer
13412       (save-excursion
13413         (set-buffer gnus-summary-buffer)
13414         (setq gnus-article-buffer name)
13415         (setq gnus-original-article-buffer original)
13416         (gnus-set-global-variables))
13417       (make-local-variable 'gnus-summary-buffer))
13418     ;; Init original article buffer.
13419     (save-excursion
13420       (set-buffer (get-buffer-create gnus-original-article-buffer))
13421       (buffer-disable-undo (current-buffer))
13422       (setq major-mode 'gnus-original-article-mode)
13423       (make-local-variable 'gnus-original-article))
13424     (if (get-buffer name)
13425         (save-excursion
13426           (set-buffer name)
13427           (buffer-disable-undo (current-buffer))
13428           (setq buffer-read-only t)
13429           (gnus-add-current-to-buffer-list)
13430           (or (eq major-mode 'gnus-article-mode)
13431               (gnus-article-mode))
13432           (current-buffer))
13433       (save-excursion
13434         (set-buffer (get-buffer-create name))
13435         (gnus-add-current-to-buffer-list)
13436         (gnus-article-mode)
13437         (current-buffer)))))
13438
13439 ;; Set article window start at LINE, where LINE is the number of lines
13440 ;; from the head of the article.
13441 (defun gnus-article-set-window-start (&optional line)
13442   (set-window-start
13443    (get-buffer-window gnus-article-buffer t)
13444    (save-excursion
13445      (set-buffer gnus-article-buffer)
13446      (goto-char (point-min))
13447      (if (not line)
13448          (point-min)
13449        (gnus-message 6 "Moved to bookmark")
13450        (search-forward "\n\n" nil t)
13451        (forward-line line)
13452        (point)))))
13453
13454 (defun gnus-kill-all-overlays ()
13455   "Delete all overlays in the current buffer."
13456   (when (fboundp 'overlay-lists)
13457     (let* ((overlayss (overlay-lists))
13458            (buffer-read-only nil)
13459            (overlays (nconc (car overlayss) (cdr overlayss))))
13460       (while overlays
13461         (delete-overlay (pop overlays))))))
13462
13463 (defun gnus-request-article-this-buffer (article group)
13464   "Get an article and insert it into this buffer."
13465   (let (do-update-line)
13466     (prog1
13467         (save-excursion
13468           (erase-buffer)
13469           (gnus-kill-all-overlays)
13470           (setq group (or group gnus-newsgroup-name))
13471
13472           ;; Open server if it has closed.
13473           (gnus-check-server (gnus-find-method-for-group group))
13474
13475           ;; Using `gnus-request-article' directly will insert the article into
13476           ;; `nntp-server-buffer' - so we'll save some time by not having to
13477           ;; copy it from the server buffer into the article buffer.
13478
13479           ;; We only request an article by message-id when we do not have the
13480           ;; headers for it, so we'll have to get those.
13481           (when (stringp article)
13482             (let ((gnus-override-method gnus-refer-article-method))
13483               (gnus-read-header article)))
13484
13485           ;; If the article number is negative, that means that this article
13486           ;; doesn't belong in this newsgroup (possibly), so we find its
13487           ;; message-id and request it by id instead of number.
13488           (when (and (numberp article)
13489                      gnus-summary-buffer
13490                      (get-buffer gnus-summary-buffer)
13491                      (buffer-name (get-buffer gnus-summary-buffer)))
13492             (save-excursion
13493               (set-buffer gnus-summary-buffer)
13494               (let ((header (gnus-summary-article-header article)))
13495                 (if (< article 0)
13496                     (cond 
13497                      ((memq article gnus-newsgroup-sparse)
13498                       ;; This is a sparse gap article.
13499                       (setq do-update-line article)
13500                       (setq article (mail-header-id header))
13501                       (let ((gnus-override-method gnus-refer-article-method))
13502                         (gnus-read-header article))
13503                       (setq gnus-newsgroup-sparse
13504                             (delq article gnus-newsgroup-sparse)))
13505                      ((vectorp header)
13506                       ;; It's a real article.
13507                       (setq article (mail-header-id header)))
13508                      (t
13509                       ;; It is an extracted pseudo-article.
13510                       (setq article 'pseudo)
13511                       (gnus-request-pseudo-article header))))
13512                 
13513                 (let ((method (gnus-find-method-for-group 
13514                                gnus-newsgroup-name)))
13515                   (if (not (eq (car method) 'nneething))
13516                       ()
13517                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13518                                        (mail-header-subject header))))
13519                       (if (file-directory-p dir)
13520                           (progn
13521                             (setq article 'nneething)
13522                             (gnus-group-enter-directory dir)))))))))
13523
13524           (cond
13525            ;; Refuse to select canceled articles.
13526            ((and (numberp article)
13527                  gnus-summary-buffer
13528                  (get-buffer gnus-summary-buffer)
13529                  (buffer-name (get-buffer gnus-summary-buffer))
13530                  (eq (cdr (save-excursion
13531                             (set-buffer gnus-summary-buffer)
13532                             (assq article gnus-newsgroup-reads)))
13533                      gnus-canceled-mark))
13534             nil)
13535            ;; We first check `gnus-original-article-buffer'.
13536            ((and (get-buffer gnus-original-article-buffer)
13537                  (numberp article)
13538                  (save-excursion
13539                    (set-buffer gnus-original-article-buffer)
13540                    (and (equal (car gnus-original-article) group)
13541                         (eq (cdr gnus-original-article) article))))
13542             (insert-buffer-substring gnus-original-article-buffer)
13543             'article)
13544            ;; Check the backlog.
13545            ((and gnus-keep-backlog
13546                  (gnus-backlog-request-article group article (current-buffer)))
13547             'article)
13548            ;; Check the cache.
13549            ((and gnus-use-cache
13550                  (numberp article)
13551                  (gnus-cache-request-article article group))
13552             'article)
13553            ;; Get the article and put into the article buffer.
13554            ((or (stringp article) (numberp article))
13555             (let ((gnus-override-method
13556                    (and (stringp article) gnus-refer-article-method))
13557                   (buffer-read-only nil))
13558               (erase-buffer)
13559               (gnus-kill-all-overlays)
13560               (if (gnus-request-article article group (current-buffer))
13561                   (progn
13562                     (and gnus-keep-backlog
13563                          (numberp article)
13564                          (gnus-backlog-enter-article
13565                           group article (current-buffer)))
13566                     'article))))
13567            ;; It was a pseudo.
13568            (t article)))
13569
13570       ;; Take the article from the original article buffer
13571       ;; and place it in the buffer it's supposed to be in.
13572       (when (and (get-buffer gnus-article-buffer)
13573                  ;;(numberp article)
13574                  (equal (buffer-name (current-buffer))
13575                         (buffer-name (get-buffer gnus-article-buffer))))
13576         (save-excursion
13577           (if (get-buffer gnus-original-article-buffer)
13578               (set-buffer (get-buffer gnus-original-article-buffer))
13579             (set-buffer (get-buffer-create gnus-original-article-buffer))
13580             (buffer-disable-undo (current-buffer))
13581             (setq major-mode 'gnus-original-article-mode)
13582             (setq buffer-read-only t)
13583             (gnus-add-current-to-buffer-list))
13584           (let (buffer-read-only)
13585             (erase-buffer)
13586             (insert-buffer-substring gnus-article-buffer))
13587           (setq gnus-original-article (cons group article))))
13588     
13589       ;; Update sparse articles.
13590       (when (and do-update-line
13591                  (or (numberp article)
13592                      (stringp article)))
13593         (let ((buf (current-buffer)))
13594           (set-buffer gnus-summary-buffer)
13595           (gnus-summary-update-article do-update-line)
13596           (gnus-summary-goto-subject do-update-line nil t)
13597           (set-window-point (get-buffer-window (current-buffer) t)
13598                             (point))
13599           (set-buffer buf))))))
13600
13601 (defun gnus-read-header (id &optional header)
13602   "Read the headers of article ID and enter them into the Gnus system."
13603   (let ((group gnus-newsgroup-name)
13604         (gnus-override-method 
13605          (and (gnus-news-group-p gnus-newsgroup-name)
13606               gnus-refer-article-method))       
13607         where)
13608     ;; First we check to see whether the header in question is already
13609     ;; fetched.
13610     (if (stringp id)
13611         ;; This is a Message-ID.
13612         (setq header (or header (gnus-id-to-header id)))
13613       ;; This is an article number.
13614       (setq header (or header (gnus-summary-article-header id))))
13615     (if (and header
13616              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13617         ;; We have found the header.
13618         header
13619       ;; We have to really fetch the header to this article.
13620       (when (setq where
13621                   (if (gnus-check-backend-function 'request-head group)
13622                       (gnus-request-head id group)
13623                     (gnus-request-article id group)))
13624         (save-excursion
13625           (set-buffer nntp-server-buffer)
13626           (and (search-forward "\n\n" nil t)
13627                (delete-region (1- (point)) (point-max)))
13628           (goto-char (point-max))
13629           (insert ".\n")
13630           (goto-char (point-min))
13631           (insert "211 ")
13632           (princ (cond
13633                   ((numberp id) id)
13634                   ((cdr where) (cdr where))
13635                   (header (mail-header-number header))
13636                   (t gnus-reffed-article-number))
13637                  (current-buffer))
13638           (insert " Article retrieved.\n"))
13639         ;(when (and header
13640         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13641         ;  (setcar (gnus-id-to-thread id) nil))
13642         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13643             ()                          ; Malformed head.
13644           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13645             (if (and (stringp id)
13646                      (not (string= (gnus-group-real-name group)
13647                                    (car where))))
13648                 ;; If we fetched by Message-ID and the article came
13649                 ;; from a different group, we fudge some bogus article
13650                 ;; numbers for this article.
13651                 (mail-header-set-number header gnus-reffed-article-number))
13652             (decf gnus-reffed-article-number)
13653             (push header gnus-newsgroup-headers)
13654             (setq gnus-current-headers header)
13655             (push (mail-header-number header) gnus-newsgroup-limit))
13656           header)))))
13657
13658 (defun gnus-article-prepare (article &optional all-headers header)
13659   "Prepare ARTICLE in article mode buffer.
13660 ARTICLE should either be an article number or a Message-ID.
13661 If ARTICLE is an id, HEADER should be the article headers.
13662 If ALL-HEADERS is non-nil, no headers are hidden."
13663   (save-excursion
13664     ;; Make sure we start in a summary buffer.
13665     (unless (eq major-mode 'gnus-summary-mode)
13666       (set-buffer gnus-summary-buffer))
13667     (setq gnus-summary-buffer (current-buffer))
13668     ;; Make sure the connection to the server is alive.
13669     (unless (gnus-server-opened
13670              (gnus-find-method-for-group gnus-newsgroup-name))
13671       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13672       (gnus-request-group gnus-newsgroup-name t))
13673     (let* ((article (if header (mail-header-number header) article))
13674            (summary-buffer (current-buffer))
13675            (internal-hook gnus-article-internal-prepare-hook)
13676            (group gnus-newsgroup-name)
13677            result)
13678       (save-excursion
13679         (gnus-article-setup-buffer)
13680         (set-buffer gnus-article-buffer)
13681         ;; Deactivate active regions.
13682         (when (and (boundp 'transient-mark-mode)
13683                    transient-mark-mode)
13684           (setq mark-active nil))
13685         (if (not (setq result (let ((buffer-read-only nil))
13686                                 (gnus-request-article-this-buffer
13687                                  article group))))
13688             ;; There is no such article.
13689             (save-excursion
13690               (when (and (numberp article)
13691                          (not (memq article gnus-newsgroup-sparse)))
13692                 (setq gnus-article-current
13693                       (cons gnus-newsgroup-name article))
13694                 (set-buffer gnus-summary-buffer)
13695                 (setq gnus-current-article article)
13696                 (gnus-summary-mark-article article gnus-canceled-mark))
13697               (unless (memq article gnus-newsgroup-sparse)
13698                 (gnus-error
13699                  1 "No such article (may have expired or been canceled)")))
13700           (if (or (eq result 'pseudo) (eq result 'nneething))
13701               (progn
13702                 (save-excursion
13703                   (set-buffer summary-buffer)
13704                   (setq gnus-last-article gnus-current-article
13705                         gnus-newsgroup-history (cons gnus-current-article
13706                                                      gnus-newsgroup-history)
13707                         gnus-current-article 0
13708                         gnus-current-headers nil
13709                         gnus-article-current nil)
13710                   (if (eq result 'nneething)
13711                       (gnus-configure-windows 'summary)
13712                     (gnus-configure-windows 'article))
13713                   (gnus-set-global-variables))
13714                 (gnus-set-mode-line 'article))
13715             ;; The result from the `request' was an actual article -
13716             ;; or at least some text that is now displayed in the
13717             ;; article buffer.
13718             (if (and (numberp article)
13719                      (not (eq article gnus-current-article)))
13720                 ;; Seems like a new article has been selected.
13721                 ;; `gnus-current-article' must be an article number.
13722                 (save-excursion
13723                   (set-buffer summary-buffer)
13724                   (setq gnus-last-article gnus-current-article
13725                         gnus-newsgroup-history (cons gnus-current-article
13726                                                      gnus-newsgroup-history)
13727                         gnus-current-article article
13728                         gnus-current-headers
13729                         (gnus-summary-article-header gnus-current-article)
13730                         gnus-article-current
13731                         (cons gnus-newsgroup-name gnus-current-article))
13732                   (unless (vectorp gnus-current-headers)
13733                     (setq gnus-current-headers nil))
13734                   (gnus-summary-show-thread)
13735                   (run-hooks 'gnus-mark-article-hook)
13736                   (gnus-set-mode-line 'summary)
13737                   (and (gnus-visual-p 'article-highlight 'highlight)
13738                        (run-hooks 'gnus-visual-mark-article-hook))
13739                   ;; Set the global newsgroup variables here.
13740                   ;; Suggested by Jim Sisolak
13741                   ;; <sisolak@trans4.neep.wisc.edu>.
13742                   (gnus-set-global-variables)
13743                   (setq gnus-have-all-headers
13744                         (or all-headers gnus-show-all-headers))
13745                   (and gnus-use-cache
13746                        (vectorp (gnus-summary-article-header article))
13747                        (gnus-cache-possibly-enter-article
13748                         group article
13749                         (gnus-summary-article-header article)
13750                         (memq article gnus-newsgroup-marked)
13751                         (memq article gnus-newsgroup-dormant)
13752                         (memq article gnus-newsgroup-unreads)))))
13753             (when (or (numberp article)
13754                       (stringp article))
13755               ;; Hooks for getting information from the article.
13756               ;; This hook must be called before being narrowed.
13757               (let (buffer-read-only)
13758                 (run-hooks 'internal-hook)
13759                 (run-hooks 'gnus-article-prepare-hook)
13760                 ;; Decode MIME message.
13761                 (if gnus-show-mime
13762                     (if (or (not gnus-strict-mime)
13763                             (gnus-fetch-field "Mime-Version"))
13764                         (funcall gnus-show-mime-method)
13765                       (funcall gnus-decode-encoded-word-method)))
13766                 ;; Perform the article display hooks.
13767                 (run-hooks 'gnus-article-display-hook))
13768               ;; Do page break.
13769               (goto-char (point-min))
13770               (and gnus-break-pages (gnus-narrow-to-page)))
13771             (gnus-set-mode-line 'article)
13772             (gnus-configure-windows 'article)
13773             (goto-char (point-min))
13774             t))))))
13775
13776 (defun gnus-article-show-all-headers ()
13777   "Show all article headers in article mode buffer."
13778   (save-excursion
13779     (gnus-article-setup-buffer)
13780     (set-buffer gnus-article-buffer)
13781     (let ((buffer-read-only nil))
13782       (gnus-unhide-text (point-min) (point-max)))))
13783
13784 (defun gnus-article-hide-headers-if-wanted ()
13785   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13786 Provided for backwards compatibility."
13787   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13788       gnus-inhibit-hiding
13789       (gnus-article-hide-headers)))
13790
13791 (defsubst gnus-article-header-rank ()
13792   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13793   (let ((list gnus-sorted-header-list)
13794         (i 0))
13795     (while list
13796       (when (looking-at (car list))
13797         (setq list nil))
13798       (setq list (cdr list))
13799       (incf i))
13800     i))
13801
13802 (defun gnus-article-hide-headers (&optional arg delete)
13803   "Toggle whether to hide unwanted headers and possibly sort them as well.
13804 If given a negative prefix, always show; if given a positive prefix,
13805 always hide."
13806   (interactive "P")
13807   (unless (gnus-article-check-hidden-text 'headers arg)
13808     ;; This function might be inhibited.
13809     (unless gnus-inhibit-hiding
13810       (save-excursion
13811         (set-buffer gnus-article-buffer)
13812         (save-restriction
13813           (let ((buffer-read-only nil)
13814                 (props (nconc (list 'gnus-type 'headers)
13815                               gnus-hidden-properties))
13816                 (max (1+ (length gnus-sorted-header-list)))
13817                 (ignored (when (not (stringp gnus-visible-headers))
13818                            (cond ((stringp gnus-ignored-headers)
13819                                   gnus-ignored-headers)
13820                                  ((listp gnus-ignored-headers)
13821                                   (mapconcat 'identity gnus-ignored-headers
13822                                              "\\|")))))
13823                 (visible
13824                  (cond ((stringp gnus-visible-headers)
13825                         gnus-visible-headers)
13826                        ((and gnus-visible-headers
13827                              (listp gnus-visible-headers))
13828                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13829                 want-list beg)
13830             ;; First we narrow to just the headers.
13831             (widen)
13832             (goto-char (point-min))
13833             ;; Hide any "From " lines at the beginning of (mail) articles.
13834             (while (looking-at "From ")
13835               (forward-line 1))
13836             (unless (bobp)
13837               (gnus-hide-text (point-min) (point) props))
13838             ;; Then treat the rest of the header lines.
13839             (narrow-to-region
13840              (point)
13841              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13842             ;; Then we use the two regular expressions
13843             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13844             ;; select which header lines is to remain visible in the
13845             ;; article buffer.
13846             (goto-char (point-min))
13847             (while (re-search-forward "^[^ \t]*:" nil t)
13848               (beginning-of-line)
13849               ;; We add the headers we want to keep to a list and delete
13850               ;; them from the buffer.
13851               (gnus-put-text-property 
13852                (point) (1+ (point)) 'message-rank
13853                (if (or (and visible (looking-at visible))
13854                        (and ignored
13855                             (not (looking-at ignored))))
13856                    (gnus-article-header-rank) 
13857                  (+ 2 max)))
13858               (forward-line 1))
13859             (message-sort-headers-1)
13860             (when (setq beg (text-property-any 
13861                              (point-min) (point-max) 'message-rank (+ 2 max)))
13862               ;; We make the unwanted headers invisible.
13863               (if delete
13864                   (delete-region beg (point-max))
13865                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13866                 (gnus-hide-text-type beg (point-max) 'headers))
13867               ;; Work around XEmacs lossage.
13868               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13869
13870 (defun gnus-article-hide-boring-headers (&optional arg)
13871   "Toggle hiding of headers that aren't very interesting.
13872 If given a negative prefix, always show; if given a positive prefix,
13873 always hide."
13874   (interactive "P")
13875   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13876     (save-excursion
13877       (set-buffer gnus-article-buffer)
13878       (save-restriction
13879         (let ((buffer-read-only nil)
13880               (list gnus-boring-article-headers)
13881               (inhibit-point-motion-hooks t)
13882               elem)
13883           (nnheader-narrow-to-headers)
13884           (while list
13885             (setq elem (pop list))
13886             (goto-char (point-min))
13887             (cond
13888              ;; Hide empty headers.
13889              ((eq elem 'empty)
13890               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13891                 (forward-line -1)
13892                 (gnus-hide-text-type
13893                  (progn (beginning-of-line) (point))
13894                  (progn 
13895                    (end-of-line)
13896                    (if (re-search-forward "^[^ \t]" nil t)
13897                        (match-beginning 0)
13898                      (point-max)))
13899                  'boring-headers)))
13900              ;; Hide boring Newsgroups header.
13901              ((eq elem 'newsgroups)
13902               (when (equal (message-fetch-field "newsgroups")
13903                            (gnus-group-real-name gnus-newsgroup-name))
13904                 (gnus-article-hide-header "newsgroups")))
13905              ((eq elem 'followup-to)
13906               (when (equal (message-fetch-field "followup-to")
13907                            (message-fetch-field "newsgroups"))
13908                 (gnus-article-hide-header "followup-to")))
13909              ((eq elem 'reply-to)
13910               (let ((from (message-fetch-field "from"))
13911                     (reply-to (message-fetch-field "reply-to")))
13912                 (when (and
13913                        from reply-to
13914                        (equal 
13915                         (nth 1 (funcall gnus-extract-address-components from))
13916                         (nth 1 (funcall gnus-extract-address-components
13917                                         reply-to))))
13918                   (gnus-article-hide-header "reply-to"))))
13919              ((eq elem 'date)
13920               (let ((date (message-fetch-field "date")))
13921                 (when (and date
13922                            (< (gnus-days-between date (current-time-string))
13923                               4))
13924                   (gnus-article-hide-header "date")))))))))))
13925
13926 (defun gnus-article-hide-header (header)
13927   (save-excursion
13928     (goto-char (point-min))
13929     (when (re-search-forward (concat "^" header ":") nil t)
13930       (gnus-hide-text-type
13931        (progn (beginning-of-line) (point))
13932        (progn 
13933          (end-of-line)
13934          (if (re-search-forward "^[^ \t]" nil t)
13935              (match-beginning 0)
13936            (point-max)))
13937        'boring-headers))))
13938
13939 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13940 (defun gnus-article-treat-overstrike ()
13941   "Translate overstrikes into bold text."
13942   (interactive)
13943   (save-excursion
13944     (set-buffer gnus-article-buffer)
13945     (let ((buffer-read-only nil))
13946       (while (search-forward "\b" nil t)
13947         (let ((next (following-char))
13948               (previous (char-after (- (point) 2))))
13949           (cond ((eq next previous)
13950                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13951                  (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
13952                 ((eq next ?_)
13953                  (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
13954                  (gnus-put-text-property
13955                   (- (point) 2) (1- (point)) 'face 'underline))
13956                 ((eq previous ?_)
13957                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13958                  (gnus-put-text-property
13959                   (point) (1+ (point))  'face 'underline))))))))
13960
13961 (defun gnus-article-word-wrap ()
13962   "Format too long lines."
13963   (interactive)
13964   (save-excursion
13965     (set-buffer gnus-article-buffer)
13966     (let ((buffer-read-only nil))
13967       (widen)
13968       (goto-char (point-min))
13969       (search-forward "\n\n" nil t)
13970       (end-of-line 1)
13971       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13972             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13973             (adaptive-fill-mode t))
13974         (while (not (eobp))
13975           (and (>= (current-column) (min fill-column (window-width)))
13976                (/= (preceding-char) ?:)
13977                (fill-paragraph nil))
13978           (end-of-line 2))))))
13979
13980 (defun gnus-article-remove-cr ()
13981   "Remove carriage returns from an article."
13982   (interactive)
13983   (save-excursion
13984     (set-buffer gnus-article-buffer)
13985     (let ((buffer-read-only nil))
13986       (goto-char (point-min))
13987       (while (search-forward "\r" nil t)
13988         (replace-match "" t t)))))
13989
13990 (defun gnus-article-remove-trailing-blank-lines ()
13991   "Remove all trailing blank lines from the article."
13992   (interactive)
13993   (save-excursion
13994     (set-buffer gnus-article-buffer)
13995     (let ((buffer-read-only nil))
13996       (goto-char (point-max))
13997       (delete-region
13998        (point)
13999        (progn
14000          (while (looking-at "^[ \t]*$")
14001            (forward-line -1))
14002          (forward-line 1)
14003          (point))))))
14004
14005 (defun gnus-article-display-x-face (&optional force)
14006   "Look for an X-Face header and display it if present."
14007   (interactive (list 'force))
14008   (save-excursion
14009     (set-buffer gnus-article-buffer)
14010     ;; Delete the old process, if any.
14011     (when (process-status "gnus-x-face")
14012       (delete-process "gnus-x-face"))
14013     (let ((inhibit-point-motion-hooks t)
14014           (case-fold-search nil)
14015           from)
14016       (save-restriction
14017         (nnheader-narrow-to-headers)
14018         (setq from (message-fetch-field "from"))
14019         (goto-char (point-min))
14020         (when (and gnus-article-x-face-command
14021                    (or force
14022                        ;; Check whether this face is censored.
14023                        (not gnus-article-x-face-too-ugly)
14024                        (and gnus-article-x-face-too-ugly from
14025                             (not (string-match gnus-article-x-face-too-ugly
14026                                                from))))
14027                    ;; Has to be present.
14028                    (re-search-forward "^X-Face: " nil t))
14029           ;; We now have the area of the buffer where the X-Face is stored.
14030           (let ((beg (point))
14031                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14032             ;; We display the face.
14033             (if (symbolp gnus-article-x-face-command)
14034                 ;; The command is a lisp function, so we call it.
14035                 (if (gnus-functionp gnus-article-x-face-command)
14036                     (funcall gnus-article-x-face-command beg end)
14037                   (error "%s is not a function" gnus-article-x-face-command))
14038               ;; The command is a string, so we interpret the command
14039               ;; as a, well, command, and fork it off.
14040               (let ((process-connection-type nil))
14041                 (process-kill-without-query
14042                  (start-process
14043                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14044                 (process-send-region "gnus-x-face" beg end)
14045                 (process-send-eof "gnus-x-face")))))))))
14046
14047 (defalias 'gnus-header-decode-quoted-printable 'gnus-decode-rfc1522)
14048 (defun gnus-decode-rfc1522 ()
14049   "Hack to remove QP encoding from headers."
14050   (let ((case-fold-search t)
14051         (inhibit-point-motion-hooks t)
14052         (buffer-read-only nil)
14053         string)
14054     (save-restriction
14055       (narrow-to-region
14056        (goto-char (point-min))
14057        (or (search-forward "\n\n" nil t) (point-max)))
14058
14059       (while (re-search-forward 
14060               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14061         (setq string (match-string 1))
14062         (narrow-to-region (match-beginning 0) (match-end 0))
14063         (delete-region (point-min) (point-max))
14064         (insert string)
14065         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14066         (subst-char-in-region (point-min) (point-max) ?_ ? )
14067         (widen)
14068         (goto-char (point-min))))))
14069
14070 (defun gnus-article-de-quoted-unreadable (&optional force)
14071   "Do a naive translation of a quoted-printable-encoded article.
14072 This is in no way, shape or form meant as a replacement for real MIME
14073 processing, but is simply a stop-gap measure until MIME support is
14074 written.
14075 If FORCE, decode the article whether it is marked as quoted-printable
14076 or not."
14077   (interactive (list 'force))
14078   (save-excursion
14079     (set-buffer gnus-article-buffer)
14080     (let ((case-fold-search t)
14081           (buffer-read-only nil)
14082           (type (gnus-fetch-field "content-transfer-encoding")))
14083       (gnus-decode-rfc1522)
14084       (when (or force
14085                 (and type (string-match "quoted-printable" (downcase type))))
14086         (goto-char (point-min))
14087         (search-forward "\n\n" nil 'move)
14088         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14089
14090 (defun gnus-mime-decode-quoted-printable (from to)
14091   "Decode Quoted-Printable in the region between FROM and TO."
14092   (interactive "r")
14093   (goto-char from)
14094   (while (search-forward "=" to t)
14095     (cond ((eq (following-char) ?\n)
14096            (delete-char -1)
14097            (delete-char 1))
14098           ((looking-at "[0-9A-F][0-9A-F]")
14099            (subst-char-in-region
14100             (1- (point)) (point) ?=
14101             (hexl-hex-string-to-integer
14102              (buffer-substring (point) (+ 2 (point)))))
14103            (delete-char 2))
14104           ((looking-at "=")
14105            (delete-char 1))
14106           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14107
14108 (defun gnus-article-hide-pgp (&optional arg)
14109   "Toggle hiding of any PGP headers and signatures in the current article.
14110 If given a negative prefix, always show; if given a positive prefix,
14111 always hide."
14112   (interactive "P")
14113   (unless (gnus-article-check-hidden-text 'pgp arg)
14114     (save-excursion
14115       (set-buffer gnus-article-buffer)
14116       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14117             buffer-read-only beg end)
14118         (widen)
14119         (goto-char (point-min))
14120         ;; Hide the "header".
14121         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14122              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14123         (setq beg (point))
14124         ;; Hide the actual signature.
14125         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14126              (setq end (1+ (match-beginning 0)))
14127              (gnus-hide-text
14128               end
14129               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14130                   (match-end 0)
14131                 ;; Perhaps we shouldn't hide to the end of the buffer
14132                 ;; if there is no end to the signature?
14133                 (point-max))
14134               props))
14135         ;; Hide "- " PGP quotation markers.
14136         (when (and beg end)
14137           (narrow-to-region beg end)
14138           (goto-char (point-min))
14139           (while (re-search-forward "^- " nil t)
14140             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14141           (widen))))))
14142
14143 (defun gnus-article-hide-signature (&optional arg)
14144   "Hide the signature in the current article.
14145 If given a negative prefix, always show; if given a positive prefix,
14146 always hide."
14147   (interactive "P")
14148   (unless (gnus-article-check-hidden-text 'signature arg)
14149     (save-excursion
14150       (set-buffer gnus-article-buffer)
14151       (save-restriction
14152         (let ((buffer-read-only nil))
14153           (when (gnus-narrow-to-signature)
14154             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14155
14156 (defun gnus-article-strip-leading-blank-lines ()
14157   "Remove all blank lines from the beginning of the article."
14158   (interactive)
14159   (save-excursion
14160     (set-buffer gnus-article-buffer)
14161     (let (buffer-read-only)
14162       (goto-char (point-min))
14163       (when (search-forward "\n\n" nil t)
14164         (while (looking-at "[ \t]$")
14165           (gnus-delete-line))))))
14166
14167 (defvar mime::preview/content-list)
14168 (defvar mime::preview-content-info/point-min)
14169 (defun gnus-narrow-to-signature ()
14170   "Narrow to the signature."
14171   (widen)
14172   (if (and (boundp 'mime::preview/content-list)
14173            mime::preview/content-list)
14174       (let ((pcinfo (car (last mime::preview/content-list))))
14175         (narrow-to-region
14176          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14177          (point-max))
14178         t))
14179   (goto-char (point-max))
14180   (when (re-search-backward gnus-signature-separator nil t)
14181     (forward-line 1)
14182     (when (or (null gnus-signature-limit)
14183               (and (numberp gnus-signature-limit)
14184                    (< (- (point-max) (point)) gnus-signature-limit))
14185               (and (gnus-functionp gnus-signature-limit)
14186                    (funcall gnus-signature-limit))
14187               (and (stringp gnus-signature-limit)
14188                    (not (re-search-forward gnus-signature-limit nil t))))
14189       (narrow-to-region (point) (point-max))
14190       t)))
14191
14192 (defun gnus-article-check-hidden-text (type arg)
14193   "Return nil if hiding is necessary."
14194   (save-excursion
14195     (set-buffer gnus-article-buffer)
14196     (let ((hide (gnus-article-hidden-text-p type)))
14197       (cond ((or (and (null arg) (eq hide 'hidden))
14198                  (and arg (< 0 (prefix-numeric-value arg))))
14199              (gnus-article-show-hidden-text type))
14200             ((eq hide 'shown)
14201              (gnus-article-show-hidden-text type t))
14202             (t nil)))))
14203
14204 (defun gnus-article-hidden-text-p (type)
14205   "Say whether the current buffer contains hidden text of type TYPE."
14206   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14207     (when pos
14208       (if (get-text-property pos 'invisible)
14209           'hidden
14210         'shown))))
14211
14212 (defun gnus-article-hide (&optional arg force)
14213   "Hide all the gruft in the current article.
14214 This means that PGP stuff, signatures, cited text and (some)
14215 headers will be hidden.
14216 If given a prefix, show the hidden text instead."
14217   (interactive (list current-prefix-arg 'force))
14218   (gnus-article-hide-headers arg)
14219   (gnus-article-hide-pgp arg)
14220   (gnus-article-hide-citation-maybe arg force)
14221   (gnus-article-hide-signature arg))
14222
14223 (defun gnus-article-show-hidden-text (type &optional hide)
14224   "Show all hidden text of type TYPE.
14225 If HIDE, hide the text instead."
14226   (save-excursion
14227     (set-buffer gnus-article-buffer)
14228     (let ((buffer-read-only nil)
14229           (inhibit-point-motion-hooks t)
14230           (beg (point-min)))
14231       (while (gnus-goto-char (text-property-any
14232                               beg (point-max) 'gnus-type type))
14233         (setq beg (point))
14234         (forward-char)
14235         (if hide
14236             (gnus-hide-text beg (point) gnus-hidden-properties)
14237           (gnus-unhide-text beg (point)))
14238         (setq beg (point)))
14239       t)))
14240
14241 (defvar gnus-article-time-units
14242   `((year . ,(* 365.25 24 60 60))
14243     (week . ,(* 7 24 60 60))
14244     (day . ,(* 24 60 60))
14245     (hour . ,(* 60 60))
14246     (minute . 60)
14247     (second . 1))
14248   "Mapping from time units to seconds.")
14249
14250 (defun gnus-article-date-ut (&optional type highlight)
14251   "Convert DATE date to universal time in the current article.
14252 If TYPE is `local', convert to local time; if it is `lapsed', output
14253 how much time has lapsed since DATE."
14254   (interactive (list 'ut t))
14255   (let* ((header (or gnus-current-headers
14256                      (gnus-summary-article-header) ""))
14257          (date (and (vectorp header) (mail-header-date header)))
14258          (date-regexp "^Date: \\|^X-Sent: ")
14259          (now (current-time))
14260          (inhibit-point-motion-hooks t)
14261          bface eface)
14262     (when (and date (not (string= date "")))
14263       (save-excursion
14264         (set-buffer gnus-article-buffer)
14265         (save-restriction
14266           (nnheader-narrow-to-headers)
14267           (let ((buffer-read-only nil))
14268             ;; Delete any old Date headers.
14269             (if (re-search-forward date-regexp nil t)
14270                 (progn
14271                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14272                         eface (get-text-property (1- (gnus-point-at-eol))
14273                                                  'face))
14274                   (message-remove-header date-regexp t)
14275                   (beginning-of-line))
14276               (goto-char (point-max)))
14277             (insert (gnus-make-date-line date type))
14278             ;; Do highlighting.
14279             (forward-line -1)
14280             (when (and (gnus-visual-p 'article-highlight 'highlight)
14281                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14282               (gnus-put-text-property (match-beginning 1) (match-end 1)
14283                                  'face bface)
14284               (gnus-put-text-property (match-beginning 2) (match-end 2)
14285                                  'face eface))))))))
14286
14287 (defun gnus-make-date-line (date type)
14288   "Return a DATE line of TYPE."
14289   (cond
14290    ;; Convert to the local timezone.  We have to slap a
14291    ;; `condition-case' round the calls to the timezone
14292    ;; functions since they aren't particularly resistant to
14293    ;; buggy dates.
14294    ((eq type 'local)
14295     (concat "Date: " (condition-case ()
14296                          (timezone-make-date-arpa-standard date)
14297                        (error date))
14298             "\n"))
14299    ;; Convert to Universal Time.
14300    ((eq type 'ut)
14301     (concat "Date: "
14302             (condition-case ()
14303                 (timezone-make-date-arpa-standard date nil "UT")
14304               (error date))
14305             "\n"))
14306    ;; Get the original date from the article.
14307    ((eq type 'original)
14308     (concat "Date: " date "\n"))
14309    ;; Do an X-Sent lapsed format.
14310    ((eq type 'lapsed)
14311     ;; If the date is seriously mangled, the timezone
14312     ;; functions are liable to bug out, so we condition-case
14313     ;; the entire thing.
14314     (let* ((now (current-time))
14315            (real-time
14316             (condition-case ()
14317                 (gnus-time-minus
14318                  (gnus-encode-date
14319                   (timezone-make-date-arpa-standard
14320                    (current-time-string now)
14321                    (current-time-zone now) "UT"))
14322                  (gnus-encode-date
14323                   (timezone-make-date-arpa-standard
14324                    date nil "UT")))
14325               (error '(0 0))))
14326            (real-sec (+ (* (float (car real-time)) 65536)
14327                         (cadr real-time)))
14328            (sec (abs real-sec))
14329            num prev)
14330       (cond
14331        ((equal real-time '(0 0))
14332         "X-Sent: Unknown\n")
14333        ((zerop sec)
14334         "X-Sent: Now\n")
14335        (t
14336         (concat
14337          "X-Sent: "
14338          ;; This is a bit convoluted, but basically we go
14339          ;; through the time units for years, weeks, etc,
14340          ;; and divide things to see whether that results
14341          ;; in positive answers.
14342          (mapconcat
14343           (lambda (unit)
14344             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14345                 ;; The (remaining) seconds are too few to
14346                 ;; be divided into this time unit.
14347                 ""
14348               ;; It's big enough, so we output it.
14349               (setq sec (- sec (* num (cdr unit))))
14350               (prog1
14351                   (concat (if prev ", " "") (int-to-string
14352                                              (floor num))
14353                           " " (symbol-name (car unit))
14354                           (if (> num 1) "s" ""))
14355                 (setq prev t))))
14356           gnus-article-time-units "")
14357          ;; If dates are odd, then it might appear like the
14358          ;; article was sent in the future.
14359          (if (> real-sec 0)
14360              " ago\n"
14361            " in the future\n"))))))
14362    (t
14363     (error "Unknown conversion type: %s" type))))
14364
14365 (defun gnus-article-date-local (&optional highlight)
14366   "Convert the current article date to the local timezone."
14367   (interactive (list t))
14368   (gnus-article-date-ut 'local highlight))
14369
14370 (defun gnus-article-date-original (&optional highlight)
14371   "Convert the current article date to what it was originally.
14372 This is only useful if you have used some other date conversion
14373 function and want to see what the date was before converting."
14374   (interactive (list t))
14375   (gnus-article-date-ut 'original highlight))
14376
14377 (defun gnus-article-date-lapsed (&optional highlight)
14378   "Convert the current article date to time lapsed since it was sent."
14379   (interactive (list t))
14380   (gnus-article-date-ut 'lapsed highlight))
14381
14382 (defun gnus-article-maybe-highlight ()
14383   "Do some article highlighting if `gnus-visual' is non-nil."
14384   (if (gnus-visual-p 'article-highlight 'highlight)
14385       (gnus-article-highlight-some)))
14386
14387 ;; Article savers.
14388
14389 (defun gnus-output-to-rmail (file-name)
14390   "Append the current article to an Rmail file named FILE-NAME."
14391   (require 'rmail)
14392   ;; Most of these codes are borrowed from rmailout.el.
14393   (setq file-name (expand-file-name file-name))
14394   (setq rmail-default-rmail-file file-name)
14395   (let ((artbuf (current-buffer))
14396         (tmpbuf (get-buffer-create " *Gnus-output*")))
14397     (save-excursion
14398       (or (get-file-buffer file-name)
14399           (file-exists-p file-name)
14400           (if (gnus-yes-or-no-p
14401                (concat "\"" file-name "\" does not exist, create it? "))
14402               (let ((file-buffer (create-file-buffer file-name)))
14403                 (save-excursion
14404                   (set-buffer file-buffer)
14405                   (rmail-insert-rmail-file-header)
14406                   (let ((require-final-newline nil))
14407                     (write-region (point-min) (point-max) file-name t 1)))
14408                 (kill-buffer file-buffer))
14409             (error "Output file does not exist")))
14410       (set-buffer tmpbuf)
14411       (buffer-disable-undo (current-buffer))
14412       (erase-buffer)
14413       (insert-buffer-substring artbuf)
14414       (gnus-convert-article-to-rmail)
14415       ;; Decide whether to append to a file or to an Emacs buffer.
14416       (let ((outbuf (get-file-buffer file-name)))
14417         (if (not outbuf)
14418             (append-to-file (point-min) (point-max) file-name)
14419           ;; File has been visited, in buffer OUTBUF.
14420           (set-buffer outbuf)
14421           (let ((buffer-read-only nil)
14422                 (msg (and (boundp 'rmail-current-message)
14423                           (symbol-value 'rmail-current-message))))
14424             ;; If MSG is non-nil, buffer is in RMAIL mode.
14425             (if msg
14426                 (progn (widen)
14427                        (narrow-to-region (point-max) (point-max))))
14428             (insert-buffer-substring tmpbuf)
14429             (if msg
14430                 (progn
14431                   (goto-char (point-min))
14432                   (widen)
14433                   (search-backward "\^_")
14434                   (narrow-to-region (point) (point-max))
14435                   (goto-char (1+ (point-min)))
14436                   (rmail-count-new-messages t)
14437                   (rmail-show-message msg)))))))
14438     (kill-buffer tmpbuf)))
14439
14440 (defun gnus-output-to-file (file-name)
14441   "Append the current article to a file named FILE-NAME."
14442   (setq file-name (expand-file-name file-name))
14443   (let ((artbuf (current-buffer))
14444         (tmpbuf (get-buffer-create " *Gnus-output*")))
14445     (save-excursion
14446       (set-buffer tmpbuf)
14447       (buffer-disable-undo (current-buffer))
14448       (erase-buffer)
14449       (insert-buffer-substring artbuf)
14450       ;; Append newline at end of the buffer as separator, and then
14451       ;; save it to file.
14452       (goto-char (point-max))
14453       (insert "\n")
14454       (append-to-file (point-min) (point-max) file-name))
14455     (kill-buffer tmpbuf)))
14456
14457 (defun gnus-convert-article-to-rmail ()
14458   "Convert article in current buffer to Rmail message format."
14459   (let ((buffer-read-only nil))
14460     ;; Convert article directly into Babyl format.
14461     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14462     (goto-char (point-min))
14463     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14464     (while (search-forward "\n\^_" nil t) ;single char
14465       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14466     (goto-char (point-max))
14467     (insert "\^_")))
14468
14469 (defun gnus-narrow-to-page (&optional arg)
14470   "Narrow the article buffer to a page.
14471 If given a numerical ARG, move forward ARG pages."
14472   (interactive "P")
14473   (setq arg (if arg (prefix-numeric-value arg) 0))
14474   (save-excursion
14475     (set-buffer gnus-article-buffer)
14476     (goto-char (point-min))
14477     (widen)
14478     (when (gnus-visual-p 'page-marker)
14479       (let ((buffer-read-only nil))
14480         (gnus-remove-text-with-property 'gnus-prev)
14481         (gnus-remove-text-with-property 'gnus-next)))
14482     (when
14483         (cond ((< arg 0)
14484                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14485               ((> arg 0)
14486                (re-search-forward page-delimiter nil 'move arg)))
14487       (goto-char (match-end 0)))
14488     (narrow-to-region
14489      (point)
14490      (if (re-search-forward page-delimiter nil 'move)
14491          (match-beginning 0)
14492        (point)))
14493     (when (and (gnus-visual-p 'page-marker)
14494                (not (= (point-min) 1)))
14495       (save-excursion
14496         (goto-char (point-min))
14497         (gnus-insert-prev-page-button)))
14498     (when (and (gnus-visual-p 'page-marker)
14499                (not (= (1- (point-max)) (buffer-size))))
14500       (save-excursion
14501         (goto-char (point-max))
14502         (gnus-insert-next-page-button)))))
14503
14504 ;; Article mode commands
14505
14506 (defun gnus-article-goto-next-page ()
14507   "Show the next page of the article."
14508   (interactive)
14509   (when (gnus-article-next-page)
14510     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14511
14512 (defun gnus-article-goto-prev-page ()
14513   "Show the next page of the article."
14514   (interactive)
14515   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14516     (gnus-article-prev-page nil)))
14517
14518 (defun gnus-article-next-page (&optional lines)
14519   "Show the next page of the current article.
14520 If end of article, return non-nil.  Otherwise return nil.
14521 Argument LINES specifies lines to be scrolled up."
14522   (interactive "p")
14523   (move-to-window-line -1)
14524   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14525   (if (save-excursion
14526         (end-of-line)
14527         (and (pos-visible-in-window-p)  ;Not continuation line.
14528              (eobp)))
14529       ;; Nothing in this page.
14530       (if (or (not gnus-break-pages)
14531               (save-excursion
14532                 (save-restriction
14533                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14534           t                             ;Nothing more.
14535         (gnus-narrow-to-page 1)         ;Go to next page.
14536         nil)
14537     ;; More in this page.
14538     (condition-case ()
14539         (scroll-up lines)
14540       (end-of-buffer
14541        ;; Long lines may cause an end-of-buffer error.
14542        (goto-char (point-max))))
14543     (move-to-window-line 0)
14544     nil))
14545
14546 (defun gnus-article-prev-page (&optional lines)
14547   "Show previous page of current article.
14548 Argument LINES specifies lines to be scrolled down."
14549   (interactive "p")
14550   (move-to-window-line 0)
14551   (if (and gnus-break-pages
14552            (bobp)
14553            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14554       (progn
14555         (gnus-narrow-to-page -1)        ;Go to previous page.
14556         (goto-char (point-max))
14557         (recenter -1))
14558     (prog1
14559         (condition-case ()
14560             (scroll-down lines)
14561           (error nil))
14562       (move-to-window-line 0))))
14563
14564 (defun gnus-article-refer-article ()
14565   "Read article specified by message-id around point."
14566   (interactive)
14567   (let ((point (point)))
14568     (search-forward ">" nil t)          ;Move point to end of "<....>".
14569     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14570         (let ((message-id (match-string 1)))
14571           (goto-char point)
14572           (set-buffer gnus-summary-buffer)
14573           (gnus-summary-refer-article message-id))
14574       (goto-char (point))
14575       (error "No references around point"))))
14576
14577 (defun gnus-article-show-summary ()
14578   "Reconfigure windows to show summary buffer."
14579   (interactive)
14580   (gnus-configure-windows 'article)
14581   (gnus-summary-goto-subject gnus-current-article))
14582
14583 (defun gnus-article-describe-briefly ()
14584   "Describe article mode commands briefly."
14585   (interactive)
14586   (gnus-message 6
14587                 (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")))
14588
14589 (defun gnus-article-summary-command ()
14590   "Execute the last keystroke in the summary buffer."
14591   (interactive)
14592   (let ((obuf (current-buffer))
14593         (owin (current-window-configuration))
14594         func)
14595     (switch-to-buffer gnus-summary-buffer 'norecord)
14596     (setq func (lookup-key (current-local-map) (this-command-keys)))
14597     (call-interactively func)
14598     (set-buffer obuf)
14599     (set-window-configuration owin)
14600     (set-window-point (get-buffer-window (current-buffer)) (point))))
14601
14602 (defun gnus-article-summary-command-nosave ()
14603   "Execute the last keystroke in the summary buffer."
14604   (interactive)
14605   (let (func)
14606     (pop-to-buffer gnus-summary-buffer 'norecord)
14607     (setq func (lookup-key (current-local-map) (this-command-keys)))
14608     (call-interactively func)))
14609
14610 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14611   "Read a summary buffer key sequence and execute it from the article buffer."
14612   (interactive "P")
14613   (let ((nosaves
14614          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14615            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14616            "=" "^" "\M-^"))
14617         keys)
14618     (save-excursion
14619       (set-buffer gnus-summary-buffer)
14620       (push (or key last-command-event) unread-command-events)
14621       (setq keys (read-key-sequence nil)))
14622     (message "")
14623
14624     (if (member keys nosaves)
14625         (let (func)
14626           (pop-to-buffer gnus-summary-buffer 'norecord)
14627           (if (setq func (lookup-key (current-local-map) keys))
14628               (call-interactively func)
14629             (ding)))
14630       (let ((obuf (current-buffer))
14631             (owin (current-window-configuration))
14632             (opoint (point))
14633             func in-buffer)
14634         (if not-restore-window
14635             (pop-to-buffer gnus-summary-buffer 'norecord)
14636           (switch-to-buffer gnus-summary-buffer 'norecord))
14637         (setq in-buffer (current-buffer))
14638         (if (setq func (lookup-key (current-local-map) keys))
14639             (call-interactively func)
14640           (ding))
14641         (when (eq in-buffer (current-buffer))
14642           (set-buffer obuf)
14643           (unless not-restore-window
14644             (set-window-configuration owin))
14645           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14646
14647 \f
14648 ;;;
14649 ;;; Kill file handling.
14650 ;;;
14651
14652 ;;;###autoload
14653 (defalias 'gnus-batch-kill 'gnus-batch-score)
14654 ;;;###autoload
14655 (defun gnus-batch-score ()
14656   "Run batched scoring.
14657 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14658 Newsgroups is a list of strings in Bnews format.  If you want to score
14659 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14660 score the alt hierarchy, you'd say \"!alt.all\"."
14661   (interactive)
14662   (let* ((yes-and-no
14663           (gnus-newsrc-parse-options
14664            (apply (function concat)
14665                   (mapcar (lambda (g) (concat g " "))
14666                           command-line-args-left))))
14667          (gnus-expert-user t)
14668          (nnmail-spool-file nil)
14669          (gnus-use-dribble-file nil)
14670          (yes (car yes-and-no))
14671          (no (cdr yes-and-no))
14672          group newsrc entry
14673          ;; Disable verbose message.
14674          gnus-novice-user gnus-large-newsgroup)
14675     ;; Eat all arguments.
14676     (setq command-line-args-left nil)
14677     ;; Start Gnus.
14678     (gnus)
14679     ;; Apply kills to specified newsgroups in command line arguments.
14680     (setq newsrc (cdr gnus-newsrc-alist))
14681     (while newsrc
14682       (setq group (caar newsrc))
14683       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14684       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14685                (and (car entry)
14686                     (or (eq (car entry) t)
14687                         (not (zerop (car entry)))))
14688                (if yes (string-match yes group) t)
14689                (or (null no) (not (string-match no group))))
14690           (progn
14691             (gnus-summary-read-group group nil t nil t)
14692             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14693                  (gnus-summary-exit))))
14694       (setq newsrc (cdr newsrc)))
14695     ;; Exit Emacs.
14696     (switch-to-buffer gnus-group-buffer)
14697     (gnus-group-save-newsrc)))
14698
14699 (defun gnus-apply-kill-file ()
14700   "Apply a kill file to the current newsgroup.
14701 Returns the number of articles marked as read."
14702   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14703           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14704       (gnus-apply-kill-file-internal)
14705     0))
14706
14707 (defun gnus-kill-save-kill-buffer ()
14708   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14709     (when (get-file-buffer file)
14710       (save-excursion
14711         (set-buffer (get-file-buffer file))
14712         (and (buffer-modified-p) (save-buffer))
14713         (kill-buffer (current-buffer))))))
14714
14715 (defvar gnus-kill-file-name "KILL"
14716   "Suffix of the kill files.")
14717
14718 (defun gnus-newsgroup-kill-file (newsgroup)
14719   "Return the name of a kill file name for NEWSGROUP.
14720 If NEWSGROUP is nil, return the global kill file name instead."
14721   (cond 
14722    ;; The global KILL file is placed at top of the directory.
14723    ((or (null newsgroup)
14724         (string-equal newsgroup ""))
14725     (expand-file-name gnus-kill-file-name
14726                       gnus-kill-files-directory))
14727    ;; Append ".KILL" to newsgroup name.
14728    ((gnus-use-long-file-name 'not-kill)
14729     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14730                               "." gnus-kill-file-name)
14731                       gnus-kill-files-directory))
14732    ;; Place "KILL" under the hierarchical directory.
14733    (t
14734     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14735                               "/" gnus-kill-file-name)
14736                       gnus-kill-files-directory))))
14737
14738 \f
14739 ;;;
14740 ;;; Dribble file
14741 ;;;
14742
14743 (defvar gnus-dribble-ignore nil)
14744 (defvar gnus-dribble-eval-file nil)
14745
14746 (defun gnus-dribble-file-name ()
14747   "Return the dribble file for the current .newsrc."
14748   (concat
14749    (if gnus-dribble-directory
14750        (concat (file-name-as-directory gnus-dribble-directory)
14751                (file-name-nondirectory gnus-current-startup-file))
14752      gnus-current-startup-file)
14753    "-dribble"))
14754
14755 (defun gnus-dribble-enter (string)
14756   "Enter STRING into the dribble buffer."
14757   (if (and (not gnus-dribble-ignore)
14758            gnus-dribble-buffer
14759            (buffer-name gnus-dribble-buffer))
14760       (let ((obuf (current-buffer)))
14761         (set-buffer gnus-dribble-buffer)
14762         (insert string "\n")
14763         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14764         (set-buffer obuf))))
14765
14766 (defun gnus-dribble-read-file ()
14767   "Read the dribble file from disk."
14768   (let ((dribble-file (gnus-dribble-file-name)))
14769     (save-excursion
14770       (set-buffer (setq gnus-dribble-buffer
14771                         (get-buffer-create
14772                          (file-name-nondirectory dribble-file))))
14773       (gnus-add-current-to-buffer-list)
14774       (erase-buffer)
14775       (setq buffer-file-name dribble-file)
14776       (auto-save-mode t)
14777       (buffer-disable-undo (current-buffer))
14778       (bury-buffer (current-buffer))
14779       (set-buffer-modified-p nil)
14780       (let ((auto (make-auto-save-file-name))
14781             (gnus-dribble-ignore t)
14782             modes)
14783         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14784           ;; Load whichever file is newest -- the auto save file
14785           ;; or the "real" file.
14786           (if (file-newer-than-file-p auto dribble-file)
14787               (insert-file-contents auto)
14788             (insert-file-contents dribble-file))
14789           (unless (zerop (buffer-size))
14790             (set-buffer-modified-p t))
14791           ;; Set the file modes to reflect the .newsrc file modes.
14792           (save-buffer)
14793           (when (and (file-exists-p gnus-current-startup-file)
14794                      (setq modes (file-modes gnus-current-startup-file)))
14795             (set-file-modes dribble-file modes))
14796           ;; Possibly eval the file later.
14797           (when (gnus-y-or-n-p
14798                  "Auto-save file exists.  Do you want to read it? ")
14799             (setq gnus-dribble-eval-file t)))))))
14800
14801 (defun gnus-dribble-eval-file ()
14802   (when gnus-dribble-eval-file
14803     (setq gnus-dribble-eval-file nil)
14804     (save-excursion
14805       (let ((gnus-dribble-ignore t))
14806         (set-buffer gnus-dribble-buffer)
14807         (eval-buffer (current-buffer))))))
14808
14809 (defun gnus-dribble-delete-file ()
14810   (when (file-exists-p (gnus-dribble-file-name))
14811     (delete-file (gnus-dribble-file-name)))
14812   (when gnus-dribble-buffer
14813     (save-excursion
14814       (set-buffer gnus-dribble-buffer)
14815       (let ((auto (make-auto-save-file-name)))
14816         (if (file-exists-p auto)
14817             (delete-file auto))
14818         (erase-buffer)
14819         (set-buffer-modified-p nil)))))
14820
14821 (defun gnus-dribble-save ()
14822   (when (and gnus-dribble-buffer
14823              (buffer-name gnus-dribble-buffer))
14824     (save-excursion
14825       (set-buffer gnus-dribble-buffer)
14826       (save-buffer))))
14827
14828 (defun gnus-dribble-clear ()
14829   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14830     (save-excursion
14831       (set-buffer gnus-dribble-buffer)
14832       (erase-buffer)
14833       (set-buffer-modified-p nil)
14834       (setq buffer-saved-size (buffer-size)))))
14835
14836 \f
14837 ;;;
14838 ;;; Server Communication
14839 ;;;
14840
14841 (defun gnus-start-news-server (&optional confirm)
14842   "Open a method for getting news.
14843 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14844   (let (how)
14845     (if gnus-current-select-method
14846         ;; Stream is already opened.
14847         nil
14848       ;; Open NNTP server.
14849       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14850       (if confirm
14851           (progn
14852             ;; Read server name with completion.
14853             (setq gnus-nntp-server
14854                   (completing-read "NNTP server: "
14855                                    (mapcar (lambda (server) (list server))
14856                                            (cons (list gnus-nntp-server)
14857                                                  gnus-secondary-servers))
14858                                    nil nil gnus-nntp-server))))
14859
14860       (if (and gnus-nntp-server
14861                (stringp gnus-nntp-server)
14862                (not (string= gnus-nntp-server "")))
14863           (setq gnus-select-method
14864                 (cond ((or (string= gnus-nntp-server "")
14865                            (string= gnus-nntp-server "::"))
14866                        (list 'nnspool (system-name)))
14867                       ((string-match "^:" gnus-nntp-server)
14868                        (list 'nnmh gnus-nntp-server
14869                              (list 'nnmh-directory
14870                                    (file-name-as-directory
14871                                     (expand-file-name
14872                                      (concat "~/" (substring
14873                                                    gnus-nntp-server 1)))))
14874                              (list 'nnmh-get-new-mail nil)))
14875                       (t
14876                        (list 'nntp gnus-nntp-server)))))
14877
14878       (setq how (car gnus-select-method))
14879       (cond ((eq how 'nnspool)
14880              (require 'nnspool)
14881              (gnus-message 5 "Looking up local news spool..."))
14882             ((eq how 'nnmh)
14883              (require 'nnmh)
14884              (gnus-message 5 "Looking up mh spool..."))
14885             (t
14886              (require 'nntp)))
14887       (setq gnus-current-select-method gnus-select-method)
14888       (run-hooks 'gnus-open-server-hook)
14889       (or
14890        ;; gnus-open-server-hook might have opened it
14891        (gnus-server-opened gnus-select-method)
14892        (gnus-open-server gnus-select-method)
14893        (gnus-y-or-n-p
14894         (format
14895          "%s (%s) open error: '%s'.     Continue? "
14896          (car gnus-select-method) (cadr gnus-select-method)
14897          (gnus-status-message gnus-select-method)))
14898        (gnus-error 1 "Couldn't open server on %s"
14899                    (nth 1 gnus-select-method))))))
14900
14901 (defun gnus-check-group (group)
14902   "Try to make sure that the server where GROUP exists is alive."
14903   (let ((method (gnus-find-method-for-group group)))
14904     (or (gnus-server-opened method)
14905         (gnus-open-server method))))
14906
14907 (defun gnus-check-server (&optional method silent)
14908   "Check whether the connection to METHOD is down.
14909 If METHOD is nil, use `gnus-select-method'.
14910 If it is down, start it up (again)."
14911   (let ((method (or method gnus-select-method)))
14912     ;; Transform virtual server names into select methods.
14913     (when (stringp method)
14914       (setq method (gnus-server-to-method method)))
14915     (if (gnus-server-opened method)
14916         ;; The stream is already opened.
14917         t
14918       ;; Open the server.
14919       (unless silent
14920         (gnus-message 5 "Opening %s server%s..." (car method)
14921                       (if (equal (nth 1 method) "") ""
14922                         (format " on %s" (nth 1 method)))))
14923       (run-hooks 'gnus-open-server-hook)
14924       (prog1
14925           (gnus-open-server method)
14926         (unless silent
14927           (message ""))))))
14928
14929 (defun gnus-get-function (method function)
14930   "Return a function symbol based on METHOD and FUNCTION."
14931   ;; Translate server names into methods.
14932   (unless method
14933     (error "Attempted use of a nil select method"))
14934   (when (stringp method)
14935     (setq method (gnus-server-to-method method)))
14936   (let ((func (intern (format "%s-%s" (car method) function))))
14937     ;; If the functions isn't bound, we require the backend in
14938     ;; question.
14939     (unless (fboundp func)
14940       (require (car method))
14941       (unless (fboundp func)
14942         ;; This backend doesn't implement this function.
14943         (error "No such function: %s" func)))
14944     func))
14945
14946 \f
14947 ;;;
14948 ;;; Interface functions to the backends.
14949 ;;;
14950
14951 (defun gnus-open-server (method)
14952   "Open a connection to METHOD."
14953   (when (stringp method)
14954     (setq method (gnus-server-to-method method)))
14955   (let ((elem (assoc method gnus-opened-servers)))
14956     ;; If this method was previously denied, we just return nil.
14957     (if (eq (nth 1 elem) 'denied)
14958         (progn
14959           (gnus-message 1 "Denied server")
14960           nil)
14961       ;; Open the server.
14962       (let ((result
14963              (funcall (gnus-get-function method 'open-server)
14964                       (nth 1 method) (nthcdr 2 method))))
14965         ;; If this hasn't been opened before, we add it to the list.
14966         (unless elem
14967           (setq elem (list method nil)
14968                 gnus-opened-servers (cons elem gnus-opened-servers)))
14969         ;; Set the status of this server.
14970         (setcar (cdr elem) (if result 'ok 'denied))
14971         ;; Return the result from the "open" call.
14972         result))))
14973
14974 (defun gnus-close-server (method)
14975   "Close the connection to METHOD."
14976   (when (stringp method)
14977     (setq method (gnus-server-to-method method)))
14978   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14979
14980 (defun gnus-request-list (method)
14981   "Request the active file from METHOD."
14982   (when (stringp method)
14983     (setq method (gnus-server-to-method method)))
14984   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14985
14986 (defun gnus-request-list-newsgroups (method)
14987   "Request the newsgroups file from METHOD."
14988   (when (stringp method)
14989     (setq method (gnus-server-to-method method)))
14990   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14991
14992 (defun gnus-request-newgroups (date method)
14993   "Request all new groups since DATE from METHOD."
14994   (when (stringp method)
14995     (setq method (gnus-server-to-method method)))
14996   (funcall (gnus-get-function method 'request-newgroups)
14997            date (nth 1 method)))
14998
14999 (defun gnus-server-opened (method)
15000   "Check whether a connection to METHOD has been opened."
15001   (when (stringp method)
15002     (setq method (gnus-server-to-method method)))
15003   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15004
15005 (defun gnus-status-message (method)
15006   "Return the status message from METHOD.
15007 If METHOD is a string, it is interpreted as a group name.   The method
15008 this group uses will be queried."
15009   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15010                   method)))
15011     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15012
15013 (defun gnus-request-group (group &optional dont-check method)
15014   "Request GROUP.  If DONT-CHECK, no information is required."
15015   (let ((method (or method (gnus-find-method-for-group group))))
15016     (when (stringp method)
15017       (setq method (gnus-server-to-method method)))
15018     (funcall (gnus-get-function method 'request-group)
15019              (gnus-group-real-name group) (nth 1 method) dont-check)))
15020
15021 (defun gnus-request-asynchronous (group &optional articles)
15022   "Request that GROUP behave asynchronously.
15023 ARTICLES is the `data' of the group."
15024   (let ((method (gnus-find-method-for-group group)))
15025     (funcall (gnus-get-function method 'request-asynchronous)
15026              (gnus-group-real-name group) (nth 1 method) articles)))
15027
15028 (defun gnus-list-active-group (group)
15029   "Request active information on GROUP."
15030   (let ((method (gnus-find-method-for-group group))
15031         (func 'list-active-group))
15032     (when (gnus-check-backend-function func group)
15033       (funcall (gnus-get-function method func)
15034                (gnus-group-real-name group) (nth 1 method)))))
15035
15036 (defun gnus-request-group-description (group)
15037   "Request a description of GROUP."
15038   (let ((method (gnus-find-method-for-group group))
15039         (func 'request-group-description))
15040     (when (gnus-check-backend-function func group)
15041       (funcall (gnus-get-function method func)
15042                (gnus-group-real-name group) (nth 1 method)))))
15043
15044 (defun gnus-close-group (group)
15045   "Request the GROUP be closed."
15046   (let ((method (gnus-find-method-for-group group)))
15047     (funcall (gnus-get-function method 'close-group)
15048              (gnus-group-real-name group) (nth 1 method))))
15049
15050 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15051   "Request headers for ARTICLES in GROUP.
15052 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15053   (let ((method (gnus-find-method-for-group group)))
15054     (if (and gnus-use-cache (numberp (car articles)))
15055         (gnus-cache-retrieve-headers articles group fetch-old)
15056       (funcall (gnus-get-function method 'retrieve-headers)
15057                articles (gnus-group-real-name group) (nth 1 method)
15058                fetch-old))))
15059
15060 (defun gnus-retrieve-groups (groups method)
15061   "Request active information on GROUPS from METHOD."
15062   (when (stringp method)
15063     (setq method (gnus-server-to-method method)))
15064   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15065
15066 (defun gnus-request-type (group &optional article)
15067   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15068   (let ((method (gnus-find-method-for-group group)))
15069     (if (not (gnus-check-backend-function 'request-type (car method)))
15070         'unknown
15071       (funcall (gnus-get-function method 'request-type)
15072                (gnus-group-real-name group) article))))
15073
15074 (defun gnus-request-update-mark (group article mark)
15075   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15076   (let ((method (gnus-find-method-for-group group)))
15077     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15078         mark
15079       (funcall (gnus-get-function method 'request-update-mark)
15080                (gnus-group-real-name group) article mark))))
15081
15082 (defun gnus-request-article (article group &optional buffer)
15083   "Request the ARTICLE in GROUP.
15084 ARTICLE can either be an article number or an article Message-ID.
15085 If BUFFER, insert the article in that group."
15086   (let ((method (gnus-find-method-for-group group)))
15087     (funcall (gnus-get-function method 'request-article)
15088              article (gnus-group-real-name group) (nth 1 method) buffer)))
15089
15090 (defun gnus-request-head (article group)
15091   "Request the head of ARTICLE in GROUP."
15092   (let ((method (gnus-find-method-for-group group)))
15093     (funcall (gnus-get-function method 'request-head)
15094              article (gnus-group-real-name group) (nth 1 method))))
15095
15096 (defun gnus-request-body (article group)
15097   "Request the body of ARTICLE in GROUP."
15098   (let ((method (gnus-find-method-for-group group)))
15099     (funcall (gnus-get-function method 'request-body)
15100              article (gnus-group-real-name group) (nth 1 method))))
15101
15102 (defun gnus-request-post (method)
15103   "Post the current buffer using METHOD."
15104   (when (stringp method)
15105     (setq method (gnus-server-to-method method)))
15106   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15107
15108 (defun gnus-request-scan (group method)
15109   "Request a SCAN being performed in GROUP from METHOD.
15110 If GROUP is nil, all groups on METHOD are scanned."
15111   (let ((method (if group (gnus-find-method-for-group group) method)))
15112     (funcall (gnus-get-function method 'request-scan)
15113              (and group (gnus-group-real-name group)) (nth 1 method))))
15114
15115 (defsubst gnus-request-update-info (info method)
15116   "Request that METHOD update INFO."
15117   (when (stringp method)
15118     (setq method (gnus-server-to-method method)))
15119   (when (gnus-check-backend-function 'request-update-info (car method))
15120     (funcall (gnus-get-function method 'request-update-info)
15121              (gnus-group-real-name (gnus-info-group info))
15122              info (nth 1 method))))
15123
15124 (defun gnus-request-expire-articles (articles group &optional force)
15125   (let ((method (gnus-find-method-for-group group)))
15126     (funcall (gnus-get-function method 'request-expire-articles)
15127              articles (gnus-group-real-name group) (nth 1 method)
15128              force)))
15129
15130 (defun gnus-request-move-article
15131   (article group server accept-function &optional last)
15132   (let ((method (gnus-find-method-for-group group)))
15133     (funcall (gnus-get-function method 'request-move-article)
15134              article (gnus-group-real-name group)
15135              (nth 1 method) accept-function last)))
15136
15137 (defun gnus-request-accept-article (group method &optional last)
15138   ;; Make sure there's a newline at the end of the article.
15139   (when (stringp method)
15140     (setq method (gnus-server-to-method method)))
15141   (when (and (not method)
15142              (stringp group))
15143     (setq method (gnus-find-method-for-group group)))
15144   (goto-char (point-max))
15145   (unless (bolp)
15146     (insert "\n"))
15147   (let ((func (car (or method (gnus-find-method-for-group group)))))
15148     (funcall (intern (format "%s-request-accept-article" func))
15149              (if (stringp group) (gnus-group-real-name group) group)
15150              (cadr method)
15151              last)))
15152
15153 (defun gnus-request-replace-article (article group buffer)
15154   (let ((func (car (gnus-find-method-for-group group))))
15155     (funcall (intern (format "%s-request-replace-article" func))
15156              article (gnus-group-real-name group) buffer)))
15157
15158 (defun gnus-request-associate-buffer (group)
15159   (let ((method (gnus-find-method-for-group group)))
15160     (funcall (gnus-get-function method 'request-associate-buffer)
15161              (gnus-group-real-name group))))
15162
15163 (defun gnus-request-restore-buffer (article group)
15164   "Request a new buffer restored to the state of ARTICLE."
15165   (let ((method (gnus-find-method-for-group group)))
15166     (funcall (gnus-get-function method 'request-restore-buffer)
15167              article (gnus-group-real-name group) (nth 1 method))))
15168
15169 (defun gnus-request-create-group (group &optional method)
15170   (when (stringp method)
15171     (setq method (gnus-server-to-method method)))
15172   (let ((method (or method (gnus-find-method-for-group group))))
15173     (funcall (gnus-get-function method 'request-create-group)
15174              (gnus-group-real-name group) (nth 1 method))))
15175
15176 (defun gnus-request-delete-group (group &optional force)
15177   (let ((method (gnus-find-method-for-group group)))
15178     (funcall (gnus-get-function method 'request-delete-group)
15179              (gnus-group-real-name group) force (nth 1 method))))
15180
15181 (defun gnus-request-rename-group (group new-name)
15182   (let ((method (gnus-find-method-for-group group)))
15183     (funcall (gnus-get-function method 'request-rename-group)
15184              (gnus-group-real-name group)
15185              (gnus-group-real-name new-name) (nth 1 method))))
15186
15187 (defun gnus-member-of-valid (symbol group)
15188   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15189   (memq symbol (assoc
15190                 (symbol-name (car (gnus-find-method-for-group group)))
15191                 gnus-valid-select-methods)))
15192
15193 (defun gnus-method-option-p (method option)
15194   "Return non-nil if select METHOD has OPTION as a parameter."
15195   (when (stringp method)
15196     (setq method (gnus-server-to-method method)))
15197   (memq option (assoc (format "%s" (car method))
15198                       gnus-valid-select-methods)))
15199
15200 (defun gnus-server-extend-method (group method)
15201   ;; This function "extends" a virtual server.  If the server is
15202   ;; "hello", and the select method is ("hello" (my-var "something"))
15203   ;; in the group "alt.alt", this will result in a new virtual server
15204   ;; called "hello+alt.alt".
15205   (let ((entry
15206          (gnus-copy-sequence
15207           (if (equal (car method) "native") gnus-select-method
15208             (cdr (assoc (car method) gnus-server-alist))))))
15209     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15210     (nconc entry (cdr method))))
15211
15212 (defun gnus-find-method-for-group (group &optional info)
15213   "Find the select method that GROUP uses."
15214   (or gnus-override-method
15215       (and (not group)
15216            gnus-select-method)
15217       (let ((info (or info (gnus-get-info group)))
15218             method)
15219         (if (or (not info)
15220                 (not (setq method (gnus-info-method info)))
15221                 (equal method "native"))
15222             gnus-select-method
15223           (setq method
15224                 (cond ((stringp method)
15225                        (gnus-server-to-method method))
15226                       ((stringp (car method))
15227                        (gnus-server-extend-method group method))
15228                       (t
15229                        method)))
15230           (cond ((equal (cadr method) "")
15231                  method)
15232                 ((null (cadr method))
15233                  (list (car method) ""))
15234                 (t
15235                  (gnus-server-add-address method)))))))
15236
15237 (defun gnus-check-backend-function (func group)
15238   "Check whether GROUP supports function FUNC."
15239   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15240                   group)))
15241     (fboundp (intern (format "%s-%s" method func)))))
15242
15243 (defun gnus-methods-using (feature)
15244   "Find all methods that have FEATURE."
15245   (let ((valids gnus-valid-select-methods)
15246         outs)
15247     (while valids
15248       (if (memq feature (car valids))
15249           (setq outs (cons (car valids) outs)))
15250       (setq valids (cdr valids)))
15251     outs))
15252
15253 \f
15254 ;;;
15255 ;;; Active & Newsrc File Handling
15256 ;;;
15257
15258 (defun gnus-setup-news (&optional rawfile level dont-connect)
15259   "Setup news information.
15260 If RAWFILE is non-nil, the .newsrc file will also be read.
15261 If LEVEL is non-nil, the news will be set up at level LEVEL."
15262   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15263
15264     (when init 
15265       ;; Clear some variables to re-initialize news information.
15266       (setq gnus-newsrc-alist nil
15267             gnus-active-hashtb nil)
15268       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15269       (gnus-read-newsrc-file rawfile))
15270
15271     (when (and (not (assoc "archive" gnus-server-alist))
15272                gnus-message-archive-method)
15273       (push (cons "archive" gnus-message-archive-method)
15274             gnus-server-alist))
15275
15276     ;; If we don't read the complete active file, we fill in the
15277     ;; hashtb here.
15278     (if (or (null gnus-read-active-file)
15279             (eq gnus-read-active-file 'some))
15280         (gnus-update-active-hashtb-from-killed))
15281
15282     ;; Read the active file and create `gnus-active-hashtb'.
15283     ;; If `gnus-read-active-file' is nil, then we just create an empty
15284     ;; hash table.  The partial filling out of the hash table will be
15285     ;; done in `gnus-get-unread-articles'.
15286     (and gnus-read-active-file
15287          (not level)
15288          (gnus-read-active-file))
15289
15290     (or gnus-active-hashtb
15291         (setq gnus-active-hashtb (make-vector 4095 0)))
15292
15293     ;; Initialize the cache.
15294     (when gnus-use-cache
15295       (gnus-cache-open))
15296
15297     ;; Possibly eval the dribble file.
15298     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15299
15300     ;; Slave Gnusii should then clear the dribble buffer.
15301     (when (and init gnus-slave)
15302       (gnus-dribble-clear))
15303
15304     (gnus-update-format-specifications)
15305
15306     ;; See whether we need to read the description file.
15307     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15308              (not gnus-description-hashtb)
15309              (not dont-connect)
15310              gnus-read-active-file)
15311         (gnus-read-all-descriptions-files))
15312
15313     ;; Find new newsgroups and treat them.
15314     (if (and init gnus-check-new-newsgroups (not level)
15315              (gnus-check-server gnus-select-method))
15316         (gnus-find-new-newsgroups))
15317
15318     ;; We might read in new NoCeM messages here.
15319     (when (and gnus-use-nocem 
15320                (not level)
15321                (not dont-connect))
15322       (gnus-nocem-scan-groups))
15323
15324     ;; Find the number of unread articles in each non-dead group.
15325     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15326       (gnus-get-unread-articles level))
15327
15328     (if (and init gnus-check-bogus-newsgroups
15329              gnus-read-active-file (not level)
15330              (gnus-server-opened gnus-select-method))
15331         (gnus-check-bogus-newsgroups))))
15332
15333 (defun gnus-find-new-newsgroups (&optional arg)
15334   "Search for new newsgroups and add them.
15335 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15336 The `-n' option line from .newsrc is respected.
15337 If ARG (the prefix), use the `ask-server' method to query
15338 the server for new groups."
15339   (interactive "P")
15340   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15341                        (null gnus-read-active-file)
15342                        (eq gnus-read-active-file 'some))
15343                    'ask-server gnus-check-new-newsgroups)))
15344     (unless (gnus-check-first-time-used)
15345       (if (or (consp check)
15346               (eq check 'ask-server))
15347           ;; Ask the server for new groups.
15348           (gnus-ask-server-for-new-groups)
15349         ;; Go through the active hashtb and look for new groups.
15350         (let ((groups 0)
15351               group new-newsgroups)
15352           (gnus-message 5 "Looking for new newsgroups...")
15353           (unless gnus-have-read-active-file
15354             (gnus-read-active-file))
15355           (setq gnus-newsrc-last-checked-date (current-time-string))
15356           (unless gnus-killed-hashtb
15357             (gnus-make-hashtable-from-killed))
15358           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15359           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15360           (mapatoms
15361            (lambda (sym)
15362              (if (or (null (setq group (symbol-name sym)))
15363                      (not (boundp sym))
15364                      (null (symbol-value sym))
15365                      (gnus-gethash group gnus-killed-hashtb)
15366                      (gnus-gethash group gnus-newsrc-hashtb))
15367                  ()
15368                (let ((do-sub (gnus-matches-options-n group)))
15369                  (cond
15370                   ((eq do-sub 'subscribe)
15371                    (setq groups (1+ groups))
15372                    (gnus-sethash group group gnus-killed-hashtb)
15373                    (funcall gnus-subscribe-options-newsgroup-method group))
15374                   ((eq do-sub 'ignore)
15375                    nil)
15376                   (t
15377                    (setq groups (1+ groups))
15378                    (gnus-sethash group group gnus-killed-hashtb)
15379                    (if gnus-subscribe-hierarchical-interactive
15380                        (setq new-newsgroups (cons group new-newsgroups))
15381                      (funcall gnus-subscribe-newsgroup-method group)))))))
15382            gnus-active-hashtb)
15383           (when new-newsgroups
15384             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15385           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15386           (if (> groups 0)
15387               (gnus-message 6 "%d new newsgroup%s arrived."
15388                             groups (if (> groups 1) "s have" " has"))
15389             (gnus-message 6 "No new newsgroups.")))))))
15390
15391 (defun gnus-matches-options-n (group)
15392   ;; Returns `subscribe' if the group is to be unconditionally
15393   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15394   ;; no match for the group.
15395
15396   ;; First we check the two user variables.
15397   (cond
15398    ((and gnus-options-subscribe
15399          (string-match gnus-options-subscribe group))
15400     'subscribe)
15401    ((and gnus-auto-subscribed-groups
15402          (string-match gnus-auto-subscribed-groups group))
15403     'subscribe)
15404    ((and gnus-options-not-subscribe
15405          (string-match gnus-options-not-subscribe group))
15406     'ignore)
15407    ;; Then we go through the list that was retrieved from the .newsrc
15408    ;; file.  This list has elements on the form
15409    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15410    ;; is in the reverse order of the options line) is returned.
15411    (t
15412     (let ((regs gnus-newsrc-options-n))
15413       (while (and regs
15414                   (not (string-match (caar regs) group)))
15415         (setq regs (cdr regs)))
15416       (and regs (cdar regs))))))
15417
15418 (defun gnus-ask-server-for-new-groups ()
15419   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15420          (methods (cons gnus-select-method
15421                         (nconc
15422                          (when gnus-message-archive-method
15423                            (list "archive"))
15424                          (append
15425                           (and (consp gnus-check-new-newsgroups)
15426                                gnus-check-new-newsgroups)
15427                           gnus-secondary-select-methods))))
15428          (groups 0)
15429          (new-date (current-time-string))
15430          group new-newsgroups got-new method hashtb
15431          gnus-override-subscribe-method)
15432     ;; Go through both primary and secondary select methods and
15433     ;; request new newsgroups.
15434     (while (setq method (gnus-server-get-method nil (pop methods)))
15435       (setq new-newsgroups nil)
15436       (setq gnus-override-subscribe-method method)
15437       (when (and (gnus-check-server method)
15438                  (gnus-request-newgroups date method))
15439         (save-excursion
15440           (setq got-new t)
15441           (setq hashtb (gnus-make-hashtable 100))
15442           (set-buffer nntp-server-buffer)
15443           ;; Enter all the new groups into a hashtable.
15444           (gnus-active-to-gnus-format method hashtb 'ignore))
15445         ;; Now all new groups from `method' are in `hashtb'.
15446         (mapatoms
15447          (lambda (group-sym)
15448            (if (or (null (setq group (symbol-name group-sym)))
15449                    (not (boundp group-sym))
15450                    (null (symbol-value group-sym))
15451                    (gnus-gethash group gnus-newsrc-hashtb)
15452                    (member group gnus-zombie-list)
15453                    (member group gnus-killed-list))
15454                ;; The group is already known.
15455                ()
15456              ;; Make this group active.
15457              (when (symbol-value group-sym)
15458                (gnus-set-active group (symbol-value group-sym)))
15459              ;; Check whether we want it or not.
15460              (let ((do-sub (gnus-matches-options-n group)))
15461                (cond
15462                 ((eq do-sub 'subscribe)
15463                  (incf groups)
15464                  (gnus-sethash group group gnus-killed-hashtb)
15465                  (funcall gnus-subscribe-options-newsgroup-method group))
15466                 ((eq do-sub 'ignore)
15467                  nil)
15468                 (t
15469                  (incf groups)
15470                  (gnus-sethash group group gnus-killed-hashtb)
15471                  (if gnus-subscribe-hierarchical-interactive
15472                      (push group new-newsgroups)
15473                    (funcall gnus-subscribe-newsgroup-method group)))))))
15474          hashtb))
15475       (when new-newsgroups
15476         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15477     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15478     (when (> groups 0)
15479       (gnus-message 6 "%d new newsgroup%s arrived."
15480                     groups (if (> groups 1) "s have" " has")))
15481     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15482     got-new))
15483
15484 (defun gnus-check-first-time-used ()
15485   (if (or (> (length gnus-newsrc-alist) 1)
15486           (file-exists-p gnus-startup-file)
15487           (file-exists-p (concat gnus-startup-file ".el"))
15488           (file-exists-p (concat gnus-startup-file ".eld")))
15489       nil
15490     (gnus-message 6 "First time user; subscribing you to default groups")
15491     (unless (gnus-read-active-file-p)
15492       (gnus-read-active-file))
15493     (setq gnus-newsrc-last-checked-date (current-time-string))
15494     (let ((groups gnus-default-subscribed-newsgroups)
15495           group)
15496       (if (eq groups t)
15497           nil
15498         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15499         (mapatoms
15500          (lambda (sym)
15501            (if (null (setq group (symbol-name sym)))
15502                ()
15503              (let ((do-sub (gnus-matches-options-n group)))
15504                (cond
15505                 ((eq do-sub 'subscribe)
15506                  (gnus-sethash group group gnus-killed-hashtb)
15507                  (funcall gnus-subscribe-options-newsgroup-method group))
15508                 ((eq do-sub 'ignore)
15509                  nil)
15510                 (t
15511                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15512          gnus-active-hashtb)
15513         (while groups
15514           (if (gnus-active (car groups))
15515               (gnus-group-change-level
15516                (car groups) gnus-level-default-subscribed gnus-level-killed))
15517           (setq groups (cdr groups)))
15518         (gnus-group-make-help-group)
15519         (and gnus-novice-user
15520              (gnus-message 7 "`A k' to list killed groups"))))))
15521
15522 (defun gnus-subscribe-group (group previous &optional method)
15523   (gnus-group-change-level
15524    (if method
15525        (list t group gnus-level-default-subscribed nil nil method)
15526      group)
15527    gnus-level-default-subscribed gnus-level-killed previous t))
15528
15529 ;; `gnus-group-change-level' is the fundamental function for changing
15530 ;; subscription levels of newsgroups.  This might mean just changing
15531 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15532 ;; again, which subscribes/unsubscribes a group, which is equally
15533 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15534 ;; from 8-9 to 1-7 means that you remove the group from the list of
15535 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15536 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15537 ;; which is trivial.
15538 ;; ENTRY can either be a string (newsgroup name) or a list (if
15539 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15540 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15541 ;; entries.
15542 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15543 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15544 ;; after.
15545 (defun gnus-group-change-level (entry level &optional oldlevel
15546                                       previous fromkilled)
15547   (let (group info active num)
15548     ;; Glean what info we can from the arguments
15549     (if (consp entry)
15550         (if fromkilled (setq group (nth 1 entry))
15551           (setq group (car (nth 2 entry))))
15552       (setq group entry))
15553     (if (and (stringp entry)
15554              oldlevel
15555              (< oldlevel gnus-level-zombie))
15556         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15557     (if (and (not oldlevel)
15558              (consp entry))
15559         (setq oldlevel (gnus-info-level (nth 2 entry)))
15560       (setq oldlevel (or oldlevel 9)))
15561     (if (stringp previous)
15562         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15563
15564     (if (and (>= oldlevel gnus-level-zombie)
15565              (gnus-gethash group gnus-newsrc-hashtb))
15566         ;; We are trying to subscribe a group that is already
15567         ;; subscribed.
15568         ()                              ; Do nothing.
15569
15570       (or (gnus-ephemeral-group-p group)
15571           (gnus-dribble-enter
15572            (format "(gnus-group-change-level %S %S %S %S %S)"
15573                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15574
15575       ;; Then we remove the newgroup from any old structures, if needed.
15576       ;; If the group was killed, we remove it from the killed or zombie
15577       ;; list.  If not, and it is in fact going to be killed, we remove
15578       ;; it from the newsrc hash table and assoc.
15579       (cond
15580        ((>= oldlevel gnus-level-zombie)
15581         (if (= oldlevel gnus-level-zombie)
15582             (setq gnus-zombie-list (delete group gnus-zombie-list))
15583           (setq gnus-killed-list (delete group gnus-killed-list))))
15584        (t
15585         (if (and (>= level gnus-level-zombie)
15586                  entry)
15587             (progn
15588               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15589               (if (nth 3 entry)
15590                   (setcdr (gnus-gethash (car (nth 3 entry))
15591                                         gnus-newsrc-hashtb)
15592                           (cdr entry)))
15593               (setcdr (cdr entry) (cdddr entry))))))
15594
15595       ;; Finally we enter (if needed) the list where it is supposed to
15596       ;; go, and change the subscription level.  If it is to be killed,
15597       ;; we enter it into the killed or zombie list.
15598       (cond 
15599        ((>= level gnus-level-zombie)
15600         ;; Remove from the hash table.
15601         (gnus-sethash group nil gnus-newsrc-hashtb)
15602         ;; We do not enter foreign groups into the list of dead
15603         ;; groups.
15604         (unless (gnus-group-foreign-p group)
15605           (if (= level gnus-level-zombie)
15606               (setq gnus-zombie-list (cons group gnus-zombie-list))
15607             (setq gnus-killed-list (cons group gnus-killed-list)))))
15608        (t
15609         ;; If the list is to be entered into the newsrc assoc, and
15610         ;; it was killed, we have to create an entry in the newsrc
15611         ;; hashtb format and fix the pointers in the newsrc assoc.
15612         (if (< oldlevel gnus-level-zombie)
15613             ;; It was alive, and it is going to stay alive, so we
15614             ;; just change the level and don't change any pointers or
15615             ;; hash table entries.
15616             (setcar (cdaddr entry) level)
15617           (if (listp entry)
15618               (setq info (cdr entry)
15619                     num (car entry))
15620             (setq active (gnus-active group))
15621             (setq num
15622                   (if active (- (1+ (cdr active)) (car active)) t))
15623             ;; Check whether the group is foreign.  If so, the
15624             ;; foreign select method has to be entered into the
15625             ;; info.
15626             (let ((method (or gnus-override-subscribe-method
15627                               (gnus-group-method group))))
15628               (if (eq method gnus-select-method)
15629                   (setq info (list group level nil))
15630                 (setq info (list group level nil nil method)))))
15631           (unless previous
15632             (setq previous
15633                   (let ((p gnus-newsrc-alist))
15634                     (while (cddr p)
15635                       (setq p (cdr p)))
15636                     p)))
15637           (setq entry (cons info (cddr previous)))
15638           (if (cdr previous)
15639               (progn
15640                 (setcdr (cdr previous) entry)
15641                 (gnus-sethash group (cons num (cdr previous))
15642                               gnus-newsrc-hashtb))
15643             (setcdr previous entry)
15644             (gnus-sethash group (cons num previous)
15645                           gnus-newsrc-hashtb))
15646           (when (cdr entry)
15647             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15648       (when gnus-group-change-level-function
15649         (funcall gnus-group-change-level-function group level oldlevel)))))
15650
15651 (defun gnus-kill-newsgroup (newsgroup)
15652   "Obsolete function.  Kills a newsgroup."
15653   (gnus-group-change-level
15654    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15655
15656 (defun gnus-check-bogus-newsgroups (&optional confirm)
15657   "Remove bogus newsgroups.
15658 If CONFIRM is non-nil, the user has to confirm the deletion of every
15659 newsgroup."
15660   (let ((newsrc (cdr gnus-newsrc-alist))
15661         bogus group entry info)
15662     (gnus-message 5 "Checking bogus newsgroups...")
15663     (unless (gnus-read-active-file-p)
15664       (gnus-read-active-file))
15665     (when (gnus-read-active-file-p)
15666       ;; Find all bogus newsgroup that are subscribed.
15667       (while newsrc
15668         (setq info (pop newsrc)
15669               group (gnus-info-group info))
15670         (unless (or (gnus-active group) ; Active
15671                     (gnus-info-method info) ; Foreign
15672                     (and confirm
15673                          (not (gnus-y-or-n-p
15674                                (format "Remove bogus newsgroup: %s " group)))))
15675           ;; Found a bogus newsgroup.
15676           (push group bogus)))
15677       ;; Remove all bogus subscribed groups by first killing them, and
15678       ;; then removing them from the list of killed groups.
15679       (while bogus
15680         (when (setq entry (gnus-gethash (setq group (pop bogus))
15681                                         gnus-newsrc-hashtb))
15682           (gnus-group-change-level entry gnus-level-killed)
15683           (setq gnus-killed-list (delete group gnus-killed-list))))
15684       ;; Then we remove all bogus groups from the list of killed and
15685       ;; zombie groups.  They are are removed without confirmation.
15686       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15687             killed)
15688         (while dead-lists
15689           (setq killed (symbol-value (car dead-lists)))
15690           (while killed
15691             (unless (gnus-active (setq group (pop killed)))
15692               ;; The group is bogus.
15693               ;; !!!Slow as hell.
15694               (set (car dead-lists)
15695                    (delete group (symbol-value (car dead-lists))))))
15696           (setq dead-lists (cdr dead-lists))))
15697       (gnus-message 5 "Checking bogus newsgroups...done"))))
15698
15699 (defun gnus-check-duplicate-killed-groups ()
15700   "Remove duplicates from the list of killed groups."
15701   (interactive)
15702   (let ((killed gnus-killed-list))
15703     (while killed
15704       (gnus-message 9 "%d" (length killed))
15705       (setcdr killed (delete (car killed) (cdr killed)))
15706       (setq killed (cdr killed)))))
15707
15708 ;; We want to inline a function from gnus-cache, so we cheat here:
15709 (eval-when-compile
15710   (provide 'gnus)
15711   (require 'gnus-cache))
15712
15713 (defun gnus-get-unread-articles-in-group (info active &optional update)
15714   (when active
15715     ;; Allow the backend to update the info in the group.
15716     (when (and update 
15717                (gnus-request-update-info
15718                 info (gnus-find-method-for-group (gnus-info-group info))))
15719       (gnus-activate-group (gnus-info-group info) nil t))
15720     (let* ((range (gnus-info-read info))
15721            (num 0))
15722       ;; If a cache is present, we may have to alter the active info.
15723       (when (and gnus-use-cache info)
15724         (inline (gnus-cache-possibly-alter-active 
15725                  (gnus-info-group info) active)))
15726       ;; Modify the list of read articles according to what articles
15727       ;; are available; then tally the unread articles and add the
15728       ;; number to the group hash table entry.
15729       (cond
15730        ((zerop (cdr active))
15731         (setq num 0))
15732        ((not range)
15733         (setq num (- (1+ (cdr active)) (car active))))
15734        ((not (listp (cdr range)))
15735         ;; Fix a single (num . num) range according to the
15736         ;; active hash table.
15737         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15738         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15739         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15740         ;; Compute number of unread articles.
15741         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15742        (t
15743         ;; The read list is a list of ranges.  Fix them according to
15744         ;; the active hash table.
15745         ;; First peel off any elements that are below the lower
15746         ;; active limit.
15747         (while (and (cdr range)
15748                     (>= (car active)
15749                         (or (and (atom (cadr range)) (cadr range))
15750                             (caadr range))))
15751           (if (numberp (car range))
15752               (setcar range
15753                       (cons (car range)
15754                             (or (and (numberp (cadr range))
15755                                      (cadr range))
15756                                 (cdadr range))))
15757             (setcdr (car range)
15758                     (or (and (numberp (nth 1 range)) (nth 1 range))
15759                         (cdadr range))))
15760           (setcdr range (cddr range)))
15761         ;; Adjust the first element to be the same as the lower limit.
15762         (if (and (not (atom (car range)))
15763                  (< (cdar range) (car active)))
15764             (setcdr (car range) (1- (car active))))
15765         ;; Then we want to peel off any elements that are higher
15766         ;; than the upper active limit.
15767         (let ((srange range))
15768           ;; Go past all legal elements.
15769           (while (and (cdr srange)
15770                       (<= (or (and (atom (cadr srange))
15771                                    (cadr srange))
15772                               (caadr srange)) (cdr active)))
15773             (setq srange (cdr srange)))
15774           (if (cdr srange)
15775               ;; Nuke all remaining illegal elements.
15776               (setcdr srange nil))
15777
15778           ;; Adjust the final element.
15779           (if (and (not (atom (car srange)))
15780                    (> (cdar srange) (cdr active)))
15781               (setcdr (car srange) (cdr active))))
15782         ;; Compute the number of unread articles.
15783         (while range
15784           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15785                                       (cdar range)))
15786                               (or (and (atom (car range)) (car range))
15787                                   (caar range)))))
15788           (setq range (cdr range)))
15789         (setq num (max 0 (- (cdr active) num)))))
15790       ;; Set the number of unread articles.
15791       (when info
15792         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15793       num)))
15794
15795 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15796 ;; and compute how many unread articles there are in each group.
15797 (defun gnus-get-unread-articles (&optional level)
15798   (let* ((newsrc (cdr gnus-newsrc-alist))
15799          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15800          (foreign-level
15801           (min
15802            (cond ((and gnus-activate-foreign-newsgroups
15803                        (not (numberp gnus-activate-foreign-newsgroups)))
15804                   (1+ gnus-level-subscribed))
15805                  ((numberp gnus-activate-foreign-newsgroups)
15806                   gnus-activate-foreign-newsgroups)
15807                  (t 0))
15808            level))
15809          info group active method)
15810     (gnus-message 5 "Checking new news...")
15811
15812     (while newsrc
15813       (setq active (gnus-active (setq group (gnus-info-group
15814                                              (setq info (pop newsrc))))))
15815
15816       ;; Check newsgroups.  If the user doesn't want to check them, or
15817       ;; they can't be checked (for instance, if the news server can't
15818       ;; be reached) we just set the number of unread articles in this
15819       ;; newsgroup to t.  This means that Gnus thinks that there are
15820       ;; unread articles, but it has no idea how many.
15821       (if (and (setq method (gnus-info-method info))
15822                (not (gnus-server-equal
15823                      gnus-select-method
15824                      (setq method (gnus-server-get-method nil method))))
15825                (not (gnus-secondary-method-p method)))
15826           ;; These groups are foreign.  Check the level.
15827           (when (<= (gnus-info-level info) foreign-level)
15828             (setq active (gnus-activate-group group 'scan))
15829             (unless (inline (gnus-virtual-group-p group))
15830               (inline (gnus-close-group group)))
15831             (when (fboundp (intern (concat (symbol-name (car method))
15832                                            "-request-update-info")))
15833               (inline (gnus-request-update-info info method))))
15834         ;; These groups are native or secondary.
15835         (when (and (<= (gnus-info-level info) level)
15836                    (not gnus-read-active-file))
15837           (setq active (gnus-activate-group group 'scan))
15838           (inline (gnus-close-group group))))
15839
15840       ;; Get the number of unread articles in the group.
15841       (if active
15842           (inline (gnus-get-unread-articles-in-group info active))
15843         ;; The group couldn't be reached, so we nix out the number of
15844         ;; unread articles and stuff.
15845         (gnus-set-active group nil)
15846         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15847
15848     (gnus-message 5 "Checking new news...done")))
15849
15850 ;; Create a hash table out of the newsrc alist.  The `car's of the
15851 ;; alist elements are used as keys.
15852 (defun gnus-make-hashtable-from-newsrc-alist ()
15853   (let ((alist gnus-newsrc-alist)
15854         (ohashtb gnus-newsrc-hashtb)
15855         prev)
15856     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15857     (setq alist
15858           (setq prev (setq gnus-newsrc-alist
15859                            (if (equal (caar gnus-newsrc-alist)
15860                                       "dummy.group")
15861                                gnus-newsrc-alist
15862                              (cons (list "dummy.group" 0 nil) alist)))))
15863     (while alist
15864       (gnus-sethash
15865        (caar alist)
15866        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15867              prev)
15868        gnus-newsrc-hashtb)
15869       (setq prev alist
15870             alist (cdr alist)))))
15871
15872 (defun gnus-make-hashtable-from-killed ()
15873   "Create a hash table from the killed and zombie lists."
15874   (let ((lists '(gnus-killed-list gnus-zombie-list))
15875         list)
15876     (setq gnus-killed-hashtb
15877           (gnus-make-hashtable
15878            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15879     (while (setq list (pop lists))
15880       (setq list (symbol-value list))
15881       (while list
15882         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15883
15884 (defun gnus-activate-group (group &optional scan dont-check)
15885   ;; Check whether a group has been activated or not.
15886   ;; If SCAN, request a scan of that group as well.
15887   (let ((method (gnus-find-method-for-group group))
15888         active)
15889     (and (gnus-check-server method)
15890          ;; We escape all bugs and quit here to make it possible to
15891          ;; continue if a group is so out-there that it reports bugs
15892          ;; and stuff.
15893          (progn
15894            (and scan
15895                 (gnus-check-backend-function 'request-scan (car method))
15896                 (gnus-request-scan group method))
15897            t)
15898          (condition-case ()
15899              (gnus-request-group group dont-check)
15900         ;   (error nil)
15901            (quit nil))
15902          (save-excursion
15903            (set-buffer nntp-server-buffer)
15904            (goto-char (point-min))
15905            ;; Parse the result we got from `gnus-request-group'.
15906            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15907                 (progn
15908                   (goto-char (match-beginning 1))
15909                   (gnus-set-active
15910                    group (setq active (cons (read (current-buffer))
15911                                             (read (current-buffer)))))
15912                   ;; Return the new active info.
15913                   active))))))
15914
15915 (defun gnus-update-read-articles (group unread)
15916   "Update the list of read and ticked articles in GROUP using the
15917 UNREAD and TICKED lists.
15918 Note: UNSELECTED has to be sorted over `<'.
15919 Returns whether the updating was successful."
15920   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15921          (entry (gnus-gethash group gnus-newsrc-hashtb))
15922          (info (nth 2 entry))
15923          (prev 1)
15924          (unread (sort (copy-sequence unread) '<))
15925          read)
15926     (if (or (not info) (not active))
15927         ;; There is no info on this group if it was, in fact,
15928         ;; killed.  Gnus stores no information on killed groups, so
15929         ;; there's nothing to be done.
15930         ;; One could store the information somewhere temporarily,
15931         ;; perhaps...  Hmmm...
15932         ()
15933       ;; Remove any negative articles numbers.
15934       (while (and unread (< (car unread) 0))
15935         (setq unread (cdr unread)))
15936       ;; Remove any expired article numbers
15937       (while (and unread (< (car unread) (car active)))
15938         (setq unread (cdr unread)))
15939       ;; Compute the ranges of read articles by looking at the list of
15940       ;; unread articles.
15941       (while unread
15942         (if (/= (car unread) prev)
15943             (setq read (cons (if (= prev (1- (car unread))) prev
15944                                (cons prev (1- (car unread)))) read)))
15945         (setq prev (1+ (car unread)))
15946         (setq unread (cdr unread)))
15947       (when (<= prev (cdr active))
15948         (setq read (cons (cons prev (cdr active)) read)))
15949       ;; Enter this list into the group info.
15950       (gnus-info-set-read
15951        info (if (> (length read) 1) (nreverse read) read))
15952       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15953       (gnus-get-unread-articles-in-group info (gnus-active group))
15954       t)))
15955
15956 (defun gnus-make-articles-unread (group articles)
15957   "Mark ARTICLES in GROUP as unread."
15958   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15959                           (gnus-gethash (gnus-group-real-name group)
15960                                         gnus-newsrc-hashtb))))
15961          (ranges (gnus-info-read info))
15962          news article)
15963     (while articles
15964       (when (gnus-member-of-range
15965              (setq article (pop articles)) ranges)
15966         (setq news (cons article news))))
15967     (when news
15968       (gnus-info-set-read
15969        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15970       (gnus-group-update-group group t))))
15971
15972 ;; Enter all dead groups into the hashtb.
15973 (defun gnus-update-active-hashtb-from-killed ()
15974   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15975         (lists (list gnus-killed-list gnus-zombie-list))
15976         killed)
15977     (while lists
15978       (setq killed (car lists))
15979       (while killed
15980         (gnus-sethash (car killed) nil hashtb)
15981         (setq killed (cdr killed)))
15982       (setq lists (cdr lists)))))
15983
15984 (defun gnus-get-killed-groups ()
15985   "Go through the active hashtb and all all unknown groups as killed."
15986   ;; First make sure active file has been read.
15987   (unless (gnus-read-active-file-p)
15988     (let ((gnus-read-active-file t))
15989       (gnus-read-active-file)))
15990   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15991   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15992   (mapatoms
15993    (lambda (sym)
15994      (let ((groups 0)
15995            (group (symbol-name sym)))
15996        (if (or (null group)
15997                (gnus-gethash group gnus-killed-hashtb)
15998                (gnus-gethash group gnus-newsrc-hashtb))
15999            ()
16000          (let ((do-sub (gnus-matches-options-n group)))
16001            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16002                ()
16003              (setq groups (1+ groups))
16004              (setq gnus-killed-list
16005                    (cons group gnus-killed-list))
16006              (gnus-sethash group group gnus-killed-hashtb))))))
16007    gnus-active-hashtb))
16008
16009 ;; Get the active file(s) from the backend(s).
16010 (defun gnus-read-active-file ()
16011   (gnus-group-set-mode-line)
16012   (let ((methods 
16013          (append
16014           (if (gnus-check-server gnus-select-method)
16015               ;; The native server is available.
16016               (cons gnus-select-method gnus-secondary-select-methods)
16017             ;; The native server is down, so we just do the
16018             ;; secondary ones.
16019             gnus-secondary-select-methods)
16020           ;; Also read from the archive server.
16021           (when gnus-message-archive-method
16022             (list "archive"))))
16023         list-type)
16024     (setq gnus-have-read-active-file nil)
16025     (save-excursion
16026       (set-buffer nntp-server-buffer)
16027       (while methods
16028         (let* ((method (if (stringp (car methods))
16029                            (gnus-server-get-method nil (car methods))
16030                          (car methods)))
16031                (where (nth 1 method))
16032                (mesg (format "Reading active file%s via %s..."
16033                              (if (and where (not (zerop (length where))))
16034                                  (concat " from " where) "")
16035                              (car method))))
16036           (gnus-message 5 mesg)
16037           (when (gnus-check-server method)
16038             ;; Request that the backend scan its incoming messages.
16039             (and (gnus-check-backend-function 'request-scan (car method))
16040                  (gnus-request-scan nil method))
16041             (cond
16042              ((and (eq gnus-read-active-file 'some)
16043                    (gnus-check-backend-function 'retrieve-groups (car method)))
16044               (let ((newsrc (cdr gnus-newsrc-alist))
16045                     (gmethod (gnus-server-get-method nil method))
16046                     groups info)
16047                 (while (setq info (pop newsrc))
16048                   (when (gnus-server-equal
16049                          (gnus-find-method-for-group 
16050                           (gnus-info-group info) info)
16051                          gmethod)
16052                     (push (gnus-group-real-name (gnus-info-group info)) 
16053                           groups)))
16054                 (when groups
16055                   (gnus-check-server method)
16056                   (setq list-type (gnus-retrieve-groups groups method))
16057                   (cond
16058                    ((not list-type)
16059                     (gnus-error
16060                      1.2 "Cannot read partial active file from %s server."
16061                      (car method)))
16062                    ((eq list-type 'active)
16063                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16064                    (t
16065                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16066              (t
16067               (if (not (gnus-request-list method))
16068                   (unless (equal method gnus-message-archive-method)
16069                     (gnus-error 1 "Cannot read active file from %s server."
16070                                 (car method)))
16071                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16072                 ;; We mark this active file as read.
16073                 (push method gnus-have-read-active-file)
16074                 (gnus-message 5 "%sdone" mesg))))))
16075         (setq methods (cdr methods))))))
16076
16077 ;; Read an active file and place the results in `gnus-active-hashtb'.
16078 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16079   (unless method
16080     (setq method gnus-select-method))
16081   (let ((cur (current-buffer))
16082         (hashtb (or hashtb
16083                     (if (and gnus-active-hashtb
16084                              (not (equal method gnus-select-method)))
16085                         gnus-active-hashtb
16086                       (setq gnus-active-hashtb
16087                             (if (equal method gnus-select-method)
16088                                 (gnus-make-hashtable
16089                                  (count-lines (point-min) (point-max)))
16090                               (gnus-make-hashtable 4096)))))))
16091     ;; Delete unnecessary lines.
16092     (goto-char (point-min))
16093     (while (search-forward "\nto." nil t)
16094       (delete-region (1+ (match-beginning 0))
16095                      (progn (forward-line 1) (point))))
16096     (or (string= gnus-ignored-newsgroups "")
16097         (progn
16098           (goto-char (point-min))
16099           (delete-matching-lines gnus-ignored-newsgroups)))
16100     ;; Make the group names readable as a lisp expression even if they
16101     ;; contain special characters.
16102     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16103     (goto-char (point-max))
16104     (while (re-search-backward "[][';?()#]" nil t)
16105       (insert ?\\))
16106     ;; If these are groups from a foreign select method, we insert the
16107     ;; group prefix in front of the group names.
16108     (and method (not (gnus-server-equal
16109                       (gnus-server-get-method nil method)
16110                       (gnus-server-get-method nil gnus-select-method)))
16111          (let ((prefix (gnus-group-prefixed-name "" method)))
16112            (goto-char (point-min))
16113            (while (and (not (eobp))
16114                        (progn (insert prefix)
16115                               (zerop (forward-line 1)))))))
16116     ;; Store the active file in a hash table.
16117     (goto-char (point-min))
16118     (if (string-match "%[oO]" gnus-group-line-format)
16119         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16120         ;; If we want information on moderated groups, we use this
16121         ;; loop...
16122         (let* ((mod-hashtb (make-vector 7 0))
16123                (m (intern "m" mod-hashtb))
16124                group max min)
16125           (while (not (eobp))
16126             (condition-case nil
16127                 (progn
16128                   (narrow-to-region (point) (gnus-point-at-eol))
16129                   (setq group (let ((obarray hashtb)) (read cur)))
16130                   (if (and (numberp (setq max (read cur)))
16131                            (numberp (setq min (read cur)))
16132                            (progn
16133                              (skip-chars-forward " \t")
16134                              (not
16135                               (or (= (following-char) ?=)
16136                                   (= (following-char) ?x)
16137                                   (= (following-char) ?j)))))
16138                       (set group (cons min max))
16139                     (set group nil))
16140                   ;; Enter moderated groups into a list.
16141                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16142                       (setq gnus-moderated-list
16143                             (cons (symbol-name group) gnus-moderated-list))))
16144               (error
16145                (and group
16146                     (symbolp group)
16147                     (set group nil))))
16148             (widen)
16149             (forward-line 1)))
16150       ;; And if we do not care about moderation, we use this loop,
16151       ;; which is faster.
16152       (let (group max min)
16153         (while (not (eobp))
16154           (condition-case ()
16155               (progn
16156                 (narrow-to-region (point) (gnus-point-at-eol))
16157                 ;; group gets set to a symbol interned in the hash table
16158                 ;; (what a hack!!) - jwz
16159                 (setq group (let ((obarray hashtb)) (read cur)))
16160                 (if (and (numberp (setq max (read cur)))
16161                          (numberp (setq min (read cur)))
16162                          (progn
16163                            (skip-chars-forward " \t")
16164                            (not
16165                             (or (= (following-char) ?=)
16166                                 (= (following-char) ?x)
16167                                 (= (following-char) ?j)))))
16168                     (set group (cons min max))
16169                   (set group nil)))
16170             (error
16171              (progn
16172                (and group
16173                     (symbolp group)
16174                     (set group nil))
16175                (or ignore-errors
16176                    (gnus-message 3 "Warning - illegal active: %s"
16177                                  (buffer-substring
16178                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16179           (widen)
16180           (forward-line 1))))))
16181
16182 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16183   ;; Parse a "groups" active file.
16184   (let ((cur (current-buffer))
16185         (hashtb (or hashtb
16186                     (if (and method gnus-active-hashtb)
16187                         gnus-active-hashtb
16188                       (setq gnus-active-hashtb
16189                             (gnus-make-hashtable
16190                              (count-lines (point-min) (point-max)))))))
16191         (prefix (and method
16192                      (not (gnus-server-equal
16193                            (gnus-server-get-method nil method)
16194                            (gnus-server-get-method nil gnus-select-method)))
16195                      (gnus-group-prefixed-name "" method))))
16196
16197     (goto-char (point-min))
16198     ;; We split this into to separate loops, one with the prefix
16199     ;; and one without to speed the reading up somewhat.
16200     (if prefix
16201         (let (min max opoint group)
16202           (while (not (eobp))
16203             (condition-case ()
16204                 (progn
16205                   (read cur) (read cur)
16206                   (setq min (read cur)
16207                         max (read cur)
16208                         opoint (point))
16209                   (skip-chars-forward " \t")
16210                   (insert prefix)
16211                   (goto-char opoint)
16212                   (set (let ((obarray hashtb)) (read cur))
16213                        (cons min max)))
16214               (error (and group (symbolp group) (set group nil))))
16215             (forward-line 1)))
16216       (let (min max group)
16217         (while (not (eobp))
16218           (condition-case ()
16219               (if (= (following-char) ?2)
16220                   (progn
16221                     (read cur) (read cur)
16222                     (setq min (read cur)
16223                           max (read cur))
16224                     (set (setq group (let ((obarray hashtb)) (read cur)))
16225                          (cons min max))))
16226             (error (and group (symbolp group) (set group nil))))
16227           (forward-line 1))))))
16228
16229 (defun gnus-read-newsrc-file (&optional force)
16230   "Read startup file.
16231 If FORCE is non-nil, the .newsrc file is read."
16232   ;; Reset variables that might be defined in the .newsrc.eld file.
16233   (let ((variables gnus-variable-list))
16234     (while variables
16235       (set (car variables) nil)
16236       (setq variables (cdr variables))))
16237   (let* ((newsrc-file gnus-current-startup-file)
16238          (quick-file (concat newsrc-file ".el")))
16239     (save-excursion
16240       ;; We always load the .newsrc.eld file.  If always contains
16241       ;; much information that can not be gotten from the .newsrc
16242       ;; file (ticked articles, killed groups, foreign methods, etc.)
16243       (gnus-read-newsrc-el-file quick-file)
16244
16245       (if (and (file-exists-p gnus-current-startup-file)
16246                (or force
16247                    (and (file-newer-than-file-p newsrc-file quick-file)
16248                         (file-newer-than-file-p newsrc-file
16249                                                 (concat quick-file "d")))
16250                    (not gnus-newsrc-alist)))
16251           ;; We read the .newsrc file.  Note that if there if a
16252           ;; .newsrc.eld file exists, it has already been read, and
16253           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16254           ;; the .newsrc file, Gnus will only use the information it
16255           ;; can find there for changing the data already read -
16256           ;; ie. reading the .newsrc file will not trash the data
16257           ;; already read (except for read articles).
16258           (save-excursion
16259             (gnus-message 5 "Reading %s..." newsrc-file)
16260             (set-buffer (find-file-noselect newsrc-file))
16261             (buffer-disable-undo (current-buffer))
16262             (gnus-newsrc-to-gnus-format)
16263             (kill-buffer (current-buffer))
16264             (gnus-message 5 "Reading %s...done" newsrc-file)))
16265
16266       ;; Read any slave files.
16267       (unless gnus-slave
16268         (gnus-master-read-slave-newsrc))
16269       
16270       ;; Convert old to new.
16271       (gnus-convert-old-newsrc))))
16272
16273 (defun gnus-continuum-version (version)
16274   "Return VERSION as a floating point number."
16275   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16276     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16277            (number (match-string 2 version))
16278            major minor least)
16279       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16280       (setq major (string-to-number (match-string 1 number)))
16281       (setq minor (string-to-number (match-string 2 number)))
16282       (setq least (if (match-beginning 3)
16283                       (string-to-number (match-string 3 number))
16284                     0))
16285       (string-to-number
16286        (if (zerop major)
16287            (format "%s00%02d%02d"
16288                    (cond 
16289                     ((string= alpha "(ding)") "4.99")
16290                     ((string= alpha "September") "5.01")
16291                     ((string= alpha "Red") "5.03"))
16292                    minor least)
16293          (format "%d.%02d%20d" major minor least))))))
16294
16295 (defun gnus-convert-old-newsrc ()
16296   "Convert old newsrc into the new format, if needed."
16297   (let ((fcv (and gnus-newsrc-file-version
16298                   (gnus-continuum-version gnus-newsrc-file-version))))
16299     (cond
16300      ;; No .newsrc.eld file was loaded.
16301      ((null fcv) nil)
16302      ;; Gnus 5 .newsrc.eld was loaded.
16303      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16304       (gnus-convert-old-ticks)))))
16305
16306 (defun gnus-convert-old-ticks ()
16307   (let ((newsrc (cdr gnus-newsrc-alist))
16308         marks info dormant ticked)
16309     (while (setq info (pop newsrc))
16310       (when (setq marks (gnus-info-marks info))
16311         (setq dormant (cdr (assq 'dormant marks))
16312               ticked (cdr (assq 'tick marks)))
16313         (when (or dormant ticked)
16314           (gnus-info-set-read
16315            info
16316            (gnus-add-to-range
16317             (gnus-info-read info)
16318             (nconc (gnus-uncompress-range dormant)
16319                    (gnus-uncompress-range ticked)))))))))
16320
16321 (defun gnus-read-newsrc-el-file (file)
16322   (let ((ding-file (concat file "d")))
16323     ;; We always, always read the .eld file.
16324     (gnus-message 5 "Reading %s..." ding-file)
16325     (let (gnus-newsrc-assoc)
16326       (condition-case nil
16327           (load ding-file t t t)
16328         (error
16329          (gnus-error 1 "Error in %s" ding-file)))
16330       (when gnus-newsrc-assoc
16331         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16332     (gnus-make-hashtable-from-newsrc-alist)
16333     (when (file-newer-than-file-p file ding-file)
16334       ;; Old format quick file
16335       (gnus-message 5 "Reading %s..." file)
16336       ;; The .el file is newer than the .eld file, so we read that one
16337       ;; as well.
16338       (gnus-read-old-newsrc-el-file file))))
16339
16340 ;; Parse the old-style quick startup file
16341 (defun gnus-read-old-newsrc-el-file (file)
16342   (let (newsrc killed marked group m info)
16343     (prog1
16344         (let ((gnus-killed-assoc nil)
16345               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16346           (prog1
16347               (condition-case nil
16348                   (load file t t t)
16349                 (error nil))
16350             (setq newsrc gnus-newsrc-assoc
16351                   killed gnus-killed-assoc
16352                   marked gnus-marked-assoc)))
16353       (setq gnus-newsrc-alist nil)
16354       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16355         (if info
16356             (progn
16357               (gnus-info-set-read info (cddr group))
16358               (gnus-info-set-level
16359                info (if (nth 1 group) gnus-level-default-subscribed
16360                       gnus-level-default-unsubscribed))
16361               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16362           (push (setq info
16363                       (list (car group)
16364                             (if (nth 1 group) gnus-level-default-subscribed
16365                               gnus-level-default-unsubscribed)
16366                             (cddr group)))
16367                 gnus-newsrc-alist))
16368         ;; Copy marks into info.
16369         (when (setq m (assoc (car group) marked))
16370           (unless (nthcdr 3 info)
16371             (nconc info (list nil)))
16372           (gnus-info-set-marks
16373            info (list (cons 'tick (gnus-compress-sequence 
16374                                    (sort (cdr m) '<) t))))))
16375       (setq newsrc killed)
16376       (while newsrc
16377         (setcar newsrc (caar newsrc))
16378         (setq newsrc (cdr newsrc)))
16379       (setq gnus-killed-list killed))
16380     ;; The .el file version of this variable does not begin with
16381     ;; "options", while the .eld version does, so we just add it if it
16382     ;; isn't there.
16383     (and
16384      gnus-newsrc-options
16385      (progn
16386        (and (not (string-match "^ *options" gnus-newsrc-options))
16387             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16388        (and (not (string-match "\n$" gnus-newsrc-options))
16389             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16390        ;; Finally, if we read some options lines, we parse them.
16391        (or (string= gnus-newsrc-options "")
16392            (gnus-newsrc-parse-options gnus-newsrc-options))))
16393
16394     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16395     (gnus-make-hashtable-from-newsrc-alist)))
16396
16397 (defun gnus-make-newsrc-file (file)
16398   "Make server dependent file name by catenating FILE and server host name."
16399   (let* ((file (expand-file-name file nil))
16400          (real-file (concat file "-" (nth 1 gnus-select-method))))
16401     (if (or (file-exists-p real-file)
16402             (file-exists-p (concat real-file ".el"))
16403             (file-exists-p (concat real-file ".eld")))
16404         real-file file)))
16405
16406 (defun gnus-newsrc-to-gnus-format ()
16407   (setq gnus-newsrc-options "")
16408   (setq gnus-newsrc-options-n nil)
16409
16410   (or gnus-active-hashtb
16411       (setq gnus-active-hashtb (make-vector 4095 0)))
16412   (let ((buf (current-buffer))
16413         (already-read (> (length gnus-newsrc-alist) 1))
16414         group subscribed options-symbol newsrc Options-symbol
16415         symbol reads num1)
16416     (goto-char (point-min))
16417     ;; We intern the symbol `options' in the active hashtb so that we
16418     ;; can `eq' against it later.
16419     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16420     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16421
16422     (while (not (eobp))
16423       ;; We first read the first word on the line by narrowing and
16424       ;; then reading into `gnus-active-hashtb'.  Most groups will
16425       ;; already exist in that hashtb, so this will save some string
16426       ;; space.
16427       (narrow-to-region
16428        (point)
16429        (progn (skip-chars-forward "^ \t!:\n") (point)))
16430       (goto-char (point-min))
16431       (setq symbol
16432             (and (/= (point-min) (point-max))
16433                  (let ((obarray gnus-active-hashtb)) (read buf))))
16434       (widen)
16435       ;; Now, the symbol we have read is either `options' or a group
16436       ;; name.  If it is an options line, we just add it to a string.
16437       (cond
16438        ((or (eq symbol options-symbol)
16439             (eq symbol Options-symbol))
16440         (setq gnus-newsrc-options
16441               ;; This concating is quite inefficient, but since our
16442               ;; thorough studies show that approx 99.37% of all
16443               ;; .newsrc files only contain a single options line, we
16444               ;; don't give a damn, frankly, my dear.
16445               (concat gnus-newsrc-options
16446                       (buffer-substring
16447                        (gnus-point-at-bol)
16448                        ;; Options may continue on the next line.
16449                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16450                                 (progn (beginning-of-line) (point)))
16451                            (point)))))
16452         (forward-line -1))
16453        (symbol
16454         ;; Group names can be just numbers.  
16455         (when (numberp symbol) 
16456           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16457         (or (boundp symbol) (set symbol nil))
16458         ;; It was a group name.
16459         (setq subscribed (= (following-char) ?:)
16460               group (symbol-name symbol)
16461               reads nil)
16462         (if (eolp)
16463             ;; If the line ends here, this is clearly a buggy line, so
16464             ;; we put point a the beginning of line and let the cond
16465             ;; below do the error handling.
16466             (beginning-of-line)
16467           ;; We skip to the beginning of the ranges.
16468           (skip-chars-forward "!: \t"))
16469         ;; We are now at the beginning of the list of read articles.
16470         ;; We read them range by range.
16471         (while
16472             (cond
16473              ((looking-at "[0-9]+")
16474               ;; We narrow and read a number instead of buffer-substring/
16475               ;; string-to-int because it's faster.  narrow/widen is
16476               ;; faster than save-restriction/narrow, and save-restriction
16477               ;; produces a garbage object.
16478               (setq num1 (progn
16479                            (narrow-to-region (match-beginning 0) (match-end 0))
16480                            (read buf)))
16481               (widen)
16482               ;; If the next character is a dash, then this is a range.
16483               (if (= (following-char) ?-)
16484                   (progn
16485                     ;; We read the upper bound of the range.
16486                     (forward-char 1)
16487                     (if (not (looking-at "[0-9]+"))
16488                         ;; This is a buggy line, by we pretend that
16489                         ;; it's kinda OK.  Perhaps the user should be
16490                         ;; dinged?
16491                         (setq reads (cons num1 reads))
16492                       (setq reads
16493                             (cons
16494                              (cons num1
16495                                    (progn
16496                                      (narrow-to-region (match-beginning 0)
16497                                                        (match-end 0))
16498                                      (read buf)))
16499                              reads))
16500                       (widen)))
16501                 ;; It was just a simple number, so we add it to the
16502                 ;; list of ranges.
16503                 (setq reads (cons num1 reads)))
16504               ;; If the next char in ?\n, then we have reached the end
16505               ;; of the line and return nil.
16506               (/= (following-char) ?\n))
16507              ((= (following-char) ?\n)
16508               ;; End of line, so we end.
16509               nil)
16510              (t
16511               ;; Not numbers and not eol, so this might be a buggy
16512               ;; line...
16513               (or (eobp)
16514                   ;; If it was eob instead of ?\n, we allow it.
16515                   (progn
16516                     ;; The line was buggy.
16517                     (setq group nil)
16518                     (gnus-error 3.1 "Mangled line: %s"
16519                                 (buffer-substring (gnus-point-at-bol)
16520                                                   (gnus-point-at-eol)))))
16521               nil))
16522           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16523           ;; we allow them, because it's a common mistake to put a
16524           ;; space after the comma.
16525           (skip-chars-forward ", "))
16526
16527         ;; We have already read .newsrc.eld, so we gently update the
16528         ;; data in the hash table with the information we have just
16529         ;; read.
16530         (when group
16531           (let ((info (gnus-get-info group))
16532                 level)
16533             (if info
16534                 ;; There is an entry for this file in the alist.
16535                 (progn
16536                   (gnus-info-set-read info (nreverse reads))
16537                   ;; We update the level very gently.  In fact, we
16538                   ;; only change it if there's been a status change
16539                   ;; from subscribed to unsubscribed, or vice versa.
16540                   (setq level (gnus-info-level info))
16541                   (cond ((and (<= level gnus-level-subscribed)
16542                               (not subscribed))
16543                          (setq level (if reads
16544                                          gnus-level-default-unsubscribed
16545                                        (1+ gnus-level-default-unsubscribed))))
16546                         ((and (> level gnus-level-subscribed) subscribed)
16547                          (setq level gnus-level-default-subscribed)))
16548                   (gnus-info-set-level info level))
16549               ;; This is a new group.
16550               (setq info (list group
16551                                (if subscribed
16552                                    gnus-level-default-subscribed
16553                                  (if reads
16554                                      (1+ gnus-level-subscribed)
16555                                    gnus-level-default-unsubscribed))
16556                                (nreverse reads))))
16557             (setq newsrc (cons info newsrc))))))
16558       (forward-line 1))
16559
16560     (setq newsrc (nreverse newsrc))
16561
16562     (if (not already-read)
16563         ()
16564       ;; We now have two newsrc lists - `newsrc', which is what we
16565       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16566       ;; what we've read from .newsrc.eld.  We have to merge these
16567       ;; lists.  We do this by "attaching" any (foreign) groups in the
16568       ;; gnus-newsrc-alist to the (native) group that precedes them.
16569       (let ((rc (cdr gnus-newsrc-alist))
16570             (prev gnus-newsrc-alist)
16571             entry mentry)
16572         (while rc
16573           (or (null (nth 4 (car rc)))   ; It's a native group.
16574               (assoc (caar rc) newsrc) ; It's already in the alist.
16575               (if (setq entry (assoc (caar prev) newsrc))
16576                   (setcdr (setq mentry (memq entry newsrc))
16577                           (cons (car rc) (cdr mentry)))
16578                 (setq newsrc (cons (car rc) newsrc))))
16579           (setq prev rc
16580                 rc (cdr rc)))))
16581
16582     (setq gnus-newsrc-alist newsrc)
16583     ;; We make the newsrc hashtb.
16584     (gnus-make-hashtable-from-newsrc-alist)
16585
16586     ;; Finally, if we read some options lines, we parse them.
16587     (or (string= gnus-newsrc-options "")
16588         (gnus-newsrc-parse-options gnus-newsrc-options))))
16589
16590 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16591 ;; The return value will be a list on the form
16592 ;; ((regexp1 . ignore)
16593 ;;  (regexp2 . subscribe)...)
16594 ;; When handling new newsgroups, groups that match a `ignore' regexp
16595 ;; will be ignored, and groups that match a `subscribe' regexp will be
16596 ;; subscribed.  A line like
16597 ;; options -n !all rec.all
16598 ;; will lead to a list that looks like
16599 ;; (("^rec\\..+" . subscribe)
16600 ;;  ("^.+" . ignore))
16601 ;; So all "rec.*" groups will be subscribed, while all the other
16602 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16603 ;; different from "options -n rec.all !all".
16604 (defun gnus-newsrc-parse-options (options)
16605   (let (out eol)
16606     (save-excursion
16607       (gnus-set-work-buffer)
16608       (insert (regexp-quote options))
16609       ;; First we treat all continuation lines.
16610       (goto-char (point-min))
16611       (while (re-search-forward "\n[ \t]+" nil t)
16612         (replace-match " " t t))
16613       ;; Then we transform all "all"s into ".+"s.
16614       (goto-char (point-min))
16615       (while (re-search-forward "\\ball\\b" nil t)
16616         (replace-match ".+" t t))
16617       (goto-char (point-min))
16618       ;; We remove all other options than the "-n" ones.
16619       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16620         (replace-match " ")
16621         (forward-char -1))
16622       (goto-char (point-min))
16623
16624       ;; We are only interested in "options -n" lines - we
16625       ;; ignore the other option lines.
16626       (while (re-search-forward "[ \t]-n" nil t)
16627         (setq eol
16628               (or (save-excursion
16629                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16630                          (- (point) 2)))
16631                   (gnus-point-at-eol)))
16632         ;; Search for all "words"...
16633         (while (re-search-forward "[^ \t,\n]+" eol t)
16634           (if (= (char-after (match-beginning 0)) ?!)
16635               ;; If the word begins with a bang (!), this is a "not"
16636               ;; spec.  We put this spec (minus the bang) and the
16637               ;; symbol `ignore' into the list.
16638               (setq out (cons (cons (concat
16639                                      "^" (buffer-substring
16640                                           (1+ (match-beginning 0))
16641                                           (match-end 0)))
16642                                     'ignore) out))
16643             ;; There was no bang, so this is a "yes" spec.
16644             (setq out (cons (cons (concat "^" (match-string 0))
16645                                   'subscribe) out)))))
16646
16647       (setq gnus-newsrc-options-n out))))
16648
16649 (defun gnus-save-newsrc-file (&optional force)
16650   "Save .newsrc file."
16651   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16652   ;; from the variable gnus-newsrc-alist.
16653   (when (and (or gnus-newsrc-alist gnus-killed-list)
16654              gnus-current-startup-file)
16655     (save-excursion
16656       (if (and (or gnus-use-dribble-file gnus-slave)
16657                (not force)
16658                (or (not gnus-dribble-buffer)
16659                    (not (buffer-name gnus-dribble-buffer))
16660                    (zerop (save-excursion
16661                             (set-buffer gnus-dribble-buffer)
16662                             (buffer-size)))))
16663           (gnus-message 4 "(No changes need to be saved)")
16664         (run-hooks 'gnus-save-newsrc-hook)
16665         (if gnus-slave
16666             (gnus-slave-save-newsrc)
16667           ;; Save .newsrc.
16668           (when gnus-save-newsrc-file
16669             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16670             (gnus-gnus-to-newsrc-format)
16671             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16672           ;; Save .newsrc.eld.
16673           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16674           (make-local-variable 'version-control)
16675           (setq version-control 'never)
16676           (setq buffer-file-name
16677                 (concat gnus-current-startup-file ".eld"))
16678           (setq default-directory (file-name-directory buffer-file-name))
16679           (gnus-add-current-to-buffer-list)
16680           (buffer-disable-undo (current-buffer))
16681           (erase-buffer)
16682           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16683           (gnus-gnus-to-quick-newsrc-format)
16684           (run-hooks 'gnus-save-quick-newsrc-hook)
16685           (save-buffer)
16686           (kill-buffer (current-buffer))
16687           (gnus-message
16688            5 "Saving %s.eld...done" gnus-current-startup-file))
16689         (gnus-dribble-delete-file)
16690         (gnus-group-set-mode-line)))))
16691
16692 (defun gnus-gnus-to-quick-newsrc-format ()
16693   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16694   (insert ";; Gnus startup file.\n")
16695   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16696   (insert ";; to read .newsrc.\n")
16697   (insert "(setq gnus-newsrc-file-version "
16698           (prin1-to-string gnus-version) ")\n")
16699   (let ((variables
16700          (if gnus-save-killed-list gnus-variable-list
16701            ;; Remove the `gnus-killed-list' from the list of variables
16702            ;; to be saved, if required.
16703            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16704         ;; Peel off the "dummy" group.
16705         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16706         variable)
16707     ;; Insert the variables into the file.
16708     (while variables
16709       (when (and (boundp (setq variable (pop variables)))
16710                  (symbol-value variable))
16711         (insert "(setq " (symbol-name variable) " '")
16712         (prin1 (symbol-value variable) (current-buffer))
16713         (insert ")\n")))))
16714
16715 (defun gnus-gnus-to-newsrc-format ()
16716   ;; Generate and save the .newsrc file.
16717   (save-excursion
16718     (set-buffer (create-file-buffer gnus-current-startup-file))
16719     (let ((newsrc (cdr gnus-newsrc-alist))
16720           (standard-output (current-buffer))
16721           info ranges range method)
16722       (setq buffer-file-name gnus-current-startup-file)
16723       (setq default-directory (file-name-directory buffer-file-name))
16724       (buffer-disable-undo (current-buffer))
16725       (erase-buffer)
16726       ;; Write options.
16727       (if gnus-newsrc-options (insert gnus-newsrc-options))
16728       ;; Write subscribed and unsubscribed.
16729       (while (setq info (pop newsrc))
16730         ;; Don't write foreign groups to .newsrc.
16731         (when (or (null (setq method (gnus-info-method info)))
16732                   (equal method "native")
16733                   (gnus-server-equal method gnus-select-method))
16734           (insert (gnus-info-group info)
16735                   (if (> (gnus-info-level info) gnus-level-subscribed)
16736                       "!" ":"))
16737           (when (setq ranges (gnus-info-read info))
16738             (insert " ")
16739             (if (not (listp (cdr ranges)))
16740                 (if (= (car ranges) (cdr ranges))
16741                     (princ (car ranges))
16742                   (princ (car ranges))
16743                   (insert "-")
16744                   (princ (cdr ranges)))
16745               (while (setq range (pop ranges))
16746                 (if (or (atom range) (= (car range) (cdr range)))
16747                     (princ (or (and (atom range) range) (car range)))
16748                   (princ (car range))
16749                   (insert "-")
16750                   (princ (cdr range)))
16751                 (if ranges (insert ",")))))
16752           (insert "\n")))
16753       (make-local-variable 'version-control)
16754       (setq version-control 'never)
16755       ;; It has been reported that sometime the modtime on the .newsrc
16756       ;; file seems to be off.  We really do want to overwrite it, so
16757       ;; we clear the modtime here before saving.  It's a bit odd,
16758       ;; though...
16759       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16760       ;; delete the silly thing entirely first.  but this fails to provide
16761       ;; such niceties as .newsrc~ creation.
16762       (if gnus-modtime-botch
16763           (delete-file gnus-startup-file)
16764         (clear-visited-file-modtime))
16765       (run-hooks 'gnus-save-standard-newsrc-hook)
16766       (save-buffer)
16767       (kill-buffer (current-buffer)))))
16768
16769 \f
16770 ;;;
16771 ;;; Slave functions.
16772 ;;;
16773
16774 (defun gnus-slave-save-newsrc ()
16775   (save-excursion
16776     (set-buffer gnus-dribble-buffer)
16777     (let ((slave-name
16778            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16779       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16780
16781 (defun gnus-master-read-slave-newsrc ()
16782   (let ((slave-files
16783          (directory-files
16784           (file-name-directory gnus-current-startup-file)
16785           t (concat
16786              "^" (regexp-quote
16787                   (concat
16788                    (file-name-nondirectory gnus-current-startup-file)
16789                    "-slave-")))
16790           t))
16791         file)
16792     (if (not slave-files)
16793         ()                              ; There are no slave files to read.
16794       (gnus-message 7 "Reading slave newsrcs...")
16795       (save-excursion
16796         (set-buffer (get-buffer-create " *gnus slave*"))
16797         (buffer-disable-undo (current-buffer))
16798         (setq slave-files
16799               (sort (mapcar (lambda (file)
16800                               (list (nth 5 (file-attributes file)) file))
16801                             slave-files)
16802                     (lambda (f1 f2)
16803                       (or (< (caar f1) (caar f2))
16804                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16805         (while slave-files
16806           (erase-buffer)
16807           (setq file (nth 1 (car slave-files)))
16808           (insert-file-contents file)
16809           (if (condition-case ()
16810                   (progn
16811                     (eval-buffer (current-buffer))
16812                     t)
16813                 (error
16814                  (gnus-error 3.2 "Possible error in %s" file)
16815                  nil))
16816               (or gnus-slave ; Slaves shouldn't delete these files.
16817                   (condition-case ()
16818                       (delete-file file)
16819                     (error nil))))
16820           (setq slave-files (cdr slave-files))))
16821       (gnus-message 7 "Reading slave newsrcs...done"))))
16822
16823 \f
16824 ;;;
16825 ;;; Group description.
16826 ;;;
16827
16828 (defun gnus-read-all-descriptions-files ()
16829   (let ((methods (cons gnus-select-method 
16830                        (nconc
16831                         (when gnus-message-archive-method
16832                           (list "archive"))
16833                         gnus-secondary-select-methods))))
16834     (while methods
16835       (gnus-read-descriptions-file (car methods))
16836       (setq methods (cdr methods)))
16837     t))
16838
16839 (defun gnus-read-descriptions-file (&optional method)
16840   (let ((method (or method gnus-select-method)))
16841     (when (stringp method)
16842       (setq method (gnus-server-to-method method)))
16843     ;; We create the hashtable whether we manage to read the desc file
16844     ;; to avoid trying to re-read after a failed read.
16845     (or gnus-description-hashtb
16846         (setq gnus-description-hashtb
16847               (gnus-make-hashtable (length gnus-active-hashtb))))
16848     ;; Mark this method's desc file as read.
16849     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16850                   gnus-description-hashtb)
16851
16852     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16853     (cond
16854      ((not (gnus-check-server method))
16855       (gnus-message 1 "Couldn't open server")
16856       nil)
16857      ((not (gnus-request-list-newsgroups method))
16858       (gnus-message 1 "Couldn't read newsgroups descriptions")
16859       nil)
16860      (t
16861       (let (group)
16862         (save-excursion
16863           (save-restriction
16864             (set-buffer nntp-server-buffer)
16865             (goto-char (point-min))
16866             (if (or (search-forward "\n.\n" nil t)
16867                     (goto-char (point-max)))
16868                 (progn
16869                   (beginning-of-line)
16870                   (narrow-to-region (point-min) (point))))
16871             (goto-char (point-min))
16872             (while (not (eobp))
16873               ;; If we get an error, we set group to 0, which is not a
16874               ;; symbol...
16875               (setq group
16876                     (condition-case ()
16877                         (let ((obarray gnus-description-hashtb))
16878                           ;; Group is set to a symbol interned in this
16879                           ;; hash table.
16880                           (read nntp-server-buffer))
16881                       (error 0)))
16882               (skip-chars-forward " \t")
16883               ;; ...  which leads to this line being effectively ignored.
16884               (and (symbolp group)
16885                    (set group (buffer-substring
16886                                (point) (progn (end-of-line) (point)))))
16887               (forward-line 1))))
16888         (gnus-message 5 "Reading descriptions file...done")
16889         t)))))
16890
16891 (defun gnus-group-get-description (group)
16892   "Get the description of a group by sending XGTITLE to the server."
16893   (when (gnus-request-group-description group)
16894     (save-excursion
16895       (set-buffer nntp-server-buffer)
16896       (goto-char (point-min))
16897       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16898         (match-string 1)))))
16899
16900 \f
16901 ;;;
16902 ;;; Buffering of read articles.
16903 ;;;
16904
16905 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16906 (defvar gnus-backlog-articles nil)
16907 (defvar gnus-backlog-hashtb nil)
16908
16909 (defun gnus-backlog-buffer ()
16910   "Return the backlog buffer."
16911   (or (get-buffer gnus-backlog-buffer)
16912       (save-excursion
16913         (set-buffer (get-buffer-create gnus-backlog-buffer))
16914         (buffer-disable-undo (current-buffer))
16915         (setq buffer-read-only t)
16916         (gnus-add-current-to-buffer-list)
16917         (get-buffer gnus-backlog-buffer))))
16918
16919 (defun gnus-backlog-setup ()
16920   "Initialize backlog variables."
16921   (unless gnus-backlog-hashtb
16922     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16923
16924 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16925
16926 (defun gnus-backlog-shutdown ()
16927   "Clear all backlog variables and buffers."
16928   (when (get-buffer gnus-backlog-buffer)
16929     (kill-buffer gnus-backlog-buffer))
16930   (setq gnus-backlog-hashtb nil
16931         gnus-backlog-articles nil))
16932
16933 (defun gnus-backlog-enter-article (group number buffer)
16934   (gnus-backlog-setup)
16935   (let ((ident (intern (concat group ":" (int-to-string number))
16936                        gnus-backlog-hashtb))
16937         b)
16938     (if (memq ident gnus-backlog-articles)
16939         () ; It's already kept.
16940       ;; Remove the oldest article, if necessary.
16941       (and (numberp gnus-keep-backlog)
16942            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16943            (gnus-backlog-remove-oldest-article))
16944       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16945       ;; Insert the new article.
16946       (save-excursion
16947         (set-buffer (gnus-backlog-buffer))
16948         (let (buffer-read-only)
16949           (goto-char (point-max))
16950           (or (bolp) (insert "\n"))
16951           (setq b (point))
16952           (insert-buffer-substring buffer)
16953           ;; Tag the beginning of the article with the ident.
16954           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
16955
16956 (defun gnus-backlog-remove-oldest-article ()
16957   (save-excursion
16958     (set-buffer (gnus-backlog-buffer))
16959     (goto-char (point-min))
16960     (if (zerop (buffer-size))
16961         () ; The buffer is empty.
16962       (let ((ident (get-text-property (point) 'gnus-backlog))
16963             buffer-read-only)
16964         ;; Remove the ident from the list of articles.
16965         (when ident
16966           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16967         ;; Delete the article itself.
16968         (delete-region
16969          (point) (next-single-property-change
16970                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16971
16972 (defun gnus-backlog-remove-article (group number)
16973   "Remove article NUMBER in GROUP from the backlog."
16974   (when (numberp number)
16975     (gnus-backlog-setup)
16976     (let ((ident (intern (concat group ":" (int-to-string number))
16977                          gnus-backlog-hashtb))
16978           beg end)
16979       (when (memq ident gnus-backlog-articles)
16980         ;; It was in the backlog.
16981         (save-excursion
16982           (set-buffer (gnus-backlog-buffer))
16983           (let (buffer-read-only)
16984             (when (setq beg (text-property-any
16985                              (point-min) (point-max) 'gnus-backlog
16986                              ident))
16987               ;; Find the end (i. e., the beginning of the next article).
16988               (setq end
16989                     (next-single-property-change
16990                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16991               (delete-region beg end)
16992               ;; Return success.
16993               t)))))))
16994
16995 (defun gnus-backlog-request-article (group number buffer)
16996   (when (numberp number)
16997     (gnus-backlog-setup)
16998     (let ((ident (intern (concat group ":" (int-to-string number))
16999                          gnus-backlog-hashtb))
17000           beg end)
17001       (when (memq ident gnus-backlog-articles)
17002         ;; It was in the backlog.
17003         (save-excursion
17004           (set-buffer (gnus-backlog-buffer))
17005           (if (not (setq beg (text-property-any
17006                               (point-min) (point-max) 'gnus-backlog
17007                               ident)))
17008               ;; It wasn't in the backlog after all.
17009               (ignore
17010                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17011             ;; Find the end (i. e., the beginning of the next article).
17012             (setq end
17013                   (next-single-property-change
17014                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17015         (let ((buffer-read-only nil))
17016           (erase-buffer)
17017           (insert-buffer-substring gnus-backlog-buffer beg end)
17018           t)))))
17019
17020 ;; Allow redefinition of Gnus functions.
17021
17022 (gnus-ems-redefine)
17023
17024 (provide 'gnus)
17025
17026 ;;; gnus.el ends here