*** 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-number "5.2.4"
1727   "Version number for this version of Gnus.")
1728
1729 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1730   "Version string for this version of Gnus.")
1731
1732 (defvar gnus-info-nodes
1733   '((gnus-group-mode "(gnus)The Group Buffer")
1734     (gnus-summary-mode "(gnus)The Summary Buffer")
1735     (gnus-article-mode "(gnus)The Article Buffer"))
1736   "Assoc list of major modes and related Info nodes.")
1737
1738 (defvar gnus-group-buffer "*Group*")
1739 (defvar gnus-summary-buffer "*Summary*")
1740 (defvar gnus-article-buffer "*Article*")
1741 (defvar gnus-server-buffer "*Server*")
1742
1743 (defvar gnus-work-buffer " *gnus work*")
1744
1745 (defvar gnus-original-article-buffer " *Original Article*")
1746 (defvar gnus-original-article nil)
1747
1748 (defvar gnus-buffer-list nil
1749   "Gnus buffers that should be killed on exit.")
1750
1751 (defvar gnus-slave nil
1752   "Whether this Gnus is a slave or not.")
1753
1754 (defvar gnus-variable-list
1755   '(gnus-newsrc-options gnus-newsrc-options-n
1756     gnus-newsrc-last-checked-date
1757     gnus-newsrc-alist gnus-server-alist
1758     gnus-killed-list gnus-zombie-list
1759     gnus-topic-topology gnus-topic-alist
1760     gnus-format-specs)
1761   "Gnus variables saved in the quick startup file.")
1762
1763 (defvar gnus-newsrc-options nil
1764   "Options line in the .newsrc file.")
1765
1766 (defvar gnus-newsrc-options-n nil
1767   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1768
1769 (defvar gnus-newsrc-last-checked-date nil
1770   "Date Gnus last asked server for new newsgroups.")
1771
1772 (defvar gnus-topic-topology nil
1773   "The complete topic hierarchy.")
1774
1775 (defvar gnus-topic-alist nil
1776   "The complete topic-group alist.")
1777
1778 (defvar gnus-newsrc-alist nil
1779   "Assoc list of read articles.
1780 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1781
1782 (defvar gnus-newsrc-hashtb nil
1783   "Hashtable of gnus-newsrc-alist.")
1784
1785 (defvar gnus-killed-list nil
1786   "List of killed newsgroups.")
1787
1788 (defvar gnus-killed-hashtb nil
1789   "Hash table equivalent of gnus-killed-list.")
1790
1791 (defvar gnus-zombie-list nil
1792   "List of almost dead newsgroups.")
1793
1794 (defvar gnus-description-hashtb nil
1795   "Descriptions of newsgroups.")
1796
1797 (defvar gnus-list-of-killed-groups nil
1798   "List of newsgroups that have recently been killed by the user.")
1799
1800 (defvar gnus-active-hashtb nil
1801   "Hashtable of active articles.")
1802
1803 (defvar gnus-moderated-list nil
1804   "List of moderated newsgroups.")
1805
1806 (defvar gnus-group-marked nil)
1807
1808 (defvar gnus-current-startup-file nil
1809   "Startup file for the current host.")
1810
1811 (defvar gnus-last-search-regexp nil
1812   "Default regexp for article search command.")
1813
1814 (defvar gnus-last-shell-command nil
1815   "Default shell command on article.")
1816
1817 (defvar gnus-current-select-method nil
1818   "The current method for selecting a newsgroup.")
1819
1820 (defvar gnus-group-list-mode nil)
1821
1822 (defvar gnus-article-internal-prepare-hook nil)
1823
1824 (defvar gnus-newsgroup-name nil)
1825 (defvar gnus-newsgroup-begin nil)
1826 (defvar gnus-newsgroup-end nil)
1827 (defvar gnus-newsgroup-last-rmail nil)
1828 (defvar gnus-newsgroup-last-mail nil)
1829 (defvar gnus-newsgroup-last-folder nil)
1830 (defvar gnus-newsgroup-last-file nil)
1831 (defvar gnus-newsgroup-auto-expire nil)
1832 (defvar gnus-newsgroup-active nil)
1833
1834 (defvar gnus-newsgroup-data nil)
1835 (defvar gnus-newsgroup-data-reverse nil)
1836 (defvar gnus-newsgroup-limit nil)
1837 (defvar gnus-newsgroup-limits nil)
1838
1839 (defvar gnus-newsgroup-unreads nil
1840   "List of unread articles in the current newsgroup.")
1841
1842 (defvar gnus-newsgroup-unselected nil
1843   "List of unselected unread articles in the current newsgroup.")
1844
1845 (defvar gnus-newsgroup-reads nil
1846   "Alist of read articles and article marks in the current newsgroup.")
1847
1848 (defvar gnus-newsgroup-expunged-tally nil)
1849
1850 (defvar gnus-newsgroup-marked nil
1851   "List of ticked articles in the current newsgroup (a subset of unread art).")
1852
1853 (defvar gnus-newsgroup-killed nil
1854   "List of ranges of articles that have been through the scoring process.")
1855
1856 (defvar gnus-newsgroup-cached nil
1857   "List of articles that come from the article cache.")
1858
1859 (defvar gnus-newsgroup-saved nil
1860   "List of articles that have been saved.")
1861
1862 (defvar gnus-newsgroup-kill-headers nil)
1863
1864 (defvar gnus-newsgroup-replied nil
1865   "List of articles that have been replied to in the current newsgroup.")
1866
1867 (defvar gnus-newsgroup-expirable nil
1868   "List of articles in the current newsgroup that can be expired.")
1869
1870 (defvar gnus-newsgroup-processable nil
1871   "List of articles in the current newsgroup that can be processed.")
1872
1873 (defvar gnus-newsgroup-bookmarks nil
1874   "List of articles in the current newsgroup that have bookmarks.")
1875
1876 (defvar gnus-newsgroup-dormant nil
1877   "List of dormant articles in the current newsgroup.")
1878
1879 (defvar gnus-newsgroup-scored nil
1880   "List of scored articles in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-headers nil
1883   "List of article headers in the current newsgroup.")
1884
1885 (defvar gnus-newsgroup-threads nil)
1886
1887 (defvar gnus-newsgroup-prepared nil
1888   "Whether the current group has been prepared properly.")
1889
1890 (defvar gnus-newsgroup-ancient nil
1891   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1892
1893 (defvar gnus-newsgroup-sparse nil)
1894
1895 (defvar gnus-current-article nil)
1896 (defvar gnus-article-current nil)
1897 (defvar gnus-current-headers nil)
1898 (defvar gnus-have-all-headers nil)
1899 (defvar gnus-last-article nil)
1900 (defvar gnus-newsgroup-history nil)
1901 (defvar gnus-current-kill-article nil)
1902
1903 ;; Save window configuration.
1904 (defvar gnus-prev-winconf nil)
1905
1906 (defvar gnus-summary-mark-positions nil)
1907 (defvar gnus-group-mark-positions nil)
1908
1909 (defvar gnus-reffed-article-number nil)
1910
1911 ;;; Let the byte-compiler know that we know about this variable.
1912 (defvar rmail-default-rmail-file)
1913
1914 (defvar gnus-cache-removable-articles nil)
1915
1916 (defvar gnus-dead-summary nil)
1917
1918 (defconst gnus-summary-local-variables
1919   '(gnus-newsgroup-name
1920     gnus-newsgroup-begin gnus-newsgroup-end
1921     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1922     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1923     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1924     gnus-newsgroup-unselected gnus-newsgroup-marked
1925     gnus-newsgroup-reads gnus-newsgroup-saved
1926     gnus-newsgroup-replied gnus-newsgroup-expirable
1927     gnus-newsgroup-processable gnus-newsgroup-killed
1928     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1929     gnus-newsgroup-headers gnus-newsgroup-threads
1930     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1931     gnus-current-article gnus-current-headers gnus-have-all-headers
1932     gnus-last-article gnus-article-internal-prepare-hook
1933     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1934     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1935     gnus-newsgroup-async gnus-thread-expunge-below
1936     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1937     (gnus-summary-mark-below . 0)
1938     gnus-newsgroup-active gnus-scores-exclude-files
1939     gnus-newsgroup-history gnus-newsgroup-ancient
1940     gnus-newsgroup-sparse
1941     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1942     gnus-newsgroup-adaptive-score-file
1943     (gnus-newsgroup-expunged-tally . 0)
1944     gnus-cache-removable-articles gnus-newsgroup-cached
1945     gnus-newsgroup-data gnus-newsgroup-data-reverse
1946     gnus-newsgroup-limit gnus-newsgroup-limits)
1947   "Variables that are buffer-local to the summary buffers.")
1948
1949 (defconst gnus-bug-message
1950   "Sending a bug report to the Gnus Towers.
1951 ========================================
1952
1953 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1954 be sent to the Gnus Bug Exterminators.
1955
1956 At the bottom of the buffer you'll see lots of variable settings.
1957 Please do not delete those.  They will tell the Bug People what your
1958 environment is, so that it will be easier to locate the bugs.
1959
1960 If you have found a bug that makes Emacs go \"beep\", set
1961 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1962 and include the backtrace in your bug report.
1963
1964 Please describe the bug in annoying, painstaking detail.
1965
1966 Thank you for your help in stamping out bugs.
1967 ")
1968
1969 ;;; End of variables.
1970
1971 ;; Define some autoload functions Gnus might use.
1972 (eval-and-compile
1973
1974   ;; This little mapcar goes through the list below and marks the
1975   ;; symbols in question as autoloaded functions.
1976   (mapcar
1977    (lambda (package)
1978      (let ((interactive (nth 1 (memq ':interactive package))))
1979        (mapcar
1980         (lambda (function)
1981           (let (keymap)
1982             (when (consp function)
1983               (setq keymap (car (memq 'keymap function)))
1984               (setq function (car function)))
1985             (autoload function (car package) nil interactive keymap)))
1986         (if (eq (nth 1 package) ':interactive)
1987             (cdddr package)
1988           (cdr package)))))
1989    '(("metamail" metamail-buffer)
1990      ("info" Info-goto-node)
1991      ("hexl" hexl-hex-string-to-integer)
1992      ("pp" pp pp-to-string pp-eval-expression)
1993      ("mail-extr" mail-extract-address-components)
1994      ("nnmail" nnmail-split-fancy nnmail-article-group)
1995      ("nnvirtual" nnvirtual-catchup-group)
1996      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1997       timezone-make-sortable-date timezone-make-time-string)
1998      ("sendmail" mail-position-on-field mail-setup)
1999      ("rmailout" rmail-output)
2000      ("rnewspost" news-mail-other-window news-reply-yank-original
2001       news-caesar-buffer-body)
2002      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2003       rmail-show-message)
2004      ("gnus-soup" :interactive t
2005       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2006       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2007      ("nnsoup" nnsoup-pack-replies)
2008      ("gnus-scomo" :interactive t gnus-score-mode)
2009      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2010       gnus-Folder-save-name gnus-folder-save-name)
2011      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2012      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2013       gnus-server-make-menu-bar gnus-article-make-menu-bar
2014       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2015       gnus-summary-highlight-line gnus-carpal-setup-buffer
2016       gnus-group-highlight-line
2017       gnus-article-add-button gnus-insert-next-page-button
2018       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2019      ("gnus-vis" :interactive t
2020       gnus-article-push-button gnus-article-press-button
2021       gnus-article-highlight gnus-article-highlight-some
2022       gnus-article-highlight-headers gnus-article-highlight-signature
2023       gnus-article-add-buttons gnus-article-add-buttons-to-head
2024       gnus-article-next-button gnus-article-prev-button)
2025      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2026       gnus-demon-add-disconnection gnus-demon-add-handler
2027       gnus-demon-remove-handler)
2028      ("gnus-demon" :interactive t
2029       gnus-demon-init gnus-demon-cancel)
2030      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2031       gnus-tree-open gnus-tree-close)
2032      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2033       gnus-nocem-unwanted-article-p)
2034      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2035      ("gnus-srvr" gnus-browse-foreign-server)
2036      ("gnus-cite" :interactive t
2037       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2038       gnus-article-hide-citation gnus-article-fill-cited-article
2039       gnus-article-hide-citation-in-followups)
2040      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2041       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2042       gnus-execute gnus-expunge)
2043      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2044       gnus-cache-possibly-remove-articles gnus-cache-request-article
2045       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2046       gnus-cache-enter-remove-article gnus-cached-article-p
2047       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2048      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2049       gnus-cache-remove-article)
2050      ("gnus-score" :interactive t
2051       gnus-summary-increase-score gnus-summary-lower-score
2052       gnus-score-flush-cache gnus-score-close
2053       gnus-score-raise-same-subject-and-select
2054       gnus-score-raise-same-subject gnus-score-default
2055       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2056       gnus-score-lower-same-subject gnus-score-lower-thread
2057       gnus-possibly-score-headers gnus-summary-raise-score 
2058       gnus-summary-set-score gnus-summary-current-score)
2059      ("gnus-score"
2060       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2061       gnus-current-score-file-nondirectory gnus-score-adaptive
2062       gnus-score-find-trace gnus-score-file-name)
2063      ("gnus-edit" :interactive t gnus-score-customize)
2064      ("gnus-topic" :interactive t gnus-topic-mode)
2065      ("gnus-topic" gnus-topic-remove-group)
2066      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2067      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2068      ("gnus-uu" :interactive t
2069       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2070       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2071       gnus-uu-mark-by-regexp gnus-uu-mark-all
2072       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2073       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2074       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2075       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2076       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2077       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2078       gnus-uu-decode-binhex-view)
2079      ("gnus-msg" (gnus-summary-send-map keymap)
2080       gnus-mail-yank-original gnus-mail-send-and-exit
2081       gnus-sendmail-setup-mail gnus-article-mail
2082       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2083      ("gnus-msg" :interactive t
2084       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2085       gnus-summary-followup gnus-summary-followup-with-original
2086       gnus-summary-followup-and-reply
2087       gnus-summary-followup-and-reply-with-original
2088       gnus-summary-cancel-article gnus-summary-supersede-article
2089       gnus-post-news gnus-inews-news gnus-cancel-news
2090       gnus-summary-reply gnus-summary-reply-with-original
2091       gnus-summary-mail-forward gnus-summary-mail-other-window
2092       gnus-bug)
2093      ("gnus-picon" :interactive t gnus-article-display-picons
2094       gnus-group-display-picons gnus-picons-article-display-x-face)
2095      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2096       gnus-grouplens-mode)
2097      ("gnus-vm" gnus-vm-mail-setup)
2098      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2099       gnus-summary-save-article-vm gnus-yank-article))))
2100
2101 \f
2102
2103 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2104 ;; If you want the cursor to go somewhere else, set these two
2105 ;; functions in some startup hook to whatever you want.
2106 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2107 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2108
2109 ;;; Various macros and substs.
2110
2111 (defun gnus-header-from (header)
2112   (mail-header-from header))
2113
2114 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2115   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2116   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2117         (w (make-symbol "w"))
2118         (buf (make-symbol "buf")))
2119     `(let* ((,tempvar (selected-window))
2120             (,buf ,buffer)
2121             (,w (get-buffer-window ,buf 'visible)))
2122        (unwind-protect
2123            (progn
2124              (if ,w
2125                  (select-window ,w)
2126                (pop-to-buffer ,buf))
2127              ,@forms)
2128          (select-window ,tempvar)))))
2129
2130 (defmacro gnus-gethash (string hashtable)
2131   "Get hash value of STRING in HASHTABLE."
2132   `(symbol-value (intern-soft ,string ,hashtable)))
2133
2134 (defmacro gnus-sethash (string value hashtable)
2135   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2136   `(set (intern ,string ,hashtable) ,value))
2137
2138 (defmacro gnus-intern-safe (string hashtable)
2139   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2140   `(let ((symbol (intern ,string ,hashtable)))
2141      (or (boundp symbol)
2142          (set symbol nil))
2143      symbol))
2144
2145 (defmacro gnus-group-unread (group)
2146   "Get the currently computed number of unread articles in GROUP."
2147   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2148
2149 (defmacro gnus-group-entry (group)
2150   "Get the newsrc entry for GROUP."
2151   `(gnus-gethash ,group gnus-newsrc-hashtb))
2152
2153 (defmacro gnus-active (group)
2154   "Get active info on GROUP."
2155   `(gnus-gethash ,group gnus-active-hashtb))
2156
2157 (defmacro gnus-set-active (group active)
2158   "Set GROUP's active info."
2159   `(gnus-sethash ,group ,active gnus-active-hashtb))
2160
2161 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2162 ;;   function `substring' might cut on a middle of multi-octet
2163 ;;   character.
2164 (defun gnus-truncate-string (str width)
2165   (substring str 0 width))
2166
2167 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2168 ;; to limit the length of a string.  This function is necessary since
2169 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2170 (defsubst gnus-limit-string (str width)
2171   (if (> (length str) width)
2172       (substring str 0 width)
2173     str))
2174
2175 (defsubst gnus-simplify-subject-re (subject)
2176   "Remove \"Re:\" from subject lines."
2177   (if (string-match "^[Rr][Ee]: *" subject)
2178       (substring subject (match-end 0))
2179     subject))
2180
2181 (defsubst gnus-functionp (form)
2182   "Return non-nil if FORM is funcallable."
2183   (or (and (symbolp form) (fboundp form))
2184       (and (listp form) (eq (car form) 'lambda))))
2185
2186 (defsubst gnus-goto-char (point)
2187   (and point (goto-char point)))
2188
2189 (defmacro gnus-buffer-exists-p (buffer)
2190   `(let ((buffer ,buffer))
2191      (and buffer
2192           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2193                    buffer))))
2194
2195 (defmacro gnus-kill-buffer (buffer)
2196   `(let ((buf ,buffer))
2197      (if (gnus-buffer-exists-p buf)
2198          (kill-buffer buf))))
2199
2200 (defsubst gnus-point-at-bol ()
2201   "Return point at the beginning of the line."
2202   (let ((p (point)))
2203     (beginning-of-line)
2204     (prog1
2205         (point)
2206       (goto-char p))))
2207
2208 (defsubst gnus-point-at-eol ()
2209   "Return point at the end of the line."
2210   (let ((p (point)))
2211     (end-of-line)
2212     (prog1
2213         (point)
2214       (goto-char p))))
2215
2216 (defun gnus-alive-p ()
2217   "Say whether Gnus is running or not."
2218   (and gnus-group-buffer
2219        (get-buffer gnus-group-buffer)))
2220
2221 ;; Delete the current line (and the next N lines.);
2222 (defmacro gnus-delete-line (&optional n)
2223   `(delete-region (progn (beginning-of-line) (point))
2224                   (progn (forward-line ,(or n 1)) (point))))
2225
2226 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2227 (defvar gnus-init-inhibit nil)
2228 (defun gnus-read-init-file (&optional inhibit-next)
2229   (if gnus-init-inhibit
2230       (setq gnus-init-inhibit nil)
2231     (setq gnus-init-inhibit inhibit-next)
2232     (and gnus-init-file
2233          (or (and (file-exists-p gnus-init-file)
2234                   ;; Don't try to load a directory.
2235                   (not (file-directory-p gnus-init-file)))
2236              (file-exists-p (concat gnus-init-file ".el"))
2237              (file-exists-p (concat gnus-init-file ".elc")))
2238          (condition-case var
2239              (load gnus-init-file nil t)
2240            (error
2241             (error "Error in %s: %s" gnus-init-file var))))))
2242
2243 ;; Info access macros.
2244
2245 (defmacro gnus-info-group (info)
2246   `(nth 0 ,info))
2247 (defmacro gnus-info-rank (info)
2248   `(nth 1 ,info))
2249 (defmacro gnus-info-read (info)
2250   `(nth 2 ,info))
2251 (defmacro gnus-info-marks (info)
2252   `(nth 3 ,info))
2253 (defmacro gnus-info-method (info)
2254   `(nth 4 ,info))
2255 (defmacro gnus-info-params (info)
2256   `(nth 5 ,info))
2257
2258 (defmacro gnus-info-level (info)
2259   `(let ((rank (gnus-info-rank ,info)))
2260      (if (consp rank)
2261          (car rank)
2262        rank)))
2263 (defmacro gnus-info-score (info)
2264   `(let ((rank (gnus-info-rank ,info)))
2265      (or (and (consp rank) (cdr rank)) 0)))
2266
2267 (defmacro gnus-info-set-group (info group)
2268   `(setcar ,info ,group))
2269 (defmacro gnus-info-set-rank (info rank)
2270   `(setcar (nthcdr 1 ,info) ,rank))
2271 (defmacro gnus-info-set-read (info read)
2272   `(setcar (nthcdr 2 ,info) ,read))
2273 (defmacro gnus-info-set-marks (info marks)
2274   `(setcar (nthcdr 3 ,info) ,marks))
2275 (defmacro gnus-info-set-method (info method)
2276   `(setcar (nthcdr 4 ,info) ,method))
2277 (defmacro gnus-info-set-params (info params)
2278   `(setcar (nthcdr 5 ,info) ,params))
2279
2280 (defmacro gnus-info-set-level (info level)
2281   `(let ((rank (cdr ,info)))
2282      (if (consp (car rank))
2283          (setcar (car rank) ,level)
2284        (setcar rank ,level))))
2285 (defmacro gnus-info-set-score (info score)
2286   `(let ((rank (cdr ,info)))
2287      (if (consp (car rank))
2288          (setcdr (car rank) ,score)
2289        (setcar rank (cons (car rank) ,score)))))
2290
2291 (defmacro gnus-get-info (group)
2292   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2293
2294 (defun gnus-byte-code (func)
2295   "Return a form that can be `eval'ed based on FUNC."
2296   (let ((fval (symbol-function func)))
2297     (if (byte-code-function-p fval)
2298         (let ((flist (append fval nil)))
2299           (setcar flist 'byte-code)
2300           flist)
2301       (cons 'progn (cddr fval)))))
2302
2303 ;;; Load the compatability functions.
2304
2305 (require 'gnus-cus)
2306 (require 'gnus-ems)
2307
2308 \f
2309 ;;;
2310 ;;; Shutdown
2311 ;;;
2312
2313 (defvar gnus-shutdown-alist nil)
2314
2315 (defun gnus-add-shutdown (function &rest symbols)
2316   "Run FUNCTION whenever one of SYMBOLS is shut down."
2317   (push (cons function symbols) gnus-shutdown-alist))
2318
2319 (defun gnus-shutdown (symbol)
2320   "Shut down everything that waits for SYMBOL."
2321   (let ((alist gnus-shutdown-alist)
2322         entry)
2323     (while (setq entry (pop alist))
2324       (when (memq symbol (cdr entry))
2325         (funcall (car entry))))))
2326
2327 \f
2328
2329 ;; Format specs.  The chunks below are the machine-generated forms
2330 ;; that are to be evaled as the result of the default format strings.
2331 ;; We write them in here to get them byte-compiled.  That way the
2332 ;; default actions will be quite fast, while still retaining the full
2333 ;; flexibility of the user-defined format specs.
2334
2335 ;; First we have lots of dummy defvars to let the compiler know these
2336 ;; are really dynamic variables.
2337
2338 (defvar gnus-tmp-unread)
2339 (defvar gnus-tmp-replied)
2340 (defvar gnus-tmp-score-char)
2341 (defvar gnus-tmp-indentation)
2342 (defvar gnus-tmp-opening-bracket)
2343 (defvar gnus-tmp-lines)
2344 (defvar gnus-tmp-name)
2345 (defvar gnus-tmp-closing-bracket)
2346 (defvar gnus-tmp-subject-or-nil)
2347 (defvar gnus-tmp-subject)
2348 (defvar gnus-tmp-marked)
2349 (defvar gnus-tmp-marked-mark)
2350 (defvar gnus-tmp-subscribed)
2351 (defvar gnus-tmp-process-marked)
2352 (defvar gnus-tmp-number-of-unread)
2353 (defvar gnus-tmp-group-name)
2354 (defvar gnus-tmp-group)
2355 (defvar gnus-tmp-article-number)
2356 (defvar gnus-tmp-unread-and-unselected)
2357 (defvar gnus-tmp-news-method)
2358 (defvar gnus-tmp-news-server)
2359 (defvar gnus-tmp-article-number)
2360 (defvar gnus-mouse-face)
2361 (defvar gnus-mouse-face-prop)
2362
2363 (defun gnus-summary-line-format-spec ()
2364   (insert gnus-tmp-unread gnus-tmp-replied
2365           gnus-tmp-score-char gnus-tmp-indentation)
2366   (gnus-put-text-property
2367    (point)
2368    (progn
2369      (insert
2370       gnus-tmp-opening-bracket
2371       (format "%4d: %-20s"
2372               gnus-tmp-lines
2373               (if (> (length gnus-tmp-name) 20)
2374                   (substring gnus-tmp-name 0 20)
2375                 gnus-tmp-name))
2376       gnus-tmp-closing-bracket)
2377      (point))
2378    gnus-mouse-face-prop gnus-mouse-face)
2379   (insert " " gnus-tmp-subject-or-nil "\n"))
2380
2381 (defvar gnus-summary-line-format-spec
2382   (gnus-byte-code 'gnus-summary-line-format-spec))
2383
2384 (defun gnus-summary-dummy-line-format-spec ()
2385   (insert "*  ")
2386   (gnus-put-text-property
2387    (point)
2388    (progn
2389      (insert ":                          :")
2390      (point))
2391    gnus-mouse-face-prop gnus-mouse-face)
2392   (insert " " gnus-tmp-subject "\n"))
2393
2394 (defvar gnus-summary-dummy-line-format-spec
2395   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2396
2397 (defun gnus-group-line-format-spec ()
2398   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2399           gnus-tmp-process-marked
2400           gnus-group-indentation
2401           (format "%5s: " gnus-tmp-number-of-unread))
2402   (gnus-put-text-property
2403    (point)
2404    (progn
2405      (insert gnus-tmp-group "\n")
2406      (1- (point)))
2407    gnus-mouse-face-prop gnus-mouse-face))
2408 (defvar gnus-group-line-format-spec
2409   (gnus-byte-code 'gnus-group-line-format-spec))
2410
2411 (defvar gnus-format-specs
2412   `((version . ,emacs-version)
2413     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2414     (summary-dummy ,gnus-summary-dummy-line-format
2415                    ,gnus-summary-dummy-line-format-spec)
2416     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2417
2418 (defvar gnus-article-mode-line-format-spec nil)
2419 (defvar gnus-summary-mode-line-format-spec nil)
2420 (defvar gnus-group-mode-line-format-spec nil)
2421
2422 ;;; Phew.  All that gruft is over, fortunately.
2423
2424 \f
2425 ;;;
2426 ;;; Gnus Utility Functions
2427 ;;;
2428
2429 (defun gnus-extract-address-components (from)
2430   (let (name address)
2431     ;; First find the address - the thing with the @ in it.  This may
2432     ;; not be accurate in mail addresses, but does the trick most of
2433     ;; the time in news messages.
2434     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2435         (setq address (substring from (match-beginning 0) (match-end 0))))
2436     ;; Then we check whether the "name <address>" format is used.
2437     (and address
2438          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2439          ;; Linear white space is not required.
2440          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2441          (and (setq name (substring from 0 (match-beginning 0)))
2442               ;; Strip any quotes from the name.
2443               (string-match "\".*\"" name)
2444               (setq name (substring name 1 (1- (match-end 0))))))
2445     ;; If not, then "address (name)" is used.
2446     (or name
2447         (and (string-match "(.+)" from)
2448              (setq name (substring from (1+ (match-beginning 0))
2449                                    (1- (match-end 0)))))
2450         (and (string-match "()" from)
2451              (setq name address))
2452         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2453         ;; XOVER might not support folded From headers.
2454         (and (string-match "(.*" from)
2455              (setq name (substring from (1+ (match-beginning 0))
2456                                    (match-end 0)))))
2457     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2458     (list (or name from) (or address from))))
2459
2460 (defun gnus-fetch-field (field)
2461   "Return the value of the header FIELD of current article."
2462   (save-excursion
2463     (save-restriction
2464       (let ((case-fold-search t)
2465             (inhibit-point-motion-hooks t))
2466         (nnheader-narrow-to-headers)
2467         (message-fetch-field field)))))
2468
2469 (defun gnus-goto-colon ()
2470   (beginning-of-line)
2471   (search-forward ":" (gnus-point-at-eol) t))
2472
2473 ;;;###autoload
2474 (defun gnus-update-format (var)
2475   "Update the format specification near point."
2476   (interactive
2477    (list
2478     (save-excursion
2479       (eval-defun nil)
2480       ;; Find the end of the current word.
2481       (re-search-forward "[ \t\n]" nil t)
2482       ;; Search backward.
2483       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2484         (match-string 1)))))
2485   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2486                               (match-string 1 var))))
2487          (entry (assq type gnus-format-specs))
2488          value spec)
2489     (when entry
2490       (setq gnus-format-specs (delq entry gnus-format-specs)))
2491     (set
2492      (intern (format "%s-spec" var))
2493      (gnus-parse-format (setq value (symbol-value (intern var)))
2494                         (symbol-value (intern (format "%s-alist" var)))
2495                         (not (string-match "mode" var))))
2496     (setq spec (symbol-value (intern (format "%s-spec" var))))
2497     (push (list type value spec) gnus-format-specs)
2498
2499     (pop-to-buffer "*Gnus Format*")
2500     (erase-buffer)
2501     (lisp-interaction-mode)
2502     (insert (pp-to-string spec))))
2503
2504 (defun gnus-update-format-specifications (&optional force)
2505   "Update all (necessary) format specifications."
2506   ;; Make the indentation array.
2507   (gnus-make-thread-indent-array)
2508
2509   ;; See whether all the stored info needs to be flushed.
2510   (when (or force
2511             (not (equal emacs-version
2512                         (cdr (assq 'version gnus-format-specs)))))
2513     (setq gnus-format-specs nil))
2514
2515   ;; Go through all the formats and see whether they need updating.
2516   (let ((types '(summary summary-dummy group
2517                          summary-mode group-mode article-mode))
2518         new-format entry type val)
2519     (while (setq type (pop types))
2520       ;; Jump to the proper buffer to find out the value of
2521       ;; the variable, if possible.  (It may be buffer-local.)
2522       (save-excursion
2523         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2524               val)
2525           (when (and (boundp buffer)
2526                      (setq val (symbol-value buffer))
2527                      (get-buffer val)
2528                      (buffer-name (get-buffer val)))
2529             (set-buffer (get-buffer val)))
2530           (setq new-format (symbol-value
2531                             (intern (format "gnus-%s-line-format" type))))))
2532       (setq entry (cdr (assq type gnus-format-specs)))
2533       (if (and entry
2534                (equal (car entry) new-format))
2535           ;; Use the old format.
2536           (set (intern (format "gnus-%s-line-format-spec" type))
2537                (cadr entry))
2538         ;; This is a new format.
2539         (setq val
2540               (if (not (stringp new-format))
2541                   ;; This is a function call or something.
2542                   new-format
2543                 ;; This is a "real" format.
2544                 (gnus-parse-format
2545                  new-format
2546                  (symbol-value
2547                   (intern (format "gnus-%s-line-format-alist"
2548                                   (if (eq type 'article-mode)
2549                                       'summary-mode type))))
2550                  (not (string-match "mode$" (symbol-name type))))))
2551         ;; Enter the new format spec into the list.
2552         (if entry
2553             (progn
2554               (setcar (cdr entry) val)
2555               (setcar entry new-format))
2556           (push (list type new-format val) gnus-format-specs))
2557         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2558
2559   (unless (assq 'version gnus-format-specs)
2560     (push (cons 'version emacs-version) gnus-format-specs))
2561
2562   (gnus-update-group-mark-positions)
2563   (gnus-update-summary-mark-positions))
2564
2565 (defun gnus-update-summary-mark-positions ()
2566   "Compute where the summary marks are to go."
2567   (save-excursion
2568     (when (and gnus-summary-buffer
2569                (get-buffer gnus-summary-buffer)
2570                (buffer-name (get-buffer gnus-summary-buffer)))
2571       (set-buffer gnus-summary-buffer))
2572     (let ((gnus-replied-mark 129)
2573           (gnus-score-below-mark 130)
2574           (gnus-score-over-mark 130)
2575           (thread nil)
2576           (gnus-visual nil)
2577           (spec gnus-summary-line-format-spec)
2578           pos)
2579       (save-excursion
2580         (gnus-set-work-buffer)
2581         (let ((gnus-summary-line-format-spec spec))
2582           (gnus-summary-insert-line
2583            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2584           (goto-char (point-min))
2585           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2586                                              (- (point) 2)))))
2587           (goto-char (point-min))
2588           (push (cons 'replied (and (search-forward "\201" nil t) 
2589                                     (- (point) 2)))
2590                 pos)
2591           (goto-char (point-min))
2592           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2593                 pos)))
2594       (setq gnus-summary-mark-positions pos))))
2595
2596 (defun gnus-update-group-mark-positions ()
2597   (save-excursion
2598     (let ((gnus-process-mark 128)
2599           (gnus-group-marked '("dummy.group"))
2600           (gnus-active-hashtb (make-vector 10 0)))
2601       (gnus-set-active "dummy.group" '(0 . 0))
2602       (gnus-set-work-buffer)
2603       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2604       (goto-char (point-min))
2605       (setq gnus-group-mark-positions
2606             (list (cons 'process (and (search-forward "\200" nil t)
2607                                       (- (point) 2))))))))
2608
2609 (defvar gnus-mouse-face-0 'highlight)
2610 (defvar gnus-mouse-face-1 'highlight)
2611 (defvar gnus-mouse-face-2 'highlight)
2612 (defvar gnus-mouse-face-3 'highlight)
2613 (defvar gnus-mouse-face-4 'highlight)
2614
2615 (defun gnus-mouse-face-function (form type)
2616   `(gnus-put-text-property
2617     (point) (progn ,@form (point))
2618     gnus-mouse-face-prop
2619     ,(if (equal type 0)
2620          'gnus-mouse-face
2621        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2622
2623 (defvar gnus-face-0 'bold)
2624 (defvar gnus-face-1 'italic)
2625 (defvar gnus-face-2 'bold-italic)
2626 (defvar gnus-face-3 'bold)
2627 (defvar gnus-face-4 'bold)
2628
2629 (defun gnus-face-face-function (form type)
2630   `(gnus-put-text-property
2631     (point) (progn ,@form (point))
2632     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2633
2634 (defun gnus-max-width-function (el max-width)
2635   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2636   (if (symbolp el)
2637       `(if (> (length ,el) ,max-width)
2638            (substring ,el 0 ,max-width)
2639          ,el)
2640     `(let ((val (eval ,el)))
2641        (if (numberp val)
2642            (setq val (int-to-string val)))
2643        (if (> (length val) ,max-width)
2644            (substring val 0 ,max-width)
2645          val))))
2646
2647 (defun gnus-parse-format (format spec-alist &optional insert)
2648   ;; This function parses the FORMAT string with the help of the
2649   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2650   ;; string.  If the FORMAT string contains the specifiers %( and %)
2651   ;; the text between them will have the mouse-face text property.
2652   (if (string-match
2653        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2654        format)
2655       (gnus-parse-complex-format format spec-alist)
2656     ;; This is a simple format.
2657     (gnus-parse-simple-format format spec-alist insert)))
2658
2659 (defun gnus-parse-complex-format (format spec-alist)
2660   (save-excursion
2661     (gnus-set-work-buffer)
2662     (insert format)
2663     (goto-char (point-min))
2664     (while (re-search-forward "\"" nil t)
2665       (replace-match "\\\"" nil t))
2666     (goto-char (point-min))
2667     (insert "(\"")
2668     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2669       (let ((number (if (match-beginning 1)
2670                         (match-string 1) "0"))
2671             (delim (aref (match-string 2) 0)))
2672         (if (or (= delim ?\() (= delim ?\{))
2673             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2674                                    " " number " \""))
2675           (replace-match "\")\""))))
2676     (goto-char (point-max))
2677     (insert "\")")
2678     (goto-char (point-min))
2679     (let ((form (read (current-buffer))))
2680       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2681
2682 (defun gnus-complex-form-to-spec (form spec-alist)
2683   (delq nil
2684         (mapcar
2685          (lambda (sform)
2686            (if (stringp sform)
2687                (gnus-parse-simple-format sform spec-alist t)
2688              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2689                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2690                       (nth 1 sform))))
2691          form)))
2692
2693 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2694   ;; This function parses the FORMAT string with the help of the
2695   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2696   ;; string.
2697   (let ((max-width 0)
2698         spec flist fstring newspec elem beg result dontinsert)
2699     (save-excursion
2700       (gnus-set-work-buffer)
2701       (insert format)
2702       (goto-char (point-min))
2703       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2704                                 nil t)
2705         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2706               (setq newspec "%"
2707                     beg (1+ (match-beginning 0)))
2708           ;; First check if there are any specs that look anything like
2709           ;; "%12,12A", ie. with a "max width specification".  These have
2710           ;; to be treated specially.
2711           (if (setq beg (match-beginning 1))
2712               (setq max-width
2713                     (string-to-int
2714                      (buffer-substring
2715                       (1+ (match-beginning 1)) (match-end 1))))
2716             (setq max-width 0)
2717             (setq beg (match-beginning 2)))
2718           ;; Find the specification from `spec-alist'.
2719           (unless (setq elem (cdr (assq spec spec-alist)))
2720             (setq elem '("*" ?s)))
2721           ;; Treat user defined format specifiers specially.
2722           (when (eq (car elem) 'gnus-tmp-user-defined)
2723             (setq elem
2724                   (list
2725                    (list (intern (concat "gnus-user-format-function-"
2726                                          (match-string 3)))
2727                          'gnus-tmp-header) ?s))
2728             (delete-region (match-beginning 3) (match-end 3)))
2729           (if (not (zerop max-width))
2730               (let ((el (car elem)))
2731                 (cond ((= (cadr elem) ?c)
2732                        (setq el (list 'char-to-string el)))
2733                       ((= (cadr elem) ?d)
2734                        (setq el (list 'int-to-string el))))
2735                 (setq flist (cons (gnus-max-width-function el max-width)
2736                                   flist))
2737                 (setq newspec ?s))
2738             (progn
2739               (setq flist (cons (car elem) flist))
2740               (setq newspec (cadr elem)))))
2741         ;; Remove the old specification (and possibly a ",12" string).
2742         (delete-region beg (match-end 2))
2743         ;; Insert the new specification.
2744         (goto-char beg)
2745         (insert newspec))
2746       (setq fstring (buffer-substring 1 (point-max))))
2747     ;; Do some postprocessing to increase efficiency.
2748     (setq
2749      result
2750      (cond
2751       ;; Emptyness.
2752       ((string= fstring "")
2753        nil)
2754       ;; Not a format string.
2755       ((not (string-match "%" fstring))
2756        (list fstring))
2757       ;; A format string with just a single string spec.
2758       ((string= fstring "%s")
2759        (list (car flist)))
2760       ;; A single character.
2761       ((string= fstring "%c")
2762        (list (car flist)))
2763       ;; A single number.
2764       ((string= fstring "%d")
2765        (setq dontinsert)
2766        (if insert
2767            (list `(princ ,(car flist)))
2768          (list `(int-to-string ,(car flist)))))
2769       ;; Just lots of chars and strings.
2770       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2771        (nreverse flist))
2772       ;; A single string spec at the beginning of the spec.
2773       ((string-match "\\`%[sc][^%]+\\'" fstring)
2774        (list (car flist) (substring fstring 2)))
2775       ;; A single string spec in the middle of the spec.
2776       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2777        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2778       ;; A single string spec in the end of the spec.
2779       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2780        (list (match-string 1 fstring) (car flist)))
2781       ;; A more complex spec.
2782       (t
2783        (list (cons 'format (cons fstring (nreverse flist)))))))
2784
2785     (if insert
2786         (when result
2787           (if dontinsert
2788               result
2789             (cons 'insert result)))
2790       (cond ((stringp result)
2791              result)
2792             ((consp result)
2793              (cons 'concat result))
2794             (t "")))))
2795
2796 (defun gnus-eval-format (format &optional alist props)
2797   "Eval the format variable FORMAT, using ALIST.
2798 If PROPS, insert the result."
2799   (let ((form (gnus-parse-format format alist props)))
2800     (if props
2801         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2802       (eval form))))
2803
2804 (defun gnus-remove-text-with-property (prop)
2805   "Delete all text in the current buffer with text property PROP."
2806   (save-excursion
2807     (goto-char (point-min))
2808     (while (not (eobp))
2809       (while (get-text-property (point) prop)
2810         (delete-char 1))
2811       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2812
2813 (defun gnus-set-work-buffer ()
2814   (if (get-buffer gnus-work-buffer)
2815       (progn
2816         (set-buffer gnus-work-buffer)
2817         (erase-buffer))
2818     (set-buffer (get-buffer-create gnus-work-buffer))
2819     (kill-all-local-variables)
2820     (buffer-disable-undo (current-buffer))
2821     (gnus-add-current-to-buffer-list)))
2822
2823 ;; Article file names when saving.
2824
2825 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2826   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2827 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2828 Otherwise, it is like ~/News/news/group/num."
2829   (let ((default
2830           (expand-file-name
2831            (concat (if (gnus-use-long-file-name 'not-save)
2832                        (gnus-capitalize-newsgroup newsgroup)
2833                      (gnus-newsgroup-directory-form newsgroup))
2834                    "/" (int-to-string (mail-header-number headers)))
2835            gnus-article-save-directory)))
2836     (if (and last-file
2837              (string-equal (file-name-directory default)
2838                            (file-name-directory last-file))
2839              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2840         default
2841       (or last-file default))))
2842
2843 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2844   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2845 If variable `gnus-use-long-file-name' is non-nil, it is
2846 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2847   (let ((default
2848           (expand-file-name
2849            (concat (if (gnus-use-long-file-name 'not-save)
2850                        newsgroup
2851                      (gnus-newsgroup-directory-form newsgroup))
2852                    "/" (int-to-string (mail-header-number headers)))
2853            gnus-article-save-directory)))
2854     (if (and last-file
2855              (string-equal (file-name-directory default)
2856                            (file-name-directory last-file))
2857              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2858         default
2859       (or last-file default))))
2860
2861 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2862   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2863 If variable `gnus-use-long-file-name' is non-nil, it is
2864 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2865   (or last-file
2866       (expand-file-name
2867        (if (gnus-use-long-file-name 'not-save)
2868            (gnus-capitalize-newsgroup newsgroup)
2869          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2870        gnus-article-save-directory)))
2871
2872 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2873   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2874 If variable `gnus-use-long-file-name' is non-nil, it is
2875 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2876   (or last-file
2877       (expand-file-name
2878        (if (gnus-use-long-file-name 'not-save)
2879            newsgroup
2880          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2881        gnus-article-save-directory)))
2882
2883 ;; For subscribing new newsgroup
2884
2885 (defun gnus-subscribe-hierarchical-interactive (groups)
2886   (let ((groups (sort groups 'string<))
2887         prefixes prefix start ans group starts)
2888     (while groups
2889       (setq prefixes (list "^"))
2890       (while (and groups prefixes)
2891         (while (not (string-match (car prefixes) (car groups)))
2892           (setq prefixes (cdr prefixes)))
2893         (setq prefix (car prefixes))
2894         (setq start (1- (length prefix)))
2895         (if (and (string-match "[^\\.]\\." (car groups) start)
2896                  (cdr groups)
2897                  (setq prefix
2898                        (concat "^" (substring (car groups) 0 (match-end 0))))
2899                  (string-match prefix (cadr groups)))
2900             (progn
2901               (setq prefixes (cons prefix prefixes))
2902               (message "Descend hierarchy %s? ([y]nsq): "
2903                        (substring prefix 1 (1- (length prefix))))
2904               (setq ans (read-char))
2905               (cond ((= ans ?n)
2906                      (while (and groups
2907                                  (string-match prefix
2908                                                (setq group (car groups))))
2909                        (setq gnus-killed-list
2910                              (cons group gnus-killed-list))
2911                        (gnus-sethash group group gnus-killed-hashtb)
2912                        (setq groups (cdr groups)))
2913                      (setq starts (cdr starts)))
2914                     ((= ans ?s)
2915                      (while (and groups
2916                                  (string-match prefix
2917                                                (setq group (car groups))))
2918                        (gnus-sethash group group gnus-killed-hashtb)
2919                        (gnus-subscribe-alphabetically (car groups))
2920                        (setq groups (cdr groups)))
2921                      (setq starts (cdr starts)))
2922                     ((= ans ?q)
2923                      (while groups
2924                        (setq group (car groups))
2925                        (setq gnus-killed-list (cons group gnus-killed-list))
2926                        (gnus-sethash group group gnus-killed-hashtb)
2927                        (setq groups (cdr groups))))
2928                     (t nil)))
2929           (message "Subscribe %s? ([n]yq)" (car groups))
2930           (setq ans (read-char))
2931           (setq group (car groups))
2932           (cond ((= ans ?y)
2933                  (gnus-subscribe-alphabetically (car groups))
2934                  (gnus-sethash group group gnus-killed-hashtb))
2935                 ((= ans ?q)
2936                  (while groups
2937                    (setq group (car groups))
2938                    (setq gnus-killed-list (cons group gnus-killed-list))
2939                    (gnus-sethash group group gnus-killed-hashtb)
2940                    (setq groups (cdr groups))))
2941                 (t
2942                  (setq gnus-killed-list (cons group gnus-killed-list))
2943                  (gnus-sethash group group gnus-killed-hashtb)))
2944           (setq groups (cdr groups)))))))
2945
2946 (defun gnus-subscribe-randomly (newsgroup)
2947   "Subscribe new NEWSGROUP by making it the first newsgroup."
2948   (gnus-subscribe-newsgroup newsgroup))
2949
2950 (defun gnus-subscribe-alphabetically (newgroup)
2951   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2952   (let ((groups (cdr gnus-newsrc-alist))
2953         before)
2954     (while (and (not before) groups)
2955       (if (string< newgroup (caar groups))
2956           (setq before (caar groups))
2957         (setq groups (cdr groups))))
2958     (gnus-subscribe-newsgroup newgroup before)))
2959
2960 (defun gnus-subscribe-hierarchically (newgroup)
2961   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2962   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2963   (save-excursion
2964     (set-buffer (find-file-noselect gnus-current-startup-file))
2965     (let ((groupkey newgroup)
2966           before)
2967       (while (and (not before) groupkey)
2968         (goto-char (point-min))
2969         (let ((groupkey-re
2970                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2971           (while (and (re-search-forward groupkey-re nil t)
2972                       (progn
2973                         (setq before (match-string 1))
2974                         (string< before newgroup)))))
2975         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2976         (setq groupkey
2977               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2978                   (substring groupkey (match-beginning 1) (match-end 1)))))
2979       (gnus-subscribe-newsgroup newgroup before))))
2980
2981 (defun gnus-subscribe-interactively (group)
2982   "Subscribe the new GROUP interactively.
2983 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2984 it is killed."
2985   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2986       (gnus-subscribe-hierarchically group)
2987     (push group gnus-killed-list)))
2988
2989 (defun gnus-subscribe-zombies (group)
2990   "Make the new GROUP into a zombie group."
2991   (push group gnus-zombie-list))
2992
2993 (defun gnus-subscribe-killed (group)
2994   "Make the new GROUP a killed group."
2995   (push group gnus-killed-list))
2996
2997 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2998   "Subscribe new NEWSGROUP.
2999 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3000 the first newsgroup."
3001   ;; We subscribe the group by changing its level to `subscribed'.
3002   (gnus-group-change-level
3003    newsgroup gnus-level-default-subscribed
3004    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3005   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3006
3007 ;; For directories
3008
3009 (defun gnus-newsgroup-directory-form (newsgroup)
3010   "Make hierarchical directory name from NEWSGROUP name."
3011   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3012         (len (length newsgroup))
3013         idx)
3014     ;; If this is a foreign group, we don't want to translate the
3015     ;; entire name.
3016     (if (setq idx (string-match ":" newsgroup))
3017         (aset newsgroup idx ?/)
3018       (setq idx 0))
3019     ;; Replace all occurrences of `.' with `/'.
3020     (while (< idx len)
3021       (if (= (aref newsgroup idx) ?.)
3022           (aset newsgroup idx ?/))
3023       (setq idx (1+ idx)))
3024     newsgroup))
3025
3026 (defun gnus-newsgroup-savable-name (group)
3027   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3028   ;; with dots.
3029   (nnheader-replace-chars-in-string group ?/ ?.))
3030
3031 (defun gnus-make-directory (dir)
3032   "Make DIRECTORY recursively."
3033   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3034   ;; of the many mysteries of the universe.
3035   (let* ((dir (expand-file-name dir default-directory))
3036          dirs err)
3037     (if (string-match "/$" dir)
3038         (setq dir (substring dir 0 (match-beginning 0))))
3039     ;; First go down the path until we find a directory that exists.
3040     (while (not (file-exists-p dir))
3041       (setq dirs (cons dir dirs))
3042       (string-match "/[^/]+$" dir)
3043       (setq dir (substring dir 0 (match-beginning 0))))
3044     ;; Then create all the subdirs.
3045     (while (and dirs (not err))
3046       (condition-case ()
3047           (make-directory (car dirs))
3048         (error (setq err t)))
3049       (setq dirs (cdr dirs)))
3050     ;; We return whether we were successful or not.
3051     (not dirs)))
3052
3053 (defun gnus-capitalize-newsgroup (newsgroup)
3054   "Capitalize NEWSGROUP name."
3055   (and (not (zerop (length newsgroup)))
3056        (concat (char-to-string (upcase (aref newsgroup 0)))
3057                (substring newsgroup 1))))
3058
3059 ;; Various... things.
3060
3061 (defun gnus-simplify-subject (subject &optional re-only)
3062   "Remove `Re:' and words in parentheses.
3063 If RE-ONLY is non-nil, strip leading `Re:'s only."
3064   (let ((case-fold-search t))           ;Ignore case.
3065     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3066     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3067       (setq subject (substring subject (match-end 0))))
3068     ;; Remove uninteresting prefixes.
3069     (if (and (not re-only)
3070              gnus-simplify-ignored-prefixes
3071              (string-match gnus-simplify-ignored-prefixes subject))
3072         (setq subject (substring subject (match-end 0))))
3073     ;; Remove words in parentheses from end.
3074     (unless re-only
3075       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3076         (setq subject (substring subject 0 (match-beginning 0)))))
3077     ;; Return subject string.
3078     subject))
3079
3080 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3081 ;; all whitespace.
3082 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3083 (defun gnus-simplify-buffer-fuzzy ()
3084   (goto-char (point-min))
3085   (while (search-forward "\t" nil t)
3086     (replace-match " " t t))
3087   (goto-char (point-min))
3088   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3089   (goto-char (match-beginning 0))
3090   (while (or
3091           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3092           (looking-at "^[[].*: .*[]]$"))
3093     (goto-char (point-min))
3094     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3095                               nil t)
3096       (replace-match "" t t))
3097     (goto-char (point-min))
3098     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3099       (goto-char (match-end 0))
3100       (delete-char -1)
3101       (delete-region
3102        (progn (goto-char (match-beginning 0)))
3103        (re-search-forward ":"))))
3104   (goto-char (point-min))
3105   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" 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   (while (re-search-forward "^ +" nil t)
3115     (replace-match "" t t))
3116   (goto-char (point-min))
3117   (when gnus-simplify-subject-fuzzy-regexp
3118     (if (listp gnus-simplify-subject-fuzzy-regexp)
3119         (let ((list gnus-simplify-subject-fuzzy-regexp))
3120           (while list
3121             (goto-char (point-min))
3122             (while (re-search-forward (car list) nil t)
3123               (replace-match "" t t))
3124             (setq list (cdr list))))
3125       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3126         (replace-match "" t t)))))
3127
3128 (defun gnus-simplify-subject-fuzzy (subject)
3129   "Siplify a subject string fuzzily."
3130   (save-excursion
3131     (gnus-set-work-buffer)
3132     (let ((case-fold-search t))
3133       (insert subject)
3134       (inline (gnus-simplify-buffer-fuzzy))
3135       (buffer-string))))
3136
3137 ;; Add the current buffer to the list of buffers to be killed on exit.
3138 (defun gnus-add-current-to-buffer-list ()
3139   (or (memq (current-buffer) gnus-buffer-list)
3140       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3141
3142 (defun gnus-string> (s1 s2)
3143   (not (or (string< s1 s2)
3144            (string= s1 s2))))
3145
3146 (defun gnus-read-active-file-p ()
3147   "Say whether the active file has been read from `gnus-select-method'."
3148   (memq gnus-select-method gnus-have-read-active-file))
3149
3150 ;;; General various misc type functions.
3151
3152 (defun gnus-clear-system ()
3153   "Clear all variables and buffers."
3154   ;; Clear Gnus variables.
3155   (let ((variables gnus-variable-list))
3156     (while variables
3157       (set (car variables) nil)
3158       (setq variables (cdr variables))))
3159   ;; Clear other internal variables.
3160   (setq gnus-list-of-killed-groups nil
3161         gnus-have-read-active-file nil
3162         gnus-newsrc-alist nil
3163         gnus-newsrc-hashtb nil
3164         gnus-killed-list nil
3165         gnus-zombie-list nil
3166         gnus-killed-hashtb nil
3167         gnus-active-hashtb nil
3168         gnus-moderated-list nil
3169         gnus-description-hashtb nil
3170         gnus-current-headers nil
3171         gnus-thread-indent-array nil
3172         gnus-newsgroup-headers nil
3173         gnus-newsgroup-name nil
3174         gnus-server-alist nil
3175         gnus-group-list-mode nil
3176         gnus-opened-servers nil
3177         gnus-current-select-method nil)
3178   (gnus-shutdown 'gnus)
3179   ;; Kill the startup file.
3180   (and gnus-current-startup-file
3181        (get-file-buffer gnus-current-startup-file)
3182        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3183   ;; Clear the dribble buffer.
3184   (gnus-dribble-clear)
3185   ;; Kill global KILL file buffer.
3186   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3187     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3188   (gnus-kill-buffer nntp-server-buffer)
3189   ;; Kill Gnus buffers.
3190   (while gnus-buffer-list
3191     (gnus-kill-buffer (pop gnus-buffer-list)))
3192   ;; Remove Gnus frames.
3193   (while gnus-created-frames
3194     (when (frame-live-p (car gnus-created-frames))
3195       ;; We slap a condition-case around this `delete-frame' to ensure 
3196       ;; agains errors if we try do delete the single frame that's left.
3197       (condition-case ()
3198           (delete-frame (car gnus-created-frames))
3199         (error nil)))
3200     (pop gnus-created-frames)))
3201
3202 (defun gnus-windows-old-to-new (setting)
3203   ;; First we take care of the really, really old Gnus 3 actions.
3204   (when (symbolp setting)
3205     (setq setting
3206           ;; Take care of ooold GNUS 3.x values.
3207           (cond ((eq setting 'SelectArticle) 'article)
3208                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3209                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3210                 (t setting))))
3211   (if (or (listp setting)
3212           (not (and gnus-window-configuration
3213                     (memq setting '(group summary article)))))
3214       setting
3215     (let* ((setting (if (eq setting 'group)
3216                         (if (assq 'newsgroup gnus-window-configuration)
3217                             'newsgroup
3218                           'newsgroups) setting))
3219            (elem (cadr (assq setting gnus-window-configuration)))
3220            (total (apply '+ elem))
3221            (types '(group summary article))
3222            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3223            (i 0)
3224            perc
3225            out)
3226       (while (< i 3)
3227         (or (not (numberp (nth i elem)))
3228             (zerop (nth i elem))
3229             (progn
3230               (setq perc  (/ (float (nth 0 elem)) total))
3231               (setq out (cons (if (eq pbuf (nth i types))
3232                                   (vector (nth i types) perc 'point)
3233                                 (vector (nth i types) perc))
3234                               out))))
3235         (setq i (1+ i)))
3236       (list (nreverse out)))))
3237
3238 ;;;###autoload
3239 (defun gnus-add-configuration (conf)
3240   "Add the window configuration CONF to `gnus-buffer-configuration'."
3241   (setq gnus-buffer-configuration
3242         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3243                          gnus-buffer-configuration))))
3244
3245 (defvar gnus-frame-list nil)
3246
3247 (defun gnus-configure-frame (split &optional window)
3248   "Split WINDOW according to SPLIT."
3249   (unless window
3250     (setq window (get-buffer-window (current-buffer))))
3251   (select-window window)
3252   ;; This might be an old-stylee buffer config.
3253   (when (vectorp split)
3254     (setq split (append split nil)))
3255   (when (or (consp (car split))
3256             (vectorp (car split)))
3257     (push 1.0 split)
3258     (push 'vertical split))
3259   ;; The SPLIT might be something that is to be evaled to
3260   ;; return a new SPLIT.
3261   (while (and (not (assq (car split) gnus-window-to-buffer))
3262               (gnus-functionp (car split)))
3263     (setq split (eval split)))
3264   (let* ((type (car split))
3265          (subs (cddr split))
3266          (len (if (eq type 'horizontal) (window-width) (window-height)))
3267          (total 0)
3268          (window-min-width (or gnus-window-min-width window-min-width))
3269          (window-min-height (or gnus-window-min-height window-min-height))
3270          s result new-win rest comp-subs size sub)
3271     (cond
3272      ;; Nothing to do here.
3273      ((null split))
3274      ;; Don't switch buffers.
3275      ((null type)
3276       (and (memq 'point split) window))
3277      ;; This is a buffer to be selected.
3278      ((not (memq type '(frame horizontal vertical)))
3279       (let ((buffer (cond ((stringp type) type)
3280                           (t (cdr (assq type gnus-window-to-buffer)))))
3281             buf)
3282         (unless buffer
3283           (error "Illegal buffer type: %s" type))
3284         (unless (setq buf (get-buffer (if (symbolp buffer)
3285                                           (symbol-value buffer) buffer)))
3286           (setq buf (get-buffer-create (if (symbolp buffer)
3287                                            (symbol-value buffer) buffer))))
3288         (switch-to-buffer buf)
3289         ;; We return the window if it has the `point' spec.
3290         (and (memq 'point split) window)))
3291      ;; This is a frame split.
3292      ((eq type 'frame)
3293       (unless gnus-frame-list
3294         (setq gnus-frame-list (list (window-frame
3295                                      (get-buffer-window (current-buffer))))))
3296       (let ((i 0)
3297             params frame fresult)
3298         (while (< i (length subs))
3299           ;; Frame parameter is gotten from the sub-split.
3300           (setq params (cadr (elt subs i)))
3301           ;; It should be a list.
3302           (unless (listp params)
3303             (setq params nil))
3304           ;; Create a new frame?
3305           (unless (setq frame (elt gnus-frame-list i))
3306             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3307             (push frame gnus-created-frames))
3308           ;; Is the old frame still alive?
3309           (unless (frame-live-p frame)
3310             (setcar (nthcdr i gnus-frame-list)
3311                     (setq frame (make-frame params))))
3312           ;; Select the frame in question and do more splits there.
3313           (select-frame frame)
3314           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3315           (incf i))
3316         ;; Select the frame that has the selected buffer.
3317         (when fresult
3318           (select-frame (window-frame fresult)))))
3319      ;; This is a normal split.
3320      (t
3321       (when (> (length subs) 0)
3322         ;; First we have to compute the sizes of all new windows.
3323         (while subs
3324           (setq sub (append (pop subs) nil))
3325           (while (and (not (assq (car sub) gnus-window-to-buffer))
3326                       (gnus-functionp (car sub)))
3327             (setq sub (eval sub)))
3328           (when sub
3329             (push sub comp-subs)
3330             (setq size (cadar comp-subs))
3331             (cond ((equal size 1.0)
3332                    (setq rest (car comp-subs))
3333                    (setq s 0))
3334                   ((floatp size)
3335                    (setq s (floor (* size len))))
3336                   ((integerp size)
3337                    (setq s size))
3338                   (t
3339                    (error "Illegal size: %s" size)))
3340             ;; Try to make sure that we are inside the safe limits.
3341             (cond ((zerop s))
3342                   ((eq type 'horizontal)
3343                    (setq s (max s window-min-width)))
3344                   ((eq type 'vertical)
3345                    (setq s (max s window-min-height))))
3346             (setcar (cdar comp-subs) s)
3347             (incf total s)))
3348         ;; Take care of the "1.0" spec.
3349         (if rest
3350             (setcar (cdr rest) (- len total))
3351           (error "No 1.0 specs in %s" split))
3352         ;; The we do the actual splitting in a nice recursive
3353         ;; fashion.
3354         (setq comp-subs (nreverse comp-subs))
3355         (while comp-subs
3356           (if (null (cdr comp-subs))
3357               (setq new-win window)
3358             (setq new-win
3359                   (split-window window (cadar comp-subs)
3360                                 (eq type 'horizontal))))
3361           (setq result (or (gnus-configure-frame
3362                             (car comp-subs) window) result))
3363           (select-window new-win)
3364           (setq window new-win)
3365           (setq comp-subs (cdr comp-subs))))
3366       ;; Return the proper window, if any.
3367       (when result
3368         (select-window result))))))
3369
3370 (defvar gnus-frame-split-p nil)
3371
3372 (defun gnus-configure-windows (setting &optional force)
3373   (setq setting (gnus-windows-old-to-new setting))
3374   (let ((split (if (symbolp setting)
3375                    (cadr (assq setting gnus-buffer-configuration))
3376                  setting))
3377         all-visible)
3378
3379     (setq gnus-frame-split-p nil)
3380
3381     (unless split
3382       (error "No such setting: %s" setting))
3383
3384     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3385              (not force))
3386         ;; All the windows mentioned are already visible, so we just
3387         ;; put point in the assigned buffer, and do not touch the
3388         ;; winconf.
3389         (select-window all-visible)
3390
3391       ;; Either remove all windows or just remove all Gnus windows.
3392       (let ((frame (selected-frame)))
3393         (unwind-protect
3394             (if gnus-use-full-window
3395                 ;; We want to remove all other windows.
3396                 (if (not gnus-frame-split-p)
3397                     ;; This is not a `frame' split, so we ignore the
3398                     ;; other frames.  
3399                     (delete-other-windows)
3400                   ;; This is a `frame' split, so we delete all windows
3401                   ;; on all frames.
3402                   (mapcar 
3403                    (lambda (frame)
3404                      (unless (eq (cdr (assq 'minibuffer
3405                                             (frame-parameters frame)))
3406                                  'only)
3407                        (select-frame frame)
3408                        (delete-other-windows)))
3409                    (frame-list)))
3410               ;; Just remove some windows.
3411               (gnus-remove-some-windows)
3412               (switch-to-buffer nntp-server-buffer))
3413           (select-frame frame)))
3414
3415       (switch-to-buffer nntp-server-buffer)
3416       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3417
3418 (defun gnus-all-windows-visible-p (split)
3419   "Say whether all buffers in SPLIT are currently visible.
3420 In particular, the value returned will be the window that
3421 should have point."
3422   (let ((stack (list split))
3423         (all-visible t)
3424         type buffer win buf)
3425     (while (and (setq split (pop stack))
3426                 all-visible)
3427       ;; Be backwards compatible.
3428       (when (vectorp split)
3429         (setq split (append split nil)))
3430       (when (or (consp (car split))
3431                 (vectorp (car split)))
3432         (push 1.0 split)
3433         (push 'vertical split))
3434       ;; The SPLIT might be something that is to be evaled to
3435       ;; return a new SPLIT.
3436       (while (and (not (assq (car split) gnus-window-to-buffer))
3437                   (gnus-functionp (car split)))
3438         (setq split (eval split)))
3439
3440       (setq type (elt split 0))
3441       (cond
3442        ;; Nothing here.
3443        ((null split) t)
3444        ;; A buffer.
3445        ((not (memq type '(horizontal vertical frame)))
3446         (setq buffer (cond ((stringp type) type)
3447                            (t (cdr (assq type gnus-window-to-buffer)))))
3448         (unless buffer
3449           (error "Illegal buffer type: %s" type))
3450         (when (setq buf (get-buffer (if (symbolp buffer)
3451                                         (symbol-value buffer)
3452                                       buffer)))
3453           (setq win (get-buffer-window buf t)))
3454         (if win
3455             (when (memq 'point split)
3456                 (setq all-visible win))
3457           (setq all-visible nil)))
3458        (t
3459         (when (eq type 'frame)
3460           (setq gnus-frame-split-p t))
3461         (setq stack (append (cddr split) stack)))))
3462     (unless (eq all-visible t)
3463       all-visible)))
3464
3465 (defun gnus-window-top-edge (&optional window)
3466   (nth 1 (window-edges window)))
3467
3468 (defun gnus-remove-some-windows ()
3469   (let ((buffers gnus-window-to-buffer)
3470         buf bufs lowest-buf lowest)
3471     (save-excursion
3472       ;; Remove windows on all known Gnus buffers.
3473       (while buffers
3474         (setq buf (cdar buffers))
3475         (if (symbolp buf)
3476             (setq buf (and (boundp buf) (symbol-value buf))))
3477         (and buf
3478              (get-buffer-window buf)
3479              (progn
3480                (setq bufs (cons buf bufs))
3481                (pop-to-buffer buf)
3482                (if (or (not lowest)
3483                        (< (gnus-window-top-edge) lowest))
3484                    (progn
3485                      (setq lowest (gnus-window-top-edge))
3486                      (setq lowest-buf buf)))))
3487         (setq buffers (cdr buffers)))
3488       ;; Remove windows on *all* summary buffers.
3489       (walk-windows
3490        (lambda (win)
3491          (let ((buf (window-buffer win)))
3492            (if (string-match    "^\\*Summary" (buffer-name buf))
3493                (progn
3494                  (setq bufs (cons buf bufs))
3495                  (pop-to-buffer buf)
3496                  (if (or (not lowest)
3497                          (< (gnus-window-top-edge) lowest))
3498                      (progn
3499                        (setq lowest-buf buf)
3500                        (setq lowest (gnus-window-top-edge)))))))))
3501       (and lowest-buf
3502            (progn
3503              (pop-to-buffer lowest-buf)
3504              (switch-to-buffer nntp-server-buffer)))
3505       (while bufs
3506         (and (not (eq (car bufs) lowest-buf))
3507              (delete-windows-on (car bufs)))
3508         (setq bufs (cdr bufs))))))
3509
3510 (defun gnus-version ()
3511   "Version numbers of this version of Gnus."
3512   (interactive)
3513   (let ((methods gnus-valid-select-methods)
3514         (mess gnus-version)
3515         meth)
3516     ;; Go through all the legal select methods and add their version
3517     ;; numbers to the total version string.  Only the backends that are
3518     ;; currently in use will have their message numbers taken into
3519     ;; consideration.
3520     (while methods
3521       (setq meth (intern (concat (caar methods) "-version")))
3522       (and (boundp meth)
3523            (stringp (symbol-value meth))
3524            (setq mess (concat mess "; " (symbol-value meth))))
3525       (setq methods (cdr methods)))
3526     (gnus-message 2 mess)))
3527
3528 (defun gnus-info-find-node ()
3529   "Find Info documentation of Gnus."
3530   (interactive)
3531   ;; Enlarge info window if needed.
3532   (let ((mode major-mode)
3533         gnus-info-buffer)
3534     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3535     (setq gnus-info-buffer (current-buffer))
3536     (gnus-configure-windows 'info)))
3537
3538 (defun gnus-days-between (date1 date2)
3539   ;; Return the number of days between date1 and date2.
3540   (- (gnus-day-number date1) (gnus-day-number date2)))
3541
3542 (defun gnus-day-number (date)
3543   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3544                      (timezone-parse-date date))))
3545     (timezone-absolute-from-gregorian
3546      (nth 1 dat) (nth 2 dat) (car dat))))
3547
3548 (defun gnus-encode-date (date)
3549   "Convert DATE to internal time."
3550   (let* ((parse (timezone-parse-date date))
3551          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3552          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3553     (encode-time (caddr time) (cadr time) (car time)
3554                  (caddr date) (cadr date) (car date) (nth 4 date))))
3555
3556 (defun gnus-time-minus (t1 t2)
3557   "Subtract two internal times."
3558   (let ((borrow (< (cadr t1) (cadr t2))))
3559     (list (- (car t1) (car t2) (if borrow 1 0))
3560           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3561
3562 (defun gnus-file-newer-than (file date)
3563   (let ((fdate (nth 5 (file-attributes file))))
3564     (or (> (car fdate) (car date))
3565         (and (= (car fdate) (car date))
3566              (> (nth 1 fdate) (nth 1 date))))))
3567
3568 (defmacro gnus-local-set-keys (&rest plist)
3569   "Set the keys in PLIST in the current keymap."
3570   `(gnus-define-keys-1 (current-local-map) ',plist))
3571
3572 (defmacro gnus-define-keys (keymap &rest plist)
3573   "Define all keys in PLIST in KEYMAP."
3574   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3575
3576 (put 'gnus-define-keys 'lisp-indent-function 1)
3577 (put 'gnus-define-keys 'lisp-indent-hook 1)
3578 (put 'gnus-define-keymap 'lisp-indent-function 1)
3579 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3580
3581 (defmacro gnus-define-keymap (keymap &rest plist)
3582   "Define all keys in PLIST in KEYMAP."
3583   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3584
3585 (defun gnus-define-keys-1 (keymap plist)
3586   (when (null keymap)
3587     (error "Can't set keys in a null keymap"))
3588   (cond ((symbolp keymap)
3589          (setq keymap (symbol-value keymap)))
3590         ((keymapp keymap))
3591         ((listp keymap)
3592          (set (car keymap) nil)
3593          (define-prefix-command (car keymap))
3594          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3595          (setq keymap (symbol-value (car keymap)))))
3596   (let (key)
3597     (while plist
3598       (when (symbolp (setq key (pop plist)))
3599         (setq key (symbol-value key)))
3600       (define-key keymap key (pop plist)))))
3601
3602 (defun gnus-group-read-only-p (&optional group)
3603   "Check whether GROUP supports editing or not.
3604 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3605 that that variable is buffer-local to the summary buffers."
3606   (let ((group (or group gnus-newsgroup-name)))
3607     (not (gnus-check-backend-function 'request-replace-article group))))
3608
3609 (defun gnus-group-total-expirable-p (group)
3610   "Check whether GROUP is total-expirable or not."
3611   (let ((params (gnus-info-params (gnus-get-info group))))
3612     (or (memq 'total-expire params)
3613         (cdr (assq 'total-expire params)) ; (total-expire . t)
3614         (and gnus-total-expirable-newsgroups ; Check var.
3615              (string-match gnus-total-expirable-newsgroups group)))))
3616
3617 (defun gnus-group-auto-expirable-p (group)
3618   "Check whether GROUP is total-expirable or not."
3619   (let ((params (gnus-info-params (gnus-get-info group))))
3620     (or (memq 'auto-expire params)
3621         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3622         (and gnus-auto-expirable-newsgroups ; Check var.
3623              (string-match gnus-auto-expirable-newsgroups group)))))
3624
3625 (defun gnus-virtual-group-p (group)
3626   "Say whether GROUP is virtual or not."
3627   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3628                         gnus-valid-select-methods)))
3629
3630 (defun gnus-news-group-p (group &optional article)
3631   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3632   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3633       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3634            (eq (gnus-request-type group article) 'news))))
3635
3636 (defsubst gnus-simplify-subject-fully (subject)
3637   "Simplify a subject string according to the user's wishes."
3638   (cond
3639    ((null gnus-summary-gather-subject-limit)
3640     (gnus-simplify-subject-re subject))
3641    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3642     (gnus-simplify-subject-fuzzy subject))
3643    ((numberp gnus-summary-gather-subject-limit)
3644     (gnus-limit-string (gnus-simplify-subject-re subject)
3645                        gnus-summary-gather-subject-limit))
3646    (t
3647     subject)))
3648
3649 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3650   "Check whether two subjects are equal.  If optional argument
3651 simple-first is t, first argument is already simplified."
3652   (cond
3653    ((null simple-first)
3654     (equal (gnus-simplify-subject-fully s1)
3655            (gnus-simplify-subject-fully s2)))
3656    (t
3657     (equal s1
3658            (gnus-simplify-subject-fully s2)))))
3659
3660 ;; Returns a list of writable groups.
3661 (defun gnus-writable-groups ()
3662   (let ((alist gnus-newsrc-alist)
3663         groups group)
3664     (while (setq group (car (pop alist)))
3665       (unless (gnus-group-read-only-p group)
3666         (push group groups)))
3667     (nreverse groups)))
3668
3669 (defun gnus-completing-read (default prompt &rest args)
3670   ;; Like `completing-read', except that DEFAULT is the default argument.
3671   (let* ((prompt (if default 
3672                      (concat prompt " (default " default ") ")
3673                    (concat prompt " ")))
3674          (answer (apply 'completing-read prompt args)))
3675     (if (or (null answer) (zerop (length answer)))
3676         default
3677       answer)))
3678
3679 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3680 ;; the echo area.
3681 (defun gnus-y-or-n-p (prompt)
3682   (prog1
3683       (y-or-n-p prompt)
3684     (message "")))
3685
3686 (defun gnus-yes-or-no-p (prompt)
3687   (prog1
3688       (yes-or-no-p prompt)
3689     (message "")))
3690
3691 ;; Check whether to use long file names.
3692 (defun gnus-use-long-file-name (symbol)
3693   ;; The variable has to be set...
3694   (and gnus-use-long-file-name
3695        ;; If it isn't a list, then we return t.
3696        (or (not (listp gnus-use-long-file-name))
3697            ;; If it is a list, and the list contains `symbol', we
3698            ;; return nil.
3699            (not (memq symbol gnus-use-long-file-name)))))
3700
3701 ;; I suspect there's a better way, but I haven't taken the time to do
3702 ;; it yet. -erik selberg@cs.washington.edu
3703 (defun gnus-dd-mmm (messy-date)
3704   "Return a string like DD-MMM from a big messy string"
3705   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3706                    (error nil))))
3707     (if (not datevec)
3708         "??-???"
3709       (format "%2s-%s"
3710               (condition-case ()
3711                   ;; Make sure leading zeroes are stripped.
3712                   (number-to-string (string-to-number (aref datevec 2)))
3713                 (error "??"))
3714               (capitalize
3715                (or (car
3716                     (nth (1- (string-to-number (aref datevec 1)))
3717                          timezone-months-assoc))
3718                    "???"))))))
3719
3720 (defun gnus-mode-string-quote (string)
3721   "Quote all \"%\" in STRING."
3722   (save-excursion
3723     (gnus-set-work-buffer)
3724     (insert string)
3725     (goto-char (point-min))
3726     (while (search-forward "%" nil t)
3727       (insert "%"))
3728     (buffer-string)))
3729
3730 ;; Make a hash table (default and minimum size is 255).
3731 ;; Optional argument HASHSIZE specifies the table size.
3732 (defun gnus-make-hashtable (&optional hashsize)
3733   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3734
3735 ;; Make a number that is suitable for hashing; bigger than MIN and one
3736 ;; less than 2^x.
3737 (defun gnus-create-hash-size (min)
3738   (let ((i 1))
3739     (while (< i min)
3740       (setq i (* 2 i)))
3741     (1- i)))
3742
3743 ;; Show message if message has a lower level than `gnus-verbose'.
3744 ;; Guideline for numbers:
3745 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3746 ;; for things that take a long time, 7 - not very important messages
3747 ;; on stuff, 9 - messages inside loops.
3748 (defun gnus-message (level &rest args)
3749   (if (<= level gnus-verbose)
3750       (apply 'message args)
3751     ;; We have to do this format thingy here even if the result isn't
3752     ;; shown - the return value has to be the same as the return value
3753     ;; from `message'.
3754     (apply 'format args)))
3755
3756 (defun gnus-error (level &rest args)
3757   "Beep an error if `gnus-verbose' is on LEVEL or less."
3758   (when (<= (floor level) gnus-verbose)
3759     (apply 'message args)
3760     (ding)
3761     (let (duration)
3762       (when (and (floatp level)
3763                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3764         (sit-for duration))))
3765   nil)
3766
3767 ;; Generate a unique new group name.
3768 (defun gnus-generate-new-group-name (leaf)
3769   (let ((name leaf)
3770         (num 0))
3771     (while (gnus-gethash name gnus-newsrc-hashtb)
3772       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3773     name))
3774
3775 (defsubst gnus-hide-text (b e props)
3776   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3777   (gnus-add-text-properties b e props)
3778   (when (memq 'intangible props)
3779     (gnus-put-text-property (max (1- b) (point-min))
3780                        b 'intangible (cddr (memq 'intangible props)))))
3781
3782 (defsubst gnus-unhide-text (b e)
3783   "Remove hidden text properties from region between B and E."
3784   (remove-text-properties b e gnus-hidden-properties)
3785   (when (memq 'intangible gnus-hidden-properties)
3786     (gnus-put-text-property (max (1- b) (point-min))
3787                        b 'intangible nil)))
3788
3789 (defun gnus-hide-text-type (b e type)
3790   "Hide text of TYPE between B and E."
3791   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3792
3793 ;; Find out whether the gnus-visual TYPE is wanted.
3794 (defun gnus-visual-p (&optional type class)
3795   (and gnus-visual                      ; Has to be non-nil, at least.
3796        (if (not type)                   ; We don't care about type.
3797            gnus-visual
3798          (if (listp gnus-visual)        ; It's a list, so we check it.
3799              (or (memq type gnus-visual)
3800                  (memq class gnus-visual))
3801            t))))
3802
3803 (defun gnus-parent-headers (headers &optional generation)
3804   "Return the headers of the GENERATIONeth parent of HEADERS."
3805   (unless generation 
3806     (setq generation 1))
3807   (let (references parent)
3808     (while (and headers (not (zerop generation)))
3809       (setq references (mail-header-references headers))
3810       (when (and references
3811                  (setq parent (gnus-parent-id references))
3812                  (setq headers (car (gnus-id-to-thread parent))))
3813         (decf generation)))
3814     headers))
3815
3816 (defun gnus-parent-id (references)
3817   "Return the last Message-ID in REFERENCES."
3818   (when (and references
3819              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3820     (substring references (match-beginning 1) (match-end 1))))
3821
3822 (defun gnus-split-references (references)
3823   "Return a list of Message-IDs in REFERENCES."
3824   (let ((beg 0)
3825         ids)
3826     (while (string-match "<[^>]+>" references beg)
3827       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3828             ids))
3829     (nreverse ids)))
3830
3831 (defun gnus-buffer-live-p (buffer)
3832   "Say whether BUFFER is alive or not."
3833   (and buffer
3834        (get-buffer buffer)
3835        (buffer-name (get-buffer buffer))))
3836
3837 (defun gnus-ephemeral-group-p (group)
3838   "Say whether GROUP is ephemeral or not."
3839   (gnus-group-get-parameter group 'quit-config))
3840
3841 (defun gnus-group-quit-config (group)
3842   "Return the quit-config of GROUP."
3843   (gnus-group-get-parameter group 'quit-config))
3844
3845 (defun gnus-simplify-mode-line ()
3846   "Make mode lines a bit simpler."
3847   (setq mode-line-modified "-- ")
3848   (when (listp mode-line-format)
3849     (make-local-variable 'mode-line-format)
3850     (setq mode-line-format (copy-sequence mode-line-format))
3851     (when (equal (nth 3 mode-line-format) "   ")
3852       (setcar (nthcdr 3 mode-line-format) " "))))
3853
3854 ;;; List and range functions
3855
3856 (defun gnus-last-element (list)
3857   "Return last element of LIST."
3858   (while (cdr list)
3859     (setq list (cdr list)))
3860   (car list))
3861
3862 (defun gnus-copy-sequence (list)
3863   "Do a complete, total copy of a list."
3864   (if (and (consp list) (not (consp (cdr list))))
3865       (cons (car list) (cdr list))
3866     (mapcar (lambda (elem) (if (consp elem)
3867                                (if (consp (cdr elem))
3868                                    (gnus-copy-sequence elem)
3869                                  (cons (car elem) (cdr elem)))
3870                              elem))
3871             list)))
3872
3873 (defun gnus-set-difference (list1 list2)
3874   "Return a list of elements of LIST1 that do not appear in LIST2."
3875   (let ((list1 (copy-sequence list1)))
3876     (while list2
3877       (setq list1 (delq (car list2) list1))
3878       (setq list2 (cdr list2)))
3879     list1))
3880
3881 (defun gnus-sorted-complement (list1 list2)
3882   "Return a list of elements of LIST1 that do not appear in LIST2.
3883 Both lists have to be sorted over <."
3884   (let (out)
3885     (if (or (null list1) (null list2))
3886         (or list1 list2)
3887       (while (and list1 list2)
3888         (cond ((= (car list1) (car list2))
3889                (setq list1 (cdr list1)
3890                      list2 (cdr list2)))
3891               ((< (car list1) (car list2))
3892                (setq out (cons (car list1) out))
3893                (setq list1 (cdr list1)))
3894               (t
3895                (setq out (cons (car list2) out))
3896                (setq list2 (cdr list2)))))
3897       (nconc (nreverse out) (or list1 list2)))))
3898
3899 (defun gnus-intersection (list1 list2)
3900   (let ((result nil))
3901     (while list2
3902       (if (memq (car list2) list1)
3903           (setq result (cons (car list2) result)))
3904       (setq list2 (cdr list2)))
3905     result))
3906
3907 (defun gnus-sorted-intersection (list1 list2)
3908   ;; LIST1 and LIST2 have to be sorted over <.
3909   (let (out)
3910     (while (and list1 list2)
3911       (cond ((= (car list1) (car list2))
3912              (setq out (cons (car list1) out)
3913                    list1 (cdr list1)
3914                    list2 (cdr list2)))
3915             ((< (car list1) (car list2))
3916              (setq list1 (cdr list1)))
3917             (t
3918              (setq list2 (cdr list2)))))
3919     (nreverse out)))
3920
3921 (defun gnus-set-sorted-intersection (list1 list2)
3922   ;; LIST1 and LIST2 have to be sorted over <.
3923   ;; This function modifies LIST1.
3924   (let* ((top (cons nil list1))
3925          (prev top))
3926     (while (and list1 list2)
3927       (cond ((= (car list1) (car list2))
3928              (setq prev list1
3929                    list1 (cdr list1)
3930                    list2 (cdr list2)))
3931             ((< (car list1) (car list2))
3932              (setcdr prev (cdr list1))
3933              (setq list1 (cdr list1)))
3934             (t
3935              (setq list2 (cdr list2)))))
3936     (setcdr prev nil)
3937     (cdr top)))
3938
3939 (defun gnus-compress-sequence (numbers &optional always-list)
3940   "Convert list of numbers to a list of ranges or a single range.
3941 If ALWAYS-LIST is non-nil, this function will always release a list of
3942 ranges."
3943   (let* ((first (car numbers))
3944          (last (car numbers))
3945          result)
3946     (if (null numbers)
3947         nil
3948       (if (not (listp (cdr numbers)))
3949           numbers
3950         (while numbers
3951           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3952                 ((= (1+ last) (car numbers)) ;Still in sequence
3953                  (setq last (car numbers)))
3954                 (t                      ;End of one sequence
3955                  (setq result
3956                        (cons (if (= first last) first
3957                                (cons first last)) result))
3958                  (setq first (car numbers))
3959                  (setq last  (car numbers))))
3960           (setq numbers (cdr numbers)))
3961         (if (and (not always-list) (null result))
3962             (if (= first last) (list first) (cons first last))
3963           (nreverse (cons (if (= first last) first (cons first last))
3964                           result)))))))
3965
3966 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3967 (defun gnus-uncompress-range (ranges)
3968   "Expand a list of ranges into a list of numbers.
3969 RANGES is either a single range on the form `(num . num)' or a list of
3970 these ranges."
3971   (let (first last result)
3972     (cond
3973      ((null ranges)
3974       nil)
3975      ((not (listp (cdr ranges)))
3976       (setq first (car ranges))
3977       (setq last (cdr ranges))
3978       (while (<= first last)
3979         (setq result (cons first result))
3980         (setq first (1+ first)))
3981       (nreverse result))
3982      (t
3983       (while ranges
3984         (if (atom (car ranges))
3985             (if (numberp (car ranges))
3986                 (setq result (cons (car ranges) result)))
3987           (setq first (caar ranges))
3988           (setq last  (cdar ranges))
3989           (while (<= first last)
3990             (setq result (cons first result))
3991             (setq first (1+ first))))
3992         (setq ranges (cdr ranges)))
3993       (nreverse result)))))
3994
3995 (defun gnus-add-to-range (ranges list)
3996   "Return a list of ranges that has all articles from both RANGES and LIST.
3997 Note: LIST has to be sorted over `<'."
3998   (if (not ranges)
3999       (gnus-compress-sequence list t)
4000     (setq list (copy-sequence list))
4001     (or (listp (cdr ranges))
4002         (setq ranges (list ranges)))
4003     (let ((out ranges)
4004           ilist lowest highest temp)
4005       (while (and ranges list)
4006         (setq ilist list)
4007         (setq lowest (or (and (atom (car ranges)) (car ranges))
4008                          (caar ranges)))
4009         (while (and list (cdr list) (< (cadr list) lowest))
4010           (setq list (cdr list)))
4011         (if (< (car ilist) lowest)
4012             (progn
4013               (setq temp list)
4014               (setq list (cdr list))
4015               (setcdr temp nil)
4016               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4017         (setq highest (or (and (atom (car ranges)) (car ranges))
4018                           (cdar ranges)))
4019         (while (and list (<= (car list) highest))
4020           (setq list (cdr list)))
4021         (setq ranges (cdr ranges)))
4022       (if list
4023           (setq out (nconc (gnus-compress-sequence list t) out)))
4024       (setq out (sort out (lambda (r1 r2)
4025                             (< (or (and (atom r1) r1) (car r1))
4026                                (or (and (atom r2) r2) (car r2))))))
4027       (setq ranges out)
4028       (while ranges
4029         (if (atom (car ranges))
4030             (if (cdr ranges)
4031                 (if (atom (cadr ranges))
4032                     (if (= (1+ (car ranges)) (cadr ranges))
4033                         (progn
4034                           (setcar ranges (cons (car ranges)
4035                                                (cadr ranges)))
4036                           (setcdr ranges (cddr ranges))))
4037                   (if (= (1+ (car ranges)) (caadr ranges))
4038                       (progn
4039                         (setcar (cadr ranges) (car ranges))
4040                         (setcar ranges (cadr ranges))
4041                         (setcdr ranges (cddr ranges))))))
4042           (if (cdr ranges)
4043               (if (atom (cadr ranges))
4044                   (if (= (1+ (cdar ranges)) (cadr ranges))
4045                       (progn
4046                         (setcdr (car ranges) (cadr ranges))
4047                         (setcdr ranges (cddr ranges))))
4048                 (if (= (1+ (cdar ranges)) (caadr ranges))
4049                     (progn
4050                       (setcdr (car ranges) (cdadr ranges))
4051                       (setcdr ranges (cddr ranges)))))))
4052         (setq ranges (cdr ranges)))
4053       out)))
4054
4055 (defun gnus-remove-from-range (ranges list)
4056   "Return a list of ranges that has all articles from LIST removed from RANGES.
4057 Note: LIST has to be sorted over `<'."
4058   ;; !!! This function shouldn't look like this, but I've got a headache.
4059   (gnus-compress-sequence
4060    (gnus-sorted-complement
4061     (gnus-uncompress-range ranges) list)))
4062
4063 (defun gnus-member-of-range (number ranges)
4064   (if (not (listp (cdr ranges)))
4065       (and (>= number (car ranges))
4066            (<= number (cdr ranges)))
4067     (let ((not-stop t))
4068       (while (and ranges
4069                   (if (numberp (car ranges))
4070                       (>= number (car ranges))
4071                     (>= number (caar ranges)))
4072                   not-stop)
4073         (if (if (numberp (car ranges))
4074                 (= number (car ranges))
4075               (and (>= number (caar ranges))
4076                    (<= number (cdar ranges))))
4077             (setq not-stop nil))
4078         (setq ranges (cdr ranges)))
4079       (not not-stop))))
4080
4081 (defun gnus-range-length (range)
4082   "Return the length RANGE would have if uncompressed."
4083   (length (gnus-uncompress-range range)))
4084
4085 (defun gnus-sublist-p (list sublist)
4086   "Test whether all elements in SUBLIST are members of LIST."
4087   (let ((sublistp t))
4088     (while sublist
4089       (unless (memq (pop sublist) list)
4090         (setq sublistp nil
4091               sublist nil)))
4092     sublistp))
4093
4094 \f
4095 ;;;
4096 ;;; Gnus group mode
4097 ;;;
4098
4099 (defvar gnus-group-mode-map nil)
4100 (put 'gnus-group-mode 'mode-class 'special)
4101
4102 (unless gnus-group-mode-map
4103   (setq gnus-group-mode-map (make-keymap))
4104   (suppress-keymap gnus-group-mode-map)
4105
4106   (gnus-define-keys gnus-group-mode-map
4107     " " gnus-group-read-group
4108     "=" gnus-group-select-group
4109     "\r" gnus-group-select-group
4110     "\M-\r" gnus-group-quick-select-group
4111     "j" gnus-group-jump-to-group
4112     "n" gnus-group-next-unread-group
4113     "p" gnus-group-prev-unread-group
4114     "\177" gnus-group-prev-unread-group
4115     [delete] gnus-group-prev-unread-group
4116     "N" gnus-group-next-group
4117     "P" gnus-group-prev-group
4118     "\M-n" gnus-group-next-unread-group-same-level
4119     "\M-p" gnus-group-prev-unread-group-same-level
4120     "," gnus-group-best-unread-group
4121     "." gnus-group-first-unread-group
4122     "u" gnus-group-unsubscribe-current-group
4123     "U" gnus-group-unsubscribe-group
4124     "c" gnus-group-catchup-current
4125     "C" gnus-group-catchup-current-all
4126     "l" gnus-group-list-groups
4127     "L" gnus-group-list-all-groups
4128     "m" gnus-group-mail
4129     "g" gnus-group-get-new-news
4130     "\M-g" gnus-group-get-new-news-this-group
4131     "R" gnus-group-restart
4132     "r" gnus-group-read-init-file
4133     "B" gnus-group-browse-foreign-server
4134     "b" gnus-group-check-bogus-groups
4135     "F" gnus-find-new-newsgroups
4136     "\C-c\C-d" gnus-group-describe-group
4137     "\M-d" gnus-group-describe-all-groups
4138     "\C-c\C-a" gnus-group-apropos
4139     "\C-c\M-\C-a" gnus-group-description-apropos
4140     "a" gnus-group-post-news
4141     "\ek" gnus-group-edit-local-kill
4142     "\eK" gnus-group-edit-global-kill
4143     "\C-k" gnus-group-kill-group
4144     "\C-y" gnus-group-yank-group
4145     "\C-w" gnus-group-kill-region
4146     "\C-x\C-t" gnus-group-transpose-groups
4147     "\C-c\C-l" gnus-group-list-killed
4148     "\C-c\C-x" gnus-group-expire-articles
4149     "\C-c\M-\C-x" gnus-group-expire-all-groups
4150     "V" gnus-version
4151     "s" gnus-group-save-newsrc
4152     "z" gnus-group-suspend
4153     "Z" gnus-group-clear-dribble
4154     "q" gnus-group-exit
4155     "Q" gnus-group-quit
4156     "?" gnus-group-describe-briefly
4157     "\C-c\C-i" gnus-info-find-node
4158     "\M-e" gnus-group-edit-group-method
4159     "^" gnus-group-enter-server-mode
4160     gnus-mouse-2 gnus-mouse-pick-group
4161     "<" beginning-of-buffer
4162     ">" end-of-buffer
4163     "\C-c\C-b" gnus-bug
4164     "\C-c\C-s" gnus-group-sort-groups
4165     "t" gnus-topic-mode
4166     "\C-c\M-g" gnus-activate-all-groups
4167     "\M-&" gnus-group-universal-argument
4168     "#" gnus-group-mark-group
4169     "\M-#" gnus-group-unmark-group)
4170
4171   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4172     "m" gnus-group-mark-group
4173     "u" gnus-group-unmark-group
4174     "w" gnus-group-mark-region
4175     "m" gnus-group-mark-buffer
4176     "r" gnus-group-mark-regexp
4177     "U" gnus-group-unmark-all-groups)
4178
4179   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4180     "d" gnus-group-make-directory-group
4181     "h" gnus-group-make-help-group
4182     "a" gnus-group-make-archive-group
4183     "k" gnus-group-make-kiboze-group
4184     "m" gnus-group-make-group
4185     "E" gnus-group-edit-group
4186     "e" gnus-group-edit-group-method
4187     "p" gnus-group-edit-group-parameters
4188     "v" gnus-group-add-to-virtual
4189     "V" gnus-group-make-empty-virtual
4190     "D" gnus-group-enter-directory
4191     "f" gnus-group-make-doc-group
4192     "r" gnus-group-rename-group
4193     "\177" gnus-group-delete-group
4194     [delete] gnus-group-delete-group)
4195
4196    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4197      "b" gnus-group-brew-soup
4198      "w" gnus-soup-save-areas
4199      "s" gnus-soup-send-replies
4200      "p" gnus-soup-pack-packet
4201      "r" nnsoup-pack-replies)
4202
4203    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4204      "s" gnus-group-sort-groups
4205      "a" gnus-group-sort-groups-by-alphabet
4206      "u" gnus-group-sort-groups-by-unread
4207      "l" gnus-group-sort-groups-by-level
4208      "v" gnus-group-sort-groups-by-score
4209      "r" gnus-group-sort-groups-by-rank
4210      "m" gnus-group-sort-groups-by-method)
4211
4212    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4213      "k" gnus-group-list-killed
4214      "z" gnus-group-list-zombies
4215      "s" gnus-group-list-groups
4216      "u" gnus-group-list-all-groups
4217      "A" gnus-group-list-active
4218      "a" gnus-group-apropos
4219      "d" gnus-group-description-apropos
4220      "m" gnus-group-list-matching
4221      "M" gnus-group-list-all-matching
4222      "l" gnus-group-list-level)
4223
4224    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4225      "f" gnus-score-flush-cache)
4226
4227    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4228      "f" gnus-group-fetch-faq)
4229
4230    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4231      "l" gnus-group-set-current-level
4232      "t" gnus-group-unsubscribe-current-group
4233      "s" gnus-group-unsubscribe-group
4234      "k" gnus-group-kill-group
4235      "y" gnus-group-yank-group
4236      "w" gnus-group-kill-region
4237      "\C-k" gnus-group-kill-level
4238      "z" gnus-group-kill-all-zombies))
4239
4240 (defun gnus-group-mode ()
4241   "Major mode for reading news.
4242
4243 All normal editing commands are switched off.
4244 \\<gnus-group-mode-map>
4245 The group buffer lists (some of) the groups available.  For instance,
4246 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4247 lists all zombie groups.
4248
4249 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4250 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4251
4252 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4253
4254 The following commands are available:
4255
4256 \\{gnus-group-mode-map}"
4257   (interactive)
4258   (when (and menu-bar-mode
4259              (gnus-visual-p 'group-menu 'menu))
4260     (gnus-group-make-menu-bar))
4261   (kill-all-local-variables)
4262   (gnus-simplify-mode-line)
4263   (setq major-mode 'gnus-group-mode)
4264   (setq mode-name "Group")
4265   (gnus-group-set-mode-line)
4266   (setq mode-line-process nil)
4267   (use-local-map gnus-group-mode-map)
4268   (buffer-disable-undo (current-buffer))
4269   (setq truncate-lines t)
4270   (setq buffer-read-only t)
4271   (gnus-make-local-hook 'post-command-hook)
4272   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4273   (run-hooks 'gnus-group-mode-hook))
4274
4275 (defun gnus-clear-inboxes-moved ()
4276   (setq nnmail-moved-inboxes nil))
4277
4278 (defun gnus-mouse-pick-group (e)
4279   "Enter the group under the mouse pointer."
4280   (interactive "e")
4281   (mouse-set-point e)
4282   (gnus-group-read-group nil))
4283
4284 ;; Look at LEVEL and find out what the level is really supposed to be.
4285 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4286 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4287 (defun gnus-group-default-level (&optional level number-or-nil)
4288   (cond
4289    (gnus-group-use-permanent-levels
4290     (or (setq gnus-group-use-permanent-levels
4291               (or level (if (numberp gnus-group-use-permanent-levels)
4292                             gnus-group-use-permanent-levels
4293                           (or gnus-group-default-list-level
4294                               gnus-level-subscribed))))
4295         gnus-group-default-list-level gnus-level-subscribed))
4296    (number-or-nil
4297     level)
4298    (t
4299     (or level gnus-group-default-list-level gnus-level-subscribed))))
4300
4301 ;;;###autoload
4302 (defun gnus-slave-no-server (&optional arg)
4303   "Read network news as a slave, without connecting to local server"
4304   (interactive "P")
4305   (gnus-no-server arg t))
4306
4307 ;;;###autoload
4308 (defun gnus-no-server (&optional arg slave)
4309   "Read network news.
4310 If ARG is a positive number, Gnus will use that as the
4311 startup level.  If ARG is nil, Gnus will be started at level 2.
4312 If ARG is non-nil and not a positive number, Gnus will
4313 prompt the user for the name of an NNTP server to use.
4314 As opposed to `gnus', this command will not connect to the local server."
4315   (interactive "P")
4316   (let ((val (or arg (1- gnus-level-default-subscribed))))
4317     (gnus val t slave)
4318     (make-local-variable 'gnus-group-use-permanent-levels)
4319     (setq gnus-group-use-permanent-levels val)))
4320
4321 ;;;###autoload
4322 (defun gnus-slave (&optional arg)
4323   "Read news as a slave."
4324   (interactive "P")
4325   (gnus arg nil 'slave))
4326
4327 ;;;###autoload
4328 (defun gnus-other-frame (&optional arg)
4329   "Pop up a frame to read news."
4330   (interactive "P")
4331   (if (get-buffer gnus-group-buffer)
4332       (let ((pop-up-frames t))
4333         (gnus arg))
4334     (select-frame (make-frame))
4335     (gnus arg)))
4336
4337 ;;;###autoload
4338 (defun gnus (&optional arg dont-connect slave)
4339   "Read network news.
4340 If ARG is non-nil and a positive number, Gnus will use that as the
4341 startup level.  If ARG is non-nil and not a positive number, Gnus will
4342 prompt the user for the name of an NNTP server to use."
4343   (interactive "P")
4344
4345   (if (get-buffer gnus-group-buffer)
4346       (progn
4347         (switch-to-buffer gnus-group-buffer)
4348         (gnus-group-get-new-news))
4349
4350     (gnus-clear-system)
4351     (nnheader-init-server-buffer)
4352     (gnus-read-init-file)
4353     (setq gnus-slave slave)
4354
4355     (gnus-group-setup-buffer)
4356     (let ((buffer-read-only nil))
4357       (erase-buffer)
4358       (if (not gnus-inhibit-startup-message)
4359           (progn
4360             (gnus-group-startup-message)
4361             (sit-for 0))))
4362
4363     (let ((level (and (numberp arg) (> arg 0) arg))
4364           did-connect)
4365       (unwind-protect
4366           (progn
4367             (or dont-connect
4368                 (setq did-connect
4369                       (gnus-start-news-server (and arg (not level))))))
4370         (if (and (not dont-connect)
4371                  (not did-connect))
4372             (gnus-group-quit)
4373           (run-hooks 'gnus-startup-hook)
4374           ;; NNTP server is successfully open.
4375
4376           ;; Find the current startup file name.
4377           (setq gnus-current-startup-file
4378                 (gnus-make-newsrc-file gnus-startup-file))
4379
4380           ;; Read the dribble file.
4381           (when (or gnus-slave gnus-use-dribble-file)
4382             (gnus-dribble-read-file))
4383
4384           ;; Allow using GroupLens predictions.
4385           (when gnus-use-grouplens
4386             (bbb-login)
4387             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4388
4389           (gnus-summary-make-display-table)
4390           ;; Do the actual startup.
4391           (gnus-setup-news nil level dont-connect)
4392           ;; Generate the group buffer.
4393           (gnus-group-list-groups level)
4394           (gnus-group-first-unread-group)
4395           (gnus-configure-windows 'group)
4396           (gnus-group-set-mode-line))))))
4397
4398 (defun gnus-unload ()
4399   "Unload all Gnus features."
4400   (interactive)
4401   (or (boundp 'load-history)
4402       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4403   (let ((history load-history)
4404         feature)
4405     (while history
4406       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4407            (setq feature (cdr (assq 'provide (car history))))
4408            (unload-feature feature 'force))
4409       (setq history (cdr history)))))
4410
4411 (defun gnus-compile ()
4412   "Byte-compile the user-defined format specs."
4413   (interactive)
4414   (let ((entries gnus-format-specs)
4415         entry gnus-tmp-func)
4416     (save-excursion
4417       (gnus-message 7 "Compiling format specs...")
4418
4419       (while entries
4420         (setq entry (pop entries))
4421         (if (eq (car entry) 'version)
4422             (setq gnus-format-specs (delq entry gnus-format-specs))
4423           (when (and (listp (caddr entry))
4424                      (not (eq 'byte-code (caaddr entry))))
4425             (fset 'gnus-tmp-func
4426                   `(lambda () ,(caddr entry)))
4427             (byte-compile 'gnus-tmp-func)
4428             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4429
4430       (push (cons 'version emacs-version) gnus-format-specs)
4431
4432       (gnus-message 7 "Compiling user specs...done"))))
4433
4434 (defun gnus-indent-rigidly (start end arg)
4435   "Indent rigidly using only spaces and no tabs."
4436   (save-excursion
4437     (save-restriction
4438       (narrow-to-region start end)
4439       (indent-rigidly start end arg)
4440       (goto-char (point-min))
4441       (while (search-forward "\t" nil t)
4442         (replace-match "        " t t)))))
4443
4444 (defun gnus-group-startup-message (&optional x y)
4445   "Insert startup message in current buffer."
4446   ;; Insert the message.
4447   (erase-buffer)
4448   (insert
4449    (format "              %s
4450           _    ___ _             _
4451           _ ___ __ ___  __    _ ___
4452           __   _     ___    __  ___
4453               _           ___     _
4454              _  _ __             _
4455              ___   __            _
4456                    __           _
4457                     _      _   _
4458                    _      _    _
4459                       _  _    _
4460                   __  ___
4461                  _   _ _     _
4462                 _   _
4463               _    _
4464              _    _
4465             _
4466           __
4467
4468 "
4469            ""))
4470   ;; And then hack it.
4471   (gnus-indent-rigidly (point-min) (point-max)
4472                        (/ (max (- (window-width) (or x 46)) 0) 2))
4473   (goto-char (point-min))
4474   (forward-line 1)
4475   (let* ((pheight (count-lines (point-min) (point-max)))
4476          (wheight (window-height))
4477          (rest (- wheight pheight)))
4478     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4479   ;; Fontify some.
4480   (goto-char (point-min))
4481   (and (search-forward "Praxis" nil t)
4482        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4483   (goto-char (point-min))
4484   (let* ((mode-string (gnus-group-set-mode-line)))
4485     (setq mode-line-buffer-identification
4486           (list (concat gnus-version (substring (car mode-string) 4))))
4487     (set-buffer-modified-p t)))
4488
4489 (defun gnus-group-setup-buffer ()
4490   (or (get-buffer gnus-group-buffer)
4491       (progn
4492         (switch-to-buffer gnus-group-buffer)
4493         (gnus-add-current-to-buffer-list)
4494         (gnus-group-mode)
4495         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4496
4497 (defun gnus-group-list-groups (&optional level unread lowest)
4498   "List newsgroups with level LEVEL or lower that have unread articles.
4499 Default is all subscribed groups.
4500 If argument UNREAD is non-nil, groups with no unread articles are also
4501 listed."
4502   (interactive (list (if current-prefix-arg
4503                          (prefix-numeric-value current-prefix-arg)
4504                        (or
4505                         (gnus-group-default-level nil t)
4506                         gnus-group-default-list-level
4507                         gnus-level-subscribed))))
4508   (or level
4509       (setq level (car gnus-group-list-mode)
4510             unread (cdr gnus-group-list-mode)))
4511   (setq level (gnus-group-default-level level))
4512   (gnus-group-setup-buffer)             ;May call from out of group buffer
4513   (gnus-update-format-specifications)
4514   (let ((case-fold-search nil)
4515         (props (text-properties-at (gnus-point-at-bol)))
4516         (group (gnus-group-group-name)))
4517     (set-buffer gnus-group-buffer)
4518     (funcall gnus-group-prepare-function level unread lowest)
4519     (if (zerop (buffer-size))
4520         (gnus-message 5 gnus-no-groups-message)
4521       (goto-char (point-max))
4522       (when (or (not gnus-group-goto-next-group-function)
4523                 (not (funcall gnus-group-goto-next-group-function 
4524                               group props)))
4525         (if (not group)
4526             ;; Go to the first group with unread articles.
4527             (gnus-group-search-forward t)
4528           ;; Find the right group to put point on.  If the current group
4529           ;; has disappeared in the new listing, try to find the next
4530           ;; one.        If no next one can be found, just leave point at the
4531           ;; first newsgroup in the buffer.
4532           (if (not (gnus-goto-char
4533                     (text-property-any
4534                      (point-min) (point-max)
4535                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4536               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4537                 (while (and newsrc
4538                             (not (gnus-goto-char
4539                                   (text-property-any
4540                                    (point-min) (point-max) 'gnus-group
4541                                    (gnus-intern-safe
4542                                     (caar newsrc) gnus-active-hashtb)))))
4543                   (setq newsrc (cdr newsrc)))
4544                 (or newsrc (progn (goto-char (point-max))
4545                                   (forward-line -1)))))))
4546       ;; Adjust cursor point.
4547       (gnus-group-position-point))))
4548
4549 (defun gnus-group-list-level (level &optional all)
4550   "List groups on LEVEL.
4551 If ALL (the prefix), also list groups that have no unread articles."
4552   (interactive "nList groups on level: \nP")
4553   (gnus-group-list-groups level all level))
4554
4555 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4556   "List all newsgroups with unread articles of level LEVEL or lower.
4557 If ALL is non-nil, list groups that have no unread articles.
4558 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4559 If REGEXP, only list groups matching REGEXP."
4560   (set-buffer gnus-group-buffer)
4561   (let ((buffer-read-only nil)
4562         (newsrc (cdr gnus-newsrc-alist))
4563         (lowest (or lowest 1))
4564         info clevel unread group params)
4565     (erase-buffer)
4566     (if (< lowest gnus-level-zombie)
4567         ;; List living groups.
4568         (while newsrc
4569           (setq info (car newsrc)
4570                 group (gnus-info-group info)
4571                 params (gnus-info-params info)
4572                 newsrc (cdr newsrc)
4573                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4574           (and unread                   ; This group might be bogus
4575                (or (not regexp)
4576                    (string-match regexp group))
4577                (<= (setq clevel (gnus-info-level info)) level)
4578                (>= clevel lowest)
4579                (or all                  ; We list all groups?
4580                    (if (eq unread t)    ; Unactivated?
4581                        gnus-group-list-inactive-groups ; We list unactivated 
4582                      (> unread 0))      ; We list groups with unread articles
4583                    (and gnus-list-groups-with-ticked-articles
4584                         (cdr (assq 'tick (gnus-info-marks info))))
4585                                         ; And groups with tickeds
4586                    ;; Check for permanent visibility.
4587                    (and gnus-permanently-visible-groups
4588                         (string-match gnus-permanently-visible-groups
4589                                       group))
4590                    (memq 'visible params)
4591                    (cdr (assq 'visible params)))
4592                (gnus-group-insert-group-line
4593                 group (gnus-info-level info)
4594                 (gnus-info-marks info) unread (gnus-info-method info)))))
4595
4596     ;; List dead groups.
4597     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4598          (gnus-group-prepare-flat-list-dead
4599           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4600           gnus-level-zombie ?Z
4601           regexp))
4602     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4603          (gnus-group-prepare-flat-list-dead
4604           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4605           gnus-level-killed ?K regexp))
4606
4607     (gnus-group-set-mode-line)
4608     (setq gnus-group-list-mode (cons level all))
4609     (run-hooks 'gnus-group-prepare-hook)))
4610
4611 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4612   ;; List zombies and killed lists somewhat faster, which was
4613   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4614   ;; this by ignoring the group format specification altogether.
4615   (let (group)
4616     (if regexp
4617         ;; This loop is used when listing groups that match some
4618         ;; regexp.
4619         (while groups
4620           (setq group (pop groups))
4621           (when (string-match regexp group)
4622             (gnus-add-text-properties
4623              (point) (prog1 (1+ (point))
4624                        (insert " " mark "     *: " group "\n"))
4625              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4626                    'gnus-unread t
4627                    'gnus-level level))))
4628       ;; This loop is used when listing all groups.
4629       (while groups
4630         (gnus-add-text-properties
4631          (point) (prog1 (1+ (point))
4632                    (insert " " mark "     *: "
4633                            (setq group (pop groups)) "\n"))
4634          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4635                'gnus-unread t
4636                'gnus-level level))))))
4637
4638 (defmacro gnus-group-real-name (group)
4639   "Find the real name of a foreign newsgroup."
4640   `(let ((gname ,group))
4641      (if (string-match ":[^:]+$" gname)
4642          (substring gname (1+ (match-beginning 0)))
4643        gname)))
4644
4645 (defsubst gnus-server-add-address (method)
4646   (let ((method-name (symbol-name (car method))))
4647     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4648              (not (assq (intern (concat method-name "-address")) method)))
4649         (append method (list (list (intern (concat method-name "-address"))
4650                                    (nth 1 method))))
4651       method)))
4652
4653 (defsubst gnus-server-get-method (group method)
4654   ;; Input either a server name, and extended server name, or a
4655   ;; select method, and return a select method.
4656   (cond ((stringp method)
4657          (gnus-server-to-method method))
4658         ((equal method gnus-select-method)
4659          gnus-select-method)
4660         ((and (stringp (car method)) group)
4661          (gnus-server-extend-method group method))
4662         ((and method (not group)
4663               (equal (cadr method) ""))
4664          method)
4665         (t
4666          (gnus-server-add-address method))))
4667
4668 (defun gnus-server-to-method (server)
4669   "Map virtual server names to select methods."
4670   (or 
4671    ;; Is this a method, perhaps?
4672    (and server (listp server) server)
4673    ;; Perhaps this is the native server?
4674    (and (equal server "native") gnus-select-method)
4675    ;; It should be in the server alist.
4676    (cdr (assoc server gnus-server-alist))
4677    ;; If not, we look through all the opened server
4678    ;; to see whether we can find it there.
4679    (let ((opened gnus-opened-servers))
4680      (while (and opened
4681                  (not (equal server (format "%s:%s" (caaar opened)
4682                                             (cadaar opened)))))
4683        (pop opened))
4684      (caar opened))))
4685
4686 (defmacro gnus-method-equal (ss1 ss2)
4687   "Say whether two servers are equal."
4688   `(let ((s1 ,ss1)
4689          (s2 ,ss2))
4690      (or (equal s1 s2)
4691          (and (= (length s1) (length s2))
4692               (progn
4693                 (while (and s1 (member (car s1) s2))
4694                   (setq s1 (cdr s1)))
4695                 (null s1))))))
4696
4697 (defun gnus-server-equal (m1 m2)
4698   "Say whether two methods are equal."
4699   (let ((m1 (cond ((null m1) gnus-select-method)
4700                   ((stringp m1) (gnus-server-to-method m1))
4701                   (t m1)))
4702         (m2 (cond ((null m2) gnus-select-method)
4703                   ((stringp m2) (gnus-server-to-method m2))
4704                   (t m2))))
4705     (gnus-method-equal m1 m2)))
4706
4707 (defun gnus-servers-using-backend (backend)
4708   "Return a list of known servers using BACKEND."
4709   (let ((opened gnus-opened-servers)
4710         out)
4711     (while opened
4712       (when (eq backend (caaar opened))
4713         (push (caar opened) out))
4714       (pop opened))
4715     out))
4716
4717 (defun gnus-group-prefixed-name (group method)
4718   "Return the whole name from GROUP and METHOD."
4719   (and (stringp method) (setq method (gnus-server-to-method method)))
4720   (concat (format "%s" (car method))
4721           (if (and
4722                (or (assoc (format "%s" (car method)) 
4723                           (gnus-methods-using 'address))
4724                    (gnus-server-equal method gnus-message-archive-method))
4725                (nth 1 method)
4726                (not (string= (nth 1 method) "")))
4727               (concat "+" (nth 1 method)))
4728           ":" group))
4729
4730 (defun gnus-group-real-prefix (group)
4731   "Return the prefix of the current group name."
4732   (if (string-match "^[^:]+:" group)
4733       (substring group 0 (match-end 0))
4734     ""))
4735
4736 (defun gnus-group-method (group)
4737   "Return the server or method used for selecting GROUP."
4738   (let ((prefix (gnus-group-real-prefix group)))
4739     (if (equal prefix "")
4740         gnus-select-method
4741       (let ((servers gnus-opened-servers)
4742             (server "")
4743             backend possible found)
4744         (if (string-match "^[^\\+]+\\+" prefix)
4745             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4746                   server (substring prefix (match-end 0) (1- (length prefix))))
4747           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4748         (while servers
4749           (when (eq (caaar servers) backend)
4750             (setq possible (caar servers))
4751             (when (equal (cadaar servers) server)
4752               (setq found (caar servers))))
4753           (pop servers))
4754         (or (car (rassoc found gnus-server-alist))
4755             found
4756             (car (rassoc possible gnus-server-alist))
4757             possible
4758             (list backend server))))))
4759
4760 (defsubst gnus-secondary-method-p (method)
4761   "Return whether METHOD is a secondary select method."
4762   (let ((methods gnus-secondary-select-methods)
4763         (gmethod (gnus-server-get-method nil method)))
4764     (while (and methods
4765                 (not (equal (gnus-server-get-method nil (car methods))
4766                             gmethod)))
4767       (setq methods (cdr methods)))
4768     methods))
4769
4770 (defun gnus-group-foreign-p (group)
4771   "Say whether a group is foreign or not."
4772   (and (not (gnus-group-native-p group))
4773        (not (gnus-group-secondary-p group))))
4774
4775 (defun gnus-group-native-p (group)
4776   "Say whether the group is native or not."
4777   (not (string-match ":" group)))
4778
4779 (defun gnus-group-secondary-p (group)
4780   "Say whether the group is secondary or not."
4781   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4782
4783 (defun gnus-group-get-parameter (group &optional symbol)
4784   "Returns the group parameters for GROUP.
4785 If SYMBOL, return the value of that symbol in the group parameters."
4786   (let ((params (gnus-info-params (gnus-get-info group))))
4787     (if symbol
4788         (gnus-group-parameter-value params symbol)
4789       params)))
4790
4791 (defun gnus-group-parameter-value (params symbol)
4792   "Return the value of SYMBOL in group PARAMS."
4793   (or (car (memq symbol params))        ; It's either a simple symbol
4794       (cdr (assq symbol params))))      ; or a cons.
4795
4796 (defun gnus-group-add-parameter (group param)
4797   "Add parameter PARAM to GROUP."
4798   (let ((info (gnus-get-info group)))
4799     (if (not info)
4800         () ; This is a dead group.  We just ignore it.
4801       ;; Cons the new param to the old one and update.
4802       (gnus-group-set-info (cons param (gnus-info-params info))
4803                            group 'params))))
4804
4805 (defun gnus-group-set-parameter (group name value)
4806   "Set parameter NAME to VALUE in GROUP."
4807   (let ((info (gnus-get-info group)))
4808     (if (not info)
4809         () ; This is a dead group.  We just ignore it.
4810       (let ((old-params (gnus-info-params info))
4811             (new-params (list (cons name value))))
4812         (while old-params
4813           (if (or (not (listp (car old-params)))
4814                   (not (eq (caar old-params) name)))
4815               (setq new-params (append new-params (list (car old-params)))))
4816           (setq old-params (cdr old-params)))
4817         (gnus-group-set-info new-params group 'params)))))
4818
4819 (defun gnus-group-add-score (group &optional score)
4820   "Add SCORE to the GROUP score.
4821 If SCORE is nil, add 1 to the score of GROUP."
4822   (let ((info (gnus-get-info group)))
4823     (when info
4824       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4825
4826 (defun gnus-summary-bubble-group ()
4827   "Increase the score of the current group.
4828 This is a handy function to add to `gnus-summary-exit-hook' to
4829 increase the score of each group you read."
4830   (gnus-group-add-score gnus-newsgroup-name))
4831
4832 (defun gnus-group-set-info (info &optional method-only-group part)
4833   (let* ((entry (gnus-gethash
4834                  (or method-only-group (gnus-info-group info))
4835                  gnus-newsrc-hashtb))
4836          (part-info info)
4837          (info (if method-only-group (nth 2 entry) info))
4838          method)
4839     (when method-only-group
4840       (unless entry
4841         (error "Trying to change non-existent group %s" method-only-group))
4842       ;; We have received parts of the actual group info - either the
4843       ;; select method or the group parameters.  We first check
4844       ;; whether we have to extend the info, and if so, do that.
4845       (let ((len (length info))
4846             (total (if (eq part 'method) 5 6)))
4847         (when (< len total)
4848           (setcdr (nthcdr (1- len) info)
4849                   (make-list (- total len) nil)))
4850         ;; Then we enter the new info.
4851         (setcar (nthcdr (1- total) info) part-info)))
4852     (unless entry
4853       ;; This is a new group, so we just create it.
4854       (save-excursion
4855         (set-buffer gnus-group-buffer)
4856         (setq method (gnus-info-method info))
4857         (when (gnus-server-equal method "native")
4858           (setq method nil))
4859         (save-excursion
4860           (set-buffer gnus-group-buffer)
4861           (if method
4862               ;; It's a foreign group...
4863               (gnus-group-make-group
4864                (gnus-group-real-name (gnus-info-group info))
4865                (if (stringp method) method
4866                  (prin1-to-string (car method)))
4867                (and (consp method)
4868                     (nth 1 (gnus-info-method info))))
4869             ;; It's a native group.
4870             (gnus-group-make-group (gnus-info-group info))))
4871         (gnus-message 6 "Note: New group created")
4872         (setq entry
4873               (gnus-gethash (gnus-group-prefixed-name
4874                              (gnus-group-real-name (gnus-info-group info))
4875                              (or (gnus-info-method info) gnus-select-method))
4876                             gnus-newsrc-hashtb))))
4877     ;; Whether it was a new group or not, we now have the entry, so we
4878     ;; can do the update.
4879     (if entry
4880         (progn
4881           (setcar (nthcdr 2 entry) info)
4882           (when (and (not (eq (car entry) t))
4883                      (gnus-active (gnus-info-group info)))
4884             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4885       (error "No such group: %s" (gnus-info-group info)))))
4886
4887 (defun gnus-group-set-method-info (group select-method)
4888   (gnus-group-set-info select-method group 'method))
4889
4890 (defun gnus-group-set-params-info (group params)
4891   (gnus-group-set-info params group 'params))
4892
4893 (defun gnus-group-update-group-line ()
4894   "Update the current line in the group buffer."
4895   (let* ((buffer-read-only nil)
4896          (group (gnus-group-group-name))
4897          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4898          gnus-group-indentation)
4899     (and entry
4900          (not (gnus-ephemeral-group-p group))
4901          (gnus-dribble-enter
4902           (concat "(gnus-group-set-info '"
4903                   (prin1-to-string (nth 2 entry)) ")")))
4904     (setq gnus-group-indentation (gnus-group-group-indentation))
4905     (gnus-delete-line)
4906     (gnus-group-insert-group-line-info group)
4907     (forward-line -1)
4908     (gnus-group-position-point)))
4909
4910 (defun gnus-group-insert-group-line-info (group)
4911   "Insert GROUP on the current line."
4912   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4913         active info)
4914     (if entry
4915         (progn
4916           ;; (Un)subscribed group.
4917           (setq info (nth 2 entry))
4918           (gnus-group-insert-group-line
4919            group (gnus-info-level info) (gnus-info-marks info)
4920            (or (car entry) t) (gnus-info-method info)))
4921       ;; This group is dead.
4922       (gnus-group-insert-group-line
4923        group
4924        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4925        nil
4926        (if (setq active (gnus-active group))
4927            (- (1+ (cdr active)) (car active)) 0)
4928        nil))))
4929
4930 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4931                                                     gnus-tmp-marked number
4932                                                     gnus-tmp-method)
4933   "Insert a group line in the group buffer."
4934   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4935          (gnus-tmp-number-total
4936           (if gnus-tmp-active
4937               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4938             0))
4939          (gnus-tmp-number-of-unread
4940           (if (numberp number) (int-to-string (max 0 number))
4941             "*"))
4942          (gnus-tmp-number-of-read
4943           (if (numberp number)
4944               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4945             "*"))
4946          (gnus-tmp-subscribed
4947           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4948                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4949                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4950                 (t ?K)))
4951          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4952          (gnus-tmp-newsgroup-description
4953           (if gnus-description-hashtb
4954               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4955             ""))
4956          (gnus-tmp-moderated
4957           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4958          (gnus-tmp-moderated-string
4959           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4960          (gnus-tmp-method
4961           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4962          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4963          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4964          (gnus-tmp-news-method-string
4965           (if gnus-tmp-method
4966               (format "(%s:%s)" (car gnus-tmp-method)
4967                       (cadr gnus-tmp-method)) ""))
4968          (gnus-tmp-marked-mark
4969           (if (and (numberp number)
4970                    (zerop number)
4971                    (cdr (assq 'tick gnus-tmp-marked)))
4972               ?* ? ))
4973          (gnus-tmp-process-marked
4974           (if (member gnus-tmp-group gnus-group-marked)
4975               gnus-process-mark ? ))
4976          (gnus-tmp-grouplens
4977           (or (and gnus-use-grouplens
4978                    (bbb-grouplens-group-p gnus-tmp-group))
4979               ""))
4980          (buffer-read-only nil)
4981          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4982     (beginning-of-line)
4983     (gnus-add-text-properties
4984      (point)
4985      (prog1 (1+ (point))
4986        ;; Insert the text.
4987        (eval gnus-group-line-format-spec))
4988      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4989        gnus-unread ,(if (numberp number)
4990                         (string-to-int gnus-tmp-number-of-unread)
4991                       t)
4992        gnus-marked ,gnus-tmp-marked-mark
4993        gnus-indentation ,gnus-group-indentation
4994        gnus-level ,gnus-tmp-level))
4995     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4996       (forward-line -1)
4997       (run-hooks 'gnus-group-update-hook)
4998       (forward-line))
4999     ;; Allow XEmacs to remove front-sticky text properties.
5000     (gnus-group-remove-excess-properties)))
5001
5002 (defun gnus-group-update-group (group &optional visible-only)
5003   "Update all lines where GROUP appear.
5004 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5005 already."
5006   (save-excursion
5007     (set-buffer gnus-group-buffer)
5008     ;; The buffer may be narrowed.
5009     (save-restriction
5010       (widen)
5011       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5012             (loc (point-min))
5013             found buffer-read-only)
5014         ;; Enter the current status into the dribble buffer.
5015         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5016           (if (and entry (not (gnus-ephemeral-group-p group)))
5017               (gnus-dribble-enter
5018                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5019                        ")"))))
5020         ;; Find all group instances.  If topics are in use, each group
5021         ;; may be listed in more than once.
5022         (while (setq loc (text-property-any
5023                           loc (point-max) 'gnus-group ident))
5024           (setq found t)
5025           (goto-char loc)
5026           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5027             (gnus-delete-line)
5028             (gnus-group-insert-group-line-info group))
5029           (setq loc (1+ loc)))
5030         (unless (or found visible-only)
5031           ;; No such line in the buffer, find out where it's supposed to
5032           ;; go, and insert it there (or at the end of the buffer).
5033           (if gnus-goto-missing-group-function
5034               (funcall gnus-goto-missing-group-function group)
5035             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5036               (while (and entry (car entry)
5037                           (not
5038                            (gnus-goto-char
5039                             (text-property-any
5040                              (point-min) (point-max)
5041                              'gnus-group (gnus-intern-safe
5042                                           (caar entry) gnus-active-hashtb)))))
5043                 (setq entry (cdr entry)))
5044               (or entry (goto-char (point-max)))))
5045           ;; Finally insert the line.
5046           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5047             (gnus-group-insert-group-line-info group)))
5048         (gnus-group-set-mode-line)))))
5049
5050 (defun gnus-group-set-mode-line ()
5051   "Update the mode line in the group buffer."
5052   (when (memq 'group gnus-updated-mode-lines)
5053     ;; Yes, we want to keep this mode line updated.
5054     (save-excursion
5055       (set-buffer gnus-group-buffer)
5056       (let* ((gformat (or gnus-group-mode-line-format-spec
5057                           (setq gnus-group-mode-line-format-spec
5058                                 (gnus-parse-format
5059                                  gnus-group-mode-line-format
5060                                  gnus-group-mode-line-format-alist))))
5061              (gnus-tmp-news-server (cadr gnus-select-method))
5062              (gnus-tmp-news-method (car gnus-select-method))
5063              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5064              (max-len 60)
5065              gnus-tmp-header            ;Dummy binding for user-defined formats
5066              ;; Get the resulting string.
5067              (mode-string (eval gformat)))
5068         ;; Say whether the dribble buffer has been modified.
5069         (setq mode-line-modified
5070               (if (and gnus-dribble-buffer
5071                        (buffer-name gnus-dribble-buffer)
5072                        (buffer-modified-p gnus-dribble-buffer)
5073                        (save-excursion
5074                          (set-buffer gnus-dribble-buffer)
5075                          (not (zerop (buffer-size)))))
5076                   "---*- " "----- "))
5077         ;; If the line is too long, we chop it off.
5078         (when (> (length mode-string) max-len)
5079           (setq mode-string (substring mode-string 0 (- max-len 4))))
5080         (prog1
5081             (setq mode-line-buffer-identification 
5082                   (list mode-string))
5083           (set-buffer-modified-p t))))))
5084
5085 (defun gnus-group-group-name ()
5086   "Get the name of the newsgroup on the current line."
5087   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5088     (and group (symbol-name group))))
5089
5090 (defun gnus-group-group-level ()
5091   "Get the level of the newsgroup on the current line."
5092   (get-text-property (gnus-point-at-bol) 'gnus-level))
5093
5094 (defun gnus-group-group-indentation ()
5095   "Get the indentation of the newsgroup on the current line."
5096   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5097       (and gnus-group-indentation-function
5098            (funcall gnus-group-indentation-function))
5099       ""))
5100
5101 (defun gnus-group-group-unread ()
5102   "Get the number of unread articles of the newsgroup on the current line."
5103   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5104
5105 (defun gnus-group-search-forward (&optional backward all level first-too)
5106   "Find the next newsgroup with unread articles.
5107 If BACKWARD is non-nil, find the previous newsgroup instead.
5108 If ALL is non-nil, just find any newsgroup.
5109 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5110 group exists.
5111 If FIRST-TOO, the current line is also eligible as a target."
5112   (let ((way (if backward -1 1))
5113         (low gnus-level-killed)
5114         (beg (point))
5115         pos found lev)
5116     (if (and backward (progn (beginning-of-line)) (bobp))
5117         nil
5118       (or first-too (forward-line way))
5119       (while (and
5120               (not (eobp))
5121               (not (setq
5122                     found
5123                     (and (or all
5124                              (and
5125                               (let ((unread
5126                                      (get-text-property (point) 'gnus-unread)))
5127                                 (and (numberp unread) (> unread 0)))
5128                               (setq lev (get-text-property (point)
5129                                                            'gnus-level))
5130                               (<= lev gnus-level-subscribed)))
5131                          (or (not level)
5132                              (and (setq lev (get-text-property (point)
5133                                                                'gnus-level))
5134                                   (or (= lev level)
5135                                       (and (< lev low)
5136                                            (< level lev)
5137                                            (progn
5138                                              (setq low lev)
5139                                              (setq pos (point))
5140                                              nil))))))))
5141               (zerop (forward-line way)))))
5142     (if found
5143         (progn (gnus-group-position-point) t)
5144       (goto-char (or pos beg))
5145       (and pos t))))
5146
5147 ;;; Gnus group mode commands
5148
5149 ;; Group marking.
5150
5151 (defun gnus-group-mark-group (n &optional unmark no-advance)
5152   "Mark the current group."
5153   (interactive "p")
5154   (let ((buffer-read-only nil)
5155         group)
5156     (while (and (> n 0)
5157                 (not (eobp)))
5158       (when (setq group (gnus-group-group-name))
5159         ;; Update the mark.
5160         (beginning-of-line)
5161         (forward-char
5162          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5163         (delete-char 1)
5164         (if unmark
5165             (progn
5166               (insert " ")
5167               (setq gnus-group-marked (delete group gnus-group-marked)))
5168           (insert "#")
5169           (setq gnus-group-marked
5170                 (cons group (delete group gnus-group-marked)))))
5171       (or no-advance (gnus-group-next-group 1))
5172       (decf n))
5173     (gnus-summary-position-point)
5174     n))
5175
5176 (defun gnus-group-unmark-group (n)
5177   "Remove the mark from the current group."
5178   (interactive "p")
5179   (gnus-group-mark-group n 'unmark)
5180   (gnus-group-position-point))
5181
5182 (defun gnus-group-unmark-all-groups ()
5183   "Unmark all groups."
5184   (interactive)
5185   (let ((groups gnus-group-marked))
5186     (save-excursion
5187       (while groups
5188         (gnus-group-remove-mark (pop groups)))))
5189   (gnus-group-position-point))
5190
5191 (defun gnus-group-mark-region (unmark beg end)
5192   "Mark all groups between point and mark.
5193 If UNMARK, remove the mark instead."
5194   (interactive "P\nr")
5195   (let ((num (count-lines beg end)))
5196     (save-excursion
5197       (goto-char beg)
5198       (- num (gnus-group-mark-group num unmark)))))
5199
5200 (defun gnus-group-mark-buffer (&optional unmark)
5201   "Mark all groups in the buffer.
5202 If UNMARK, remove the mark instead."
5203   (interactive "P")
5204   (gnus-group-mark-region unmark (point-min) (point-max)))
5205
5206 (defun gnus-group-mark-regexp (regexp)
5207   "Mark all groups that match some regexp."
5208   (interactive "sMark (regexp): ")
5209   (let ((alist (cdr gnus-newsrc-alist))
5210         group)
5211     (while alist
5212       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5213         (gnus-group-set-mark group))))
5214   (gnus-group-position-point))
5215
5216 (defun gnus-group-remove-mark (group)
5217   "Remove the process mark from GROUP and move point there.
5218 Return nil if the group isn't displayed."
5219   (if (gnus-group-goto-group group)
5220       (save-excursion
5221         (gnus-group-mark-group 1 'unmark t)
5222         t)
5223     (setq gnus-group-marked
5224           (delete group gnus-group-marked))
5225     nil))
5226
5227 (defun gnus-group-set-mark (group)
5228   "Set the process mark on GROUP."
5229   (if (gnus-group-goto-group group) 
5230       (save-excursion
5231         (gnus-group-mark-group 1 nil t))
5232     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5233
5234 (defun gnus-group-universal-argument (arg &optional groups func)
5235   "Perform any command on all groups accoring to the process/prefix convention."
5236   (interactive "P")
5237   (let ((groups (or groups (gnus-group-process-prefix arg)))
5238         group func)
5239     (if (eq (setq func (or func
5240                            (key-binding
5241                             (read-key-sequence
5242                              (substitute-command-keys
5243                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5244             'undefined)
5245         (gnus-error 1 "Undefined key")
5246       (while groups
5247         (gnus-group-remove-mark (setq group (pop groups)))
5248         (command-execute func))))
5249   (gnus-group-position-point))
5250
5251 (defun gnus-group-process-prefix (n)
5252   "Return a list of groups to work on.
5253 Take into consideration N (the prefix) and the list of marked groups."
5254   (cond
5255    (n
5256     (setq n (prefix-numeric-value n))
5257     ;; There is a prefix, so we return a list of the N next
5258     ;; groups.
5259     (let ((way (if (< n 0) -1 1))
5260           (n (abs n))
5261           group groups)
5262       (save-excursion
5263         (while (and (> n 0)
5264                     (setq group (gnus-group-group-name)))
5265           (setq groups (cons group groups))
5266           (setq n (1- n))
5267           (gnus-group-next-group way)))
5268       (nreverse groups)))
5269    ((and (boundp 'transient-mark-mode)
5270          transient-mark-mode
5271          (boundp 'mark-active)
5272          mark-active)
5273     ;; Work on the region between point and mark.
5274     (let ((max (max (point) (mark)))
5275           groups)
5276       (save-excursion
5277         (goto-char (min (point) (mark)))
5278         (while
5279             (and
5280              (push (gnus-group-group-name) groups)
5281              (zerop (gnus-group-next-group 1))
5282              (< (point) max)))
5283         (nreverse groups))))
5284    (gnus-group-marked
5285     ;; No prefix, but a list of marked articles.
5286     (reverse gnus-group-marked))
5287    (t
5288     ;; Neither marked articles or a prefix, so we return the
5289     ;; current group.
5290     (let ((group (gnus-group-group-name)))
5291       (and group (list group))))))
5292
5293 ;; Selecting groups.
5294
5295 (defun gnus-group-read-group (&optional all no-article group)
5296   "Read news in this newsgroup.
5297 If the prefix argument ALL is non-nil, already read articles become
5298 readable.  IF ALL is a number, fetch this number of articles.  If the
5299 optional argument NO-ARTICLE is non-nil, no article will be
5300 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5301 group."
5302   (interactive "P")
5303   (let ((group (or group (gnus-group-group-name)))
5304         number active marked entry)
5305     (or group (error "No group on current line"))
5306     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5307                                             group gnus-newsrc-hashtb)))))
5308     ;; This group might be a dead group.  In that case we have to get
5309     ;; the number of unread articles from `gnus-active-hashtb'.
5310     (setq number
5311           (cond ((numberp all) all)
5312                 (entry (car entry))
5313                 ((setq active (gnus-active group))
5314                  (- (1+ (cdr active)) (car active)))))
5315     (gnus-summary-read-group
5316      group (or all (and (numberp number)
5317                         (zerop (+ number (length (cdr (assq 'tick marked)))
5318                                   (length (cdr (assq 'dormant marked)))))))
5319      no-article)))
5320
5321 (defun gnus-group-select-group (&optional all)
5322   "Select this newsgroup.
5323 No article is selected automatically.
5324 If ALL is non-nil, already read articles become readable.
5325 If ALL is a number, fetch this number of articles."
5326   (interactive "P")
5327   (gnus-group-read-group all t))
5328
5329 (defun gnus-group-quick-select-group (&optional all)
5330   "Select the current group \"quickly\".
5331 This means that no highlighting or scoring will be performed."
5332   (interactive "P")
5333   (let (gnus-visual
5334         gnus-score-find-score-files-function
5335         gnus-apply-kill-hook
5336         gnus-summary-expunge-below)
5337     (gnus-group-read-group all t)))
5338
5339 (defun gnus-group-visible-select-group (&optional all)
5340   "Select the current group without hiding any articles."
5341   (interactive "P")
5342   (let ((gnus-inhibit-limiting t))
5343     (gnus-group-read-group all t)))
5344
5345 ;;;###autoload
5346 (defun gnus-fetch-group (group)
5347   "Start Gnus if necessary and enter GROUP.
5348 Returns whether the fetching was successful or not."
5349   (interactive "sGroup name: ")
5350   (or (get-buffer gnus-group-buffer)
5351       (gnus))
5352   (gnus-group-read-group nil nil group))
5353
5354 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5355 ;; if selection was successful.
5356 (defun gnus-group-read-ephemeral-group
5357   (group method &optional activate quit-config)
5358   (let ((group (if (gnus-group-foreign-p group) group
5359                  (gnus-group-prefixed-name group method))))
5360     (gnus-sethash
5361      group
5362      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5363                      ((quit-config . ,(if quit-config quit-config
5364                                         (cons (current-buffer) 'summary))))))
5365      gnus-newsrc-hashtb)
5366     (set-buffer gnus-group-buffer)
5367     (or (gnus-check-server method)
5368         (error "Unable to contact server: %s" (gnus-status-message method)))
5369     (if activate (or (gnus-request-group group)
5370                      (error "Couldn't request group")))
5371     (condition-case ()
5372         (gnus-group-read-group t t group)
5373       (error nil)
5374       (quit nil))))
5375
5376 (defun gnus-group-jump-to-group (group)
5377   "Jump to newsgroup GROUP."
5378   (interactive
5379    (list (completing-read
5380           "Group: " gnus-active-hashtb nil
5381           (gnus-read-active-file-p)
5382           nil
5383           'gnus-group-history)))
5384
5385   (when (equal group "")
5386     (error "Empty group name"))
5387
5388   (when (string-match "[\000-\032]" group)
5389     (error "Control characters in group: %s" group))
5390
5391   (let ((b (text-property-any
5392             (point-min) (point-max)
5393             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5394     (unless (gnus-ephemeral-group-p group)
5395       (if b
5396           ;; Either go to the line in the group buffer...
5397           (goto-char b)
5398         ;; ... or insert the line.
5399         (or
5400          (gnus-active group)
5401          (gnus-activate-group group)
5402          (error "%s error: %s" group (gnus-status-message group)))
5403
5404         (gnus-group-update-group group)
5405         (goto-char (text-property-any
5406                     (point-min) (point-max)
5407                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5408     ;; Adjust cursor point.
5409     (gnus-group-position-point)))
5410
5411 (defun gnus-group-goto-group (group)
5412   "Goto to newsgroup GROUP."
5413   (when group
5414     (let ((b (text-property-any (point-min) (point-max)
5415                                 'gnus-group (gnus-intern-safe
5416                                              group gnus-active-hashtb))))
5417       (and b (goto-char b)))))
5418
5419 (defun gnus-group-next-group (n)
5420   "Go to next N'th newsgroup.
5421 If N is negative, search backward instead.
5422 Returns the difference between N and the number of skips actually
5423 done."
5424   (interactive "p")
5425   (gnus-group-next-unread-group n t))
5426
5427 (defun gnus-group-next-unread-group (n &optional all level)
5428   "Go to next N'th unread newsgroup.
5429 If N is negative, search backward instead.
5430 If ALL is non-nil, choose any newsgroup, unread or not.
5431 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5432 such group can be found, the next group with a level higher than
5433 LEVEL.
5434 Returns the difference between N and the number of skips actually
5435 made."
5436   (interactive "p")
5437   (let ((backward (< n 0))
5438         (n (abs n)))
5439     (while (and (> n 0)
5440                 (gnus-group-search-forward
5441                  backward (or (not gnus-group-goto-unread) all) level))
5442       (setq n (1- n)))
5443     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5444                                (if level " on this level or higher" "")))
5445     n))
5446
5447 (defun gnus-group-prev-group (n)
5448   "Go to previous N'th newsgroup.
5449 Returns the difference between N and the number of skips actually
5450 done."
5451   (interactive "p")
5452   (gnus-group-next-unread-group (- n) t))
5453
5454 (defun gnus-group-prev-unread-group (n)
5455   "Go to previous N'th unread newsgroup.
5456 Returns the difference between N and the number of skips actually
5457 done."
5458   (interactive "p")
5459   (gnus-group-next-unread-group (- n)))
5460
5461 (defun gnus-group-next-unread-group-same-level (n)
5462   "Go to next N'th unread newsgroup on the same level.
5463 If N is negative, search backward instead.
5464 Returns the difference between N and the number of skips actually
5465 done."
5466   (interactive "p")
5467   (gnus-group-next-unread-group n t (gnus-group-group-level))
5468   (gnus-group-position-point))
5469
5470 (defun gnus-group-prev-unread-group-same-level (n)
5471   "Go to next N'th unread newsgroup on the same level.
5472 Returns the difference between N and the number of skips actually
5473 done."
5474   (interactive "p")
5475   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5476   (gnus-group-position-point))
5477
5478 (defun gnus-group-best-unread-group (&optional exclude-group)
5479   "Go to the group with the highest level.
5480 If EXCLUDE-GROUP, do not go to that group."
5481   (interactive)
5482   (goto-char (point-min))
5483   (let ((best 100000)
5484         unread best-point)
5485     (while (not (eobp))
5486       (setq unread (get-text-property (point) 'gnus-unread))
5487       (if (and (numberp unread) (> unread 0))
5488           (progn
5489             (if (and (get-text-property (point) 'gnus-level)
5490                      (< (get-text-property (point) 'gnus-level) best)
5491                      (or (not exclude-group)
5492                          (not (equal exclude-group (gnus-group-group-name)))))
5493                 (progn
5494                   (setq best (get-text-property (point) 'gnus-level))
5495                   (setq best-point (point))))))
5496       (forward-line 1))
5497     (if best-point (goto-char best-point))
5498     (gnus-summary-position-point)
5499     (and best-point (gnus-group-group-name))))
5500
5501 (defun gnus-group-first-unread-group ()
5502   "Go to the first group with unread articles."
5503   (interactive)
5504   (prog1
5505       (let ((opoint (point))
5506             unread)
5507         (goto-char (point-min))
5508         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5509                 (and (numberp unread)   ; Not a topic.
5510                      (not (zerop unread))) ; Has unread articles.
5511                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5512             (point)                     ; Success.
5513           (goto-char opoint)
5514           nil))                         ; Not success.
5515     (gnus-group-position-point)))
5516
5517 (defun gnus-group-enter-server-mode ()
5518   "Jump to the server buffer."
5519   (interactive)
5520   (gnus-enter-server-buffer))
5521
5522 (defun gnus-group-make-group (name &optional method address)
5523   "Add a new newsgroup.
5524 The user will be prompted for a NAME, for a select METHOD, and an
5525 ADDRESS."
5526   (interactive
5527    (cons
5528     (read-string "Group name: ")
5529     (let ((method
5530            (completing-read
5531             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5532             nil t nil 'gnus-method-history)))
5533       (cond ((assoc method gnus-valid-select-methods)
5534              (list method
5535                    (if (memq 'prompt-address
5536                              (assoc method gnus-valid-select-methods))
5537                        (read-string "Address: ")
5538                      "")))
5539             ((assoc method gnus-server-alist)
5540              (list method))
5541             (t
5542              (list method ""))))))
5543
5544   (let* ((meth (and method (if address (list (intern method) address)
5545                              method)))
5546          (nname (if method (gnus-group-prefixed-name name meth) name))
5547          backend info)
5548     (when (gnus-gethash nname gnus-newsrc-hashtb)
5549       (error "Group %s already exists" nname))
5550     ;; Subscribe to the new group.
5551     (gnus-group-change-level
5552      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5553      gnus-level-default-subscribed gnus-level-killed
5554      (and (gnus-group-group-name)
5555           (gnus-gethash (gnus-group-group-name)
5556                         gnus-newsrc-hashtb))
5557      t)
5558     ;; Make it active.
5559     (gnus-set-active nname (cons 1 0))
5560     (or (gnus-ephemeral-group-p name)
5561         (gnus-dribble-enter
5562          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5563     ;; Insert the line.
5564     (gnus-group-insert-group-line-info nname)
5565     (forward-line -1)
5566     (gnus-group-position-point)
5567
5568     ;; Load the backend and try to make the backend create
5569     ;; the group as well.
5570     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5571                                                   nil meth))))
5572                  gnus-valid-select-methods)
5573       (require backend))
5574     (gnus-check-server meth)
5575     (and (gnus-check-backend-function 'request-create-group nname)
5576          (gnus-request-create-group nname))
5577     t))
5578
5579 (defun gnus-group-delete-group (group &optional force)
5580   "Delete the current group.
5581 If FORCE (the prefix) is non-nil, all the articles in the group will
5582 be deleted.  This is \"deleted\" as in \"removed forever from the face
5583 of the Earth\".  There is no undo."
5584   (interactive
5585    (list (gnus-group-group-name)
5586          current-prefix-arg))
5587   (or group (error "No group to rename"))
5588   (or (gnus-check-backend-function 'request-delete-group group)
5589       (error "This backend does not support group deletion"))
5590   (prog1
5591       (if (not (gnus-yes-or-no-p
5592                 (format
5593                  "Do you really want to delete %s%s? "
5594                  group (if force " and all its contents" ""))))
5595           () ; Whew!
5596         (gnus-message 6 "Deleting group %s..." group)
5597         (if (not (gnus-request-delete-group group force))
5598             (gnus-error 3 "Couldn't delete group %s" group)
5599           (gnus-message 6 "Deleting group %s...done" group)
5600           (gnus-group-goto-group group)
5601           (gnus-group-kill-group 1 t)
5602           (gnus-sethash group nil gnus-active-hashtb)
5603           t))
5604     (gnus-group-position-point)))
5605
5606 (defun gnus-group-rename-group (group new-name)
5607   (interactive
5608    (list
5609     (gnus-group-group-name)
5610     (progn
5611       (or (gnus-check-backend-function
5612            'request-rename-group (gnus-group-group-name))
5613           (error "This backend does not support renaming groups"))
5614       (read-string "New group name: "))))
5615
5616   (or (gnus-check-backend-function 'request-rename-group group)
5617       (error "This backend does not support renaming groups"))
5618
5619   (or group (error "No group to rename"))
5620   (and (string-match "^[ \t]*$" new-name)
5621        (error "Not a valid group name"))
5622
5623   ;; We find the proper prefixed name.
5624   (setq new-name
5625         (gnus-group-prefixed-name
5626          (gnus-group-real-name new-name)
5627          (gnus-info-method (gnus-get-info group))))
5628
5629   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5630   (prog1
5631       (if (not (gnus-request-rename-group group new-name))
5632           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5633         ;; We rename the group internally by killing it...
5634         (gnus-group-goto-group group)
5635         (gnus-group-kill-group)
5636         ;; ... changing its name ...
5637         (setcar (cdar gnus-list-of-killed-groups) new-name)
5638         ;; ... and then yanking it.  Magic!
5639         (gnus-group-yank-group)
5640         (gnus-set-active new-name (gnus-active group))
5641         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5642         new-name)
5643     (gnus-group-position-point)))
5644
5645 (defun gnus-group-edit-group (group &optional part)
5646   "Edit the group on the current line."
5647   (interactive (list (gnus-group-group-name)))
5648   (let* ((part (or part 'info))
5649          (done-func `(lambda ()
5650                        "Exit editing mode and update the information."
5651                        (interactive)
5652                        (gnus-group-edit-group-done ',part ,group)))
5653          (winconf (current-window-configuration))
5654          info)
5655     (or group (error "No group on current line"))
5656     (or (setq info (gnus-get-info group))
5657         (error "Killed group; can't be edited"))
5658     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5659     (gnus-configure-windows 'edit-group)
5660     (gnus-add-current-to-buffer-list)
5661     (emacs-lisp-mode)
5662     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5663     (use-local-map (copy-keymap emacs-lisp-mode-map))
5664     (local-set-key "\C-c\C-c" done-func)
5665     (make-local-variable 'gnus-prev-winconf)
5666     (setq gnus-prev-winconf winconf)
5667     (erase-buffer)
5668     (insert
5669      (cond
5670       ((eq part 'method)
5671        ";; Type `C-c C-c' after editing the select method.\n\n")
5672       ((eq part 'params)
5673        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5674       ((eq part 'info)
5675        ";; Type `C-c C-c' after editing the group info.\n\n")))
5676     (insert
5677      (pp-to-string
5678       (cond ((eq part 'method)
5679              (or (gnus-info-method info) "native"))
5680             ((eq part 'params)
5681              (gnus-info-params info))
5682             (t info)))
5683      "\n")))
5684
5685 (defun gnus-group-edit-group-method (group)
5686   "Edit the select method of GROUP."
5687   (interactive (list (gnus-group-group-name)))
5688   (gnus-group-edit-group group 'method))
5689
5690 (defun gnus-group-edit-group-parameters (group)
5691   "Edit the group parameters of GROUP."
5692   (interactive (list (gnus-group-group-name)))
5693   (gnus-group-edit-group group 'params))
5694
5695 (defun gnus-group-edit-group-done (part group)
5696   "Get info from buffer, update variables and jump to the group buffer."
5697   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5698   (goto-char (point-min))
5699   (let* ((form (read (current-buffer)))
5700          (winconf gnus-prev-winconf)
5701          (method (cond ((eq part 'info) (nth 4 form))
5702                        ((eq part 'method) form)
5703                        (t nil)))
5704          (info (cond ((eq part 'info) form)
5705                      ((eq part 'method) (gnus-get-info group))
5706                      (t nil)))
5707          (new-group (if info
5708                       (if (or (not method)
5709                               (gnus-server-equal
5710                                gnus-select-method method))
5711                           (gnus-group-real-name (car info))
5712                         (gnus-group-prefixed-name
5713                          (gnus-group-real-name (car info)) method))
5714                       nil)))
5715     (when (and new-group
5716                (not (equal new-group group)))
5717       (when (gnus-group-goto-group group)
5718         (gnus-group-kill-group 1))
5719       (gnus-activate-group new-group))
5720     ;; Set the info.
5721     (if (and info new-group)
5722         (progn
5723           (setq info (gnus-copy-sequence info))
5724           (setcar info new-group)
5725           (unless (gnus-server-equal method "native")
5726             (unless (nthcdr 3 info)
5727               (nconc info (list nil nil)))
5728             (unless (nthcdr 4 info)
5729               (nconc info (list nil)))
5730             (gnus-info-set-method info method))
5731           (gnus-group-set-info info))
5732       (gnus-group-set-info form (or new-group group) part))
5733     (kill-buffer (current-buffer))
5734     (and winconf (set-window-configuration winconf))
5735     (set-buffer gnus-group-buffer)
5736     (gnus-group-update-group (or new-group group))
5737     (gnus-group-position-point)))
5738
5739 (defun gnus-group-make-help-group ()
5740   "Create the Gnus documentation group."
5741   (interactive)
5742   (let ((path load-path)
5743         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5744         file dir)
5745     (and (gnus-gethash name gnus-newsrc-hashtb)
5746          (error "Documentation group already exists"))
5747     (while path
5748       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5749             file nil)
5750       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5751                 (file-exists-p
5752                  (setq file (concat (file-name-directory
5753                                      (directory-file-name dir))
5754                                     "etc/gnus-tut.txt"))))
5755         (setq path nil)))
5756     (if (not file)
5757         (gnus-message 1 "Couldn't find doc group")
5758       (gnus-group-make-group
5759        (gnus-group-real-name name)
5760        (list 'nndoc "gnus-help"
5761              (list 'nndoc-address file)
5762              (list 'nndoc-article-type 'mbox)))))
5763   (gnus-group-position-point))
5764
5765 (defun gnus-group-make-doc-group (file type)
5766   "Create a group that uses a single file as the source."
5767   (interactive
5768    (list (read-file-name "File name: ")
5769          (and current-prefix-arg 'ask)))
5770   (when (eq type 'ask)
5771     (let ((err "")
5772           char found)
5773       (while (not found)
5774         (message
5775          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5776          err)
5777         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5778                           ((= char ?b) 'babyl)
5779                           ((= char ?d) 'digest)
5780                           ((= char ?f) 'forward)
5781                           ((= char ?a) 'mmfd)
5782                           (t (setq err (format "%c unknown. " char))
5783                              nil))))
5784       (setq type found)))
5785   (let* ((file (expand-file-name file))
5786          (name (gnus-generate-new-group-name
5787                 (gnus-group-prefixed-name
5788                  (file-name-nondirectory file) '(nndoc "")))))
5789     (gnus-group-make-group
5790      (gnus-group-real-name name)
5791      (list 'nndoc (file-name-nondirectory file)
5792            (list 'nndoc-address file)
5793            (list 'nndoc-article-type (or type 'guess))))))
5794
5795 (defun gnus-group-make-archive-group (&optional all)
5796   "Create the (ding) Gnus archive group of the most recent articles.
5797 Given a prefix, create a full group."
5798   (interactive "P")
5799   (let ((group (gnus-group-prefixed-name
5800                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5801     (and (gnus-gethash group gnus-newsrc-hashtb)
5802          (error "Archive group already exists"))
5803     (gnus-group-make-group
5804      (gnus-group-real-name group)
5805      (list 'nndir (if all "hpc" "edu")
5806            (list 'nndir-directory
5807                  (if all gnus-group-archive-directory
5808                    gnus-group-recent-archive-directory))))))
5809
5810 (defun gnus-group-make-directory-group (dir)
5811   "Create an nndir group.
5812 The user will be prompted for a directory.  The contents of this
5813 directory will be used as a newsgroup.  The directory should contain
5814 mail messages or news articles in files that have numeric names."
5815   (interactive
5816    (list (read-file-name "Create group from directory: ")))
5817   (or (file-exists-p dir) (error "No such directory"))
5818   (or (file-directory-p dir) (error "Not a directory"))
5819   (let ((ext "")
5820         (i 0)
5821         group)
5822     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5823       (setq group
5824             (gnus-group-prefixed-name
5825              (concat (file-name-as-directory (directory-file-name dir))
5826                      ext)
5827              '(nndir "")))
5828       (setq ext (format "<%d>" (setq i (1+ i)))))
5829     (gnus-group-make-group
5830      (gnus-group-real-name group)
5831      (list 'nndir group (list 'nndir-directory dir)))))
5832
5833 (defun gnus-group-make-kiboze-group (group address scores)
5834   "Create an nnkiboze group.
5835 The user will be prompted for a name, a regexp to match groups, and
5836 score file entries for articles to include in the group."
5837   (interactive
5838    (list
5839     (read-string "nnkiboze group name: ")
5840     (read-string "Source groups (regexp): ")
5841     (let ((headers (mapcar (lambda (group) (list group))
5842                            '("subject" "from" "number" "date" "message-id"
5843                              "references" "chars" "lines" "xref"
5844                              "followup" "all" "body" "head")))
5845           scores header regexp regexps)
5846       (while (not (equal "" (setq header (completing-read
5847                                           "Match on header: " headers nil t))))
5848         (setq regexps nil)
5849         (while (not (equal "" (setq regexp (read-string
5850                                             (format "Match on %s (string): "
5851                                                     header)))))
5852           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5853         (setq scores (cons (cons header regexps) scores)))
5854       scores)))
5855   (gnus-group-make-group group "nnkiboze" address)
5856   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5857     (let (emacs-lisp-mode-hook)
5858       (pp scores (current-buffer)))))
5859
5860 (defun gnus-group-add-to-virtual (n vgroup)
5861   "Add the current group to a virtual group."
5862   (interactive
5863    (list current-prefix-arg
5864          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5865                           "nnvirtual:")))
5866   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5867       (error "%s is not an nnvirtual group" vgroup))
5868   (let* ((groups (gnus-group-process-prefix n))
5869          (method (gnus-info-method (gnus-get-info vgroup))))
5870     (setcar (cdr method)
5871             (concat
5872              (nth 1 method) "\\|"
5873              (mapconcat
5874               (lambda (s)
5875                 (gnus-group-remove-mark s)
5876                 (concat "\\(^" (regexp-quote s) "$\\)"))
5877               groups "\\|"))))
5878   (gnus-group-position-point))
5879
5880 (defun gnus-group-make-empty-virtual (group)
5881   "Create a new, fresh, empty virtual group."
5882   (interactive "sCreate new, empty virtual group: ")
5883   (let* ((method (list 'nnvirtual "^$"))
5884          (pgroup (gnus-group-prefixed-name group method)))
5885     ;; Check whether it exists already.
5886     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5887          (error "Group %s already exists." pgroup))
5888     ;; Subscribe the new group after the group on the current line.
5889     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5890     (gnus-group-update-group pgroup)
5891     (forward-line -1)
5892     (gnus-group-position-point)))
5893
5894 (defun gnus-group-enter-directory (dir)
5895   "Enter an ephemeral nneething group."
5896   (interactive "DDirectory to read: ")
5897   (let* ((method (list 'nneething dir))
5898          (leaf (gnus-group-prefixed-name
5899                 (file-name-nondirectory (directory-file-name dir))
5900                 method))
5901          (name (gnus-generate-new-group-name leaf)))
5902     (let ((nneething-read-only t))
5903       (or (gnus-group-read-ephemeral-group
5904            name method t
5905            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5906                                       'summary 'group)))
5907           (error "Couldn't enter %s" dir)))))
5908
5909 ;; Group sorting commands
5910 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5911
5912 (defun gnus-group-sort-groups (func &optional reverse)
5913   "Sort the group buffer according to FUNC.
5914 If REVERSE, reverse the sorting order."
5915   (interactive (list gnus-group-sort-function
5916                      current-prefix-arg))
5917   (let ((func (cond 
5918                ((not (listp func)) func)
5919                ((null func) func)
5920                ((= 1 (length func)) (car func))
5921                (t `(lambda (t1 t2)
5922                      ,(gnus-make-sort-function 
5923                        (reverse func)))))))
5924     ;; We peel off the dummy group from the alist.
5925     (when func
5926       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5927         (pop gnus-newsrc-alist))
5928       ;; Do the sorting.
5929       (setq gnus-newsrc-alist
5930             (sort gnus-newsrc-alist func))
5931       (when reverse
5932         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5933       ;; Regenerate the hash table.
5934       (gnus-make-hashtable-from-newsrc-alist)
5935       (gnus-group-list-groups))))
5936
5937 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5938   "Sort the group buffer alphabetically by group name.
5939 If REVERSE, sort in reverse order."
5940   (interactive "P")
5941   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5942
5943 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5944   "Sort the group buffer by number of unread articles.
5945 If REVERSE, sort in reverse order."
5946   (interactive "P")
5947   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5948
5949 (defun gnus-group-sort-groups-by-level (&optional reverse)
5950   "Sort the group buffer by group level.
5951 If REVERSE, sort in reverse order."
5952   (interactive "P")
5953   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5954
5955 (defun gnus-group-sort-groups-by-score (&optional reverse)
5956   "Sort the group buffer by group score.
5957 If REVERSE, sort in reverse order."
5958   (interactive "P")
5959   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5960
5961 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5962   "Sort the group buffer by group rank.
5963 If REVERSE, sort in reverse order."
5964   (interactive "P")
5965   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5966
5967 (defun gnus-group-sort-groups-by-method (&optional reverse)
5968   "Sort the group buffer alphabetically by backend name.
5969 If REVERSE, sort in reverse order."
5970   (interactive "P")
5971   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5972
5973 (defun gnus-group-sort-by-alphabet (info1 info2)
5974   "Sort alphabetically."
5975   (string< (gnus-info-group info1) (gnus-info-group info2)))
5976
5977 (defun gnus-group-sort-by-unread (info1 info2)
5978   "Sort by number of unread articles."
5979   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5980         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5981     (< (or (and (numberp n1) n1) 0)
5982        (or (and (numberp n2) n2) 0))))
5983
5984 (defun gnus-group-sort-by-level (info1 info2)
5985   "Sort by level."
5986   (< (gnus-info-level info1) (gnus-info-level info2)))
5987
5988 (defun gnus-group-sort-by-method (info1 info2)
5989   "Sort alphabetically by backend name."
5990   (string< (symbol-name (car (gnus-find-method-for-group
5991                               (gnus-info-group info1) info1)))
5992            (symbol-name (car (gnus-find-method-for-group
5993                               (gnus-info-group info2) info2)))))
5994
5995 (defun gnus-group-sort-by-score (info1 info2)
5996   "Sort by group score."
5997   (< (gnus-info-score info1) (gnus-info-score info2)))
5998
5999 (defun gnus-group-sort-by-rank (info1 info2)
6000   "Sort by level and score."
6001   (let ((level1 (gnus-info-level info1))
6002         (level2 (gnus-info-level info2)))
6003     (or (< level1 level2)
6004         (and (= level1 level2)
6005              (> (gnus-info-score info1) (gnus-info-score info2))))))
6006
6007 ;; Group catching up.
6008
6009 (defun gnus-group-clear-data (n)
6010   "Clear all marks and read ranges from the current group."
6011   (interactive "P")
6012   (let ((groups (gnus-group-process-prefix n))
6013         group info)
6014     (while (setq group (pop groups))
6015       (setq info (gnus-get-info group))
6016       (gnus-info-set-read info nil)
6017       (when (gnus-info-marks info)
6018         (gnus-info-set-marks info nil))
6019       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6020       (when (gnus-group-goto-group group)
6021         (gnus-group-remove-mark group)
6022         (gnus-group-update-group-line)))))
6023
6024 (defun gnus-group-catchup-current (&optional n all)
6025   "Mark all articles not marked as unread in current newsgroup as read.
6026 If prefix argument N is numeric, the ARG next newsgroups will be
6027 caught up.  If ALL is non-nil, marked articles will also be marked as
6028 read.  Cross references (Xref: header) of articles are ignored.
6029 The difference between N and actual number of newsgroups that were
6030 caught up is returned."
6031   (interactive "P")
6032   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6033                gnus-expert-user
6034                (gnus-y-or-n-p
6035                 (if all
6036                     "Do you really want to mark all articles as read? "
6037                   "Mark all unread articles as read? "))))
6038       n
6039     (let ((groups (gnus-group-process-prefix n))
6040           (ret 0))
6041       (while groups
6042         ;; Virtual groups have to be given special treatment.
6043         (let ((method (gnus-find-method-for-group (car groups))))
6044           (if (eq 'nnvirtual (car method))
6045               (nnvirtual-catchup-group
6046                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6047         (gnus-group-remove-mark (car groups))
6048         (if (>= (gnus-group-group-level) gnus-level-zombie)
6049             (gnus-message 2 "Dead groups can't be caught up")
6050           (if (prog1
6051                   (gnus-group-goto-group (car groups))
6052                 (gnus-group-catchup (car groups) all))
6053               (gnus-group-update-group-line)
6054             (setq ret (1+ ret))))
6055         (setq groups (cdr groups)))
6056       (gnus-group-next-unread-group 1)
6057       ret)))
6058
6059 (defun gnus-group-catchup-current-all (&optional n)
6060   "Mark all articles in current newsgroup as read.
6061 Cross references (Xref: header) of articles are ignored."
6062   (interactive "P")
6063   (gnus-group-catchup-current n 'all))
6064
6065 (defun gnus-group-catchup (group &optional all)
6066   "Mark all articles in GROUP as read.
6067 If ALL is non-nil, all articles are marked as read.
6068 The return value is the number of articles that were marked as read,
6069 or nil if no action could be taken."
6070   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6071          (num (car entry)))
6072     ;; Do the updating only if the newsgroup isn't killed.
6073     (if (not (numberp (car entry)))
6074         (gnus-message 1 "Can't catch up; non-active group")
6075       ;; Do auto-expirable marks if that's required.
6076       (when (gnus-group-auto-expirable-p group)
6077         (gnus-add-marked-articles
6078          group 'expire (gnus-list-of-unread-articles group))
6079         (when all
6080           (let ((marks (nth 3 (nth 2 entry))))
6081             (gnus-add-marked-articles
6082              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6083             (gnus-add-marked-articles
6084              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6085       (when entry
6086         (gnus-update-read-articles group nil)
6087         ;; Also nix out the lists of marks and dormants.
6088         (when all
6089           (gnus-add-marked-articles group 'tick nil nil 'force)
6090           (gnus-add-marked-articles group 'dormant nil nil 'force))
6091         (run-hooks 'gnus-group-catchup-group-hook)
6092         num))))
6093
6094 (defun gnus-group-expire-articles (&optional n)
6095   "Expire all expirable articles in the current newsgroup."
6096   (interactive "P")
6097   (let ((groups (gnus-group-process-prefix n))
6098         group)
6099     (unless groups
6100       (error "No groups to expire"))
6101     (while (setq group (pop groups))
6102       (gnus-group-remove-mark group)
6103       (when (gnus-check-backend-function 'request-expire-articles group)
6104         (gnus-message 6 "Expiring articles in %s..." group)
6105         (let* ((info (gnus-get-info group))
6106                (expirable (if (gnus-group-total-expirable-p group)
6107                               (cons nil (gnus-list-of-read-articles group))
6108                             (assq 'expire (gnus-info-marks info))))
6109                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6110           (when expirable
6111             (setcdr
6112              expirable
6113              (gnus-compress-sequence
6114               (if expiry-wait
6115                   ;; We set the expiry variables to the groupp
6116                   ;; parameter. 
6117                   (let ((nnmail-expiry-wait-function nil)
6118                         (nnmail-expiry-wait expiry-wait))
6119                     (gnus-request-expire-articles
6120                      (gnus-uncompress-sequence (cdr expirable)) group))
6121                 ;; Just expire using the normal expiry values.
6122                 (gnus-request-expire-articles
6123                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6124           (gnus-message 6 "Expiring articles in %s...done" group)))
6125       (gnus-group-position-point))))
6126
6127 (defun gnus-group-expire-all-groups ()
6128   "Expire all expirable articles in all newsgroups."
6129   (interactive)
6130   (save-excursion
6131     (gnus-message 5 "Expiring...")
6132     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6133                                      (cdr gnus-newsrc-alist))))
6134       (gnus-group-expire-articles nil)))
6135   (gnus-group-position-point)
6136   (gnus-message 5 "Expiring...done"))
6137
6138 (defun gnus-group-set-current-level (n level)
6139   "Set the level of the next N groups to LEVEL."
6140   (interactive
6141    (list
6142     current-prefix-arg
6143     (string-to-int
6144      (let ((s (read-string
6145                (format "Level (default %s): "
6146                        (or (gnus-group-group-level) 
6147                            gnus-level-default-subscribed)))))
6148        (if (string-match "^\\s-*$" s)
6149            (int-to-string (or (gnus-group-group-level) 
6150                               gnus-level-default-subscribed))
6151          s)))))
6152   (or (and (>= level 1) (<= level gnus-level-killed))
6153       (error "Illegal level: %d" level))
6154   (let ((groups (gnus-group-process-prefix n))
6155         group)
6156     (while (setq group (pop groups))
6157       (gnus-group-remove-mark group)
6158       (gnus-message 6 "Changed level of %s from %d to %d"
6159                     group (or (gnus-group-group-level) gnus-level-killed)
6160                     level)
6161       (gnus-group-change-level
6162        group level (or (gnus-group-group-level) gnus-level-killed))
6163       (gnus-group-update-group-line)))
6164   (gnus-group-position-point))
6165
6166 (defun gnus-group-unsubscribe-current-group (&optional n)
6167   "Toggle subscription of the current group.
6168 If given numerical prefix, toggle the N next groups."
6169   (interactive "P")
6170   (let ((groups (gnus-group-process-prefix n))
6171         group)
6172     (while groups
6173       (setq group (car groups)
6174             groups (cdr groups))
6175       (gnus-group-remove-mark group)
6176       (gnus-group-unsubscribe-group
6177        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6178                  gnus-level-default-unsubscribed
6179                gnus-level-default-subscribed) t)
6180       (gnus-group-update-group-line))
6181     (gnus-group-next-group 1)))
6182
6183 (defun gnus-group-unsubscribe-group (group &optional level silent)
6184   "Toggle subscription to GROUP.
6185 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6186 group line."
6187   (interactive
6188    (list (completing-read
6189           "Group: " gnus-active-hashtb nil
6190           (gnus-read-active-file-p)
6191           nil 
6192           'gnus-group-history)))
6193   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6194     (cond
6195      ((string-match "^[ \t]$" group)
6196       (error "Empty group name"))
6197      (newsrc
6198       ;; Toggle subscription flag.
6199       (gnus-group-change-level
6200        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6201                                       gnus-level-subscribed)
6202                                   (1+ gnus-level-subscribed)
6203                                 gnus-level-default-subscribed)))
6204       (unless silent
6205         (gnus-group-update-group group)))
6206      ((and (stringp group)
6207            (or (not (gnus-read-active-file-p))
6208                (gnus-active group)))
6209       ;; Add new newsgroup.
6210       (gnus-group-change-level
6211        group
6212        (if level level gnus-level-default-subscribed)
6213        (or (and (member group gnus-zombie-list)
6214                 gnus-level-zombie)
6215            gnus-level-killed)
6216        (and (gnus-group-group-name)
6217             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6218       (unless silent
6219         (gnus-group-update-group group)))
6220      (t (error "No such newsgroup: %s" group)))
6221     (gnus-group-position-point)))
6222
6223 (defun gnus-group-transpose-groups (n)
6224   "Move the current newsgroup up N places.
6225 If given a negative prefix, move down instead.  The difference between
6226 N and the number of steps taken is returned."
6227   (interactive "p")
6228   (or (gnus-group-group-name)
6229       (error "No group on current line"))
6230   (gnus-group-kill-group 1)
6231   (prog1
6232       (forward-line (- n))
6233     (gnus-group-yank-group)
6234     (gnus-group-position-point)))
6235
6236 (defun gnus-group-kill-all-zombies ()
6237   "Kill all zombie newsgroups."
6238   (interactive)
6239   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6240   (setq gnus-zombie-list nil)
6241   (gnus-group-list-groups))
6242
6243 (defun gnus-group-kill-region (begin end)
6244   "Kill newsgroups in current region (excluding current point).
6245 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6246   (interactive "r")
6247   (let ((lines
6248          ;; Count lines.
6249          (save-excursion
6250            (count-lines
6251             (progn
6252               (goto-char begin)
6253               (beginning-of-line)
6254               (point))
6255             (progn
6256               (goto-char end)
6257               (beginning-of-line)
6258               (point))))))
6259     (goto-char begin)
6260     (beginning-of-line)                 ;Important when LINES < 1
6261     (gnus-group-kill-group lines)))
6262
6263 (defun gnus-group-kill-group (&optional n discard)
6264   "Kill the next N groups.
6265 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6266 However, only groups that were alive can be yanked; already killed
6267 groups or zombie groups can't be yanked.
6268 The return value is the name of the group that was killed, or a list
6269 of groups killed."
6270   (interactive "P")
6271   (let ((buffer-read-only nil)
6272         (groups (gnus-group-process-prefix n))
6273         group entry level out)
6274     (if (< (length groups) 10)
6275         ;; This is faster when there are few groups.
6276         (while groups
6277           (push (setq group (pop groups)) out)
6278           (gnus-group-remove-mark group)
6279           (setq level (gnus-group-group-level))
6280           (gnus-delete-line)
6281           (when (and (not discard)
6282                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6283             (push (cons (car entry) (nth 2 entry))
6284                   gnus-list-of-killed-groups))
6285           (gnus-group-change-level
6286            (if entry entry group) gnus-level-killed (if entry nil level)))
6287       ;; If there are lots and lots of groups to be killed, we use
6288       ;; this thing instead.
6289       (let (entry)
6290         (setq groups (nreverse groups))
6291         (while groups
6292           (gnus-group-remove-mark (setq group (pop groups)))
6293           (gnus-delete-line)
6294           (cond
6295            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6296             (push (cons (car entry) (nth 2 entry))
6297                   gnus-list-of-killed-groups)
6298             (setcdr (cdr entry) (cdddr entry)))
6299            ((member group gnus-zombie-list)
6300             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6301         (gnus-make-hashtable-from-newsrc-alist)))
6302
6303     (gnus-group-position-point)
6304     (if (< (length out) 2) (car out) (nreverse out))))
6305
6306 (defun gnus-group-yank-group (&optional arg)
6307   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6308 inserting it before the current newsgroup.  The numeric ARG specifies
6309 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6310 is returned, or (if several groups are yanked) a list of yanked groups
6311 is returned."
6312   (interactive "p")
6313   (setq arg (or arg 1))
6314   (let (info group prev out)
6315     (while (>= (decf arg) 0)
6316       (if (not (setq info (pop gnus-list-of-killed-groups)))
6317           (error "No more newsgroups to yank"))
6318       (push (setq group (nth 1 info)) out)
6319       ;; Find which newsgroup to insert this one before - search
6320       ;; backward until something suitable is found.  If there are no
6321       ;; other newsgroups in this buffer, just make this newsgroup the
6322       ;; first newsgroup.
6323       (setq prev (gnus-group-group-name))
6324       (gnus-group-change-level
6325        info (gnus-info-level (cdr info)) gnus-level-killed
6326        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6327        t)
6328       (gnus-group-insert-group-line-info group))
6329     (forward-line -1)
6330     (gnus-group-position-point)
6331     (if (< (length out) 2) (car out) (nreverse out))))
6332
6333 (defun gnus-group-kill-level (level)
6334   "Kill all groups that is on a certain LEVEL."
6335   (interactive "nKill all groups on level: ")
6336   (cond
6337    ((= level gnus-level-zombie)
6338     (setq gnus-killed-list
6339           (nconc gnus-zombie-list gnus-killed-list))
6340     (setq gnus-zombie-list nil))
6341    ((and (< level gnus-level-zombie)
6342          (> level 0)
6343          (or gnus-expert-user
6344              (gnus-yes-or-no-p
6345               (format
6346                "Do you really want to kill all groups on level %d? "
6347                level))))
6348     (let* ((prev gnus-newsrc-alist)
6349            (alist (cdr prev)))
6350       (while alist
6351         (if (= (gnus-info-level level) level)
6352             (setcdr prev (cdr alist))
6353           (setq prev alist))
6354         (setq alist (cdr alist)))
6355       (gnus-make-hashtable-from-newsrc-alist)
6356       (gnus-group-list-groups)))
6357    (t
6358     (error "Can't kill; illegal level: %d" level))))
6359
6360 (defun gnus-group-list-all-groups (&optional arg)
6361   "List all newsgroups with level ARG or lower.
6362 Default is gnus-level-unsubscribed, which lists all subscribed and most
6363 unsubscribed groups."
6364   (interactive "P")
6365   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6366
6367 ;; Redefine this to list ALL killed groups if prefix arg used.
6368 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6369 (defun gnus-group-list-killed (&optional arg)
6370   "List all killed newsgroups in the group buffer.
6371 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6372 entail asking the server for the groups."
6373   (interactive "P")
6374   ;; Find all possible killed newsgroups if arg.
6375   (when arg
6376     (gnus-get-killed-groups))
6377   (if (not gnus-killed-list)
6378       (gnus-message 6 "No killed groups")
6379     (let (gnus-group-list-mode)
6380       (funcall gnus-group-prepare-function
6381                gnus-level-killed t gnus-level-killed))
6382     (goto-char (point-min)))
6383   (gnus-group-position-point))
6384
6385 (defun gnus-group-list-zombies ()
6386   "List all zombie newsgroups in the group buffer."
6387   (interactive)
6388   (if (not gnus-zombie-list)
6389       (gnus-message 6 "No zombie groups")
6390     (let (gnus-group-list-mode)
6391       (funcall gnus-group-prepare-function
6392                gnus-level-zombie t gnus-level-zombie))
6393     (goto-char (point-min)))
6394   (gnus-group-position-point))
6395
6396 (defun gnus-group-list-active ()
6397   "List all groups that are available from the server(s)."
6398   (interactive)
6399   ;; First we make sure that we have really read the active file.
6400   (unless (gnus-read-active-file-p)
6401     (let ((gnus-read-active-file t))
6402       (gnus-read-active-file)))
6403   ;; Find all groups and sort them.
6404   (let ((groups
6405          (sort
6406           (let (list)
6407             (mapatoms
6408              (lambda (sym)
6409                (and (boundp sym)
6410                     (symbol-value sym)
6411                     (setq list (cons (symbol-name sym) list))))
6412              gnus-active-hashtb)
6413             list)
6414           'string<))
6415         (buffer-read-only nil))
6416     (erase-buffer)
6417     (while groups
6418       (gnus-group-insert-group-line-info (pop groups)))
6419     (goto-char (point-min))))
6420
6421 (defun gnus-activate-all-groups (level)
6422   "Activate absolutely all groups."
6423   (interactive (list 7))
6424   (let ((gnus-activate-level level)
6425         (gnus-activate-foreign-newsgroups level))
6426     (gnus-group-get-new-news)))
6427
6428 (defun gnus-group-get-new-news (&optional arg)
6429   "Get newly arrived articles.
6430 If ARG is a number, it specifies which levels you are interested in
6431 re-scanning.  If ARG is non-nil and not a number, this will force
6432 \"hard\" re-reading of the active files from all servers."
6433   (interactive "P")
6434   (run-hooks 'gnus-get-new-news-hook)
6435   ;; We might read in new NoCeM messages here.
6436   (when (and gnus-use-nocem 
6437              (null arg))
6438     (gnus-nocem-scan-groups))
6439   ;; If ARG is not a number, then we read the active file.
6440   (when (and arg (not (numberp arg)))
6441     (let ((gnus-read-active-file t))
6442       (gnus-read-active-file))
6443     (setq arg nil))
6444
6445   (setq arg (gnus-group-default-level arg t))
6446   (if (and gnus-read-active-file (not arg))
6447       (progn
6448         (gnus-read-active-file)
6449         (gnus-get-unread-articles arg))
6450     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6451       (gnus-get-unread-articles arg)))
6452   (run-hooks 'gnus-after-getting-new-news-hook)
6453   (gnus-group-list-groups))
6454
6455 (defun gnus-group-get-new-news-this-group (&optional n)
6456   "Check for newly arrived news in the current group (and the N-1 next groups).
6457 The difference between N and the number of newsgroup checked is returned.
6458 If N is negative, this group and the N-1 previous groups will be checked."
6459   (interactive "P")
6460   (let* ((groups (gnus-group-process-prefix n))
6461          (ret (if (numberp n) (- n (length groups)) 0))
6462          (beg (unless n (point)))
6463          group)
6464     (while (setq group (pop groups))
6465       (gnus-group-remove-mark group)
6466       (if (gnus-activate-group group 'scan)
6467           (progn
6468             (gnus-get-unread-articles-in-group
6469              (gnus-get-info group) (gnus-active group) t)
6470             (unless (gnus-virtual-group-p group)
6471               (gnus-close-group group))
6472             (gnus-group-update-group group))
6473         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6474     (when beg (goto-char beg))
6475     (when gnus-goto-next-group-when-activating
6476       (gnus-group-next-unread-group 1 t))
6477     (gnus-summary-position-point)
6478     ret))
6479
6480 (defun gnus-group-fetch-faq (group &optional faq-dir)
6481   "Fetch the FAQ for the current group."
6482   (interactive
6483    (list
6484     (gnus-group-real-name (gnus-group-group-name))
6485     (cond (current-prefix-arg
6486            (completing-read
6487             "Faq dir: " (and (listp gnus-group-faq-directory)
6488                              gnus-group-faq-directory))))))
6489   (or faq-dir
6490       (setq faq-dir (if (listp gnus-group-faq-directory)
6491                         (car gnus-group-faq-directory)
6492                       gnus-group-faq-directory)))
6493   (or group (error "No group name given"))
6494   (let ((file (concat (file-name-as-directory faq-dir)
6495                       (gnus-group-real-name group))))
6496     (if (not (file-exists-p file))
6497         (error "No such file: %s" file)
6498       (find-file file))))
6499
6500 (defun gnus-group-describe-group (force &optional group)
6501   "Display a description of the current newsgroup."
6502   (interactive (list current-prefix-arg (gnus-group-group-name)))
6503   (and force (setq gnus-description-hashtb nil))
6504   (let ((method (gnus-find-method-for-group group))
6505         desc)
6506     (or group (error "No group name given"))
6507     (and (or (and gnus-description-hashtb
6508                   ;; We check whether this group's method has been
6509                   ;; queried for a description file.
6510                   (gnus-gethash
6511                    (gnus-group-prefixed-name "" method)
6512                    gnus-description-hashtb))
6513              (setq desc (gnus-group-get-description group))
6514              (gnus-read-descriptions-file method))
6515          (gnus-message 1
6516           (or desc (gnus-gethash group gnus-description-hashtb)
6517               "No description available")))))
6518
6519 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6520 (defun gnus-group-describe-all-groups (&optional force)
6521   "Pop up a buffer with descriptions of all newsgroups."
6522   (interactive "P")
6523   (and force (setq gnus-description-hashtb nil))
6524   (if (not (or gnus-description-hashtb
6525                (gnus-read-all-descriptions-files)))
6526       (error "Couldn't request descriptions file"))
6527   (let ((buffer-read-only nil)
6528         b)
6529     (erase-buffer)
6530     (mapatoms
6531      (lambda (group)
6532        (setq b (point))
6533        (insert (format "      *: %-20s %s\n" (symbol-name group)
6534                        (symbol-value group)))
6535        (gnus-add-text-properties
6536         b (1+ b) (list 'gnus-group group
6537                        'gnus-unread t 'gnus-marked nil
6538                        'gnus-level (1+ gnus-level-subscribed))))
6539      gnus-description-hashtb)
6540     (goto-char (point-min))
6541     (gnus-group-position-point)))
6542
6543 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6544 (defun gnus-group-apropos (regexp &optional search-description)
6545   "List all newsgroups that have names that match a regexp."
6546   (interactive "sGnus apropos (regexp): ")
6547   (let ((prev "")
6548         (obuf (current-buffer))
6549         groups des)
6550     ;; Go through all newsgroups that are known to Gnus.
6551     (mapatoms
6552      (lambda (group)
6553        (and (symbol-name group)
6554             (string-match regexp (symbol-name group))
6555             (setq groups (cons (symbol-name group) groups))))
6556      gnus-active-hashtb)
6557     ;; Also go through all descriptions that are known to Gnus.
6558     (when search-description
6559       (mapatoms
6560        (lambda (group)
6561          (and (string-match regexp (symbol-value group))
6562               (gnus-active (symbol-name group))
6563               (setq groups (cons (symbol-name group) groups))))
6564        gnus-description-hashtb))
6565     (if (not groups)
6566         (gnus-message 3 "No groups matched \"%s\"." regexp)
6567       ;; Print out all the groups.
6568       (save-excursion
6569         (pop-to-buffer "*Gnus Help*")
6570         (buffer-disable-undo (current-buffer))
6571         (erase-buffer)
6572         (setq groups (sort groups 'string<))
6573         (while groups
6574           ;; Groups may be entered twice into the list of groups.
6575           (if (not (string= (car groups) prev))
6576               (progn
6577                 (insert (setq prev (car groups)) "\n")
6578                 (if (and gnus-description-hashtb
6579                          (setq des (gnus-gethash (car groups)
6580                                                  gnus-description-hashtb)))
6581                     (insert "  " des "\n"))))
6582           (setq groups (cdr groups)))
6583         (goto-char (point-min))))
6584     (pop-to-buffer obuf)))
6585
6586 (defun gnus-group-description-apropos (regexp)
6587   "List all newsgroups that have names or descriptions that match a regexp."
6588   (interactive "sGnus description apropos (regexp): ")
6589   (if (not (or gnus-description-hashtb
6590                (gnus-read-all-descriptions-files)))
6591       (error "Couldn't request descriptions file"))
6592   (gnus-group-apropos regexp t))
6593
6594 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6595 (defun gnus-group-list-matching (level regexp &optional all lowest)
6596   "List all groups with unread articles that match REGEXP.
6597 If the prefix LEVEL is non-nil, it should be a number that says which
6598 level to cut off listing groups.
6599 If ALL, also list groups with no unread articles.
6600 If LOWEST, don't list groups with level lower than LOWEST.
6601
6602 This command may read the active file."
6603   (interactive "P\nsList newsgroups matching: ")
6604   ;; First make sure active file has been read.
6605   (when (and level
6606              (>= (prefix-numeric-value level) gnus-level-killed))
6607     (gnus-get-killed-groups))
6608   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6609                            all (or lowest 1) regexp)
6610   (goto-char (point-min))
6611   (gnus-group-position-point))
6612
6613 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6614   "List all groups that match REGEXP.
6615 If the prefix LEVEL is non-nil, it should be a number that says which
6616 level to cut off listing groups.
6617 If LOWEST, don't list groups with level lower than LOWEST."
6618   (interactive "P\nsList newsgroups matching: ")
6619   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6620
6621 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6622 (defun gnus-group-save-newsrc (&optional force)
6623   "Save the Gnus startup files.
6624 If FORCE, force saving whether it is necessary or not."
6625   (interactive "P")
6626   (gnus-save-newsrc-file force))
6627
6628 (defun gnus-group-restart (&optional arg)
6629   "Force Gnus to read the .newsrc file."
6630   (interactive "P")
6631   (when (gnus-yes-or-no-p
6632          (format "Are you sure you want to read %s? "
6633                  gnus-current-startup-file))
6634     (gnus-save-newsrc-file)
6635     (gnus-setup-news 'force)
6636     (gnus-group-list-groups arg)))
6637
6638 (defun gnus-group-read-init-file ()
6639   "Read the Gnus elisp init file."
6640   (interactive)
6641   (gnus-read-init-file))
6642
6643 (defun gnus-group-check-bogus-groups (&optional silent)
6644   "Check bogus newsgroups.
6645 If given a prefix, don't ask for confirmation before removing a bogus
6646 group."
6647   (interactive "P")
6648   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6649   (gnus-group-list-groups))
6650
6651 (defun gnus-group-edit-global-kill (&optional article group)
6652   "Edit the global kill file.
6653 If GROUP, edit that local kill file instead."
6654   (interactive "P")
6655   (setq gnus-current-kill-article article)
6656   (gnus-kill-file-edit-file group)
6657   (gnus-message
6658    6
6659    (substitute-command-keys
6660     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6661             (if group "local" "global")))))
6662
6663 (defun gnus-group-edit-local-kill (article group)
6664   "Edit a local kill file."
6665   (interactive (list nil (gnus-group-group-name)))
6666   (gnus-group-edit-global-kill article group))
6667
6668 (defun gnus-group-force-update ()
6669   "Update `.newsrc' file."
6670   (interactive)
6671   (gnus-save-newsrc-file))
6672
6673 (defun gnus-group-suspend ()
6674   "Suspend the current Gnus session.
6675 In fact, cleanup buffers except for group mode buffer.
6676 The hook gnus-suspend-gnus-hook is called before actually suspending."
6677   (interactive)
6678   (run-hooks 'gnus-suspend-gnus-hook)
6679   ;; Kill Gnus buffers except for group mode buffer.
6680   (let ((group-buf (get-buffer gnus-group-buffer)))
6681     ;; Do this on a separate list in case the user does a ^G before we finish
6682     (let ((gnus-buffer-list
6683            (delq group-buf (delq gnus-dribble-buffer
6684                                  (append gnus-buffer-list nil)))))
6685       (while gnus-buffer-list
6686         (gnus-kill-buffer (car gnus-buffer-list))
6687         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6688     (if group-buf
6689         (progn
6690           (setq gnus-buffer-list (list group-buf))
6691           (bury-buffer group-buf)
6692           (delete-windows-on group-buf t)))))
6693
6694 (defun gnus-group-clear-dribble ()
6695   "Clear all information from the dribble buffer."
6696   (interactive)
6697   (gnus-dribble-clear)
6698   (gnus-message 7 "Cleared dribble buffer"))
6699
6700 (defun gnus-group-exit ()
6701   "Quit reading news after updating .newsrc.eld and .newsrc.
6702 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6703   (interactive)
6704   (when 
6705       (or noninteractive                ;For gnus-batch-kill
6706           (not gnus-interactive-exit)   ;Without confirmation
6707           gnus-expert-user
6708           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6709     (run-hooks 'gnus-exit-gnus-hook)
6710     ;; Offer to save data from non-quitted summary buffers.
6711     (gnus-offer-save-summaries)
6712     ;; Save the newsrc file(s).
6713     (gnus-save-newsrc-file)
6714     ;; Kill-em-all.
6715     (gnus-close-backends)
6716     ;; Reset everything.
6717     (gnus-clear-system)
6718     ;; Allow the user to do things after cleaning up.
6719     (run-hooks 'gnus-after-exiting-gnus-hook)))
6720
6721 (defun gnus-close-backends ()
6722   ;; Send a close request to all backends that support such a request.
6723   (let ((methods gnus-valid-select-methods)
6724         func)
6725     (while methods
6726       (if (fboundp (setq func (intern (concat (caar methods)
6727                                               "-request-close"))))
6728           (funcall func))
6729       (setq methods (cdr methods)))))
6730
6731 (defun gnus-group-quit ()
6732   "Quit reading news without updating .newsrc.eld or .newsrc.
6733 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6734   (interactive)
6735   (when (or noninteractive              ;For gnus-batch-kill
6736             (zerop (buffer-size))
6737             (not (gnus-server-opened gnus-select-method))
6738             gnus-expert-user
6739             (not gnus-current-startup-file)
6740             (gnus-yes-or-no-p
6741              (format "Quit reading news without saving %s? "
6742                      (file-name-nondirectory gnus-current-startup-file))))
6743     (run-hooks 'gnus-exit-gnus-hook)
6744     (if gnus-use-full-window
6745         (delete-other-windows)
6746       (gnus-remove-some-windows))
6747     (gnus-dribble-save)
6748     (gnus-close-backends)
6749     (gnus-clear-system)
6750     ;; Allow the user to do things after cleaning up.
6751     (run-hooks 'gnus-after-exiting-gnus-hook)))
6752
6753 (defun gnus-offer-save-summaries ()
6754   "Offer to save all active summary buffers."
6755   (save-excursion
6756     (let ((buflist (buffer-list))
6757           buffers bufname)
6758       ;; Go through all buffers and find all summaries.
6759       (while buflist
6760         (and (setq bufname (buffer-name (car buflist)))
6761              (string-match "Summary" bufname)
6762              (save-excursion
6763                (set-buffer bufname)
6764                ;; We check that this is, indeed, a summary buffer.
6765                (and (eq major-mode 'gnus-summary-mode)
6766                     ;; Also make sure this isn't bogus.
6767                     gnus-newsgroup-prepared))
6768              (push bufname buffers))
6769         (setq buflist (cdr buflist)))
6770       ;; Go through all these summary buffers and offer to save them.
6771       (when buffers
6772         (map-y-or-n-p
6773          "Update summary buffer %s? "
6774          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6775          buffers)))))
6776
6777 (defun gnus-group-describe-briefly ()
6778   "Give a one line description of the group mode commands."
6779   (interactive)
6780   (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")))
6781
6782 (defun gnus-group-browse-foreign-server (method)
6783   "Browse a foreign news server.
6784 If called interactively, this function will ask for a select method
6785  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6786 If not, METHOD should be a list where the first element is the method
6787 and the second element is the address."
6788   (interactive
6789    (list (let ((how (completing-read
6790                      "Which backend: "
6791                      (append gnus-valid-select-methods gnus-server-alist)
6792                      nil t (cons "nntp" 0) 'gnus-method-history)))
6793            ;; We either got a backend name or a virtual server name.
6794            ;; If the first, we also need an address.
6795            (if (assoc how gnus-valid-select-methods)
6796                (list (intern how)
6797                      ;; Suggested by mapjph@bath.ac.uk.
6798                      (completing-read
6799                       "Address: "
6800                       (mapcar (lambda (server) (list server))
6801                               gnus-secondary-servers)))
6802              ;; We got a server name, so we find the method.
6803              (gnus-server-to-method how)))))
6804   (gnus-browse-foreign-server method))
6805
6806 \f
6807 ;;;
6808 ;;; Gnus summary mode
6809 ;;;
6810
6811 (defvar gnus-summary-mode-map nil)
6812
6813 (put 'gnus-summary-mode 'mode-class 'special)
6814
6815 (unless gnus-summary-mode-map
6816   (setq gnus-summary-mode-map (make-keymap))
6817   (suppress-keymap gnus-summary-mode-map)
6818
6819   ;; Non-orthogonal keys
6820
6821   (gnus-define-keys gnus-summary-mode-map
6822     " " gnus-summary-next-page
6823     "\177" gnus-summary-prev-page
6824     [delete] gnus-summary-prev-page
6825     "\r" gnus-summary-scroll-up
6826     "n" gnus-summary-next-unread-article
6827     "p" gnus-summary-prev-unread-article
6828     "N" gnus-summary-next-article
6829     "P" gnus-summary-prev-article
6830     "\M-\C-n" gnus-summary-next-same-subject
6831     "\M-\C-p" gnus-summary-prev-same-subject
6832     "\M-n" gnus-summary-next-unread-subject
6833     "\M-p" gnus-summary-prev-unread-subject
6834     "." gnus-summary-first-unread-article
6835     "," gnus-summary-best-unread-article
6836     "\M-s" gnus-summary-search-article-forward
6837     "\M-r" gnus-summary-search-article-backward
6838     "<" gnus-summary-beginning-of-article
6839     ">" gnus-summary-end-of-article
6840     "j" gnus-summary-goto-article
6841     "^" gnus-summary-refer-parent-article
6842     "\M-^" gnus-summary-refer-article
6843     "u" gnus-summary-tick-article-forward
6844     "!" gnus-summary-tick-article-forward
6845     "U" gnus-summary-tick-article-backward
6846     "d" gnus-summary-mark-as-read-forward
6847     "D" gnus-summary-mark-as-read-backward
6848     "E" gnus-summary-mark-as-expirable
6849     "\M-u" gnus-summary-clear-mark-forward
6850     "\M-U" gnus-summary-clear-mark-backward
6851     "k" gnus-summary-kill-same-subject-and-select
6852     "\C-k" gnus-summary-kill-same-subject
6853     "\M-\C-k" gnus-summary-kill-thread
6854     "\M-\C-l" gnus-summary-lower-thread
6855     "e" gnus-summary-edit-article
6856     "#" gnus-summary-mark-as-processable
6857     "\M-#" gnus-summary-unmark-as-processable
6858     "\M-\C-t" gnus-summary-toggle-threads
6859     "\M-\C-s" gnus-summary-show-thread
6860     "\M-\C-h" gnus-summary-hide-thread
6861     "\M-\C-f" gnus-summary-next-thread
6862     "\M-\C-b" gnus-summary-prev-thread
6863     "\M-\C-u" gnus-summary-up-thread
6864     "\M-\C-d" gnus-summary-down-thread
6865     "&" gnus-summary-execute-command
6866     "c" gnus-summary-catchup-and-exit
6867     "\C-w" gnus-summary-mark-region-as-read
6868     "\C-t" gnus-summary-toggle-truncation
6869     "?" gnus-summary-mark-as-dormant
6870     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6871     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6872     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6873     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6874     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6875     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6876     "=" gnus-summary-expand-window
6877     "\C-x\C-s" gnus-summary-reselect-current-group
6878     "\M-g" gnus-summary-rescan-group
6879     "w" gnus-summary-stop-page-breaking
6880     "\C-c\C-r" gnus-summary-caesar-message
6881     "\M-t" gnus-summary-toggle-mime
6882     "f" gnus-summary-followup
6883     "F" gnus-summary-followup-with-original
6884     "C" gnus-summary-cancel-article
6885     "r" gnus-summary-reply
6886     "R" gnus-summary-reply-with-original
6887     "\C-c\C-f" gnus-summary-mail-forward
6888     "o" gnus-summary-save-article
6889     "\C-o" gnus-summary-save-article-mail
6890     "|" gnus-summary-pipe-output
6891     "\M-k" gnus-summary-edit-local-kill
6892     "\M-K" gnus-summary-edit-global-kill
6893     "V" gnus-version
6894     "\C-c\C-d" gnus-summary-describe-group
6895     "q" gnus-summary-exit
6896     "Q" gnus-summary-exit-no-update
6897     "\C-c\C-i" gnus-info-find-node
6898     gnus-mouse-2 gnus-mouse-pick-article
6899     "m" gnus-summary-mail-other-window
6900     "a" gnus-summary-post-news
6901     "x" gnus-summary-limit-to-unread
6902     "s" gnus-summary-isearch-article
6903     "t" gnus-article-hide-headers
6904     "g" gnus-summary-show-article
6905     "l" gnus-summary-goto-last-article
6906     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6907     "\C-d" gnus-summary-enter-digest-group
6908     "\C-c\C-b" gnus-bug
6909     "*" gnus-cache-enter-article
6910     "\M-*" gnus-cache-remove-article
6911     "\M-&" gnus-summary-universal-argument
6912     "\C-l" gnus-recenter
6913     "I" gnus-summary-increase-score
6914     "L" gnus-summary-lower-score
6915
6916     "V" gnus-summary-score-map
6917     "X" gnus-uu-extract-map
6918     "S" gnus-summary-send-map)
6919
6920   ;; Sort of orthogonal keymap
6921   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6922     "t" gnus-summary-tick-article-forward
6923     "!" gnus-summary-tick-article-forward
6924     "d" gnus-summary-mark-as-read-forward
6925     "r" gnus-summary-mark-as-read-forward
6926     "c" gnus-summary-clear-mark-forward
6927     " " gnus-summary-clear-mark-forward
6928     "e" gnus-summary-mark-as-expirable
6929     "x" gnus-summary-mark-as-expirable
6930     "?" gnus-summary-mark-as-dormant
6931     "b" gnus-summary-set-bookmark
6932     "B" gnus-summary-remove-bookmark
6933     "#" gnus-summary-mark-as-processable
6934     "\M-#" gnus-summary-unmark-as-processable
6935     "S" gnus-summary-limit-include-expunged
6936     "C" gnus-summary-catchup
6937     "H" gnus-summary-catchup-to-here
6938     "\C-c" gnus-summary-catchup-all
6939     "k" gnus-summary-kill-same-subject-and-select
6940     "K" gnus-summary-kill-same-subject
6941     "P" gnus-uu-mark-map)
6942
6943   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6944     "c" gnus-summary-clear-above
6945     "u" gnus-summary-tick-above
6946     "m" gnus-summary-mark-above
6947     "k" gnus-summary-kill-below)
6948
6949   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6950     "/" gnus-summary-limit-to-subject
6951     "n" gnus-summary-limit-to-articles
6952     "w" gnus-summary-pop-limit
6953     "s" gnus-summary-limit-to-subject
6954     "a" gnus-summary-limit-to-author
6955     "u" gnus-summary-limit-to-unread
6956     "m" gnus-summary-limit-to-marks
6957     "v" gnus-summary-limit-to-score
6958     "D" gnus-summary-limit-include-dormant
6959     "d" gnus-summary-limit-exclude-dormant
6960     ;;  "t" gnus-summary-limit-exclude-thread
6961     "E" gnus-summary-limit-include-expunged
6962     "c" gnus-summary-limit-exclude-childless-dormant
6963     "C" gnus-summary-limit-mark-excluded-as-read)
6964
6965   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6966     "n" gnus-summary-next-unread-article
6967     "p" gnus-summary-prev-unread-article
6968     "N" gnus-summary-next-article
6969     "P" gnus-summary-prev-article
6970     "\C-n" gnus-summary-next-same-subject
6971     "\C-p" gnus-summary-prev-same-subject
6972     "\M-n" gnus-summary-next-unread-subject
6973     "\M-p" gnus-summary-prev-unread-subject
6974     "f" gnus-summary-first-unread-article
6975     "b" gnus-summary-best-unread-article
6976     "j" gnus-summary-goto-article
6977     "g" gnus-summary-goto-subject
6978     "l" gnus-summary-goto-last-article
6979     "p" gnus-summary-pop-article)
6980
6981   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6982     "k" gnus-summary-kill-thread
6983     "l" gnus-summary-lower-thread
6984     "i" gnus-summary-raise-thread
6985     "T" gnus-summary-toggle-threads
6986     "t" gnus-summary-rethread-current
6987     "^" gnus-summary-reparent-thread
6988     "s" gnus-summary-show-thread
6989     "S" gnus-summary-show-all-threads
6990     "h" gnus-summary-hide-thread
6991     "H" gnus-summary-hide-all-threads
6992     "n" gnus-summary-next-thread
6993     "p" gnus-summary-prev-thread
6994     "u" gnus-summary-up-thread
6995     "o" gnus-summary-top-thread
6996     "d" gnus-summary-down-thread
6997     "#" gnus-uu-mark-thread
6998     "\M-#" gnus-uu-unmark-thread)
6999
7000   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7001     "c" gnus-summary-catchup-and-exit
7002     "C" gnus-summary-catchup-all-and-exit
7003     "E" gnus-summary-exit-no-update
7004     "Q" gnus-summary-exit
7005     "Z" gnus-summary-exit
7006     "n" gnus-summary-catchup-and-goto-next-group
7007     "R" gnus-summary-reselect-current-group
7008     "G" gnus-summary-rescan-group
7009     "N" gnus-summary-next-group
7010     "P" gnus-summary-prev-group)
7011
7012   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7013     " " gnus-summary-next-page
7014     "n" gnus-summary-next-page
7015     "\177" gnus-summary-prev-page
7016     [delete] gnus-summary-prev-page
7017     "p" gnus-summary-prev-page
7018     "\r" gnus-summary-scroll-up
7019     "<" gnus-summary-beginning-of-article
7020     ">" gnus-summary-end-of-article
7021     "b" gnus-summary-beginning-of-article
7022     "e" gnus-summary-end-of-article
7023     "^" gnus-summary-refer-parent-article
7024     "r" gnus-summary-refer-parent-article
7025     "R" gnus-summary-refer-references
7026     "g" gnus-summary-show-article
7027     "s" gnus-summary-isearch-article)
7028
7029   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7030     "b" gnus-article-add-buttons
7031     "B" gnus-article-add-buttons-to-head
7032     "o" gnus-article-treat-overstrike
7033     ;;  "w" gnus-article-word-wrap
7034     "w" gnus-article-fill-cited-article
7035     "c" gnus-article-remove-cr
7036     "L" gnus-article-remove-trailing-blank-lines
7037     "q" gnus-article-de-quoted-unreadable
7038     "f" gnus-article-display-x-face
7039     "l" gnus-summary-stop-page-breaking
7040     "r" gnus-summary-caesar-message
7041     "t" gnus-article-hide-headers
7042     "v" gnus-summary-verbose-headers
7043     "m" gnus-summary-toggle-mime)
7044
7045   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7046     "a" gnus-article-hide
7047     "h" gnus-article-hide-headers
7048     "b" gnus-article-hide-boring-headers
7049     "s" gnus-article-hide-signature
7050     "c" gnus-article-hide-citation
7051     "p" gnus-article-hide-pgp
7052     "\C-c" gnus-article-hide-citation-maybe)
7053
7054   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7055     "a" gnus-article-highlight
7056     "h" gnus-article-highlight-headers
7057     "c" gnus-article-highlight-citation
7058     "s" gnus-article-highlight-signature)
7059
7060   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7061     "z" gnus-article-date-ut
7062     "u" gnus-article-date-ut
7063     "l" gnus-article-date-local
7064     "e" gnus-article-date-lapsed
7065     "o" gnus-article-date-original)
7066
7067   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7068     "v" gnus-version
7069     "f" gnus-summary-fetch-faq
7070     "d" gnus-summary-describe-group
7071     "h" gnus-summary-describe-briefly
7072     "i" gnus-info-find-node)
7073
7074   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7075     "e" gnus-summary-expire-articles
7076     "\M-\C-e" gnus-summary-expire-articles-now
7077     "\177" gnus-summary-delete-article
7078     [delete] gnus-summary-delete-article
7079     "m" gnus-summary-move-article
7080     "r" gnus-summary-respool-article
7081     "w" gnus-summary-edit-article
7082     "c" gnus-summary-copy-article
7083     "B" gnus-summary-crosspost-article
7084     "q" gnus-summary-respool-query
7085     "i" gnus-summary-import-article)
7086
7087   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7088     "o" gnus-summary-save-article
7089     "m" gnus-summary-save-article-mail
7090     "r" gnus-summary-save-article-rmail
7091     "f" gnus-summary-save-article-file
7092     "b" gnus-summary-save-article-body-file
7093     "h" gnus-summary-save-article-folder
7094     "v" gnus-summary-save-article-vm
7095     "p" gnus-summary-pipe-output
7096     "s" gnus-soup-add-article)
7097   )
7098
7099 \f
7100
7101 (defun gnus-summary-mode (&optional group)
7102   "Major mode for reading articles.
7103
7104 All normal editing commands are switched off.
7105 \\<gnus-summary-mode-map>
7106 Each line in this buffer represents one article.  To read an
7107 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7108 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7109 respectively.
7110
7111 You can also post articles and send mail from this buffer.  To
7112 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7113 of an article, type `\\[gnus-summary-reply]'.
7114
7115 There are approx. one gazillion commands you can execute in this
7116 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7117
7118 The following commands are available:
7119
7120 \\{gnus-summary-mode-map}"
7121   (interactive)
7122   (when (and menu-bar-mode
7123              (gnus-visual-p 'summary-menu 'menu))
7124     (gnus-summary-make-menu-bar))
7125   (kill-all-local-variables)
7126   (let ((locals gnus-summary-local-variables))
7127     (while locals
7128       (if (consp (car locals))
7129           (progn
7130             (make-local-variable (caar locals))
7131             (set (caar locals) (eval (cdar locals))))
7132         (make-local-variable (car locals))
7133         (set (car locals) nil))
7134       (setq locals (cdr locals))))
7135   (gnus-make-thread-indent-array)
7136   (gnus-simplify-mode-line)
7137   (setq major-mode 'gnus-summary-mode)
7138   (setq mode-name "Summary")
7139   (make-local-variable 'minor-mode-alist)
7140   (use-local-map gnus-summary-mode-map)
7141   (buffer-disable-undo (current-buffer))
7142   (setq buffer-read-only t)             ;Disable modification
7143   (setq truncate-lines t)
7144   (setq selective-display t)
7145   (setq selective-display-ellipses t)   ;Display `...'
7146   (setq buffer-display-table gnus-summary-display-table)
7147   (setq gnus-newsgroup-name group)
7148   (make-local-variable 'gnus-summary-line-format)
7149   (make-local-variable 'gnus-summary-line-format-spec)
7150   (make-local-variable 'gnus-summary-mark-positions)
7151   (run-hooks 'gnus-summary-mode-hook))
7152
7153 (defun gnus-summary-make-display-table ()
7154   ;; Change the display table.  Odd characters have a tendency to mess
7155   ;; up nicely formatted displays - we make all possible glyphs
7156   ;; display only a single character.
7157
7158   ;; We start from the standard display table, if any.
7159   (setq gnus-summary-display-table
7160         (or (copy-sequence standard-display-table)
7161             (make-display-table)))
7162   ;; Nix out all the control chars...
7163   (let ((i 32))
7164     (while (>= (setq i (1- i)) 0)
7165       (aset gnus-summary-display-table i [??])))
7166   ;; ... but not newline and cr, of course. (cr is necessary for the
7167   ;; selective display).
7168   (aset gnus-summary-display-table ?\n nil)
7169   (aset gnus-summary-display-table ?\r nil)
7170   ;; We nix out any glyphs over 126 that are not set already.
7171   (let ((i 256))
7172     (while (>= (setq i (1- i)) 127)
7173       ;; Only modify if the entry is nil.
7174       (or (aref gnus-summary-display-table i)
7175           (aset gnus-summary-display-table i [??])))))
7176
7177 (defun gnus-summary-clear-local-variables ()
7178   (let ((locals gnus-summary-local-variables))
7179     (while locals
7180       (if (consp (car locals))
7181           (and (vectorp (caar locals))
7182                (set (caar locals) nil))
7183         (and (vectorp (car locals))
7184              (set (car locals) nil)))
7185       (setq locals (cdr locals)))))
7186
7187 ;; Summary data functions.
7188
7189 (defmacro gnus-data-number (data)
7190   `(car ,data))
7191
7192 (defmacro gnus-data-set-number (data number)
7193   `(setcar ,data ,number))
7194
7195 (defmacro gnus-data-mark (data)
7196   `(nth 1 ,data))
7197
7198 (defmacro gnus-data-set-mark (data mark)
7199   `(setcar (nthcdr 1 ,data) ,mark))
7200
7201 (defmacro gnus-data-pos (data)
7202   `(nth 2 ,data))
7203
7204 (defmacro gnus-data-set-pos (data pos)
7205   `(setcar (nthcdr 2 ,data) ,pos))
7206
7207 (defmacro gnus-data-header (data)
7208   `(nth 3 ,data))
7209
7210 (defmacro gnus-data-level (data)
7211   `(nth 4 ,data))
7212
7213 (defmacro gnus-data-unread-p (data)
7214   `(= (nth 1 ,data) gnus-unread-mark))
7215
7216 (defmacro gnus-data-pseudo-p (data)
7217   `(consp (nth 3 ,data)))
7218
7219 (defmacro gnus-data-find (number)
7220   `(assq ,number gnus-newsgroup-data))
7221
7222 (defmacro gnus-data-find-list (number &optional data)
7223   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7224      (memq (assq ,number bdata)
7225            bdata)))
7226
7227 (defmacro gnus-data-make (number mark pos header level)
7228   `(list ,number ,mark ,pos ,header ,level))
7229
7230 (defun gnus-data-enter (after-article number mark pos header level offset)
7231   (let ((data (gnus-data-find-list after-article)))
7232     (or data (error "No such article: %d" after-article))
7233     (setcdr data (cons (gnus-data-make number mark pos header level)
7234                        (cdr data)))
7235     (setq gnus-newsgroup-data-reverse nil)
7236     (gnus-data-update-list (cddr data) offset)))
7237
7238 (defun gnus-data-enter-list (after-article list &optional offset)
7239   (when list
7240     (let ((data (and after-article (gnus-data-find-list after-article)))
7241           (ilist list))
7242       (or data (not after-article) (error "No such article: %d" after-article))
7243       ;; Find the last element in the list to be spliced into the main
7244       ;; list.
7245       (while (cdr list)
7246         (setq list (cdr list)))
7247       (if (not data)
7248           (progn
7249             (setcdr list gnus-newsgroup-data)
7250             (setq gnus-newsgroup-data ilist)
7251             (and offset (gnus-data-update-list (cdr list) offset)))
7252         (setcdr list (cdr data))
7253         (setcdr data ilist)
7254         (and offset (gnus-data-update-list (cdr data) offset)))
7255       (setq gnus-newsgroup-data-reverse nil))))
7256
7257 (defun gnus-data-remove (article &optional offset)
7258   (let ((data gnus-newsgroup-data))
7259     (if (= (gnus-data-number (car data)) article)
7260         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7261               gnus-newsgroup-data-reverse nil)
7262       (while (cdr data)
7263         (and (= (gnus-data-number (cadr data)) article)
7264              (progn
7265                (setcdr data (cddr data))
7266                (and offset (gnus-data-update-list (cdr data) offset))
7267                (setq data nil
7268                      gnus-newsgroup-data-reverse nil)))
7269         (setq data (cdr data))))))
7270
7271 (defmacro gnus-data-list (backward)
7272   `(if ,backward
7273        (or gnus-newsgroup-data-reverse
7274            (setq gnus-newsgroup-data-reverse
7275                  (reverse gnus-newsgroup-data)))
7276      gnus-newsgroup-data))
7277
7278 (defun gnus-data-update-list (data offset)
7279   "Add OFFSET to the POS of all data entries in DATA."
7280   (while data
7281     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7282     (setq data (cdr data))))
7283
7284 (defun gnus-data-compute-positions ()
7285   "Compute the positions of all articles."
7286   (let ((data gnus-newsgroup-data)
7287         pos)
7288     (while data
7289       (when (setq pos (text-property-any
7290                        (point-min) (point-max)
7291                        'gnus-number (gnus-data-number (car data))))
7292         (gnus-data-set-pos (car data) (+ pos 3)))
7293       (setq data (cdr data)))))
7294
7295 (defun gnus-summary-article-pseudo-p (article)
7296   "Say whether this article is a pseudo article or not."
7297   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7298
7299 (defun gnus-article-parent-p (number)
7300   "Say whether this article is a parent or not."
7301   (let ((data (gnus-data-find-list number)))
7302     (and (cdr data)                     ; There has to be an article after...
7303          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7304             (gnus-data-level (nth 1 data))))))
7305
7306 (defun gnus-article-children (number)
7307   "Return a list of all children to NUMBER."
7308   (let* ((data (gnus-data-find-list number))
7309          (level (gnus-data-level (car data)))
7310          children)
7311     (setq data (cdr data))
7312     (while (and data            
7313                 (= (gnus-data-level (car data)) (1+ level)))
7314       (push (gnus-data-number (car data)) children)
7315       (setq data (cdr data)))
7316     children))
7317
7318 (defmacro gnus-summary-skip-intangible ()
7319   "If the current article is intangible, then jump to a different article."
7320   '(let ((to (get-text-property (point) 'gnus-intangible)))
7321     (and to (gnus-summary-goto-subject to))))
7322
7323 (defmacro gnus-summary-article-intangible-p ()
7324   "Say whether this article is intangible or not."
7325   '(get-text-property (point) 'gnus-intangible))
7326
7327 ;; Some summary mode macros.
7328
7329 (defmacro gnus-summary-article-number ()
7330   "The article number of the article on the current line.
7331 If there isn's an article number here, then we return the current
7332 article number."
7333   '(progn
7334      (gnus-summary-skip-intangible)
7335      (or (get-text-property (point) 'gnus-number)
7336          (gnus-summary-last-subject))))
7337
7338 (defmacro gnus-summary-article-header (&optional number)
7339   `(gnus-data-header (gnus-data-find
7340                       ,(or number '(gnus-summary-article-number)))))
7341
7342 (defmacro gnus-summary-thread-level (&optional number)
7343   `(if (and (eq gnus-summary-make-false-root 'dummy)
7344             (get-text-property (point) 'gnus-intangible))
7345        0
7346      (gnus-data-level (gnus-data-find
7347                        ,(or number '(gnus-summary-article-number))))))
7348
7349 (defmacro gnus-summary-article-mark (&optional number)
7350   `(gnus-data-mark (gnus-data-find
7351                     ,(or number '(gnus-summary-article-number)))))
7352
7353 (defmacro gnus-summary-article-pos (&optional number)
7354   `(gnus-data-pos (gnus-data-find
7355                    ,(or number '(gnus-summary-article-number)))))
7356
7357 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7358 (defmacro gnus-summary-article-subject (&optional number)
7359   "Return current subject string or nil if nothing."
7360   `(let ((headers
7361           ,(if number
7362                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7363              '(gnus-data-header (assq (gnus-summary-article-number)
7364                                       gnus-newsgroup-data)))))
7365      (and headers
7366           (vectorp headers)
7367           (mail-header-subject headers))))
7368
7369 (defmacro gnus-summary-article-score (&optional number)
7370   "Return current article score."
7371   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7372                   gnus-newsgroup-scored))
7373        gnus-summary-default-score 0))
7374
7375 (defun gnus-summary-article-children (&optional number)
7376   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7377          (level (gnus-data-level (car data)))
7378          l children)
7379     (while (and (setq data (cdr data))
7380                 (> (setq l (gnus-data-level (car data))) level))
7381       (and (= (1+ level) l)
7382            (setq children (cons (gnus-data-number (car data))
7383                                 children))))
7384     (nreverse children)))
7385
7386 (defun gnus-summary-article-parent (&optional number)
7387   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7388                                     (gnus-data-list t)))
7389          (level (gnus-data-level (car data))))
7390     (if (zerop level)
7391         () ; This is a root.
7392       ;; We search until we find an article with a level less than
7393       ;; this one.  That function has to be the parent.
7394       (while (and (setq data (cdr data))
7395                   (not (< (gnus-data-level (car data)) level))))
7396       (and data (gnus-data-number (car data))))))
7397
7398 (defun gnus-unread-mark-p (mark)
7399   "Say whether MARK is the unread mark."
7400   (= mark gnus-unread-mark))
7401
7402 (defun gnus-read-mark-p (mark)
7403   "Say whether MARK is one of the marks that mark as read.
7404 This is all marks except unread, ticked, dormant, and expirable."
7405   (not (or (= mark gnus-unread-mark)
7406            (= mark gnus-ticked-mark)
7407            (= mark gnus-dormant-mark)
7408            (= mark gnus-expirable-mark))))
7409
7410 ;; Saving hidden threads.
7411
7412 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7413 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7414 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7415
7416 (defmacro gnus-save-hidden-threads (&rest forms)
7417   "Save hidden threads, eval FORMS, and restore the hidden threads."
7418   (let ((config (make-symbol "config")))
7419     `(let ((,config (gnus-hidden-threads-configuration)))
7420        (unwind-protect
7421            (progn
7422              ,@forms)
7423          (gnus-restore-hidden-threads-configuration ,config)))))
7424
7425 (defun gnus-hidden-threads-configuration ()
7426   "Return the current hidden threads configuration."
7427   (save-excursion
7428     (let (config)
7429       (goto-char (point-min))
7430       (while (search-forward "\r" nil t)
7431         (push (1- (point)) config))
7432       config)))
7433
7434 (defun gnus-restore-hidden-threads-configuration (config)
7435   "Restore hidden threads configuration from CONFIG."
7436   (let (point buffer-read-only)
7437     (while (setq point (pop config))
7438       (when (and (< point (point-max))
7439                  (goto-char point)
7440                  (= (following-char) ?\n))
7441         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7442
7443 ;; Various summary mode internalish functions.
7444
7445 (defun gnus-mouse-pick-article (e)
7446   (interactive "e")
7447   (mouse-set-point e)
7448   (gnus-summary-next-page nil t))
7449
7450 (defun gnus-summary-setup-buffer (group)
7451   "Initialize summary buffer."
7452   (let ((buffer (concat "*Summary " group "*")))
7453     (if (get-buffer buffer)
7454         (progn
7455           (set-buffer buffer)
7456           (setq gnus-summary-buffer (current-buffer))
7457           (not gnus-newsgroup-prepared))
7458       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7459       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7460       (gnus-add-current-to-buffer-list)
7461       (gnus-summary-mode group)
7462       (when gnus-carpal
7463         (gnus-carpal-setup-buffer 'summary))
7464       (unless gnus-single-article-buffer
7465         (make-local-variable 'gnus-article-buffer)
7466         (make-local-variable 'gnus-article-current)
7467         (make-local-variable 'gnus-original-article-buffer))
7468       (setq gnus-newsgroup-name group)
7469       t)))
7470
7471 (defun gnus-set-global-variables ()
7472   ;; Set the global equivalents of the summary buffer-local variables
7473   ;; to the latest values they had.  These reflect the summary buffer
7474   ;; that was in action when the last article was fetched.
7475   (when (eq major-mode 'gnus-summary-mode)
7476     (setq gnus-summary-buffer (current-buffer))
7477     (let ((name gnus-newsgroup-name)
7478           (marked gnus-newsgroup-marked)
7479           (unread gnus-newsgroup-unreads)
7480           (headers gnus-current-headers)
7481           (data gnus-newsgroup-data)
7482           (summary gnus-summary-buffer)
7483           (article-buffer gnus-article-buffer)
7484           (original gnus-original-article-buffer)
7485           (gac gnus-article-current)
7486           (score-file gnus-current-score-file))
7487       (save-excursion
7488         (set-buffer gnus-group-buffer)
7489         (setq gnus-newsgroup-name name)
7490         (setq gnus-newsgroup-marked marked)
7491         (setq gnus-newsgroup-unreads unread)
7492         (setq gnus-current-headers headers)
7493         (setq gnus-newsgroup-data data)
7494         (setq gnus-article-current gac)
7495         (setq gnus-summary-buffer summary)
7496         (setq gnus-article-buffer article-buffer)
7497         (setq gnus-original-article-buffer original)
7498         (setq gnus-current-score-file score-file)))))
7499
7500 (defun gnus-summary-last-article-p (&optional article)
7501   "Return whether ARTICLE is the last article in the buffer."
7502   (if (not (setq article (or article (gnus-summary-article-number))))
7503       t ; All non-existant numbers are the last article. :-)
7504     (not (cdr (gnus-data-find-list article)))))
7505
7506 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7507   "Insert a dummy root in the summary buffer."
7508   (beginning-of-line)
7509   (gnus-add-text-properties
7510    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7511    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7512
7513 (defun gnus-make-thread-indent-array ()
7514   (let ((n 200))
7515     (unless (and gnus-thread-indent-array
7516                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7517       (setq gnus-thread-indent-array (make-vector 201 "")
7518             gnus-thread-indent-array-level gnus-thread-indent-level)
7519       (while (>= n 0)
7520         (aset gnus-thread-indent-array n
7521               (make-string (* n gnus-thread-indent-level) ? ))
7522         (setq n (1- n))))))
7523
7524 (defun gnus-summary-insert-line
7525   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7526                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7527                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7528   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7529          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7530          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7531          (gnus-tmp-score-char
7532           (if (or (null gnus-summary-default-score)
7533                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7534                       gnus-summary-zcore-fuzz)) ? 
7535             (if (< gnus-tmp-score gnus-summary-default-score)
7536                 gnus-score-below-mark gnus-score-over-mark)))
7537          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7538                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7539                                   gnus-cached-mark)
7540                                  (gnus-tmp-replied gnus-replied-mark)
7541                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7542                                   gnus-saved-mark)
7543                                  (t gnus-unread-mark)))
7544          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7545          (gnus-tmp-name
7546           (cond
7547            ((string-match "(.+)" gnus-tmp-from)
7548             (substring gnus-tmp-from
7549                        (1+ (match-beginning 0)) (1- (match-end 0))))
7550            ((string-match "<[^>]+> *$" gnus-tmp-from)
7551             (let ((beg (match-beginning 0)))
7552               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7553                        (substring gnus-tmp-from (1+ (match-beginning 0))
7554                                   (1- (match-end 0))))
7555                   (substring gnus-tmp-from 0 beg))))
7556            (t gnus-tmp-from)))
7557          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7558          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7559          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7560          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7561          (buffer-read-only nil))
7562     (when (string= gnus-tmp-name "")
7563       (setq gnus-tmp-name gnus-tmp-from))
7564     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7565     (gnus-put-text-property
7566      (point)
7567      (progn (eval gnus-summary-line-format-spec) (point))
7568      'gnus-number gnus-tmp-number)
7569     (when (gnus-visual-p 'summary-highlight 'highlight)
7570       (forward-line -1)
7571       (run-hooks 'gnus-summary-update-hook)
7572       (forward-line 1))))
7573
7574 (defun gnus-summary-update-line (&optional dont-update)
7575   ;; Update summary line after change.
7576   (when (and gnus-summary-default-score
7577              (not gnus-summary-inhibit-highlight))
7578     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7579            (article (gnus-summary-article-number))
7580            (score (gnus-summary-article-score article)))
7581       (unless dont-update
7582         (if (and gnus-summary-mark-below
7583                  (< (gnus-summary-article-score)
7584                     gnus-summary-mark-below))
7585             ;; This article has a low score, so we mark it as read.
7586             (when (memq article gnus-newsgroup-unreads)
7587               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7588           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7589             ;; This article was previously marked as read on account
7590             ;; of a low score, but now it has risen, so we mark it as
7591             ;; unread.
7592             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7593         (gnus-summary-update-mark
7594          (if (or (null gnus-summary-default-score)
7595                  (<= (abs (- score gnus-summary-default-score))
7596                      gnus-summary-zcore-fuzz)) ? 
7597            (if (< score gnus-summary-default-score)
7598                gnus-score-below-mark gnus-score-over-mark)) 'score))
7599       ;; Do visual highlighting.
7600       (when (gnus-visual-p 'summary-highlight 'highlight)
7601         (run-hooks 'gnus-summary-update-hook)))))
7602
7603 (defvar gnus-tmp-new-adopts nil)
7604
7605 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7606   ;; Sum up all elements (and sub-elements) in a list.
7607   (let* ((number
7608           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7609           (cond
7610            ((and (consp thread) (cdr thread))
7611             (apply
7612              '+ 1 (mapcar
7613                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7614            ((null thread)
7615             1)
7616            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7617             1)
7618            (t 0))))
7619     (when (and level (zerop level) gnus-tmp-new-adopts)
7620       (incf number
7621             (apply '+ (mapcar
7622                        'gnus-summary-number-of-articles-in-thread
7623                        gnus-tmp-new-adopts))))
7624     (if char
7625         (if (> number 1) gnus-not-empty-thread-mark
7626           gnus-empty-thread-mark)
7627       number)))
7628
7629 (defun gnus-summary-set-local-parameters (group)
7630  "Go through the local params of GROUP and set all variable specs in that list."
7631   (let ((params (gnus-info-params (gnus-get-info group)))
7632         elem)
7633     (while params
7634       (setq elem (car params)
7635             params (cdr params))
7636       (and (consp elem)                 ; Has to be a cons.
7637            (consp (cdr elem))           ; The cdr has to be a list.
7638            (symbolp (car elem))         ; Has to be a symbol in there.
7639            (not (memq (car elem) 
7640                       '(quit-config to-address to-list to-group)))
7641            (progn                       ; So we set it.
7642              (make-local-variable (car elem))
7643              (set (car elem) (eval (nth 1 elem))))))))
7644
7645 (defun gnus-summary-read-group (group &optional show-all no-article
7646                                       kill-buffer no-display)
7647   "Start reading news in newsgroup GROUP.
7648 If SHOW-ALL is non-nil, already read articles are also listed.
7649 If NO-ARTICLE is non-nil, no article is selected initially.
7650 If NO-DISPLAY, don't generate a summary buffer."
7651   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7652   (let* ((new-group (gnus-summary-setup-buffer group))
7653          (quit-config (gnus-group-quit-config group))
7654          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7655     (cond
7656      ;; This summary buffer exists already, so we just select it.
7657      ((not new-group)
7658       (gnus-set-global-variables)
7659       (when kill-buffer
7660         (gnus-kill-or-deaden-summary kill-buffer))
7661       (gnus-configure-windows 'summary 'force)
7662       (gnus-set-mode-line 'summary)
7663       (gnus-summary-position-point)
7664       (message "")
7665       t)
7666      ;; We couldn't select this group.
7667      ((null did-select)
7668       (when (and (eq major-mode 'gnus-summary-mode)
7669                  (not (equal (current-buffer) kill-buffer)))
7670         (kill-buffer (current-buffer))
7671         (if (not quit-config)
7672             (progn
7673               (set-buffer gnus-group-buffer)
7674               (gnus-group-jump-to-group group)
7675               (gnus-group-next-unread-group 1))
7676           (if (not (buffer-name (car quit-config)))
7677               (gnus-configure-windows 'group 'force)
7678             (set-buffer (car quit-config))
7679             (and (eq major-mode 'gnus-summary-mode)
7680                  (gnus-set-global-variables))
7681             (gnus-configure-windows (cdr quit-config)))))
7682       (gnus-message 3 "Can't select group")
7683       nil)
7684      ;; The user did a `C-g' while prompting for number of articles,
7685      ;; so we exit this group.
7686      ((eq did-select 'quit)
7687       (and (eq major-mode 'gnus-summary-mode)
7688            (not (equal (current-buffer) kill-buffer))
7689            (kill-buffer (current-buffer)))
7690       (when kill-buffer
7691         (gnus-kill-or-deaden-summary kill-buffer))
7692       (if (not quit-config)
7693           (progn
7694             (set-buffer gnus-group-buffer)
7695             (gnus-group-jump-to-group group)
7696             (gnus-group-next-unread-group 1)
7697             (gnus-configure-windows 'group 'force))
7698         (if (not (buffer-name (car quit-config)))
7699             (gnus-configure-windows 'group 'force)
7700           (set-buffer (car quit-config))
7701           (and (eq major-mode 'gnus-summary-mode)
7702                (gnus-set-global-variables))
7703           (gnus-configure-windows (cdr quit-config))))
7704       ;; Finally signal the quit.
7705       (signal 'quit nil))
7706      ;; The group was successfully selected.
7707      (t
7708       (gnus-set-global-variables)
7709       ;; Save the active value in effect when the group was entered.
7710       (setq gnus-newsgroup-active
7711             (gnus-copy-sequence
7712              (gnus-active gnus-newsgroup-name)))
7713       ;; You can change the summary buffer in some way with this hook.
7714       (run-hooks 'gnus-select-group-hook)
7715       ;; Set any local variables in the group parameters.
7716       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7717       (gnus-update-format-specifications)
7718       ;; Do score processing.
7719       (when gnus-use-scoring
7720         (gnus-possibly-score-headers))
7721       ;; Check whether to fill in the gaps in the threads.
7722       (when gnus-build-sparse-threads
7723         (gnus-build-sparse-threads))
7724       ;; Find the initial limit.
7725       (if gnus-show-threads
7726           (if show-all
7727               (let ((gnus-newsgroup-dormant nil))
7728                 (gnus-summary-initial-limit show-all))
7729             (gnus-summary-initial-limit show-all))
7730         (setq gnus-newsgroup-limit 
7731               (mapcar 
7732                (lambda (header) (mail-header-number header))
7733                gnus-newsgroup-headers)))
7734       ;; Generate the summary buffer.
7735       (unless no-display
7736         (gnus-summary-prepare))
7737       (when gnus-use-trees
7738         (gnus-tree-open group)
7739         (setq gnus-summary-highlight-line-function
7740               'gnus-tree-highlight-article))
7741       ;; If the summary buffer is empty, but there are some low-scored
7742       ;; articles or some excluded dormants, we include these in the
7743       ;; buffer.
7744       (when (and (zerop (buffer-size))
7745                  (not no-display))
7746         (cond (gnus-newsgroup-dormant
7747                (gnus-summary-limit-include-dormant))
7748               ((and gnus-newsgroup-scored show-all)
7749                (gnus-summary-limit-include-expunged))))
7750       ;; Function `gnus-apply-kill-file' must be called in this hook.
7751       (run-hooks 'gnus-apply-kill-hook)
7752       (if (and (zerop (buffer-size))
7753                (not no-display))
7754           (progn
7755             ;; This newsgroup is empty.
7756             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7757             (gnus-message 6 "No unread news")
7758             (when kill-buffer
7759               (gnus-kill-or-deaden-summary kill-buffer))
7760             ;; Return nil from this function.
7761             nil)
7762         ;; Hide conversation thread subtrees.  We cannot do this in
7763         ;; gnus-summary-prepare-hook since kill processing may not
7764         ;; work with hidden articles.
7765         (and gnus-show-threads
7766              gnus-thread-hide-subtree
7767              (gnus-summary-hide-all-threads))
7768         ;; Show first unread article if requested.
7769         (if (and (not no-article)
7770                  (not no-display)
7771                  gnus-newsgroup-unreads
7772                  gnus-auto-select-first)
7773             (unless (if (eq gnus-auto-select-first 'best)
7774                         (gnus-summary-best-unread-article)
7775                       (gnus-summary-first-unread-article))
7776               (gnus-configure-windows 'summary))
7777           ;; Don't select any articles, just move point to the first
7778           ;; article in the group.
7779           (goto-char (point-min))
7780           (gnus-summary-position-point)
7781           (gnus-set-mode-line 'summary)
7782           (gnus-configure-windows 'summary 'force))
7783         ;; If we are in async mode, we send some info to the backend.
7784         (when gnus-newsgroup-async
7785           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7786         (when kill-buffer
7787           (gnus-kill-or-deaden-summary kill-buffer))
7788         (when (get-buffer-window gnus-group-buffer t)
7789           ;; Gotta use windows, because recenter does wierd stuff if
7790           ;; the current buffer ain't the displayed window.
7791           (let ((owin (selected-window)))
7792             (select-window (get-buffer-window gnus-group-buffer t))
7793             (when (gnus-group-goto-group group)
7794               (recenter))
7795             (select-window owin))))
7796       ;; Mark this buffer as "prepared".
7797       (setq gnus-newsgroup-prepared t)
7798       t))))
7799
7800 (defun gnus-summary-prepare ()
7801   "Generate the summary buffer."
7802   (let ((buffer-read-only nil))
7803     (erase-buffer)
7804     (setq gnus-newsgroup-data nil
7805           gnus-newsgroup-data-reverse nil)
7806     (run-hooks 'gnus-summary-generate-hook)
7807     ;; Generate the buffer, either with threads or without.
7808     (when gnus-newsgroup-headers
7809       (gnus-summary-prepare-threads
7810        (if gnus-show-threads
7811            (gnus-sort-gathered-threads
7812             (funcall gnus-summary-thread-gathering-function
7813                      (gnus-sort-threads
7814                       (gnus-cut-threads (gnus-make-threads)))))
7815          ;; Unthreaded display.
7816          (gnus-sort-articles gnus-newsgroup-headers))))
7817     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7818     ;; Call hooks for modifying summary buffer.
7819     (goto-char (point-min))
7820     (run-hooks 'gnus-summary-prepare-hook)))
7821
7822 (defun gnus-gather-threads-by-subject (threads)
7823   "Gather threads by looking at Subject headers."
7824   (if (not gnus-summary-make-false-root)
7825       threads
7826     (let ((hashtb (gnus-make-hashtable 1023))
7827           (prev threads)
7828           (result threads)
7829           subject hthread whole-subject)
7830       (while threads
7831         (setq whole-subject (mail-header-subject (caar threads)))
7832         (setq subject
7833               (cond
7834                ;; Truncate the subject.
7835                ((numberp gnus-summary-gather-subject-limit)
7836                 (setq subject (gnus-simplify-subject-re whole-subject))
7837                 (if (> (length subject) gnus-summary-gather-subject-limit)
7838                     (substring subject 0 gnus-summary-gather-subject-limit)
7839                   subject))
7840                ;; Fuzzily simplify it.
7841                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7842                 (gnus-simplify-subject-fuzzy whole-subject))
7843                ;; Just remove the leading "Re:".
7844                (t
7845                 (gnus-simplify-subject-re whole-subject))))
7846
7847         (if (and gnus-summary-gather-exclude-subject
7848                  (string-match gnus-summary-gather-exclude-subject
7849                                subject))
7850             ()          ; We don't want to do anything with this article.
7851           ;; We simplify the subject before looking it up in the
7852           ;; hash table.
7853
7854           (if (setq hthread (gnus-gethash subject hashtb))
7855               (progn
7856                 ;; We enter a dummy root into the thread, if we
7857                 ;; haven't done that already.
7858                 (unless (stringp (caar hthread))
7859                   (setcar hthread (list whole-subject (car hthread))))
7860                 ;; We add this new gathered thread to this gathered
7861                 ;; thread.
7862                 (setcdr (car hthread)
7863                         (nconc (cdar hthread) (list (car threads))))
7864                 ;; Remove it from the list of threads.
7865                 (setcdr prev (cdr threads))
7866                 (setq threads prev))
7867             ;; Enter this thread into the hash table.
7868             (gnus-sethash subject threads hashtb)))
7869         (setq prev threads)
7870         (setq threads (cdr threads)))
7871       result)))
7872
7873 (defun gnus-gather-threads-by-references (threads)
7874   "Gather threads by looking at References headers."
7875   (let ((idhashtb (gnus-make-hashtable 1023))
7876         (thhashtb (gnus-make-hashtable 1023))
7877         (prev threads)
7878         (result threads)
7879         ids references id gthread gid entered)
7880     (while threads
7881       (when (setq references (mail-header-references (caar threads)))
7882         (setq id (mail-header-id (caar threads)))
7883         (setq ids (gnus-split-references references))
7884         (setq entered nil)
7885         (while ids
7886           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7887               (progn
7888                 (gnus-sethash (car ids) id idhashtb)
7889                 (gnus-sethash id threads thhashtb))
7890             (setq gthread (gnus-gethash gid thhashtb))
7891             (unless entered
7892               ;; We enter a dummy root into the thread, if we
7893               ;; haven't done that already.
7894               (unless (stringp (caar gthread))
7895                 (setcar gthread (list (mail-header-subject (caar gthread))
7896                                       (car gthread))))
7897               ;; We add this new gathered thread to this gathered
7898               ;; thread.
7899               (setcdr (car gthread)
7900                       (nconc (cdar gthread) (list (car threads)))))
7901             ;; Add it into the thread hash table.
7902             (gnus-sethash id gthread thhashtb)
7903             (setq entered t)
7904             ;; Remove it from the list of threads.
7905             (setcdr prev (cdr threads))
7906             (setq threads prev))
7907           (setq ids (cdr ids))))
7908       (setq prev threads)
7909       (setq threads (cdr threads)))
7910     result))
7911
7912 (defun gnus-sort-gathered-threads (threads)
7913   "Sort subtreads inside each gathered thread by article number."
7914   (let ((result threads))
7915     (while threads
7916       (when (stringp (caar threads))
7917         (setcdr (car threads)
7918                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7919       (setq threads (cdr threads)))
7920     result))
7921
7922 (defun gnus-make-threads ()
7923   "Go through the dependency hashtb and find the roots.  Return all threads."
7924   (let (threads)
7925     (mapatoms
7926      (lambda (refs)
7927        (unless (car (symbol-value refs))
7928          ;; These threads do not refer back to any other articles,
7929          ;; so they're roots.
7930          (setq threads (append (cdr (symbol-value refs)) threads))))
7931      gnus-newsgroup-dependencies)
7932     threads))
7933
7934 (defun gnus-build-sparse-threads ()
7935   (let ((headers gnus-newsgroup-headers)
7936         (deps gnus-newsgroup-dependencies)
7937         header references generation relations 
7938         cthread subject child end pthread relation)
7939     ;; First we create an alist of generations/relations, where 
7940     ;; generations is how much we trust the ralation, and the relation
7941     ;; is parent/child.
7942     (gnus-message 7 "Making sparse threads...")
7943     (save-excursion
7944       (nnheader-set-temp-buffer " *gnus sparse threads*")
7945       (while (setq header (pop headers))
7946         (when (and (setq references (mail-header-references header))
7947                    (not (string= references "")))
7948           (insert references)
7949           (setq child (mail-header-id header)
7950                 subject (mail-header-subject header))
7951           (setq generation 0)
7952           (while (search-backward ">" nil t)
7953             (setq end (1+ (point)))
7954             (when (search-backward "<" nil t)
7955               (push (list (incf generation) 
7956                           child (setq child (buffer-substring (point) end))
7957                           subject)
7958                     relations)))
7959           (push (list (1+ generation) child nil subject) relations)
7960           (erase-buffer)))
7961       (kill-buffer (current-buffer)))
7962     ;; Sort over trustworthiness.
7963     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7964     (while (setq relation (pop relations))
7965       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7966                 (unless (car (symbol-value cthread))
7967                   ;; Make this article the parent of these threads.
7968                   (setcar (symbol-value cthread)
7969                           (vector gnus-reffed-article-number 
7970                                   (cadddr relation) 
7971                                   "" ""
7972                                   (cadr relation) 
7973                                   (or (caddr relation) "") 0 0 "")))
7974               (set cthread (list (vector gnus-reffed-article-number
7975                                          (cadddr relation) 
7976                                          "" "" (cadr relation) 
7977                                          (or (caddr relation) "") 0 0 ""))))
7978         (push gnus-reffed-article-number gnus-newsgroup-limit)
7979         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7980         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7981               gnus-newsgroup-reads)
7982         (decf gnus-reffed-article-number)
7983         ;; Make this new thread the child of its parent.
7984         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7985             (setcdr (symbol-value pthread)
7986                     (nconc (cdr (symbol-value pthread))
7987                            (list (symbol-value cthread))))
7988           (set pthread (list nil (symbol-value cthread))))))
7989     (gnus-message 7 "Making sparse threads...done")))
7990
7991 (defun gnus-build-old-threads ()
7992   ;; Look at all the articles that refer back to old articles, and
7993   ;; fetch the headers for the articles that aren't there.  This will
7994   ;; build complete threads - if the roots haven't been expired by the
7995   ;; server, that is.
7996   (let (id heads)
7997     (mapatoms
7998      (lambda (refs)
7999        (when (not (car (symbol-value refs)))
8000          (setq heads (cdr (symbol-value refs)))
8001          (while heads
8002            (if (memq (mail-header-number (caar heads))
8003                      gnus-newsgroup-dormant)
8004                (setq heads (cdr heads))
8005              (setq id (symbol-name refs))
8006              (while (and (setq id (gnus-build-get-header id))
8007                          (not (car (gnus-gethash
8008                                     id gnus-newsgroup-dependencies)))))
8009              (setq heads nil)))))
8010      gnus-newsgroup-dependencies)))
8011
8012 (defun gnus-build-get-header (id)
8013   ;; Look through the buffer of NOV lines and find the header to
8014   ;; ID.  Enter this line into the dependencies hash table, and return
8015   ;; the id of the parent article (if any).
8016   (let ((deps gnus-newsgroup-dependencies)
8017         found header)
8018     (prog1
8019         (save-excursion
8020           (set-buffer nntp-server-buffer)
8021           (goto-char (point-min))
8022           (while (and (not found) (search-forward id nil t))
8023             (beginning-of-line)
8024             (setq found (looking-at
8025                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8026                                  (regexp-quote id))))
8027             (or found (beginning-of-line 2)))
8028           (when found
8029             (beginning-of-line)
8030             (and
8031              (setq header (gnus-nov-parse-line
8032                            (read (current-buffer)) deps))
8033              (gnus-parent-id (mail-header-references header)))))
8034       (when header
8035         (let ((number (mail-header-number header)))
8036           (push number gnus-newsgroup-limit)
8037           (push header gnus-newsgroup-headers)
8038           (if (memq number gnus-newsgroup-unselected)
8039               (progn
8040                 (push number gnus-newsgroup-unreads)
8041                 (setq gnus-newsgroup-unselected
8042                       (delq number gnus-newsgroup-unselected)))
8043             (push number gnus-newsgroup-ancient)))))))
8044
8045 (defun gnus-summary-update-article (article &optional header)
8046   "Update ARTICLE in the summary buffer."
8047   (set-buffer gnus-summary-buffer)
8048   (let* ((header (or header (gnus-summary-article-header article)))
8049          (id (mail-header-id header))
8050          (data (gnus-data-find article))
8051          (thread (gnus-id-to-thread id))
8052          (parent
8053           (gnus-id-to-thread (or (gnus-parent-id 
8054                                   (mail-header-references header))
8055                                  "tull")))
8056          (buffer-read-only nil)
8057          (old (car thread))
8058          (number (mail-header-number header))
8059          pos)
8060     (when thread
8061       ;; !!! Should this be in or not?
8062       ;(setcar thread nil)
8063       (when parent
8064         (delq thread parent))
8065       (if (gnus-summary-insert-subject id header)
8066           ;; Set the (possibly) new article number in the data structure.
8067           (gnus-data-set-number data (gnus-id-to-article id))
8068         (setcar thread old)
8069         nil))))
8070
8071 (defun gnus-rebuild-thread (id)
8072   "Rebuild the thread containing ID."
8073   (let ((buffer-read-only nil)
8074         current thread data)
8075     (if (not gnus-show-threads)
8076         (setq thread (list (car (gnus-id-to-thread id))))
8077       ;; Get the thread this article is part of.
8078       (setq thread (gnus-remove-thread id)))
8079     (setq current (save-excursion
8080                     (and (zerop (forward-line -1))
8081                          (gnus-summary-article-number))))
8082     ;; If this is a gathered thread, we have to go some re-gathering.
8083     (when (stringp (car thread))
8084       (let ((subject (car thread))
8085             roots thr)
8086         (setq thread (cdr thread))
8087         (while thread
8088           (unless (memq (setq thr (gnus-id-to-thread
8089                                       (gnus-root-id
8090                                        (mail-header-id (caar thread)))))
8091                         roots)
8092             (push thr roots))
8093           (setq thread (cdr thread)))
8094         ;; We now have all (unique) roots.
8095         (if (= (length roots) 1)
8096             ;; All the loose roots are now one solid root.
8097             (setq thread (car roots))
8098           (setq thread (cons subject (gnus-sort-threads roots))))))
8099     (let (threads)
8100       ;; We then insert this thread into the summary buffer.
8101       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8102         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8103         (setq data (nreverse gnus-newsgroup-data))
8104         (setq threads gnus-newsgroup-threads))
8105       ;; We splice the new data into the data structure.
8106       (gnus-data-enter-list current data)
8107       (gnus-data-compute-positions)
8108       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8109
8110 (defun gnus-id-to-thread (id)
8111   "Return the (sub-)thread where ID appears."
8112   (gnus-gethash id gnus-newsgroup-dependencies))
8113
8114 (defun gnus-id-to-article (id)
8115   "Return the article number of ID."
8116   (let ((thread (gnus-id-to-thread id)))
8117     (when (and thread
8118                (car thread))
8119       (mail-header-number (car thread)))))
8120
8121 (defun gnus-id-to-header (id)
8122   "Return the article headers of ID."
8123   (car (gnus-id-to-thread id)))
8124
8125 (defun gnus-article-displayed-root-p (article)
8126   "Say whether ARTICLE is a root(ish) article."
8127   (let ((level (gnus-summary-thread-level article))
8128         (refs (mail-header-references  (gnus-summary-article-header article)))
8129         particle)
8130     (cond 
8131      ((null level) nil)
8132      ((zerop level) t)
8133      ((null refs) t)
8134      ((null (gnus-parent-id refs)) t)
8135      ((and (= 1 level)
8136            (null (setq particle (gnus-id-to-article
8137                                  (gnus-parent-id refs))))
8138            (null (gnus-summary-thread-level particle)))))))
8139
8140 (defun gnus-root-id (id)
8141   "Return the id of the root of the thread where ID appears."
8142   (let (last-id prev)
8143     (while (and id (setq prev (car (gnus-gethash 
8144                                     id gnus-newsgroup-dependencies))))
8145       (setq last-id id
8146             id (gnus-parent-id (mail-header-references prev))))
8147     last-id))
8148
8149 (defun gnus-remove-thread (id &optional dont-remove)
8150   "Remove the thread that has ID in it."
8151   (let ((dep gnus-newsgroup-dependencies)
8152         headers thread last-id)
8153     ;; First go up in this thread until we find the root.
8154     (setq last-id (gnus-root-id id))
8155     (setq headers (list (car (gnus-id-to-thread last-id))
8156                         (caadr (gnus-id-to-thread last-id))))
8157     ;; We have now found the real root of this thread.  It might have
8158     ;; been gathered into some loose thread, so we have to search
8159     ;; through the threads to find the thread we wanted.
8160     (let ((threads gnus-newsgroup-threads)
8161           sub)
8162       (while threads
8163         (setq sub (car threads))
8164         (if (stringp (car sub))
8165             ;; This is a gathered threads, so we look at the roots
8166             ;; below it to find whether this article in in this
8167             ;; gathered root.
8168             (progn
8169               (setq sub (cdr sub))
8170               (while sub
8171                 (when (member (caar sub) headers)
8172                   (setq thread (car threads)
8173                         threads nil
8174                         sub nil))
8175                 (setq sub (cdr sub))))
8176           ;; It's an ordinary thread, so we check it.
8177           (when (eq (car sub) (car headers))
8178             (setq thread sub
8179                   threads nil)))
8180         (setq threads (cdr threads)))
8181       ;; If this article is in no thread, then it's a root.
8182       (if thread
8183           (unless dont-remove
8184             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8185         (setq thread (gnus-gethash last-id dep)))
8186       (when thread
8187         (prog1
8188             thread ; We return this thread.
8189           (unless dont-remove
8190             (if (stringp (car thread))
8191                 (progn
8192                   ;; If we use dummy roots, then we have to remove the
8193                   ;; dummy root as well.
8194                   (when (eq gnus-summary-make-false-root 'dummy)
8195                     ;; Uhm.
8196                     )
8197                   (setq thread (cdr thread))
8198                   (while thread
8199                     (gnus-remove-thread-1 (car thread))
8200                     (setq thread (cdr thread))))
8201               (gnus-remove-thread-1 thread))))))))
8202
8203 (defun gnus-remove-thread-1 (thread)
8204   "Remove the thread THREAD recursively."
8205   (let ((number (mail-header-number (car thread)))
8206         pos)
8207     (when (setq pos (text-property-any
8208                      (point-min) (point-max) 'gnus-number number))
8209       (goto-char pos)
8210       (gnus-delete-line)
8211       (gnus-data-remove number))
8212     (setq thread (cdr thread))
8213     (while thread
8214       (gnus-remove-thread-1 (pop thread)))))
8215
8216 (defun gnus-sort-threads (threads)
8217   "Sort THREADS."
8218   (if (not gnus-thread-sort-functions)
8219       threads
8220     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8221                     (car gnus-thread-sort-functions)
8222                   `(lambda (t1 t2)
8223                      ,(gnus-make-sort-function 
8224                        (reverse gnus-thread-sort-functions))))))
8225       (gnus-message 7 "Sorting threads...")
8226       (prog1
8227           (sort threads func)
8228         (gnus-message 7 "Sorting threads...done")))))
8229
8230 (defun gnus-sort-articles (articles)
8231   "Sort ARTICLES."
8232   (when gnus-article-sort-functions
8233     (let ((func (if (= 1 (length gnus-article-sort-functions))
8234                     (car gnus-article-sort-functions)
8235                   `(lambda (t1 t2)
8236                      ,(gnus-make-sort-function 
8237                        (reverse gnus-article-sort-functions))))))
8238       (gnus-message 7 "Sorting articles...")
8239       (prog1
8240           (setq gnus-newsgroup-headers (sort articles func))
8241         (gnus-message 7 "Sorting articles...done")))))
8242
8243 (defun gnus-make-sort-function (funs)
8244   "Return a composite sort condition based on the functions in FUNC."
8245   (if (cdr funs)
8246       `(or (,(car funs) t1 t2)
8247            (and (not (,(car funs) t2 t1))
8248                 ,(gnus-make-sort-function (cdr funs))))
8249     `(,(car funs) t1 t2)))
8250                  
8251 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8252 (defmacro gnus-thread-header (thread)
8253   ;; Return header of first article in THREAD.
8254   ;; Note that THREAD must never, ever be anything else than a variable -
8255   ;; using some other form will lead to serious barfage.
8256   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8257   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8258   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8259         (vector thread) 2))
8260
8261 (defsubst gnus-article-sort-by-number (h1 h2)
8262   "Sort articles by article number."
8263   (< (mail-header-number h1)
8264      (mail-header-number h2)))
8265
8266 (defun gnus-thread-sort-by-number (h1 h2)
8267   "Sort threads by root article number."
8268   (gnus-article-sort-by-number
8269    (gnus-thread-header h1) (gnus-thread-header h2)))
8270
8271 (defsubst gnus-article-sort-by-author (h1 h2)
8272   "Sort articles by root author."
8273   (string-lessp
8274    (let ((extract (funcall
8275                    gnus-extract-address-components
8276                    (mail-header-from h1))))
8277      (or (car extract) (cdr extract)))
8278    (let ((extract (funcall
8279                    gnus-extract-address-components
8280                    (mail-header-from h2))))
8281      (or (car extract) (cdr extract)))))
8282
8283 (defun gnus-thread-sort-by-author (h1 h2)
8284   "Sort threads by root author."
8285   (gnus-article-sort-by-author
8286    (gnus-thread-header h1)  (gnus-thread-header h2)))
8287
8288 (defsubst gnus-article-sort-by-subject (h1 h2)
8289   "Sort articles by root subject."
8290   (string-lessp
8291    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8292    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8293
8294 (defun gnus-thread-sort-by-subject (h1 h2)
8295   "Sort threads by root subject."
8296   (gnus-article-sort-by-subject
8297    (gnus-thread-header h1) (gnus-thread-header h2)))
8298
8299 (defsubst gnus-article-sort-by-date (h1 h2)
8300   "Sort articles by root article date."
8301   (string-lessp
8302    (inline (gnus-sortable-date (mail-header-date h1)))
8303    (inline (gnus-sortable-date (mail-header-date h2)))))
8304
8305 (defun gnus-thread-sort-by-date (h1 h2)
8306   "Sort threads by root article date."
8307   (gnus-article-sort-by-date
8308    (gnus-thread-header h1) (gnus-thread-header h2)))
8309
8310 (defsubst gnus-article-sort-by-score (h1 h2)
8311   "Sort articles by root article score.
8312 Unscored articles will be counted as having a score of zero."
8313   (> (or (cdr (assq (mail-header-number h1)
8314                     gnus-newsgroup-scored))
8315          gnus-summary-default-score 0)
8316      (or (cdr (assq (mail-header-number h2)
8317                     gnus-newsgroup-scored))
8318          gnus-summary-default-score 0)))
8319
8320 (defun gnus-thread-sort-by-score (h1 h2)
8321   "Sort threads by root article score."
8322   (gnus-article-sort-by-score
8323    (gnus-thread-header h1) (gnus-thread-header h2)))
8324
8325 (defun gnus-thread-sort-by-total-score (h1 h2)
8326   "Sort threads by the sum of all scores in the thread.
8327 Unscored articles will be counted as having a score of zero."
8328   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8329
8330 (defun gnus-thread-total-score (thread)
8331   ;;  This function find the total score of THREAD.
8332   (if (consp thread)
8333       (if (stringp (car thread))
8334           (apply gnus-thread-score-function 0
8335                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8336         (gnus-thread-total-score-1 thread))
8337     (gnus-thread-total-score-1 (list thread))))
8338
8339 (defun gnus-thread-total-score-1 (root)
8340   ;; This function find the total score of the thread below ROOT.
8341   (setq root (car root))
8342   (apply gnus-thread-score-function
8343          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8344              gnus-summary-default-score 0)
8345          (mapcar 'gnus-thread-total-score
8346                  (cdr (gnus-gethash (mail-header-id root)
8347                                     gnus-newsgroup-dependencies)))))
8348
8349 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8350 (defvar gnus-tmp-prev-subject nil)
8351 (defvar gnus-tmp-false-parent nil)
8352 (defvar gnus-tmp-root-expunged nil)
8353 (defvar gnus-tmp-dummy-line nil)
8354
8355 (defun gnus-summary-prepare-threads (threads)
8356   "Prepare summary buffer from THREADS and indentation LEVEL.
8357 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8358 or a straight list of headers."
8359   (gnus-message 7 "Generating summary...")
8360
8361   (setq gnus-newsgroup-threads threads)
8362   (beginning-of-line)
8363
8364   (let ((gnus-tmp-level 0)
8365         (default-score (or gnus-summary-default-score 0))
8366         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8367         thread number subject stack state gnus-tmp-gathered beg-match
8368         new-roots gnus-tmp-new-adopts thread-end
8369         gnus-tmp-header gnus-tmp-unread
8370         gnus-tmp-replied gnus-tmp-subject-or-nil
8371         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8372         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8373         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8374
8375     (setq gnus-tmp-prev-subject nil)
8376
8377     (if (vectorp (car threads))
8378         ;; If this is a straight (sic) list of headers, then a
8379         ;; threaded summary display isn't required, so we just create
8380         ;; an unthreaded one.
8381         (gnus-summary-prepare-unthreaded threads)
8382
8383       ;; Do the threaded display.
8384
8385       (while (or threads stack gnus-tmp-new-adopts new-roots)
8386
8387         (if (and (= gnus-tmp-level 0)
8388                  (not (setq gnus-tmp-dummy-line nil))
8389                  (or (not stack)
8390                      (= (caar stack) 0))
8391                  (not gnus-tmp-false-parent)
8392                  (or gnus-tmp-new-adopts new-roots))
8393             (if gnus-tmp-new-adopts
8394                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8395                       thread (list (car gnus-tmp-new-adopts))
8396                       gnus-tmp-header (caar thread)
8397                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8398               (if new-roots
8399                   (setq thread (list (car new-roots))
8400                         gnus-tmp-header (caar thread)
8401                         new-roots (cdr new-roots))))
8402
8403           (if threads
8404               ;; If there are some threads, we do them before the
8405               ;; threads on the stack.
8406               (setq thread threads
8407                     gnus-tmp-header (caar thread))
8408             ;; There were no current threads, so we pop something off
8409             ;; the stack.
8410             (setq state (car stack)
8411                   gnus-tmp-level (car state)
8412                   thread (cdr state)
8413                   stack (cdr stack)
8414                   gnus-tmp-header (caar thread))))
8415
8416         (setq gnus-tmp-false-parent nil)
8417         (setq gnus-tmp-root-expunged nil)
8418         (setq thread-end nil)
8419
8420         (if (stringp gnus-tmp-header)
8421             ;; The header is a dummy root.
8422             (cond
8423              ((eq gnus-summary-make-false-root 'adopt)
8424               ;; We let the first article adopt the rest.
8425               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8426                                                (cddar thread)))
8427               (setq gnus-tmp-gathered
8428                     (nconc (mapcar
8429                             (lambda (h) (mail-header-number (car h)))
8430                             (cddar thread))
8431                            gnus-tmp-gathered))
8432               (setq thread (cons (list (caar thread)
8433                                        (cadar thread))
8434                                  (cdr thread)))
8435               (setq gnus-tmp-level -1
8436                     gnus-tmp-false-parent t))
8437              ((eq gnus-summary-make-false-root 'empty)
8438               ;; We print adopted articles with empty subject fields.
8439               (setq gnus-tmp-gathered
8440                     (nconc (mapcar
8441                             (lambda (h) (mail-header-number (car h)))
8442                             (cddar thread))
8443                            gnus-tmp-gathered))
8444               (setq gnus-tmp-level -1))
8445              ((eq gnus-summary-make-false-root 'dummy)
8446               ;; We remember that we probably want to output a dummy
8447               ;; root.
8448               (setq gnus-tmp-dummy-line gnus-tmp-header)
8449               (setq gnus-tmp-prev-subject gnus-tmp-header))
8450              (t
8451               ;; We do not make a root for the gathered
8452               ;; sub-threads at all.
8453               (setq gnus-tmp-level -1)))
8454
8455           (setq number (mail-header-number gnus-tmp-header)
8456                 subject (mail-header-subject gnus-tmp-header))
8457
8458           (cond
8459            ;; If the thread has changed subject, we might want to make
8460            ;; this subthread into a root.
8461            ((and (null gnus-thread-ignore-subject)
8462                  (not (zerop gnus-tmp-level))
8463                  gnus-tmp-prev-subject
8464                  (not (inline
8465                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8466             (setq new-roots (nconc new-roots (list (car thread)))
8467                   thread-end t
8468                   gnus-tmp-header nil))
8469            ;; If the article lies outside the current limit,
8470            ;; then we do not display it.
8471            ((and (not (memq number gnus-newsgroup-limit))
8472                  (not gnus-tmp-dummy-line))
8473             (setq gnus-tmp-gathered
8474                   (nconc (mapcar
8475                           (lambda (h) (mail-header-number (car h)))
8476                           (cdar thread))
8477                          gnus-tmp-gathered))
8478             (setq gnus-tmp-new-adopts (if (cdar thread)
8479                                           (append gnus-tmp-new-adopts
8480                                                   (cdar thread))
8481                                         gnus-tmp-new-adopts)
8482                   thread-end t
8483                   gnus-tmp-header nil)
8484             (when (zerop gnus-tmp-level)
8485               (setq gnus-tmp-root-expunged t)))
8486            ;; Perhaps this article is to be marked as read?
8487            ((and gnus-summary-mark-below
8488                  (< (or (cdr (assq number gnus-newsgroup-scored))
8489                         default-score)
8490                     gnus-summary-mark-below)
8491                  ;; Don't touch sparse articles.
8492                  (not (memq number gnus-newsgroup-sparse))
8493                  (not (memq number gnus-newsgroup-ancient)))
8494             (setq gnus-newsgroup-unreads
8495                   (delq number gnus-newsgroup-unreads))
8496             (if gnus-newsgroup-auto-expire
8497                 (push number gnus-newsgroup-expirable)
8498               (push (cons number gnus-low-score-mark)
8499                     gnus-newsgroup-reads))))
8500
8501           (when gnus-tmp-header
8502             ;; We may have an old dummy line to output before this
8503             ;; article.
8504             (when gnus-tmp-dummy-line
8505               (gnus-summary-insert-dummy-line
8506                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8507               (setq gnus-tmp-dummy-line nil))
8508
8509             ;; Compute the mark.
8510             (setq
8511              gnus-tmp-unread
8512              (cond
8513               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8514               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8515               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8516               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8517               (t (or (cdr (assq number gnus-newsgroup-reads))
8518                      gnus-ancient-mark))))
8519
8520             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8521                                   gnus-tmp-header gnus-tmp-level)
8522                   gnus-newsgroup-data)
8523
8524             ;; Actually insert the line.
8525             (setq
8526              gnus-tmp-subject-or-nil
8527              (cond
8528               ((and gnus-thread-ignore-subject
8529                     gnus-tmp-prev-subject
8530                     (not (inline (gnus-subject-equal
8531                                   gnus-tmp-prev-subject subject))))
8532                subject)
8533               ((zerop gnus-tmp-level)
8534                (if (and (eq gnus-summary-make-false-root 'empty)
8535                         (memq number gnus-tmp-gathered)
8536                         gnus-tmp-prev-subject
8537                         (inline (gnus-subject-equal
8538                                  gnus-tmp-prev-subject subject)))
8539                    gnus-summary-same-subject
8540                  subject))
8541               (t gnus-summary-same-subject)))
8542             (if (and (eq gnus-summary-make-false-root 'adopt)
8543                      (= gnus-tmp-level 1)
8544                      (memq number gnus-tmp-gathered))
8545                 (setq gnus-tmp-opening-bracket ?\<
8546                       gnus-tmp-closing-bracket ?\>)
8547               (setq gnus-tmp-opening-bracket ?\[
8548                     gnus-tmp-closing-bracket ?\]))
8549             (setq
8550              gnus-tmp-indentation
8551              (aref gnus-thread-indent-array gnus-tmp-level)
8552              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8553              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8554                                 gnus-summary-default-score 0)
8555              gnus-tmp-score-char
8556              (if (or (null gnus-summary-default-score)
8557                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8558                          gnus-summary-zcore-fuzz)) ? 
8559                (if (< gnus-tmp-score gnus-summary-default-score)
8560                    gnus-score-below-mark gnus-score-over-mark))
8561              gnus-tmp-replied
8562              (cond ((memq number gnus-newsgroup-processable)
8563                     gnus-process-mark)
8564                    ((memq number gnus-newsgroup-cached)
8565                     gnus-cached-mark)
8566                    ((memq number gnus-newsgroup-replied)
8567                     gnus-replied-mark)
8568                    (t gnus-unread-mark))
8569              gnus-tmp-from (mail-header-from gnus-tmp-header)
8570              gnus-tmp-name
8571              (cond
8572               ((string-match "(.+)" gnus-tmp-from)
8573                (substring gnus-tmp-from
8574                           (1+ (match-beginning 0)) (1- (match-end 0))))
8575               ((string-match "<[^>]+> *$" gnus-tmp-from)
8576                (setq beg-match (match-beginning 0))
8577                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8578                         (substring gnus-tmp-from (1+ (match-beginning 0))
8579                                    (1- (match-end 0))))
8580                    (substring gnus-tmp-from 0 beg-match)))
8581               (t gnus-tmp-from)))
8582             (when (string= gnus-tmp-name "")
8583               (setq gnus-tmp-name gnus-tmp-from))
8584             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8585             (gnus-put-text-property
8586              (point)
8587              (progn (eval gnus-summary-line-format-spec) (point))
8588              'gnus-number number)
8589             (when gnus-visual-p
8590               (forward-line -1)
8591               (run-hooks 'gnus-summary-update-hook)
8592               (forward-line 1))
8593
8594             (setq gnus-tmp-prev-subject subject)))
8595
8596         (when (nth 1 thread)
8597           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8598         (incf gnus-tmp-level)
8599         (setq threads (if thread-end nil (cdar thread)))
8600         (unless threads
8601           (setq gnus-tmp-level 0)))))
8602   (gnus-message 7 "Generating summary...done"))
8603
8604 (defun gnus-summary-prepare-unthreaded (headers)
8605   "Generate an unthreaded summary buffer based on HEADERS."
8606   (let (header number mark)
8607
8608     (while headers
8609       ;; We may have to root out some bad articles...
8610       (when (memq (setq number (mail-header-number
8611                                 (setq header (pop headers))))
8612                   gnus-newsgroup-limit)
8613         ;; Mark article as read when it has a low score.
8614         (when (and gnus-summary-mark-below
8615                    (< (or (cdr (assq number gnus-newsgroup-scored))
8616                           gnus-summary-default-score 0)
8617                       gnus-summary-mark-below)
8618                    (not (memq number gnus-newsgroup-ancient)))
8619           (setq gnus-newsgroup-unreads
8620                 (delq number gnus-newsgroup-unreads))
8621           (if gnus-newsgroup-auto-expire
8622               (push number gnus-newsgroup-expirable)
8623             (push (cons number gnus-low-score-mark)
8624                   gnus-newsgroup-reads)))
8625
8626         (setq mark
8627               (cond
8628                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8629                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8630                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8631                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8632                (t (or (cdr (assq number gnus-newsgroup-reads))
8633                       gnus-ancient-mark))))
8634         (setq gnus-newsgroup-data
8635               (cons (gnus-data-make number mark (1+ (point)) header 0)
8636                     gnus-newsgroup-data))
8637         (gnus-summary-insert-line
8638          header 0 nil mark (memq number gnus-newsgroup-replied)
8639          (memq number gnus-newsgroup-expirable)
8640          (mail-header-subject header) nil
8641          (cdr (assq number gnus-newsgroup-scored))
8642          (memq number gnus-newsgroup-processable))))))
8643
8644 (defun gnus-select-newsgroup (group &optional read-all)
8645   "Select newsgroup GROUP.
8646 If READ-ALL is non-nil, all articles in the group are selected."
8647   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8648          (info (nth 2 entry))
8649          articles fetched-articles cached)
8650
8651     (or (gnus-check-server
8652          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8653         (error "Couldn't open server"))
8654
8655     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8656         (gnus-activate-group group)     ; Or we can activate it...
8657         (progn                          ; Or we bug out.
8658           (when (equal major-mode 'gnus-summary-mode)
8659             (kill-buffer (current-buffer)))
8660           (error "Couldn't request group %s: %s"
8661                  group (gnus-status-message group))))
8662
8663     (unless (gnus-request-group group t)
8664       (when (equal major-mode 'gnus-summary-mode)
8665         (kill-buffer (current-buffer)))
8666       (error "Couldn't request group %s: %s"
8667              group (gnus-status-message group)))      
8668
8669     (setq gnus-newsgroup-name group)
8670     (setq gnus-newsgroup-unselected nil)
8671     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8672
8673     (and gnus-asynchronous
8674          (gnus-check-backend-function
8675           'request-asynchronous gnus-newsgroup-name)
8676          (setq gnus-newsgroup-async
8677                (gnus-request-asynchronous gnus-newsgroup-name)))
8678
8679     ;; Adjust and set lists of article marks.
8680     (when info
8681       (gnus-adjust-marked-articles info))
8682
8683     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8684     (when (gnus-virtual-group-p group)
8685       (setq cached gnus-newsgroup-cached))
8686
8687     (setq gnus-newsgroup-unreads
8688           (gnus-set-difference
8689            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8690            gnus-newsgroup-dormant))
8691
8692     (setq gnus-newsgroup-processable nil)
8693
8694     (setq articles (gnus-articles-to-read group read-all))
8695
8696     (cond
8697      ((null articles)
8698       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8699       'quit)
8700      ((eq articles 0) nil)
8701      (t
8702       ;; Init the dependencies hash table.
8703       (setq gnus-newsgroup-dependencies
8704             (gnus-make-hashtable (length articles)))
8705       ;; Retrieve the headers and read them in.
8706       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8707       (setq gnus-newsgroup-headers
8708             (if (eq 'nov
8709                     (setq gnus-headers-retrieved-by
8710                           (gnus-retrieve-headers
8711                            articles gnus-newsgroup-name
8712                            ;; We might want to fetch old headers, but
8713                            ;; not if there is only 1 article.
8714                            (and gnus-fetch-old-headers
8715                                 (or (and
8716                                      (not (eq gnus-fetch-old-headers 'some))
8717                                      (not (numberp gnus-fetch-old-headers)))
8718                                     (> (length articles) 1))))))
8719                 (gnus-get-newsgroup-headers-xover articles)
8720               (gnus-get-newsgroup-headers)))
8721       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8722
8723       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8724       (when cached
8725         (setq gnus-newsgroup-cached cached))
8726
8727       ;; Set the initial limit.
8728       (setq gnus-newsgroup-limit (copy-sequence articles))
8729       ;; Remove canceled articles from the list of unread articles.
8730       (setq gnus-newsgroup-unreads
8731             (gnus-set-sorted-intersection
8732              gnus-newsgroup-unreads
8733              (setq fetched-articles
8734                    (mapcar (lambda (headers) (mail-header-number headers))
8735                            gnus-newsgroup-headers))))
8736       ;; Removed marked articles that do not exist.
8737       (gnus-update-missing-marks
8738        (gnus-sorted-complement fetched-articles articles))
8739       ;; We might want to build some more threads first.
8740       (and gnus-fetch-old-headers
8741            (eq gnus-headers-retrieved-by 'nov)
8742            (gnus-build-old-threads))
8743       ;; Check whether auto-expire is to be done in this group.
8744       (setq gnus-newsgroup-auto-expire
8745             (gnus-group-auto-expirable-p group))
8746       ;; Set up the article buffer now, if necessary.
8747       (unless gnus-single-article-buffer
8748         (gnus-article-setup-buffer))
8749       ;; First and last article in this newsgroup.
8750       (and gnus-newsgroup-headers
8751            (setq gnus-newsgroup-begin
8752                  (mail-header-number (car gnus-newsgroup-headers)))
8753            (setq gnus-newsgroup-end
8754                  (mail-header-number
8755                   (gnus-last-element gnus-newsgroup-headers))))
8756       (setq gnus-reffed-article-number -1)
8757       ;; GROUP is successfully selected.
8758       (or gnus-newsgroup-headers t)))))
8759
8760 (defun gnus-articles-to-read (group read-all)
8761   ;; Find out what articles the user wants to read.
8762   (let* ((articles
8763           ;; Select all articles if `read-all' is non-nil, or if there
8764           ;; are no unread articles.
8765           (if (or read-all
8766                   (and (zerop (length gnus-newsgroup-marked))
8767                        (zerop (length gnus-newsgroup-unreads))))
8768               (gnus-uncompress-range (gnus-active group))
8769             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8770                           (copy-sequence gnus-newsgroup-unreads))
8771                   '<)))
8772          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8773          (scored (length scored-list))
8774          (number (length articles))
8775          (marked (+ (length gnus-newsgroup-marked)
8776                     (length gnus-newsgroup-dormant)))
8777          (select
8778           (cond
8779            ((numberp read-all)
8780             read-all)
8781            (t
8782             (condition-case ()
8783                 (cond
8784                  ((and (or (<= scored marked) (= scored number))
8785                        (numberp gnus-large-newsgroup)
8786                        (> number gnus-large-newsgroup))
8787                   (let ((input
8788                          (read-string
8789                           (format
8790                            "How many articles from %s (default %d): "
8791                            gnus-newsgroup-name number))))
8792                     (if (string-match "^[ \t]*$" input) number input)))
8793                  ((and (> scored marked) (< scored number))
8794                   (let ((input
8795                          (read-string
8796                           (format "%s %s (%d scored, %d total): "
8797                                   "How many articles from"
8798                                   group scored number))))
8799                     (if (string-match "^[ \t]*$" input)
8800                         number input)))
8801                  (t number))
8802               (quit nil))))))
8803     (setq select (if (stringp select) (string-to-number select) select))
8804     (if (or (null select) (zerop select))
8805         select
8806       (if (and (not (zerop scored)) (<= (abs select) scored))
8807           (progn
8808             (setq articles (sort scored-list '<))
8809             (setq number (length articles)))
8810         (setq articles (copy-sequence articles)))
8811
8812       (if (< (abs select) number)
8813           (if (< select 0)
8814               ;; Select the N oldest articles.
8815               (setcdr (nthcdr (1- (abs select)) articles) nil)
8816             ;; Select the N most recent articles.
8817             (setq articles (nthcdr (- number select) articles))))
8818       (setq gnus-newsgroup-unselected
8819             (gnus-sorted-intersection
8820              gnus-newsgroup-unreads
8821              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8822       articles)))
8823
8824 (defun gnus-killed-articles (killed articles)
8825   (let (out)
8826     (while articles
8827       (if (inline (gnus-member-of-range (car articles) killed))
8828           (setq out (cons (car articles) out)))
8829       (setq articles (cdr articles)))
8830     out))
8831
8832 (defun gnus-uncompress-marks (marks)
8833   "Uncompress the mark ranges in MARKS."
8834   (let ((uncompressed '(score bookmark))
8835         out)
8836     (while marks
8837       (if (memq (caar marks) uncompressed)
8838           (push (car marks) out)
8839         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8840       (setq marks (cdr marks)))
8841     out))
8842
8843 (defun gnus-adjust-marked-articles (info)
8844   "Set all article lists and remove all marks that are no longer legal."
8845   (let* ((marked-lists (gnus-info-marks info))
8846          (active (gnus-active (gnus-info-group info)))
8847          (min (car active))
8848          (max (cdr active))
8849          (types gnus-article-mark-lists)
8850          (uncompressed '(score bookmark))
8851          marks var articles article mark)
8852
8853     (while marked-lists
8854       (setq marks (pop marked-lists))
8855       (set (setq var (intern (format "gnus-newsgroup-%s"
8856                                      (car (rassq (setq mark (car marks))
8857                                                  types)))))
8858            (if (memq (car marks) uncompressed) (cdr marks)
8859              (gnus-uncompress-range (cdr marks))))
8860
8861       (setq articles (symbol-value var))
8862
8863       ;; All articles have to be subsets of the active articles.
8864       (cond
8865        ;; Adjust "simple" lists.
8866        ((memq mark '(tick dormant expirable reply killed save))
8867         (while articles
8868           (when (or (< (setq article (pop articles)) min) (> article max))
8869             (set var (delq article (symbol-value var))))))
8870        ;; Adjust assocs.
8871        ((memq mark '(score bookmark))
8872         (while articles
8873           (when (or (< (car (setq article (pop articles))) min)
8874                     (> (car article) max))
8875             (set var (delq article (symbol-value var))))))))))
8876
8877 (defun gnus-update-missing-marks (missing)
8878   "Go through the list of MISSING articles and remove them mark lists."
8879   (when missing
8880     (let ((types gnus-article-mark-lists)
8881           var m)
8882       ;; Go through all types.
8883       (while types
8884         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8885         (when (symbol-value var)
8886           ;; This list has articles.  So we delete all missing articles
8887           ;; from it.
8888           (setq m missing)
8889           (while m
8890             (set var (delq (pop m) (symbol-value var)))))))))
8891
8892 (defun gnus-update-marks ()
8893   "Enter the various lists of marked articles into the newsgroup info list."
8894   (let ((types gnus-article-mark-lists)
8895         (info (gnus-get-info gnus-newsgroup-name))
8896         (uncompressed '(score bookmark killed))
8897         type list newmarked symbol)
8898     (when info
8899       ;; Add all marks lists that are non-nil to the list of marks lists.
8900       (while types
8901         (setq type (pop types))
8902         (when (setq list (symbol-value
8903                           (setq symbol
8904                                 (intern (format "gnus-newsgroup-%s"
8905                                                 (car type))))))
8906           (push (cons (cdr type)
8907                       (if (memq (cdr type) uncompressed) list
8908                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8909                 newmarked)))
8910
8911       ;; Enter these new marks into the info of the group.
8912       (if (nthcdr 3 info)
8913           (setcar (nthcdr 3 info) newmarked)
8914         ;; Add the marks lists to the end of the info.
8915         (when newmarked
8916           (setcdr (nthcdr 2 info) (list newmarked))))
8917
8918       ;; Cut off the end of the info if there's nothing else there.
8919       (let ((i 5))
8920         (while (and (> i 2)
8921                     (not (nth i info)))
8922           (when (nthcdr (decf i) info)
8923             (setcdr (nthcdr i info) nil)))))))
8924
8925 (defun gnus-add-marked-articles (group type articles &optional info force)
8926   ;; Add ARTICLES of TYPE to the info of GROUP.
8927   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8928   ;; add, but replace marked articles of TYPE with ARTICLES.
8929   (let ((info (or info (gnus-get-info group)))
8930         (uncompressed '(score bookmark killed))
8931         marked m)
8932     (or (not info)
8933         (and (not (setq marked (nthcdr 3 info)))
8934              (or (null articles)
8935                  (setcdr (nthcdr 2 info)
8936                          (list (list (cons type (gnus-compress-sequence
8937                                                  articles t)))))))
8938         (and (not (setq m (assq type (car marked))))
8939              (or (null articles)
8940                  (setcar marked
8941                          (cons (cons type (gnus-compress-sequence articles t) )
8942                                (car marked)))))
8943         (if force
8944             (if (null articles)
8945                 (setcar (nthcdr 3 info)
8946                         (delq (assq type (car marked)) (car marked)))
8947               (setcdr m (gnus-compress-sequence articles t)))
8948           (setcdr m (gnus-compress-sequence
8949                      (sort (nconc (gnus-uncompress-range (cdr m))
8950                                   (copy-sequence articles)) '<) t))))))
8951
8952 (defun gnus-set-mode-line (where)
8953   "This function sets the mode line of the article or summary buffers.
8954 If WHERE is `summary', the summary mode line format will be used."
8955   ;; Is this mode line one we keep updated?
8956   (when (memq where gnus-updated-mode-lines)
8957     (let (mode-string)
8958       (save-excursion
8959         ;; We evaluate this in the summary buffer since these
8960         ;; variables are buffer-local to that buffer.
8961         (set-buffer gnus-summary-buffer)
8962         ;; We bind all these variables that are used in the `eval' form
8963         ;; below.
8964         (let* ((mformat (symbol-value
8965                          (intern
8966                           (format "gnus-%s-mode-line-format-spec" where))))
8967                (gnus-tmp-group-name gnus-newsgroup-name)
8968                (gnus-tmp-article-number (or gnus-current-article 0))
8969                (gnus-tmp-unread gnus-newsgroup-unreads)
8970                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8971                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8972                (gnus-tmp-unread-and-unselected
8973                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8974                             (zerop gnus-tmp-unselected)) "")
8975                       ((zerop gnus-tmp-unselected)
8976                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8977                       (t (format "{%d(+%d) more}"
8978                                  gnus-tmp-unread-and-unticked
8979                                  gnus-tmp-unselected))))
8980                (gnus-tmp-subject
8981                 (if (and gnus-current-headers
8982                          (vectorp gnus-current-headers))
8983                     (gnus-mode-string-quote
8984                      (mail-header-subject gnus-current-headers)) ""))
8985                max-len
8986                gnus-tmp-header);; passed as argument to any user-format-funcs
8987           (setq mode-string (eval mformat))
8988           (setq max-len (max 4 (if gnus-mode-non-string-length
8989                                    (- (window-width)
8990                                       gnus-mode-non-string-length)
8991                                  (length mode-string))))
8992           ;; We might have to chop a bit of the string off...
8993           (when (> (length mode-string) max-len)
8994             (setq mode-string
8995                   (concat (gnus-truncate-string mode-string (- max-len 3))
8996                           "...")))
8997           ;; Pad the mode string a bit.
8998           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8999       ;; Update the mode line.
9000       (setq mode-line-buffer-identification (list mode-string))
9001       (set-buffer-modified-p t))))
9002
9003 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9004   "Go through the HEADERS list and add all Xrefs to a hash table.
9005 The resulting hash table is returned, or nil if no Xrefs were found."
9006   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9007          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9008          (xref-hashtb (make-vector 63 0))
9009          start group entry number xrefs header)
9010     (while headers
9011       (setq header (pop headers))
9012       (when (and (setq xrefs (mail-header-xref header))
9013                  (not (memq (setq number (mail-header-number header))
9014                             unreads)))
9015         (setq start 0)
9016         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9017           (setq start (match-end 0))
9018           (setq group (if prefix
9019                           (concat prefix (substring xrefs (match-beginning 1)
9020                                                     (match-end 1)))
9021                         (substring xrefs (match-beginning 1) (match-end 1))))
9022           (setq number
9023                 (string-to-int (substring xrefs (match-beginning 2)
9024                                           (match-end 2))))
9025           (if (setq entry (gnus-gethash group xref-hashtb))
9026               (setcdr entry (cons number (cdr entry)))
9027             (gnus-sethash group (cons number nil) xref-hashtb)))))
9028     (and start xref-hashtb)))
9029
9030 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9031   "Look through all the headers and mark the Xrefs as read."
9032   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9033         name entry info xref-hashtb idlist method nth4)
9034     (save-excursion
9035       (set-buffer gnus-group-buffer)
9036       (when (setq xref-hashtb
9037                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9038         (mapatoms
9039          (lambda (group)
9040            (unless (string= from-newsgroup (setq name (symbol-name group)))
9041              (setq idlist (symbol-value group))
9042              ;; Dead groups are not updated.
9043              (and (prog1
9044                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9045                             info (nth 2 entry))
9046                     (if (stringp (setq nth4 (gnus-info-method info)))
9047                         (setq nth4 (gnus-server-to-method nth4))))
9048                   ;; Only do the xrefs if the group has the same
9049                   ;; select method as the group we have just read.
9050                   (or (gnus-methods-equal-p
9051                        nth4 (gnus-find-method-for-group from-newsgroup))
9052                       virtual
9053                       (equal nth4 (setq method (gnus-find-method-for-group
9054                                                 from-newsgroup)))
9055                       (and (equal (car nth4) (car method))
9056                            (equal (nth 1 nth4) (nth 1 method))))
9057                   gnus-use-cross-reference
9058                   (or (not (eq gnus-use-cross-reference t))
9059                       virtual
9060                       ;; Only do cross-references on subscribed
9061                       ;; groups, if that is what is wanted.
9062                       (<= (gnus-info-level info) gnus-level-subscribed))
9063                   (gnus-group-make-articles-read name idlist))))
9064          xref-hashtb)))))
9065
9066 (defun gnus-group-make-articles-read (group articles)
9067   (let* ((num 0)
9068          (entry (gnus-gethash group gnus-newsrc-hashtb))
9069          (info (nth 2 entry))
9070          (active (gnus-active group))
9071          range)
9072     ;; First peel off all illegal article numbers.
9073     (if active
9074         (let ((ids articles)
9075               id first)
9076           (while ids
9077             (setq id (car ids))
9078             (if (and first (> id (cdr active)))
9079                 (progn
9080                   ;; We'll end up in this situation in one particular
9081                   ;; obscure situation.  If you re-scan a group and get
9082                   ;; a new article that is cross-posted to a different
9083                   ;; group that has not been re-scanned, you might get
9084                   ;; crossposted article that has a higher number than
9085                   ;; Gnus believes possible.  So we re-activate this
9086                   ;; group as well.  This might mean doing the
9087                   ;; crossposting thingy will *increase* the number
9088                   ;; of articles in some groups.  Tsk, tsk.
9089                   (setq active (or (gnus-activate-group group) active))))
9090             (if (or (> id (cdr active))
9091                     (< id (car active)))
9092                 (setq articles (delq id articles)))
9093             (setq ids (cdr ids)))))
9094     ;; If the read list is nil, we init it.
9095     (and active
9096          (null (gnus-info-read info))
9097          (> (car active) 1)
9098          (gnus-info-set-read info (cons 1 (1- (car active)))))
9099     ;; Then we add the read articles to the range.
9100     (gnus-info-set-read
9101      info
9102      (setq range
9103            (gnus-add-to-range
9104             (gnus-info-read info) (setq articles (sort articles '<)))))
9105     ;; Then we have to re-compute how many unread
9106     ;; articles there are in this group.
9107     (if active
9108         (progn
9109           (cond
9110            ((not range)
9111             (setq num (- (1+ (cdr active)) (car active))))
9112            ((not (listp (cdr range)))
9113             (setq num (- (cdr active) (- (1+ (cdr range))
9114                                          (car range)))))
9115            (t
9116             (while range
9117               (if (numberp (car range))
9118                   (setq num (1+ num))
9119                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9120               (setq range (cdr range)))
9121             (setq num (- (cdr active) num))))
9122           ;; Update the number of unread articles.
9123           (setcar entry num)
9124           ;; Update the group buffer.
9125           (gnus-group-update-group group t)))))
9126
9127 (defun gnus-methods-equal-p (m1 m2)
9128   (let ((m1 (or m1 gnus-select-method))
9129         (m2 (or m2 gnus-select-method)))
9130     (or (equal m1 m2)
9131         (and (eq (car m1) (car m2))
9132              (or (not (memq 'address (assoc (symbol-name (car m1))
9133                                             gnus-valid-select-methods)))
9134                  (equal (nth 1 m1) (nth 1 m2)))))))
9135
9136 (defsubst gnus-header-value ()
9137   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9138
9139 (defvar gnus-newsgroup-none-id 0)
9140
9141 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9142   (let ((cur nntp-server-buffer)
9143         (dependencies
9144          (or dependencies
9145              (save-excursion (set-buffer gnus-summary-buffer)
9146                              gnus-newsgroup-dependencies)))
9147         headers id id-dep ref-dep end ref)
9148     (save-excursion
9149       (set-buffer nntp-server-buffer)
9150       (run-hooks 'gnus-parse-headers-hook)
9151       (let ((case-fold-search t)
9152             in-reply-to header p lines)
9153         (goto-char (point-min))
9154         ;; Search to the beginning of the next header.  Error messages
9155         ;; do not begin with 2 or 3.
9156         (while (re-search-forward "^[23][0-9]+ " nil t)
9157           (setq id nil
9158                 ref nil)
9159           ;; This implementation of this function, with nine
9160           ;; search-forwards instead of the one re-search-forward and
9161           ;; a case (which basically was the old function) is actually
9162           ;; about twice as fast, even though it looks messier.  You
9163           ;; can't have everything, I guess.  Speed and elegance
9164           ;; doesn't always go hand in hand.
9165           (setq
9166            header
9167            (vector
9168             ;; Number.
9169             (prog1
9170                 (read cur)
9171               (end-of-line)
9172               (setq p (point))
9173               (narrow-to-region (point)
9174                                 (or (and (search-forward "\n.\n" nil t)
9175                                          (- (point) 2))
9176                                     (point))))
9177             ;; Subject.
9178             (progn
9179               (goto-char p)
9180               (if (search-forward "\nsubject: " nil t)
9181                   (gnus-header-value) "(none)"))
9182             ;; From.
9183             (progn
9184               (goto-char p)
9185               (if (search-forward "\nfrom: " nil t)
9186                   (gnus-header-value) "(nobody)"))
9187             ;; Date.
9188             (progn
9189               (goto-char p)
9190               (if (search-forward "\ndate: " nil t)
9191                   (gnus-header-value) ""))
9192             ;; Message-ID.
9193             (progn
9194               (goto-char p)
9195               (if (search-forward "\nmessage-id: " nil t)
9196                   (setq id (gnus-header-value))
9197                 ;; If there was no message-id, we just fake one to make
9198                 ;; subsequent routines simpler.
9199                 (setq id (concat "none+"
9200                                  (int-to-string
9201                                   (setq gnus-newsgroup-none-id
9202                                         (1+ gnus-newsgroup-none-id)))))))
9203             ;; References.
9204             (progn
9205               (goto-char p)
9206               (if (search-forward "\nreferences: " nil t)
9207                   (prog1
9208                       (gnus-header-value)
9209                     (setq end (match-end 0))
9210                     (save-excursion
9211                       (setq ref
9212                             (buffer-substring
9213                              (progn
9214                                (end-of-line)
9215                                (search-backward ">" end t)
9216                                (1+ (point)))
9217                              (progn
9218                                (search-backward "<" end t)
9219                                (point))))))
9220                 ;; Get the references from the in-reply-to header if there
9221                 ;; were no references and the in-reply-to header looks
9222                 ;; promising.
9223                 (if (and (search-forward "\nin-reply-to: " nil t)
9224                          (setq in-reply-to (gnus-header-value))
9225                          (string-match "<[^>]+>" in-reply-to))
9226                     (setq ref (substring in-reply-to (match-beginning 0)
9227                                          (match-end 0)))
9228                   (setq ref ""))))
9229             ;; Chars.
9230             0
9231             ;; Lines.
9232             (progn
9233               (goto-char p)
9234               (if (search-forward "\nlines: " nil t)
9235                   (if (numberp (setq lines (read cur)))
9236                       lines 0)
9237                 0))
9238             ;; Xref.
9239             (progn
9240               (goto-char p)
9241               (and (search-forward "\nxref: " nil t)
9242                    (gnus-header-value)))))
9243           ;; We do the threading while we read the headers.  The
9244           ;; message-id and the last reference are both entered into
9245           ;; the same hash table.  Some tippy-toeing around has to be
9246           ;; done in case an article has arrived before the article
9247           ;; which it refers to.
9248           (if (boundp (setq id-dep (intern id dependencies)))
9249               (if (and (car (symbol-value id-dep))
9250                        (not force-new))
9251                   ;; An article with this Message-ID has already
9252                   ;; been seen, so we ignore this one, except we add
9253                   ;; any additional Xrefs (in case the two articles
9254                   ;; came from different servers).
9255                   (progn
9256                     (mail-header-set-xref
9257                      (car (symbol-value id-dep))
9258                      (concat (or (mail-header-xref
9259                                   (car (symbol-value id-dep))) "")
9260                              (or (mail-header-xref header) "")))
9261                     (setq header nil))
9262                 (setcar (symbol-value id-dep) header))
9263             (set id-dep (list header)))
9264           (when header
9265             (if (boundp (setq ref-dep (intern ref dependencies)))
9266                 (setcdr (symbol-value ref-dep)
9267                         (nconc (cdr (symbol-value ref-dep))
9268                                (list (symbol-value id-dep))))
9269               (set ref-dep (list nil (symbol-value id-dep))))
9270             (setq headers (cons header headers)))
9271           (goto-char (point-max))
9272           (widen))
9273         (nreverse headers)))))
9274
9275 ;; The following macros and functions were written by Felix Lee
9276 ;; <flee@cse.psu.edu>.
9277
9278 (defmacro gnus-nov-read-integer ()
9279   '(prog1
9280        (if (= (following-char) ?\t)
9281            0
9282          (let ((num (condition-case nil (read buffer) (error nil))))
9283            (if (numberp num) num 0)))
9284      (or (eobp) (forward-char 1))))
9285
9286 (defmacro gnus-nov-skip-field ()
9287   '(search-forward "\t" eol 'move))
9288
9289 (defmacro gnus-nov-field ()
9290   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9291
9292 ;; Goes through the xover lines and returns a list of vectors
9293 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9294                                                   force-new dependencies)
9295   "Parse the news overview data in the server buffer, and return a
9296 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9297   ;; Get the Xref when the users reads the articles since most/some
9298   ;; NNTP servers do not include Xrefs when using XOVER.
9299   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9300   (let ((cur nntp-server-buffer)
9301         (dependencies (or dependencies gnus-newsgroup-dependencies))
9302         number headers header)
9303     (save-excursion
9304       (set-buffer nntp-server-buffer)
9305       ;; Allow the user to mangle the headers before parsing them.
9306       (run-hooks 'gnus-parse-headers-hook)
9307       (goto-char (point-min))
9308       (while (and sequence (not (eobp)))
9309         (setq number (read cur))
9310         (while (and sequence (< (car sequence) number))
9311           (setq sequence (cdr sequence)))
9312         (and sequence
9313              (eq number (car sequence))
9314              (progn
9315                (setq sequence (cdr sequence))
9316                (if (setq header
9317                          (inline (gnus-nov-parse-line
9318                                   number dependencies force-new)))
9319                    (setq headers (cons header headers)))))
9320         (forward-line 1))
9321       (setq headers (nreverse headers)))
9322     headers))
9323
9324 ;; This function has to be called with point after the article number
9325 ;; on the beginning of the line.
9326 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9327   (let ((none 0)
9328         (eol (gnus-point-at-eol))
9329         (buffer (current-buffer))
9330         header ref id id-dep ref-dep)
9331
9332     ;; overview: [num subject from date id refs chars lines misc]
9333     (narrow-to-region (point) eol)
9334     (or (eobp) (forward-char))
9335
9336     (condition-case nil
9337         (setq header
9338               (vector
9339                number                   ; number
9340                (gnus-nov-field)         ; subject
9341                (gnus-nov-field)         ; from
9342                (gnus-nov-field)         ; date
9343                (setq id (or (gnus-nov-field)
9344                             (concat "none+"
9345                                     (int-to-string
9346                                      (setq none (1+ none)))))) ; id
9347                (progn
9348                  (save-excursion
9349                    (let ((beg (point)))
9350                      (search-forward "\t" eol)
9351                      (if (search-backward ">" beg t)
9352                          (setq ref
9353                                (buffer-substring
9354                                 (1+ (point))
9355                                 (search-backward "<" beg t)))
9356                        (setq ref nil))))
9357                  (gnus-nov-field))      ; refs
9358                (gnus-nov-read-integer)  ; chars
9359                (gnus-nov-read-integer)  ; lines
9360                (if (= (following-char) ?\n)
9361                    nil
9362                  (gnus-nov-field))      ; misc
9363                ))
9364       (error (progn
9365                (gnus-error 4 "Strange nov line")
9366                (setq header nil)
9367                (goto-char eol))))
9368
9369     (widen)
9370
9371     ;; We build the thread tree.
9372     (when header
9373       (if (boundp (setq id-dep (intern id dependencies)))
9374           (if (and (car (symbol-value id-dep))
9375                    (not force-new))
9376               ;; An article with this Message-ID has already been seen,
9377               ;; so we ignore this one, except we add any additional
9378               ;; Xrefs (in case the two articles came from different
9379               ;; servers.
9380               (progn
9381                 (mail-header-set-xref
9382                  (car (symbol-value id-dep))
9383                  (concat (or (mail-header-xref
9384                               (car (symbol-value id-dep))) "")
9385                          (or (mail-header-xref header) "")))
9386                 (setq header nil))
9387             (setcar (symbol-value id-dep) header))
9388         (set id-dep (list header))))
9389     (if header
9390         (progn
9391           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9392               (setcdr (symbol-value ref-dep)
9393                       (nconc (cdr (symbol-value ref-dep))
9394                              (list (symbol-value id-dep))))
9395             (set ref-dep (list nil (symbol-value id-dep))))))
9396     header))
9397
9398 (defun gnus-article-get-xrefs ()
9399   "Fill in the Xref value in `gnus-current-headers', if necessary.
9400 This is meant to be called in `gnus-article-internal-prepare-hook'."
9401   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9402                                  gnus-current-headers)))
9403     (or (not gnus-use-cross-reference)
9404         (not headers)
9405         (and (mail-header-xref headers)
9406              (not (string= (mail-header-xref headers) "")))
9407         (let ((case-fold-search t)
9408               xref)
9409           (save-restriction
9410             (nnheader-narrow-to-headers)
9411             (goto-char (point-min))
9412             (if (or (and (eq (downcase (following-char)) ?x)
9413                          (looking-at "Xref:"))
9414                     (search-forward "\nXref:" nil t))
9415                 (progn
9416                   (goto-char (1+ (match-end 0)))
9417                   (setq xref (buffer-substring (point)
9418                                                (progn (end-of-line) (point))))
9419                   (mail-header-set-xref headers xref))))))))
9420
9421 (defun gnus-summary-insert-subject (id &optional old-header)
9422   "Find article ID and insert the summary line for that article."
9423   (let ((header (or old-header (gnus-read-header id)))
9424         (number (and (numberp id) id))
9425         pos)
9426     (when header
9427       ;; Rebuild the thread that this article is part of and go to the
9428       ;; article we have fetched.
9429       (when old-header
9430         (when (setq pos (text-property-any
9431                          (point-min) (point-max) 'gnus-number 
9432                          (mail-header-number old-header)))
9433           (goto-char pos)
9434           (gnus-delete-line)
9435           (gnus-data-remove (mail-header-number old-header))))
9436       (when old-header
9437         (mail-header-set-number header (mail-header-number old-header)))
9438       (setq gnus-newsgroup-sparse
9439             (delq (setq number (mail-header-number header)) 
9440                   gnus-newsgroup-sparse))
9441       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9442       (gnus-rebuild-thread (mail-header-id header))
9443       (gnus-summary-goto-subject number nil t))
9444     (when (and (numberp number)
9445                (> number 0))
9446       ;; We have to update the boundaries even if we can't fetch the
9447       ;; article if ID is a number -- so that the next `P' or `N'
9448       ;; command will fetch the previous (or next) article even
9449       ;; if the one we tried to fetch this time has been canceled.
9450       (and (> number gnus-newsgroup-end)
9451            (setq gnus-newsgroup-end number))
9452       (and (< number gnus-newsgroup-begin)
9453            (setq gnus-newsgroup-begin number))
9454       (setq gnus-newsgroup-unselected
9455             (delq number gnus-newsgroup-unselected)))
9456     ;; Report back a success?
9457     (and header (mail-header-number header))))
9458
9459 (defun gnus-summary-work-articles (n)
9460   "Return a list of articles to be worked upon.  The prefix argument,
9461 the list of process marked articles, and the current article will be
9462 taken into consideration."
9463   (cond
9464    ((and n (numberp n))
9465     ;; A numerical prefix has been given.
9466     (let ((backward (< n 0))
9467           (n (abs n))
9468           articles article)
9469       (save-excursion
9470         (while
9471             (and (> n 0)
9472                  (push (setq article (gnus-summary-article-number))
9473                        articles)
9474                  (if backward
9475                      (gnus-summary-find-prev nil article)
9476                    (gnus-summary-find-next nil article)))
9477           (decf n)))
9478       (nreverse articles)))
9479    ((and (boundp 'transient-mark-mode)
9480          transient-mark-mode
9481          mark-active)
9482     ;; Work on the region between point and mark.
9483     (let ((max (max (point) (mark)))
9484           articles article)
9485       (save-excursion
9486         (goto-char (min (point) (mark)))
9487         (while
9488             (and
9489              (push (setq article (gnus-summary-article-number)) articles)
9490              (gnus-summary-find-next nil article)
9491              (< (point) max)))
9492         (nreverse articles))))
9493    (gnus-newsgroup-processable
9494     ;; There are process-marked articles present.
9495     (reverse gnus-newsgroup-processable))
9496    (t
9497     ;; Just return the current article.
9498     (list (gnus-summary-article-number)))))
9499
9500 (defun gnus-summary-search-group (&optional backward use-level)
9501   "Search for next unread newsgroup.
9502 If optional argument BACKWARD is non-nil, search backward instead."
9503   (save-excursion
9504     (set-buffer gnus-group-buffer)
9505     (if (gnus-group-search-forward
9506          backward nil (if use-level (gnus-group-group-level) nil))
9507         (gnus-group-group-name))))
9508
9509 (defun gnus-summary-best-group (&optional exclude-group)
9510   "Find the name of the best unread group.
9511 If EXCLUDE-GROUP, do not go to this group."
9512   (save-excursion
9513     (set-buffer gnus-group-buffer)
9514     (save-excursion
9515       (gnus-group-best-unread-group exclude-group))))
9516
9517 (defun gnus-summary-find-next (&optional unread article backward)
9518   (if backward (gnus-summary-find-prev)
9519     (let* ((dummy (gnus-summary-article-intangible-p))
9520            (article (or article (gnus-summary-article-number)))
9521            (arts (gnus-data-find-list article))
9522            result)
9523       (when (and (not dummy)
9524                  (or (not gnus-summary-check-current)
9525                      (not unread)
9526                      (not (gnus-data-unread-p (car arts)))))
9527         (setq arts (cdr arts)))
9528       (when (setq result
9529                   (if unread
9530                       (progn
9531                         (while arts
9532                           (when (gnus-data-unread-p (car arts))
9533                             (setq result (car arts)
9534                                   arts nil))
9535                           (setq arts (cdr arts)))
9536                         result)
9537                     (car arts)))
9538         (goto-char (gnus-data-pos result))
9539         (gnus-data-number result)))))
9540
9541 (defun gnus-summary-find-prev (&optional unread article)
9542   (let* ((eobp (eobp))
9543          (article (or article (gnus-summary-article-number)))
9544          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9545          result)
9546     (when (and (not eobp)
9547                (or (not gnus-summary-check-current)
9548                    (not unread)
9549                    (not (gnus-data-unread-p (car arts)))))
9550       (setq arts (cdr arts)))
9551     (if (setq result
9552               (if unread
9553                   (progn
9554                     (while arts
9555                       (and (gnus-data-unread-p (car arts))
9556                            (setq result (car arts)
9557                                  arts nil))
9558                       (setq arts (cdr arts)))
9559                     result)
9560                 (car arts)))
9561         (progn
9562           (goto-char (gnus-data-pos result))
9563           (gnus-data-number result)))))
9564
9565 (defun gnus-summary-find-subject (subject &optional unread backward article)
9566   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9567          (article (or article (gnus-summary-article-number)))
9568          (articles (gnus-data-list backward))
9569          (arts (gnus-data-find-list article articles))
9570          result)
9571     (when (or (not gnus-summary-check-current)
9572               (not unread)
9573               (not (gnus-data-unread-p (car arts))))
9574       (setq arts (cdr arts)))
9575     (while arts
9576       (and (or (not unread)
9577                (gnus-data-unread-p (car arts)))
9578            (vectorp (gnus-data-header (car arts)))
9579            (gnus-subject-equal
9580             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9581            (setq result (car arts)
9582                  arts nil))
9583       (setq arts (cdr arts)))
9584     (and result
9585          (goto-char (gnus-data-pos result))
9586          (gnus-data-number result))))
9587
9588 (defun gnus-summary-search-forward (&optional unread subject backward)
9589   "Search forward for an article.
9590 If UNREAD, look for unread articles.  If SUBJECT, look for
9591 articles with that subject.  If BACKWARD, search backward instead."
9592   (cond (subject (gnus-summary-find-subject subject unread backward))
9593         (backward (gnus-summary-find-prev unread))
9594         (t (gnus-summary-find-next unread))))
9595
9596 (defun gnus-recenter (&optional n)
9597   "Center point in window and redisplay frame.
9598 Also do horizontal recentering."
9599   (interactive "P")
9600   (when (and gnus-auto-center-summary
9601              (not (eq gnus-auto-center-summary 'vertical)))
9602     (gnus-horizontal-recenter))
9603   (recenter n))
9604
9605 (defun gnus-summary-recenter ()
9606   "Center point in the summary window.
9607 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9608 displayed, no centering will be performed."
9609   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9610   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9611   (let* ((top (cond ((< (window-height) 4) 0)
9612                     ((< (window-height) 7) 1)
9613                     (t 2)))
9614          (height (1- (window-height)))
9615          (bottom (save-excursion (goto-char (point-max))
9616                                  (forward-line (- height))
9617                                  (point)))
9618          (window (get-buffer-window (current-buffer))))
9619     ;; The user has to want it.
9620     (when gnus-auto-center-summary
9621       (when (get-buffer-window gnus-article-buffer)
9622        ;; Only do recentering when the article buffer is displayed,
9623        ;; Set the window start to either `bottom', which is the biggest
9624        ;; possible valid number, or the second line from the top,
9625        ;; whichever is the least.
9626        (set-window-start
9627         window (min bottom (save-excursion 
9628                              (forward-line (- top)) (point)))))
9629       ;; Do horizontal recentering while we're at it.
9630       (when (and (get-buffer-window (current-buffer) t)
9631                  (not (eq gnus-auto-center-summary 'vertical)))
9632         (let ((selected (selected-window)))
9633           (select-window (get-buffer-window (current-buffer) t))
9634           (gnus-summary-position-point)
9635           (gnus-horizontal-recenter)
9636           (select-window selected))))))
9637
9638 (defun gnus-horizontal-recenter ()
9639   "Recenter the current buffer horizontally."
9640   (if (< (current-column) (/ (window-width) 2))
9641       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9642     (let* ((orig (point))
9643            (end (window-end (get-buffer-window (current-buffer) t)))
9644            (max 0))
9645       ;; Find the longest line currently displayed in the window.
9646       (goto-char (window-start))
9647       (while (and (not (eobp)) 
9648                   (< (point) end))
9649         (end-of-line)
9650         (setq max (max max (current-column)))
9651         (forward-line 1))
9652       (goto-char orig)
9653       ;; Scroll horizontally to center (sort of) the point.
9654       (if (> max (window-width))
9655           (set-window-hscroll 
9656            (get-buffer-window (current-buffer) t)
9657            (min (- (current-column) (/ (window-width) 3))
9658                 (+ 2 (- max (window-width)))))
9659         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9660       max)))
9661
9662 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9663 (defun gnus-short-group-name (group &optional levels)
9664   "Collapse GROUP name LEVELS."
9665   (let* ((name "") 
9666          (foreign "")
9667          (depth 0) 
9668          (skip 1)
9669          (levels (or levels
9670                      (progn
9671                        (while (string-match "\\." group skip)
9672                          (setq skip (match-end 0)
9673                                depth (+ depth 1)))
9674                        depth))))
9675     (if (string-match ":" group)
9676         (setq foreign (substring group 0 (match-end 0))
9677               group (substring group (match-end 0))))
9678     (while group
9679       (if (and (string-match "\\." group)
9680                (> levels (- gnus-group-uncollapsed-levels 1)))
9681           (setq name (concat name (substring group 0 1))
9682                 group (substring group (match-end 0))
9683                 levels (- levels 1)
9684                 name (concat name "."))
9685         (setq name (concat foreign name group)
9686               group nil)))
9687     name))
9688
9689 (defun gnus-summary-jump-to-group (newsgroup)
9690   "Move point to NEWSGROUP in group mode buffer."
9691   ;; Keep update point of group mode buffer if visible.
9692   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9693       (save-window-excursion
9694         ;; Take care of tree window mode.
9695         (if (get-buffer-window gnus-group-buffer)
9696             (pop-to-buffer gnus-group-buffer))
9697         (gnus-group-jump-to-group newsgroup))
9698     (save-excursion
9699       ;; Take care of tree window mode.
9700       (if (get-buffer-window gnus-group-buffer)
9701           (pop-to-buffer gnus-group-buffer)
9702         (set-buffer gnus-group-buffer))
9703       (gnus-group-jump-to-group newsgroup))))
9704
9705 ;; This function returns a list of article numbers based on the
9706 ;; difference between the ranges of read articles in this group and
9707 ;; the range of active articles.
9708 (defun gnus-list-of-unread-articles (group)
9709   (let* ((read (gnus-info-read (gnus-get-info group)))
9710          (active (gnus-active group))
9711          (last (cdr active))
9712          first nlast unread)
9713     ;; If none are read, then all are unread.
9714     (if (not read)
9715         (setq first (car active))
9716       ;; If the range of read articles is a single range, then the
9717       ;; first unread article is the article after the last read
9718       ;; article.  Sounds logical, doesn't it?
9719       (if (not (listp (cdr read)))
9720           (setq first (1+ (cdr read)))
9721         ;; `read' is a list of ranges.
9722         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9723                                 (caar read))) 1)
9724             (setq first 1))
9725         (while read
9726           (if first
9727               (while (< first nlast)
9728                 (setq unread (cons first unread))
9729                 (setq first (1+ first))))
9730           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9731           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9732           (setq read (cdr read)))))
9733     ;; And add the last unread articles.
9734     (while (<= first last)
9735       (setq unread (cons first unread))
9736       (setq first (1+ first)))
9737     ;; Return the list of unread articles.
9738     (nreverse unread)))
9739
9740 (defun gnus-list-of-read-articles (group)
9741   "Return a list of unread, unticked and non-dormant articles."
9742   (let* ((info (gnus-get-info group))
9743          (marked (gnus-info-marks info))
9744          (active (gnus-active group)))
9745     (and info active
9746          (gnus-set-difference
9747           (gnus-sorted-complement
9748            (gnus-uncompress-range active)
9749            (gnus-list-of-unread-articles group))
9750           (append
9751            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9752            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9753
9754 ;; Various summary commands
9755
9756 (defun gnus-summary-universal-argument (arg)
9757   "Perform any operation on all articles that are process/prefixed."
9758   (interactive "P")
9759   (gnus-set-global-variables)
9760   (let ((articles (gnus-summary-work-articles arg))
9761         func article)
9762     (if (eq
9763          (setq
9764           func
9765           (key-binding
9766            (read-key-sequence
9767             (substitute-command-keys
9768              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9769              ))))
9770          'undefined)
9771         (gnus-error 1 "Undefined key")
9772       (save-excursion
9773         (while articles
9774           (gnus-summary-goto-subject (setq article (pop articles)))
9775           (command-execute func)
9776           (gnus-summary-remove-process-mark article)))))
9777   (gnus-summary-position-point))
9778
9779 (defun gnus-summary-toggle-truncation (&optional arg)
9780   "Toggle truncation of summary lines.
9781 With arg, turn line truncation on iff arg is positive."
9782   (interactive "P")
9783   (setq truncate-lines
9784         (if (null arg) (not truncate-lines)
9785           (> (prefix-numeric-value arg) 0)))
9786   (redraw-display))
9787
9788 (defun gnus-summary-reselect-current-group (&optional all rescan)
9789   "Exit and then reselect the current newsgroup.
9790 The prefix argument ALL means to select all articles."
9791   (interactive "P")
9792   (gnus-set-global-variables)
9793   (let ((current-subject (gnus-summary-article-number))
9794         (group gnus-newsgroup-name))
9795     (setq gnus-newsgroup-begin nil)
9796     (gnus-summary-exit)
9797     ;; We have to adjust the point of group mode buffer because the
9798     ;; current point was moved to the next unread newsgroup by
9799     ;; exiting.
9800     (gnus-summary-jump-to-group group)
9801     (when rescan
9802       (save-excursion
9803         (gnus-group-get-new-news-this-group 1)))
9804     (gnus-group-read-group all t)
9805     (gnus-summary-goto-subject current-subject)))
9806
9807 (defun gnus-summary-rescan-group (&optional all)
9808   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9809   (interactive "P")
9810   (gnus-summary-reselect-current-group all t))
9811
9812 (defun gnus-summary-update-info ()
9813   (let* ((group gnus-newsgroup-name))
9814     (when gnus-newsgroup-kill-headers
9815       (setq gnus-newsgroup-killed
9816             (gnus-compress-sequence
9817              (nconc
9818               (gnus-set-sorted-intersection
9819                (gnus-uncompress-range gnus-newsgroup-killed)
9820                (setq gnus-newsgroup-unselected
9821                      (sort gnus-newsgroup-unselected '<)))
9822               (setq gnus-newsgroup-unreads
9823                     (sort gnus-newsgroup-unreads '<))) t)))
9824     (unless (listp (cdr gnus-newsgroup-killed))
9825       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9826     (let ((headers gnus-newsgroup-headers))
9827       (run-hooks 'gnus-exit-group-hook)
9828       (unless gnus-save-score
9829         (setq gnus-newsgroup-scored nil))
9830       ;; Set the new ranges of read articles.
9831       (gnus-update-read-articles
9832        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9833       ;; Set the current article marks.
9834       (gnus-update-marks)
9835       ;; Do the cross-ref thing.
9836       (when gnus-use-cross-reference
9837         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9838       ;; Do adaptive scoring, and possibly save score files.
9839       (when gnus-newsgroup-adaptive
9840         (gnus-score-adaptive))
9841       (when gnus-use-scoring
9842         (gnus-score-save))
9843       ;; Do not switch windows but change the buffer to work.
9844       (set-buffer gnus-group-buffer)
9845       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9846           (gnus-group-update-group group)))))
9847
9848 (defun gnus-summary-exit (&optional temporary)
9849   "Exit reading current newsgroup, and then return to group selection mode.
9850 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9851   (interactive)
9852   (gnus-set-global-variables)
9853   (gnus-kill-save-kill-buffer)
9854   (let* ((group gnus-newsgroup-name)
9855          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9856          (mode major-mode)
9857          (buf (current-buffer)))
9858     (run-hooks 'gnus-summary-prepare-exit-hook)
9859     ;; If we have several article buffers, we kill them at exit.
9860     (unless gnus-single-article-buffer
9861       (gnus-kill-buffer gnus-original-article-buffer)
9862       (setq gnus-article-current nil))
9863     (when gnus-use-cache
9864       (gnus-cache-possibly-remove-articles)
9865       (gnus-cache-save-buffers))
9866     (when gnus-use-trees
9867       (gnus-tree-close group))
9868     ;; Make all changes in this group permanent.
9869     (unless quit-config
9870       (gnus-summary-update-info))
9871     (gnus-close-group group)
9872     ;; Make sure where I was, and go to next newsgroup.
9873     (set-buffer gnus-group-buffer)
9874     (unless quit-config
9875       (gnus-group-jump-to-group group))
9876     (run-hooks 'gnus-summary-exit-hook)
9877     (unless quit-config
9878       (gnus-group-next-unread-group 1))
9879     (if temporary
9880         nil                             ;Nothing to do.
9881       ;; If we have several article buffers, we kill them at exit.
9882       (unless gnus-single-article-buffer
9883         (gnus-kill-buffer gnus-article-buffer)
9884         (gnus-kill-buffer gnus-original-article-buffer)
9885         (setq gnus-article-current nil))
9886       (set-buffer buf)
9887       (if (not gnus-kill-summary-on-exit)
9888           (gnus-deaden-summary)
9889         ;; We set all buffer-local variables to nil.  It is unclear why
9890         ;; this is needed, but if we don't, buffer-local variables are
9891         ;; not garbage-collected, it seems.  This would the lead to en
9892         ;; ever-growing Emacs.
9893         (gnus-summary-clear-local-variables)
9894         (when (get-buffer gnus-article-buffer)
9895           (bury-buffer gnus-article-buffer))
9896         ;; We clear the global counterparts of the buffer-local
9897         ;; variables as well, just to be on the safe side.
9898         (gnus-configure-windows 'group 'force)
9899         (gnus-summary-clear-local-variables)
9900         ;; Return to group mode buffer.
9901         (if (eq mode 'gnus-summary-mode)
9902             (gnus-kill-buffer buf)))
9903       (setq gnus-current-select-method gnus-select-method)
9904       (pop-to-buffer gnus-group-buffer)
9905       ;; Clear the current group name.
9906       (if (not quit-config)
9907           (progn
9908             (gnus-group-jump-to-group group)
9909             (gnus-group-next-unread-group 1)
9910             (gnus-configure-windows 'group 'force))
9911         (if (not (buffer-name (car quit-config)))
9912             (gnus-configure-windows 'group 'force)
9913           (set-buffer (car quit-config))
9914           (and (eq major-mode 'gnus-summary-mode)
9915                (gnus-set-global-variables))
9916           (gnus-configure-windows (cdr quit-config))))
9917       (unless quit-config
9918         (setq gnus-newsgroup-name nil)))))
9919
9920 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9921 (defun gnus-summary-exit-no-update (&optional no-questions)
9922   "Quit reading current newsgroup without updating read article info."
9923   (interactive)
9924   (gnus-set-global-variables)
9925   (let* ((group gnus-newsgroup-name)
9926          (quit-config (gnus-group-quit-config group)))
9927     (when (or no-questions
9928               gnus-expert-user
9929               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9930       ;; If we have several article buffers, we kill them at exit.
9931       (unless gnus-single-article-buffer
9932         (gnus-kill-buffer gnus-article-buffer)
9933         (gnus-kill-buffer gnus-original-article-buffer)
9934         (setq gnus-article-current nil))
9935       (if (not gnus-kill-summary-on-exit)
9936           (gnus-deaden-summary)
9937         (gnus-close-group group)
9938         (gnus-summary-clear-local-variables)
9939         (set-buffer gnus-group-buffer)
9940         (gnus-summary-clear-local-variables)
9941         (when (get-buffer gnus-summary-buffer)
9942           (kill-buffer gnus-summary-buffer)))
9943       (unless gnus-single-article-buffer
9944         (setq gnus-article-current nil))
9945       (when gnus-use-trees
9946         (gnus-tree-close group))
9947       (when (get-buffer gnus-article-buffer)
9948         (bury-buffer gnus-article-buffer))
9949       ;; Return to the group buffer.
9950       (gnus-configure-windows 'group 'force)
9951       ;; Clear the current group name.
9952       (setq gnus-newsgroup-name nil)
9953       (when (equal (gnus-group-group-name) group)
9954         (gnus-group-next-unread-group 1))
9955       (when quit-config
9956         (if (not (buffer-name (car quit-config)))
9957             (gnus-configure-windows 'group 'force)
9958           (set-buffer (car quit-config))
9959           (when (eq major-mode 'gnus-summary-mode)
9960             (gnus-set-global-variables))
9961           (gnus-configure-windows (cdr quit-config)))))))
9962
9963 ;;; Dead summaries.
9964
9965 (defvar gnus-dead-summary-mode-map nil)
9966
9967 (if gnus-dead-summary-mode-map
9968     nil
9969   (setq gnus-dead-summary-mode-map (make-keymap))
9970   (suppress-keymap gnus-dead-summary-mode-map)
9971   (substitute-key-definition
9972    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9973   (let ((keys '("\C-d" "\r" "\177")))
9974     (while keys
9975       (define-key gnus-dead-summary-mode-map
9976         (pop keys) 'gnus-summary-wake-up-the-dead))))
9977
9978 (defvar gnus-dead-summary-mode nil
9979   "Minor mode for Gnus summary buffers.")
9980
9981 (defun gnus-dead-summary-mode (&optional arg)
9982   "Minor mode for Gnus summary buffers."
9983   (interactive "P")
9984   (when (eq major-mode 'gnus-summary-mode)
9985     (make-local-variable 'gnus-dead-summary-mode)
9986     (setq gnus-dead-summary-mode
9987           (if (null arg) (not gnus-dead-summary-mode)
9988             (> (prefix-numeric-value arg) 0)))
9989     (when gnus-dead-summary-mode
9990       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9991         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9992       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9993         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9994               minor-mode-map-alist)))))
9995
9996 (defun gnus-deaden-summary ()
9997   "Make the current summary buffer into a dead summary buffer."
9998   ;; Kill any previous dead summary buffer.
9999   (when (and gnus-dead-summary
10000              (buffer-name gnus-dead-summary))
10001     (save-excursion
10002       (set-buffer gnus-dead-summary)
10003       (when gnus-dead-summary-mode
10004         (kill-buffer (current-buffer)))))
10005   ;; Make this the current dead summary.
10006   (setq gnus-dead-summary (current-buffer))
10007   (gnus-dead-summary-mode 1)
10008   (let ((name (buffer-name)))
10009     (when (string-match "Summary" name)
10010       (rename-buffer
10011        (concat (substring name 0 (match-beginning 0)) "Dead "
10012                (substring name (match-beginning 0))) t))))
10013
10014 (defun gnus-kill-or-deaden-summary (buffer)
10015   "Kill or deaden the summary BUFFER."
10016   (when (and (buffer-name buffer)
10017              (not gnus-single-article-buffer))
10018     (save-excursion
10019       (set-buffer buffer)
10020       (gnus-kill-buffer gnus-article-buffer)
10021       (gnus-kill-buffer gnus-original-article-buffer)))
10022   (cond (gnus-kill-summary-on-exit
10023          (when (and gnus-use-trees
10024                     (and (get-buffer buffer)
10025                          (buffer-name (get-buffer buffer))))
10026            (save-excursion
10027              (set-buffer (get-buffer buffer))
10028              (gnus-tree-close gnus-newsgroup-name)))
10029          (gnus-kill-buffer buffer))
10030         ((and (get-buffer buffer)
10031               (buffer-name (get-buffer buffer)))
10032          (save-excursion
10033            (set-buffer buffer)
10034            (gnus-deaden-summary)))))
10035
10036 (defun gnus-summary-wake-up-the-dead (&rest args)
10037   "Wake up the dead summary buffer."
10038   (interactive)
10039   (gnus-dead-summary-mode -1)
10040   (let ((name (buffer-name)))
10041     (when (string-match "Dead " name)
10042       (rename-buffer
10043        (concat (substring name 0 (match-beginning 0))
10044                (substring name (match-end 0))) t)))
10045   (gnus-message 3 "This dead summary is now alive again"))
10046
10047 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10048 (defun gnus-summary-fetch-faq (&optional faq-dir)
10049   "Fetch the FAQ for the current group.
10050 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10051 in."
10052   (interactive
10053    (list
10054     (if current-prefix-arg
10055         (completing-read
10056          "Faq dir: " (and (listp gnus-group-faq-directory)
10057                           gnus-group-faq-directory)))))
10058   (let (gnus-faq-buffer)
10059     (and (setq gnus-faq-buffer
10060                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10061          (gnus-configure-windows 'summary-faq))))
10062
10063 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10064 (defun gnus-summary-describe-group (&optional force)
10065   "Describe the current newsgroup."
10066   (interactive "P")
10067   (gnus-group-describe-group force gnus-newsgroup-name))
10068
10069 (defun gnus-summary-describe-briefly ()
10070   "Describe summary mode commands briefly."
10071   (interactive)
10072   (gnus-message 6
10073                 (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")))
10074
10075 ;; Walking around group mode buffer from summary mode.
10076
10077 (defun gnus-summary-next-group (&optional no-article target-group backward)
10078   "Exit current newsgroup and then select next unread newsgroup.
10079 If prefix argument NO-ARTICLE is non-nil, no article is selected
10080 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10081 previous group instead."
10082   (interactive "P")
10083   (gnus-set-global-variables)
10084   (let ((current-group gnus-newsgroup-name)
10085         (current-buffer (current-buffer))
10086         entered)
10087     ;; First we semi-exit this group to update Xrefs and all variables.
10088     ;; We can't do a real exit, because the window conf must remain
10089     ;; the same in case the user is prompted for info, and we don't
10090     ;; want the window conf to change before that...
10091     (gnus-summary-exit t)
10092     (while (not entered)
10093       ;; Then we find what group we are supposed to enter.
10094       (set-buffer gnus-group-buffer)
10095       (gnus-group-jump-to-group current-group)
10096       (setq target-group
10097             (or target-group
10098                 (if (eq gnus-keep-same-level 'best)
10099                     (gnus-summary-best-group gnus-newsgroup-name)
10100                   (gnus-summary-search-group backward gnus-keep-same-level))))
10101       (if (not target-group)
10102           ;; There are no further groups, so we return to the group
10103           ;; buffer.
10104           (progn
10105             (gnus-message 5 "Returning to the group buffer")
10106             (setq entered t)
10107             (set-buffer current-buffer)
10108             (gnus-summary-exit))
10109         ;; We try to enter the target group.
10110         (gnus-group-jump-to-group target-group)
10111         (let ((unreads (gnus-group-group-unread)))
10112           (if (and (or (eq t unreads)
10113                        (and unreads (not (zerop unreads))))
10114                    (gnus-summary-read-group
10115                     target-group nil no-article current-buffer))
10116               (setq entered t)
10117             (setq current-group target-group
10118                   target-group nil)))))))
10119
10120 (defun gnus-summary-prev-group (&optional no-article)
10121   "Exit current newsgroup and then select previous unread newsgroup.
10122 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10123   (interactive "P")
10124   (gnus-summary-next-group no-article nil t))
10125
10126 ;; Walking around summary lines.
10127
10128 (defun gnus-summary-first-subject (&optional unread)
10129   "Go to the first unread subject.
10130 If UNREAD is non-nil, go to the first unread article.
10131 Returns the article selected or nil if there are no unread articles."
10132   (interactive "P")
10133   (prog1
10134       (cond
10135        ;; Empty summary.
10136        ((null gnus-newsgroup-data)
10137         (gnus-message 3 "No articles in the group")
10138         nil)
10139        ;; Pick the first article.
10140        ((not unread)
10141         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10142         (gnus-data-number (car gnus-newsgroup-data)))
10143        ;; No unread articles.
10144        ((null gnus-newsgroup-unreads)
10145         (gnus-message 3 "No more unread articles")
10146         nil)
10147        ;; Find the first unread article.
10148        (t
10149         (let ((data gnus-newsgroup-data))
10150           (while (and data
10151                       (not (gnus-data-unread-p (car data))))
10152             (setq data (cdr data)))
10153           (if data
10154               (progn
10155                 (goto-char (gnus-data-pos (car data)))
10156                 (gnus-data-number (car data)))))))
10157     (gnus-summary-position-point)))
10158
10159 (defun gnus-summary-next-subject (n &optional unread dont-display)
10160   "Go to next N'th summary line.
10161 If N is negative, go to the previous N'th subject line.
10162 If UNREAD is non-nil, only unread articles are selected.
10163 The difference between N and the actual number of steps taken is
10164 returned."
10165   (interactive "p")
10166   (let ((backward (< n 0))
10167         (n (abs n)))
10168     (while (and (> n 0)
10169                 (if backward
10170                     (gnus-summary-find-prev unread)
10171                   (gnus-summary-find-next unread)))
10172       (setq n (1- n)))
10173     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10174                                (if unread " unread" "")))
10175     (unless dont-display
10176       (gnus-summary-recenter)
10177       (gnus-summary-position-point))
10178     n))
10179
10180 (defun gnus-summary-next-unread-subject (n)
10181   "Go to next N'th unread summary line."
10182   (interactive "p")
10183   (gnus-summary-next-subject n t))
10184
10185 (defun gnus-summary-prev-subject (n &optional unread)
10186   "Go to previous N'th summary line.
10187 If optional argument UNREAD is non-nil, only unread article is selected."
10188   (interactive "p")
10189   (gnus-summary-next-subject (- n) unread))
10190
10191 (defun gnus-summary-prev-unread-subject (n)
10192   "Go to previous N'th unread summary line."
10193   (interactive "p")
10194   (gnus-summary-next-subject (- n) t))
10195
10196 (defun gnus-summary-goto-subject (article &optional force silent)
10197   "Go the subject line of ARTICLE.
10198 If FORCE, also allow jumping to articles not currently shown."
10199   (let ((b (point))
10200         (data (gnus-data-find article)))
10201     ;; We read in the article if we have to.
10202     (and (not data)
10203          force
10204          (gnus-summary-insert-subject article (and (vectorp force) force))
10205          (setq data (gnus-data-find article)))
10206     (goto-char b)
10207     (if (not data)
10208         (progn
10209           (unless silent
10210             (gnus-message 3 "Can't find article %d" article))
10211           nil)
10212       (goto-char (gnus-data-pos data))
10213       article)))
10214
10215 ;; Walking around summary lines with displaying articles.
10216
10217 (defun gnus-summary-expand-window (&optional arg)
10218   "Make the summary buffer take up the entire Emacs frame.
10219 Given a prefix, will force an `article' buffer configuration."
10220   (interactive "P")
10221   (gnus-set-global-variables)
10222   (if arg
10223       (gnus-configure-windows 'article 'force)
10224     (gnus-configure-windows 'summary 'force)))
10225
10226 (defun gnus-summary-display-article (article &optional all-header)
10227   "Display ARTICLE in article buffer."
10228   (gnus-set-global-variables)
10229   (if (null article)
10230       nil
10231     (prog1
10232         (if gnus-summary-display-article-function
10233             (funcall gnus-summary-display-article-function article all-header)
10234           (gnus-article-prepare article all-header))
10235       (run-hooks 'gnus-select-article-hook)
10236       (unless (zerop gnus-current-article)
10237         (gnus-summary-goto-subject gnus-current-article))
10238       (gnus-summary-recenter)
10239       (when gnus-use-trees
10240         (gnus-possibly-generate-tree article)
10241         (gnus-highlight-selected-tree article))
10242       ;; Successfully display article.
10243       (gnus-article-set-window-start
10244        (cdr (assq article gnus-newsgroup-bookmarks))))))
10245
10246 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10247   "Select the current article.
10248 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10249 non-nil, the article will be re-fetched even if it already present in
10250 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10251 be displayed."
10252   ;; Make sure we are in the summary buffer to work around bbdb bug.
10253   (unless (eq major-mode 'gnus-summary-mode)
10254     (set-buffer gnus-summary-buffer))
10255   (let ((article (or article (gnus-summary-article-number)))
10256         (all-headers (not (not all-headers))) ;Must be T or NIL.
10257         gnus-summary-display-article-function
10258         did)
10259     (and (not pseudo)
10260          (gnus-summary-article-pseudo-p article)
10261          (error "This is a pseudo-article."))
10262     (prog1
10263         (save-excursion
10264           (set-buffer gnus-summary-buffer)
10265           (if (or (and gnus-single-article-buffer
10266                        (or (null gnus-current-article)
10267                            (null gnus-article-current)
10268                            (null (get-buffer gnus-article-buffer))
10269                            (not (eq article (cdr gnus-article-current)))
10270                            (not (equal (car gnus-article-current)
10271                                        gnus-newsgroup-name))))
10272                   (and (not gnus-single-article-buffer)
10273                        (or (null gnus-current-article)
10274                            (not (eq gnus-current-article article))))
10275                   force)
10276               ;; The requested article is different from the current article.
10277               (prog1
10278                   (gnus-summary-display-article article all-headers)
10279                 (setq did article))
10280             (if (or all-headers gnus-show-all-headers)
10281                 (gnus-article-show-all-headers))
10282             'old))
10283       (if did
10284           (gnus-article-set-window-start
10285            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10286
10287 (defun gnus-summary-set-current-mark (&optional current-mark)
10288   "Obsolete function."
10289   nil)
10290
10291 (defun gnus-summary-next-article (&optional unread subject backward push)
10292   "Select the next article.
10293 If UNREAD, only unread articles are selected.
10294 If SUBJECT, only articles with SUBJECT are selected.
10295 If BACKWARD, the previous article is selected instead of the next."
10296   (interactive "P")
10297   (gnus-set-global-variables)
10298   (cond
10299    ;; Is there such an article?
10300    ((and (gnus-summary-search-forward unread subject backward)
10301          (or (gnus-summary-display-article (gnus-summary-article-number))
10302              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10303     (gnus-summary-position-point))
10304    ;; If not, we try the first unread, if that is wanted.
10305    ((and subject
10306          gnus-auto-select-same
10307          (or (gnus-summary-first-unread-article)
10308              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10309     (gnus-summary-position-point)
10310     (gnus-message 6 "Wrapped"))
10311    ;; Try to get next/previous article not displayed in this group.
10312    ((and gnus-auto-extend-newsgroup
10313          (not unread) (not subject))
10314     (gnus-summary-goto-article
10315      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10316      nil t))
10317    ;; Go to next/previous group.
10318    (t
10319     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10320         (gnus-summary-jump-to-group gnus-newsgroup-name))
10321     (let ((cmd last-command-char)
10322           (group
10323            (if (eq gnus-keep-same-level 'best)
10324                (gnus-summary-best-group gnus-newsgroup-name)
10325              (gnus-summary-search-group backward gnus-keep-same-level))))
10326       ;; For some reason, the group window gets selected.  We change
10327       ;; it back.
10328       (select-window (get-buffer-window (current-buffer)))
10329       ;; Select next unread newsgroup automagically.
10330       (cond
10331        ((not gnus-auto-select-next)
10332         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10333        ((or (eq gnus-auto-select-next 'quietly)
10334             (and (eq gnus-auto-select-next 'slightly-quietly)
10335                  push)
10336             (and (eq gnus-auto-select-next 'almost-quietly)
10337                  (gnus-summary-last-article-p)))
10338         ;; Select quietly.
10339         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10340             (gnus-summary-exit)
10341           (gnus-message 7 "No more%s articles (%s)..."
10342                         (if unread " unread" "")
10343                         (if group (concat "selecting " group)
10344                           "exiting"))
10345           (gnus-summary-next-group nil group backward)))
10346        (t
10347         (gnus-summary-walk-group-buffer
10348          gnus-newsgroup-name cmd unread backward)))))))
10349
10350 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10351   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10352                       (?\C-p (gnus-group-prev-unread-group 1))))
10353         keve key group ended)
10354     (save-excursion
10355       (set-buffer gnus-group-buffer)
10356       (gnus-summary-jump-to-group from-group)
10357       (setq group
10358             (if (eq gnus-keep-same-level 'best)
10359                 (gnus-summary-best-group gnus-newsgroup-name)
10360               (gnus-summary-search-group backward gnus-keep-same-level))))
10361     (while (not ended)
10362       (gnus-message
10363        5 "No more%s articles%s" (if unread " unread" "")
10364        (if (and group
10365                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10366            (format " (Type %s for %s [%s])"
10367                    (single-key-description cmd) group
10368                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10369          (format " (Type %s to exit %s)"
10370                  (single-key-description cmd)
10371                  gnus-newsgroup-name)))
10372       ;; Confirm auto selection.
10373       (setq key (car (setq keve (gnus-read-event-char))))
10374       (setq ended t)
10375       (cond
10376        ((assq key keystrokes)
10377         (let ((obuf (current-buffer)))
10378           (switch-to-buffer gnus-group-buffer)
10379           (and group
10380                (gnus-group-jump-to-group group))
10381           (eval (cadr (assq key keystrokes)))
10382           (setq group (gnus-group-group-name))
10383           (switch-to-buffer obuf))
10384         (setq ended nil))
10385        ((equal key cmd)
10386         (if (or (not group)
10387                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10388             (gnus-summary-exit)
10389           (gnus-summary-next-group nil group backward)))
10390        (t
10391         (push (cdr keve) unread-command-events))))))
10392
10393 (defun gnus-read-event-char ()
10394   "Get the next event."
10395   (let ((event (read-event)))
10396     (cons (and (numberp event) event) event)))
10397
10398 (defun gnus-summary-next-unread-article ()
10399   "Select unread article after current one."
10400   (interactive)
10401   (gnus-summary-next-article t (and gnus-auto-select-same
10402                                     (gnus-summary-article-subject))))
10403
10404 (defun gnus-summary-prev-article (&optional unread subject)
10405   "Select the article after the current one.
10406 If UNREAD is non-nil, only unread articles are selected."
10407   (interactive "P")
10408   (gnus-summary-next-article unread subject t))
10409
10410 (defun gnus-summary-prev-unread-article ()
10411   "Select unred article before current one."
10412   (interactive)
10413   (gnus-summary-prev-article t (and gnus-auto-select-same
10414                                     (gnus-summary-article-subject))))
10415
10416 (defun gnus-summary-next-page (&optional lines circular)
10417   "Show next page of the selected article.
10418 If at the end of the current article, select the next article.
10419 LINES says how many lines should be scrolled up.
10420
10421 If CIRCULAR is non-nil, go to the start of the article instead of
10422 selecting the next article when reaching the end of the current
10423 article."
10424   (interactive "P")
10425   (setq gnus-summary-buffer (current-buffer))
10426   (gnus-set-global-variables)
10427   (let ((article (gnus-summary-article-number))
10428         (endp nil))
10429     (gnus-configure-windows 'article)
10430     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10431         (if (and (eq gnus-summary-goto-unread 'never)
10432                  (not (gnus-summary-last-article-p article)))
10433             (gnus-summary-next-article)
10434           (gnus-summary-next-unread-article))
10435       (if (or (null gnus-current-article)
10436               (null gnus-article-current)
10437               (/= article (cdr gnus-article-current))
10438               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10439           ;; Selected subject is different from current article's.
10440           (gnus-summary-display-article article)
10441         (gnus-eval-in-buffer-window
10442          gnus-article-buffer
10443          (setq endp (gnus-article-next-page lines)))
10444         (if endp
10445             (cond (circular
10446                    (gnus-summary-beginning-of-article))
10447                   (lines
10448                    (gnus-message 3 "End of message"))
10449                   ((null lines)
10450                    (if (and (eq gnus-summary-goto-unread 'never)
10451                             (not (gnus-summary-last-article-p article)))
10452                        (gnus-summary-next-article)
10453                      (gnus-summary-next-unread-article)))))))
10454     (gnus-summary-recenter)
10455     (gnus-summary-position-point)))
10456
10457 (defun gnus-summary-prev-page (&optional lines)
10458   "Show previous page of selected article.
10459 Argument LINES specifies lines to be scrolled down."
10460   (interactive "P")
10461   (gnus-set-global-variables)
10462   (let ((article (gnus-summary-article-number)))
10463     (gnus-configure-windows 'article)
10464     (if (or (null gnus-current-article)
10465             (null gnus-article-current)
10466             (/= article (cdr gnus-article-current))
10467             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10468         ;; Selected subject is different from current article's.
10469         (gnus-summary-display-article article)
10470       (gnus-summary-recenter)
10471       (gnus-eval-in-buffer-window gnus-article-buffer
10472                                   (gnus-article-prev-page lines))))
10473   (gnus-summary-position-point))
10474
10475 (defun gnus-summary-scroll-up (lines)
10476   "Scroll up (or down) one line current article.
10477 Argument LINES specifies lines to be scrolled up (or down if negative)."
10478   (interactive "p")
10479   (gnus-set-global-variables)
10480   (gnus-configure-windows 'article)
10481   (gnus-summary-show-thread)
10482   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10483     (gnus-eval-in-buffer-window
10484      gnus-article-buffer
10485      (cond ((> lines 0)
10486             (if (gnus-article-next-page lines)
10487                 (gnus-message 3 "End of message")))
10488            ((< lines 0)
10489             (gnus-article-prev-page (- lines))))))
10490   (gnus-summary-recenter)
10491   (gnus-summary-position-point))
10492
10493 (defun gnus-summary-next-same-subject ()
10494   "Select next article which has the same subject as current one."
10495   (interactive)
10496   (gnus-set-global-variables)
10497   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10498
10499 (defun gnus-summary-prev-same-subject ()
10500   "Select previous article which has the same subject as current one."
10501   (interactive)
10502   (gnus-set-global-variables)
10503   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10504
10505 (defun gnus-summary-next-unread-same-subject ()
10506   "Select next unread article which has the same subject as current one."
10507   (interactive)
10508   (gnus-set-global-variables)
10509   (gnus-summary-next-article t (gnus-summary-article-subject)))
10510
10511 (defun gnus-summary-prev-unread-same-subject ()
10512   "Select previous unread article which has the same subject as current one."
10513   (interactive)
10514   (gnus-set-global-variables)
10515   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10516
10517 (defun gnus-summary-first-unread-article ()
10518   "Select the first unread article.
10519 Return nil if there are no unread articles."
10520   (interactive)
10521   (gnus-set-global-variables)
10522   (prog1
10523       (if (gnus-summary-first-subject t)
10524           (progn
10525             (gnus-summary-show-thread)
10526             (gnus-summary-first-subject t)
10527             (gnus-summary-display-article (gnus-summary-article-number))))
10528     (gnus-summary-position-point)))
10529
10530 (defun gnus-summary-best-unread-article ()
10531   "Select the unread article with the highest score."
10532   (interactive)
10533   (gnus-set-global-variables)
10534   (let ((best -1000000)
10535         (data gnus-newsgroup-data)
10536         article score)
10537     (while data
10538       (and (gnus-data-unread-p (car data))
10539            (> (setq score
10540                     (gnus-summary-article-score (gnus-data-number (car data))))
10541               best)
10542            (setq best score
10543                  article (gnus-data-number (car data))))
10544       (setq data (cdr data)))
10545     (prog1
10546         (if article
10547             (gnus-summary-goto-article article)
10548           (error "No unread articles"))
10549       (gnus-summary-position-point))))
10550
10551 (defun gnus-summary-last-subject ()
10552   "Go to the last displayed subject line in the group."
10553   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10554     (when article
10555       (gnus-summary-goto-subject article))))
10556
10557 (defun gnus-summary-goto-article (article &optional all-headers force)
10558   "Fetch ARTICLE and display it if it exists.
10559 If ALL-HEADERS is non-nil, no header lines are hidden."
10560   (interactive
10561    (list
10562     (string-to-int
10563      (completing-read
10564       "Article number: "
10565       (mapcar (lambda (number) (list (int-to-string number)))
10566               gnus-newsgroup-limit)))
10567     current-prefix-arg
10568     t))
10569   (prog1
10570       (if (gnus-summary-goto-subject article force)
10571           (gnus-summary-display-article article all-headers)
10572         (gnus-message 4 "Couldn't go to article %s" article) nil)
10573     (gnus-summary-position-point)))
10574
10575 (defun gnus-summary-goto-last-article ()
10576   "Go to the previously read article."
10577   (interactive)
10578   (prog1
10579       (and gnus-last-article
10580            (gnus-summary-goto-article gnus-last-article))
10581     (gnus-summary-position-point)))
10582
10583 (defun gnus-summary-pop-article (number)
10584   "Pop one article off the history and go to the previous.
10585 NUMBER articles will be popped off."
10586   (interactive "p")
10587   (let (to)
10588     (setq gnus-newsgroup-history
10589           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10590     (if to
10591         (gnus-summary-goto-article (car to))
10592       (error "Article history empty")))
10593   (gnus-summary-position-point))
10594
10595 ;; Summary commands and functions for limiting the summary buffer.
10596
10597 (defun gnus-summary-limit-to-articles (n)
10598   "Limit the summary buffer to the next N articles.
10599 If not given a prefix, use the process marked articles instead."
10600   (interactive "P")
10601   (gnus-set-global-variables)
10602   (prog1
10603       (let ((articles (gnus-summary-work-articles n)))
10604         (setq gnus-newsgroup-processable nil)
10605         (gnus-summary-limit articles))
10606     (gnus-summary-position-point)))
10607
10608 (defun gnus-summary-pop-limit (&optional total)
10609   "Restore the previous limit.
10610 If given a prefix, remove all limits."
10611   (interactive "P")
10612   (gnus-set-global-variables)
10613   (when total 
10614     (setq gnus-newsgroup-limits
10615           (list (mapcar (lambda (h) (mail-header-number h))
10616                         gnus-newsgroup-headers))))
10617   (unless gnus-newsgroup-limits
10618     (error "No limit to pop"))
10619   (prog1
10620       (gnus-summary-limit nil 'pop)
10621     (gnus-summary-position-point)))
10622
10623 (defun gnus-summary-limit-to-subject (subject &optional header)
10624   "Limit the summary buffer to articles that have subjects that match a regexp."
10625   (interactive "sRegexp: ")
10626   (unless header
10627     (setq header "subject"))
10628   (when (not (equal "" subject))
10629     (prog1
10630         (let ((articles (gnus-summary-find-matching
10631                          (or header "subject") subject 'all)))
10632           (or articles (error "Found no matches for \"%s\"" subject))
10633           (gnus-summary-limit articles))
10634       (gnus-summary-position-point))))
10635
10636 (defun gnus-summary-limit-to-author (from)
10637   "Limit the summary buffer to articles that have authors that match a regexp."
10638   (interactive "sRegexp: ")
10639   (gnus-summary-limit-to-subject from "from"))
10640
10641 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10642 (make-obsolete
10643  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10644
10645 (defun gnus-summary-limit-to-unread (&optional all)
10646   "Limit the summary buffer to articles that are not marked as read.
10647 If ALL is non-nil, limit strictly to unread articles."
10648   (interactive "P")
10649   (if all
10650       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10651     (gnus-summary-limit-to-marks
10652      ;; Concat all the marks that say that an article is read and have
10653      ;; those removed.
10654      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10655            gnus-killed-mark gnus-kill-file-mark
10656            gnus-low-score-mark gnus-expirable-mark
10657            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10658      'reverse)))
10659
10660 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10661 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10662
10663 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10664   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10665 If REVERSE, limit the summary buffer to articles that are not marked
10666 with MARKS.  MARKS can either be a string of marks or a list of marks.
10667 Returns how many articles were removed."
10668   (interactive "sMarks: ")
10669   (gnus-set-global-variables)
10670   (prog1
10671       (let ((data gnus-newsgroup-data)
10672             (marks (if (listp marks) marks
10673                      (append marks nil))) ; Transform to list.
10674             articles)
10675         (while data
10676           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10677                  (memq (gnus-data-mark (car data)) marks))
10678                (setq articles (cons (gnus-data-number (car data)) articles)))
10679           (setq data (cdr data)))
10680         (gnus-summary-limit articles))
10681     (gnus-summary-position-point)))
10682
10683 (defun gnus-summary-limit-to-score (&optional score)
10684   "Limit to articles with score at or above SCORE."
10685   (interactive "P")
10686   (gnus-set-global-variables)
10687   (setq score (if score
10688                   (prefix-numeric-value score)
10689                 (or gnus-summary-default-score 0)))
10690   (let ((data gnus-newsgroup-data)
10691         articles)
10692     (while data
10693       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10694                 score)
10695         (push (gnus-data-number (car data)) articles))
10696       (setq data (cdr data)))
10697     (prog1
10698         (gnus-summary-limit articles)
10699       (gnus-summary-position-point))))
10700
10701 (defun gnus-summary-limit-include-dormant ()
10702   "Display all the hidden articles that are marked as dormant."
10703   (interactive)
10704   (gnus-set-global-variables)
10705   (or gnus-newsgroup-dormant
10706       (error "There are no dormant articles in this group"))
10707   (prog1
10708       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10709     (gnus-summary-position-point)))
10710
10711 (defun gnus-summary-limit-exclude-dormant ()
10712   "Hide all dormant articles."
10713   (interactive)
10714   (gnus-set-global-variables)
10715   (prog1
10716       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10717     (gnus-summary-position-point)))
10718
10719 (defun gnus-summary-limit-exclude-childless-dormant ()
10720   "Hide all dormant articles that have no children."
10721   (interactive)
10722   (gnus-set-global-variables)
10723   (let ((data (gnus-data-list t))
10724         articles d children)
10725     ;; Find all articles that are either not dormant or have
10726     ;; children.
10727     (while (setq d (pop data))
10728       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10729                 (and (setq children 
10730                            (gnus-article-children (gnus-data-number d)))
10731                      (let (found)
10732                        (while children
10733                          (when (memq (car children) articles)
10734                            (setq children nil
10735                                  found t))
10736                          (pop children))
10737                        found)))
10738         (push (gnus-data-number d) articles)))
10739     ;; Do the limiting.
10740     (prog1
10741         (gnus-summary-limit articles)
10742       (gnus-summary-position-point))))
10743
10744 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10745   "Mark all unread excluded articles as read.
10746 If ALL, mark even excluded ticked and dormants as read."
10747   (interactive "P")
10748   (let ((articles (gnus-sorted-complement
10749                    (sort
10750                     (mapcar (lambda (h) (mail-header-number h))
10751                             gnus-newsgroup-headers)
10752                     '<)
10753                    (sort gnus-newsgroup-limit '<)))
10754         article)
10755     (setq gnus-newsgroup-unreads nil)
10756     (if all
10757         (setq gnus-newsgroup-dormant nil
10758               gnus-newsgroup-marked nil
10759               gnus-newsgroup-reads
10760               (nconc
10761                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10762                gnus-newsgroup-reads))
10763       (while (setq article (pop articles))
10764         (unless (or (memq article gnus-newsgroup-dormant)
10765                     (memq article gnus-newsgroup-marked))
10766           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10767
10768 (defun gnus-summary-limit (articles &optional pop)
10769   (if pop
10770       ;; We pop the previous limit off the stack and use that.
10771       (setq articles (car gnus-newsgroup-limits)
10772             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10773     ;; We use the new limit, so we push the old limit on the stack.
10774     (setq gnus-newsgroup-limits
10775           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10776   ;; Set the limit.
10777   (setq gnus-newsgroup-limit articles)
10778   (let ((total (length gnus-newsgroup-data))
10779         (data (gnus-data-find-list (gnus-summary-article-number)))
10780         found)
10781     ;; This will do all the work of generating the new summary buffer
10782     ;; according to the new limit.
10783     (gnus-summary-prepare)
10784     ;; Hide any threads, possibly.
10785     (and gnus-show-threads
10786          gnus-thread-hide-subtree
10787          (gnus-summary-hide-all-threads))
10788     ;; Try to return to the article you were at, or one in the
10789     ;; neighborhood.
10790     (if data
10791         ;; We try to find some article after the current one.
10792         (while data
10793           (and (gnus-summary-goto-subject
10794                 (gnus-data-number (car data)) nil t)
10795                (setq data nil
10796                      found t))
10797           (setq data (cdr data))))
10798     (or found
10799         ;; If there is no data, that means that we were after the last
10800         ;; article.  The same goes when we can't find any articles
10801         ;; after the current one.
10802         (progn
10803           (goto-char (point-max))
10804           (gnus-summary-find-prev)))
10805     ;; We return how many articles were removed from the summary
10806     ;; buffer as a result of the new limit.
10807     (- total (length gnus-newsgroup-data))))
10808
10809 (defsubst gnus-invisible-cut-children (threads)
10810   (let ((num 0))
10811     (while threads
10812       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10813         (incf num))
10814       (pop threads))
10815     (< num 2)))
10816
10817 (defsubst gnus-cut-thread (thread)
10818   "Go forwards in the thread until we find an article that we want to display."
10819   (when (or (eq gnus-fetch-old-headers 'some)
10820             (eq gnus-build-sparse-threads 'some)
10821             (eq gnus-build-sparse-threads 'more))
10822     ;; Deal with old-fetched headers and sparse threads.
10823     (while (and
10824             thread
10825             (or
10826              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10827              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10828             (or (<= (length (cdr thread)) 1)
10829                 (gnus-invisible-cut-children (cdr thread))))
10830       (setq thread (cadr thread))))
10831   thread)
10832
10833 (defun gnus-cut-threads (threads)
10834   "Cut off all uninteresting articles from the beginning of threads."
10835   (when (or (eq gnus-fetch-old-headers 'some)
10836             (eq gnus-build-sparse-threads 'some)
10837             (eq gnus-build-sparse-threads 'more))
10838     (let ((th threads))
10839       (while th
10840         (setcar th (gnus-cut-thread (car th)))
10841         (setq th (cdr th)))))
10842   ;; Remove nixed out threads.
10843   (delq nil threads))
10844
10845 (defun gnus-summary-initial-limit (&optional show-if-empty)
10846   "Figure out what the initial limit is supposed to be on group entry.
10847 This entails weeding out unwanted dormants, low-scored articles,
10848 fetch-old-headers verbiage, and so on."
10849   ;; Most groups have nothing to remove.
10850   (if (or gnus-inhibit-limiting
10851           (and (null gnus-newsgroup-dormant)
10852                (not (eq gnus-fetch-old-headers 'some))
10853                (null gnus-summary-expunge-below)
10854                (not (eq gnus-build-sparse-threads 'some))
10855                (not (eq gnus-build-sparse-threads 'more))
10856                (null gnus-thread-expunge-below)
10857                (not gnus-use-nocem)))
10858       () ; Do nothing.
10859     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10860     (setq gnus-newsgroup-limit nil)
10861     (mapatoms
10862      (lambda (node)
10863        (unless (car (symbol-value node))
10864          ;; These threads have no parents -- they are roots.
10865          (let ((nodes (cdr (symbol-value node)))
10866                thread)
10867            (while nodes
10868              (if (and gnus-thread-expunge-below
10869                       (< (gnus-thread-total-score (car nodes))
10870                          gnus-thread-expunge-below))
10871                  (gnus-expunge-thread (pop nodes))
10872                (setq thread (pop nodes))
10873                (gnus-summary-limit-children thread))))))
10874      gnus-newsgroup-dependencies)
10875     ;; If this limitation resulted in an empty group, we might
10876     ;; pop the previous limit and use it instead.
10877     (when (and (not gnus-newsgroup-limit)
10878                show-if-empty)
10879       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10880     gnus-newsgroup-limit))
10881
10882 (defun gnus-summary-limit-children (thread)
10883   "Return 1 if this subthread is visible and 0 if it is not."
10884   ;; First we get the number of visible children to this thread.  This
10885   ;; is done by recursing down the thread using this function, so this
10886   ;; will really go down to a leaf article first, before slowly
10887   ;; working its way up towards the root.
10888   (when thread
10889     (let ((children
10890            (if (cdr thread)
10891                (apply '+ (mapcar 'gnus-summary-limit-children
10892                                  (cdr thread)))
10893              0))
10894           (number (mail-header-number (car thread)))
10895           score)
10896       (if (or
10897            ;; If this article is dormant and has absolutely no visible
10898            ;; children, then this article isn't visible.
10899            (and (memq number gnus-newsgroup-dormant)
10900                 (= children 0))
10901            ;; If this is "fetch-old-headered" and there is only one
10902            ;; visible child (or less), then we don't want this article.
10903            (and (eq gnus-fetch-old-headers 'some)
10904                 (memq number gnus-newsgroup-ancient)
10905                 (zerop children))
10906            ;; If this is a sparsely inserted article with no children,
10907            ;; we don't want it.
10908            (and (eq gnus-build-sparse-threads 'some)
10909                 (memq number gnus-newsgroup-sparse)
10910                 (zerop children))
10911            ;; If we use expunging, and this article is really
10912            ;; low-scored, then we don't want this article.
10913            (when (and gnus-summary-expunge-below
10914                       (< (setq score
10915                                (or (cdr (assq number gnus-newsgroup-scored))
10916                                    gnus-summary-default-score))
10917                          gnus-summary-expunge-below))
10918              ;; We increase the expunge-tally here, but that has
10919              ;; nothing to do with the limits, really.
10920              (incf gnus-newsgroup-expunged-tally)
10921              ;; We also mark as read here, if that's wanted.
10922              (when (and gnus-summary-mark-below
10923                         (< score gnus-summary-mark-below))
10924                (setq gnus-newsgroup-unreads
10925                      (delq number gnus-newsgroup-unreads))
10926                (if gnus-newsgroup-auto-expire
10927                    (push number gnus-newsgroup-expirable)
10928                  (push (cons number gnus-low-score-mark)
10929                        gnus-newsgroup-reads)))
10930              t)
10931            (and gnus-use-nocem
10932                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10933           ;; Nope, invisible article.
10934           0
10935         ;; Ok, this article is to be visible, so we add it to the limit
10936         ;; and return 1.
10937         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10938         1))))
10939
10940 (defun gnus-expunge-thread (thread)
10941   "Mark all articles in THREAD as read."
10942   (let* ((number (mail-header-number (car thread))))
10943     (incf gnus-newsgroup-expunged-tally)
10944     ;; We also mark as read here, if that's wanted.
10945     (setq gnus-newsgroup-unreads
10946           (delq number gnus-newsgroup-unreads))
10947     (if gnus-newsgroup-auto-expire
10948         (push number gnus-newsgroup-expirable)
10949       (push (cons number gnus-low-score-mark)
10950             gnus-newsgroup-reads)))
10951   ;; Go recursively through all subthreads.
10952   (mapcar 'gnus-expunge-thread (cdr thread)))
10953
10954 ;; Summary article oriented commands
10955
10956 (defun gnus-summary-refer-parent-article (n)
10957   "Refer parent article N times.
10958 The difference between N and the number of articles fetched is returned."
10959   (interactive "p")
10960   (gnus-set-global-variables)
10961   (while
10962       (and
10963        (> n 0)
10964        (let* ((header (gnus-summary-article-header))
10965               (ref
10966                ;; If we try to find the parent of the currently
10967                ;; displayed article, then we take a look at the actual
10968                ;; References header, since this is slightly more
10969                ;; reliable than the References field we got from the
10970                ;; server.
10971                (if (and (eq (mail-header-number header)
10972                             (cdr gnus-article-current))
10973                         (equal gnus-newsgroup-name
10974                                (car gnus-article-current)))
10975                    (save-excursion
10976                      (set-buffer gnus-original-article-buffer)
10977                      (nnheader-narrow-to-headers)
10978                      (prog1
10979                          (message-fetch-field "references")
10980                        (widen)))
10981                  ;; It's not the current article, so we take a bet on
10982                  ;; the value we got from the server.
10983                  (mail-header-references header))))
10984          (if (setq ref (or ref (mail-header-references header)))
10985              (or (gnus-summary-refer-article (gnus-parent-id ref))
10986                  (gnus-message 1 "Couldn't find parent"))
10987            (gnus-message 1 "No references in article %d"
10988                          (gnus-summary-article-number))
10989            nil)))
10990     (setq n (1- n)))
10991   (gnus-summary-position-point)
10992   n)
10993
10994 (defun gnus-summary-refer-references ()
10995   "Fetch all articles mentioned in the References header.
10996 Return how many articles were fetched."
10997   (interactive)
10998   (gnus-set-global-variables)
10999   (let ((ref (mail-header-references (gnus-summary-article-header)))
11000         (current (gnus-summary-article-number))
11001         (n 0))
11002     ;; For each Message-ID in the References header...
11003     (while (string-match "<[^>]*>" ref)
11004       (incf n)
11005       ;; ... fetch that article.
11006       (gnus-summary-refer-article
11007        (prog1 (match-string 0 ref)
11008          (setq ref (substring ref (match-end 0))))))
11009     (gnus-summary-goto-subject current)
11010     (gnus-summary-position-point)
11011     n))
11012
11013 (defun gnus-summary-refer-article (message-id)
11014   "Fetch an article specified by MESSAGE-ID."
11015   (interactive "sMessage-ID: ")
11016   (when (and (stringp message-id)
11017              (not (zerop (length message-id))))
11018     ;; Construct the correct Message-ID if necessary.
11019     ;; Suggested by tale@pawl.rpi.edu.
11020     (unless (string-match "^<" message-id)
11021       (setq message-id (concat "<" message-id)))
11022     (unless (string-match ">$" message-id)
11023       (setq message-id (concat message-id ">")))
11024     (let ((header (gnus-id-to-header message-id)))
11025       (if header
11026           ;; The article is present in the buffer, to we just go to it.
11027           (gnus-summary-goto-article (mail-header-number header) nil header)
11028         ;; We fetch the article
11029         (let ((gnus-override-method 
11030                (and (gnus-news-group-p gnus-newsgroup-name)
11031                     gnus-refer-article-method))
11032               number)
11033           ;; Start the special refer-article method, if necessary.
11034           (when (and gnus-refer-article-method
11035                      (gnus-news-group-p gnus-newsgroup-name))
11036             (gnus-check-server gnus-refer-article-method))
11037           ;; Fetch the header, and display the article.
11038           (if (setq number (gnus-summary-insert-subject message-id))
11039               (gnus-summary-select-article nil nil nil number)
11040             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11041
11042 (defun gnus-summary-enter-digest-group (&optional force)
11043   "Enter a digest group based on the current article."
11044   (interactive "P")
11045   (gnus-set-global-variables)
11046   (gnus-summary-select-article)
11047   (let ((name (format "%s-%d"
11048                       (gnus-group-prefixed-name
11049                        gnus-newsgroup-name (list 'nndoc ""))
11050                       gnus-current-article))
11051         (ogroup gnus-newsgroup-name)
11052         (case-fold-search t)
11053         (buf (current-buffer))
11054         dig)
11055     (save-excursion
11056       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11057       (insert-buffer-substring gnus-original-article-buffer)
11058       (narrow-to-region
11059        (goto-char (point-min))
11060        (or (search-forward "\n\n" nil t) (point)))
11061       (goto-char (point-min))
11062       (delete-matching-lines "^\\(Path\\):\\|^From ")
11063       (widen))
11064     (unwind-protect
11065         (if (gnus-group-read-ephemeral-group
11066              name `(nndoc ,name (nndoc-address
11067                                  ,(get-buffer dig))
11068                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11069             ;; Make all postings to this group go to the parent group.
11070             (nconc (gnus-info-params (gnus-get-info name))
11071                    (list (cons 'to-group ogroup)))
11072           ;; Couldn't select this doc group.
11073           (switch-to-buffer buf)
11074           (gnus-set-global-variables)
11075           (gnus-configure-windows 'summary)
11076           (gnus-message 3 "Article couldn't be entered?"))
11077       (kill-buffer dig))))
11078
11079 (defun gnus-summary-isearch-article (&optional regexp-p)
11080   "Do incremental search forward on the current article.
11081 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11082   (interactive "P")
11083   (gnus-set-global-variables)
11084   (gnus-summary-select-article)
11085   (gnus-configure-windows 'article)
11086   (gnus-eval-in-buffer-window
11087    gnus-article-buffer
11088    (goto-char (point-min))
11089    (isearch-forward regexp-p)))
11090
11091 (defun gnus-summary-search-article-forward (regexp &optional backward)
11092   "Search for an article containing REGEXP forward.
11093 If BACKWARD, search backward instead."
11094   (interactive
11095    (list (read-string
11096           (format "Search article %s (regexp%s): "
11097                   (if current-prefix-arg "backward" "forward")
11098                   (if gnus-last-search-regexp
11099                       (concat ", default " gnus-last-search-regexp)
11100                     "")))
11101          current-prefix-arg))
11102   (gnus-set-global-variables)
11103   (if (string-equal regexp "")
11104       (setq regexp (or gnus-last-search-regexp ""))
11105     (setq gnus-last-search-regexp regexp))
11106   (unless (gnus-summary-search-article regexp backward)
11107     (error "Search failed: \"%s\"" regexp)))
11108
11109 (defun gnus-summary-search-article-backward (regexp)
11110   "Search for an article containing REGEXP backward."
11111   (interactive
11112    (list (read-string
11113           (format "Search article backward (regexp%s): "
11114                   (if gnus-last-search-regexp
11115                       (concat ", default " gnus-last-search-regexp)
11116                     "")))))
11117   (gnus-summary-search-article-forward regexp 'backward))
11118
11119 (defun gnus-summary-search-article (regexp &optional backward)
11120   "Search for an article containing REGEXP.
11121 Optional argument BACKWARD means do search for backward.
11122 `gnus-select-article-hook' is not called during the search."
11123   (let ((gnus-select-article-hook nil)  ;Disable hook.
11124         (gnus-article-display-hook nil)
11125         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11126         (re-search
11127          (if backward
11128              're-search-backward 're-search-forward))
11129         (sum (current-buffer))
11130         (found nil))
11131     (gnus-save-hidden-threads
11132       (gnus-summary-select-article)
11133       (set-buffer gnus-article-buffer)
11134       (while (not found)
11135         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11136         (if (if backward
11137                 (re-search-backward regexp nil t)
11138               (re-search-forward regexp nil t))
11139             ;; We found the regexp.
11140             (progn
11141               (setq found 'found)
11142               (beginning-of-line)
11143               (set-window-start
11144                (get-buffer-window (current-buffer))
11145                (point))
11146               (forward-line 1)
11147               (set-buffer sum))
11148           ;; We didn't find it, so we go to the next article.
11149           (set-buffer sum)
11150           (if (not (if backward (gnus-summary-find-prev)
11151                      (gnus-summary-find-next)))
11152               ;; No more articles.
11153               (setq found t)
11154             ;; Select the next article and adjust point.
11155             (gnus-summary-select-article)
11156             (set-buffer gnus-article-buffer)
11157             (widen)
11158             (goto-char (if backward (point-max) (point-min))))))
11159       (gnus-message 7 ""))
11160     ;; Return whether we found the regexp.
11161     (when (eq found 'found)
11162       (gnus-summary-show-thread)
11163       (gnus-summary-goto-subject gnus-current-article)
11164       (gnus-summary-position-point)
11165       t)))
11166
11167 (defun gnus-summary-find-matching (header regexp &optional backward unread
11168                                           not-case-fold)
11169   "Return a list of all articles that match REGEXP on HEADER.
11170 The search stars on the current article and goes forwards unless
11171 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11172 If UNREAD is non-nil, only unread articles will
11173 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11174 in the comparisons."
11175   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11176                 (gnus-data-find-list
11177                  (gnus-summary-article-number) (gnus-data-list backward))))
11178         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11179         (case-fold-search (not not-case-fold))
11180         articles d)
11181     (or (fboundp (intern (concat "mail-header-" header)))
11182         (error "%s is not a valid header" header))
11183     (while data
11184       (setq d (car data))
11185       (and (or (not unread)             ; We want all articles...
11186                (gnus-data-unread-p d))  ; Or just unreads.
11187            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11188            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11189            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11190       (setq data (cdr data)))
11191     (nreverse articles)))
11192
11193 (defun gnus-summary-execute-command (header regexp command &optional backward)
11194   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11195 If HEADER is an empty string (or nil), the match is done on the entire
11196 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11197   (interactive
11198    (list (let ((completion-ignore-case t))
11199            (completing-read
11200             "Header name: "
11201             (mapcar (lambda (string) (list string))
11202                     '("Number" "Subject" "From" "Lines" "Date"
11203                       "Message-ID" "Xref" "References" "Body"))
11204             nil 'require-match))
11205          (read-string "Regexp: ")
11206          (read-key-sequence "Command: ")
11207          current-prefix-arg))
11208   (when (equal header "Body")
11209     (setq header ""))
11210   (gnus-set-global-variables)
11211   ;; Hidden thread subtrees must be searched as well.
11212   (gnus-summary-show-all-threads)
11213   ;; We don't want to change current point nor window configuration.
11214   (save-excursion
11215     (save-window-excursion
11216       (gnus-message 6 "Executing %s..." (key-description command))
11217       ;; We'd like to execute COMMAND interactively so as to give arguments.
11218       (gnus-execute header regexp
11219                     `(lambda () (call-interactively ',(key-binding command)))
11220                     backward)
11221       (gnus-message 6 "Executing %s...done" (key-description command)))))
11222
11223 (defun gnus-summary-beginning-of-article ()
11224   "Scroll the article back to the beginning."
11225   (interactive)
11226   (gnus-set-global-variables)
11227   (gnus-summary-select-article)
11228   (gnus-configure-windows 'article)
11229   (gnus-eval-in-buffer-window
11230    gnus-article-buffer
11231    (widen)
11232    (goto-char (point-min))
11233    (and gnus-break-pages (gnus-narrow-to-page))))
11234
11235 (defun gnus-summary-end-of-article ()
11236   "Scroll to the end of the article."
11237   (interactive)
11238   (gnus-set-global-variables)
11239   (gnus-summary-select-article)
11240   (gnus-configure-windows 'article)
11241   (gnus-eval-in-buffer-window
11242    gnus-article-buffer
11243    (widen)
11244    (goto-char (point-max))
11245    (recenter -3)
11246    (and gnus-break-pages (gnus-narrow-to-page))))
11247
11248 (defun gnus-summary-show-article (&optional arg)
11249   "Force re-fetching of the current article.
11250 If ARG (the prefix) is non-nil, show the raw article without any
11251 article massaging functions being run."
11252   (interactive "P")
11253   (gnus-set-global-variables)
11254   (if (not arg)
11255       ;; Select the article the normal way.
11256       (gnus-summary-select-article nil 'force)
11257     ;; Bind the article treatment functions to nil.
11258     (let ((gnus-have-all-headers t)
11259           gnus-article-display-hook
11260           gnus-article-prepare-hook
11261           gnus-break-pages
11262           gnus-visual)
11263       (gnus-summary-select-article nil 'force)))
11264   (gnus-summary-goto-subject gnus-current-article)
11265 ;  (gnus-configure-windows 'article)
11266   (gnus-summary-position-point))
11267
11268 (defun gnus-summary-verbose-headers (&optional arg)
11269   "Toggle permanent full header display.
11270 If ARG is a positive number, turn header display on.
11271 If ARG is a negative number, turn header display off."
11272   (interactive "P")
11273   (gnus-set-global-variables)
11274   (gnus-summary-toggle-header arg)
11275   (setq gnus-show-all-headers
11276         (cond ((or (not (numberp arg))
11277                    (zerop arg))
11278                (not gnus-show-all-headers))
11279               ((natnump arg)
11280                t))))
11281
11282 (defun gnus-summary-toggle-header (&optional arg)
11283   "Show the headers if they are hidden, or hide them if they are shown.
11284 If ARG is a positive number, show the entire header.
11285 If ARG is a negative number, hide the unwanted header lines."
11286   (interactive "P")
11287   (gnus-set-global-variables)
11288   (save-excursion
11289     (set-buffer gnus-article-buffer)
11290     (let* ((buffer-read-only nil)
11291            (inhibit-point-motion-hooks t)
11292            (hidden (text-property-any
11293                     (goto-char (point-min)) (search-forward "\n\n")
11294                     'invisible t))
11295            e)
11296       (goto-char (point-min))
11297       (when (search-forward "\n\n" nil t)
11298         (delete-region (point-min) (1- (point))))
11299       (goto-char (point-min))
11300       (save-excursion
11301         (set-buffer gnus-original-article-buffer)
11302         (goto-char (point-min))
11303         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11304       (insert-buffer-substring gnus-original-article-buffer 1 e)
11305       (let ((gnus-inhibit-hiding t))
11306         (run-hooks 'gnus-article-display-hook))
11307       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11308           (gnus-article-hide-headers)))))
11309
11310 (defun gnus-summary-show-all-headers ()
11311   "Make all header lines visible."
11312   (interactive)
11313   (gnus-set-global-variables)
11314   (gnus-article-show-all-headers))
11315
11316 (defun gnus-summary-toggle-mime (&optional arg)
11317   "Toggle MIME processing.
11318 If ARG is a positive number, turn MIME processing on."
11319   (interactive "P")
11320   (gnus-set-global-variables)
11321   (setq gnus-show-mime
11322         (if (null arg) (not gnus-show-mime)
11323           (> (prefix-numeric-value arg) 0)))
11324   (gnus-summary-select-article t 'force))
11325
11326 (defun gnus-summary-caesar-message (&optional arg)
11327   "Caesar rotate the current article by 13.
11328 The numerical prefix specifies how manu places to rotate each letter
11329 forward."
11330   (interactive "P")
11331   (gnus-set-global-variables)
11332   (gnus-summary-select-article)
11333   (let ((mail-header-separator ""))
11334     (gnus-eval-in-buffer-window
11335      gnus-article-buffer
11336      (save-restriction
11337        (widen)
11338        (let ((start (window-start)))
11339          (news-caesar-buffer-body arg)
11340          (set-window-start (get-buffer-window (current-buffer)) start))))))
11341
11342 (defun gnus-summary-stop-page-breaking ()
11343   "Stop page breaking in the current article."
11344   (interactive)
11345   (gnus-set-global-variables)
11346   (gnus-summary-select-article)
11347   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11348
11349 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11350   "Move the current article to a different newsgroup.
11351 If N is a positive number, move the N next articles.
11352 If N is a negative number, move the N previous articles.
11353 If N is nil and any articles have been marked with the process mark,
11354 move those articles instead.
11355 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11356 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11357 re-spool using this method.
11358
11359 For this function to work, both the current newsgroup and the
11360 newsgroup that you want to move to have to support the `request-move'
11361 and `request-accept' functions."
11362   (interactive "P")
11363   (unless action (setq action 'move))
11364   (gnus-set-global-variables)
11365   ;; Check whether the source group supports the required functions.
11366   (cond ((and (eq action 'move)
11367               (not (gnus-check-backend-function
11368                     'request-move-article gnus-newsgroup-name)))
11369          (error "The current group does not support article moving"))
11370         ((and (eq action 'crosspost)
11371               (not (gnus-check-backend-function
11372                     'request-replace-article gnus-newsgroup-name)))
11373          (error "The current group does not support article editing")))
11374   (let ((articles (gnus-summary-work-articles n))
11375         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11376         (names '((move "Move" "Moving")
11377                  (copy "Copy" "Copying")
11378                  (crosspost "Crosspost" "Crossposting")))
11379         (copy-buf (save-excursion
11380                     (nnheader-set-temp-buffer " *copy article*")))
11381         art-group to-method new-xref article to-groups)
11382     (unless (assq action names)
11383       (error "Unknown action %s" action))
11384     ;; Read the newsgroup name.
11385     (when (and (not to-newsgroup)
11386                (not select-method))
11387       (setq to-newsgroup
11388             (gnus-read-move-group-name
11389              (cadr (assq action names))
11390              (symbol-value (intern (format "gnus-current-%s-group" action)))
11391              articles prefix))
11392       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11393     (setq to-method (or select-method 
11394                         (gnus-find-method-for-group to-newsgroup)))
11395     ;; Check the method we are to move this article to...
11396     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11397         (error "%s does not support article copying" (car to-method)))
11398     (or (gnus-check-server to-method)
11399         (error "Can't open server %s" (car to-method)))
11400     (gnus-message 6 "%s to %s: %s..."
11401                   (caddr (assq action names))
11402                   (or (car select-method) to-newsgroup) articles)
11403     (while articles
11404       (setq article (pop articles))
11405       (setq
11406        art-group
11407        (cond
11408         ;; Move the article.
11409         ((eq action 'move)
11410          (gnus-request-move-article
11411           article                       ; Article to move
11412           gnus-newsgroup-name           ; From newsgrouo
11413           (nth 1 (gnus-find-method-for-group
11414                   gnus-newsgroup-name)) ; Server
11415           (list 'gnus-request-accept-article
11416                 to-newsgroup (list 'quote select-method)
11417                 (not articles))         ; Accept form
11418           (not articles)))              ; Only save nov last time
11419         ;; Copy the article.
11420         ((eq action 'copy)
11421          (save-excursion
11422            (set-buffer copy-buf)
11423            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11424            (gnus-request-accept-article
11425             to-newsgroup select-method (not articles))))
11426         ;; Crosspost the article.
11427         ((eq action 'crosspost)
11428          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11429            (setq new-xref (concat gnus-newsgroup-name ":" article))
11430            (if (and xref (not (string= xref "")))
11431                (progn
11432                  (when (string-match "^Xref: " xref)
11433                    (setq xref (substring xref (match-end 0))))
11434                  (setq new-xref (concat xref " " new-xref)))
11435              (setq new-xref (concat (system-name) " " new-xref)))
11436            (save-excursion
11437              (set-buffer copy-buf)
11438              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11439              (nnheader-replace-header "xref" new-xref)
11440              (gnus-request-accept-article
11441               to-newsgroup select-method (not articles)))))))
11442       (if (not art-group)
11443           (gnus-message 1 "Couldn't %s article %s"
11444                         (cadr (assq action names)) article)
11445         (let* ((entry
11446                 (or
11447                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11448                  (gnus-gethash
11449                   (gnus-group-prefixed-name
11450                    (car art-group)
11451                    (or select-method 
11452                        (gnus-find-method-for-group to-newsgroup)))
11453                   gnus-newsrc-hashtb)))
11454                (info (nth 2 entry))
11455                (to-group (gnus-info-group info)))
11456           ;; Update the group that has been moved to.
11457           (when (and info
11458                      (memq action '(move copy)))
11459             (unless (member to-group to-groups)
11460               (push to-group to-groups))
11461
11462             (unless (memq article gnus-newsgroup-unreads)
11463               (gnus-info-set-read
11464                info (gnus-add-to-range (gnus-info-read info)
11465                                        (list (cdr art-group)))))
11466
11467             ;; Copy any marks over to the new group.
11468             (let ((marks gnus-article-mark-lists)
11469                   (to-article (cdr art-group)))
11470
11471               ;; See whether the article is to be put in the cache.
11472               (when gnus-use-cache
11473                 (gnus-cache-possibly-enter-article
11474                  to-group to-article
11475                  (let ((header (copy-sequence
11476                                 (gnus-summary-article-header article))))
11477                    (mail-header-set-number header to-article)
11478                    header)
11479                  (memq article gnus-newsgroup-marked)
11480                  (memq article gnus-newsgroup-dormant)
11481                  (memq article gnus-newsgroup-unreads)))
11482
11483               (while marks
11484                 (when (memq article (symbol-value
11485                                      (intern (format "gnus-newsgroup-%s"
11486                                                      (caar marks)))))
11487                   ;; If the other group is the same as this group,
11488                   ;; then we have to add the mark to the list.
11489                   (when (equal to-group gnus-newsgroup-name)
11490                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11491                          (cons to-article
11492                                (symbol-value
11493                                 (intern (format "gnus-newsgroup-%s"
11494                                                 (caar marks)))))))
11495                   ;; Copy mark to other group.
11496                   (gnus-add-marked-articles
11497                    to-group (cdar marks) (list to-article) info))
11498                 (setq marks (cdr marks)))))
11499
11500           ;; Update the Xref header in this article to point to
11501           ;; the new crossposted article we have just created.
11502           (when (eq action 'crosspost)
11503             (save-excursion
11504               (set-buffer copy-buf)
11505               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11506               (nnheader-replace-header
11507                "xref" (concat new-xref " " (gnus-group-prefixed-name
11508                                             (car art-group) to-method)
11509                               ":" (cdr art-group)))
11510               (gnus-request-replace-article
11511                article gnus-newsgroup-name (current-buffer)))))
11512
11513         (gnus-summary-goto-subject article)
11514         (when (eq action 'move)
11515           (gnus-summary-mark-article article gnus-canceled-mark)))
11516       (gnus-summary-remove-process-mark article))
11517     ;; Re-activate all groups that have been moved to.
11518     (while to-groups
11519       (gnus-activate-group (pop to-groups)))
11520     
11521     (gnus-kill-buffer copy-buf)
11522     (gnus-summary-position-point)
11523     (gnus-set-mode-line 'summary)))
11524
11525 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11526   "Move the current article to a different newsgroup.
11527 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11528 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11529 re-spool using this method."
11530   (interactive "P")
11531   (gnus-summary-move-article n nil select-method 'copy))
11532
11533 (defun gnus-summary-crosspost-article (&optional n)
11534   "Crosspost the current article to some other group."
11535   (interactive "P")
11536   (gnus-summary-move-article n nil nil 'crosspost))
11537
11538 (defvar gnus-summary-respool-default-method nil
11539   "Default method for respooling an article.  
11540 If nil, use to the current newsgroup method.")
11541
11542 (defun gnus-summary-respool-article (&optional n method)
11543   "Respool the current article.
11544 The article will be squeezed through the mail spooling process again,
11545 which means that it will be put in some mail newsgroup or other
11546 depending on `nnmail-split-methods'.
11547 If N is a positive number, respool the N next articles.
11548 If N is a negative number, respool the N previous articles.
11549 If N is nil and any articles have been marked with the process mark,
11550 respool those articles instead.
11551
11552 Respooling can be done both from mail groups and \"real\" newsgroups.
11553 In the former case, the articles in question will be moved from the
11554 current group into whatever groups they are destined to.  In the
11555 latter case, they will be copied into the relevant groups."
11556   (interactive 
11557    (list current-prefix-arg
11558          (let* ((methods (gnus-methods-using 'respool))
11559                 (methname
11560                  (symbol-name (or gnus-summary-respool-default-method
11561                                   (car (gnus-find-method-for-group
11562                                         gnus-newsgroup-name)))))
11563                 (method
11564                  (gnus-completing-read 
11565                   methname "What backend do you want to use when? "
11566                   methods nil t nil 'gnus-method-history))
11567                 ms)
11568            (cond
11569             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11570              (list (intern method) ""))
11571             ((= 1 (length ms))
11572              (car ms))
11573             (t
11574              (cdr (completing-read 
11575                    "Server name: "
11576                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11577   (gnus-set-global-variables)
11578   (unless method
11579     (error "No method given for respooling"))
11580   (if (assoc (symbol-name
11581               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11582              (gnus-methods-using 'respool))
11583       (gnus-summary-move-article n nil method)
11584     (gnus-summary-copy-article n nil method)))
11585
11586 (defun gnus-summary-import-article (file)
11587   "Import a random file into a mail newsgroup."
11588   (interactive "fImport file: ")
11589   (gnus-set-global-variables)
11590   (let ((group gnus-newsgroup-name)
11591         (now (current-time))
11592         atts lines)
11593     (or (gnus-check-backend-function 'request-accept-article group)
11594         (error "%s does not support article importing" group))
11595     (or (file-readable-p file)
11596         (not (file-regular-p file))
11597         (error "Can't read %s" file))
11598     (save-excursion
11599       (set-buffer (get-buffer-create " *import file*"))
11600       (buffer-disable-undo (current-buffer))
11601       (erase-buffer)
11602       (insert-file-contents file)
11603       (goto-char (point-min))
11604       (unless (nnheader-article-p)
11605         ;; This doesn't look like an article, so we fudge some headers.
11606         (setq atts (file-attributes file)
11607               lines (count-lines (point-min) (point-max)))
11608         (insert "From: " (read-string "From: ") "\n"
11609                 "Subject: " (read-string "Subject: ") "\n"
11610                 "Date: " (timezone-make-date-arpa-standard
11611                           (current-time-string (nth 5 atts))
11612                           (current-time-zone now)
11613                           (current-time-zone now)) "\n"
11614                 "Message-ID: " (gnus-inews-message-id) "\n"
11615                 "Lines: " (int-to-string lines) "\n"
11616                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11617       (gnus-request-accept-article group nil t)
11618       (kill-buffer (current-buffer)))))
11619
11620 (defun gnus-summary-expire-articles (&optional now)
11621   "Expire all articles that are marked as expirable in the current group."
11622   (interactive)
11623   (gnus-set-global-variables)
11624   (when (gnus-check-backend-function
11625          'request-expire-articles gnus-newsgroup-name)
11626     ;; This backend supports expiry.
11627     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11628            (expirable (if total
11629                           (gnus-list-of-read-articles gnus-newsgroup-name)
11630                         (setq gnus-newsgroup-expirable
11631                               (sort gnus-newsgroup-expirable '<))))
11632            (expiry-wait (if now 'immediate
11633                           (gnus-group-get-parameter
11634                            gnus-newsgroup-name 'expiry-wait)))
11635            es)
11636       (when expirable
11637         ;; There are expirable articles in this group, so we run them
11638         ;; through the expiry process.
11639         (gnus-message 6 "Expiring articles...")
11640         ;; The list of articles that weren't expired is returned.
11641         (if expiry-wait
11642             (let ((nnmail-expiry-wait-function nil)
11643                   (nnmail-expiry-wait expiry-wait))
11644               (setq es (gnus-request-expire-articles
11645                         expirable gnus-newsgroup-name)))
11646           (setq es (gnus-request-expire-articles
11647                     expirable gnus-newsgroup-name)))
11648         (or total (setq gnus-newsgroup-expirable es))
11649         ;; We go through the old list of expirable, and mark all
11650         ;; really expired articles as nonexistent.
11651         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11652           (let ((gnus-use-cache nil))
11653             (while expirable
11654               (unless (memq (car expirable) es)
11655                 (when (gnus-data-find (car expirable))
11656                   (gnus-summary-mark-article
11657                    (car expirable) gnus-canceled-mark)))
11658               (setq expirable (cdr expirable)))))
11659         (gnus-message 6 "Expiring articles...done")))))
11660
11661 (defun gnus-summary-expire-articles-now ()
11662   "Expunge all expirable articles in the current group.
11663 This means that *all* articles that are marked as expirable will be
11664 deleted forever, right now."
11665   (interactive)
11666   (gnus-set-global-variables)
11667   (or gnus-expert-user
11668       (gnus-y-or-n-p
11669        "Are you really, really, really sure you want to delete all these messages? ")
11670       (error "Phew!"))
11671   (gnus-summary-expire-articles t))
11672
11673 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11674 (defun gnus-summary-delete-article (&optional n)
11675   "Delete the N next (mail) articles.
11676 This command actually deletes articles.  This is not a marking
11677 command.  The article will disappear forever from your life, never to
11678 return.
11679 If N is negative, delete backwards.
11680 If N is nil and articles have been marked with the process mark,
11681 delete these instead."
11682   (interactive "P")
11683   (gnus-set-global-variables)
11684   (or (gnus-check-backend-function 'request-expire-articles
11685                                    gnus-newsgroup-name)
11686       (error "The current newsgroup does not support article deletion."))
11687   ;; Compute the list of articles to delete.
11688   (let ((articles (gnus-summary-work-articles n))
11689         not-deleted)
11690     (if (and gnus-novice-user
11691              (not (gnus-y-or-n-p
11692                    (format "Do you really want to delete %s forever? "
11693                            (if (> (length articles) 1) 
11694                                (format "these %s articles" (length articles))
11695                              "this article")))))
11696         ()
11697       ;; Delete the articles.
11698       (setq not-deleted (gnus-request-expire-articles
11699                          articles gnus-newsgroup-name 'force))
11700       (while articles
11701         (gnus-summary-remove-process-mark (car articles))
11702         ;; The backend might not have been able to delete the article
11703         ;; after all.
11704         (or (memq (car articles) not-deleted)
11705             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11706         (setq articles (cdr articles))))
11707     (gnus-summary-position-point)
11708     (gnus-set-mode-line 'summary)
11709     not-deleted))
11710
11711 (defun gnus-summary-edit-article (&optional force)
11712   "Enter into a buffer and edit the current article.
11713 This will have permanent effect only in mail groups.
11714 If FORCE is non-nil, allow editing of articles even in read-only
11715 groups."
11716   (interactive "P")
11717   (save-excursion
11718     (set-buffer gnus-summary-buffer)
11719     (gnus-set-global-variables)
11720     (when (and (not force)
11721                (gnus-group-read-only-p))
11722       (error "The current newsgroup does not support article editing."))
11723     (gnus-summary-select-article t nil t)
11724     (gnus-configure-windows 'article)
11725     (select-window (get-buffer-window gnus-article-buffer))
11726     (gnus-message 6 "C-c C-c to end edits")
11727     (setq buffer-read-only nil)
11728     (text-mode)
11729     (use-local-map (copy-keymap (current-local-map)))
11730     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11731     (buffer-enable-undo)
11732     (widen)
11733     (goto-char (point-min))
11734     (search-forward "\n\n" nil t)))
11735
11736 (defun gnus-summary-edit-article-done ()
11737   "Make edits to the current article permanent."
11738   (interactive)
11739   (if (gnus-group-read-only-p)
11740       (progn
11741         (gnus-summary-edit-article-postpone)
11742         (gnus-error
11743          1 "The current newsgroup does not support article editing."))
11744     (let ((buf (format "%s" (buffer-string))))
11745       (erase-buffer)
11746       (insert buf)
11747       (if (not (gnus-request-replace-article
11748                 (cdr gnus-article-current) (car gnus-article-current)
11749                 (current-buffer)))
11750           (error "Couldn't replace article.")
11751         (gnus-article-mode)
11752         (use-local-map gnus-article-mode-map)
11753         (setq buffer-read-only t)
11754         (buffer-disable-undo (current-buffer))
11755         (gnus-configure-windows 'summary)
11756         (gnus-summary-update-article (cdr gnus-article-current))
11757         (when gnus-use-cache
11758           (gnus-cache-update-article 
11759            (cdr gnus-article-current) (car gnus-article-current)))
11760         (when gnus-keep-backlog
11761           (gnus-backlog-remove-article 
11762            (car gnus-article-current) (cdr gnus-article-current))))
11763       (save-excursion
11764         (when (get-buffer gnus-original-article-buffer)
11765           (set-buffer gnus-original-article-buffer)
11766           (setq gnus-original-article nil)))
11767       (setq gnus-article-current nil
11768             gnus-current-article nil)
11769       (run-hooks 'gnus-article-display-hook)
11770       (and (gnus-visual-p 'summary-highlight 'highlight)
11771            (run-hooks 'gnus-visual-mark-article-hook)))))
11772
11773 (defun gnus-summary-edit-article-postpone ()
11774   "Postpone changes to the current article."
11775   (interactive)
11776   (gnus-article-mode)
11777   (use-local-map gnus-article-mode-map)
11778   (setq buffer-read-only t)
11779   (buffer-disable-undo (current-buffer))
11780   (gnus-configure-windows 'summary)
11781   (and (gnus-visual-p 'summary-highlight 'highlight)
11782        (run-hooks 'gnus-visual-mark-article-hook)))
11783
11784 (defun gnus-summary-respool-query ()
11785   "Query where the respool algorithm would put this article."
11786   (interactive)
11787   (gnus-set-global-variables)
11788   (gnus-summary-select-article)
11789   (save-excursion
11790     (set-buffer gnus-article-buffer)
11791     (save-restriction
11792       (goto-char (point-min))
11793       (search-forward "\n\n")
11794       (narrow-to-region (point-min) (point))
11795       (pp-eval-expression
11796        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11797
11798 ;; Summary marking commands.
11799
11800 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11801   "Mark articles which has the same subject as read, and then select the next.
11802 If UNMARK is positive, remove any kind of mark.
11803 If UNMARK is negative, tick articles."
11804   (interactive "P")
11805   (gnus-set-global-variables)
11806   (if unmark
11807       (setq unmark (prefix-numeric-value unmark)))
11808   (let ((count
11809          (gnus-summary-mark-same-subject
11810           (gnus-summary-article-subject) unmark)))
11811     ;; Select next unread article.  If auto-select-same mode, should
11812     ;; select the first unread article.
11813     (gnus-summary-next-article t (and gnus-auto-select-same
11814                                       (gnus-summary-article-subject)))
11815     (gnus-message 7 "%d article%s marked as %s"
11816                   count (if (= count 1) " is" "s are")
11817                   (if unmark "unread" "read"))))
11818
11819 (defun gnus-summary-kill-same-subject (&optional unmark)
11820   "Mark articles which has the same subject as read.
11821 If UNMARK is positive, remove any kind of mark.
11822 If UNMARK is negative, tick articles."
11823   (interactive "P")
11824   (gnus-set-global-variables)
11825   (if unmark
11826       (setq unmark (prefix-numeric-value unmark)))
11827   (let ((count
11828          (gnus-summary-mark-same-subject
11829           (gnus-summary-article-subject) unmark)))
11830     ;; If marked as read, go to next unread subject.
11831     (if (null unmark)
11832         ;; Go to next unread subject.
11833         (gnus-summary-next-subject 1 t))
11834     (gnus-message 7 "%d articles are marked as %s"
11835                   count (if unmark "unread" "read"))))
11836
11837 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11838   "Mark articles with same SUBJECT as read, and return marked number.
11839 If optional argument UNMARK is positive, remove any kinds of marks.
11840 If optional argument UNMARK is negative, mark articles as unread instead."
11841   (let ((count 1))
11842     (save-excursion
11843       (cond
11844        ((null unmark)                   ; Mark as read.
11845         (while (and
11846                 (progn
11847                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11848                   (gnus-summary-show-thread) t)
11849                 (gnus-summary-find-subject subject))
11850           (setq count (1+ count))))
11851        ((> unmark 0)                    ; Tick.
11852         (while (and
11853                 (progn
11854                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11855                   (gnus-summary-show-thread) t)
11856                 (gnus-summary-find-subject subject))
11857           (setq count (1+ count))))
11858        (t                               ; Mark as unread.
11859         (while (and
11860                 (progn
11861                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11862                   (gnus-summary-show-thread) t)
11863                 (gnus-summary-find-subject subject))
11864           (setq count (1+ count)))))
11865       (gnus-set-mode-line 'summary)
11866       ;; Return the number of marked articles.
11867       count)))
11868
11869 (defun gnus-summary-mark-as-processable (n &optional unmark)
11870   "Set the process mark on the next N articles.
11871 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11872 the process mark instead.  The difference between N and the actual
11873 number of articles marked is returned."
11874   (interactive "p")
11875   (gnus-set-global-variables)
11876   (let ((backward (< n 0))
11877         (n (abs n)))
11878     (while (and
11879             (> n 0)
11880             (if unmark
11881                 (gnus-summary-remove-process-mark
11882                  (gnus-summary-article-number))
11883               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11884             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11885       (setq n (1- n)))
11886     (if (/= 0 n) (gnus-message 7 "No more articles"))
11887     (gnus-summary-recenter)
11888     (gnus-summary-position-point)
11889     n))
11890
11891 (defun gnus-summary-unmark-as-processable (n)
11892   "Remove the process mark from the next N articles.
11893 If N is negative, mark backward instead.  The difference between N and
11894 the actual number of articles marked is returned."
11895   (interactive "p")
11896   (gnus-set-global-variables)
11897   (gnus-summary-mark-as-processable n t))
11898
11899 (defun gnus-summary-unmark-all-processable ()
11900   "Remove the process mark from all articles."
11901   (interactive)
11902   (gnus-set-global-variables)
11903   (save-excursion
11904     (while gnus-newsgroup-processable
11905       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11906   (gnus-summary-position-point))
11907
11908 (defun gnus-summary-mark-as-expirable (n)
11909   "Mark N articles forward as expirable.
11910 If N is negative, mark backward instead.  The difference between N and
11911 the actual number of articles marked is returned."
11912   (interactive "p")
11913   (gnus-set-global-variables)
11914   (gnus-summary-mark-forward n gnus-expirable-mark))
11915
11916 (defun gnus-summary-mark-article-as-replied (article)
11917   "Mark ARTICLE replied and update the summary line."
11918   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11919   (let ((buffer-read-only nil))
11920     (when (gnus-summary-goto-subject article)
11921       (gnus-summary-update-secondary-mark article))))
11922
11923 (defun gnus-summary-set-bookmark (article)
11924   "Set a bookmark in current article."
11925   (interactive (list (gnus-summary-article-number)))
11926   (gnus-set-global-variables)
11927   (if (or (not (get-buffer gnus-article-buffer))
11928           (not gnus-current-article)
11929           (not gnus-article-current)
11930           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11931       (error "No current article selected"))
11932   ;; Remove old bookmark, if one exists.
11933   (let ((old (assq article gnus-newsgroup-bookmarks)))
11934     (if old (setq gnus-newsgroup-bookmarks
11935                   (delq old gnus-newsgroup-bookmarks))))
11936   ;; Set the new bookmark, which is on the form
11937   ;; (article-number . line-number-in-body).
11938   (setq gnus-newsgroup-bookmarks
11939         (cons
11940          (cons article
11941                (save-excursion
11942                  (set-buffer gnus-article-buffer)
11943                  (count-lines
11944                   (min (point)
11945                        (save-excursion
11946                          (goto-char (point-min))
11947                          (search-forward "\n\n" nil t)
11948                          (point)))
11949                   (point))))
11950          gnus-newsgroup-bookmarks))
11951   (gnus-message 6 "A bookmark has been added to the current article."))
11952
11953 (defun gnus-summary-remove-bookmark (article)
11954   "Remove the bookmark from the current article."
11955   (interactive (list (gnus-summary-article-number)))
11956   (gnus-set-global-variables)
11957   ;; Remove old bookmark, if one exists.
11958   (let ((old (assq article gnus-newsgroup-bookmarks)))
11959     (if old
11960         (progn
11961           (setq gnus-newsgroup-bookmarks
11962                 (delq old gnus-newsgroup-bookmarks))
11963           (gnus-message 6 "Removed bookmark."))
11964       (gnus-message 6 "No bookmark in current article."))))
11965
11966 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11967 (defun gnus-summary-mark-as-dormant (n)
11968   "Mark N articles forward as dormant.
11969 If N is negative, mark backward instead.  The difference between N and
11970 the actual number of articles marked is returned."
11971   (interactive "p")
11972   (gnus-set-global-variables)
11973   (gnus-summary-mark-forward n gnus-dormant-mark))
11974
11975 (defun gnus-summary-set-process-mark (article)
11976   "Set the process mark on ARTICLE and update the summary line."
11977   (setq gnus-newsgroup-processable
11978         (cons article
11979               (delq article gnus-newsgroup-processable)))
11980   (when (gnus-summary-goto-subject article)
11981     (gnus-summary-show-thread)
11982     (gnus-summary-update-secondary-mark article)))
11983
11984 (defun gnus-summary-remove-process-mark (article)
11985   "Remove the process mark from ARTICLE and update the summary line."
11986   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11987   (when (gnus-summary-goto-subject article)
11988     (gnus-summary-show-thread)
11989     (gnus-summary-update-secondary-mark article)))
11990
11991 (defun gnus-summary-set-saved-mark (article)
11992   "Set the process mark on ARTICLE and update the summary line."
11993   (push article gnus-newsgroup-saved)
11994   (when (gnus-summary-goto-subject article)
11995     (gnus-summary-update-secondary-mark article)))
11996
11997 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11998   "Mark N articles as read forwards.
11999 If N is negative, mark backwards instead.
12000 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
12001 marked as unread.
12002 The difference between N and the actual number of articles marked is
12003 returned."
12004   (interactive "p")
12005   (gnus-set-global-variables)
12006   (let ((backward (< n 0))
12007         (gnus-summary-goto-unread
12008          (and gnus-summary-goto-unread
12009               (not (eq gnus-summary-goto-unread 'never))
12010               (not (memq mark (list gnus-unread-mark
12011                                     gnus-ticked-mark gnus-dormant-mark)))))
12012         (n (abs n))
12013         (mark (or mark gnus-del-mark)))
12014     (while (and (> n 0)
12015                 (gnus-summary-mark-article nil mark no-expire)
12016                 (zerop (gnus-summary-next-subject
12017                         (if backward -1 1)
12018                         (and gnus-summary-goto-unread
12019                              (not (eq gnus-summary-goto-unread 'never)))
12020                         t)))
12021       (setq n (1- n)))
12022     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12023     (gnus-summary-recenter)
12024     (gnus-summary-position-point)
12025     (gnus-set-mode-line 'summary)
12026     n))
12027
12028 (defun gnus-summary-mark-article-as-read (mark)
12029   "Mark the current article quickly as read with MARK."
12030   (let ((article (gnus-summary-article-number)))
12031     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12032     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12033     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12034     (setq gnus-newsgroup-reads
12035           (cons (cons article mark) gnus-newsgroup-reads))
12036     ;; Possibly remove from cache, if that is used.
12037     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12038     ;; Allow the backend to change the mark.
12039     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12040     ;; Check for auto-expiry.
12041     (when (and gnus-newsgroup-auto-expire
12042                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12043                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12044                    (= mark gnus-ancient-mark)
12045                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12046       (setq mark gnus-expirable-mark)
12047       (push article gnus-newsgroup-expirable))
12048     ;; Set the mark in the buffer.
12049     (gnus-summary-update-mark mark 'unread)
12050     t))
12051
12052 (defun gnus-summary-mark-article-as-unread (mark)
12053   "Mark the current article quickly as unread with MARK."
12054   (let ((article (gnus-summary-article-number)))
12055     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12056     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12057     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12058     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12059     (cond ((= mark gnus-ticked-mark)
12060            (push article gnus-newsgroup-marked))
12061           ((= mark gnus-dormant-mark)
12062            (push article gnus-newsgroup-dormant))
12063           (t
12064            (push article gnus-newsgroup-unreads)))
12065     (setq gnus-newsgroup-reads
12066           (delq (assq article gnus-newsgroup-reads)
12067                 gnus-newsgroup-reads))
12068
12069     ;; See whether the article is to be put in the cache.
12070     (and gnus-use-cache
12071          (vectorp (gnus-summary-article-header article))
12072          (save-excursion
12073            (gnus-cache-possibly-enter-article
12074             gnus-newsgroup-name article
12075             (gnus-summary-article-header article)
12076             (= mark gnus-ticked-mark)
12077             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12078
12079     ;; Fix the mark.
12080     (gnus-summary-update-mark mark 'unread)
12081     t))
12082
12083 (defun gnus-summary-mark-article (&optional article mark no-expire)
12084   "Mark ARTICLE with MARK.  MARK can be any character.
12085 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12086 `??' (dormant) and `?E' (expirable).
12087 If MARK is nil, then the default character `?D' is used.
12088 If ARTICLE is nil, then the article on the current line will be
12089 marked."
12090   ;; The mark might be a string.
12091   (and (stringp mark)
12092        (setq mark (aref mark 0)))
12093   ;; If no mark is given, then we check auto-expiring.
12094   (and (not no-expire)
12095        gnus-newsgroup-auto-expire
12096        (or (not mark)
12097            (and (numberp mark)
12098                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12099                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12100                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12101        (setq mark gnus-expirable-mark))
12102   (let* ((mark (or mark gnus-del-mark))
12103          (article (or article (gnus-summary-article-number))))
12104     (or article (error "No article on current line"))
12105     (if (or (= mark gnus-unread-mark)
12106             (= mark gnus-ticked-mark)
12107             (= mark gnus-dormant-mark))
12108         (gnus-mark-article-as-unread article mark)
12109       (gnus-mark-article-as-read article mark))
12110
12111     ;; See whether the article is to be put in the cache.
12112     (and gnus-use-cache
12113          (not (= mark gnus-canceled-mark))
12114          (vectorp (gnus-summary-article-header article))
12115          (save-excursion
12116            (gnus-cache-possibly-enter-article
12117             gnus-newsgroup-name article
12118             (gnus-summary-article-header article)
12119             (= mark gnus-ticked-mark)
12120             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12121
12122     (if (gnus-summary-goto-subject article nil t)
12123         (let ((buffer-read-only nil))
12124           (gnus-summary-show-thread)
12125           ;; Fix the mark.
12126           (gnus-summary-update-mark mark 'unread)
12127           t))))
12128
12129 (defun gnus-summary-update-secondary-mark (article)
12130   "Update the secondary (read, process, cache) mark."
12131   (gnus-summary-update-mark
12132    (cond ((memq article gnus-newsgroup-processable)
12133           gnus-process-mark)
12134          ((memq article gnus-newsgroup-cached)
12135           gnus-cached-mark)
12136          ((memq article gnus-newsgroup-replied)
12137           gnus-replied-mark)
12138          ((memq article gnus-newsgroup-saved)
12139           gnus-saved-mark)
12140          (t gnus-unread-mark))
12141    'replied)
12142   (when (gnus-visual-p 'summary-highlight 'highlight)
12143     (run-hooks 'gnus-summary-update-hook))
12144   t)
12145
12146 (defun gnus-summary-update-mark (mark type)
12147   (beginning-of-line)
12148   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12149         (buffer-read-only nil))
12150     (when (and forward
12151                (<= (+ forward (point)) (point-max)))
12152       ;; Go to the right position on the line.
12153       (goto-char (+ forward (point)))
12154       ;; Replace the old mark with the new mark.
12155       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12156       ;; Optionally update the marks by some user rule.
12157       (when (eq type 'unread)
12158         (gnus-data-set-mark
12159          (gnus-data-find (gnus-summary-article-number)) mark)
12160         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12161
12162 (defun gnus-mark-article-as-read (article &optional mark)
12163   "Enter ARTICLE in the pertinent lists and remove it from others."
12164   ;; Make the article expirable.
12165   (let ((mark (or mark gnus-del-mark)))
12166     (if (= mark gnus-expirable-mark)
12167         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12168       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12169     ;; Remove from unread and marked lists.
12170     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12171     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12172     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12173     (push (cons article mark) gnus-newsgroup-reads)
12174     ;; Possibly remove from cache, if that is used.
12175     (when gnus-use-cache
12176       (gnus-cache-enter-remove-article article))))
12177
12178 (defun gnus-mark-article-as-unread (article &optional mark)
12179   "Enter ARTICLE in the pertinent lists and remove it from others."
12180   (let ((mark (or mark gnus-ticked-mark)))
12181     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12182     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12183     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12184     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12185     (cond ((= mark gnus-ticked-mark)
12186            (push article gnus-newsgroup-marked))
12187           ((= mark gnus-dormant-mark)
12188            (push article gnus-newsgroup-dormant))
12189           (t
12190            (push article gnus-newsgroup-unreads)))
12191     (setq gnus-newsgroup-reads
12192           (delq (assq article gnus-newsgroup-reads)
12193                 gnus-newsgroup-reads))))
12194
12195 (defalias 'gnus-summary-mark-as-unread-forward
12196   'gnus-summary-tick-article-forward)
12197 (make-obsolete 'gnus-summary-mark-as-unread-forward
12198                'gnus-summary-tick-article-forward)
12199 (defun gnus-summary-tick-article-forward (n)
12200   "Tick N articles forwards.
12201 If N is negative, tick backwards instead.
12202 The difference between N and the number of articles ticked is returned."
12203   (interactive "p")
12204   (gnus-summary-mark-forward n gnus-ticked-mark))
12205
12206 (defalias 'gnus-summary-mark-as-unread-backward
12207   'gnus-summary-tick-article-backward)
12208 (make-obsolete 'gnus-summary-mark-as-unread-backward
12209                'gnus-summary-tick-article-backward)
12210 (defun gnus-summary-tick-article-backward (n)
12211   "Tick N articles backwards.
12212 The difference between N and the number of articles ticked is returned."
12213   (interactive "p")
12214   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12215
12216 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12217 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12218 (defun gnus-summary-tick-article (&optional article clear-mark)
12219   "Mark current article as unread.
12220 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12221 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12222   (interactive)
12223   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12224                                        gnus-ticked-mark)))
12225
12226 (defun gnus-summary-mark-as-read-forward (n)
12227   "Mark N articles as read forwards.
12228 If N is negative, mark backwards instead.
12229 The difference between N and the actual number of articles marked is
12230 returned."
12231   (interactive "p")
12232   (gnus-summary-mark-forward n gnus-del-mark t))
12233
12234 (defun gnus-summary-mark-as-read-backward (n)
12235   "Mark the N articles as read backwards.
12236 The difference between N and the actual number of articles marked is
12237 returned."
12238   (interactive "p")
12239   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12240
12241 (defun gnus-summary-mark-as-read (&optional article mark)
12242   "Mark current article as read.
12243 ARTICLE specifies the article to be marked as read.
12244 MARK specifies a string to be inserted at the beginning of the line."
12245   (gnus-summary-mark-article article mark))
12246
12247 (defun gnus-summary-clear-mark-forward (n)
12248   "Clear marks from N articles forward.
12249 If N is negative, clear backward instead.
12250 The difference between N and the number of marks cleared is returned."
12251   (interactive "p")
12252   (gnus-summary-mark-forward n gnus-unread-mark))
12253
12254 (defun gnus-summary-clear-mark-backward (n)
12255   "Clear marks from N articles backward.
12256 The difference between N and the number of marks cleared is returned."
12257   (interactive "p")
12258   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12259
12260 (defun gnus-summary-mark-unread-as-read ()
12261   "Intended to be used by `gnus-summary-mark-article-hook'."
12262   (when (memq gnus-current-article gnus-newsgroup-unreads)
12263     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12264
12265 (defun gnus-summary-mark-read-and-unread-as-read ()
12266   "Intended to be used by `gnus-summary-mark-article-hook'."
12267   (let ((mark (gnus-summary-article-mark)))
12268     (when (or (gnus-unread-mark-p mark)
12269               (gnus-read-mark-p mark))
12270       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12271
12272 (defun gnus-summary-mark-region-as-read (point mark all)
12273   "Mark all unread articles between point and mark as read.
12274 If given a prefix, mark all articles between point and mark as read,
12275 even ticked and dormant ones."
12276   (interactive "r\nP")
12277   (save-excursion
12278     (let (article)
12279       (goto-char point)
12280       (beginning-of-line)
12281       (while (and
12282               (< (point) mark)
12283               (progn
12284                 (when (or all
12285                           (memq (setq article (gnus-summary-article-number))
12286                                 gnus-newsgroup-unreads))
12287                   (gnus-summary-mark-article article gnus-del-mark))
12288                 t)
12289               (gnus-summary-find-next))))))
12290
12291 (defun gnus-summary-mark-below (score mark)
12292   "Mark articles with score less than SCORE with MARK."
12293   (interactive "P\ncMark: ")
12294   (gnus-set-global-variables)
12295   (setq score (if score
12296                   (prefix-numeric-value score)
12297                 (or gnus-summary-default-score 0)))
12298   (save-excursion
12299     (set-buffer gnus-summary-buffer)
12300     (goto-char (point-min))
12301     (while 
12302         (progn
12303           (and (< (gnus-summary-article-score) score)
12304                (gnus-summary-mark-article nil mark))
12305           (gnus-summary-find-next)))))
12306
12307 (defun gnus-summary-kill-below (&optional score)
12308   "Mark articles with score below SCORE as read."
12309   (interactive "P")
12310   (gnus-set-global-variables)
12311   (gnus-summary-mark-below score gnus-killed-mark))
12312
12313 (defun gnus-summary-clear-above (&optional score)
12314   "Clear all marks from articles with score above SCORE."
12315   (interactive "P")
12316   (gnus-set-global-variables)
12317   (gnus-summary-mark-above score gnus-unread-mark))
12318
12319 (defun gnus-summary-tick-above (&optional score)
12320   "Tick all articles with score above SCORE."
12321   (interactive "P")
12322   (gnus-set-global-variables)
12323   (gnus-summary-mark-above score gnus-ticked-mark))
12324
12325 (defun gnus-summary-mark-above (score mark)
12326   "Mark articles with score over SCORE with MARK."
12327   (interactive "P\ncMark: ")
12328   (gnus-set-global-variables)
12329   (setq score (if score
12330                   (prefix-numeric-value score)
12331                 (or gnus-summary-default-score 0)))
12332   (save-excursion
12333     (set-buffer gnus-summary-buffer)
12334     (goto-char (point-min))
12335     (while (and (progn
12336                   (if (> (gnus-summary-article-score) score)
12337                       (gnus-summary-mark-article nil mark))
12338                   t)
12339                 (gnus-summary-find-next)))))
12340
12341 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12342 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12343 (defun gnus-summary-limit-include-expunged ()
12344   "Display all the hidden articles that were expunged for low scores."
12345   (interactive)
12346   (gnus-set-global-variables)
12347   (let ((buffer-read-only nil))
12348     (let ((scored gnus-newsgroup-scored)
12349           headers h)
12350       (while scored
12351         (or (gnus-summary-goto-subject (caar scored))
12352             (and (setq h (gnus-summary-article-header (caar scored)))
12353                  (< (cdar scored) gnus-summary-expunge-below)
12354                  (setq headers (cons h headers))))
12355         (setq scored (cdr scored)))
12356       (or headers (error "No expunged articles hidden."))
12357       (goto-char (point-min))
12358       (gnus-summary-prepare-unthreaded (nreverse headers)))
12359     (goto-char (point-min))
12360     (gnus-summary-position-point)))
12361
12362 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12363   "Mark all articles not marked as unread in this newsgroup as read.
12364 If prefix argument ALL is non-nil, all articles are marked as read.
12365 If QUIETLY is non-nil, no questions will be asked.
12366 If TO-HERE is non-nil, it should be a point in the buffer.  All
12367 articles before this point will be marked as read.
12368 The number of articles marked as read is returned."
12369   (interactive "P")
12370   (gnus-set-global-variables)
12371   (prog1
12372       (if (or quietly
12373               (not gnus-interactive-catchup) ;Without confirmation?
12374               gnus-expert-user
12375               (gnus-y-or-n-p
12376                (if all
12377                    "Mark absolutely all articles as read? "
12378                  "Mark all unread articles as read? ")))
12379           (if (and not-mark
12380                    (not gnus-newsgroup-adaptive)
12381                    (not gnus-newsgroup-auto-expire))
12382               (progn
12383                 (when all
12384                   (setq gnus-newsgroup-marked nil
12385                         gnus-newsgroup-dormant nil))
12386                 (setq gnus-newsgroup-unreads nil))
12387             ;; We actually mark all articles as canceled, which we
12388             ;; have to do when using auto-expiry or adaptive scoring.
12389             (gnus-summary-show-all-threads)
12390             (if (gnus-summary-first-subject (not all))
12391                 (while (and
12392                         (if to-here (< (point) to-here) t)
12393                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12394                         (gnus-summary-find-next (not all)))))
12395             (unless to-here
12396               (setq gnus-newsgroup-unreads nil))
12397             (gnus-set-mode-line 'summary)))
12398     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12399       (if (and (not to-here) (eq 'nnvirtual (car method)))
12400           (nnvirtual-catchup-group
12401            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12402     (gnus-summary-position-point)))
12403
12404 (defun gnus-summary-catchup-to-here (&optional all)
12405   "Mark all unticked articles before the current one as read.
12406 If ALL is non-nil, also mark ticked and dormant articles as read."
12407   (interactive "P")
12408   (gnus-set-global-variables)
12409   (save-excursion
12410     (gnus-save-hidden-threads
12411       (let ((beg (point)))
12412         ;; We check that there are unread articles.
12413         (when (or all (gnus-summary-find-prev))
12414           (gnus-summary-catchup all t beg)))))
12415   (gnus-summary-position-point))
12416
12417 (defun gnus-summary-catchup-all (&optional quietly)
12418   "Mark all articles in this newsgroup as read."
12419   (interactive "P")
12420   (gnus-set-global-variables)
12421   (gnus-summary-catchup t quietly))
12422
12423 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12424   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12425 If prefix argument ALL is non-nil, all articles are marked as read."
12426   (interactive "P")
12427   (gnus-set-global-variables)
12428   (gnus-summary-catchup all quietly nil 'fast)
12429   ;; Select next newsgroup or exit.
12430   (if (eq gnus-auto-select-next 'quietly)
12431       (gnus-summary-next-group nil)
12432     (gnus-summary-exit)))
12433
12434 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12435   "Mark all articles in this newsgroup as read, and then exit."
12436   (interactive "P")
12437   (gnus-set-global-variables)
12438   (gnus-summary-catchup-and-exit t quietly))
12439
12440 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12441 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12442   "Mark all articles in this group as read and select the next group.
12443 If given a prefix, mark all articles, unread as well as ticked, as
12444 read."
12445   (interactive "P")
12446   (gnus-set-global-variables)
12447   (save-excursion
12448     (gnus-summary-catchup all))
12449   (gnus-summary-next-article t nil nil t))
12450
12451 ;; Thread-based commands.
12452
12453 (defun gnus-summary-articles-in-thread (&optional article)
12454   "Return a list of all articles in the current thread.
12455 If ARTICLE is non-nil, return all articles in the thread that starts
12456 with that article."
12457   (let* ((article (or article (gnus-summary-article-number)))
12458          (data (gnus-data-find-list article))
12459          (top-level (gnus-data-level (car data)))
12460          (top-subject
12461           (cond ((null gnus-thread-operation-ignore-subject)
12462                  (gnus-simplify-subject-re
12463                   (mail-header-subject (gnus-data-header (car data)))))
12464                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12465                  (gnus-simplify-subject-fuzzy
12466                   (mail-header-subject (gnus-data-header (car data)))))
12467                 (t nil)))
12468          (end-point (save-excursion
12469                       (if (gnus-summary-go-to-next-thread) 
12470                           (point) (point-max))))
12471          articles)
12472     (while (and data
12473                 (< (gnus-data-pos (car data)) end-point))
12474       (when (or (not top-subject)
12475                 (string= top-subject
12476                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12477                              (gnus-simplify-subject-fuzzy
12478                               (mail-header-subject
12479                                (gnus-data-header (car data))))
12480                            (gnus-simplify-subject-re
12481                             (mail-header-subject
12482                              (gnus-data-header (car data)))))))
12483         (push (gnus-data-number (car data)) articles))
12484       (unless (and (setq data (cdr data))
12485                    (> (gnus-data-level (car data)) top-level))
12486         (setq data nil)))
12487     ;; Return the list of articles.
12488     (nreverse articles)))
12489
12490 (defun gnus-summary-rethread-current ()
12491   "Rethread the thread the current article is part of."
12492   (interactive)
12493   (gnus-set-global-variables)
12494   (let* ((gnus-show-threads t)
12495          (article (gnus-summary-article-number))
12496          (id (mail-header-id (gnus-summary-article-header)))
12497          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12498     (unless id
12499       (error "No article on the current line"))
12500     (gnus-rebuild-thread id)
12501     (gnus-summary-goto-subject article)))
12502
12503 (defun gnus-summary-reparent-thread ()
12504   "Make current article child of the marked (or previous) article.
12505
12506 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12507 is non-nil or the Subject: of both articles are the same."
12508   (interactive)
12509   (or (not (gnus-group-read-only-p))
12510       (error "The current newsgroup does not support article editing."))
12511   (or (<= (length gnus-newsgroup-processable) 1)
12512       (error "No more than one article may be marked."))
12513   (save-window-excursion
12514     (let ((gnus-article-buffer " *reparent*")
12515           (current-article (gnus-summary-article-number))
12516           ; first grab the marked article, otherwise one line up.
12517           (parent-article (if (not (null gnus-newsgroup-processable))
12518                               (car gnus-newsgroup-processable)
12519                             (save-excursion
12520                               (if (eq (forward-line -1) 0)
12521                                   (gnus-summary-article-number)
12522                                 (error "Beginning of summary buffer."))))))
12523       (or (not (eq current-article parent-article))
12524           (error "An article may not be self-referential."))
12525       (let ((message-id (mail-header-id 
12526                          (gnus-summary-article-header parent-article))))
12527         (or (and message-id (not (equal message-id "")))
12528             (error "No message-id in desired parent."))
12529         (gnus-summary-select-article t t nil current-article)
12530         (set-buffer gnus-article-buffer)
12531         (setq buffer-read-only nil)
12532         (let ((buf (format "%s" (buffer-string))))
12533           (erase-buffer)
12534           (insert buf))
12535         (goto-char (point-min))
12536         (if (search-forward-regexp "^References: " nil t)
12537             (insert message-id " " )
12538           (insert "References: " message-id "\n"))
12539         (or (gnus-request-replace-article current-article
12540                                           (car gnus-article-current)
12541                                           gnus-article-buffer)
12542             (error "Couldn't replace article."))
12543         (set-buffer gnus-summary-buffer)
12544         (gnus-summary-unmark-all-processable)
12545         (gnus-summary-rethread-current)
12546         (gnus-message 3 "Article %d is now the child of article %d."
12547                       current-article parent-article)))))
12548
12549 (defun gnus-summary-toggle-threads (&optional arg)
12550   "Toggle showing conversation threads.
12551 If ARG is positive number, turn showing conversation threads on."
12552   (interactive "P")
12553   (gnus-set-global-variables)
12554   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12555     (setq gnus-show-threads
12556           (if (null arg) (not gnus-show-threads)
12557             (> (prefix-numeric-value arg) 0)))
12558     (gnus-summary-prepare)
12559     (gnus-summary-goto-subject current)
12560     (gnus-summary-position-point)))
12561
12562 (defun gnus-summary-show-all-threads ()
12563   "Show all threads."
12564   (interactive)
12565   (gnus-set-global-variables)
12566   (save-excursion
12567     (let ((buffer-read-only nil))
12568       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12569   (gnus-summary-position-point))
12570
12571 (defun gnus-summary-show-thread ()
12572   "Show thread subtrees.
12573 Returns nil if no thread was there to be shown."
12574   (interactive)
12575   (gnus-set-global-variables)
12576   (let ((buffer-read-only nil)
12577         (orig (point))
12578         ;; first goto end then to beg, to have point at beg after let
12579         (end (progn (end-of-line) (point)))
12580         (beg (progn (beginning-of-line) (point))))
12581     (prog1
12582         ;; Any hidden lines here?
12583         (search-forward "\r" end t)
12584       (subst-char-in-region beg end ?\^M ?\n t)
12585       (goto-char orig)
12586       (gnus-summary-position-point))))
12587
12588 (defun gnus-summary-hide-all-threads ()
12589   "Hide all thread subtrees."
12590   (interactive)
12591   (gnus-set-global-variables)
12592   (save-excursion
12593     (goto-char (point-min))
12594     (gnus-summary-hide-thread)
12595     (while (zerop (gnus-summary-next-thread 1 t))
12596       (gnus-summary-hide-thread)))
12597   (gnus-summary-position-point))
12598
12599 (defun gnus-summary-hide-thread ()
12600   "Hide thread subtrees.
12601 Returns nil if no threads were there to be hidden."
12602   (interactive)
12603   (gnus-set-global-variables)
12604   (let ((buffer-read-only nil)
12605         (start (point))
12606         (article (gnus-summary-article-number)))
12607     (goto-char start)
12608     ;; Go forward until either the buffer ends or the subthread
12609     ;; ends.
12610     (when (and (not (eobp))
12611                (or (zerop (gnus-summary-next-thread 1 t))
12612                    (goto-char (point-max))))
12613       (prog1
12614           (if (and (> (point) start)
12615                    (search-backward "\n" start t))
12616               (progn
12617                 (subst-char-in-region start (point) ?\n ?\^M)
12618                 (gnus-summary-goto-subject article))
12619             (goto-char start)
12620             nil)
12621         ;;(gnus-summary-position-point)
12622         ))))
12623
12624 (defun gnus-summary-go-to-next-thread (&optional previous)
12625   "Go to the same level (or less) next thread.
12626 If PREVIOUS is non-nil, go to previous thread instead.
12627 Return the article number moved to, or nil if moving was impossible."
12628   (let ((level (gnus-summary-thread-level))
12629         (way (if previous -1 1))
12630         (beg (point)))
12631     (forward-line way)
12632     (while (and (not (eobp))
12633                 (< level (gnus-summary-thread-level)))
12634       (forward-line way))
12635     (if (eobp)
12636         (progn
12637           (goto-char beg)
12638           nil)
12639       (setq beg (point))
12640       (prog1
12641           (gnus-summary-article-number)
12642         (goto-char beg)))))
12643
12644 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12645   "Go to the same level (or less) next thread.
12646 If PREVIOUS is non-nil, go to previous thread instead.
12647 Return the article number moved to, or nil if moving was impossible."
12648   (if (and (eq gnus-summary-make-false-root 'dummy)
12649            (gnus-summary-article-intangible-p))
12650       (let ((beg (point)))
12651         (while (and (zerop (forward-line 1))
12652                     (not (gnus-summary-article-intangible-p))
12653                     (not (zerop (save-excursion 
12654                                   (gnus-summary-thread-level))))))
12655         (if (eobp)
12656             (progn
12657               (goto-char beg)
12658               nil)
12659           (point)))
12660     (let* ((level (gnus-summary-thread-level))
12661            (article (gnus-summary-article-number))
12662            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12663            oart)
12664       (while data
12665         (if (<= (gnus-data-level (car data)) level)
12666             (setq oart (gnus-data-number (car data))
12667                   data nil)
12668           (setq data (cdr data))))
12669       (and oart
12670            (gnus-summary-goto-subject oart)))))
12671
12672 (defun gnus-summary-next-thread (n &optional silent)
12673   "Go to the same level next N'th thread.
12674 If N is negative, search backward instead.
12675 Returns the difference between N and the number of skips actually
12676 done.
12677
12678 If SILENT, don't output messages."
12679   (interactive "p")
12680   (gnus-set-global-variables)
12681   (let ((backward (< n 0))
12682         (n (abs n))
12683         old dum int)
12684     (while (and (> n 0)
12685                 (gnus-summary-go-to-next-thread backward))
12686       (decf n))
12687     (unless silent 
12688       (gnus-summary-position-point))
12689     (when (and (not silent) (/= 0 n))
12690       (gnus-message 7 "No more threads"))
12691     n))
12692
12693 (defun gnus-summary-prev-thread (n)
12694   "Go to the same level previous N'th thread.
12695 Returns the difference between N and the number of skips actually
12696 done."
12697   (interactive "p")
12698   (gnus-set-global-variables)
12699   (gnus-summary-next-thread (- n)))
12700
12701 (defun gnus-summary-go-down-thread ()
12702   "Go down one level in the current thread."
12703   (let ((children (gnus-summary-article-children)))
12704     (and children
12705          (gnus-summary-goto-subject (car children)))))
12706
12707 (defun gnus-summary-go-up-thread ()
12708   "Go up one level in the current thread."
12709   (let ((parent (gnus-summary-article-parent)))
12710     (and parent
12711          (gnus-summary-goto-subject parent))))
12712
12713 (defun gnus-summary-down-thread (n)
12714   "Go down thread N steps.
12715 If N is negative, go up instead.
12716 Returns the difference between N and how many steps down that were
12717 taken."
12718   (interactive "p")
12719   (gnus-set-global-variables)
12720   (let ((up (< n 0))
12721         (n (abs n)))
12722     (while (and (> n 0)
12723                 (if up (gnus-summary-go-up-thread)
12724                   (gnus-summary-go-down-thread)))
12725       (setq n (1- n)))
12726     (gnus-summary-position-point)
12727     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12728     n))
12729
12730 (defun gnus-summary-up-thread (n)
12731   "Go up thread N steps.
12732 If N is negative, go up instead.
12733 Returns the difference between N and how many steps down that were
12734 taken."
12735   (interactive "p")
12736   (gnus-set-global-variables)
12737   (gnus-summary-down-thread (- n)))
12738
12739 (defun gnus-summary-top-thread ()
12740   "Go to the top of the thread."
12741   (interactive)
12742   (gnus-set-global-variables)
12743   (while (gnus-summary-go-up-thread))
12744   (gnus-summary-article-number))
12745
12746 (defun gnus-summary-kill-thread (&optional unmark)
12747   "Mark articles under current thread as read.
12748 If the prefix argument is positive, remove any kinds of marks.
12749 If the prefix argument is negative, tick articles instead."
12750   (interactive "P")
12751   (gnus-set-global-variables)
12752   (if unmark
12753       (setq unmark (prefix-numeric-value unmark)))
12754   (let ((articles (gnus-summary-articles-in-thread)))
12755     (save-excursion
12756       ;; Expand the thread.
12757       (gnus-summary-show-thread)
12758       ;; Mark all the articles.
12759       (while articles
12760         (gnus-summary-goto-subject (car articles))
12761         (cond ((null unmark)
12762                (gnus-summary-mark-article-as-read gnus-killed-mark))
12763               ((> unmark 0)
12764                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12765               (t
12766                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12767         (setq articles (cdr articles))))
12768     ;; Hide killed subtrees.
12769     (and (null unmark)
12770          gnus-thread-hide-killed
12771          (gnus-summary-hide-thread))
12772     ;; If marked as read, go to next unread subject.
12773     (if (null unmark)
12774         ;; Go to next unread subject.
12775         (gnus-summary-next-subject 1 t)))
12776   (gnus-set-mode-line 'summary))
12777
12778 ;; Summary sorting commands
12779
12780 (defun gnus-summary-sort-by-number (&optional reverse)
12781   "Sort summary buffer by article number.
12782 Argument REVERSE means reverse order."
12783   (interactive "P")
12784   (gnus-summary-sort 'number reverse))
12785
12786 (defun gnus-summary-sort-by-author (&optional reverse)
12787   "Sort summary buffer by author name alphabetically.
12788 If case-fold-search is non-nil, case of letters is ignored.
12789 Argument REVERSE means reverse order."
12790   (interactive "P")
12791   (gnus-summary-sort 'author reverse))
12792
12793 (defun gnus-summary-sort-by-subject (&optional reverse)
12794   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12795 If case-fold-search is non-nil, case of letters is ignored.
12796 Argument REVERSE means reverse order."
12797   (interactive "P")
12798   (gnus-summary-sort 'subject reverse))
12799
12800 (defun gnus-summary-sort-by-date (&optional reverse)
12801   "Sort summary buffer by date.
12802 Argument REVERSE means reverse order."
12803   (interactive "P")
12804   (gnus-summary-sort 'date reverse))
12805
12806 (defun gnus-summary-sort-by-score (&optional reverse)
12807   "Sort summary buffer by score.
12808 Argument REVERSE means reverse order."
12809   (interactive "P")
12810   (gnus-summary-sort 'score reverse))
12811
12812 (defun gnus-summary-sort (predicate reverse)
12813   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12814   (gnus-set-global-variables)
12815   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12816          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12817          (gnus-thread-sort-functions
12818           (list
12819            (if (not reverse)
12820                thread
12821              `(lambda (t1 t2)
12822                 (,thread t2 t1)))))
12823          (gnus-article-sort-functions
12824           (list
12825            (if (not reverse)
12826                article
12827              `(lambda (t1 t2)
12828                 (,article t2 t1)))))
12829          (buffer-read-only)
12830          (gnus-summary-prepare-hook nil))
12831     ;; We do the sorting by regenerating the threads.
12832     (gnus-summary-prepare)
12833     ;; Hide subthreads if needed.
12834     (when (and gnus-show-threads gnus-thread-hide-subtree)
12835       (gnus-summary-hide-all-threads)))
12836   ;; If in async mode, we send some info to the backend.
12837   (when gnus-newsgroup-async
12838     (gnus-request-asynchronous
12839      gnus-newsgroup-name gnus-newsgroup-data)))
12840
12841 (defun gnus-sortable-date (date)
12842   "Make sortable string by string-lessp from DATE.
12843 Timezone package is used."
12844   (condition-case ()
12845       (progn
12846         (setq date (inline (timezone-fix-time 
12847                             date nil 
12848                             (aref (inline (timezone-parse-date date)) 4))))
12849         (inline
12850           (timezone-make-sortable-date
12851            (aref date 0) (aref date 1) (aref date 2)
12852            (inline
12853              (timezone-make-time-string
12854               (aref date 3) (aref date 4) (aref date 5))))))
12855     (error "")))
12856   
12857 ;; Summary saving commands.
12858
12859 (defun gnus-summary-save-article (&optional n not-saved)
12860   "Save the current article using the default saver function.
12861 If N is a positive number, save the N next articles.
12862 If N is a negative number, save the N previous articles.
12863 If N is nil and any articles have been marked with the process mark,
12864 save those articles instead.
12865 The variable `gnus-default-article-saver' specifies the saver function."
12866   (interactive "P")
12867   (gnus-set-global-variables)
12868   (let ((articles (gnus-summary-work-articles n))
12869         file header article)
12870     (while articles
12871       (setq header (gnus-summary-article-header
12872                     (setq article (pop articles))))
12873       (if (not (vectorp header))
12874           ;; This is a pseudo-article.
12875           (if (assq 'name header)
12876               (gnus-copy-file (cdr (assq 'name header)))
12877             (gnus-message 1 "Article %d is unsaveable" article))
12878         ;; This is a real article.
12879         (save-window-excursion
12880           (gnus-summary-select-article t nil nil article))
12881         (unless gnus-save-all-headers
12882           ;; Remove headers accoring to `gnus-saved-headers'.
12883           (let ((gnus-visible-headers
12884                  (or gnus-saved-headers gnus-visible-headers)))
12885             (gnus-article-hide-headers nil t)))
12886         ;; Remove any X-Gnus lines.
12887         (save-excursion
12888           (set-buffer gnus-article-buffer)
12889           (save-restriction
12890             (let ((buffer-read-only nil))
12891               (nnheader-narrow-to-headers)
12892               (while (re-search-forward "^X-Gnus" nil t)
12893                 (gnus-delete-line)))))
12894         (save-window-excursion
12895           (if (not gnus-default-article-saver)
12896               (error "No default saver is defined.")
12897             (setq file (funcall
12898                         gnus-default-article-saver
12899                         (cond
12900                          ((not gnus-prompt-before-saving)
12901                           'default)
12902                          ((eq gnus-prompt-before-saving 'always)
12903                           nil)
12904                          (t file))))))
12905         (gnus-summary-remove-process-mark article)
12906         (unless not-saved
12907           (gnus-summary-set-saved-mark article))))
12908     (gnus-summary-position-point)
12909     n))
12910
12911 (defun gnus-summary-pipe-output (&optional arg)
12912   "Pipe the current article to a subprocess.
12913 If N is a positive number, pipe the N next articles.
12914 If N is a negative number, pipe the N previous articles.
12915 If N is nil and any articles have been marked with the process mark,
12916 pipe those articles instead."
12917   (interactive "P")
12918   (gnus-set-global-variables)
12919   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12920     (gnus-summary-save-article arg t))
12921   (gnus-configure-windows 'pipe))
12922
12923 (defun gnus-summary-save-article-mail (&optional arg)
12924   "Append the current article to an mail file.
12925 If N is a positive number, save the N next articles.
12926 If N is a negative number, save the N previous articles.
12927 If N is nil and any articles have been marked with the process mark,
12928 save those articles instead."
12929   (interactive "P")
12930   (gnus-set-global-variables)
12931   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12932     (gnus-summary-save-article arg)))
12933
12934 (defun gnus-summary-save-article-rmail (&optional arg)
12935   "Append the current article to an rmail file.
12936 If N is a positive number, save the N next articles.
12937 If N is a negative number, save the N previous articles.
12938 If N is nil and any articles have been marked with the process mark,
12939 save those articles instead."
12940   (interactive "P")
12941   (gnus-set-global-variables)
12942   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12943     (gnus-summary-save-article arg)))
12944
12945 (defun gnus-summary-save-article-file (&optional arg)
12946   "Append the current article to a file.
12947 If N is a positive number, save the N next articles.
12948 If N is a negative number, save the N previous articles.
12949 If N is nil and any articles have been marked with the process mark,
12950 save those articles instead."
12951   (interactive "P")
12952   (gnus-set-global-variables)
12953   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12954     (gnus-summary-save-article arg)))
12955
12956 (defun gnus-summary-save-article-body-file (&optional arg)
12957   "Append the current article body to a file.
12958 If N is a positive number, save the N next articles.
12959 If N is a negative number, save the N previous articles.
12960 If N is nil and any articles have been marked with the process mark,
12961 save those articles instead."
12962   (interactive "P")
12963   (gnus-set-global-variables)
12964   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12965     (gnus-summary-save-article arg)))
12966
12967 (defun gnus-get-split-value (methods)
12968   "Return a value based on the split METHODS."
12969   (let (split-name method result match)
12970     (when methods
12971       (save-excursion
12972         (set-buffer gnus-original-article-buffer)
12973         (save-restriction
12974           (nnheader-narrow-to-headers)
12975           (while methods
12976             (goto-char (point-min))
12977             (setq method (pop methods))
12978             (setq match (car method))
12979             (when (cond
12980                    ((stringp match)
12981                     ;; Regular expression.
12982                     (condition-case ()
12983                         (re-search-forward match nil t)
12984                       (error nil)))
12985                    ((gnus-functionp match)
12986                     ;; Function.
12987                     (save-restriction
12988                       (widen)
12989                       (setq result (funcall match gnus-newsgroup-name))))
12990                    ((consp match)
12991                     ;; Form.
12992                     (save-restriction
12993                       (widen)
12994                       (setq result (eval match)))))
12995               (setq split-name (append (cdr method) split-name))
12996               (cond ((stringp result)
12997                      (push result split-name))
12998                     ((consp result)
12999                      (setq split-name (append result split-name)))))))))
13000     split-name))
13001
13002 (defun gnus-read-move-group-name (prompt default articles prefix)
13003   "Read a group name."
13004   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13005          (minibuffer-confirm-incomplete nil) ; XEmacs
13006          group-map
13007          (dum (mapatoms
13008                (lambda (g) 
13009                  (and (boundp g)
13010                       (symbol-name g)
13011                       (memq 'respool
13012                             (assoc (symbol-name
13013                                     (car (gnus-find-method-for-group
13014                                           (symbol-name g))))
13015                                    gnus-valid-select-methods))
13016                       (push (list (symbol-name g)) group-map)))
13017                gnus-active-hashtb))
13018          (prom
13019           (format "%s %s to:"
13020                   prompt
13021                   (if (> (length articles) 1)
13022                       (format "these %d articles" (length articles))
13023                     "this article")))
13024          (to-newsgroup
13025           (cond
13026            ((null split-name)
13027             (gnus-completing-read default prom
13028                                   group-map nil nil prefix
13029                                   'gnus-group-history))
13030            ((= 1 (length split-name))
13031             (gnus-completing-read (car split-name) prom group-map
13032                                   nil nil nil
13033                                   'gnus-group-history))
13034            (t
13035             (gnus-completing-read nil prom 
13036                                   (mapcar (lambda (el) (list el))
13037                                           (nreverse split-name))
13038                                   nil nil nil
13039                                   'gnus-group-history)))))
13040     (when to-newsgroup
13041       (if (or (string= to-newsgroup "")
13042               (string= to-newsgroup prefix))
13043           (setq to-newsgroup (or default "")))
13044       (or (gnus-active to-newsgroup)
13045           (gnus-activate-group to-newsgroup)
13046           (error "No such group: %s" to-newsgroup)))
13047     to-newsgroup))
13048
13049 (defun gnus-read-save-file-name (prompt default-name)
13050   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13051          (file
13052           ;; Let the split methods have their say.
13053           (cond
13054            ;; No split name was found.
13055            ((null split-name)
13056             (read-file-name
13057              (concat prompt " (default "
13058                      (file-name-nondirectory default-name) ") ")
13059              (file-name-directory default-name)
13060              default-name))
13061            ;; A single split name was found
13062            ((= 1 (length split-name))
13063             (read-file-name
13064              (concat prompt " (default " (car split-name) ") ")
13065              gnus-article-save-directory
13066              (concat gnus-article-save-directory (car split-name))))
13067            ;; A list of splits was found.
13068            (t
13069             (setq split-name (nreverse split-name))
13070             (let (result)
13071               (let ((file-name-history (nconc split-name file-name-history)))
13072                 (setq result
13073                       (read-file-name
13074                        (concat prompt " (`M-p' for defaults) ")
13075                        gnus-article-save-directory
13076                        (car split-name))))
13077               (car (push result file-name-history)))))))
13078     ;; If we have read a directory, we append the default file name.
13079     (when (file-directory-p file)
13080       (setq file (concat (file-name-as-directory file)
13081                          (file-name-nondirectory default-name))))
13082     ;; Possibly translate some charaters.
13083     (nnheader-translate-file-chars file)))
13084
13085 (defun gnus-article-archive-name (group)
13086   "Return the first instance of an \"Archive-name\" in the current buffer."
13087   (let ((case-fold-search t))
13088     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13089       (match-string 1))))
13090
13091 (defun gnus-summary-save-in-rmail (&optional filename)
13092   "Append this article to Rmail file.
13093 Optional argument FILENAME specifies file name.
13094 Directory to save to is default to `gnus-article-save-directory'."
13095   (interactive)
13096   (gnus-set-global-variables)
13097   (let ((default-name
13098           (funcall gnus-rmail-save-name gnus-newsgroup-name
13099                    gnus-current-headers gnus-newsgroup-last-rmail)))
13100     (setq filename
13101           (cond ((eq filename 'default)
13102                  default-name)
13103                 (filename filename)
13104                 (t (gnus-read-save-file-name
13105                     "Save in rmail file:" default-name))))
13106     (gnus-make-directory (file-name-directory filename))
13107     (gnus-eval-in-buffer-window
13108      gnus-original-article-buffer
13109      (save-excursion
13110        (save-restriction
13111          (widen)
13112          (gnus-output-to-rmail filename))))
13113     ;; Remember the directory name to save articles
13114     (setq gnus-newsgroup-last-rmail filename)))
13115
13116 (defun gnus-summary-save-in-mail (&optional filename)
13117   "Append this article to Unix mail file.
13118 Optional argument FILENAME specifies file name.
13119 Directory to save to is default to `gnus-article-save-directory'."
13120   (interactive)
13121   (gnus-set-global-variables)
13122   (let ((default-name
13123           (funcall gnus-mail-save-name gnus-newsgroup-name
13124                    gnus-current-headers gnus-newsgroup-last-mail)))
13125     (setq filename
13126           (cond ((eq filename 'default)
13127                  default-name)
13128                 (filename filename)
13129                 (t (gnus-read-save-file-name
13130                     "Save in Unix mail file:" default-name))))
13131     (setq filename
13132           (expand-file-name filename
13133                             (and default-name
13134                                  (file-name-directory default-name))))
13135     (gnus-make-directory (file-name-directory filename))
13136     (gnus-eval-in-buffer-window
13137      gnus-original-article-buffer
13138      (save-excursion
13139        (save-restriction
13140          (widen)
13141          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13142              (gnus-output-to-rmail filename)
13143            (let ((mail-use-rfc822 t))
13144              (rmail-output filename 1 t t))))))
13145     ;; Remember the directory name to save articles.
13146     (setq gnus-newsgroup-last-mail filename)))
13147
13148 (defun gnus-summary-save-in-file (&optional filename)
13149   "Append this article to file.
13150 Optional argument FILENAME specifies file name.
13151 Directory to save to is default to `gnus-article-save-directory'."
13152   (interactive)
13153   (gnus-set-global-variables)
13154   (let ((default-name
13155           (funcall gnus-file-save-name gnus-newsgroup-name
13156                    gnus-current-headers gnus-newsgroup-last-file)))
13157     (setq filename
13158           (cond ((eq filename 'default)
13159                  default-name)
13160                 (filename filename)
13161                 (t (gnus-read-save-file-name
13162                     "Save in file:" default-name))))
13163     (gnus-make-directory (file-name-directory filename))
13164     (gnus-eval-in-buffer-window
13165      gnus-original-article-buffer
13166      (save-excursion
13167        (save-restriction
13168          (widen)
13169          (gnus-output-to-file filename))))
13170     ;; Remember the directory name to save articles.
13171     (setq gnus-newsgroup-last-file filename)))
13172
13173 (defun gnus-summary-save-body-in-file (&optional filename)
13174   "Append this article body to a file.
13175 Optional argument FILENAME specifies file name.
13176 The directory to save in defaults to `gnus-article-save-directory'."
13177   (interactive)
13178   (gnus-set-global-variables)
13179   (let ((default-name
13180           (funcall gnus-file-save-name gnus-newsgroup-name
13181                    gnus-current-headers gnus-newsgroup-last-file)))
13182     (setq filename
13183           (cond ((eq filename 'default)
13184                  default-name)
13185                 (filename filename)
13186                 (t (gnus-read-save-file-name
13187                     "Save body in file:" default-name))))
13188     (gnus-make-directory (file-name-directory filename))
13189     (gnus-eval-in-buffer-window
13190      gnus-article-buffer
13191      (save-excursion
13192        (save-restriction
13193          (widen)
13194          (goto-char (point-min))
13195          (and (search-forward "\n\n" nil t)
13196               (narrow-to-region (point) (point-max)))
13197          (gnus-output-to-file filename))))
13198     ;; Remember the directory name to save articles.
13199     (setq gnus-newsgroup-last-file filename)))
13200
13201 (defun gnus-summary-save-in-pipe (&optional command)
13202   "Pipe this article to subprocess."
13203   (interactive)
13204   (gnus-set-global-variables)
13205   (setq command
13206         (cond ((eq command 'default)
13207                gnus-last-shell-command)
13208               (command command)
13209               (t (read-string "Shell command on article: "
13210                               gnus-last-shell-command))))
13211   (if (string-equal command "")
13212       (setq command gnus-last-shell-command))
13213   (gnus-eval-in-buffer-window
13214    gnus-article-buffer
13215    (save-restriction
13216      (widen)
13217      (shell-command-on-region (point-min) (point-max) command nil)))
13218   (setq gnus-last-shell-command command))
13219
13220 ;; Summary extract commands
13221
13222 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13223   (let ((buffer-read-only nil)
13224         (article (gnus-summary-article-number))
13225         after-article b e)
13226     (or (gnus-summary-goto-subject article)
13227         (error (format "No such article: %d" article)))
13228     (gnus-summary-position-point)
13229     ;; If all commands are to be bunched up on one line, we collect
13230     ;; them here.
13231     (if gnus-view-pseudos-separately
13232         ()
13233       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13234             files action)
13235         (while ps
13236           (setq action (cdr (assq 'action (car ps))))
13237           (setq files (list (cdr (assq 'name (car ps)))))
13238           (while (and ps (cdr ps)
13239                       (string= (or action "1")
13240                                (or (cdr (assq 'action (cadr ps))) "2")))
13241             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13242             (setcdr ps (cddr ps)))
13243           (if (not files)
13244               ()
13245             (if (not (string-match "%s" action))
13246                 (setq files (cons " " files)))
13247             (setq files (cons " " files))
13248             (and (assq 'execute (car ps))
13249                  (setcdr (assq 'execute (car ps))
13250                          (funcall (if (string-match "%s" action)
13251                                       'format 'concat)
13252                                   action
13253                                   (mapconcat (lambda (f) f) files " ")))))
13254           (setq ps (cdr ps)))))
13255     (if (and gnus-view-pseudos (not not-view))
13256         (while pslist
13257           (and (assq 'execute (car pslist))
13258                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13259                                      (eq gnus-view-pseudos 'not-confirm)))
13260           (setq pslist (cdr pslist)))
13261       (save-excursion
13262         (while pslist
13263           (setq after-article (or (cdr (assq 'article (car pslist)))
13264                                   (gnus-summary-article-number)))
13265           (gnus-summary-goto-subject after-article)
13266           (forward-line 1)
13267           (setq b (point))
13268           (insert "    " (file-name-nondirectory
13269                                 (cdr (assq 'name (car pslist))))
13270                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13271           (setq e (point))
13272           (forward-line -1)             ; back to `b'
13273           (gnus-add-text-properties
13274            b (1- e) (list 'gnus-number gnus-reffed-article-number
13275                           gnus-mouse-face-prop gnus-mouse-face))
13276           (gnus-data-enter
13277            after-article gnus-reffed-article-number
13278            gnus-unread-mark b (car pslist) 0 (- e b))
13279           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13280           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13281           (setq pslist (cdr pslist)))))))
13282
13283 (defun gnus-pseudos< (p1 p2)
13284   (let ((c1 (cdr (assq 'action p1)))
13285         (c2 (cdr (assq 'action p2))))
13286     (and c1 c2 (string< c1 c2))))
13287
13288 (defun gnus-request-pseudo-article (props)
13289   (cond ((assq 'execute props)
13290          (gnus-execute-command (cdr (assq 'execute props)))))
13291   (let ((gnus-current-article (gnus-summary-article-number)))
13292     (run-hooks 'gnus-mark-article-hook)))
13293
13294 (defun gnus-execute-command (command &optional automatic)
13295   (save-excursion
13296     (gnus-article-setup-buffer)
13297     (set-buffer gnus-article-buffer)
13298     (setq buffer-read-only nil)
13299     (let ((command (if automatic command (read-string "Command: " command)))
13300           ;; Just binding this here doesn't help, because there might
13301           ;; be output from the process after exiting the scope of 
13302           ;; this `let'.
13303           ;; (buffer-read-only nil)
13304           )
13305       (erase-buffer)
13306       (insert "$ " command "\n\n")
13307       (if gnus-view-pseudo-asynchronously
13308           (start-process "gnus-execute" nil "sh" "-c" command)
13309         (call-process "sh" nil t nil "-c" command)))))
13310
13311 (defun gnus-copy-file (file &optional to)
13312   "Copy FILE to TO."
13313   (interactive
13314    (list (read-file-name "Copy file: " default-directory)
13315          (read-file-name "Copy file to: " default-directory)))
13316   (gnus-set-global-variables)
13317   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13318   (and (file-directory-p to)
13319        (setq to (concat (file-name-as-directory to)
13320                         (file-name-nondirectory file))))
13321   (copy-file file to))
13322
13323 ;; Summary kill commands.
13324
13325 (defun gnus-summary-edit-global-kill (article)
13326   "Edit the \"global\" kill file."
13327   (interactive (list (gnus-summary-article-number)))
13328   (gnus-set-global-variables)
13329   (gnus-group-edit-global-kill article))
13330
13331 (defun gnus-summary-edit-local-kill ()
13332   "Edit a local kill file applied to the current newsgroup."
13333   (interactive)
13334   (gnus-set-global-variables)
13335   (setq gnus-current-headers (gnus-summary-article-header))
13336   (gnus-set-global-variables)
13337   (gnus-group-edit-local-kill
13338    (gnus-summary-article-number) gnus-newsgroup-name))
13339
13340 \f
13341 ;;;
13342 ;;; Gnus article mode
13343 ;;;
13344
13345 (put 'gnus-article-mode 'mode-class 'special)
13346
13347 (if gnus-article-mode-map
13348     nil
13349   (setq gnus-article-mode-map (make-keymap))
13350   (suppress-keymap gnus-article-mode-map)
13351
13352   (gnus-define-keys gnus-article-mode-map
13353     " " gnus-article-goto-next-page
13354     "\177" gnus-article-goto-prev-page
13355     [delete] gnus-article-goto-prev-page
13356     "\C-c^" gnus-article-refer-article
13357     "h" gnus-article-show-summary
13358     "s" gnus-article-show-summary
13359     "\C-c\C-m" gnus-article-mail
13360     "?" gnus-article-describe-briefly
13361     gnus-mouse-2 gnus-article-push-button
13362     "\r" gnus-article-press-button
13363     "\t" gnus-article-next-button
13364     "\M-\t" gnus-article-prev-button
13365     "\C-c\C-b" gnus-bug)
13366
13367   (substitute-key-definition
13368    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13369
13370 (defun gnus-article-mode ()
13371   "Major mode for displaying an article.
13372
13373 All normal editing commands are switched off.
13374
13375 The following commands are available:
13376
13377 \\<gnus-article-mode-map>
13378 \\[gnus-article-next-page]\t Scroll the article one page forwards
13379 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13380 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13381 \\[gnus-article-show-summary]\t Display the summary buffer
13382 \\[gnus-article-mail]\t Send a reply to the address near point
13383 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13384 \\[gnus-info-find-node]\t Go to the Gnus info node"
13385   (interactive)
13386   (when (and menu-bar-mode
13387              (gnus-visual-p 'article-menu 'menu))
13388     (gnus-article-make-menu-bar))
13389   (kill-all-local-variables)
13390   (gnus-simplify-mode-line)
13391   (setq mode-name "Article")
13392   (setq major-mode 'gnus-article-mode)
13393   (make-local-variable 'minor-mode-alist)
13394   (or (assq 'gnus-show-mime minor-mode-alist)
13395       (setq minor-mode-alist
13396             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13397   (use-local-map gnus-article-mode-map)
13398   (make-local-variable 'page-delimiter)
13399   (setq page-delimiter gnus-page-delimiter)
13400   (buffer-disable-undo (current-buffer))
13401   (setq buffer-read-only t)             ;Disable modification
13402   (run-hooks 'gnus-article-mode-hook))
13403
13404 (defun gnus-article-setup-buffer ()
13405   "Initialize the article buffer."
13406   (let* ((name (if gnus-single-article-buffer "*Article*"
13407                  (concat "*Article " gnus-newsgroup-name "*")))
13408          (original
13409           (progn (string-match "\\*Article" name)
13410                  (concat " *Original Article"
13411                          (substring name (match-end 0))))))
13412     (setq gnus-article-buffer name)
13413     (setq gnus-original-article-buffer original)
13414     ;; This might be a variable local to the summary buffer.
13415     (unless gnus-single-article-buffer
13416       (save-excursion
13417         (set-buffer gnus-summary-buffer)
13418         (setq gnus-article-buffer name)
13419         (setq gnus-original-article-buffer original)
13420         (gnus-set-global-variables))
13421       (make-local-variable 'gnus-summary-buffer))
13422     ;; Init original article buffer.
13423     (save-excursion
13424       (set-buffer (get-buffer-create gnus-original-article-buffer))
13425       (buffer-disable-undo (current-buffer))
13426       (setq major-mode 'gnus-original-article-mode)
13427       (make-local-variable 'gnus-original-article))
13428     (if (get-buffer name)
13429         (save-excursion
13430           (set-buffer name)
13431           (buffer-disable-undo (current-buffer))
13432           (setq buffer-read-only t)
13433           (gnus-add-current-to-buffer-list)
13434           (or (eq major-mode 'gnus-article-mode)
13435               (gnus-article-mode))
13436           (current-buffer))
13437       (save-excursion
13438         (set-buffer (get-buffer-create name))
13439         (gnus-add-current-to-buffer-list)
13440         (gnus-article-mode)
13441         (current-buffer)))))
13442
13443 ;; Set article window start at LINE, where LINE is the number of lines
13444 ;; from the head of the article.
13445 (defun gnus-article-set-window-start (&optional line)
13446   (set-window-start
13447    (get-buffer-window gnus-article-buffer t)
13448    (save-excursion
13449      (set-buffer gnus-article-buffer)
13450      (goto-char (point-min))
13451      (if (not line)
13452          (point-min)
13453        (gnus-message 6 "Moved to bookmark")
13454        (search-forward "\n\n" nil t)
13455        (forward-line line)
13456        (point)))))
13457
13458 (defun gnus-kill-all-overlays ()
13459   "Delete all overlays in the current buffer."
13460   (when (fboundp 'overlay-lists)
13461     (let* ((overlayss (overlay-lists))
13462            (buffer-read-only nil)
13463            (overlays (nconc (car overlayss) (cdr overlayss))))
13464       (while overlays
13465         (delete-overlay (pop overlays))))))
13466
13467 (defun gnus-request-article-this-buffer (article group)
13468   "Get an article and insert it into this buffer."
13469   (let (do-update-line)
13470     (prog1
13471         (save-excursion
13472           (erase-buffer)
13473           (gnus-kill-all-overlays)
13474           (setq group (or group gnus-newsgroup-name))
13475
13476           ;; Open server if it has closed.
13477           (gnus-check-server (gnus-find-method-for-group group))
13478
13479           ;; Using `gnus-request-article' directly will insert the article into
13480           ;; `nntp-server-buffer' - so we'll save some time by not having to
13481           ;; copy it from the server buffer into the article buffer.
13482
13483           ;; We only request an article by message-id when we do not have the
13484           ;; headers for it, so we'll have to get those.
13485           (when (stringp article)
13486             (let ((gnus-override-method gnus-refer-article-method))
13487               (gnus-read-header article)))
13488
13489           ;; If the article number is negative, that means that this article
13490           ;; doesn't belong in this newsgroup (possibly), so we find its
13491           ;; message-id and request it by id instead of number.
13492           (when (and (numberp article)
13493                      gnus-summary-buffer
13494                      (get-buffer gnus-summary-buffer)
13495                      (buffer-name (get-buffer gnus-summary-buffer)))
13496             (save-excursion
13497               (set-buffer gnus-summary-buffer)
13498               (let ((header (gnus-summary-article-header article)))
13499                 (if (< article 0)
13500                     (cond 
13501                      ((memq article gnus-newsgroup-sparse)
13502                       ;; This is a sparse gap article.
13503                       (setq do-update-line article)
13504                       (setq article (mail-header-id header))
13505                       (let ((gnus-override-method gnus-refer-article-method))
13506                         (gnus-read-header article))
13507                       (setq gnus-newsgroup-sparse
13508                             (delq article gnus-newsgroup-sparse)))
13509                      ((vectorp header)
13510                       ;; It's a real article.
13511                       (setq article (mail-header-id header)))
13512                      (t
13513                       ;; It is an extracted pseudo-article.
13514                       (setq article 'pseudo)
13515                       (gnus-request-pseudo-article header))))
13516                 
13517                 (let ((method (gnus-find-method-for-group 
13518                                gnus-newsgroup-name)))
13519                   (if (not (eq (car method) 'nneething))
13520                       ()
13521                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13522                                        (mail-header-subject header))))
13523                       (if (file-directory-p dir)
13524                           (progn
13525                             (setq article 'nneething)
13526                             (gnus-group-enter-directory dir)))))))))
13527
13528           (cond
13529            ;; Refuse to select canceled articles.
13530            ((and (numberp article)
13531                  gnus-summary-buffer
13532                  (get-buffer gnus-summary-buffer)
13533                  (buffer-name (get-buffer gnus-summary-buffer))
13534                  (eq (cdr (save-excursion
13535                             (set-buffer gnus-summary-buffer)
13536                             (assq article gnus-newsgroup-reads)))
13537                      gnus-canceled-mark))
13538             nil)
13539            ;; We first check `gnus-original-article-buffer'.
13540            ((and (get-buffer gnus-original-article-buffer)
13541                  (numberp article)
13542                  (save-excursion
13543                    (set-buffer gnus-original-article-buffer)
13544                    (and (equal (car gnus-original-article) group)
13545                         (eq (cdr gnus-original-article) article))))
13546             (insert-buffer-substring gnus-original-article-buffer)
13547             'article)
13548            ;; Check the backlog.
13549            ((and gnus-keep-backlog
13550                  (gnus-backlog-request-article group article (current-buffer)))
13551             'article)
13552            ;; Check the cache.
13553            ((and gnus-use-cache
13554                  (numberp article)
13555                  (gnus-cache-request-article article group))
13556             'article)
13557            ;; Get the article and put into the article buffer.
13558            ((or (stringp article) (numberp article))
13559             (let ((gnus-override-method
13560                    (and (stringp article) gnus-refer-article-method))
13561                   (buffer-read-only nil))
13562               (erase-buffer)
13563               (gnus-kill-all-overlays)
13564               (if (gnus-request-article article group (current-buffer))
13565                   (progn
13566                     (and gnus-keep-backlog
13567                          (numberp article)
13568                          (gnus-backlog-enter-article
13569                           group article (current-buffer)))
13570                     'article))))
13571            ;; It was a pseudo.
13572            (t article)))
13573
13574       ;; Take the article from the original article buffer
13575       ;; and place it in the buffer it's supposed to be in.
13576       (when (and (get-buffer gnus-article-buffer)
13577                  ;;(numberp article)
13578                  (equal (buffer-name (current-buffer))
13579                         (buffer-name (get-buffer gnus-article-buffer))))
13580         (save-excursion
13581           (if (get-buffer gnus-original-article-buffer)
13582               (set-buffer (get-buffer gnus-original-article-buffer))
13583             (set-buffer (get-buffer-create gnus-original-article-buffer))
13584             (buffer-disable-undo (current-buffer))
13585             (setq major-mode 'gnus-original-article-mode)
13586             (setq buffer-read-only t)
13587             (gnus-add-current-to-buffer-list))
13588           (let (buffer-read-only)
13589             (erase-buffer)
13590             (insert-buffer-substring gnus-article-buffer))
13591           (setq gnus-original-article (cons group article))))
13592     
13593       ;; Update sparse articles.
13594       (when (and do-update-line
13595                  (or (numberp article)
13596                      (stringp article)))
13597         (let ((buf (current-buffer)))
13598           (set-buffer gnus-summary-buffer)
13599           (gnus-summary-update-article do-update-line)
13600           (gnus-summary-goto-subject do-update-line nil t)
13601           (set-window-point (get-buffer-window (current-buffer) t)
13602                             (point))
13603           (set-buffer buf))))))
13604
13605 (defun gnus-read-header (id &optional header)
13606   "Read the headers of article ID and enter them into the Gnus system."
13607   (let ((group gnus-newsgroup-name)
13608         (gnus-override-method 
13609          (and (gnus-news-group-p gnus-newsgroup-name)
13610               gnus-refer-article-method))       
13611         where)
13612     ;; First we check to see whether the header in question is already
13613     ;; fetched.
13614     (if (stringp id)
13615         ;; This is a Message-ID.
13616         (setq header (or header (gnus-id-to-header id)))
13617       ;; This is an article number.
13618       (setq header (or header (gnus-summary-article-header id))))
13619     (if (and header
13620              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13621         ;; We have found the header.
13622         header
13623       ;; We have to really fetch the header to this article.
13624       (when (setq where
13625                   (if (gnus-check-backend-function 'request-head group)
13626                       (gnus-request-head id group)
13627                     (gnus-request-article id group)))
13628         (save-excursion
13629           (set-buffer nntp-server-buffer)
13630           (and (search-forward "\n\n" nil t)
13631                (delete-region (1- (point)) (point-max)))
13632           (goto-char (point-max))
13633           (insert ".\n")
13634           (goto-char (point-min))
13635           (insert "211 ")
13636           (princ (cond
13637                   ((numberp id) id)
13638                   ((cdr where) (cdr where))
13639                   (header (mail-header-number header))
13640                   (t gnus-reffed-article-number))
13641                  (current-buffer))
13642           (insert " Article retrieved.\n"))
13643         ;(when (and header
13644         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13645         ;  (setcar (gnus-id-to-thread id) nil))
13646         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13647             ()                          ; Malformed head.
13648           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13649             (if (and (stringp id)
13650                      (not (string= (gnus-group-real-name group)
13651                                    (car where))))
13652                 ;; If we fetched by Message-ID and the article came
13653                 ;; from a different group, we fudge some bogus article
13654                 ;; numbers for this article.
13655                 (mail-header-set-number header gnus-reffed-article-number))
13656             (decf gnus-reffed-article-number)
13657             (push header gnus-newsgroup-headers)
13658             (setq gnus-current-headers header)
13659             (push (mail-header-number header) gnus-newsgroup-limit))
13660           header)))))
13661
13662 (defun gnus-article-prepare (article &optional all-headers header)
13663   "Prepare ARTICLE in article mode buffer.
13664 ARTICLE should either be an article number or a Message-ID.
13665 If ARTICLE is an id, HEADER should be the article headers.
13666 If ALL-HEADERS is non-nil, no headers are hidden."
13667   (save-excursion
13668     ;; Make sure we start in a summary buffer.
13669     (unless (eq major-mode 'gnus-summary-mode)
13670       (set-buffer gnus-summary-buffer))
13671     (setq gnus-summary-buffer (current-buffer))
13672     ;; Make sure the connection to the server is alive.
13673     (unless (gnus-server-opened
13674              (gnus-find-method-for-group gnus-newsgroup-name))
13675       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13676       (gnus-request-group gnus-newsgroup-name t))
13677     (let* ((article (if header (mail-header-number header) article))
13678            (summary-buffer (current-buffer))
13679            (internal-hook gnus-article-internal-prepare-hook)
13680            (group gnus-newsgroup-name)
13681            result)
13682       (save-excursion
13683         (gnus-article-setup-buffer)
13684         (set-buffer gnus-article-buffer)
13685         ;; Deactivate active regions.
13686         (when (and (boundp 'transient-mark-mode)
13687                    transient-mark-mode)
13688           (setq mark-active nil))
13689         (if (not (setq result (let ((buffer-read-only nil))
13690                                 (gnus-request-article-this-buffer
13691                                  article group))))
13692             ;; There is no such article.
13693             (save-excursion
13694               (when (and (numberp article)
13695                          (not (memq article gnus-newsgroup-sparse)))
13696                 (setq gnus-article-current
13697                       (cons gnus-newsgroup-name article))
13698                 (set-buffer gnus-summary-buffer)
13699                 (setq gnus-current-article article)
13700                 (gnus-summary-mark-article article gnus-canceled-mark))
13701               (unless (memq article gnus-newsgroup-sparse)
13702                 (gnus-error
13703                  1 "No such article (may have expired or been canceled)")))
13704           (if (or (eq result 'pseudo) (eq result 'nneething))
13705               (progn
13706                 (save-excursion
13707                   (set-buffer summary-buffer)
13708                   (setq gnus-last-article gnus-current-article
13709                         gnus-newsgroup-history (cons gnus-current-article
13710                                                      gnus-newsgroup-history)
13711                         gnus-current-article 0
13712                         gnus-current-headers nil
13713                         gnus-article-current nil)
13714                   (if (eq result 'nneething)
13715                       (gnus-configure-windows 'summary)
13716                     (gnus-configure-windows 'article))
13717                   (gnus-set-global-variables))
13718                 (gnus-set-mode-line 'article))
13719             ;; The result from the `request' was an actual article -
13720             ;; or at least some text that is now displayed in the
13721             ;; article buffer.
13722             (if (and (numberp article)
13723                      (not (eq article gnus-current-article)))
13724                 ;; Seems like a new article has been selected.
13725                 ;; `gnus-current-article' must be an article number.
13726                 (save-excursion
13727                   (set-buffer summary-buffer)
13728                   (setq gnus-last-article gnus-current-article
13729                         gnus-newsgroup-history (cons gnus-current-article
13730                                                      gnus-newsgroup-history)
13731                         gnus-current-article article
13732                         gnus-current-headers
13733                         (gnus-summary-article-header gnus-current-article)
13734                         gnus-article-current
13735                         (cons gnus-newsgroup-name gnus-current-article))
13736                   (unless (vectorp gnus-current-headers)
13737                     (setq gnus-current-headers nil))
13738                   (gnus-summary-show-thread)
13739                   (run-hooks 'gnus-mark-article-hook)
13740                   (gnus-set-mode-line 'summary)
13741                   (and (gnus-visual-p 'article-highlight 'highlight)
13742                        (run-hooks 'gnus-visual-mark-article-hook))
13743                   ;; Set the global newsgroup variables here.
13744                   ;; Suggested by Jim Sisolak
13745                   ;; <sisolak@trans4.neep.wisc.edu>.
13746                   (gnus-set-global-variables)
13747                   (setq gnus-have-all-headers
13748                         (or all-headers gnus-show-all-headers))
13749                   (and gnus-use-cache
13750                        (vectorp (gnus-summary-article-header article))
13751                        (gnus-cache-possibly-enter-article
13752                         group article
13753                         (gnus-summary-article-header article)
13754                         (memq article gnus-newsgroup-marked)
13755                         (memq article gnus-newsgroup-dormant)
13756                         (memq article gnus-newsgroup-unreads)))))
13757             (when (or (numberp article)
13758                       (stringp article))
13759               ;; Hooks for getting information from the article.
13760               ;; This hook must be called before being narrowed.
13761               (let (buffer-read-only)
13762                 (run-hooks 'internal-hook)
13763                 (run-hooks 'gnus-article-prepare-hook)
13764                 ;; Decode MIME message.
13765                 (if gnus-show-mime
13766                     (if (or (not gnus-strict-mime)
13767                             (gnus-fetch-field "Mime-Version"))
13768                         (funcall gnus-show-mime-method)
13769                       (funcall gnus-decode-encoded-word-method)))
13770                 ;; Perform the article display hooks.
13771                 (run-hooks 'gnus-article-display-hook))
13772               ;; Do page break.
13773               (goto-char (point-min))
13774               (and gnus-break-pages (gnus-narrow-to-page)))
13775             (gnus-set-mode-line 'article)
13776             (gnus-configure-windows 'article)
13777             (goto-char (point-min))
13778             t))))))
13779
13780 (defun gnus-article-show-all-headers ()
13781   "Show all article headers in article mode buffer."
13782   (save-excursion
13783     (gnus-article-setup-buffer)
13784     (set-buffer gnus-article-buffer)
13785     (let ((buffer-read-only nil))
13786       (gnus-unhide-text (point-min) (point-max)))))
13787
13788 (defun gnus-article-hide-headers-if-wanted ()
13789   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13790 Provided for backwards compatibility."
13791   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13792       gnus-inhibit-hiding
13793       (gnus-article-hide-headers)))
13794
13795 (defsubst gnus-article-header-rank ()
13796   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13797   (let ((list gnus-sorted-header-list)
13798         (i 0))
13799     (while list
13800       (when (looking-at (car list))
13801         (setq list nil))
13802       (setq list (cdr list))
13803       (incf i))
13804     i))
13805
13806 (defun gnus-article-hide-headers (&optional arg delete)
13807   "Toggle whether to hide unwanted headers and possibly sort them as well.
13808 If given a negative prefix, always show; if given a positive prefix,
13809 always hide."
13810   (interactive "P")
13811   (if (gnus-article-check-hidden-text 'headers arg)
13812       ;; Show boring headers as well.
13813       (gnus-article-show-hidden-text 'boring-headers)
13814     ;; This function might be inhibited.
13815     (unless gnus-inhibit-hiding
13816       (save-excursion
13817         (set-buffer gnus-article-buffer)
13818         (save-restriction
13819           (let ((buffer-read-only nil)
13820                 (props (nconc (list 'gnus-type 'headers)
13821                               gnus-hidden-properties))
13822                 (max (1+ (length gnus-sorted-header-list)))
13823                 (ignored (when (not (stringp gnus-visible-headers))
13824                            (cond ((stringp gnus-ignored-headers)
13825                                   gnus-ignored-headers)
13826                                  ((listp gnus-ignored-headers)
13827                                   (mapconcat 'identity gnus-ignored-headers
13828                                              "\\|")))))
13829                 (visible
13830                  (cond ((stringp gnus-visible-headers)
13831                         gnus-visible-headers)
13832                        ((and gnus-visible-headers
13833                              (listp gnus-visible-headers))
13834                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13835                 want-list beg)
13836             ;; First we narrow to just the headers.
13837             (widen)
13838             (goto-char (point-min))
13839             ;; Hide any "From " lines at the beginning of (mail) articles.
13840             (while (looking-at "From ")
13841               (forward-line 1))
13842             (unless (bobp)
13843               (gnus-hide-text (point-min) (point) props))
13844             ;; Then treat the rest of the header lines.
13845             (narrow-to-region
13846              (point)
13847              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13848             ;; Then we use the two regular expressions
13849             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13850             ;; select which header lines is to remain visible in the
13851             ;; article buffer.
13852             (goto-char (point-min))
13853             (while (re-search-forward "^[^ \t]*:" nil t)
13854               (beginning-of-line)
13855               ;; We add the headers we want to keep to a list and delete
13856               ;; them from the buffer.
13857               (gnus-put-text-property 
13858                (point) (1+ (point)) 'message-rank
13859                (if (or (and visible (looking-at visible))
13860                        (and ignored
13861                             (not (looking-at ignored))))
13862                    (gnus-article-header-rank) 
13863                  (+ 2 max)))
13864               (forward-line 1))
13865             (message-sort-headers-1)
13866             (when (setq beg (text-property-any 
13867                              (point-min) (point-max) 'message-rank (+ 2 max)))
13868               ;; We make the unwanted headers invisible.
13869               (if delete
13870                   (delete-region beg (point-max))
13871                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13872                 (gnus-hide-text-type beg (point-max) 'headers))
13873               ;; Work around XEmacs lossage.
13874               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13875
13876 (defun gnus-article-hide-boring-headers (&optional arg)
13877   "Toggle hiding of headers that aren't very interesting.
13878 If given a negative prefix, always show; if given a positive prefix,
13879 always hide."
13880   (interactive "P")
13881   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13882     (save-excursion
13883       (set-buffer gnus-article-buffer)
13884       (save-restriction
13885         (let ((buffer-read-only nil)
13886               (list gnus-boring-article-headers)
13887               (inhibit-point-motion-hooks t)
13888               elem)
13889           (nnheader-narrow-to-headers)
13890           (while list
13891             (setq elem (pop list))
13892             (goto-char (point-min))
13893             (cond
13894              ;; Hide empty headers.
13895              ((eq elem 'empty)
13896               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13897                 (forward-line -1)
13898                 (gnus-hide-text-type
13899                  (progn (beginning-of-line) (point))
13900                  (progn 
13901                    (end-of-line)
13902                    (if (re-search-forward "^[^ \t]" nil t)
13903                        (match-beginning 0)
13904                      (point-max)))
13905                  'boring-headers)))
13906              ;; Hide boring Newsgroups header.
13907              ((eq elem 'newsgroups)
13908               (when (equal (message-fetch-field "newsgroups")
13909                            (gnus-group-real-name gnus-newsgroup-name))
13910                 (gnus-article-hide-header "newsgroups")))
13911              ((eq elem 'followup-to)
13912               (when (equal (message-fetch-field "followup-to")
13913                            (message-fetch-field "newsgroups"))
13914                 (gnus-article-hide-header "followup-to")))
13915              ((eq elem 'reply-to)
13916               (let ((from (message-fetch-field "from"))
13917                     (reply-to (message-fetch-field "reply-to")))
13918                 (when (and
13919                        from reply-to
13920                        (equal 
13921                         (nth 1 (funcall gnus-extract-address-components from))
13922                         (nth 1 (funcall gnus-extract-address-components
13923                                         reply-to))))
13924                   (gnus-article-hide-header "reply-to"))))
13925              ((eq elem 'date)
13926               (let ((date (message-fetch-field "date")))
13927                 (when (and date
13928                            (< (gnus-days-between date (current-time-string))
13929                               4))
13930                   (gnus-article-hide-header "date")))))))))))
13931
13932 (defun gnus-article-hide-header (header)
13933   (save-excursion
13934     (goto-char (point-min))
13935     (when (re-search-forward (concat "^" header ":") nil t)
13936       (gnus-hide-text-type
13937        (progn (beginning-of-line) (point))
13938        (progn 
13939          (end-of-line)
13940          (if (re-search-forward "^[^ \t]" nil t)
13941              (match-beginning 0)
13942            (point-max)))
13943        'boring-headers))))
13944
13945 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13946 (defun gnus-article-treat-overstrike ()
13947   "Translate overstrikes into bold text."
13948   (interactive)
13949   (save-excursion
13950     (set-buffer gnus-article-buffer)
13951     (let ((buffer-read-only nil))
13952       (while (search-forward "\b" nil t)
13953         (let ((next (following-char))
13954               (previous (char-after (- (point) 2))))
13955           (cond ((eq next previous)
13956                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13957                  (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
13958                 ((eq next ?_)
13959                  (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
13960                  (gnus-put-text-property
13961                   (- (point) 2) (1- (point)) 'face 'underline))
13962                 ((eq previous ?_)
13963                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13964                  (gnus-put-text-property
13965                   (point) (1+ (point))  'face 'underline))))))))
13966
13967 (defun gnus-article-word-wrap ()
13968   "Format too long lines."
13969   (interactive)
13970   (save-excursion
13971     (set-buffer gnus-article-buffer)
13972     (let ((buffer-read-only nil))
13973       (widen)
13974       (goto-char (point-min))
13975       (search-forward "\n\n" nil t)
13976       (end-of-line 1)
13977       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13978             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13979             (adaptive-fill-mode t))
13980         (while (not (eobp))
13981           (and (>= (current-column) (min fill-column (window-width)))
13982                (/= (preceding-char) ?:)
13983                (fill-paragraph nil))
13984           (end-of-line 2))))))
13985
13986 (defun gnus-article-remove-cr ()
13987   "Remove carriage returns from an article."
13988   (interactive)
13989   (save-excursion
13990     (set-buffer gnus-article-buffer)
13991     (let ((buffer-read-only nil))
13992       (goto-char (point-min))
13993       (while (search-forward "\r" nil t)
13994         (replace-match "" t t)))))
13995
13996 (defun gnus-article-remove-trailing-blank-lines ()
13997   "Remove all trailing blank lines from the article."
13998   (interactive)
13999   (save-excursion
14000     (set-buffer gnus-article-buffer)
14001     (let ((buffer-read-only nil))
14002       (goto-char (point-max))
14003       (delete-region
14004        (point)
14005        (progn
14006          (while (looking-at "^[ \t]*$")
14007            (forward-line -1))
14008          (forward-line 1)
14009          (point))))))
14010
14011 (defun gnus-article-display-x-face (&optional force)
14012   "Look for an X-Face header and display it if present."
14013   (interactive (list 'force))
14014   (save-excursion
14015     (set-buffer gnus-article-buffer)
14016     ;; Delete the old process, if any.
14017     (when (process-status "gnus-x-face")
14018       (delete-process "gnus-x-face"))
14019     (let ((inhibit-point-motion-hooks t)
14020           (case-fold-search nil)
14021           from)
14022       (save-restriction
14023         (nnheader-narrow-to-headers)
14024         (setq from (message-fetch-field "from"))
14025         (goto-char (point-min))
14026         (when (and gnus-article-x-face-command
14027                    (or force
14028                        ;; Check whether this face is censored.
14029                        (not gnus-article-x-face-too-ugly)
14030                        (and gnus-article-x-face-too-ugly from
14031                             (not (string-match gnus-article-x-face-too-ugly
14032                                                from))))
14033                    ;; Has to be present.
14034                    (re-search-forward "^X-Face: " nil t))
14035           ;; We now have the area of the buffer where the X-Face is stored.
14036           (let ((beg (point))
14037                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14038             ;; We display the face.
14039             (if (symbolp gnus-article-x-face-command)
14040                 ;; The command is a lisp function, so we call it.
14041                 (if (gnus-functionp gnus-article-x-face-command)
14042                     (funcall gnus-article-x-face-command beg end)
14043                   (error "%s is not a function" gnus-article-x-face-command))
14044               ;; The command is a string, so we interpret the command
14045               ;; as a, well, command, and fork it off.
14046               (let ((process-connection-type nil))
14047                 (process-kill-without-query
14048                  (start-process
14049                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14050                 (process-send-region "gnus-x-face" beg end)
14051                 (process-send-eof "gnus-x-face")))))))))
14052
14053 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14054 (defun gnus-decode-rfc1522 ()
14055   "Hack to remove QP encoding from headers."
14056   (let ((case-fold-search t)
14057         (inhibit-point-motion-hooks t)
14058         (buffer-read-only nil)
14059         string)
14060     (save-restriction
14061       (narrow-to-region
14062        (goto-char (point-min))
14063        (or (search-forward "\n\n" nil t) (point-max)))
14064
14065       (while (re-search-forward 
14066               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14067         (setq string (match-string 1))
14068         (narrow-to-region (match-beginning 0) (match-end 0))
14069         (delete-region (point-min) (point-max))
14070         (insert string)
14071         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14072         (subst-char-in-region (point-min) (point-max) ?_ ? )
14073         (widen)
14074         (goto-char (point-min))))))
14075
14076 (defun gnus-article-de-quoted-unreadable (&optional force)
14077   "Do a naive translation of a quoted-printable-encoded article.
14078 This is in no way, shape or form meant as a replacement for real MIME
14079 processing, but is simply a stop-gap measure until MIME support is
14080 written.
14081 If FORCE, decode the article whether it is marked as quoted-printable
14082 or not."
14083   (interactive (list 'force))
14084   (save-excursion
14085     (set-buffer gnus-article-buffer)
14086     (let ((case-fold-search t)
14087           (buffer-read-only nil)
14088           (type (gnus-fetch-field "content-transfer-encoding")))
14089       (gnus-decode-rfc1522)
14090       (when (or force
14091                 (and type (string-match "quoted-printable" (downcase type))))
14092         (goto-char (point-min))
14093         (search-forward "\n\n" nil 'move)
14094         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14095
14096 (defun gnus-mime-decode-quoted-printable (from to)
14097   "Decode Quoted-Printable in the region between FROM and TO."
14098   (interactive "r")
14099   (goto-char from)
14100   (while (search-forward "=" to t)
14101     (cond ((eq (following-char) ?\n)
14102            (delete-char -1)
14103            (delete-char 1))
14104           ((looking-at "[0-9A-F][0-9A-F]")
14105            (subst-char-in-region
14106             (1- (point)) (point) ?=
14107             (hexl-hex-string-to-integer
14108              (buffer-substring (point) (+ 2 (point)))))
14109            (delete-char 2))
14110           ((looking-at "=")
14111            (delete-char 1))
14112           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14113
14114 (defun gnus-article-hide-pgp (&optional arg)
14115   "Toggle hiding of any PGP headers and signatures in the current article.
14116 If given a negative prefix, always show; if given a positive prefix,
14117 always hide."
14118   (interactive "P")
14119   (unless (gnus-article-check-hidden-text 'pgp arg)
14120     (save-excursion
14121       (set-buffer gnus-article-buffer)
14122       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14123             buffer-read-only beg end)
14124         (widen)
14125         (goto-char (point-min))
14126         ;; Hide the "header".
14127         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14128              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14129         (setq beg (point))
14130         ;; Hide the actual signature.
14131         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14132              (setq end (1+ (match-beginning 0)))
14133              (gnus-hide-text
14134               end
14135               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14136                   (match-end 0)
14137                 ;; Perhaps we shouldn't hide to the end of the buffer
14138                 ;; if there is no end to the signature?
14139                 (point-max))
14140               props))
14141         ;; Hide "- " PGP quotation markers.
14142         (when (and beg end)
14143           (narrow-to-region beg end)
14144           (goto-char (point-min))
14145           (while (re-search-forward "^- " nil t)
14146             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14147           (widen))))))
14148
14149 (defun gnus-article-hide-signature (&optional arg)
14150   "Hide the signature in the current article.
14151 If given a negative prefix, always show; if given a positive prefix,
14152 always hide."
14153   (interactive "P")
14154   (unless (gnus-article-check-hidden-text 'signature arg)
14155     (save-excursion
14156       (set-buffer gnus-article-buffer)
14157       (save-restriction
14158         (let ((buffer-read-only nil))
14159           (when (gnus-narrow-to-signature)
14160             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14161
14162 (defun gnus-article-strip-leading-blank-lines ()
14163   "Remove all blank lines from the beginning of the article."
14164   (interactive)
14165   (save-excursion
14166     (set-buffer gnus-article-buffer)
14167     (let (buffer-read-only)
14168       (goto-char (point-min))
14169       (when (search-forward "\n\n" nil t)
14170         (while (looking-at "[ \t]$")
14171           (gnus-delete-line))))))
14172
14173 (defvar mime::preview/content-list)
14174 (defvar mime::preview-content-info/point-min)
14175 (defun gnus-narrow-to-signature ()
14176   "Narrow to the signature."
14177   (widen)
14178   (if (and (boundp 'mime::preview/content-list)
14179            mime::preview/content-list)
14180       (let ((pcinfo (car (last mime::preview/content-list))))
14181         (narrow-to-region
14182          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14183          (point-max))
14184         t))
14185   (goto-char (point-max))
14186   (when (re-search-backward gnus-signature-separator nil t)
14187     (forward-line 1)
14188     (when (or (null gnus-signature-limit)
14189               (and (numberp gnus-signature-limit)
14190                    (< (- (point-max) (point)) gnus-signature-limit))
14191               (and (gnus-functionp gnus-signature-limit)
14192                    (funcall gnus-signature-limit))
14193               (and (stringp gnus-signature-limit)
14194                    (not (re-search-forward gnus-signature-limit nil t))))
14195       (narrow-to-region (point) (point-max))
14196       t)))
14197
14198 (defun gnus-article-check-hidden-text (type arg)
14199   "Return nil if hiding is necessary."
14200   (save-excursion
14201     (set-buffer gnus-article-buffer)
14202     (let ((hide (gnus-article-hidden-text-p type)))
14203       (cond ((or (and (null arg) (eq hide 'hidden))
14204                  (and arg (< 0 (prefix-numeric-value arg))))
14205              (gnus-article-show-hidden-text type))
14206             ((eq hide 'shown)
14207              (gnus-article-show-hidden-text type t))
14208             (t nil)))))
14209
14210 (defun gnus-article-hidden-text-p (type)
14211   "Say whether the current buffer contains hidden text of type TYPE."
14212   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14213     (when pos
14214       (if (get-text-property pos 'invisible)
14215           'hidden
14216         'shown))))
14217
14218 (defun gnus-article-hide (&optional arg force)
14219   "Hide all the gruft in the current article.
14220 This means that PGP stuff, signatures, cited text and (some)
14221 headers will be hidden.
14222 If given a prefix, show the hidden text instead."
14223   (interactive (list current-prefix-arg 'force))
14224   (gnus-article-hide-headers arg)
14225   (gnus-article-hide-pgp arg)
14226   (gnus-article-hide-citation-maybe arg force)
14227   (gnus-article-hide-signature arg))
14228
14229 (defun gnus-article-show-hidden-text (type &optional hide)
14230   "Show all hidden text of type TYPE.
14231 If HIDE, hide the text instead."
14232   (save-excursion
14233     (set-buffer gnus-article-buffer)
14234     (let ((buffer-read-only nil)
14235           (inhibit-point-motion-hooks t)
14236           (beg (point-min)))
14237       (while (gnus-goto-char (text-property-any
14238                               beg (point-max) 'gnus-type type))
14239         (setq beg (point))
14240         (forward-char)
14241         (if hide
14242             (gnus-hide-text beg (point) gnus-hidden-properties)
14243           (gnus-unhide-text beg (point)))
14244         (setq beg (point)))
14245       t)))
14246
14247 (defvar gnus-article-time-units
14248   `((year . ,(* 365.25 24 60 60))
14249     (week . ,(* 7 24 60 60))
14250     (day . ,(* 24 60 60))
14251     (hour . ,(* 60 60))
14252     (minute . 60)
14253     (second . 1))
14254   "Mapping from time units to seconds.")
14255
14256 (defun gnus-article-date-ut (&optional type highlight)
14257   "Convert DATE date to universal time in the current article.
14258 If TYPE is `local', convert to local time; if it is `lapsed', output
14259 how much time has lapsed since DATE."
14260   (interactive (list 'ut t))
14261   (let* ((header (or gnus-current-headers
14262                      (gnus-summary-article-header) ""))
14263          (date (and (vectorp header) (mail-header-date header)))
14264          (date-regexp "^Date: \\|^X-Sent: ")
14265          (now (current-time))
14266          (inhibit-point-motion-hooks t)
14267          bface eface)
14268     (when (and date (not (string= date "")))
14269       (save-excursion
14270         (set-buffer gnus-article-buffer)
14271         (save-restriction
14272           (nnheader-narrow-to-headers)
14273           (let ((buffer-read-only nil))
14274             ;; Delete any old Date headers.
14275             (if (re-search-forward date-regexp nil t)
14276                 (progn
14277                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14278                         eface (get-text-property (1- (gnus-point-at-eol))
14279                                                  'face))
14280                   (message-remove-header date-regexp t)
14281                   (beginning-of-line))
14282               (goto-char (point-max)))
14283             (insert (gnus-make-date-line date type))
14284             ;; Do highlighting.
14285             (forward-line -1)
14286             (when (and (gnus-visual-p 'article-highlight 'highlight)
14287                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14288               (gnus-put-text-property (match-beginning 1) (match-end 1)
14289                                  'face bface)
14290               (gnus-put-text-property (match-beginning 2) (match-end 2)
14291                                  'face eface))))))))
14292
14293 (defun gnus-make-date-line (date type)
14294   "Return a DATE line of TYPE."
14295   (cond
14296    ;; Convert to the local timezone.  We have to slap a
14297    ;; `condition-case' round the calls to the timezone
14298    ;; functions since they aren't particularly resistant to
14299    ;; buggy dates.
14300    ((eq type 'local)
14301     (concat "Date: " (condition-case ()
14302                          (timezone-make-date-arpa-standard date)
14303                        (error date))
14304             "\n"))
14305    ;; Convert to Universal Time.
14306    ((eq type 'ut)
14307     (concat "Date: "
14308             (condition-case ()
14309                 (timezone-make-date-arpa-standard date nil "UT")
14310               (error date))
14311             "\n"))
14312    ;; Get the original date from the article.
14313    ((eq type 'original)
14314     (concat "Date: " date "\n"))
14315    ;; Do an X-Sent lapsed format.
14316    ((eq type 'lapsed)
14317     ;; If the date is seriously mangled, the timezone
14318     ;; functions are liable to bug out, so we condition-case
14319     ;; the entire thing.
14320     (let* ((now (current-time))
14321            (real-time
14322             (condition-case ()
14323                 (gnus-time-minus
14324                  (gnus-encode-date
14325                   (timezone-make-date-arpa-standard
14326                    (current-time-string now)
14327                    (current-time-zone now) "UT"))
14328                  (gnus-encode-date
14329                   (timezone-make-date-arpa-standard
14330                    date nil "UT")))
14331               (error '(0 0))))
14332            (real-sec (+ (* (float (car real-time)) 65536)
14333                         (cadr real-time)))
14334            (sec (abs real-sec))
14335            num prev)
14336       (cond
14337        ((equal real-time '(0 0))
14338         "X-Sent: Unknown\n")
14339        ((zerop sec)
14340         "X-Sent: Now\n")
14341        (t
14342         (concat
14343          "X-Sent: "
14344          ;; This is a bit convoluted, but basically we go
14345          ;; through the time units for years, weeks, etc,
14346          ;; and divide things to see whether that results
14347          ;; in positive answers.
14348          (mapconcat
14349           (lambda (unit)
14350             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14351                 ;; The (remaining) seconds are too few to
14352                 ;; be divided into this time unit.
14353                 ""
14354               ;; It's big enough, so we output it.
14355               (setq sec (- sec (* num (cdr unit))))
14356               (prog1
14357                   (concat (if prev ", " "") (int-to-string
14358                                              (floor num))
14359                           " " (symbol-name (car unit))
14360                           (if (> num 1) "s" ""))
14361                 (setq prev t))))
14362           gnus-article-time-units "")
14363          ;; If dates are odd, then it might appear like the
14364          ;; article was sent in the future.
14365          (if (> real-sec 0)
14366              " ago\n"
14367            " in the future\n"))))))
14368    (t
14369     (error "Unknown conversion type: %s" type))))
14370
14371 (defun gnus-article-date-local (&optional highlight)
14372   "Convert the current article date to the local timezone."
14373   (interactive (list t))
14374   (gnus-article-date-ut 'local highlight))
14375
14376 (defun gnus-article-date-original (&optional highlight)
14377   "Convert the current article date to what it was originally.
14378 This is only useful if you have used some other date conversion
14379 function and want to see what the date was before converting."
14380   (interactive (list t))
14381   (gnus-article-date-ut 'original highlight))
14382
14383 (defun gnus-article-date-lapsed (&optional highlight)
14384   "Convert the current article date to time lapsed since it was sent."
14385   (interactive (list t))
14386   (gnus-article-date-ut 'lapsed highlight))
14387
14388 (defun gnus-article-maybe-highlight ()
14389   "Do some article highlighting if `gnus-visual' is non-nil."
14390   (if (gnus-visual-p 'article-highlight 'highlight)
14391       (gnus-article-highlight-some)))
14392
14393 ;; Article savers.
14394
14395 (defun gnus-output-to-rmail (file-name)
14396   "Append the current article to an Rmail file named FILE-NAME."
14397   (require 'rmail)
14398   ;; Most of these codes are borrowed from rmailout.el.
14399   (setq file-name (expand-file-name file-name))
14400   (setq rmail-default-rmail-file file-name)
14401   (let ((artbuf (current-buffer))
14402         (tmpbuf (get-buffer-create " *Gnus-output*")))
14403     (save-excursion
14404       (or (get-file-buffer file-name)
14405           (file-exists-p file-name)
14406           (if (gnus-yes-or-no-p
14407                (concat "\"" file-name "\" does not exist, create it? "))
14408               (let ((file-buffer (create-file-buffer file-name)))
14409                 (save-excursion
14410                   (set-buffer file-buffer)
14411                   (rmail-insert-rmail-file-header)
14412                   (let ((require-final-newline nil))
14413                     (write-region (point-min) (point-max) file-name t 1)))
14414                 (kill-buffer file-buffer))
14415             (error "Output file does not exist")))
14416       (set-buffer tmpbuf)
14417       (buffer-disable-undo (current-buffer))
14418       (erase-buffer)
14419       (insert-buffer-substring artbuf)
14420       (gnus-convert-article-to-rmail)
14421       ;; Decide whether to append to a file or to an Emacs buffer.
14422       (let ((outbuf (get-file-buffer file-name)))
14423         (if (not outbuf)
14424             (append-to-file (point-min) (point-max) file-name)
14425           ;; File has been visited, in buffer OUTBUF.
14426           (set-buffer outbuf)
14427           (let ((buffer-read-only nil)
14428                 (msg (and (boundp 'rmail-current-message)
14429                           (symbol-value 'rmail-current-message))))
14430             ;; If MSG is non-nil, buffer is in RMAIL mode.
14431             (if msg
14432                 (progn (widen)
14433                        (narrow-to-region (point-max) (point-max))))
14434             (insert-buffer-substring tmpbuf)
14435             (if msg
14436                 (progn
14437                   (goto-char (point-min))
14438                   (widen)
14439                   (search-backward "\^_")
14440                   (narrow-to-region (point) (point-max))
14441                   (goto-char (1+ (point-min)))
14442                   (rmail-count-new-messages t)
14443                   (rmail-show-message msg)))))))
14444     (kill-buffer tmpbuf)))
14445
14446 (defun gnus-output-to-file (file-name)
14447   "Append the current article to a file named FILE-NAME."
14448   (setq file-name (expand-file-name file-name))
14449   (let ((artbuf (current-buffer))
14450         (tmpbuf (get-buffer-create " *Gnus-output*")))
14451     (save-excursion
14452       (set-buffer tmpbuf)
14453       (buffer-disable-undo (current-buffer))
14454       (erase-buffer)
14455       (insert-buffer-substring artbuf)
14456       ;; Append newline at end of the buffer as separator, and then
14457       ;; save it to file.
14458       (goto-char (point-max))
14459       (insert "\n")
14460       (append-to-file (point-min) (point-max) file-name))
14461     (kill-buffer tmpbuf)))
14462
14463 (defun gnus-convert-article-to-rmail ()
14464   "Convert article in current buffer to Rmail message format."
14465   (let ((buffer-read-only nil))
14466     ;; Convert article directly into Babyl format.
14467     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14468     (goto-char (point-min))
14469     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14470     (while (search-forward "\n\^_" nil t) ;single char
14471       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14472     (goto-char (point-max))
14473     (insert "\^_")))
14474
14475 (defun gnus-narrow-to-page (&optional arg)
14476   "Narrow the article buffer to a page.
14477 If given a numerical ARG, move forward ARG pages."
14478   (interactive "P")
14479   (setq arg (if arg (prefix-numeric-value arg) 0))
14480   (save-excursion
14481     (set-buffer gnus-article-buffer)
14482     (goto-char (point-min))
14483     (widen)
14484     (when (gnus-visual-p 'page-marker)
14485       (let ((buffer-read-only nil))
14486         (gnus-remove-text-with-property 'gnus-prev)
14487         (gnus-remove-text-with-property 'gnus-next)))
14488     (when
14489         (cond ((< arg 0)
14490                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14491               ((> arg 0)
14492                (re-search-forward page-delimiter nil 'move arg)))
14493       (goto-char (match-end 0)))
14494     (narrow-to-region
14495      (point)
14496      (if (re-search-forward page-delimiter nil 'move)
14497          (match-beginning 0)
14498        (point)))
14499     (when (and (gnus-visual-p 'page-marker)
14500                (not (= (point-min) 1)))
14501       (save-excursion
14502         (goto-char (point-min))
14503         (gnus-insert-prev-page-button)))
14504     (when (and (gnus-visual-p 'page-marker)
14505                (not (= (1- (point-max)) (buffer-size))))
14506       (save-excursion
14507         (goto-char (point-max))
14508         (gnus-insert-next-page-button)))))
14509
14510 ;; Article mode commands
14511
14512 (defun gnus-article-goto-next-page ()
14513   "Show the next page of the article."
14514   (interactive)
14515   (when (gnus-article-next-page)
14516     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14517
14518 (defun gnus-article-goto-prev-page ()
14519   "Show the next page of the article."
14520   (interactive)
14521   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14522     (gnus-article-prev-page nil)))
14523
14524 (defun gnus-article-next-page (&optional lines)
14525   "Show the next page of the current article.
14526 If end of article, return non-nil.  Otherwise return nil.
14527 Argument LINES specifies lines to be scrolled up."
14528   (interactive "p")
14529   (move-to-window-line -1)
14530   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14531   (if (save-excursion
14532         (end-of-line)
14533         (and (pos-visible-in-window-p)  ;Not continuation line.
14534              (eobp)))
14535       ;; Nothing in this page.
14536       (if (or (not gnus-break-pages)
14537               (save-excursion
14538                 (save-restriction
14539                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14540           t                             ;Nothing more.
14541         (gnus-narrow-to-page 1)         ;Go to next page.
14542         nil)
14543     ;; More in this page.
14544     (condition-case ()
14545         (scroll-up lines)
14546       (end-of-buffer
14547        ;; Long lines may cause an end-of-buffer error.
14548        (goto-char (point-max))))
14549     (move-to-window-line 0)
14550     nil))
14551
14552 (defun gnus-article-prev-page (&optional lines)
14553   "Show previous page of current article.
14554 Argument LINES specifies lines to be scrolled down."
14555   (interactive "p")
14556   (move-to-window-line 0)
14557   (if (and gnus-break-pages
14558            (bobp)
14559            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14560       (progn
14561         (gnus-narrow-to-page -1)        ;Go to previous page.
14562         (goto-char (point-max))
14563         (recenter -1))
14564     (prog1
14565         (condition-case ()
14566             (scroll-down lines)
14567           (error nil))
14568       (move-to-window-line 0))))
14569
14570 (defun gnus-article-refer-article ()
14571   "Read article specified by message-id around point."
14572   (interactive)
14573   (let ((point (point)))
14574     (search-forward ">" nil t)          ;Move point to end of "<....>".
14575     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14576         (let ((message-id (match-string 1)))
14577           (goto-char point)
14578           (set-buffer gnus-summary-buffer)
14579           (gnus-summary-refer-article message-id))
14580       (goto-char (point))
14581       (error "No references around point"))))
14582
14583 (defun gnus-article-show-summary ()
14584   "Reconfigure windows to show summary buffer."
14585   (interactive)
14586   (gnus-configure-windows 'article)
14587   (gnus-summary-goto-subject gnus-current-article))
14588
14589 (defun gnus-article-describe-briefly ()
14590   "Describe article mode commands briefly."
14591   (interactive)
14592   (gnus-message 6
14593                 (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")))
14594
14595 (defun gnus-article-summary-command ()
14596   "Execute the last keystroke in the summary buffer."
14597   (interactive)
14598   (let ((obuf (current-buffer))
14599         (owin (current-window-configuration))
14600         func)
14601     (switch-to-buffer gnus-summary-buffer 'norecord)
14602     (setq func (lookup-key (current-local-map) (this-command-keys)))
14603     (call-interactively func)
14604     (set-buffer obuf)
14605     (set-window-configuration owin)
14606     (set-window-point (get-buffer-window (current-buffer)) (point))))
14607
14608 (defun gnus-article-summary-command-nosave ()
14609   "Execute the last keystroke in the summary buffer."
14610   (interactive)
14611   (let (func)
14612     (pop-to-buffer gnus-summary-buffer 'norecord)
14613     (setq func (lookup-key (current-local-map) (this-command-keys)))
14614     (call-interactively func)))
14615
14616 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14617   "Read a summary buffer key sequence and execute it from the article buffer."
14618   (interactive "P")
14619   (let ((nosaves
14620          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14621            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14622            "=" "^" "\M-^"))
14623         keys)
14624     (save-excursion
14625       (set-buffer gnus-summary-buffer)
14626       (push (or key last-command-event) unread-command-events)
14627       (setq keys (read-key-sequence nil)))
14628     (message "")
14629
14630     (if (member keys nosaves)
14631         (let (func)
14632           (pop-to-buffer gnus-summary-buffer 'norecord)
14633           (if (setq func (lookup-key (current-local-map) keys))
14634               (call-interactively func)
14635             (ding)))
14636       (let ((obuf (current-buffer))
14637             (owin (current-window-configuration))
14638             (opoint (point))
14639             func in-buffer)
14640         (if not-restore-window
14641             (pop-to-buffer gnus-summary-buffer 'norecord)
14642           (switch-to-buffer gnus-summary-buffer 'norecord))
14643         (setq in-buffer (current-buffer))
14644         (if (setq func (lookup-key (current-local-map) keys))
14645             (call-interactively func)
14646           (ding))
14647         (when (eq in-buffer (current-buffer))
14648           (set-buffer obuf)
14649           (unless not-restore-window
14650             (set-window-configuration owin))
14651           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14652
14653 \f
14654 ;;;
14655 ;;; Kill file handling.
14656 ;;;
14657
14658 ;;;###autoload
14659 (defalias 'gnus-batch-kill 'gnus-batch-score)
14660 ;;;###autoload
14661 (defun gnus-batch-score ()
14662   "Run batched scoring.
14663 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14664 Newsgroups is a list of strings in Bnews format.  If you want to score
14665 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14666 score the alt hierarchy, you'd say \"!alt.all\"."
14667   (interactive)
14668   (let* ((yes-and-no
14669           (gnus-newsrc-parse-options
14670            (apply (function concat)
14671                   (mapcar (lambda (g) (concat g " "))
14672                           command-line-args-left))))
14673          (gnus-expert-user t)
14674          (nnmail-spool-file nil)
14675          (gnus-use-dribble-file nil)
14676          (yes (car yes-and-no))
14677          (no (cdr yes-and-no))
14678          group newsrc entry
14679          ;; Disable verbose message.
14680          gnus-novice-user gnus-large-newsgroup)
14681     ;; Eat all arguments.
14682     (setq command-line-args-left nil)
14683     ;; Start Gnus.
14684     (gnus)
14685     ;; Apply kills to specified newsgroups in command line arguments.
14686     (setq newsrc (cdr gnus-newsrc-alist))
14687     (while newsrc
14688       (setq group (caar newsrc))
14689       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14690       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14691                (and (car entry)
14692                     (or (eq (car entry) t)
14693                         (not (zerop (car entry)))))
14694                (if yes (string-match yes group) t)
14695                (or (null no) (not (string-match no group))))
14696           (progn
14697             (gnus-summary-read-group group nil t nil t)
14698             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14699                  (gnus-summary-exit))))
14700       (setq newsrc (cdr newsrc)))
14701     ;; Exit Emacs.
14702     (switch-to-buffer gnus-group-buffer)
14703     (gnus-group-save-newsrc)))
14704
14705 (defun gnus-apply-kill-file ()
14706   "Apply a kill file to the current newsgroup.
14707 Returns the number of articles marked as read."
14708   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14709           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14710       (gnus-apply-kill-file-internal)
14711     0))
14712
14713 (defun gnus-kill-save-kill-buffer ()
14714   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14715     (when (get-file-buffer file)
14716       (save-excursion
14717         (set-buffer (get-file-buffer file))
14718         (and (buffer-modified-p) (save-buffer))
14719         (kill-buffer (current-buffer))))))
14720
14721 (defvar gnus-kill-file-name "KILL"
14722   "Suffix of the kill files.")
14723
14724 (defun gnus-newsgroup-kill-file (newsgroup)
14725   "Return the name of a kill file name for NEWSGROUP.
14726 If NEWSGROUP is nil, return the global kill file name instead."
14727   (cond 
14728    ;; The global KILL file is placed at top of the directory.
14729    ((or (null newsgroup)
14730         (string-equal newsgroup ""))
14731     (expand-file-name gnus-kill-file-name
14732                       gnus-kill-files-directory))
14733    ;; Append ".KILL" to newsgroup name.
14734    ((gnus-use-long-file-name 'not-kill)
14735     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14736                               "." gnus-kill-file-name)
14737                       gnus-kill-files-directory))
14738    ;; Place "KILL" under the hierarchical directory.
14739    (t
14740     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14741                               "/" gnus-kill-file-name)
14742                       gnus-kill-files-directory))))
14743
14744 \f
14745 ;;;
14746 ;;; Dribble file
14747 ;;;
14748
14749 (defvar gnus-dribble-ignore nil)
14750 (defvar gnus-dribble-eval-file nil)
14751
14752 (defun gnus-dribble-file-name ()
14753   "Return the dribble file for the current .newsrc."
14754   (concat
14755    (if gnus-dribble-directory
14756        (concat (file-name-as-directory gnus-dribble-directory)
14757                (file-name-nondirectory gnus-current-startup-file))
14758      gnus-current-startup-file)
14759    "-dribble"))
14760
14761 (defun gnus-dribble-enter (string)
14762   "Enter STRING into the dribble buffer."
14763   (if (and (not gnus-dribble-ignore)
14764            gnus-dribble-buffer
14765            (buffer-name gnus-dribble-buffer))
14766       (let ((obuf (current-buffer)))
14767         (set-buffer gnus-dribble-buffer)
14768         (insert string "\n")
14769         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14770         (set-buffer obuf))))
14771
14772 (defun gnus-dribble-read-file ()
14773   "Read the dribble file from disk."
14774   (let ((dribble-file (gnus-dribble-file-name)))
14775     (save-excursion
14776       (set-buffer (setq gnus-dribble-buffer
14777                         (get-buffer-create
14778                          (file-name-nondirectory dribble-file))))
14779       (gnus-add-current-to-buffer-list)
14780       (erase-buffer)
14781       (setq buffer-file-name dribble-file)
14782       (auto-save-mode t)
14783       (buffer-disable-undo (current-buffer))
14784       (bury-buffer (current-buffer))
14785       (set-buffer-modified-p nil)
14786       (let ((auto (make-auto-save-file-name))
14787             (gnus-dribble-ignore t)
14788             modes)
14789         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14790           ;; Load whichever file is newest -- the auto save file
14791           ;; or the "real" file.
14792           (if (file-newer-than-file-p auto dribble-file)
14793               (insert-file-contents auto)
14794             (insert-file-contents dribble-file))
14795           (unless (zerop (buffer-size))
14796             (set-buffer-modified-p t))
14797           ;; Set the file modes to reflect the .newsrc file modes.
14798           (save-buffer)
14799           (when (and (file-exists-p gnus-current-startup-file)
14800                      (setq modes (file-modes gnus-current-startup-file)))
14801             (set-file-modes dribble-file modes))
14802           ;; Possibly eval the file later.
14803           (when (gnus-y-or-n-p
14804                  "Auto-save file exists.  Do you want to read it? ")
14805             (setq gnus-dribble-eval-file t)))))))
14806
14807 (defun gnus-dribble-eval-file ()
14808   (when gnus-dribble-eval-file
14809     (setq gnus-dribble-eval-file nil)
14810     (save-excursion
14811       (let ((gnus-dribble-ignore t))
14812         (set-buffer gnus-dribble-buffer)
14813         (eval-buffer (current-buffer))))))
14814
14815 (defun gnus-dribble-delete-file ()
14816   (when (file-exists-p (gnus-dribble-file-name))
14817     (delete-file (gnus-dribble-file-name)))
14818   (when gnus-dribble-buffer
14819     (save-excursion
14820       (set-buffer gnus-dribble-buffer)
14821       (let ((auto (make-auto-save-file-name)))
14822         (if (file-exists-p auto)
14823             (delete-file auto))
14824         (erase-buffer)
14825         (set-buffer-modified-p nil)))))
14826
14827 (defun gnus-dribble-save ()
14828   (when (and gnus-dribble-buffer
14829              (buffer-name gnus-dribble-buffer))
14830     (save-excursion
14831       (set-buffer gnus-dribble-buffer)
14832       (save-buffer))))
14833
14834 (defun gnus-dribble-clear ()
14835   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14836     (save-excursion
14837       (set-buffer gnus-dribble-buffer)
14838       (erase-buffer)
14839       (set-buffer-modified-p nil)
14840       (setq buffer-saved-size (buffer-size)))))
14841
14842 \f
14843 ;;;
14844 ;;; Server Communication
14845 ;;;
14846
14847 (defun gnus-start-news-server (&optional confirm)
14848   "Open a method for getting news.
14849 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14850   (let (how)
14851     (if gnus-current-select-method
14852         ;; Stream is already opened.
14853         nil
14854       ;; Open NNTP server.
14855       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14856       (if confirm
14857           (progn
14858             ;; Read server name with completion.
14859             (setq gnus-nntp-server
14860                   (completing-read "NNTP server: "
14861                                    (mapcar (lambda (server) (list server))
14862                                            (cons (list gnus-nntp-server)
14863                                                  gnus-secondary-servers))
14864                                    nil nil gnus-nntp-server))))
14865
14866       (if (and gnus-nntp-server
14867                (stringp gnus-nntp-server)
14868                (not (string= gnus-nntp-server "")))
14869           (setq gnus-select-method
14870                 (cond ((or (string= gnus-nntp-server "")
14871                            (string= gnus-nntp-server "::"))
14872                        (list 'nnspool (system-name)))
14873                       ((string-match "^:" gnus-nntp-server)
14874                        (list 'nnmh gnus-nntp-server
14875                              (list 'nnmh-directory
14876                                    (file-name-as-directory
14877                                     (expand-file-name
14878                                      (concat "~/" (substring
14879                                                    gnus-nntp-server 1)))))
14880                              (list 'nnmh-get-new-mail nil)))
14881                       (t
14882                        (list 'nntp gnus-nntp-server)))))
14883
14884       (setq how (car gnus-select-method))
14885       (cond ((eq how 'nnspool)
14886              (require 'nnspool)
14887              (gnus-message 5 "Looking up local news spool..."))
14888             ((eq how 'nnmh)
14889              (require 'nnmh)
14890              (gnus-message 5 "Looking up mh spool..."))
14891             (t
14892              (require 'nntp)))
14893       (setq gnus-current-select-method gnus-select-method)
14894       (run-hooks 'gnus-open-server-hook)
14895       (or
14896        ;; gnus-open-server-hook might have opened it
14897        (gnus-server-opened gnus-select-method)
14898        (gnus-open-server gnus-select-method)
14899        (gnus-y-or-n-p
14900         (format
14901          "%s (%s) open error: '%s'.     Continue? "
14902          (car gnus-select-method) (cadr gnus-select-method)
14903          (gnus-status-message gnus-select-method)))
14904        (gnus-error 1 "Couldn't open server on %s"
14905                    (nth 1 gnus-select-method))))))
14906
14907 (defun gnus-check-group (group)
14908   "Try to make sure that the server where GROUP exists is alive."
14909   (let ((method (gnus-find-method-for-group group)))
14910     (or (gnus-server-opened method)
14911         (gnus-open-server method))))
14912
14913 (defun gnus-check-server (&optional method silent)
14914   "Check whether the connection to METHOD is down.
14915 If METHOD is nil, use `gnus-select-method'.
14916 If it is down, start it up (again)."
14917   (let ((method (or method gnus-select-method)))
14918     ;; Transform virtual server names into select methods.
14919     (when (stringp method)
14920       (setq method (gnus-server-to-method method)))
14921     (if (gnus-server-opened method)
14922         ;; The stream is already opened.
14923         t
14924       ;; Open the server.
14925       (unless silent
14926         (gnus-message 5 "Opening %s server%s..." (car method)
14927                       (if (equal (nth 1 method) "") ""
14928                         (format " on %s" (nth 1 method)))))
14929       (run-hooks 'gnus-open-server-hook)
14930       (prog1
14931           (gnus-open-server method)
14932         (unless silent
14933           (message ""))))))
14934
14935 (defun gnus-get-function (method function)
14936   "Return a function symbol based on METHOD and FUNCTION."
14937   ;; Translate server names into methods.
14938   (unless method
14939     (error "Attempted use of a nil select method"))
14940   (when (stringp method)
14941     (setq method (gnus-server-to-method method)))
14942   (let ((func (intern (format "%s-%s" (car method) function))))
14943     ;; If the functions isn't bound, we require the backend in
14944     ;; question.
14945     (unless (fboundp func)
14946       (require (car method))
14947       (unless (fboundp func)
14948         ;; This backend doesn't implement this function.
14949         (error "No such function: %s" func)))
14950     func))
14951
14952 \f
14953 ;;;
14954 ;;; Interface functions to the backends.
14955 ;;;
14956
14957 (defun gnus-open-server (method)
14958   "Open a connection to METHOD."
14959   (when (stringp method)
14960     (setq method (gnus-server-to-method method)))
14961   (let ((elem (assoc method gnus-opened-servers)))
14962     ;; If this method was previously denied, we just return nil.
14963     (if (eq (nth 1 elem) 'denied)
14964         (progn
14965           (gnus-message 1 "Denied server")
14966           nil)
14967       ;; Open the server.
14968       (let ((result
14969              (funcall (gnus-get-function method 'open-server)
14970                       (nth 1 method) (nthcdr 2 method))))
14971         ;; If this hasn't been opened before, we add it to the list.
14972         (unless elem
14973           (setq elem (list method nil)
14974                 gnus-opened-servers (cons elem gnus-opened-servers)))
14975         ;; Set the status of this server.
14976         (setcar (cdr elem) (if result 'ok 'denied))
14977         ;; Return the result from the "open" call.
14978         result))))
14979
14980 (defun gnus-close-server (method)
14981   "Close the connection to METHOD."
14982   (when (stringp method)
14983     (setq method (gnus-server-to-method method)))
14984   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14985
14986 (defun gnus-request-list (method)
14987   "Request the active file from METHOD."
14988   (when (stringp method)
14989     (setq method (gnus-server-to-method method)))
14990   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14991
14992 (defun gnus-request-list-newsgroups (method)
14993   "Request the newsgroups file from METHOD."
14994   (when (stringp method)
14995     (setq method (gnus-server-to-method method)))
14996   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14997
14998 (defun gnus-request-newgroups (date method)
14999   "Request all new groups since DATE from METHOD."
15000   (when (stringp method)
15001     (setq method (gnus-server-to-method method)))
15002   (funcall (gnus-get-function method 'request-newgroups)
15003            date (nth 1 method)))
15004
15005 (defun gnus-server-opened (method)
15006   "Check whether a connection to METHOD has been opened."
15007   (when (stringp method)
15008     (setq method (gnus-server-to-method method)))
15009   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15010
15011 (defun gnus-status-message (method)
15012   "Return the status message from METHOD.
15013 If METHOD is a string, it is interpreted as a group name.   The method
15014 this group uses will be queried."
15015   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15016                   method)))
15017     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15018
15019 (defun gnus-request-group (group &optional dont-check method)
15020   "Request GROUP.  If DONT-CHECK, no information is required."
15021   (let ((method (or method (gnus-find-method-for-group group))))
15022     (when (stringp method)
15023       (setq method (gnus-server-to-method method)))
15024     (funcall (gnus-get-function method 'request-group)
15025              (gnus-group-real-name group) (nth 1 method) dont-check)))
15026
15027 (defun gnus-request-asynchronous (group &optional articles)
15028   "Request that GROUP behave asynchronously.
15029 ARTICLES is the `data' of the group."
15030   (let ((method (gnus-find-method-for-group group)))
15031     (funcall (gnus-get-function method 'request-asynchronous)
15032              (gnus-group-real-name group) (nth 1 method) articles)))
15033
15034 (defun gnus-list-active-group (group)
15035   "Request active information on GROUP."
15036   (let ((method (gnus-find-method-for-group group))
15037         (func 'list-active-group))
15038     (when (gnus-check-backend-function func group)
15039       (funcall (gnus-get-function method func)
15040                (gnus-group-real-name group) (nth 1 method)))))
15041
15042 (defun gnus-request-group-description (group)
15043   "Request a description of GROUP."
15044   (let ((method (gnus-find-method-for-group group))
15045         (func 'request-group-description))
15046     (when (gnus-check-backend-function func group)
15047       (funcall (gnus-get-function method func)
15048                (gnus-group-real-name group) (nth 1 method)))))
15049
15050 (defun gnus-close-group (group)
15051   "Request the GROUP be closed."
15052   (let ((method (gnus-find-method-for-group group)))
15053     (funcall (gnus-get-function method 'close-group)
15054              (gnus-group-real-name group) (nth 1 method))))
15055
15056 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15057   "Request headers for ARTICLES in GROUP.
15058 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15059   (let ((method (gnus-find-method-for-group group)))
15060     (if (and gnus-use-cache (numberp (car articles)))
15061         (gnus-cache-retrieve-headers articles group fetch-old)
15062       (funcall (gnus-get-function method 'retrieve-headers)
15063                articles (gnus-group-real-name group) (nth 1 method)
15064                fetch-old))))
15065
15066 (defun gnus-retrieve-groups (groups method)
15067   "Request active information on GROUPS from METHOD."
15068   (when (stringp method)
15069     (setq method (gnus-server-to-method method)))
15070   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15071
15072 (defun gnus-request-type (group &optional article)
15073   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15074   (let ((method (gnus-find-method-for-group group)))
15075     (if (not (gnus-check-backend-function 'request-type (car method)))
15076         'unknown
15077       (funcall (gnus-get-function method 'request-type)
15078                (gnus-group-real-name group) article))))
15079
15080 (defun gnus-request-update-mark (group article mark)
15081   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15082   (let ((method (gnus-find-method-for-group group)))
15083     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15084         mark
15085       (funcall (gnus-get-function method 'request-update-mark)
15086                (gnus-group-real-name group) article mark))))
15087
15088 (defun gnus-request-article (article group &optional buffer)
15089   "Request the ARTICLE in GROUP.
15090 ARTICLE can either be an article number or an article Message-ID.
15091 If BUFFER, insert the article in that group."
15092   (let ((method (gnus-find-method-for-group group)))
15093     (funcall (gnus-get-function method 'request-article)
15094              article (gnus-group-real-name group) (nth 1 method) buffer)))
15095
15096 (defun gnus-request-head (article group)
15097   "Request the head of ARTICLE in GROUP."
15098   (let ((method (gnus-find-method-for-group group)))
15099     (funcall (gnus-get-function method 'request-head)
15100              article (gnus-group-real-name group) (nth 1 method))))
15101
15102 (defun gnus-request-body (article group)
15103   "Request the body of ARTICLE in GROUP."
15104   (let ((method (gnus-find-method-for-group group)))
15105     (funcall (gnus-get-function method 'request-body)
15106              article (gnus-group-real-name group) (nth 1 method))))
15107
15108 (defun gnus-request-post (method)
15109   "Post the current buffer using METHOD."
15110   (when (stringp method)
15111     (setq method (gnus-server-to-method method)))
15112   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15113
15114 (defun gnus-request-scan (group method)
15115   "Request a SCAN being performed in GROUP from METHOD.
15116 If GROUP is nil, all groups on METHOD are scanned."
15117   (let ((method (if group (gnus-find-method-for-group group) method)))
15118     (funcall (gnus-get-function method 'request-scan)
15119              (and group (gnus-group-real-name group)) (nth 1 method))))
15120
15121 (defsubst gnus-request-update-info (info method)
15122   "Request that METHOD update INFO."
15123   (when (stringp method)
15124     (setq method (gnus-server-to-method method)))
15125   (when (gnus-check-backend-function 'request-update-info (car method))
15126     (funcall (gnus-get-function method 'request-update-info)
15127              (gnus-group-real-name (gnus-info-group info))
15128              info (nth 1 method))))
15129
15130 (defun gnus-request-expire-articles (articles group &optional force)
15131   (let ((method (gnus-find-method-for-group group)))
15132     (funcall (gnus-get-function method 'request-expire-articles)
15133              articles (gnus-group-real-name group) (nth 1 method)
15134              force)))
15135
15136 (defun gnus-request-move-article
15137   (article group server accept-function &optional last)
15138   (let ((method (gnus-find-method-for-group group)))
15139     (funcall (gnus-get-function method 'request-move-article)
15140              article (gnus-group-real-name group)
15141              (nth 1 method) accept-function last)))
15142
15143 (defun gnus-request-accept-article (group method &optional last)
15144   ;; Make sure there's a newline at the end of the article.
15145   (when (stringp method)
15146     (setq method (gnus-server-to-method method)))
15147   (when (and (not method)
15148              (stringp group))
15149     (setq method (gnus-find-method-for-group group)))
15150   (goto-char (point-max))
15151   (unless (bolp)
15152     (insert "\n"))
15153   (let ((func (car (or method (gnus-find-method-for-group group)))))
15154     (funcall (intern (format "%s-request-accept-article" func))
15155              (if (stringp group) (gnus-group-real-name group) group)
15156              (cadr method)
15157              last)))
15158
15159 (defun gnus-request-replace-article (article group buffer)
15160   (let ((func (car (gnus-find-method-for-group group))))
15161     (funcall (intern (format "%s-request-replace-article" func))
15162              article (gnus-group-real-name group) buffer)))
15163
15164 (defun gnus-request-associate-buffer (group)
15165   (let ((method (gnus-find-method-for-group group)))
15166     (funcall (gnus-get-function method 'request-associate-buffer)
15167              (gnus-group-real-name group))))
15168
15169 (defun gnus-request-restore-buffer (article group)
15170   "Request a new buffer restored to the state of ARTICLE."
15171   (let ((method (gnus-find-method-for-group group)))
15172     (funcall (gnus-get-function method 'request-restore-buffer)
15173              article (gnus-group-real-name group) (nth 1 method))))
15174
15175 (defun gnus-request-create-group (group &optional method)
15176   (when (stringp method)
15177     (setq method (gnus-server-to-method method)))
15178   (let ((method (or method (gnus-find-method-for-group group))))
15179     (funcall (gnus-get-function method 'request-create-group)
15180              (gnus-group-real-name group) (nth 1 method))))
15181
15182 (defun gnus-request-delete-group (group &optional force)
15183   (let ((method (gnus-find-method-for-group group)))
15184     (funcall (gnus-get-function method 'request-delete-group)
15185              (gnus-group-real-name group) force (nth 1 method))))
15186
15187 (defun gnus-request-rename-group (group new-name)
15188   (let ((method (gnus-find-method-for-group group)))
15189     (funcall (gnus-get-function method 'request-rename-group)
15190              (gnus-group-real-name group)
15191              (gnus-group-real-name new-name) (nth 1 method))))
15192
15193 (defun gnus-member-of-valid (symbol group)
15194   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15195   (memq symbol (assoc
15196                 (symbol-name (car (gnus-find-method-for-group group)))
15197                 gnus-valid-select-methods)))
15198
15199 (defun gnus-method-option-p (method option)
15200   "Return non-nil if select METHOD has OPTION as a parameter."
15201   (when (stringp method)
15202     (setq method (gnus-server-to-method method)))
15203   (memq option (assoc (format "%s" (car method))
15204                       gnus-valid-select-methods)))
15205
15206 (defun gnus-server-extend-method (group method)
15207   ;; This function "extends" a virtual server.  If the server is
15208   ;; "hello", and the select method is ("hello" (my-var "something"))
15209   ;; in the group "alt.alt", this will result in a new virtual server
15210   ;; called "hello+alt.alt".
15211   (let ((entry
15212          (gnus-copy-sequence
15213           (if (equal (car method) "native") gnus-select-method
15214             (cdr (assoc (car method) gnus-server-alist))))))
15215     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15216     (nconc entry (cdr method))))
15217
15218 (defun gnus-find-method-for-group (group &optional info)
15219   "Find the select method that GROUP uses."
15220   (or gnus-override-method
15221       (and (not group)
15222            gnus-select-method)
15223       (let ((info (or info (gnus-get-info group)))
15224             method)
15225         (if (or (not info)
15226                 (not (setq method (gnus-info-method info)))
15227                 (equal method "native"))
15228             gnus-select-method
15229           (setq method
15230                 (cond ((stringp method)
15231                        (gnus-server-to-method method))
15232                       ((stringp (car method))
15233                        (gnus-server-extend-method group method))
15234                       (t
15235                        method)))
15236           (cond ((equal (cadr method) "")
15237                  method)
15238                 ((null (cadr method))
15239                  (list (car method) ""))
15240                 (t
15241                  (gnus-server-add-address method)))))))
15242
15243 (defun gnus-check-backend-function (func group)
15244   "Check whether GROUP supports function FUNC."
15245   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15246                   group)))
15247     (fboundp (intern (format "%s-%s" method func)))))
15248
15249 (defun gnus-methods-using (feature)
15250   "Find all methods that have FEATURE."
15251   (let ((valids gnus-valid-select-methods)
15252         outs)
15253     (while valids
15254       (if (memq feature (car valids))
15255           (setq outs (cons (car valids) outs)))
15256       (setq valids (cdr valids)))
15257     outs))
15258
15259 \f
15260 ;;;
15261 ;;; Active & Newsrc File Handling
15262 ;;;
15263
15264 (defun gnus-setup-news (&optional rawfile level dont-connect)
15265   "Setup news information.
15266 If RAWFILE is non-nil, the .newsrc file will also be read.
15267 If LEVEL is non-nil, the news will be set up at level LEVEL."
15268   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15269
15270     (when init 
15271       ;; Clear some variables to re-initialize news information.
15272       (setq gnus-newsrc-alist nil
15273             gnus-active-hashtb nil)
15274       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15275       (gnus-read-newsrc-file rawfile))
15276
15277     (when (and (not (assoc "archive" gnus-server-alist))
15278                gnus-message-archive-method)
15279       (push (cons "archive" gnus-message-archive-method)
15280             gnus-server-alist))
15281
15282     ;; If we don't read the complete active file, we fill in the
15283     ;; hashtb here.
15284     (if (or (null gnus-read-active-file)
15285             (eq gnus-read-active-file 'some))
15286         (gnus-update-active-hashtb-from-killed))
15287
15288     ;; Read the active file and create `gnus-active-hashtb'.
15289     ;; If `gnus-read-active-file' is nil, then we just create an empty
15290     ;; hash table.  The partial filling out of the hash table will be
15291     ;; done in `gnus-get-unread-articles'.
15292     (and gnus-read-active-file
15293          (not level)
15294          (gnus-read-active-file))
15295
15296     (or gnus-active-hashtb
15297         (setq gnus-active-hashtb (make-vector 4095 0)))
15298
15299     ;; Initialize the cache.
15300     (when gnus-use-cache
15301       (gnus-cache-open))
15302
15303     ;; Possibly eval the dribble file.
15304     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15305
15306     ;; Slave Gnusii should then clear the dribble buffer.
15307     (when (and init gnus-slave)
15308       (gnus-dribble-clear))
15309
15310     (gnus-update-format-specifications)
15311
15312     ;; See whether we need to read the description file.
15313     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15314              (not gnus-description-hashtb)
15315              (not dont-connect)
15316              gnus-read-active-file)
15317         (gnus-read-all-descriptions-files))
15318
15319     ;; Find new newsgroups and treat them.
15320     (if (and init gnus-check-new-newsgroups (not level)
15321              (gnus-check-server gnus-select-method))
15322         (gnus-find-new-newsgroups))
15323
15324     ;; We might read in new NoCeM messages here.
15325     (when (and gnus-use-nocem 
15326                (not level)
15327                (not dont-connect))
15328       (gnus-nocem-scan-groups))
15329
15330     ;; Find the number of unread articles in each non-dead group.
15331     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15332       (gnus-get-unread-articles level))
15333
15334     (if (and init gnus-check-bogus-newsgroups
15335              gnus-read-active-file (not level)
15336              (gnus-server-opened gnus-select-method))
15337         (gnus-check-bogus-newsgroups))))
15338
15339 (defun gnus-find-new-newsgroups (&optional arg)
15340   "Search for new newsgroups and add them.
15341 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15342 The `-n' option line from .newsrc is respected.
15343 If ARG (the prefix), use the `ask-server' method to query
15344 the server for new groups."
15345   (interactive "P")
15346   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15347                        (null gnus-read-active-file)
15348                        (eq gnus-read-active-file 'some))
15349                    'ask-server gnus-check-new-newsgroups)))
15350     (unless (gnus-check-first-time-used)
15351       (if (or (consp check)
15352               (eq check 'ask-server))
15353           ;; Ask the server for new groups.
15354           (gnus-ask-server-for-new-groups)
15355         ;; Go through the active hashtb and look for new groups.
15356         (let ((groups 0)
15357               group new-newsgroups)
15358           (gnus-message 5 "Looking for new newsgroups...")
15359           (unless gnus-have-read-active-file
15360             (gnus-read-active-file))
15361           (setq gnus-newsrc-last-checked-date (current-time-string))
15362           (unless gnus-killed-hashtb
15363             (gnus-make-hashtable-from-killed))
15364           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15365           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15366           (mapatoms
15367            (lambda (sym)
15368              (if (or (null (setq group (symbol-name sym)))
15369                      (not (boundp sym))
15370                      (null (symbol-value sym))
15371                      (gnus-gethash group gnus-killed-hashtb)
15372                      (gnus-gethash group gnus-newsrc-hashtb))
15373                  ()
15374                (let ((do-sub (gnus-matches-options-n group)))
15375                  (cond
15376                   ((eq do-sub 'subscribe)
15377                    (setq groups (1+ groups))
15378                    (gnus-sethash group group gnus-killed-hashtb)
15379                    (funcall gnus-subscribe-options-newsgroup-method group))
15380                   ((eq do-sub 'ignore)
15381                    nil)
15382                   (t
15383                    (setq groups (1+ groups))
15384                    (gnus-sethash group group gnus-killed-hashtb)
15385                    (if gnus-subscribe-hierarchical-interactive
15386                        (setq new-newsgroups (cons group new-newsgroups))
15387                      (funcall gnus-subscribe-newsgroup-method group)))))))
15388            gnus-active-hashtb)
15389           (when new-newsgroups
15390             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15391           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15392           (if (> groups 0)
15393               (gnus-message 6 "%d new newsgroup%s arrived."
15394                             groups (if (> groups 1) "s have" " has"))
15395             (gnus-message 6 "No new newsgroups.")))))))
15396
15397 (defun gnus-matches-options-n (group)
15398   ;; Returns `subscribe' if the group is to be unconditionally
15399   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15400   ;; no match for the group.
15401
15402   ;; First we check the two user variables.
15403   (cond
15404    ((and gnus-options-subscribe
15405          (string-match gnus-options-subscribe group))
15406     'subscribe)
15407    ((and gnus-auto-subscribed-groups
15408          (string-match gnus-auto-subscribed-groups group))
15409     'subscribe)
15410    ((and gnus-options-not-subscribe
15411          (string-match gnus-options-not-subscribe group))
15412     'ignore)
15413    ;; Then we go through the list that was retrieved from the .newsrc
15414    ;; file.  This list has elements on the form
15415    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15416    ;; is in the reverse order of the options line) is returned.
15417    (t
15418     (let ((regs gnus-newsrc-options-n))
15419       (while (and regs
15420                   (not (string-match (caar regs) group)))
15421         (setq regs (cdr regs)))
15422       (and regs (cdar regs))))))
15423
15424 (defun gnus-ask-server-for-new-groups ()
15425   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15426          (methods (cons gnus-select-method
15427                         (nconc
15428                          (when gnus-message-archive-method
15429                            (list "archive"))
15430                          (append
15431                           (and (consp gnus-check-new-newsgroups)
15432                                gnus-check-new-newsgroups)
15433                           gnus-secondary-select-methods))))
15434          (groups 0)
15435          (new-date (current-time-string))
15436          group new-newsgroups got-new method hashtb
15437          gnus-override-subscribe-method)
15438     ;; Go through both primary and secondary select methods and
15439     ;; request new newsgroups.
15440     (while (setq method (gnus-server-get-method nil (pop methods)))
15441       (setq new-newsgroups nil)
15442       (setq gnus-override-subscribe-method method)
15443       (when (and (gnus-check-server method)
15444                  (gnus-request-newgroups date method))
15445         (save-excursion
15446           (setq got-new t)
15447           (setq hashtb (gnus-make-hashtable 100))
15448           (set-buffer nntp-server-buffer)
15449           ;; Enter all the new groups into a hashtable.
15450           (gnus-active-to-gnus-format method hashtb 'ignore))
15451         ;; Now all new groups from `method' are in `hashtb'.
15452         (mapatoms
15453          (lambda (group-sym)
15454            (if (or (null (setq group (symbol-name group-sym)))
15455                    (not (boundp group-sym))
15456                    (null (symbol-value group-sym))
15457                    (gnus-gethash group gnus-newsrc-hashtb)
15458                    (member group gnus-zombie-list)
15459                    (member group gnus-killed-list))
15460                ;; The group is already known.
15461                ()
15462              ;; Make this group active.
15463              (when (symbol-value group-sym)
15464                (gnus-set-active group (symbol-value group-sym)))
15465              ;; Check whether we want it or not.
15466              (let ((do-sub (gnus-matches-options-n group)))
15467                (cond
15468                 ((eq do-sub 'subscribe)
15469                  (incf groups)
15470                  (gnus-sethash group group gnus-killed-hashtb)
15471                  (funcall gnus-subscribe-options-newsgroup-method group))
15472                 ((eq do-sub 'ignore)
15473                  nil)
15474                 (t
15475                  (incf groups)
15476                  (gnus-sethash group group gnus-killed-hashtb)
15477                  (if gnus-subscribe-hierarchical-interactive
15478                      (push group new-newsgroups)
15479                    (funcall gnus-subscribe-newsgroup-method group)))))))
15480          hashtb))
15481       (when new-newsgroups
15482         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15483     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15484     (when (> groups 0)
15485       (gnus-message 6 "%d new newsgroup%s arrived."
15486                     groups (if (> groups 1) "s have" " has")))
15487     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15488     got-new))
15489
15490 (defun gnus-check-first-time-used ()
15491   (if (or (> (length gnus-newsrc-alist) 1)
15492           (file-exists-p gnus-startup-file)
15493           (file-exists-p (concat gnus-startup-file ".el"))
15494           (file-exists-p (concat gnus-startup-file ".eld")))
15495       nil
15496     (gnus-message 6 "First time user; subscribing you to default groups")
15497     (unless (gnus-read-active-file-p)
15498       (gnus-read-active-file))
15499     (setq gnus-newsrc-last-checked-date (current-time-string))
15500     (let ((groups gnus-default-subscribed-newsgroups)
15501           group)
15502       (if (eq groups t)
15503           nil
15504         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15505         (mapatoms
15506          (lambda (sym)
15507            (if (null (setq group (symbol-name sym)))
15508                ()
15509              (let ((do-sub (gnus-matches-options-n group)))
15510                (cond
15511                 ((eq do-sub 'subscribe)
15512                  (gnus-sethash group group gnus-killed-hashtb)
15513                  (funcall gnus-subscribe-options-newsgroup-method group))
15514                 ((eq do-sub 'ignore)
15515                  nil)
15516                 (t
15517                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15518          gnus-active-hashtb)
15519         (while groups
15520           (if (gnus-active (car groups))
15521               (gnus-group-change-level
15522                (car groups) gnus-level-default-subscribed gnus-level-killed))
15523           (setq groups (cdr groups)))
15524         (gnus-group-make-help-group)
15525         (and gnus-novice-user
15526              (gnus-message 7 "`A k' to list killed groups"))))))
15527
15528 (defun gnus-subscribe-group (group previous &optional method)
15529   (gnus-group-change-level
15530    (if method
15531        (list t group gnus-level-default-subscribed nil nil method)
15532      group)
15533    gnus-level-default-subscribed gnus-level-killed previous t))
15534
15535 ;; `gnus-group-change-level' is the fundamental function for changing
15536 ;; subscription levels of newsgroups.  This might mean just changing
15537 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15538 ;; again, which subscribes/unsubscribes a group, which is equally
15539 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15540 ;; from 8-9 to 1-7 means that you remove the group from the list of
15541 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15542 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15543 ;; which is trivial.
15544 ;; ENTRY can either be a string (newsgroup name) or a list (if
15545 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15546 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15547 ;; entries.
15548 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15549 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15550 ;; after.
15551 (defun gnus-group-change-level (entry level &optional oldlevel
15552                                       previous fromkilled)
15553   (let (group info active num)
15554     ;; Glean what info we can from the arguments
15555     (if (consp entry)
15556         (if fromkilled (setq group (nth 1 entry))
15557           (setq group (car (nth 2 entry))))
15558       (setq group entry))
15559     (if (and (stringp entry)
15560              oldlevel
15561              (< oldlevel gnus-level-zombie))
15562         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15563     (if (and (not oldlevel)
15564              (consp entry))
15565         (setq oldlevel (gnus-info-level (nth 2 entry)))
15566       (setq oldlevel (or oldlevel 9)))
15567     (if (stringp previous)
15568         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15569
15570     (if (and (>= oldlevel gnus-level-zombie)
15571              (gnus-gethash group gnus-newsrc-hashtb))
15572         ;; We are trying to subscribe a group that is already
15573         ;; subscribed.
15574         ()                              ; Do nothing.
15575
15576       (or (gnus-ephemeral-group-p group)
15577           (gnus-dribble-enter
15578            (format "(gnus-group-change-level %S %S %S %S %S)"
15579                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15580
15581       ;; Then we remove the newgroup from any old structures, if needed.
15582       ;; If the group was killed, we remove it from the killed or zombie
15583       ;; list.  If not, and it is in fact going to be killed, we remove
15584       ;; it from the newsrc hash table and assoc.
15585       (cond
15586        ((>= oldlevel gnus-level-zombie)
15587         (if (= oldlevel gnus-level-zombie)
15588             (setq gnus-zombie-list (delete group gnus-zombie-list))
15589           (setq gnus-killed-list (delete group gnus-killed-list))))
15590        (t
15591         (if (and (>= level gnus-level-zombie)
15592                  entry)
15593             (progn
15594               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15595               (if (nth 3 entry)
15596                   (setcdr (gnus-gethash (car (nth 3 entry))
15597                                         gnus-newsrc-hashtb)
15598                           (cdr entry)))
15599               (setcdr (cdr entry) (cdddr entry))))))
15600
15601       ;; Finally we enter (if needed) the list where it is supposed to
15602       ;; go, and change the subscription level.  If it is to be killed,
15603       ;; we enter it into the killed or zombie list.
15604       (cond 
15605        ((>= level gnus-level-zombie)
15606         ;; Remove from the hash table.
15607         (gnus-sethash group nil gnus-newsrc-hashtb)
15608         ;; We do not enter foreign groups into the list of dead
15609         ;; groups.
15610         (unless (gnus-group-foreign-p group)
15611           (if (= level gnus-level-zombie)
15612               (setq gnus-zombie-list (cons group gnus-zombie-list))
15613             (setq gnus-killed-list (cons group gnus-killed-list)))))
15614        (t
15615         ;; If the list is to be entered into the newsrc assoc, and
15616         ;; it was killed, we have to create an entry in the newsrc
15617         ;; hashtb format and fix the pointers in the newsrc assoc.
15618         (if (< oldlevel gnus-level-zombie)
15619             ;; It was alive, and it is going to stay alive, so we
15620             ;; just change the level and don't change any pointers or
15621             ;; hash table entries.
15622             (setcar (cdaddr entry) level)
15623           (if (listp entry)
15624               (setq info (cdr entry)
15625                     num (car entry))
15626             (setq active (gnus-active group))
15627             (setq num
15628                   (if active (- (1+ (cdr active)) (car active)) t))
15629             ;; Check whether the group is foreign.  If so, the
15630             ;; foreign select method has to be entered into the
15631             ;; info.
15632             (let ((method (or gnus-override-subscribe-method
15633                               (gnus-group-method group))))
15634               (if (eq method gnus-select-method)
15635                   (setq info (list group level nil))
15636                 (setq info (list group level nil nil method)))))
15637           (unless previous
15638             (setq previous
15639                   (let ((p gnus-newsrc-alist))
15640                     (while (cddr p)
15641                       (setq p (cdr p)))
15642                     p)))
15643           (setq entry (cons info (cddr previous)))
15644           (if (cdr previous)
15645               (progn
15646                 (setcdr (cdr previous) entry)
15647                 (gnus-sethash group (cons num (cdr previous))
15648                               gnus-newsrc-hashtb))
15649             (setcdr previous entry)
15650             (gnus-sethash group (cons num previous)
15651                           gnus-newsrc-hashtb))
15652           (when (cdr entry)
15653             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15654       (when gnus-group-change-level-function
15655         (funcall gnus-group-change-level-function group level oldlevel)))))
15656
15657 (defun gnus-kill-newsgroup (newsgroup)
15658   "Obsolete function.  Kills a newsgroup."
15659   (gnus-group-change-level
15660    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15661
15662 (defun gnus-check-bogus-newsgroups (&optional confirm)
15663   "Remove bogus newsgroups.
15664 If CONFIRM is non-nil, the user has to confirm the deletion of every
15665 newsgroup."
15666   (let ((newsrc (cdr gnus-newsrc-alist))
15667         bogus group entry info)
15668     (gnus-message 5 "Checking bogus newsgroups...")
15669     (unless (gnus-read-active-file-p)
15670       (gnus-read-active-file))
15671     (when (gnus-read-active-file-p)
15672       ;; Find all bogus newsgroup that are subscribed.
15673       (while newsrc
15674         (setq info (pop newsrc)
15675               group (gnus-info-group info))
15676         (unless (or (gnus-active group) ; Active
15677                     (gnus-info-method info) ; Foreign
15678                     (and confirm
15679                          (not (gnus-y-or-n-p
15680                                (format "Remove bogus newsgroup: %s " group)))))
15681           ;; Found a bogus newsgroup.
15682           (push group bogus)))
15683       ;; Remove all bogus subscribed groups by first killing them, and
15684       ;; then removing them from the list of killed groups.
15685       (while bogus
15686         (when (setq entry (gnus-gethash (setq group (pop bogus))
15687                                         gnus-newsrc-hashtb))
15688           (gnus-group-change-level entry gnus-level-killed)
15689           (setq gnus-killed-list (delete group gnus-killed-list))))
15690       ;; Then we remove all bogus groups from the list of killed and
15691       ;; zombie groups.  They are are removed without confirmation.
15692       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15693             killed)
15694         (while dead-lists
15695           (setq killed (symbol-value (car dead-lists)))
15696           (while killed
15697             (unless (gnus-active (setq group (pop killed)))
15698               ;; The group is bogus.
15699               ;; !!!Slow as hell.
15700               (set (car dead-lists)
15701                    (delete group (symbol-value (car dead-lists))))))
15702           (setq dead-lists (cdr dead-lists))))
15703       (gnus-message 5 "Checking bogus newsgroups...done"))))
15704
15705 (defun gnus-check-duplicate-killed-groups ()
15706   "Remove duplicates from the list of killed groups."
15707   (interactive)
15708   (let ((killed gnus-killed-list))
15709     (while killed
15710       (gnus-message 9 "%d" (length killed))
15711       (setcdr killed (delete (car killed) (cdr killed)))
15712       (setq killed (cdr killed)))))
15713
15714 ;; We want to inline a function from gnus-cache, so we cheat here:
15715 (eval-when-compile
15716   (provide 'gnus)
15717   (require 'gnus-cache))
15718
15719 (defun gnus-get-unread-articles-in-group (info active &optional update)
15720   (when active
15721     ;; Allow the backend to update the info in the group.
15722     (when (and update 
15723                (gnus-request-update-info
15724                 info (gnus-find-method-for-group (gnus-info-group info))))
15725       (gnus-activate-group (gnus-info-group info) nil t))
15726     (let* ((range (gnus-info-read info))
15727            (num 0))
15728       ;; If a cache is present, we may have to alter the active info.
15729       (when (and gnus-use-cache info)
15730         (inline (gnus-cache-possibly-alter-active 
15731                  (gnus-info-group info) active)))
15732       ;; Modify the list of read articles according to what articles
15733       ;; are available; then tally the unread articles and add the
15734       ;; number to the group hash table entry.
15735       (cond
15736        ((zerop (cdr active))
15737         (setq num 0))
15738        ((not range)
15739         (setq num (- (1+ (cdr active)) (car active))))
15740        ((not (listp (cdr range)))
15741         ;; Fix a single (num . num) range according to the
15742         ;; active hash table.
15743         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15744         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15745         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15746         ;; Compute number of unread articles.
15747         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15748        (t
15749         ;; The read list is a list of ranges.  Fix them according to
15750         ;; the active hash table.
15751         ;; First peel off any elements that are below the lower
15752         ;; active limit.
15753         (while (and (cdr range)
15754                     (>= (car active)
15755                         (or (and (atom (cadr range)) (cadr range))
15756                             (caadr range))))
15757           (if (numberp (car range))
15758               (setcar range
15759                       (cons (car range)
15760                             (or (and (numberp (cadr range))
15761                                      (cadr range))
15762                                 (cdadr range))))
15763             (setcdr (car range)
15764                     (or (and (numberp (nth 1 range)) (nth 1 range))
15765                         (cdadr range))))
15766           (setcdr range (cddr range)))
15767         ;; Adjust the first element to be the same as the lower limit.
15768         (if (and (not (atom (car range)))
15769                  (< (cdar range) (car active)))
15770             (setcdr (car range) (1- (car active))))
15771         ;; Then we want to peel off any elements that are higher
15772         ;; than the upper active limit.
15773         (let ((srange range))
15774           ;; Go past all legal elements.
15775           (while (and (cdr srange)
15776                       (<= (or (and (atom (cadr srange))
15777                                    (cadr srange))
15778                               (caadr srange)) (cdr active)))
15779             (setq srange (cdr srange)))
15780           (if (cdr srange)
15781               ;; Nuke all remaining illegal elements.
15782               (setcdr srange nil))
15783
15784           ;; Adjust the final element.
15785           (if (and (not (atom (car srange)))
15786                    (> (cdar srange) (cdr active)))
15787               (setcdr (car srange) (cdr active))))
15788         ;; Compute the number of unread articles.
15789         (while range
15790           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15791                                       (cdar range)))
15792                               (or (and (atom (car range)) (car range))
15793                                   (caar range)))))
15794           (setq range (cdr range)))
15795         (setq num (max 0 (- (cdr active) num)))))
15796       ;; Set the number of unread articles.
15797       (when info
15798         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15799       num)))
15800
15801 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15802 ;; and compute how many unread articles there are in each group.
15803 (defun gnus-get-unread-articles (&optional level)
15804   (let* ((newsrc (cdr gnus-newsrc-alist))
15805          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15806          (foreign-level
15807           (min
15808            (cond ((and gnus-activate-foreign-newsgroups
15809                        (not (numberp gnus-activate-foreign-newsgroups)))
15810                   (1+ gnus-level-subscribed))
15811                  ((numberp gnus-activate-foreign-newsgroups)
15812                   gnus-activate-foreign-newsgroups)
15813                  (t 0))
15814            level))
15815          info group active method)
15816     (gnus-message 5 "Checking new news...")
15817
15818     (while newsrc
15819       (setq active (gnus-active (setq group (gnus-info-group
15820                                              (setq info (pop newsrc))))))
15821
15822       ;; Check newsgroups.  If the user doesn't want to check them, or
15823       ;; they can't be checked (for instance, if the news server can't
15824       ;; be reached) we just set the number of unread articles in this
15825       ;; newsgroup to t.  This means that Gnus thinks that there are
15826       ;; unread articles, but it has no idea how many.
15827       (if (and (setq method (gnus-info-method info))
15828                (not (gnus-server-equal
15829                      gnus-select-method
15830                      (setq method (gnus-server-get-method nil method))))
15831                (not (gnus-secondary-method-p method)))
15832           ;; These groups are foreign.  Check the level.
15833           (when (<= (gnus-info-level info) foreign-level)
15834             (setq active (gnus-activate-group group 'scan))
15835             (unless (inline (gnus-virtual-group-p group))
15836               (inline (gnus-close-group group)))
15837             (when (fboundp (intern (concat (symbol-name (car method))
15838                                            "-request-update-info")))
15839               (inline (gnus-request-update-info info method))))
15840         ;; These groups are native or secondary.
15841         (when (and (<= (gnus-info-level info) level)
15842                    (not gnus-read-active-file))
15843           (setq active (gnus-activate-group group 'scan))
15844           (inline (gnus-close-group group))))
15845
15846       ;; Get the number of unread articles in the group.
15847       (if active
15848           (inline (gnus-get-unread-articles-in-group info active))
15849         ;; The group couldn't be reached, so we nix out the number of
15850         ;; unread articles and stuff.
15851         (gnus-set-active group nil)
15852         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15853
15854     (gnus-message 5 "Checking new news...done")))
15855
15856 ;; Create a hash table out of the newsrc alist.  The `car's of the
15857 ;; alist elements are used as keys.
15858 (defun gnus-make-hashtable-from-newsrc-alist ()
15859   (let ((alist gnus-newsrc-alist)
15860         (ohashtb gnus-newsrc-hashtb)
15861         prev)
15862     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15863     (setq alist
15864           (setq prev (setq gnus-newsrc-alist
15865                            (if (equal (caar gnus-newsrc-alist)
15866                                       "dummy.group")
15867                                gnus-newsrc-alist
15868                              (cons (list "dummy.group" 0 nil) alist)))))
15869     (while alist
15870       (gnus-sethash
15871        (caar alist)
15872        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15873              prev)
15874        gnus-newsrc-hashtb)
15875       (setq prev alist
15876             alist (cdr alist)))))
15877
15878 (defun gnus-make-hashtable-from-killed ()
15879   "Create a hash table from the killed and zombie lists."
15880   (let ((lists '(gnus-killed-list gnus-zombie-list))
15881         list)
15882     (setq gnus-killed-hashtb
15883           (gnus-make-hashtable
15884            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15885     (while (setq list (pop lists))
15886       (setq list (symbol-value list))
15887       (while list
15888         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15889
15890 (defun gnus-activate-group (group &optional scan dont-check)
15891   ;; Check whether a group has been activated or not.
15892   ;; If SCAN, request a scan of that group as well.
15893   (let ((method (gnus-find-method-for-group group))
15894         active)
15895     (and (gnus-check-server method)
15896          ;; We escape all bugs and quit here to make it possible to
15897          ;; continue if a group is so out-there that it reports bugs
15898          ;; and stuff.
15899          (progn
15900            (and scan
15901                 (gnus-check-backend-function 'request-scan (car method))
15902                 (gnus-request-scan group method))
15903            t)
15904          (condition-case ()
15905              (gnus-request-group group dont-check)
15906         ;   (error nil)
15907            (quit nil))
15908          (save-excursion
15909            (set-buffer nntp-server-buffer)
15910            (goto-char (point-min))
15911            ;; Parse the result we got from `gnus-request-group'.
15912            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15913                 (progn
15914                   (goto-char (match-beginning 1))
15915                   (gnus-set-active
15916                    group (setq active (cons (read (current-buffer))
15917                                             (read (current-buffer)))))
15918                   ;; Return the new active info.
15919                   active))))))
15920
15921 (defun gnus-update-read-articles (group unread)
15922   "Update the list of read and ticked articles in GROUP using the
15923 UNREAD and TICKED lists.
15924 Note: UNSELECTED has to be sorted over `<'.
15925 Returns whether the updating was successful."
15926   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15927          (entry (gnus-gethash group gnus-newsrc-hashtb))
15928          (info (nth 2 entry))
15929          (prev 1)
15930          (unread (sort (copy-sequence unread) '<))
15931          read)
15932     (if (or (not info) (not active))
15933         ;; There is no info on this group if it was, in fact,
15934         ;; killed.  Gnus stores no information on killed groups, so
15935         ;; there's nothing to be done.
15936         ;; One could store the information somewhere temporarily,
15937         ;; perhaps...  Hmmm...
15938         ()
15939       ;; Remove any negative articles numbers.
15940       (while (and unread (< (car unread) 0))
15941         (setq unread (cdr unread)))
15942       ;; Remove any expired article numbers
15943       (while (and unread (< (car unread) (car active)))
15944         (setq unread (cdr unread)))
15945       ;; Compute the ranges of read articles by looking at the list of
15946       ;; unread articles.
15947       (while unread
15948         (if (/= (car unread) prev)
15949             (setq read (cons (if (= prev (1- (car unread))) prev
15950                                (cons prev (1- (car unread)))) read)))
15951         (setq prev (1+ (car unread)))
15952         (setq unread (cdr unread)))
15953       (when (<= prev (cdr active))
15954         (setq read (cons (cons prev (cdr active)) read)))
15955       ;; Enter this list into the group info.
15956       (gnus-info-set-read
15957        info (if (> (length read) 1) (nreverse read) read))
15958       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15959       (gnus-get-unread-articles-in-group info (gnus-active group))
15960       t)))
15961
15962 (defun gnus-make-articles-unread (group articles)
15963   "Mark ARTICLES in GROUP as unread."
15964   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15965                           (gnus-gethash (gnus-group-real-name group)
15966                                         gnus-newsrc-hashtb))))
15967          (ranges (gnus-info-read info))
15968          news article)
15969     (while articles
15970       (when (gnus-member-of-range
15971              (setq article (pop articles)) ranges)
15972         (setq news (cons article news))))
15973     (when news
15974       (gnus-info-set-read
15975        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15976       (gnus-group-update-group group t))))
15977
15978 ;; Enter all dead groups into the hashtb.
15979 (defun gnus-update-active-hashtb-from-killed ()
15980   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15981         (lists (list gnus-killed-list gnus-zombie-list))
15982         killed)
15983     (while lists
15984       (setq killed (car lists))
15985       (while killed
15986         (gnus-sethash (car killed) nil hashtb)
15987         (setq killed (cdr killed)))
15988       (setq lists (cdr lists)))))
15989
15990 (defun gnus-get-killed-groups ()
15991   "Go through the active hashtb and all all unknown groups as killed."
15992   ;; First make sure active file has been read.
15993   (unless (gnus-read-active-file-p)
15994     (let ((gnus-read-active-file t))
15995       (gnus-read-active-file)))
15996   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15997   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15998   (mapatoms
15999    (lambda (sym)
16000      (let ((groups 0)
16001            (group (symbol-name sym)))
16002        (if (or (null group)
16003                (gnus-gethash group gnus-killed-hashtb)
16004                (gnus-gethash group gnus-newsrc-hashtb))
16005            ()
16006          (let ((do-sub (gnus-matches-options-n group)))
16007            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16008                ()
16009              (setq groups (1+ groups))
16010              (setq gnus-killed-list
16011                    (cons group gnus-killed-list))
16012              (gnus-sethash group group gnus-killed-hashtb))))))
16013    gnus-active-hashtb))
16014
16015 ;; Get the active file(s) from the backend(s).
16016 (defun gnus-read-active-file ()
16017   (gnus-group-set-mode-line)
16018   (let ((methods 
16019          (append
16020           (if (gnus-check-server gnus-select-method)
16021               ;; The native server is available.
16022               (cons gnus-select-method gnus-secondary-select-methods)
16023             ;; The native server is down, so we just do the
16024             ;; secondary ones.
16025             gnus-secondary-select-methods)
16026           ;; Also read from the archive server.
16027           (when gnus-message-archive-method
16028             (list "archive"))))
16029         list-type)
16030     (setq gnus-have-read-active-file nil)
16031     (save-excursion
16032       (set-buffer nntp-server-buffer)
16033       (while methods
16034         (let* ((method (if (stringp (car methods))
16035                            (gnus-server-get-method nil (car methods))
16036                          (car methods)))
16037                (where (nth 1 method))
16038                (mesg (format "Reading active file%s via %s..."
16039                              (if (and where (not (zerop (length where))))
16040                                  (concat " from " where) "")
16041                              (car method))))
16042           (gnus-message 5 mesg)
16043           (when (gnus-check-server method)
16044             ;; Request that the backend scan its incoming messages.
16045             (and (gnus-check-backend-function 'request-scan (car method))
16046                  (gnus-request-scan nil method))
16047             (cond
16048              ((and (eq gnus-read-active-file 'some)
16049                    (gnus-check-backend-function 'retrieve-groups (car method)))
16050               (let ((newsrc (cdr gnus-newsrc-alist))
16051                     (gmethod (gnus-server-get-method nil method))
16052                     groups info)
16053                 (while (setq info (pop newsrc))
16054                   (when (gnus-server-equal
16055                          (gnus-find-method-for-group 
16056                           (gnus-info-group info) info)
16057                          gmethod)
16058                     (push (gnus-group-real-name (gnus-info-group info)) 
16059                           groups)))
16060                 (when groups
16061                   (gnus-check-server method)
16062                   (setq list-type (gnus-retrieve-groups groups method))
16063                   (cond
16064                    ((not list-type)
16065                     (gnus-error
16066                      1.2 "Cannot read partial active file from %s server."
16067                      (car method)))
16068                    ((eq list-type 'active)
16069                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16070                    (t
16071                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16072              (t
16073               (if (not (gnus-request-list method))
16074                   (unless (equal method gnus-message-archive-method)
16075                     (gnus-error 1 "Cannot read active file from %s server."
16076                                 (car method)))
16077                 (gnus-message 5 mesg)
16078                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16079                 ;; We mark this active file as read.
16080                 (push method gnus-have-read-active-file)
16081                 (gnus-message 5 "%sdone" mesg))))))
16082         (setq methods (cdr methods))))))
16083
16084 ;; Read an active file and place the results in `gnus-active-hashtb'.
16085 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16086   (unless method
16087     (setq method gnus-select-method))
16088   (let ((cur (current-buffer))
16089         (hashtb (or hashtb
16090                     (if (and gnus-active-hashtb
16091                              (not (equal method gnus-select-method)))
16092                         gnus-active-hashtb
16093                       (setq gnus-active-hashtb
16094                             (if (equal method gnus-select-method)
16095                                 (gnus-make-hashtable
16096                                  (count-lines (point-min) (point-max)))
16097                               (gnus-make-hashtable 4096)))))))
16098     ;; Delete unnecessary lines.
16099     (goto-char (point-min))
16100     (while (search-forward "\nto." nil t)
16101       (delete-region (1+ (match-beginning 0))
16102                      (progn (forward-line 1) (point))))
16103     (or (string= gnus-ignored-newsgroups "")
16104         (progn
16105           (goto-char (point-min))
16106           (delete-matching-lines gnus-ignored-newsgroups)))
16107     ;; Make the group names readable as a lisp expression even if they
16108     ;; contain special characters.
16109     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16110     (goto-char (point-max))
16111     (while (re-search-backward "[][';?()#]" nil t)
16112       (insert ?\\))
16113     ;; If these are groups from a foreign select method, we insert the
16114     ;; group prefix in front of the group names.
16115     (and method (not (gnus-server-equal
16116                       (gnus-server-get-method nil method)
16117                       (gnus-server-get-method nil gnus-select-method)))
16118          (let ((prefix (gnus-group-prefixed-name "" method)))
16119            (goto-char (point-min))
16120            (while (and (not (eobp))
16121                        (progn (insert prefix)
16122                               (zerop (forward-line 1)))))))
16123     ;; Store the active file in a hash table.
16124     (goto-char (point-min))
16125     (if (string-match "%[oO]" gnus-group-line-format)
16126         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16127         ;; If we want information on moderated groups, we use this
16128         ;; loop...
16129         (let* ((mod-hashtb (make-vector 7 0))
16130                (m (intern "m" mod-hashtb))
16131                group max min)
16132           (while (not (eobp))
16133             (condition-case nil
16134                 (progn
16135                   (narrow-to-region (point) (gnus-point-at-eol))
16136                   (setq group (let ((obarray hashtb)) (read cur)))
16137                   (if (and (numberp (setq max (read cur)))
16138                            (numberp (setq min (read cur)))
16139                            (progn
16140                              (skip-chars-forward " \t")
16141                              (not
16142                               (or (= (following-char) ?=)
16143                                   (= (following-char) ?x)
16144                                   (= (following-char) ?j)))))
16145                       (set group (cons min max))
16146                     (set group nil))
16147                   ;; Enter moderated groups into a list.
16148                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16149                       (setq gnus-moderated-list
16150                             (cons (symbol-name group) gnus-moderated-list))))
16151               (error
16152                (and group
16153                     (symbolp group)
16154                     (set group nil))))
16155             (widen)
16156             (forward-line 1)))
16157       ;; And if we do not care about moderation, we use this loop,
16158       ;; which is faster.
16159       (let (group max min)
16160         (while (not (eobp))
16161           (condition-case ()
16162               (progn
16163                 (narrow-to-region (point) (gnus-point-at-eol))
16164                 ;; group gets set to a symbol interned in the hash table
16165                 ;; (what a hack!!) - jwz
16166                 (setq group (let ((obarray hashtb)) (read cur)))
16167                 (if (and (numberp (setq max (read cur)))
16168                          (numberp (setq min (read cur)))
16169                          (progn
16170                            (skip-chars-forward " \t")
16171                            (not
16172                             (or (= (following-char) ?=)
16173                                 (= (following-char) ?x)
16174                                 (= (following-char) ?j)))))
16175                     (set group (cons min max))
16176                   (set group nil)))
16177             (error
16178              (progn
16179                (and group
16180                     (symbolp group)
16181                     (set group nil))
16182                (or ignore-errors
16183                    (gnus-message 3 "Warning - illegal active: %s"
16184                                  (buffer-substring
16185                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16186           (widen)
16187           (forward-line 1))))))
16188
16189 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16190   ;; Parse a "groups" active file.
16191   (let ((cur (current-buffer))
16192         (hashtb (or hashtb
16193                     (if (and method gnus-active-hashtb)
16194                         gnus-active-hashtb
16195                       (setq gnus-active-hashtb
16196                             (gnus-make-hashtable
16197                              (count-lines (point-min) (point-max)))))))
16198         (prefix (and method
16199                      (not (gnus-server-equal
16200                            (gnus-server-get-method nil method)
16201                            (gnus-server-get-method nil gnus-select-method)))
16202                      (gnus-group-prefixed-name "" method))))
16203
16204     (goto-char (point-min))
16205     ;; We split this into to separate loops, one with the prefix
16206     ;; and one without to speed the reading up somewhat.
16207     (if prefix
16208         (let (min max opoint group)
16209           (while (not (eobp))
16210             (condition-case ()
16211                 (progn
16212                   (read cur) (read cur)
16213                   (setq min (read cur)
16214                         max (read cur)
16215                         opoint (point))
16216                   (skip-chars-forward " \t")
16217                   (insert prefix)
16218                   (goto-char opoint)
16219                   (set (let ((obarray hashtb)) (read cur))
16220                        (cons min max)))
16221               (error (and group (symbolp group) (set group nil))))
16222             (forward-line 1)))
16223       (let (min max group)
16224         (while (not (eobp))
16225           (condition-case ()
16226               (if (= (following-char) ?2)
16227                   (progn
16228                     (read cur) (read cur)
16229                     (setq min (read cur)
16230                           max (read cur))
16231                     (set (setq group (let ((obarray hashtb)) (read cur)))
16232                          (cons min max))))
16233             (error (and group (symbolp group) (set group nil))))
16234           (forward-line 1))))))
16235
16236 (defun gnus-read-newsrc-file (&optional force)
16237   "Read startup file.
16238 If FORCE is non-nil, the .newsrc file is read."
16239   ;; Reset variables that might be defined in the .newsrc.eld file.
16240   (let ((variables gnus-variable-list))
16241     (while variables
16242       (set (car variables) nil)
16243       (setq variables (cdr variables))))
16244   (let* ((newsrc-file gnus-current-startup-file)
16245          (quick-file (concat newsrc-file ".el")))
16246     (save-excursion
16247       ;; We always load the .newsrc.eld file.  If always contains
16248       ;; much information that can not be gotten from the .newsrc
16249       ;; file (ticked articles, killed groups, foreign methods, etc.)
16250       (gnus-read-newsrc-el-file quick-file)
16251
16252       (if (and (file-exists-p gnus-current-startup-file)
16253                (or force
16254                    (and (file-newer-than-file-p newsrc-file quick-file)
16255                         (file-newer-than-file-p newsrc-file
16256                                                 (concat quick-file "d")))
16257                    (not gnus-newsrc-alist)))
16258           ;; We read the .newsrc file.  Note that if there if a
16259           ;; .newsrc.eld file exists, it has already been read, and
16260           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16261           ;; the .newsrc file, Gnus will only use the information it
16262           ;; can find there for changing the data already read -
16263           ;; ie. reading the .newsrc file will not trash the data
16264           ;; already read (except for read articles).
16265           (save-excursion
16266             (gnus-message 5 "Reading %s..." newsrc-file)
16267             (set-buffer (find-file-noselect newsrc-file))
16268             (buffer-disable-undo (current-buffer))
16269             (gnus-newsrc-to-gnus-format)
16270             (kill-buffer (current-buffer))
16271             (gnus-message 5 "Reading %s...done" newsrc-file)))
16272
16273       ;; Read any slave files.
16274       (unless gnus-slave
16275         (gnus-master-read-slave-newsrc))
16276       
16277       ;; Convert old to new.
16278       (gnus-convert-old-newsrc))))
16279
16280 (defun gnus-continuum-version (version)
16281   "Return VERSION as a floating point number."
16282   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16283             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16284     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16285            (number (match-string 2 version))
16286            major minor least)
16287       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16288       (setq major (string-to-number (match-string 1 number)))
16289       (setq minor (string-to-number (match-string 2 number)))
16290       (setq least (if (match-beginning 3)
16291                       (string-to-number (match-string 3 number))
16292                     0))
16293       (string-to-number
16294        (if (zerop major)
16295            (format "%s00%02d%02d"
16296                    (cond 
16297                     ((member alpha '("(ding)" "d")) "4.99")
16298                     ((member alpha '("September" "s")) "5.01")
16299                     ((member alpha '("Red" "r")) "5.03"))
16300                    minor least)
16301          (format "%d.%02d%02d" major minor least))))))
16302
16303 (defun gnus-convert-old-newsrc ()
16304   "Convert old newsrc into the new format, if needed."
16305   (let ((fcv (and gnus-newsrc-file-version
16306                   (gnus-continuum-version gnus-newsrc-file-version))))
16307     (cond
16308      ;; No .newsrc.eld file was loaded.
16309      ((null fcv) nil)
16310      ;; Gnus 5 .newsrc.eld was loaded.
16311      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16312       (gnus-convert-old-ticks)))))
16313
16314 (defun gnus-convert-old-ticks ()
16315   (let ((newsrc (cdr gnus-newsrc-alist))
16316         marks info dormant ticked)
16317     (while (setq info (pop newsrc))
16318       (when (setq marks (gnus-info-marks info))
16319         (setq dormant (cdr (assq 'dormant marks))
16320               ticked (cdr (assq 'tick marks)))
16321         (when (or dormant ticked)
16322           (gnus-info-set-read
16323            info
16324            (gnus-add-to-range
16325             (gnus-info-read info)
16326             (nconc (gnus-uncompress-range dormant)
16327                    (gnus-uncompress-range ticked)))))))))
16328
16329 (defun gnus-read-newsrc-el-file (file)
16330   (let ((ding-file (concat file "d")))
16331     ;; We always, always read the .eld file.
16332     (gnus-message 5 "Reading %s..." ding-file)
16333     (let (gnus-newsrc-assoc)
16334       (condition-case nil
16335           (load ding-file t t t)
16336         (error
16337          (gnus-error 1 "Error in %s" ding-file)))
16338       (when gnus-newsrc-assoc
16339         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16340     (gnus-make-hashtable-from-newsrc-alist)
16341     (when (file-newer-than-file-p file ding-file)
16342       ;; Old format quick file
16343       (gnus-message 5 "Reading %s..." file)
16344       ;; The .el file is newer than the .eld file, so we read that one
16345       ;; as well.
16346       (gnus-read-old-newsrc-el-file file))))
16347
16348 ;; Parse the old-style quick startup file
16349 (defun gnus-read-old-newsrc-el-file (file)
16350   (let (newsrc killed marked group m info)
16351     (prog1
16352         (let ((gnus-killed-assoc nil)
16353               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16354           (prog1
16355               (condition-case nil
16356                   (load file t t t)
16357                 (error nil))
16358             (setq newsrc gnus-newsrc-assoc
16359                   killed gnus-killed-assoc
16360                   marked gnus-marked-assoc)))
16361       (setq gnus-newsrc-alist nil)
16362       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16363         (if info
16364             (progn
16365               (gnus-info-set-read info (cddr group))
16366               (gnus-info-set-level
16367                info (if (nth 1 group) gnus-level-default-subscribed
16368                       gnus-level-default-unsubscribed))
16369               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16370           (push (setq info
16371                       (list (car group)
16372                             (if (nth 1 group) gnus-level-default-subscribed
16373                               gnus-level-default-unsubscribed)
16374                             (cddr group)))
16375                 gnus-newsrc-alist))
16376         ;; Copy marks into info.
16377         (when (setq m (assoc (car group) marked))
16378           (unless (nthcdr 3 info)
16379             (nconc info (list nil)))
16380           (gnus-info-set-marks
16381            info (list (cons 'tick (gnus-compress-sequence 
16382                                    (sort (cdr m) '<) t))))))
16383       (setq newsrc killed)
16384       (while newsrc
16385         (setcar newsrc (caar newsrc))
16386         (setq newsrc (cdr newsrc)))
16387       (setq gnus-killed-list killed))
16388     ;; The .el file version of this variable does not begin with
16389     ;; "options", while the .eld version does, so we just add it if it
16390     ;; isn't there.
16391     (and
16392      gnus-newsrc-options
16393      (progn
16394        (and (not (string-match "^ *options" gnus-newsrc-options))
16395             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16396        (and (not (string-match "\n$" gnus-newsrc-options))
16397             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16398        ;; Finally, if we read some options lines, we parse them.
16399        (or (string= gnus-newsrc-options "")
16400            (gnus-newsrc-parse-options gnus-newsrc-options))))
16401
16402     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16403     (gnus-make-hashtable-from-newsrc-alist)))
16404
16405 (defun gnus-make-newsrc-file (file)
16406   "Make server dependent file name by catenating FILE and server host name."
16407   (let* ((file (expand-file-name file nil))
16408          (real-file (concat file "-" (nth 1 gnus-select-method))))
16409     (if (or (file-exists-p real-file)
16410             (file-exists-p (concat real-file ".el"))
16411             (file-exists-p (concat real-file ".eld")))
16412         real-file file)))
16413
16414 (defun gnus-newsrc-to-gnus-format ()
16415   (setq gnus-newsrc-options "")
16416   (setq gnus-newsrc-options-n nil)
16417
16418   (or gnus-active-hashtb
16419       (setq gnus-active-hashtb (make-vector 4095 0)))
16420   (let ((buf (current-buffer))
16421         (already-read (> (length gnus-newsrc-alist) 1))
16422         group subscribed options-symbol newsrc Options-symbol
16423         symbol reads num1)
16424     (goto-char (point-min))
16425     ;; We intern the symbol `options' in the active hashtb so that we
16426     ;; can `eq' against it later.
16427     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16428     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16429
16430     (while (not (eobp))
16431       ;; We first read the first word on the line by narrowing and
16432       ;; then reading into `gnus-active-hashtb'.  Most groups will
16433       ;; already exist in that hashtb, so this will save some string
16434       ;; space.
16435       (narrow-to-region
16436        (point)
16437        (progn (skip-chars-forward "^ \t!:\n") (point)))
16438       (goto-char (point-min))
16439       (setq symbol
16440             (and (/= (point-min) (point-max))
16441                  (let ((obarray gnus-active-hashtb)) (read buf))))
16442       (widen)
16443       ;; Now, the symbol we have read is either `options' or a group
16444       ;; name.  If it is an options line, we just add it to a string.
16445       (cond
16446        ((or (eq symbol options-symbol)
16447             (eq symbol Options-symbol))
16448         (setq gnus-newsrc-options
16449               ;; This concating is quite inefficient, but since our
16450               ;; thorough studies show that approx 99.37% of all
16451               ;; .newsrc files only contain a single options line, we
16452               ;; don't give a damn, frankly, my dear.
16453               (concat gnus-newsrc-options
16454                       (buffer-substring
16455                        (gnus-point-at-bol)
16456                        ;; Options may continue on the next line.
16457                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16458                                 (progn (beginning-of-line) (point)))
16459                            (point)))))
16460         (forward-line -1))
16461        (symbol
16462         ;; Group names can be just numbers.  
16463         (when (numberp symbol) 
16464           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16465         (or (boundp symbol) (set symbol nil))
16466         ;; It was a group name.
16467         (setq subscribed (= (following-char) ?:)
16468               group (symbol-name symbol)
16469               reads nil)
16470         (if (eolp)
16471             ;; If the line ends here, this is clearly a buggy line, so
16472             ;; we put point a the beginning of line and let the cond
16473             ;; below do the error handling.
16474             (beginning-of-line)
16475           ;; We skip to the beginning of the ranges.
16476           (skip-chars-forward "!: \t"))
16477         ;; We are now at the beginning of the list of read articles.
16478         ;; We read them range by range.
16479         (while
16480             (cond
16481              ((looking-at "[0-9]+")
16482               ;; We narrow and read a number instead of buffer-substring/
16483               ;; string-to-int because it's faster.  narrow/widen is
16484               ;; faster than save-restriction/narrow, and save-restriction
16485               ;; produces a garbage object.
16486               (setq num1 (progn
16487                            (narrow-to-region (match-beginning 0) (match-end 0))
16488                            (read buf)))
16489               (widen)
16490               ;; If the next character is a dash, then this is a range.
16491               (if (= (following-char) ?-)
16492                   (progn
16493                     ;; We read the upper bound of the range.
16494                     (forward-char 1)
16495                     (if (not (looking-at "[0-9]+"))
16496                         ;; This is a buggy line, by we pretend that
16497                         ;; it's kinda OK.  Perhaps the user should be
16498                         ;; dinged?
16499                         (setq reads (cons num1 reads))
16500                       (setq reads
16501                             (cons
16502                              (cons num1
16503                                    (progn
16504                                      (narrow-to-region (match-beginning 0)
16505                                                        (match-end 0))
16506                                      (read buf)))
16507                              reads))
16508                       (widen)))
16509                 ;; It was just a simple number, so we add it to the
16510                 ;; list of ranges.
16511                 (setq reads (cons num1 reads)))
16512               ;; If the next char in ?\n, then we have reached the end
16513               ;; of the line and return nil.
16514               (/= (following-char) ?\n))
16515              ((= (following-char) ?\n)
16516               ;; End of line, so we end.
16517               nil)
16518              (t
16519               ;; Not numbers and not eol, so this might be a buggy
16520               ;; line...
16521               (or (eobp)
16522                   ;; If it was eob instead of ?\n, we allow it.
16523                   (progn
16524                     ;; The line was buggy.
16525                     (setq group nil)
16526                     (gnus-error 3.1 "Mangled line: %s"
16527                                 (buffer-substring (gnus-point-at-bol)
16528                                                   (gnus-point-at-eol)))))
16529               nil))
16530           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16531           ;; we allow them, because it's a common mistake to put a
16532           ;; space after the comma.
16533           (skip-chars-forward ", "))
16534
16535         ;; We have already read .newsrc.eld, so we gently update the
16536         ;; data in the hash table with the information we have just
16537         ;; read.
16538         (when group
16539           (let ((info (gnus-get-info group))
16540                 level)
16541             (if info
16542                 ;; There is an entry for this file in the alist.
16543                 (progn
16544                   (gnus-info-set-read info (nreverse reads))
16545                   ;; We update the level very gently.  In fact, we
16546                   ;; only change it if there's been a status change
16547                   ;; from subscribed to unsubscribed, or vice versa.
16548                   (setq level (gnus-info-level info))
16549                   (cond ((and (<= level gnus-level-subscribed)
16550                               (not subscribed))
16551                          (setq level (if reads
16552                                          gnus-level-default-unsubscribed
16553                                        (1+ gnus-level-default-unsubscribed))))
16554                         ((and (> level gnus-level-subscribed) subscribed)
16555                          (setq level gnus-level-default-subscribed)))
16556                   (gnus-info-set-level info level))
16557               ;; This is a new group.
16558               (setq info (list group
16559                                (if subscribed
16560                                    gnus-level-default-subscribed
16561                                  (if reads
16562                                      (1+ gnus-level-subscribed)
16563                                    gnus-level-default-unsubscribed))
16564                                (nreverse reads))))
16565             (setq newsrc (cons info newsrc))))))
16566       (forward-line 1))
16567
16568     (setq newsrc (nreverse newsrc))
16569
16570     (if (not already-read)
16571         ()
16572       ;; We now have two newsrc lists - `newsrc', which is what we
16573       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16574       ;; what we've read from .newsrc.eld.  We have to merge these
16575       ;; lists.  We do this by "attaching" any (foreign) groups in the
16576       ;; gnus-newsrc-alist to the (native) group that precedes them.
16577       (let ((rc (cdr gnus-newsrc-alist))
16578             (prev gnus-newsrc-alist)
16579             entry mentry)
16580         (while rc
16581           (or (null (nth 4 (car rc)))   ; It's a native group.
16582               (assoc (caar rc) newsrc) ; It's already in the alist.
16583               (if (setq entry (assoc (caar prev) newsrc))
16584                   (setcdr (setq mentry (memq entry newsrc))
16585                           (cons (car rc) (cdr mentry)))
16586                 (setq newsrc (cons (car rc) newsrc))))
16587           (setq prev rc
16588                 rc (cdr rc)))))
16589
16590     (setq gnus-newsrc-alist newsrc)
16591     ;; We make the newsrc hashtb.
16592     (gnus-make-hashtable-from-newsrc-alist)
16593
16594     ;; Finally, if we read some options lines, we parse them.
16595     (or (string= gnus-newsrc-options "")
16596         (gnus-newsrc-parse-options gnus-newsrc-options))))
16597
16598 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16599 ;; The return value will be a list on the form
16600 ;; ((regexp1 . ignore)
16601 ;;  (regexp2 . subscribe)...)
16602 ;; When handling new newsgroups, groups that match a `ignore' regexp
16603 ;; will be ignored, and groups that match a `subscribe' regexp will be
16604 ;; subscribed.  A line like
16605 ;; options -n !all rec.all
16606 ;; will lead to a list that looks like
16607 ;; (("^rec\\..+" . subscribe)
16608 ;;  ("^.+" . ignore))
16609 ;; So all "rec.*" groups will be subscribed, while all the other
16610 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16611 ;; different from "options -n rec.all !all".
16612 (defun gnus-newsrc-parse-options (options)
16613   (let (out eol)
16614     (save-excursion
16615       (gnus-set-work-buffer)
16616       (insert (regexp-quote options))
16617       ;; First we treat all continuation lines.
16618       (goto-char (point-min))
16619       (while (re-search-forward "\n[ \t]+" nil t)
16620         (replace-match " " t t))
16621       ;; Then we transform all "all"s into ".+"s.
16622       (goto-char (point-min))
16623       (while (re-search-forward "\\ball\\b" nil t)
16624         (replace-match ".+" t t))
16625       (goto-char (point-min))
16626       ;; We remove all other options than the "-n" ones.
16627       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16628         (replace-match " ")
16629         (forward-char -1))
16630       (goto-char (point-min))
16631
16632       ;; We are only interested in "options -n" lines - we
16633       ;; ignore the other option lines.
16634       (while (re-search-forward "[ \t]-n" nil t)
16635         (setq eol
16636               (or (save-excursion
16637                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16638                          (- (point) 2)))
16639                   (gnus-point-at-eol)))
16640         ;; Search for all "words"...
16641         (while (re-search-forward "[^ \t,\n]+" eol t)
16642           (if (= (char-after (match-beginning 0)) ?!)
16643               ;; If the word begins with a bang (!), this is a "not"
16644               ;; spec.  We put this spec (minus the bang) and the
16645               ;; symbol `ignore' into the list.
16646               (setq out (cons (cons (concat
16647                                      "^" (buffer-substring
16648                                           (1+ (match-beginning 0))
16649                                           (match-end 0)))
16650                                     'ignore) out))
16651             ;; There was no bang, so this is a "yes" spec.
16652             (setq out (cons (cons (concat "^" (match-string 0))
16653                                   'subscribe) out)))))
16654
16655       (setq gnus-newsrc-options-n out))))
16656
16657 (defun gnus-save-newsrc-file (&optional force)
16658   "Save .newsrc file."
16659   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16660   ;; from the variable gnus-newsrc-alist.
16661   (when (and (or gnus-newsrc-alist gnus-killed-list)
16662              gnus-current-startup-file)
16663     (save-excursion
16664       (if (and (or gnus-use-dribble-file gnus-slave)
16665                (not force)
16666                (or (not gnus-dribble-buffer)
16667                    (not (buffer-name gnus-dribble-buffer))
16668                    (zerop (save-excursion
16669                             (set-buffer gnus-dribble-buffer)
16670                             (buffer-size)))))
16671           (gnus-message 4 "(No changes need to be saved)")
16672         (run-hooks 'gnus-save-newsrc-hook)
16673         (if gnus-slave
16674             (gnus-slave-save-newsrc)
16675           ;; Save .newsrc.
16676           (when gnus-save-newsrc-file
16677             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16678             (gnus-gnus-to-newsrc-format)
16679             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16680           ;; Save .newsrc.eld.
16681           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16682           (make-local-variable 'version-control)
16683           (setq version-control 'never)
16684           (setq buffer-file-name
16685                 (concat gnus-current-startup-file ".eld"))
16686           (setq default-directory (file-name-directory buffer-file-name))
16687           (gnus-add-current-to-buffer-list)
16688           (buffer-disable-undo (current-buffer))
16689           (erase-buffer)
16690           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16691           (gnus-gnus-to-quick-newsrc-format)
16692           (run-hooks 'gnus-save-quick-newsrc-hook)
16693           (save-buffer)
16694           (kill-buffer (current-buffer))
16695           (gnus-message
16696            5 "Saving %s.eld...done" gnus-current-startup-file))
16697         (gnus-dribble-delete-file)
16698         (gnus-group-set-mode-line)))))
16699
16700 (defun gnus-gnus-to-quick-newsrc-format ()
16701   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16702   (insert ";; Gnus startup file.\n")
16703   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16704   (insert ";; to read .newsrc.\n")
16705   (insert "(setq gnus-newsrc-file-version "
16706           (prin1-to-string gnus-version) ")\n")
16707   (let ((variables
16708          (if gnus-save-killed-list gnus-variable-list
16709            ;; Remove the `gnus-killed-list' from the list of variables
16710            ;; to be saved, if required.
16711            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16712         ;; Peel off the "dummy" group.
16713         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16714         variable)
16715     ;; Insert the variables into the file.
16716     (while variables
16717       (when (and (boundp (setq variable (pop variables)))
16718                  (symbol-value variable))
16719         (insert "(setq " (symbol-name variable) " '")
16720         (prin1 (symbol-value variable) (current-buffer))
16721         (insert ")\n")))))
16722
16723 (defun gnus-gnus-to-newsrc-format ()
16724   ;; Generate and save the .newsrc file.
16725   (save-excursion
16726     (set-buffer (create-file-buffer gnus-current-startup-file))
16727     (let ((newsrc (cdr gnus-newsrc-alist))
16728           (standard-output (current-buffer))
16729           info ranges range method)
16730       (setq buffer-file-name gnus-current-startup-file)
16731       (setq default-directory (file-name-directory buffer-file-name))
16732       (buffer-disable-undo (current-buffer))
16733       (erase-buffer)
16734       ;; Write options.
16735       (if gnus-newsrc-options (insert gnus-newsrc-options))
16736       ;; Write subscribed and unsubscribed.
16737       (while (setq info (pop newsrc))
16738         ;; Don't write foreign groups to .newsrc.
16739         (when (or (null (setq method (gnus-info-method info)))
16740                   (equal method "native")
16741                   (gnus-server-equal method gnus-select-method))
16742           (insert (gnus-info-group info)
16743                   (if (> (gnus-info-level info) gnus-level-subscribed)
16744                       "!" ":"))
16745           (when (setq ranges (gnus-info-read info))
16746             (insert " ")
16747             (if (not (listp (cdr ranges)))
16748                 (if (= (car ranges) (cdr ranges))
16749                     (princ (car ranges))
16750                   (princ (car ranges))
16751                   (insert "-")
16752                   (princ (cdr ranges)))
16753               (while (setq range (pop ranges))
16754                 (if (or (atom range) (= (car range) (cdr range)))
16755                     (princ (or (and (atom range) range) (car range)))
16756                   (princ (car range))
16757                   (insert "-")
16758                   (princ (cdr range)))
16759                 (if ranges (insert ",")))))
16760           (insert "\n")))
16761       (make-local-variable 'version-control)
16762       (setq version-control 'never)
16763       ;; It has been reported that sometime the modtime on the .newsrc
16764       ;; file seems to be off.  We really do want to overwrite it, so
16765       ;; we clear the modtime here before saving.  It's a bit odd,
16766       ;; though...
16767       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16768       ;; delete the silly thing entirely first.  but this fails to provide
16769       ;; such niceties as .newsrc~ creation.
16770       (if gnus-modtime-botch
16771           (delete-file gnus-startup-file)
16772         (clear-visited-file-modtime))
16773       (run-hooks 'gnus-save-standard-newsrc-hook)
16774       (save-buffer)
16775       (kill-buffer (current-buffer)))))
16776
16777 \f
16778 ;;;
16779 ;;; Slave functions.
16780 ;;;
16781
16782 (defun gnus-slave-save-newsrc ()
16783   (save-excursion
16784     (set-buffer gnus-dribble-buffer)
16785     (let ((slave-name
16786            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16787       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16788
16789 (defun gnus-master-read-slave-newsrc ()
16790   (let ((slave-files
16791          (directory-files
16792           (file-name-directory gnus-current-startup-file)
16793           t (concat
16794              "^" (regexp-quote
16795                   (concat
16796                    (file-name-nondirectory gnus-current-startup-file)
16797                    "-slave-")))
16798           t))
16799         file)
16800     (if (not slave-files)
16801         ()                              ; There are no slave files to read.
16802       (gnus-message 7 "Reading slave newsrcs...")
16803       (save-excursion
16804         (set-buffer (get-buffer-create " *gnus slave*"))
16805         (buffer-disable-undo (current-buffer))
16806         (setq slave-files
16807               (sort (mapcar (lambda (file)
16808                               (list (nth 5 (file-attributes file)) file))
16809                             slave-files)
16810                     (lambda (f1 f2)
16811                       (or (< (caar f1) (caar f2))
16812                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16813         (while slave-files
16814           (erase-buffer)
16815           (setq file (nth 1 (car slave-files)))
16816           (insert-file-contents file)
16817           (if (condition-case ()
16818                   (progn
16819                     (eval-buffer (current-buffer))
16820                     t)
16821                 (error
16822                  (gnus-error 3.2 "Possible error in %s" file)
16823                  nil))
16824               (or gnus-slave ; Slaves shouldn't delete these files.
16825                   (condition-case ()
16826                       (delete-file file)
16827                     (error nil))))
16828           (setq slave-files (cdr slave-files))))
16829       (gnus-message 7 "Reading slave newsrcs...done"))))
16830
16831 \f
16832 ;;;
16833 ;;; Group description.
16834 ;;;
16835
16836 (defun gnus-read-all-descriptions-files ()
16837   (let ((methods (cons gnus-select-method 
16838                        (nconc
16839                         (when gnus-message-archive-method
16840                           (list "archive"))
16841                         gnus-secondary-select-methods))))
16842     (while methods
16843       (gnus-read-descriptions-file (car methods))
16844       (setq methods (cdr methods)))
16845     t))
16846
16847 (defun gnus-read-descriptions-file (&optional method)
16848   (let ((method (or method gnus-select-method)))
16849     (when (stringp method)
16850       (setq method (gnus-server-to-method method)))
16851     ;; We create the hashtable whether we manage to read the desc file
16852     ;; to avoid trying to re-read after a failed read.
16853     (or gnus-description-hashtb
16854         (setq gnus-description-hashtb
16855               (gnus-make-hashtable (length gnus-active-hashtb))))
16856     ;; Mark this method's desc file as read.
16857     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16858                   gnus-description-hashtb)
16859
16860     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16861     (cond
16862      ((not (gnus-check-server method))
16863       (gnus-message 1 "Couldn't open server")
16864       nil)
16865      ((not (gnus-request-list-newsgroups method))
16866       (gnus-message 1 "Couldn't read newsgroups descriptions")
16867       nil)
16868      (t
16869       (let (group)
16870         (save-excursion
16871           (save-restriction
16872             (set-buffer nntp-server-buffer)
16873             (goto-char (point-min))
16874             (if (or (search-forward "\n.\n" nil t)
16875                     (goto-char (point-max)))
16876                 (progn
16877                   (beginning-of-line)
16878                   (narrow-to-region (point-min) (point))))
16879             (goto-char (point-min))
16880             (while (not (eobp))
16881               ;; If we get an error, we set group to 0, which is not a
16882               ;; symbol...
16883               (setq group
16884                     (condition-case ()
16885                         (let ((obarray gnus-description-hashtb))
16886                           ;; Group is set to a symbol interned in this
16887                           ;; hash table.
16888                           (read nntp-server-buffer))
16889                       (error 0)))
16890               (skip-chars-forward " \t")
16891               ;; ...  which leads to this line being effectively ignored.
16892               (and (symbolp group)
16893                    (set group (buffer-substring
16894                                (point) (progn (end-of-line) (point)))))
16895               (forward-line 1))))
16896         (gnus-message 5 "Reading descriptions file...done")
16897         t)))))
16898
16899 (defun gnus-group-get-description (group)
16900   "Get the description of a group by sending XGTITLE to the server."
16901   (when (gnus-request-group-description group)
16902     (save-excursion
16903       (set-buffer nntp-server-buffer)
16904       (goto-char (point-min))
16905       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16906         (match-string 1)))))
16907
16908 \f
16909 ;;;
16910 ;;; Buffering of read articles.
16911 ;;;
16912
16913 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16914 (defvar gnus-backlog-articles nil)
16915 (defvar gnus-backlog-hashtb nil)
16916
16917 (defun gnus-backlog-buffer ()
16918   "Return the backlog buffer."
16919   (or (get-buffer gnus-backlog-buffer)
16920       (save-excursion
16921         (set-buffer (get-buffer-create gnus-backlog-buffer))
16922         (buffer-disable-undo (current-buffer))
16923         (setq buffer-read-only t)
16924         (gnus-add-current-to-buffer-list)
16925         (get-buffer gnus-backlog-buffer))))
16926
16927 (defun gnus-backlog-setup ()
16928   "Initialize backlog variables."
16929   (unless gnus-backlog-hashtb
16930     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16931
16932 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16933
16934 (defun gnus-backlog-shutdown ()
16935   "Clear all backlog variables and buffers."
16936   (when (get-buffer gnus-backlog-buffer)
16937     (kill-buffer gnus-backlog-buffer))
16938   (setq gnus-backlog-hashtb nil
16939         gnus-backlog-articles nil))
16940
16941 (defun gnus-backlog-enter-article (group number buffer)
16942   (gnus-backlog-setup)
16943   (let ((ident (intern (concat group ":" (int-to-string number))
16944                        gnus-backlog-hashtb))
16945         b)
16946     (if (memq ident gnus-backlog-articles)
16947         () ; It's already kept.
16948       ;; Remove the oldest article, if necessary.
16949       (and (numberp gnus-keep-backlog)
16950            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16951            (gnus-backlog-remove-oldest-article))
16952       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16953       ;; Insert the new article.
16954       (save-excursion
16955         (set-buffer (gnus-backlog-buffer))
16956         (let (buffer-read-only)
16957           (goto-char (point-max))
16958           (or (bolp) (insert "\n"))
16959           (setq b (point))
16960           (insert-buffer-substring buffer)
16961           ;; Tag the beginning of the article with the ident.
16962           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
16963
16964 (defun gnus-backlog-remove-oldest-article ()
16965   (save-excursion
16966     (set-buffer (gnus-backlog-buffer))
16967     (goto-char (point-min))
16968     (if (zerop (buffer-size))
16969         () ; The buffer is empty.
16970       (let ((ident (get-text-property (point) 'gnus-backlog))
16971             buffer-read-only)
16972         ;; Remove the ident from the list of articles.
16973         (when ident
16974           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16975         ;; Delete the article itself.
16976         (delete-region
16977          (point) (next-single-property-change
16978                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16979
16980 (defun gnus-backlog-remove-article (group number)
16981   "Remove article NUMBER in GROUP from the backlog."
16982   (when (numberp number)
16983     (gnus-backlog-setup)
16984     (let ((ident (intern (concat group ":" (int-to-string number))
16985                          gnus-backlog-hashtb))
16986           beg end)
16987       (when (memq ident gnus-backlog-articles)
16988         ;; It was in the backlog.
16989         (save-excursion
16990           (set-buffer (gnus-backlog-buffer))
16991           (let (buffer-read-only)
16992             (when (setq beg (text-property-any
16993                              (point-min) (point-max) 'gnus-backlog
16994                              ident))
16995               ;; Find the end (i. e., the beginning of the next article).
16996               (setq end
16997                     (next-single-property-change
16998                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16999               (delete-region beg end)
17000               ;; Return success.
17001               t)))))))
17002
17003 (defun gnus-backlog-request-article (group number buffer)
17004   (when (numberp number)
17005     (gnus-backlog-setup)
17006     (let ((ident (intern (concat group ":" (int-to-string number))
17007                          gnus-backlog-hashtb))
17008           beg end)
17009       (when (memq ident gnus-backlog-articles)
17010         ;; It was in the backlog.
17011         (save-excursion
17012           (set-buffer (gnus-backlog-buffer))
17013           (if (not (setq beg (text-property-any
17014                               (point-min) (point-max) 'gnus-backlog
17015                               ident)))
17016               ;; It wasn't in the backlog after all.
17017               (ignore
17018                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17019             ;; Find the end (i. e., the beginning of the next article).
17020             (setq end
17021                   (next-single-property-change
17022                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17023         (let ((buffer-read-only nil))
17024           (erase-buffer)
17025           (insert-buffer-substring gnus-backlog-buffer beg end)
17026           t)))))
17027
17028 ;; Allow redefinition of Gnus functions.
17029
17030 (gnus-ems-redefine)
17031
17032 (provide 'gnus)
17033
17034 ;;; gnus.el ends here