*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35
36 (eval-when-compile (require 'cl))
37
38 ;; Site dependent variables.  These variables should be defined in
39 ;; paths.el.
40
41 (defvar gnus-default-nntp-server nil
42   "Specify a default NNTP server.
43 This variable should be defined in paths.el, and should never be set
44 by the user.
45 If you want to change servers, you should use `gnus-select-method'.
46 See the documentation to that variable.")
47
48 (defvar gnus-backup-default-subscribed-newsgroups
49   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
50   "Default default new newsgroups the first time Gnus is run.
51 Should be set in paths.el, and shouldn't be touched by the user.")
52
53 (defvar gnus-local-domain nil
54   "Local domain name without a host name.
55 The DOMAINNAME environment variable is used instead if it is defined.
56 If the `system-name' function returns the full Internet name, there is
57 no need to set this variable.")
58
59 (defvar gnus-local-organization nil
60   "String with a description of what organization (if any) the user belongs to.
61 The ORGANIZATION environment variable is used instead if it is defined.
62 If this variable contains a function, this function will be called
63 with the current newsgroup name as the argument.  The function should
64 return a string.
65
66 In any case, if the string (either in the variable, in the environment
67 variable, or returned by the function) is a file name, the contents of
68 this file will be used as the organization.")
69
70 ;; Customization variables
71
72 ;; Don't touch this variable.
73 (defvar gnus-nntp-service "nntp"
74   "*NNTP service name (\"nntp\" or 119).
75 This is an obsolete variable, which is scarcely used.  If you use an
76 nntp server for your newsgroup and want to change the port number
77 used to 899, you would say something along these lines:
78
79  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
80
81 (defvar gnus-nntpserver-file "/etc/nntpserver"
82   "*A file with only the name of the nntp server in it.")
83
84 ;; This function is used to check both the environment variable
85 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
86 ;; an nntp server name default.
87 (defun gnus-getenv-nntpserver ()
88   (or (getenv "NNTPSERVER")
89       (and (file-readable-p gnus-nntpserver-file)
90            (save-excursion
91              (set-buffer (get-buffer-create " *gnus nntp*"))
92              (buffer-disable-undo (current-buffer))
93              (insert-file-contents gnus-nntpserver-file)
94              (let ((name (buffer-string)))
95                (prog1
96                    (if (string-match "^[ \t\n]*$" name)
97                        nil
98                      name)
99                  (kill-buffer (current-buffer))))))))
100
101 (defvar gnus-select-method
102   (nconc
103    (list 'nntp (or (condition-case ()
104                        (gnus-getenv-nntpserver)
105                      (error nil))
106                    (if (and gnus-default-nntp-server
107                             (not (string= gnus-default-nntp-server "")))
108                        gnus-default-nntp-server)
109                    (system-name)))
110    (if (or (null gnus-nntp-service)
111            (equal gnus-nntp-service "nntp"))
112        nil
113      (list gnus-nntp-service)))
114   "*Default method for selecting a newsgroup.
115 This variable should be a list, where the first element is how the
116 news is to be fetched, the second is the address.
117
118 For instance, if you want to get your news via NNTP from
119 \"flab.flab.edu\", you could say:
120
121 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
122
123 If you want to use your local spool, say:
124
125 (setq gnus-select-method (list 'nnspool (system-name)))
126
127 If you use this variable, you must set `gnus-nntp-server' to nil.
128
129 There is a lot more to know about select methods and virtual servers -
130 see the manual for details.")
131
132 (defvar gnus-message-archive-method 
133   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
134              (nnfolder-active-file "~/Mail/archive/active")
135              (nnfolder-get-new-mail nil)
136              (nnfolder-inhibit-expiry t))
137   "*Method used for archiving messages you've sent.
138 This should be a mail method.")
139
140 (defvar gnus-refer-article-method nil
141   "*Preferred method for fetching an article by Message-ID.
142 If you are reading news from the local spool (with nnspool), fetching
143 articles by Message-ID is painfully slow.  By setting this method to an
144 nntp method, you might get acceptable results.
145
146 The value of this variable must be a valid select method as discussed
147 in the documentation of `gnus-select-method'.")
148
149 (defvar gnus-secondary-select-methods nil
150   "*A list of secondary methods that will be used for reading news.
151 This is a list where each element is a complete select method (see
152 `gnus-select-method').
153
154 If, for instance, you want to read your mail with the nnml backend,
155 you could set this variable:
156
157 (setq gnus-secondary-select-methods '((nnml \"\")))")
158
159 (defvar gnus-secondary-servers nil
160   "*List of NNTP servers that the user can choose between interactively.
161 To make Gnus query you for a server, you have to give `gnus' a
162 non-numeric prefix - `C-u M-x gnus', in short.")
163
164 (defvar gnus-nntp-server nil
165   "*The name of the host running the NNTP server.
166 This variable is semi-obsolete.  Use the `gnus-select-method'
167 variable instead.")
168
169 (defvar gnus-startup-file "~/.newsrc"
170   "*Your `.newsrc' file.
171 `.newsrc-SERVER' will be used instead if that exists.")
172
173 (defvar gnus-init-file "~/.gnus"
174   "*Your Gnus elisp startup file.
175 If a file with the .el or .elc suffixes exist, it will be read
176 instead.")
177
178 (defvar gnus-group-faq-directory
179   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
180     "/ftp@sunsite.auc.dk:/pub/usenet/"
181     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
182     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
183     "/ftp@rtfm.mit.edu:/pub/usenet/"
184     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
185     "/ftp@ftp.sunet.se:/pub/usenet/"
186     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
187     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
188     "/ftp@ftp.hk.super.net:/mirror/faqs/")
189   "*Directory where the group FAQs are stored.
190 This will most commonly be on a remote machine, and the file will be
191 fetched by ange-ftp.
192
193 This variable can also be a list of directories.  In that case, the
194 first element in the list will be used by default, and the others will
195 be used as backup sites.
196
197 Note that Gnus uses an aol machine as the default directory.  If this
198 feels fundamentally unclean, just think of it as a way to finally get
199 something of value back from them.
200
201 If the default site is too slow, try one of these:
202
203    North America: mirrors.aol.com                /pub/rtfm/usenet
204                   ftp.seas.gwu.edu               /pub/rtfm
205                   rtfm.mit.edu                   /pub/usenet
206    Europe:        ftp.uni-paderborn.de           /pub/FAQ
207                   src.doc.ic.ac.uk               /usenet/news-FAQS
208                   ftp.sunet.se                   /pub/usenet
209                   sunsite.auc.dk                 /pub/usenet
210    Asia:          nctuccca.edu.tw                /USENET/FAQ
211                   hwarang.postech.ac.kr          /pub/usenet
212                   ftp.hk.super.net               /mirror/faqs")
213
214 (defvar gnus-group-archive-directory
215   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
216   "*The address of the (ding) archives.")
217
218 (defvar gnus-group-recent-archive-directory
219   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
220   "*The address of the most recent (ding) articles.")
221
222 (defvar gnus-default-subscribed-newsgroups nil
223   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
224 It should be a list of strings.
225 If it is `t', Gnus will not do anything special the first time it is
226 started; it'll just use the normal newsgroups subscription methods.")
227
228 (defvar gnus-use-cross-reference t
229   "*Non-nil means that cross referenced articles will be marked as read.
230 If nil, ignore cross references.  If t, mark articles as read in
231 subscribed newsgroups.  If neither t nor nil, mark as read in all
232 newsgroups.")
233
234 (defvar gnus-single-article-buffer t
235   "*If non-nil, display all articles in the same buffer.
236 If nil, each group will get its own article buffer.")
237
238 (defvar gnus-use-dribble-file t
239   "*Non-nil means that Gnus will use a dribble file to store user updates.
240 If Emacs should crash without saving the .newsrc files, complete
241 information can be restored from the dribble file.")
242
243 (defvar gnus-dribble-directory nil
244   "*The directory where dribble files will be saved.
245 If this variable is nil, the directory where the .newsrc files are
246 saved will be used.")
247
248 (defvar gnus-asynchronous nil
249   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
250
251 (defvar gnus-kill-summary-on-exit t
252   "*If non-nil, kill the summary buffer when you exit from it.
253 If nil, the summary will become a \"*Dead Summary*\" buffer, and
254 it will be killed sometime later.")
255
256 (defvar gnus-large-newsgroup 200
257   "*The number of articles which indicates a large newsgroup.
258 If the number of articles in a newsgroup is greater than this value,
259 confirmation is required for selecting the newsgroup.")
260
261 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
262 (defvar gnus-no-groups-message "No news is horrible news"
263   "*Message displayed by Gnus when no groups are available.")
264
265 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
266   "*Non-nil means that the default name of a file to save articles in is the group name.
267 If it's nil, the directory form of the group name is used instead.
268
269 If this variable is a list, and the list contains the element
270 `not-score', long file names will not be used for score files; if it
271 contains the element `not-save', long file names will not be used for
272 saving; and if it contains the element `not-kill', long file names
273 will not be used for kill files.")
274
275 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
276   "*Name of the directory articles will be saved in (default \"~/News\").
277 Initialized from the SAVEDIR environment variable.")
278
279 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
280   "*Name of the directory where kill files will be stored (default \"~/News\").
281 Initialized from the SAVEDIR environment variable.")
282
283 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
284   "*A function to save articles in your favorite format.
285 The function must be interactively callable (in other words, it must
286 be an Emacs command).
287
288 Gnus provides the following functions:
289
290 * gnus-summary-save-in-rmail (Rmail format)
291 * gnus-summary-save-in-mail (Unix mail format)
292 * gnus-summary-save-in-folder (MH folder)
293 * gnus-summary-save-in-file (article format).
294 * gnus-summary-save-in-vm (use VM's folder format).")
295
296 (defvar gnus-prompt-before-saving 'always
297   "*This variable says how much prompting is to be done when saving articles.
298 If it is nil, no prompting will be done, and the articles will be
299 saved to the default files.  If this variable is `always', each and
300 every article that is saved will be preceded by a prompt, even when
301 saving large batches of articles.  If this variable is neither nil not
302 `always', there the user will be prompted once for a file name for
303 each invocation of the saving commands.")
304
305 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
306   "*A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
308
309 (defvar gnus-mail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Unix mail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-folder-save-name (function gnus-folder-save-name)
314   "*A function generating a file name to save articles in MH folder.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
316
317 (defvar gnus-file-save-name (function gnus-numeric-save-name)
318   "*A function generating a file name to save articles in article format.
319 The function is called with NEWSGROUP, HEADERS, and optional
320 LAST-FILE.")
321
322 (defvar gnus-split-methods
323   '((gnus-article-archive-name))
324   "*Variable used to suggest where articles are to be saved.
325 For instance, if you would like to save articles related to Gnus in
326 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
327 you could set this variable to something like:
328
329  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
330    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
331
332 This variable is an alist where the where the key is the match and the
333 value is a list of possible files to save in if the match is non-nil.
334
335 If the match is a string, it is used as a regexp match on the
336 article.  If the match is a symbol, that symbol will be funcalled
337 from the buffer of the article to be saved with the newsgroup as the
338 parameter.  If it is a list, it will be evaled in the same buffer.
339
340 If this form or function returns a string, this string will be used as
341 a possible file name; and if it returns a non-nil list, that list will
342 be used as possible file names.")
343
344 (defvar gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable.")
347
348 (defvar gnus-save-score nil
349   "*If non-nil, save group scoring info.")
350
351 (defvar gnus-use-adaptive-scoring nil
352   "*If non-nil, use some adaptive scoring scheme.")
353
354 (defvar gnus-use-cache nil
355   "*If nil, Gnus will ignore the article cache.
356 If `passive', it will allow entering (and reading) articles
357 explicitly entered into the cache.  If anything else, use the
358 cache to the full extent of the law.")
359
360 (defvar gnus-use-trees nil
361   "*If non-nil, display a thread tree buffer.")
362
363 (defvar gnus-use-grouplens nil
364   "*If non-nil, use GroupLens ratings.")
365
366 (defvar gnus-keep-backlog nil
367   "*If non-nil, Gnus will keep read articles for later re-retrieval.
368 If it is a number N, then Gnus will only keep the last N articles
369 read.  If it is neither nil nor a number, Gnus will keep all read
370 articles.  This is not a good idea.")
371
372 (defvar gnus-use-nocem nil
373   "*If non-nil, Gnus will read NoCeM cancel messages.")
374
375 (defvar gnus-use-demon nil
376   "If non-nil, Gnus might use some demons.")
377
378 (defvar gnus-use-scoring t
379   "*If non-nil, enable scoring.")
380
381 (defvar gnus-use-picons nil
382   "*If non-nil, display picons.")
383
384 (defvar gnus-fetch-old-headers nil
385   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
386 If an unread article in the group refers to an older, already read (or
387 just marked as read) article, the old article will not normally be
388 displayed in the Summary buffer.  If this variable is non-nil, Gnus
389 will attempt to grab the headers to the old articles, and thereby
390 build complete threads.  If it has the value `some', only enough
391 headers to connect otherwise loose threads will be displayed.
392 This variable can also be a number.  In that case, no more than that
393 number of old headers will be fetched.
394
395 The server has to support NOV for any of this to work.")
396
397 ;see gnus-cus.el
398 ;(defvar gnus-visual t
399 ;  "*If non-nil, will do various highlighting.
400 ;If nil, no mouse highlights (or any other highlights) will be
401 ;performed.  This might speed up Gnus some when generating large group
402 ;and summary buffers.")
403
404 (defvar gnus-novice-user t
405   "*Non-nil means that you are a usenet novice.
406 If non-nil, verbose messages may be displayed and confirmations may be
407 required.")
408
409 (defvar gnus-expert-user nil
410   "*Non-nil means that you will never be asked for confirmation about anything.
411 And that means *anything*.")
412
413 (defvar gnus-verbose 7
414   "*Integer that says how verbose Gnus should be.
415 The higher the number, the more messages Gnus will flash to say what
416 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
417 display most important messages; and at ten, Gnus will keep on
418 jabbering all the time.")
419
420 (defvar gnus-keep-same-level nil
421   "*Non-nil means that the next newsgroup after the current will be on the same level.
422 When you type, for instance, `n' after reading the last article in the
423 current newsgroup, you will go to the next newsgroup.  If this variable
424 is nil, the next newsgroup will be the next from the group
425 buffer.
426 If this variable is non-nil, Gnus will either put you in the
427 next newsgroup with the same level, or, if no such newsgroup is
428 available, the next newsgroup with the lowest possible level higher
429 than the current level.
430 If this variable is `best', Gnus will make the next newsgroup the one
431 with the best level.")
432
433 (defvar gnus-summary-make-false-root 'adopt
434   "*nil means that Gnus won't gather loose threads.
435 If the root of a thread has expired or been read in a previous
436 session, the information necessary to build a complete thread has been
437 lost.  Instead of having many small sub-threads from this original thread
438 scattered all over the summary buffer, Gnus can gather them.
439
440 If non-nil, Gnus will try to gather all loose sub-threads from an
441 original thread into one large thread.
442
443 If this variable is non-nil, it should be one of `none', `adopt',
444 `dummy' or `empty'.
445
446 If this variable is `none', Gnus will not make a false root, but just
447 present the sub-threads after another.
448 If this variable is `dummy', Gnus will create a dummy root that will
449 have all the sub-threads as children.
450 If this variable is `adopt', Gnus will make one of the \"children\"
451 the parent and mark all the step-children as such.
452 If this variable is `empty', the \"children\" are printed with empty
453 subject fields.  (Or rather, they will be printed with a string
454 given by the `gnus-summary-same-subject' variable.)")
455
456 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
457   "*A regexp to match subjects to be excluded from loose thread gathering.
458 As loose thread gathering is done on subjects only, that means that
459 there can be many false gatherings performed.  By rooting out certain
460 common subjects, gathering might become saner.")
461
462 (defvar gnus-summary-gather-subject-limit nil
463   "*Maximum length of subject comparisons when gathering loose threads.
464 Use nil to compare full subjects.  Setting this variable to a low
465 number will help gather threads that have been corrupted by
466 newsreaders chopping off subject lines, but it might also mean that
467 unrelated articles that have subject that happen to begin with the
468 same few characters will be incorrectly gathered.
469
470 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
471 comparing subjects.")
472
473 (defvar gnus-simplify-ignored-prefixes nil
474   "*Regexp, matches for which are removed from subject lines when simplifying.")
475
476 (defvar gnus-build-sparse-threads nil
477   "*If non-nil, fill in the gaps in threads.
478 If `some', only fill in the gaps that are needed to tie loose threads
479 together.  If `more', fill in all leaf nodes that Gnus can find.  If
480 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
481
482 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
483   "Function used for gathering loose threads.
484 There are two pre-defined functions: `gnus-gather-threads-by-subject',
485 which only takes Subjects into consideration; and
486 `gnus-gather-threads-by-references', which compared the References
487 headers of the articles to find matches.")
488
489 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
490 (defvar gnus-summary-same-subject ""
491   "*String indicating that the current article has the same subject as the previous.
492 This variable will only be used if the value of
493 `gnus-summary-make-false-root' is `empty'.")
494
495 (defvar gnus-summary-goto-unread t
496   "*If non-nil, marking commands will go to the next unread article.
497 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
498 whether it is read or not.")
499
500 (defvar gnus-group-goto-unread t
501   "*If non-nil, movement commands will go to the next unread and subscribed group.")
502
503 (defvar gnus-goto-next-group-when-activating t
504   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
505
506 (defvar gnus-check-new-newsgroups t
507   "*Non-nil means that Gnus will add new newsgroups at startup.
508 If this variable is `ask-server', Gnus will ask the server for new
509 groups since the last time it checked.  This means that the killed list
510 is no longer necessary, so you could set `gnus-save-killed-list' to
511 nil.
512
513 A variant is to have this variable be a list of select methods.  Gnus
514 will then use the `ask-server' method on all these select methods to
515 query for new groups from all those servers.
516
517 Eg.
518   (setq gnus-check-new-newsgroups
519         '((nntp \"some.server\") (nntp \"other.server\")))
520
521 If this variable is nil, then you have to tell Gnus explicitly to
522 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
523
524 (defvar gnus-check-bogus-newsgroups nil
525   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
528
529 (defvar gnus-read-active-file t
530   "*Non-nil means that Gnus will read the entire active file at startup.
531 If this variable is nil, Gnus will only know about the groups in your
532 `.newsrc' file.
533
534 If this variable is `some', Gnus will try to only read the relevant
535 parts of the active file from the server.  Not all servers support
536 this, and it might be quite slow with other servers, but this should
537 generally be faster than both the t and nil value.
538
539 If you set this variable to nil or `some', you probably still want to
540 be told about new newsgroups that arrive.  To do that, set
541 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
542 properly with all servers.")
543
544 (defvar gnus-level-subscribed 5
545   "*Groups with levels less than or equal to this variable are subscribed.")
546
547 (defvar gnus-level-unsubscribed 7
548   "*Groups with levels less than or equal to this variable are unsubscribed.
549 Groups with levels less than `gnus-level-subscribed', which should be
550 less than this variable, are subscribed.")
551
552 (defvar gnus-level-zombie 8
553   "*Groups with this level are zombie groups.")
554
555 (defvar gnus-level-killed 9
556   "*Groups with this level are killed.")
557
558 (defvar gnus-level-default-subscribed 3
559   "*New subscribed groups will be subscribed at this level.")
560
561 (defvar gnus-level-default-unsubscribed 6
562   "*New unsubscribed groups will be unsubscribed at this level.")
563
564 (defvar gnus-activate-level (1+ gnus-level-subscribed)
565   "*Groups higher than this level won't be activated on startup.
566 Setting this variable to something log might save lots of time when
567 you have many groups that you aren't interested in.")
568
569 (defvar gnus-activate-foreign-newsgroups 4
570   "*If nil, Gnus will not check foreign newsgroups at startup.
571 If it is non-nil, it should be a number between one and nine.  Foreign
572 newsgroups that have a level lower or equal to this number will be
573 activated on startup.  For instance, if you want to active all
574 subscribed newsgroups, but not the rest, you'd set this variable to
575 `gnus-level-subscribed'.
576
577 If you subscribe to lots of newsgroups from different servers, startup
578 might take a while.  By setting this variable to nil, you'll save time,
579 but you won't be told how many unread articles there are in the
580 groups.")
581
582 (defvar gnus-save-newsrc-file t
583   "*Non-nil means that Gnus will save the `.newsrc' file.
584 Gnus always saves its own startup file, which is called
585 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
586 be readily understood by other newsreaders.  If you don't plan on
587 using other newsreaders, set this variable to nil to save some time on
588 exit.")
589
590 (defvar gnus-save-killed-list t
591   "*If non-nil, save the list of killed groups to the startup file.
592 If you set this variable to nil, you'll save both time (when starting
593 and quitting) and space (both memory and disk), but it will also mean
594 that Gnus has no record of which groups are new and which are old, so
595 the automatic new newsgroups subscription methods become meaningless.
596
597 You should always set `gnus-check-new-newsgroups' to `ask-server' or
598 nil if you set this variable to nil.")
599
600 (defvar gnus-interactive-catchup t
601   "*If non-nil, require your confirmation when catching up a group.")
602
603 (defvar gnus-interactive-post t
604   "*If non-nil, group name will be asked for when posting.")
605
606 (defvar gnus-interactive-exit t
607   "*If non-nil, require your confirmation when exiting Gnus.")
608
609 (defvar gnus-kill-killed t
610   "*If non-nil, Gnus will apply kill files to already killed articles.
611 If it is nil, Gnus will never apply kill files to articles that have
612 already been through the scoring process, which might very well save lots
613 of time.")
614
615 (defvar gnus-extract-address-components 'gnus-extract-address-components
616   "*Function for extracting address components from a From header.
617 Two pre-defined function exist: `gnus-extract-address-components',
618 which is the default, quite fast, and too simplistic solution, and
619 `mail-extract-address-components', which works much better, but is
620 slower.")
621
622 (defvar gnus-summary-default-score 0
623   "*Default article score level.
624 If this variable is nil, scoring will be disabled.")
625
626 (defvar gnus-summary-zcore-fuzz 0
627   "*Fuzziness factor for the zcore in the summary buffer.
628 Articles with scores closer than this to `gnus-summary-default-score'
629 will not be marked.")
630
631 (defvar gnus-simplify-subject-fuzzy-regexp nil
632   "*Strings to be removed when doing fuzzy matches.
633 This can either be a regular expression or list of regular expressions
634 that will be removed from subject strings if fuzzy subject
635 simplification is selected.")
636
637 (defvar gnus-permanently-visible-groups nil
638   "*Regexp to match groups that should always be listed in the group buffer.
639 This means that they will still be listed when there are no unread
640 articles in the groups.")
641
642 (defvar gnus-list-groups-with-ticked-articles t
643   "*If non-nil, list groups that have only ticked articles.
644 If nil, only list groups that have unread articles.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level.
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-group-list-inactive-groups t
654   "*If non-nil, inactive groups will be listed.")
655
656 (defvar gnus-show-mime nil
657   "*If non-nil, do mime processing of articles.
658 The articles will simply be fed to the function given by
659 `gnus-show-mime-method'.")
660
661 (defvar gnus-strict-mime t
662   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
663
664 (defvar gnus-show-mime-method 'metamail-buffer
665   "*Function to process a MIME message.
666 The function is called from the article buffer.")
667
668 (defvar gnus-decode-encoded-word-method (lambda ())
669   "*Function to decode a MIME encoded-words.
670 The function is called from the article buffer.")
671
672 (defvar gnus-show-threads t
673   "*If non-nil, display threads in summary mode.")
674
675 (defvar gnus-thread-hide-subtree nil
676   "*If non-nil, hide all threads initially.
677 If threads are hidden, you have to run the command
678 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
679 to expose hidden threads.")
680
681 (defvar gnus-thread-hide-killed t
682   "*If non-nil, hide killed threads automatically.")
683
684 (defvar gnus-thread-ignore-subject nil
685   "*If non-nil, ignore subjects and do all threading based on the Reference header.
686 If nil, which is the default, articles that have different subjects
687 from their parents will start separate threads.")
688
689 (defvar gnus-thread-operation-ignore-subject t
690   "*If non-nil, subjects will be ignored when doing thread commands.
691 This affects commands like `gnus-summary-kill-thread' and
692 `gnus-summary-lower-thread'.
693
694 If this variable is nil, articles in the same thread with different
695 subjects will not be included in the operation in question.  If this
696 variable is `fuzzy', only articles that have subjects that are fuzzily
697 equal will be included.")
698
699 (defvar gnus-thread-indent-level 4
700   "*Number that says how much each sub-thread should be indented.")
701
702 (defvar gnus-ignored-newsgroups
703   (purecopy (mapconcat 'identity
704                        '("^to\\."       ; not "real" groups
705                          "^[0-9. \t]+ " ; all digits in name
706                          "[][\"#'()]"   ; bogus characters
707                          )
708                        "\\|"))
709   "*A regexp to match uninteresting newsgroups in the active file.
710 Any lines in the active file matching this regular expression are
711 removed from the newsgroup list before anything else is done to it,
712 thus making them effectively non-existent.")
713
714 (defvar gnus-ignored-headers
715   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
716   "*All headers that match this regexp will be hidden.
717 This variable can also be a list of regexps of headers to be ignored.
718 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
719
720 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
721   "*All headers that do not match this regexp will be hidden.
722 This variable can also be a list of regexp of headers to remain visible.
723 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
724
725 (defvar gnus-sorted-header-list
726   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
727     "^Cc:" "^Date:" "^Organization:")
728   "*This variable is a list of regular expressions.
729 If it is non-nil, headers that match the regular expressions will
730 be placed first in the article buffer in the sequence specified by
731 this list.")
732
733 (defvar gnus-boring-article-headers
734   '(empty followup-to reply-to)
735   "*Headers that are only to be displayed if they have interesting data.
736 Possible values in this list are `empty', `newsgroups', `followup-to',
737 `reply-to', and `date'.")
738
739 (defvar gnus-show-all-headers nil
740   "*If non-nil, don't hide any headers.")
741
742 (defvar gnus-save-all-headers t
743   "*If non-nil, don't remove any headers before saving.")
744
745 (defvar gnus-saved-headers gnus-visible-headers
746   "*Headers to keep if `gnus-save-all-headers' is nil.
747 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
748 If that variable is nil, however, all headers that match this regexp
749 will be kept while the rest will be deleted before saving.")
750
751 (defvar gnus-inhibit-startup-message nil
752   "*If non-nil, the startup message will not be displayed.")
753
754 (defvar gnus-signature-separator "^-- *$"
755   "Regexp matching signature separator.")
756
757 (defvar gnus-signature-limit nil
758   "Provide a limit to what is considered a signature.
759 If it is a number, no signature may not be longer (in characters) than
760 that number.  If it is a function, the function will be called without
761 any parameters, and if it returns nil, there is no signature in the
762 buffer.  If it is a string, it will be used as a regexp.  If it
763 matches, the text in question is not a signature.")
764
765 (defvar gnus-auto-extend-newsgroup t
766   "*If non-nil, extend newsgroup forward and backward when requested.")
767
768 (defvar gnus-auto-select-first t
769   "*If nil, don't select the first unread article when entering a group.
770 If this variable is `best', select the highest-scored unread article
771 in the group.  If neither nil nor `best', select the first unread
772 article.
773
774 If you want to prevent automatic selection of the first unread article
775 in some newsgroups, set the variable to nil in
776 `gnus-select-group-hook'.")
777
778 (defvar gnus-auto-select-next t
779   "*If non-nil, offer to go to the next group from the end of the previous.
780 If the value is t and the next newsgroup is empty, Gnus will exit
781 summary mode and go back to group mode.  If the value is neither nil
782 nor t, Gnus will select the following unread newsgroup.  In
783 particular, if the value is the symbol `quietly', the next unread
784 newsgroup will be selected without any confirmation, and if it is
785 `almost-quietly', the next group will be selected without any
786 confirmation if you are located on the last article in the group.
787 Finally, if this variable is `slightly-quietly', the `Z n' command
788 will go to the next group without confirmation.")
789
790 (defvar gnus-auto-select-same nil
791   "*If non-nil, select the next article with the same subject.")
792
793 (defvar gnus-summary-check-current nil
794   "*If non-nil, consider the current article when moving.
795 The \"unread\" movement commands will stay on the same line if the
796 current article is unread.")
797
798 (defvar gnus-auto-center-summary t
799   "*If non-nil, always center the current summary buffer.
800 In particular, if `vertical' do only vertical recentering.  If non-nil
801 and non-`vertical', do both horizontal and vertical recentering.")
802
803 (defvar gnus-break-pages t
804   "*If non-nil, do page breaking on articles.
805 The page delimiter is specified by the `gnus-page-delimiter'
806 variable.")
807
808 (defvar gnus-page-delimiter "^\^L"
809   "*Regexp describing what to use as article page delimiters.
810 The default value is \"^\^L\", which is a form linefeed at the
811 beginning of a line.")
812
813 (defvar gnus-use-full-window t
814   "*If non-nil, use the entire Emacs screen.")
815
816 (defvar gnus-window-configuration nil
817   "Obsolete variable.  See `gnus-buffer-configuration'.")
818
819 (defvar gnus-window-min-width 2
820   "*Minimum width of Gnus buffers.")
821
822 (defvar gnus-window-min-height 1
823   "*Minimum height of Gnus buffers.")
824
825 (defvar gnus-buffer-configuration
826   '((group
827      (vertical 1.0
828                (group 1.0 point)
829                (if gnus-carpal '(group-carpal 4))))
830     (summary
831      (vertical 1.0
832                (summary 1.0 point)
833                (if gnus-carpal '(summary-carpal 4))))
834     (article
835      (cond 
836       (gnus-use-picons
837        '(frame 1.0
838                (vertical 1.0
839                          (summary 0.25 point)
840                          (if gnus-carpal '(summary-carpal 4))
841                          (article 1.0))
842                (vertical ((height . 5) (width . 15)
843                           (user-position . t)
844                           (left . -1) (top . 1))
845                          (picons 1.0))))
846       (gnus-use-trees
847        '(vertical 1.0
848                   (summary 0.25 point)
849                   (tree 0.25)
850                   (article 1.0)))
851       (t
852        '(vertical 1.0
853                  (summary 0.25 point)
854                  (if gnus-carpal '(summary-carpal 4))
855                  (if gnus-use-trees '(tree 0.25))
856                  (article 1.0)))))
857     (server
858      (vertical 1.0
859                (server 1.0 point)
860                (if gnus-carpal '(server-carpal 2))))
861     (browse
862      (vertical 1.0
863                (browse 1.0 point)
864                (if gnus-carpal '(browse-carpal 2))))
865     (message
866      (vertical 1.0
867                (message 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article-copy 0.5)
896                (message 1.0 point)))
897     (forward
898      (vertical 1.0
899                (message 1.0 point)))
900     (reply-yank
901      (vertical 1.0
902                (message 1.0 point)))
903     (mail-bounce
904      (vertical 1.0
905                (article 0.5)
906                (message 1.0 point)))
907     (draft
908      (vertical 1.0
909                (draft 1.0 point)))
910     (pipe
911      (vertical 1.0
912                (summary 0.25 point)
913                (if gnus-carpal '(summary-carpal 4))
914                ("*Shell Command Output*" 1.0)))
915     (bug
916      (vertical 1.0
917                ("*Gnus Help Bug*" 0.5)
918                ("*Gnus Bug*" 1.0 point)))
919     (compose-bounce
920      (vertical 1.0
921                (article 0.5)
922                (message 1.0 point))))
923   "Window configuration for all possible Gnus buffers.
924 This variable is a list of lists.  Each of these lists has a NAME and
925 a RULE.  The NAMEs are commonsense names like `group', which names a
926 rule used when displaying the group buffer; `summary', which names a
927 rule for what happens when you enter a group and do not display an
928 article buffer; and so on.  See the value of this variable for a
929 complete list of NAMEs.
930
931 Each RULE is a list of vectors.  The first element in this vector is
932 the name of the buffer to be displayed; the second element is the
933 percentage of the screen this buffer is to occupy (a number in the
934 0.0-0.99 range); the optional third element is `point', which should
935 be present to denote which buffer point is to go to after making this
936 buffer configuration.")
937
938 (defvar gnus-window-to-buffer
939   '((group . gnus-group-buffer)
940     (summary . gnus-summary-buffer)
941     (article . gnus-article-buffer)
942     (server . gnus-server-buffer)
943     (browse . "*Gnus Browse Server*")
944     (edit-group . gnus-group-edit-buffer)
945     (edit-server . gnus-server-edit-buffer)
946     (group-carpal . gnus-carpal-group-buffer)
947     (summary-carpal . gnus-carpal-summary-buffer)
948     (server-carpal . gnus-carpal-server-buffer)
949     (browse-carpal . gnus-carpal-browse-buffer)
950     (edit-score . gnus-score-edit-buffer)
951     (message . gnus-message-buffer)
952     (mail . gnus-message-buffer)
953     (post-news . gnus-message-buffer)
954     (faq . gnus-faq-buffer)
955     (picons . "*Picons*")
956     (tree . gnus-tree-buffer)
957     (info . gnus-info-buffer)
958     (article-copy . gnus-article-copy)
959     (draft . gnus-draft-buffer))
960   "Mapping from short symbols to buffer names or buffer variables.")
961
962 (defvar gnus-carpal nil
963   "*If non-nil, display clickable icons.")
964
965 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
966   "*Function called with a group name when new group is detected.
967 A few pre-made functions are supplied: `gnus-subscribe-randomly'
968 inserts new groups at the beginning of the list of groups;
969 `gnus-subscribe-alphabetically' inserts new groups in strict
970 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
971 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
972 for your decision; `gnus-subscribe-killed' kills all new groups.")
973
974 ;; Suggested by a bug report by Hallvard B Furuseth.
975 ;; <h.b.furuseth@usit.uio.no>.
976 (defvar gnus-subscribe-options-newsgroup-method
977   (function gnus-subscribe-alphabetically)
978   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
979 If, for instance, you want to subscribe to all newsgroups in the
980 \"no\" and \"alt\" hierarchies, you'd put the following in your
981 .newsrc file:
982
983 options -n no.all alt.all
984
985 Gnus will the subscribe all new newsgroups in these hierarchies with
986 the subscription method in this variable.")
987
988 (defvar gnus-subscribe-hierarchical-interactive nil
989   "*If non-nil, Gnus will offer to subscribe hierarchically.
990 When a new hierarchy appears, Gnus will ask the user:
991
992 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
993
994 If the user pressed `d', Gnus will descend the hierarchy, `y' will
995 subscribe to all newsgroups in the hierarchy and `s' will skip this
996 hierarchy in its entirety.")
997
998 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
999   "*Function used for sorting the group buffer.
1000 This function will be called with group info entries as the arguments
1001 for the groups to be sorted.  Pre-made functions include
1002 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1003 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1004 `gnus-group-sort-by-rank'.
1005
1006 This variable can also be a list of sorting functions.  In that case,
1007 the most significant sort function should be the last function in the
1008 list.")
1009
1010 ;; Mark variables suggested by Thomas Michanek
1011 ;; <Thomas.Michanek@telelogic.se>.
1012 (defvar gnus-unread-mark ? 
1013   "*Mark used for unread articles.")
1014 (defvar gnus-ticked-mark ?!
1015   "*Mark used for ticked articles.")
1016 (defvar gnus-dormant-mark ??
1017   "*Mark used for dormant articles.")
1018 (defvar gnus-del-mark ?r
1019   "*Mark used for del'd articles.")
1020 (defvar gnus-read-mark ?R
1021   "*Mark used for read articles.")
1022 (defvar gnus-expirable-mark ?E
1023   "*Mark used for expirable articles.")
1024 (defvar gnus-killed-mark ?K
1025   "*Mark used for killed articles.")
1026 (defvar gnus-souped-mark ?F
1027   "*Mark used for killed articles.")
1028 (defvar gnus-kill-file-mark ?X
1029   "*Mark used for articles killed by kill files.")
1030 (defvar gnus-low-score-mark ?Y
1031   "*Mark used for articles with a low score.")
1032 (defvar gnus-catchup-mark ?C
1033   "*Mark used for articles that are caught up.")
1034 (defvar gnus-replied-mark ?A
1035   "*Mark used for articles that have been replied to.")
1036 (defvar gnus-cached-mark ?*
1037   "*Mark used for articles that are in the cache.")
1038 (defvar gnus-saved-mark ?S
1039   "*Mark used for articles that have been saved to.")
1040 (defvar gnus-process-mark ?#
1041   "*Process mark.")
1042 (defvar gnus-ancient-mark ?O
1043   "*Mark used for ancient articles.")
1044 (defvar gnus-sparse-mark ?Q
1045   "*Mark used for sparsely reffed articles.")
1046 (defvar gnus-canceled-mark ?G
1047   "*Mark used for canceled articles.")
1048 (defvar gnus-score-over-mark ?+
1049   "*Score mark used for articles with high scores.")
1050 (defvar gnus-score-below-mark ?-
1051   "*Score mark used for articles with low scores.")
1052 (defvar gnus-empty-thread-mark ? 
1053   "*There is no thread under the article.")
1054 (defvar gnus-not-empty-thread-mark ?=
1055   "*There is a thread under the article.")
1056
1057 (defvar gnus-view-pseudo-asynchronously nil
1058   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1059
1060 (defvar gnus-view-pseudos nil
1061   "*If `automatic', pseudo-articles will be viewed automatically.
1062 If `not-confirm', pseudos will be viewed automatically, and the user
1063 will not be asked to confirm the command.")
1064
1065 (defvar gnus-view-pseudos-separately t
1066   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1067 If nil, all files that use the same viewing command will be given as a
1068 list of parameters to that command.")
1069
1070 (defvar gnus-insert-pseudo-articles t
1071   "*If non-nil, insert pseudo-articles when decoding articles.")
1072
1073 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1074   "*Format of group lines.
1075 It works along the same lines as a normal formatting string,
1076 with some simple extensions.
1077
1078 %M    Only marked articles (character, \"*\" or \" \")
1079 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1080 %L    Level of subscribedness (integer)
1081 %N    Number of unread articles (integer)
1082 %I    Number of dormant articles (integer)
1083 %i    Number of ticked and dormant (integer)
1084 %T    Number of ticked articles (integer)
1085 %R    Number of read articles (integer)
1086 %t    Total number of articles (integer)
1087 %y    Number of unread, unticked articles (integer)
1088 %G    Group name (string)
1089 %g    Qualified group name (string)
1090 %D    Group description (string)
1091 %s    Select method (string)
1092 %o    Moderated group (char, \"m\")
1093 %p    Process mark (char)
1094 %O    Moderated group (string, \"(m)\" or \"\")
1095 %P    Topic indentation (string)
1096 %l    Whether there are GroupLens predictions for this group (string)
1097 %n    Select from where (string)
1098 %z    A string that look like `<%s:%n>' if a foreign select method is used
1099 %u    User defined specifier.  The next character in the format string should
1100       be a letter.  Gnus will call the function gnus-user-format-function-X,
1101       where X is the letter following %u.  The function will be passed the
1102       current header as argument.  The function should return a string, which
1103       will be inserted into the buffer just like information from any other
1104       group specifier.
1105
1106 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1107 the mouse point move inside the area.  There can only be one such area.
1108
1109 Note that this format specification is not always respected.  For
1110 reasons of efficiency, when listing killed groups, this specification
1111 is ignored altogether.  If the spec is changed considerably, your
1112 output may end up looking strange when listing both alive and killed
1113 groups.
1114
1115 If you use %o or %O, reading the active file will be slower and quite
1116 a bit of extra memory will be used. %D will also worsen performance.
1117 Also note that if you change the format specification to include any
1118 of these specs, you must probably re-start Gnus to see them go into
1119 effect.")
1120
1121 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1122   "*The format specification of the lines in the summary buffer.
1123
1124 It works along the same lines as a normal formatting string,
1125 with some simple extensions.
1126
1127 %N   Article number, left padded with spaces (string)
1128 %S   Subject (string)
1129 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1130 %n   Name of the poster (string)
1131 %a   Extracted name of the poster (string)
1132 %A   Extracted address of the poster (string)
1133 %F   Contents of the From: header (string)
1134 %x   Contents of the Xref: header (string)
1135 %D   Date of the article (string)
1136 %d   Date of the article (string) in DD-MMM format
1137 %M   Message-id of the article (string)
1138 %r   References of the article (string)
1139 %c   Number of characters in the article (integer)
1140 %L   Number of lines in the article (integer)
1141 %I   Indentation based on thread level (a string of spaces)
1142 %T   A string with two possible values: 80 spaces if the article
1143      is on thread level two or larger and 0 spaces on level one
1144 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1145 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1146 %[   Opening bracket (character, \"[\" or \"<\")
1147 %]   Closing bracket (character, \"]\" or \">\")
1148 %>   Spaces of length thread-level (string)
1149 %<   Spaces of length (- 20 thread-level) (string)
1150 %i   Article score (number)
1151 %z   Article zcore (character)
1152 %t   Number of articles under the current thread (number).
1153 %e   Whether the thread is empty or not (character).
1154 %l   GroupLens score (string).
1155 %u   User defined specifier.  The next character in the format string should
1156      be a letter.  Gnus will call the function gnus-user-format-function-X,
1157      where X is the letter following %u.  The function will be passed the
1158      current header as argument.  The function should return a string, which
1159      will be inserted into the summary just like information from any other
1160      summary specifier.
1161
1162 Text between %( and %) will be highlighted with `gnus-mouse-face'
1163 when the mouse point is placed inside the area.  There can only be one
1164 such area.
1165
1166 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1167 with care.  For reasons of efficiency, Gnus will compute what column
1168 these characters will end up in, and \"hard-code\" that.  This means that
1169 it is illegal to have these specs after a variable-length spec.  Well,
1170 you might not be arrested, but your summary buffer will look strange,
1171 which is bad enough.
1172
1173 The smart choice is to have these specs as for to the left as
1174 possible.
1175
1176 This restriction may disappear in later versions of Gnus.")
1177
1178 (defvar gnus-summary-dummy-line-format
1179   "*  %(:                          :%) %S\n"
1180   "*The format specification for the dummy roots in the summary buffer.
1181 It works along the same lines as a normal formatting string,
1182 with some simple extensions.
1183
1184 %S  The subject")
1185
1186 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1187   "*The format specification for the summary mode line.
1188 It works along the same lines as a normal formatting string,
1189 with some simple extensions:
1190
1191 %G  Group name
1192 %p  Unprefixed group name
1193 %A  Current article number
1194 %V  Gnus version
1195 %U  Number of unread articles in the group
1196 %e  Number of unselected articles in the group
1197 %Z  A string with unread/unselected article counts
1198 %g  Shortish group name
1199 %S  Subject of the current article
1200 %u  User-defined spec
1201 %s  Current score file name
1202 %d  Number of dormant articles
1203 %r  Number of articles that have been marked as read in this session
1204 %E  Number of articles expunged by the score files")
1205
1206 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1207   "*The format specification for the article mode line.
1208 See `gnus-summary-mode-line-format' for a closer description.")
1209
1210 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1211   "*The format specification for the group mode line.
1212 It works along the same lines as a normal formatting string,
1213 with some simple extensions:
1214
1215 %S   The native news server.
1216 %M   The native select method.")
1217
1218 (defvar gnus-valid-select-methods
1219   '(("nntp" post address prompt-address)
1220     ("nnspool" post address)
1221     ("nnvirtual" post-mail virtual prompt-address)
1222     ("nnmbox" mail respool address)
1223     ("nnml" mail respool address)
1224     ("nnmh" mail respool address)
1225     ("nndir" post-mail prompt-address address)
1226     ("nneething" none address prompt-address)
1227     ("nndoc" none address prompt-address)
1228     ("nnbabyl" mail address respool)
1229     ("nnkiboze" post address virtual)
1230     ("nnsoup" post-mail address)
1231     ("nndraft" post-mail)
1232     ("nnfolder" mail respool address))
1233   "An alist of valid select methods.
1234 The first element of each list lists should be a string with the name
1235 of the select method.  The other elements may be be the category of
1236 this method (ie. `post', `mail', `none' or whatever) or other
1237 properties that this method has (like being respoolable).
1238 If you implement a new select method, all you should have to change is
1239 this variable.  I think.")
1240
1241 (defvar gnus-updated-mode-lines '(group article summary tree)
1242   "*List of buffers that should update their mode lines.
1243 The list may contain the symbols `group', `article' and `summary'.  If
1244 the corresponding symbol is present, Gnus will keep that mode line
1245 updated with information that may be pertinent.
1246 If this variable is nil, screen refresh may be quicker.")
1247
1248 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1249 (defvar gnus-mode-non-string-length nil
1250   "*Max length of mode-line non-string contents.
1251 If this is nil, Gnus will take space as is needed, leaving the rest
1252 of the modeline intact.")
1253
1254 ;see gnus-cus.el
1255 ;(defvar gnus-mouse-face 'highlight
1256 ;  "*Face used for mouse highlighting in Gnus.
1257 ;No mouse highlights will be done if `gnus-visual' is nil.")
1258
1259 (defvar gnus-summary-mark-below nil
1260   "*Mark all articles with a score below this variable as read.
1261 This variable is local to each summary buffer and usually set by the
1262 score file.")
1263
1264 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1265   "*List of functions used for sorting articles in the summary buffer.
1266 This variable is only used when not using a threaded display.")
1267
1268 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1269   "*List of functions used for sorting threads in the summary buffer.
1270 By default, threads are sorted by article number.
1271
1272 Each function takes two threads and return non-nil if the first thread
1273 should be sorted before the other.  If you use more than one function,
1274 the primary sort function should be the last.  You should probably
1275 always include `gnus-thread-sort-by-number' in the list of sorting
1276 functions -- preferably first.
1277
1278 Ready-mady functions include `gnus-thread-sort-by-number',
1279 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1280 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1281 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1282
1283 (defvar gnus-thread-score-function '+
1284   "*Function used for calculating the total score of a thread.
1285
1286 The function is called with the scores of the article and each
1287 subthread and should then return the score of the thread.
1288
1289 Some functions you can use are `+', `max', or `min'.")
1290
1291 (defvar gnus-summary-expunge-below nil
1292   "All articles that have a score less than this variable will be expunged.")
1293
1294 (defvar gnus-thread-expunge-below nil
1295   "All threads that have a total score less than this variable will be expunged.
1296 See `gnus-thread-score-function' for en explanation of what a
1297 \"thread score\" is.")
1298
1299 (defvar gnus-auto-subscribed-groups
1300   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1301   "*All new groups that match this regexp will be subscribed automatically.
1302 Note that this variable only deals with new groups.  It has no effect
1303 whatsoever on old groups.")
1304
1305 (defvar gnus-options-subscribe nil
1306   "*All new groups matching this regexp will be subscribed unconditionally.
1307 Note that this variable deals only with new newsgroups.  This variable
1308 does not affect old newsgroups.")
1309
1310 (defvar gnus-options-not-subscribe nil
1311   "*All new groups matching this regexp will be ignored.
1312 Note that this variable deals only with new newsgroups.  This variable
1313 does not affect old (already subscribed) newsgroups.")
1314
1315 (defvar gnus-auto-expirable-newsgroups nil
1316   "*Groups in which to automatically mark read articles as expirable.
1317 If non-nil, this should be a regexp that should match all groups in
1318 which to perform auto-expiry.  This only makes sense for mail groups.")
1319
1320 (defvar gnus-total-expirable-newsgroups nil
1321   "*Groups in which to perform expiry of all read articles.
1322 Use with extreme caution.  All groups that match this regexp will be
1323 expiring - which means that all read articles will be deleted after
1324 (say) one week.  (This only goes for mail groups and the like, of
1325 course.)")
1326
1327 (defvar gnus-group-uncollapsed-levels 1
1328   "Number of group name elements to leave alone when making a short group name.")
1329
1330 (defvar gnus-hidden-properties '(invisible t intangible t)
1331   "Property list to use for hiding text.")
1332
1333 (defvar gnus-modtime-botch nil
1334   "*Non-nil means .newsrc should be deleted prior to save.  
1335 Its use is due to the bogus appearance that .newsrc was modified on
1336 disc.")
1337
1338 ;; Hooks.
1339
1340 (defvar gnus-group-mode-hook nil
1341   "*A hook for Gnus group mode.")
1342
1343 (defvar gnus-summary-mode-hook nil
1344   "*A hook for Gnus summary mode.
1345 This hook is run before any variables are set in the summary buffer.")
1346
1347 (defvar gnus-article-mode-hook nil
1348   "*A hook for Gnus article mode.")
1349
1350 (defvar gnus-summary-prepare-exit-hook nil
1351   "*A hook called when preparing to exit from the summary buffer.
1352 It calls `gnus-summary-expire-articles' by default.")
1353 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1354
1355 (defvar gnus-summary-exit-hook nil
1356   "*A hook called on exit from the summary buffer.")
1357
1358 (defvar gnus-group-catchup-group-hook nil
1359   "*A hook run when catching up a group from the group buffer.")
1360
1361 (defvar gnus-open-server-hook nil
1362   "*A hook called just before opening connection to the news server.")
1363
1364 (defvar gnus-load-hook nil
1365   "*A hook run while Gnus is loaded.")
1366
1367 (defvar gnus-startup-hook nil
1368   "*A hook called at startup.
1369 This hook is called after Gnus is connected to the NNTP server.")
1370
1371 (defvar gnus-get-new-news-hook nil
1372   "*A hook run just before Gnus checks for new news.")
1373
1374 (defvar gnus-after-getting-new-news-hook nil
1375   "*A hook run after Gnus checks for new news.")
1376
1377 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1378   "*A function that is called to generate the group buffer.
1379 The function is called with three arguments: The first is a number;
1380 all group with a level less or equal to that number should be listed,
1381 if the second is non-nil, empty groups should also be displayed.  If
1382 the third is non-nil, it is a number.  No groups with a level lower
1383 than this number should be displayed.
1384
1385 The only current function implemented is `gnus-group-prepare-flat'.")
1386
1387 (defvar gnus-group-prepare-hook nil
1388   "*A hook called after the group buffer has been generated.
1389 If you want to modify the group buffer, you can use this hook.")
1390
1391 (defvar gnus-summary-prepare-hook nil
1392   "*A hook called after the summary buffer has been generated.
1393 If you want to modify the summary buffer, you can use this hook.")
1394
1395 (defvar gnus-summary-generate-hook nil
1396   "*A hook run just before generating the summary buffer.
1397 This hook is commonly used to customize threading variables and the
1398 like.")
1399
1400 (defvar gnus-article-prepare-hook nil
1401   "*A hook called after an article has been prepared in the article buffer.
1402 If you want to run a special decoding program like nkf, use this hook.")
1403
1404 ;(defvar gnus-article-display-hook nil
1405 ;  "*A hook called after the article is displayed in the article buffer.
1406 ;The hook is designed to change the contents of the article
1407 ;buffer.  Typical functions that this hook may contain are
1408 ;`gnus-article-hide-headers' (hide selected headers),
1409 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1410 ;`gnus-article-hide-signature' (hide signature) and
1411 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1413 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1414 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1415
1416 (defvar gnus-article-x-face-command
1417   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1418   "String or function to be executed to display an X-Face header.
1419 If it is a string, the command will be executed in a sub-shell
1420 asynchronously.  The compressed face will be piped to this command.")
1421
1422 (defvar gnus-article-x-face-too-ugly nil
1423   "Regexp matching posters whose face shouldn't be shown automatically.")
1424
1425 (defvar gnus-select-group-hook nil
1426   "*A hook called when a newsgroup is selected.
1427
1428 If you'd like to simplify subjects like the
1429 `gnus-summary-next-same-subject' command does, you can use the
1430 following hook:
1431
1432  (setq gnus-select-group-hook
1433       (list
1434         (lambda ()
1435           (mapcar (lambda (header)
1436                      (mail-header-set-subject
1437                       header
1438                       (gnus-simplify-subject
1439                        (mail-header-subject header) 're-only)))
1440                   gnus-newsgroup-headers))))")
1441
1442 (defvar gnus-select-article-hook nil
1443   "*A hook called when an article is selected.")
1444
1445 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1446   "*A hook called to apply kill files to a group.
1447 This hook is intended to apply a kill file to the selected newsgroup.
1448 The function `gnus-apply-kill-file' is called by default.
1449
1450 Since a general kill file is too heavy to use only for a few
1451 newsgroups, I recommend you to use a lighter hook function.  For
1452 example, if you'd like to apply a kill file to articles which contains
1453 a string `rmgroup' in subject in newsgroup `control', you can use the
1454 following hook:
1455
1456  (setq gnus-apply-kill-hook
1457       (list
1458         (lambda ()
1459           (cond ((string-match \"control\" gnus-newsgroup-name)
1460                  (gnus-kill \"Subject\" \"rmgroup\")
1461                  (gnus-expunge \"X\"))))))")
1462
1463 (defvar gnus-visual-mark-article-hook
1464   (list 'gnus-highlight-selected-summary)
1465   "*Hook run after selecting an article in the summary buffer.
1466 It is meant to be used for highlighting the article in some way.  It
1467 is not run if `gnus-visual' is nil.")
1468
1469 (defvar gnus-parse-headers-hook nil
1470   "*A hook called before parsing the headers.")
1471 (add-hook 'gnus-parse-headers-hook 'gnus-headers-decode-quoted-printable)
1472
1473 (defvar gnus-exit-group-hook nil
1474   "*A hook called when exiting (not quitting) summary mode.")
1475
1476 (defvar gnus-suspend-gnus-hook nil
1477   "*A hook called when suspending (not exiting) Gnus.")
1478
1479 (defvar gnus-exit-gnus-hook nil
1480   "*A hook called when exiting Gnus.")
1481
1482 (defvar gnus-after-exiting-gnus-hook nil
1483   "*A hook called after exiting Gnus.")
1484
1485 (defvar gnus-save-newsrc-hook nil
1486   "*A hook called before saving any of the newsrc files.")
1487
1488 (defvar gnus-save-quick-newsrc-hook nil
1489   "*A hook called just before saving the quick newsrc file.
1490 Can be used to turn version control on or off.")
1491
1492 (defvar gnus-save-standard-newsrc-hook nil
1493   "*A hook called just before saving the standard newsrc file.
1494 Can be used to turn version control on or off.")
1495
1496 (defvar gnus-summary-update-hook
1497   (list 'gnus-summary-highlight-line)
1498   "*A hook called when a summary line is changed.
1499 The hook will not be called if `gnus-visual' is nil.
1500
1501 The default function `gnus-summary-highlight-line' will
1502 highlight the line according to the `gnus-summary-highlight'
1503 variable.")
1504
1505 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1506   "*A hook called when a group line is changed.
1507 The hook will not be called if `gnus-visual' is nil.
1508
1509 The default function `gnus-group-highlight-line' will
1510 highlight the line according to the `gnus-group-highlight'
1511 variable.")
1512
1513 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1514   "*A hook called when an article is selected for the first time.
1515 The hook is intended to mark an article as read (or unread)
1516 automatically when it is selected.")
1517
1518 (defvar gnus-group-change-level-function nil
1519   "Function run when a group level is changed.
1520 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1521
1522 ;; Remove any hilit infestation.
1523 (add-hook 'gnus-startup-hook
1524           (lambda ()
1525             (remove-hook 'gnus-summary-prepare-hook
1526                          'hilit-rehighlight-buffer-quietly)
1527             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1528             (setq gnus-mark-article-hook
1529                   '(gnus-summary-mark-read-and-unread-as-read))
1530             (remove-hook 'gnus-article-prepare-hook
1531                          'hilit-rehighlight-buffer-quietly)))
1532
1533 \f
1534 ;; Internal variables
1535
1536 ;; Dummy variable.
1537 (defvar gnus-use-generic-from nil)
1538
1539 (defvar gnus-thread-indent-array nil)
1540 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1541
1542 (defvar gnus-newsrc-file-version nil)
1543
1544 (defvar gnus-method-history nil)
1545 ;; Variable holding the user answers to all method prompts.
1546
1547 (defvar gnus-group-history nil)
1548 ;; Variable holding the user answers to all group prompts.
1549
1550 (defvar gnus-server-alist nil
1551   "List of available servers.")
1552
1553 (defvar gnus-group-indentation-function nil)
1554
1555 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1556
1557 (defvar gnus-goto-missing-group-function nil)
1558
1559 (defvar gnus-override-subscribe-method nil)
1560
1561 (defvar gnus-group-goto-next-group-function nil
1562   "Function to override finding the next group after listing groups.")
1563
1564 (defconst gnus-article-mark-lists
1565   '((marked . tick) (replied . reply)
1566     (expirable . expire) (killed . killed)
1567     (bookmarks . bookmark) (dormant . dormant)
1568     (scored . score) (saved . save)
1569     (cached . cache)
1570     ))
1571
1572 ;; Avoid highlighting in kill files.
1573 (defvar gnus-summary-inhibit-highlight nil)
1574 (defvar gnus-newsgroup-selected-overlay nil)
1575
1576 (defvar gnus-inhibit-hiding nil)
1577 (defvar gnus-group-indentation "")
1578 (defvar gnus-inhibit-limiting nil)
1579 (defvar gnus-created-frames nil)
1580
1581 (defvar gnus-article-mode-map nil)
1582 (defvar gnus-dribble-buffer nil)
1583 (defvar gnus-headers-retrieved-by nil)
1584 (defvar gnus-article-reply nil)
1585 (defvar gnus-override-method nil)
1586 (defvar gnus-article-check-size nil)
1587
1588 (defvar gnus-current-score-file nil)
1589 (defvar gnus-newsgroup-adaptive-score-file nil)
1590 (defvar gnus-scores-exclude-files nil)
1591
1592 (defvar gnus-opened-servers nil)
1593
1594 (defvar gnus-current-move-group nil)
1595 (defvar gnus-current-copy-group nil)
1596 (defvar gnus-current-crosspost-group nil)
1597
1598 (defvar gnus-newsgroup-dependencies nil)
1599 (defvar gnus-newsgroup-async nil)
1600 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1601
1602 (defvar gnus-newsgroup-adaptive nil)
1603
1604 (defvar gnus-summary-display-table nil)
1605 (defvar gnus-summary-display-article-function nil)
1606
1607 (defvar gnus-summary-highlight-line-function nil
1608   "Function called after highlighting a summary line.")
1609
1610 (defvar gnus-group-line-format-alist
1611   `((?M gnus-tmp-marked-mark ?c)
1612     (?S gnus-tmp-subscribed ?c)
1613     (?L gnus-tmp-level ?d)
1614     (?N (cond ((eq number t) "*" )
1615               ((numberp number) 
1616                (int-to-string
1617                 (+ number
1618                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1619                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1620               (t number)) ?s)
1621     (?R gnus-tmp-number-of-read ?s)
1622     (?t gnus-tmp-number-total ?d)
1623     (?y gnus-tmp-number-of-unread ?s)
1624     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1625     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1626     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1627            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1628     (?g gnus-tmp-group ?s)
1629     (?G gnus-tmp-qualified-group ?s)
1630     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1631     (?D gnus-tmp-newsgroup-description ?s)
1632     (?o gnus-tmp-moderated ?c)
1633     (?O gnus-tmp-moderated-string ?s)
1634     (?p gnus-tmp-process-marked ?c)
1635     (?s gnus-tmp-news-server ?s)
1636     (?n gnus-tmp-news-method ?s)
1637     (?P gnus-group-indentation ?s)
1638     (?l gnus-tmp-grouplens ?s)
1639     (?z gnus-tmp-news-method-string ?s)
1640     (?u gnus-tmp-user-defined ?s)))
1641
1642 (defvar gnus-summary-line-format-alist
1643   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1644     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1645     (?s gnus-tmp-subject-or-nil ?s)
1646     (?n gnus-tmp-name ?s)
1647     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1648         ?s)
1649     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1650             gnus-tmp-from) ?s)
1651     (?F gnus-tmp-from ?s)
1652     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1653     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1654     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1655     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1656     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1657     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1658     (?L gnus-tmp-lines ?d)
1659     (?I gnus-tmp-indentation ?s)
1660     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1661     (?R gnus-tmp-replied ?c)
1662     (?\[ gnus-tmp-opening-bracket ?c)
1663     (?\] gnus-tmp-closing-bracket ?c)
1664     (?\> (make-string gnus-tmp-level ? ) ?s)
1665     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1666     (?i gnus-tmp-score ?d)
1667     (?z gnus-tmp-score-char ?c)
1668     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1669     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1670     (?U gnus-tmp-unread ?c)
1671     (?t (gnus-summary-number-of-articles-in-thread
1672          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1673         ?d)
1674     (?e (gnus-summary-number-of-articles-in-thread
1675          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1676         ?c)
1677     (?u gnus-tmp-user-defined ?s))
1678   "An alist of format specifications that can appear in summary lines,
1679 and what variables they correspond with, along with the type of the
1680 variable (string, integer, character, etc).")
1681
1682 (defvar gnus-summary-dummy-line-format-alist
1683   `((?S gnus-tmp-subject ?s)
1684     (?N gnus-tmp-number ?d)
1685     (?u gnus-tmp-user-defined ?s)))
1686
1687 (defvar gnus-summary-mode-line-format-alist
1688   `((?G gnus-tmp-group-name ?s)
1689     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1690     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1691     (?A gnus-tmp-article-number ?d)
1692     (?Z gnus-tmp-unread-and-unselected ?s)
1693     (?V gnus-version ?s)
1694     (?U gnus-tmp-unread ?d)
1695     (?S gnus-tmp-subject ?s)
1696     (?e gnus-tmp-unselected ?d)
1697     (?u gnus-tmp-user-defined ?s)
1698     (?d (length gnus-newsgroup-dormant) ?d)
1699     (?t (length gnus-newsgroup-marked) ?d)
1700     (?r (length gnus-newsgroup-reads) ?d)
1701     (?E gnus-newsgroup-expunged-tally ?d)
1702     (?s (gnus-current-score-file-nondirectory) ?s)))
1703
1704 (defvar gnus-article-mode-line-format-alist
1705   gnus-summary-mode-line-format-alist)
1706
1707 (defvar gnus-group-mode-line-format-alist
1708   `((?S gnus-tmp-news-server ?s)
1709     (?M gnus-tmp-news-method ?s)
1710     (?u gnus-tmp-user-defined ?s)))
1711
1712 (defvar gnus-have-read-active-file nil)
1713
1714 (defconst gnus-maintainer
1715   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1716   "The mail address of the Gnus maintainers.")
1717
1718 (defconst gnus-version "September Gnus v0.81"
1719   "Version number for this version of Gnus.")
1720
1721 (defvar gnus-info-nodes
1722   '((gnus-group-mode "(gnus)The Group Buffer")
1723     (gnus-summary-mode "(gnus)The Summary Buffer")
1724     (gnus-article-mode "(gnus)The Article Buffer"))
1725   "Assoc list of major modes and related Info nodes.")
1726
1727 (defvar gnus-group-buffer "*Group*")
1728 (defvar gnus-summary-buffer "*Summary*")
1729 (defvar gnus-article-buffer "*Article*")
1730 (defvar gnus-server-buffer "*Server*")
1731
1732 (defvar gnus-work-buffer " *gnus work*")
1733
1734 (defvar gnus-original-article-buffer " *Original Article*")
1735 (defvar gnus-original-article nil)
1736
1737 (defvar gnus-buffer-list nil
1738   "Gnus buffers that should be killed on exit.")
1739
1740 (defvar gnus-slave nil
1741   "Whether this Gnus is a slave or not.")
1742
1743 (defvar gnus-variable-list
1744   '(gnus-newsrc-options gnus-newsrc-options-n
1745     gnus-newsrc-last-checked-date
1746     gnus-newsrc-alist gnus-server-alist
1747     gnus-killed-list gnus-zombie-list
1748     gnus-topic-topology gnus-topic-alist
1749     gnus-format-specs)
1750   "Gnus variables saved in the quick startup file.")
1751
1752 (defvar gnus-newsrc-options nil
1753   "Options line in the .newsrc file.")
1754
1755 (defvar gnus-newsrc-options-n nil
1756   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1757
1758 (defvar gnus-newsrc-last-checked-date nil
1759   "Date Gnus last asked server for new newsgroups.")
1760
1761 (defvar gnus-topic-topology nil
1762   "The complete topic hierarchy.")
1763
1764 (defvar gnus-topic-alist nil
1765   "The complete topic-group alist.")
1766
1767 (defvar gnus-newsrc-alist nil
1768   "Assoc list of read articles.
1769 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1770
1771 (defvar gnus-newsrc-hashtb nil
1772   "Hashtable of gnus-newsrc-alist.")
1773
1774 (defvar gnus-killed-list nil
1775   "List of killed newsgroups.")
1776
1777 (defvar gnus-killed-hashtb nil
1778   "Hash table equivalent of gnus-killed-list.")
1779
1780 (defvar gnus-zombie-list nil
1781   "List of almost dead newsgroups.")
1782
1783 (defvar gnus-description-hashtb nil
1784   "Descriptions of newsgroups.")
1785
1786 (defvar gnus-list-of-killed-groups nil
1787   "List of newsgroups that have recently been killed by the user.")
1788
1789 (defvar gnus-active-hashtb nil
1790   "Hashtable of active articles.")
1791
1792 (defvar gnus-moderated-list nil
1793   "List of moderated newsgroups.")
1794
1795 (defvar gnus-group-marked nil)
1796
1797 (defvar gnus-current-startup-file nil
1798   "Startup file for the current host.")
1799
1800 (defvar gnus-last-search-regexp nil
1801   "Default regexp for article search command.")
1802
1803 (defvar gnus-last-shell-command nil
1804   "Default shell command on article.")
1805
1806 (defvar gnus-current-select-method nil
1807   "The current method for selecting a newsgroup.")
1808
1809 (defvar gnus-group-list-mode nil)
1810
1811 (defvar gnus-article-internal-prepare-hook nil)
1812
1813 (defvar gnus-newsgroup-name nil)
1814 (defvar gnus-newsgroup-begin nil)
1815 (defvar gnus-newsgroup-end nil)
1816 (defvar gnus-newsgroup-last-rmail nil)
1817 (defvar gnus-newsgroup-last-mail nil)
1818 (defvar gnus-newsgroup-last-folder nil)
1819 (defvar gnus-newsgroup-last-file nil)
1820 (defvar gnus-newsgroup-auto-expire nil)
1821 (defvar gnus-newsgroup-active nil)
1822
1823 (defvar gnus-newsgroup-data nil)
1824 (defvar gnus-newsgroup-data-reverse nil)
1825 (defvar gnus-newsgroup-limit nil)
1826 (defvar gnus-newsgroup-limits nil)
1827
1828 (defvar gnus-newsgroup-unreads nil
1829   "List of unread articles in the current newsgroup.")
1830
1831 (defvar gnus-newsgroup-unselected nil
1832   "List of unselected unread articles in the current newsgroup.")
1833
1834 (defvar gnus-newsgroup-reads nil
1835   "Alist of read articles and article marks in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-expunged-tally nil)
1838
1839 (defvar gnus-newsgroup-marked nil
1840   "List of ticked articles in the current newsgroup (a subset of unread art).")
1841
1842 (defvar gnus-newsgroup-killed nil
1843   "List of ranges of articles that have been through the scoring process.")
1844
1845 (defvar gnus-newsgroup-cached nil
1846   "List of articles that come from the article cache.")
1847
1848 (defvar gnus-newsgroup-saved nil
1849   "List of articles that have been saved.")
1850
1851 (defvar gnus-newsgroup-kill-headers nil)
1852
1853 (defvar gnus-newsgroup-replied nil
1854   "List of articles that have been replied to in the current newsgroup.")
1855
1856 (defvar gnus-newsgroup-expirable nil
1857   "List of articles in the current newsgroup that can be expired.")
1858
1859 (defvar gnus-newsgroup-processable nil
1860   "List of articles in the current newsgroup that can be processed.")
1861
1862 (defvar gnus-newsgroup-bookmarks nil
1863   "List of articles in the current newsgroup that have bookmarks.")
1864
1865 (defvar gnus-newsgroup-dormant nil
1866   "List of dormant articles in the current newsgroup.")
1867
1868 (defvar gnus-newsgroup-scored nil
1869   "List of scored articles in the current newsgroup.")
1870
1871 (defvar gnus-newsgroup-headers nil
1872   "List of article headers in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-threads nil)
1875
1876 (defvar gnus-newsgroup-prepared nil
1877   "Whether the current group has been prepared properly.")
1878
1879 (defvar gnus-newsgroup-ancient nil
1880   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-sparse nil)
1883
1884 (defvar gnus-current-article nil)
1885 (defvar gnus-article-current nil)
1886 (defvar gnus-current-headers nil)
1887 (defvar gnus-have-all-headers nil)
1888 (defvar gnus-last-article nil)
1889 (defvar gnus-newsgroup-history nil)
1890 (defvar gnus-current-kill-article nil)
1891
1892 ;; Save window configuration.
1893 (defvar gnus-prev-winconf nil)
1894
1895 (defvar gnus-summary-mark-positions nil)
1896 (defvar gnus-group-mark-positions nil)
1897
1898 (defvar gnus-reffed-article-number nil)
1899
1900 ;;; Let the byte-compiler know that we know about this variable.
1901 (defvar rmail-default-rmail-file)
1902
1903 (defvar gnus-cache-removable-articles nil)
1904
1905 (defvar gnus-dead-summary nil)
1906
1907 (defconst gnus-summary-local-variables
1908   '(gnus-newsgroup-name
1909     gnus-newsgroup-begin gnus-newsgroup-end
1910     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1911     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1912     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1913     gnus-newsgroup-unselected gnus-newsgroup-marked
1914     gnus-newsgroup-reads gnus-newsgroup-saved
1915     gnus-newsgroup-replied gnus-newsgroup-expirable
1916     gnus-newsgroup-processable gnus-newsgroup-killed
1917     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1918     gnus-newsgroup-headers gnus-newsgroup-threads
1919     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1920     gnus-current-article gnus-current-headers gnus-have-all-headers
1921     gnus-last-article gnus-article-internal-prepare-hook
1922     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1923     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1924     gnus-newsgroup-async gnus-thread-expunge-below
1925     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1926     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1927     gnus-newsgroup-history gnus-newsgroup-ancient
1928     gnus-newsgroup-sparse
1929     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1930     gnus-newsgroup-adaptive-score-file
1931     (gnus-newsgroup-expunged-tally . 0)
1932     gnus-cache-removable-articles gnus-newsgroup-cached
1933     gnus-newsgroup-data gnus-newsgroup-data-reverse
1934     gnus-newsgroup-limit gnus-newsgroup-limits)
1935   "Variables that are buffer-local to the summary buffers.")
1936
1937 (defconst gnus-bug-message
1938   "Sending a bug report to the Gnus Towers.
1939 ========================================
1940
1941 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1942 be sent to the Gnus Bug Exterminators.
1943
1944 At the bottom of the buffer you'll see lots of variable settings.
1945 Please do not delete those.  They will tell the Bug People what your
1946 environment is, so that it will be easier to locate the bugs.
1947
1948 If you have found a bug that makes Emacs go \"beep\", set
1949 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1950 and include the backtrace in your bug report.
1951
1952 Please describe the bug in annoying, painstaking detail.
1953
1954 Thank you for your help in stamping out bugs.
1955 ")
1956
1957 ;;; End of variables.
1958
1959 ;; Define some autoload functions Gnus might use.
1960 (eval-and-compile
1961
1962   ;; This little mapcar goes through the list below and marks the
1963   ;; symbols in question as autoloaded functions.
1964   (mapcar
1965    (lambda (package)
1966      (let ((interactive (nth 1 (memq ':interactive package))))
1967        (mapcar
1968         (lambda (function)
1969           (let (keymap)
1970             (when (consp function)
1971               (setq keymap (car (memq 'keymap function)))
1972               (setq function (car function)))
1973             (autoload function (car package) nil interactive keymap)))
1974         (if (eq (nth 1 package) ':interactive)
1975             (cdddr package)
1976           (cdr package)))))
1977    '(("metamail" metamail-buffer)
1978      ("info" Info-goto-node)
1979      ("hexl" hexl-hex-string-to-integer)
1980      ("pp" pp pp-to-string pp-eval-expression)
1981      ("mail-extr" mail-extract-address-components)
1982      ("nnmail" nnmail-split-fancy nnmail-article-group)
1983      ("nnvirtual" nnvirtual-catchup-group)
1984      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1985       timezone-make-sortable-date timezone-make-time-string)
1986      ("sendmail" mail-position-on-field mail-setup)
1987      ("rmailout" rmail-output)
1988      ("rnewspost" news-mail-other-window news-reply-yank-original
1989       news-caesar-buffer-body)
1990      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1991       rmail-show-message)
1992      ("gnus-soup" :interactive t
1993       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1994       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1995      ("nnsoup" nnsoup-pack-replies)
1996      ("gnus-scomo" :interactive t gnus-score-mode)
1997      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1998       gnus-Folder-save-name gnus-folder-save-name)
1999      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2000      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2001       gnus-server-make-menu-bar gnus-article-make-menu-bar
2002       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2003       gnus-summary-highlight-line gnus-carpal-setup-buffer
2004       gnus-group-highlight-line
2005       gnus-article-add-button gnus-insert-next-page-button
2006       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2007      ("gnus-vis" :interactive t
2008       gnus-article-push-button gnus-article-press-button
2009       gnus-article-highlight gnus-article-highlight-some
2010       gnus-article-highlight-headers gnus-article-highlight-signature
2011       gnus-article-add-buttons gnus-article-add-buttons-to-head
2012       gnus-article-next-button gnus-article-prev-button)
2013      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2014       gnus-demon-add-disconnection gnus-demon-add-handler
2015       gnus-demon-remove-handler)
2016      ("gnus-demon" :interactive t
2017       gnus-demon-init gnus-demon-cancel)
2018      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2019       gnus-tree-open gnus-tree-close)
2020      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2021       gnus-nocem-unwanted-article-p)
2022      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2023      ("gnus-srvr" gnus-browse-foreign-server)
2024      ("gnus-cite" :interactive t
2025       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2026       gnus-article-hide-citation gnus-article-fill-cited-article
2027       gnus-article-hide-citation-in-followups)
2028      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2029       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2030       gnus-execute gnus-expunge)
2031      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2032       gnus-cache-possibly-remove-articles gnus-cache-request-article
2033       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2034       gnus-cache-enter-remove-article gnus-cached-article-p
2035       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2036      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2037       gnus-cache-remove-article)
2038      ("gnus-score" :interactive t
2039       gnus-summary-increase-score gnus-summary-lower-score
2040       gnus-score-flush-cache gnus-score-close
2041       gnus-score-raise-same-subject-and-select
2042       gnus-score-raise-same-subject gnus-score-default
2043       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2044       gnus-score-lower-same-subject gnus-score-lower-thread
2045       gnus-possibly-score-headers)
2046      ("gnus-score"
2047       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2048       gnus-current-score-file-nondirectory gnus-score-adaptive
2049       gnus-score-find-trace gnus-score-file-name)
2050      ("gnus-edit" :interactive t gnus-score-customize)
2051      ("gnus-topic" :interactive t gnus-topic-mode)
2052      ("gnus-topic" gnus-topic-remove-group)
2053      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2054      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2055      ("gnus-uu" :interactive t
2056       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2057       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2058       gnus-uu-mark-by-regexp gnus-uu-mark-all
2059       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2060       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2061       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2062       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2063       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2064       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2065       gnus-uu-decode-binhex-view)
2066      ("gnus-msg" (gnus-summary-send-map keymap)
2067       gnus-mail-yank-original gnus-mail-send-and-exit
2068       gnus-sendmail-setup-mail gnus-article-mail
2069       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2070      ("gnus-msg" :interactive t
2071       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2072       gnus-summary-followup gnus-summary-followup-with-original
2073       gnus-summary-followup-and-reply
2074       gnus-summary-followup-and-reply-with-original
2075       gnus-summary-cancel-article gnus-summary-supersede-article
2076       gnus-post-news gnus-inews-news gnus-cancel-news
2077       gnus-summary-reply gnus-summary-reply-with-original
2078       gnus-summary-mail-forward gnus-summary-mail-other-window
2079       gnus-bug)
2080      ("gnus-picon" :interactive t gnus-article-display-picons
2081       gnus-group-display-picons gnus-picons-article-display-x-face)
2082      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2083       gnus-grouplens-mode)
2084      ("gnus-vm" gnus-vm-mail-setup)
2085      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2086       gnus-summary-save-article-vm gnus-yank-article))))
2087
2088 \f
2089
2090 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2091 ;; If you want the cursor to go somewhere else, set these two
2092 ;; functions in some startup hook to whatever you want.
2093 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2094 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2095
2096 ;;; Various macros and substs.
2097
2098 (defun gnus-header-from (header)
2099   (mail-header-from header))
2100
2101 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2102   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2103   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2104     `(let ((,tempvar (selected-window)))
2105        (unwind-protect
2106            (progn
2107              (pop-to-buffer ,buffer)
2108              ,@forms)
2109          (select-window ,tempvar)))))
2110
2111 (defmacro gnus-gethash (string hashtable)
2112   "Get hash value of STRING in HASHTABLE."
2113   `(symbol-value (intern-soft ,string ,hashtable)))
2114
2115 (defmacro gnus-sethash (string value hashtable)
2116   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2117   `(set (intern ,string ,hashtable) ,value))
2118
2119 (defmacro gnus-intern-safe (string hashtable)
2120   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2121   `(let ((symbol (intern ,string ,hashtable)))
2122      (or (boundp symbol)
2123          (set symbol nil))
2124      symbol))
2125
2126 (defmacro gnus-group-unread (group)
2127   "Get the currently computed number of unread articles in GROUP."
2128   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2129
2130 (defmacro gnus-group-entry (group)
2131   "Get the newsrc entry for GROUP."
2132   `(gnus-gethash ,group gnus-newsrc-hashtb))
2133
2134 (defmacro gnus-active (group)
2135   "Get active info on GROUP."
2136   `(gnus-gethash ,group gnus-active-hashtb))
2137
2138 (defmacro gnus-set-active (group active)
2139   "Set GROUP's active info."
2140   `(gnus-sethash ,group ,active gnus-active-hashtb))
2141
2142 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2143 ;;   function `substring' might cut on a middle of multi-octet
2144 ;;   character.
2145 (defun gnus-truncate-string (str width)
2146   (substring str 0 width))
2147
2148 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2149 ;; to limit the length of a string.  This function is necessary since
2150 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2151 (defsubst gnus-limit-string (str width)
2152   (if (> (length str) width)
2153       (substring str 0 width)
2154     str))
2155
2156 (defsubst gnus-simplify-subject-re (subject)
2157   "Remove \"Re:\" from subject lines."
2158   (if (string-match "^[Rr][Ee]: *" subject)
2159       (substring subject (match-end 0))
2160     subject))
2161
2162 (defsubst gnus-functionp (form)
2163   "Return non-nil if FORM is funcallable."
2164   (or (and (symbolp form) (fboundp form))
2165       (and (listp form) (eq (car form) 'lambda))))
2166
2167 (defsubst gnus-goto-char (point)
2168   (and point (goto-char point)))
2169
2170 (defmacro gnus-buffer-exists-p (buffer)
2171   `(let ((buffer ,buffer))
2172      (and buffer
2173           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2174                    buffer))))
2175
2176 (defmacro gnus-kill-buffer (buffer)
2177   `(let ((buf ,buffer))
2178      (if (gnus-buffer-exists-p buf)
2179          (kill-buffer buf))))
2180
2181 (defsubst gnus-point-at-bol ()
2182   "Return point at the beginning of the line."
2183   (let ((p (point)))
2184     (beginning-of-line)
2185     (prog1
2186         (point)
2187       (goto-char p))))
2188
2189 (defsubst gnus-point-at-eol ()
2190   "Return point at the end of the line."
2191   (let ((p (point)))
2192     (end-of-line)
2193     (prog1
2194         (point)
2195       (goto-char p))))
2196
2197 (defun gnus-alive-p ()
2198   "Say whether Gnus is running or not."
2199   (and gnus-group-buffer
2200        (get-buffer gnus-group-buffer)))
2201
2202 ;; Delete the current line (and the next N lines.);
2203 (defmacro gnus-delete-line (&optional n)
2204   `(delete-region (progn (beginning-of-line) (point))
2205                   (progn (forward-line ,(or n 1)) (point))))
2206
2207 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2208 (defvar gnus-init-inhibit nil)
2209 (defun gnus-read-init-file (&optional inhibit-next)
2210   (if gnus-init-inhibit
2211       (setq gnus-init-inhibit nil)
2212     (setq gnus-init-inhibit inhibit-next)
2213     (and gnus-init-file
2214          (or (and (file-exists-p gnus-init-file)
2215                   ;; Don't try to load a directory.
2216                   (not (file-directory-p gnus-init-file)))
2217              (file-exists-p (concat gnus-init-file ".el"))
2218              (file-exists-p (concat gnus-init-file ".elc")))
2219          (condition-case var
2220              (load gnus-init-file nil t)
2221            (error
2222             (error "Error in %s: %s" gnus-init-file var))))))
2223
2224 ;; Info access macros.
2225
2226 (defmacro gnus-info-group (info)
2227   `(nth 0 ,info))
2228 (defmacro gnus-info-rank (info)
2229   `(nth 1 ,info))
2230 (defmacro gnus-info-read (info)
2231   `(nth 2 ,info))
2232 (defmacro gnus-info-marks (info)
2233   `(nth 3 ,info))
2234 (defmacro gnus-info-method (info)
2235   `(nth 4 ,info))
2236 (defmacro gnus-info-params (info)
2237   `(nth 5 ,info))
2238
2239 (defmacro gnus-info-level (info)
2240   `(let ((rank (gnus-info-rank ,info)))
2241      (if (consp rank)
2242          (car rank)
2243        rank)))
2244 (defmacro gnus-info-score (info)
2245   `(let ((rank (gnus-info-rank ,info)))
2246      (or (and (consp rank) (cdr rank)) 0)))
2247
2248 (defmacro gnus-info-set-group (info group)
2249   `(setcar ,info ,group))
2250 (defmacro gnus-info-set-rank (info rank)
2251   `(setcar (nthcdr 1 ,info) ,rank))
2252 (defmacro gnus-info-set-read (info read)
2253   `(setcar (nthcdr 2 ,info) ,read))
2254 (defmacro gnus-info-set-marks (info marks)
2255   `(setcar (nthcdr 3 ,info) ,marks))
2256 (defmacro gnus-info-set-method (info method)
2257   `(setcar (nthcdr 4 ,info) ,method))
2258 (defmacro gnus-info-set-params (info params)
2259   `(setcar (nthcdr 5 ,info) ,params))
2260
2261 (defmacro gnus-info-set-level (info level)
2262   `(let ((rank (cdr ,info)))
2263      (if (consp (car rank))
2264          (setcar (car rank) ,level)
2265        (setcar rank ,level))))
2266 (defmacro gnus-info-set-score (info score)
2267   `(let ((rank (cdr ,info)))
2268      (if (consp (car rank))
2269          (setcdr (car rank) ,score)
2270        (setcar rank (cons (car rank) ,score)))))
2271
2272 (defmacro gnus-get-info (group)
2273   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2274
2275 (defun gnus-byte-code (func)
2276   "Return a form that can be `eval'ed based on FUNC."
2277   (let ((fval (symbol-function func)))
2278     (if (byte-code-function-p fval)
2279         (let ((flist (append fval nil)))
2280           (setcar flist 'byte-code)
2281           flist)
2282       (cons 'progn (cddr fval)))))
2283
2284 ;;; Load the compatability functions.
2285
2286 (require 'gnus-cus)
2287 (require 'gnus-ems)
2288
2289 \f
2290 ;;;
2291 ;;; Shutdown
2292 ;;;
2293
2294 (defvar gnus-shutdown-alist nil)
2295
2296 (defun gnus-add-shutdown (function &rest symbols)
2297   "Run FUNCTION whenever one of SYMBOLS is shut down."
2298   (push (cons function symbols) gnus-shutdown-alist))
2299
2300 (defun gnus-shutdown (symbol)
2301   "Shut down everything that waits for SYMBOL."
2302   (let ((alist gnus-shutdown-alist)
2303         entry)
2304     (while (setq entry (pop alist))
2305       (when (memq symbol (cdr entry))
2306         (funcall (car entry))))))
2307
2308 \f
2309
2310 ;; Format specs.  The chunks below are the machine-generated forms
2311 ;; that are to be evaled as the result of the default format strings.
2312 ;; We write them in here to get them byte-compiled.  That way the
2313 ;; default actions will be quite fast, while still retaining the full
2314 ;; flexibility of the user-defined format specs.
2315
2316 ;; First we have lots of dummy defvars to let the compiler know these
2317 ;; are really dynamic variables.
2318
2319 (defvar gnus-tmp-unread)
2320 (defvar gnus-tmp-replied)
2321 (defvar gnus-tmp-score-char)
2322 (defvar gnus-tmp-indentation)
2323 (defvar gnus-tmp-opening-bracket)
2324 (defvar gnus-tmp-lines)
2325 (defvar gnus-tmp-name)
2326 (defvar gnus-tmp-closing-bracket)
2327 (defvar gnus-tmp-subject-or-nil)
2328 (defvar gnus-tmp-subject)
2329 (defvar gnus-tmp-marked)
2330 (defvar gnus-tmp-marked-mark)
2331 (defvar gnus-tmp-subscribed)
2332 (defvar gnus-tmp-process-marked)
2333 (defvar gnus-tmp-number-of-unread)
2334 (defvar gnus-tmp-group-name)
2335 (defvar gnus-tmp-group)
2336 (defvar gnus-tmp-article-number)
2337 (defvar gnus-tmp-unread-and-unselected)
2338 (defvar gnus-tmp-news-method)
2339 (defvar gnus-tmp-news-server)
2340 (defvar gnus-tmp-article-number)
2341 (defvar gnus-mouse-face)
2342 (defvar gnus-mouse-face-prop)
2343
2344 (defun gnus-summary-line-format-spec ()
2345   (insert gnus-tmp-unread gnus-tmp-replied
2346           gnus-tmp-score-char gnus-tmp-indentation)
2347   (put-text-property
2348    (point)
2349    (progn
2350      (insert
2351       gnus-tmp-opening-bracket
2352       (format "%4d: %-20s"
2353               gnus-tmp-lines
2354               (if (> (length gnus-tmp-name) 20)
2355                   (substring gnus-tmp-name 0 20)
2356                 gnus-tmp-name))
2357       gnus-tmp-closing-bracket)
2358      (point))
2359    gnus-mouse-face-prop gnus-mouse-face)
2360   (insert " " gnus-tmp-subject-or-nil "\n"))
2361
2362 (defvar gnus-summary-line-format-spec
2363   (gnus-byte-code 'gnus-summary-line-format-spec))
2364
2365 (defun gnus-summary-dummy-line-format-spec ()
2366   (insert "*  ")
2367   (put-text-property
2368    (point)
2369    (progn
2370      (insert ":                          :")
2371      (point))
2372    gnus-mouse-face-prop gnus-mouse-face)
2373   (insert " " gnus-tmp-subject "\n"))
2374
2375 (defvar gnus-summary-dummy-line-format-spec
2376   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2377
2378 (defun gnus-group-line-format-spec ()
2379   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2380           gnus-tmp-process-marked
2381           gnus-group-indentation
2382           (format "%5s: " gnus-tmp-number-of-unread))
2383   (put-text-property
2384    (point)
2385    (progn
2386      (insert gnus-tmp-group "\n")
2387      (1- (point)))
2388    gnus-mouse-face-prop gnus-mouse-face))
2389 (defvar gnus-group-line-format-spec
2390   (gnus-byte-code 'gnus-group-line-format-spec))
2391
2392 (defvar gnus-format-specs
2393   `((version . ,emacs-version)
2394     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2395     (summary-dummy ,gnus-summary-dummy-line-format
2396                    ,gnus-summary-dummy-line-format-spec)
2397     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2398
2399 (defvar gnus-article-mode-line-format-spec nil)
2400 (defvar gnus-summary-mode-line-format-spec nil)
2401 (defvar gnus-group-mode-line-format-spec nil)
2402
2403 ;;; Phew.  All that gruft is over, fortunately.
2404
2405 \f
2406 ;;;
2407 ;;; Gnus Utility Functions
2408 ;;;
2409
2410 (defun gnus-extract-address-components (from)
2411   (let (name address)
2412     ;; First find the address - the thing with the @ in it.  This may
2413     ;; not be accurate in mail addresses, but does the trick most of
2414     ;; the time in news messages.
2415     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2416         (setq address (substring from (match-beginning 0) (match-end 0))))
2417     ;; Then we check whether the "name <address>" format is used.
2418     (and address
2419          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2420          ;; Linear white space is not required.
2421          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2422          (and (setq name (substring from 0 (match-beginning 0)))
2423               ;; Strip any quotes from the name.
2424               (string-match "\".*\"" name)
2425               (setq name (substring name 1 (1- (match-end 0))))))
2426     ;; If not, then "address (name)" is used.
2427     (or name
2428         (and (string-match "(.+)" from)
2429              (setq name (substring from (1+ (match-beginning 0))
2430                                    (1- (match-end 0)))))
2431         (and (string-match "()" from)
2432              (setq name address))
2433         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2434         ;; XOVER might not support folded From headers.
2435         (and (string-match "(.*" from)
2436              (setq name (substring from (1+ (match-beginning 0))
2437                                    (match-end 0)))))
2438     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2439     (list (or name from) (or address from))))
2440
2441 (defun gnus-fetch-field (field)
2442   "Return the value of the header FIELD of current article."
2443   (save-excursion
2444     (save-restriction
2445       (let ((case-fold-search t)
2446             (inhibit-point-motion-hooks t))
2447         (nnheader-narrow-to-headers)
2448         (mail-fetch-field field)))))
2449
2450 (defun gnus-goto-colon ()
2451   (beginning-of-line)
2452   (search-forward ":" (gnus-point-at-eol) t))
2453
2454 ;;;###autoload
2455 (defun gnus-update-format (var)
2456   "Update the format specification near point."
2457   (interactive
2458    (list
2459     (save-excursion
2460       (eval-defun nil)
2461       ;; Find the end of the current word.
2462       (re-search-forward "[ \t\n]" nil t)
2463       ;; Search backward.
2464       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2465         (match-string 1)))))
2466   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2467                               (match-string 1 var))))
2468          (entry (assq type gnus-format-specs))
2469          value spec)
2470     (when entry
2471       (setq gnus-format-specs (delq entry gnus-format-specs)))
2472     (set
2473      (intern (format "%s-spec" var))
2474      (gnus-parse-format (setq value (symbol-value (intern var)))
2475                         (symbol-value (intern (format "%s-alist" var)))
2476                         (not (string-match "mode" var))))
2477     (setq spec (symbol-value (intern (format "%s-spec" var))))
2478     (push (list type value spec) gnus-format-specs)
2479
2480     (pop-to-buffer "*Gnus Format*")
2481     (erase-buffer)
2482     (lisp-interaction-mode)
2483     (insert (pp-to-string spec))))
2484
2485 (defun gnus-update-format-specifications (&optional force)
2486   "Update all (necessary) format specifications."
2487   ;; Make the indentation array.
2488   (gnus-make-thread-indent-array)
2489
2490   ;; See whether all the stored info needs to be flushed.
2491   (when (or force
2492             (not (equal emacs-version
2493                         (cdr (assq 'version gnus-format-specs)))))
2494     (setq gnus-format-specs nil))
2495
2496   ;; Go through all the formats and see whether they need updating.
2497   (let ((types '(summary summary-dummy group
2498                          summary-mode group-mode article-mode))
2499         new-format entry type val)
2500     (while (setq type (pop types))
2501       ;; Jump to the proper buffer to find out the value of
2502       ;; the variable, if possible.  (It may be buffer-local.)
2503       (save-excursion
2504         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2505               val)
2506           (when (and (boundp buffer)
2507                      (setq val (symbol-value buffer))
2508                      (get-buffer val)
2509                      (buffer-name (get-buffer val)))
2510             (set-buffer (get-buffer val)))
2511           (setq new-format (symbol-value
2512                             (intern (format "gnus-%s-line-format" type))))))
2513       (setq entry (cdr (assq type gnus-format-specs)))
2514       (if (and entry
2515                (equal (car entry) new-format))
2516           ;; Use the old format.
2517           (set (intern (format "gnus-%s-line-format-spec" type))
2518                (cadr entry))
2519         ;; This is a new format.
2520         (setq val
2521               (if (not (stringp new-format))
2522                   ;; This is a function call or something.
2523                   new-format
2524                 ;; This is a "real" format.
2525                 (gnus-parse-format
2526                  new-format
2527                  (symbol-value
2528                   (intern (format "gnus-%s-line-format-alist"
2529                                   (if (eq type 'article-mode)
2530                                       'summary-mode type))))
2531                  (not (string-match "mode$" (symbol-name type))))))
2532         ;; Enter the new format spec into the list.
2533         (if entry
2534             (progn
2535               (setcar (cdr entry) val)
2536               (setcar entry new-format))
2537           (push (list type new-format val) gnus-format-specs))
2538         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2539
2540   (unless (assq 'version gnus-format-specs)
2541     (push (cons 'version emacs-version) gnus-format-specs))
2542
2543   (gnus-update-group-mark-positions)
2544   (gnus-update-summary-mark-positions))
2545
2546 (defun gnus-update-summary-mark-positions ()
2547   "Compute where the summary marks are to go."
2548   (save-excursion
2549     (when (and gnus-summary-buffer
2550                (get-buffer gnus-summary-buffer)
2551                (buffer-name (get-buffer gnus-summary-buffer)))
2552       (set-buffer gnus-summary-buffer))
2553     (let ((gnus-replied-mark 129)
2554           (gnus-score-below-mark 130)
2555           (gnus-score-over-mark 130)
2556           (thread nil)
2557           (gnus-visual nil)
2558           (spec gnus-summary-line-format-spec)
2559           pos)
2560       (save-excursion
2561         (gnus-set-work-buffer)
2562         (let ((gnus-summary-line-format-spec spec))
2563           (gnus-summary-insert-line
2564            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2565           (goto-char (point-min))
2566           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2567                                              (- (point) 2)))))
2568           (goto-char (point-min))
2569           (push (cons 'replied (and (search-forward "\201" nil t) 
2570                                     (- (point) 2)))
2571                 pos)
2572           (goto-char (point-min))
2573           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2574                 pos)))
2575       (setq gnus-summary-mark-positions pos))))
2576
2577 (defun gnus-update-group-mark-positions ()
2578   (save-excursion
2579     (let ((gnus-process-mark 128)
2580           (gnus-group-marked '("dummy.group")))
2581       (gnus-set-active "dummy.group" '(0 . 0))
2582       (gnus-set-work-buffer)
2583       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2584       (goto-char (point-min))
2585       (setq gnus-group-mark-positions
2586             (list (cons 'process (and (search-forward "\200" nil t)
2587                                       (- (point) 2))))))))
2588
2589 (defvar gnus-mouse-face-0 'highlight)
2590 (defvar gnus-mouse-face-1 'highlight)
2591 (defvar gnus-mouse-face-2 'highlight)
2592 (defvar gnus-mouse-face-3 'highlight)
2593 (defvar gnus-mouse-face-4 'highlight)
2594
2595 (defun gnus-mouse-face-function (form type)
2596   `(put-text-property
2597     (point) (progn ,@form (point))
2598     gnus-mouse-face-prop
2599     ,(if (equal type 0)
2600          'gnus-mouse-face
2601        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2602
2603 (defvar gnus-face-0 'bold)
2604 (defvar gnus-face-1 'italic)
2605 (defvar gnus-face-2 'bold-italic)
2606 (defvar gnus-face-3 'bold)
2607 (defvar gnus-face-4 'bold)
2608
2609 (defun gnus-face-face-function (form type)
2610   `(put-text-property
2611     (point) (progn ,@form (point))
2612     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2613
2614 (defun gnus-max-width-function (el max-width)
2615   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2616   (if (symbolp el)
2617       `(if (> (length ,el) ,max-width)
2618            (substring ,el 0 ,max-width)
2619          ,el)
2620     `(let ((val (eval ,el)))
2621        (if (numberp val)
2622            (setq val (int-to-string val)))
2623        (if (> (length val) ,max-width)
2624            (substring val 0 ,max-width)
2625          val))))
2626
2627 (defun gnus-parse-format (format spec-alist &optional insert)
2628   ;; This function parses the FORMAT string with the help of the
2629   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2630   ;; string.  If the FORMAT string contains the specifiers %( and %)
2631   ;; the text between them will have the mouse-face text property.
2632   (if (string-match
2633        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2634        format)
2635       (gnus-parse-complex-format format spec-alist)
2636     ;; This is a simple format.
2637     (gnus-parse-simple-format format spec-alist insert)))
2638
2639 (defun gnus-parse-complex-format (format spec-alist)
2640   (save-excursion
2641     (gnus-set-work-buffer)
2642     (insert format)
2643     (goto-char (point-min))
2644     (while (re-search-forward "\"" nil t)
2645       (replace-match "\\\"" nil t))
2646     (goto-char (point-min))
2647     (insert "(\"")
2648     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2649       (let ((number (if (match-beginning 1)
2650                         (match-string 1) "0"))
2651             (delim (aref (match-string 2) 0)))
2652         (if (or (= delim ?\() (= delim ?\{))
2653             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2654                                    " " number " \""))
2655           (replace-match "\")\""))))
2656     (goto-char (point-max))
2657     (insert "\")")
2658     (goto-char (point-min))
2659     (let ((form (read (current-buffer))))
2660       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2661
2662 (defun gnus-complex-form-to-spec (form spec-alist)
2663   (delq nil
2664         (mapcar
2665          (lambda (sform)
2666            (if (stringp sform)
2667                (gnus-parse-simple-format sform spec-alist t)
2668              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2669                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2670                       (nth 1 sform))))
2671          form)))
2672
2673 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2674   ;; This function parses the FORMAT string with the help of the
2675   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2676   ;; string.
2677   (let ((max-width 0)
2678         spec flist fstring newspec elem beg result dontinsert)
2679     (save-excursion
2680       (gnus-set-work-buffer)
2681       (insert format)
2682       (goto-char (point-min))
2683       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2684                                 nil t)
2685         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2686               (setq newspec "%"
2687                     beg (1+ (match-beginning 0)))
2688           ;; First check if there are any specs that look anything like
2689           ;; "%12,12A", ie. with a "max width specification".  These have
2690           ;; to be treated specially.
2691           (if (setq beg (match-beginning 1))
2692               (setq max-width
2693                     (string-to-int
2694                      (buffer-substring
2695                       (1+ (match-beginning 1)) (match-end 1))))
2696             (setq max-width 0)
2697             (setq beg (match-beginning 2)))
2698           ;; Find the specification from `spec-alist'.
2699           (unless (setq elem (cdr (assq spec spec-alist)))
2700             (setq elem '("*" ?s)))
2701           ;; Treat user defined format specifiers specially.
2702           (when (eq (car elem) 'gnus-tmp-user-defined)
2703             (setq elem
2704                   (list
2705                    (list (intern (concat "gnus-user-format-function-"
2706                                          (match-string 3)))
2707                          'gnus-tmp-header) ?s))
2708             (delete-region (match-beginning 3) (match-end 3)))
2709           (if (not (zerop max-width))
2710               (let ((el (car elem)))
2711                 (cond ((= (cadr elem) ?c)
2712                        (setq el (list 'char-to-string el)))
2713                       ((= (cadr elem) ?d)
2714                        (setq el (list 'int-to-string el))))
2715                 (setq flist (cons (gnus-max-width-function el max-width)
2716                                   flist))
2717                 (setq newspec ?s))
2718             (progn
2719               (setq flist (cons (car elem) flist))
2720               (setq newspec (cadr elem)))))
2721         ;; Remove the old specification (and possibly a ",12" string).
2722         (delete-region beg (match-end 2))
2723         ;; Insert the new specification.
2724         (goto-char beg)
2725         (insert newspec))
2726       (setq fstring (buffer-substring 1 (point-max))))
2727     ;; Do some postprocessing to increase efficiency.
2728     (setq
2729      result
2730      (cond
2731       ;; Emptyness.
2732       ((string= fstring "")
2733        nil)
2734       ;; Not a format string.
2735       ((not (string-match "%" fstring))
2736        (list fstring))
2737       ;; A format string with just a single string spec.
2738       ((string= fstring "%s")
2739        (list (car flist)))
2740       ;; A single character.
2741       ((string= fstring "%c")
2742        (list (car flist)))
2743       ;; A single number.
2744       ((string= fstring "%d")
2745        (setq dontinsert)
2746        (if insert
2747            (list `(princ ,(car flist)))
2748          (list `(int-to-string ,(car flist)))))
2749       ;; Just lots of chars and strings.
2750       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2751        (nreverse flist))
2752       ;; A single string spec at the beginning of the spec.
2753       ((string-match "\\`%[sc][^%]+\\'" fstring)
2754        (list (car flist) (substring fstring 2)))
2755       ;; A single string spec in the middle of the spec.
2756       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2757        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2758       ;; A single string spec in the end of the spec.
2759       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2760        (list (match-string 1 fstring) (car flist)))
2761       ;; A more complex spec.
2762       (t
2763        (list (cons 'format (cons fstring (nreverse flist)))))))
2764
2765     (if insert
2766         (when result
2767           (if dontinsert
2768               result
2769             (cons 'insert result)))
2770       (cond ((stringp result)
2771              result)
2772             ((consp result)
2773              (cons 'concat result))
2774             (t "")))))
2775
2776 (defun gnus-eval-format (format &optional alist props)
2777   "Eval the format variable FORMAT, using ALIST.
2778 If PROPS, insert the result."
2779   (let ((form (gnus-parse-format format alist props)))
2780     (if props
2781         (add-text-properties (point) (progn (eval form) (point)) props)
2782       (eval form))))
2783
2784 (defun gnus-remove-text-with-property (prop)
2785   "Delete all text in the current buffer with text property PROP."
2786   (save-excursion
2787     (goto-char (point-min))
2788     (while (not (eobp))
2789       (while (get-text-property (point) prop)
2790         (delete-char 1))
2791       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2792
2793 (defun gnus-set-work-buffer ()
2794   (if (get-buffer gnus-work-buffer)
2795       (progn
2796         (set-buffer gnus-work-buffer)
2797         (erase-buffer))
2798     (set-buffer (get-buffer-create gnus-work-buffer))
2799     (kill-all-local-variables)
2800     (buffer-disable-undo (current-buffer))
2801     (gnus-add-current-to-buffer-list)))
2802
2803 ;; Article file names when saving.
2804
2805 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2806   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2807 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2808 Otherwise, it is like ~/News/news/group/num."
2809   (let ((default
2810           (expand-file-name
2811            (concat (if (gnus-use-long-file-name 'not-save)
2812                        (gnus-capitalize-newsgroup newsgroup)
2813                      (gnus-newsgroup-directory-form newsgroup))
2814                    "/" (int-to-string (mail-header-number headers)))
2815            (or gnus-article-save-directory "~/News"))))
2816     (if (and last-file
2817              (string-equal (file-name-directory default)
2818                            (file-name-directory last-file))
2819              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2820         default
2821       (or last-file default))))
2822
2823 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2824   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2825 If variable `gnus-use-long-file-name' is non-nil, it is
2826 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2827   (let ((default
2828           (expand-file-name
2829            (concat (if (gnus-use-long-file-name 'not-save)
2830                        newsgroup
2831                      (gnus-newsgroup-directory-form newsgroup))
2832                    "/" (int-to-string (mail-header-number headers)))
2833            (or gnus-article-save-directory "~/News"))))
2834     (if (and last-file
2835              (string-equal (file-name-directory default)
2836                            (file-name-directory last-file))
2837              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2838         default
2839       (or last-file default))))
2840
2841 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2842   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2843 If variable `gnus-use-long-file-name' is non-nil, it is
2844 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2845   (or last-file
2846       (expand-file-name
2847        (if (gnus-use-long-file-name 'not-save)
2848            (gnus-capitalize-newsgroup newsgroup)
2849          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2850        (or gnus-article-save-directory "~/News"))))
2851
2852 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2853   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2854 If variable `gnus-use-long-file-name' is non-nil, it is
2855 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2856   (or last-file
2857       (expand-file-name
2858        (if (gnus-use-long-file-name 'not-save)
2859            newsgroup
2860          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2861        (or gnus-article-save-directory "~/News"))))
2862
2863 ;; For subscribing new newsgroup
2864
2865 (defun gnus-subscribe-hierarchical-interactive (groups)
2866   (let ((groups (sort groups 'string<))
2867         prefixes prefix start ans group starts)
2868     (while groups
2869       (setq prefixes (list "^"))
2870       (while (and groups prefixes)
2871         (while (not (string-match (car prefixes) (car groups)))
2872           (setq prefixes (cdr prefixes)))
2873         (setq prefix (car prefixes))
2874         (setq start (1- (length prefix)))
2875         (if (and (string-match "[^\\.]\\." (car groups) start)
2876                  (cdr groups)
2877                  (setq prefix
2878                        (concat "^" (substring (car groups) 0 (match-end 0))))
2879                  (string-match prefix (cadr groups)))
2880             (progn
2881               (setq prefixes (cons prefix prefixes))
2882               (message "Descend hierarchy %s? ([y]nsq): "
2883                        (substring prefix 1 (1- (length prefix))))
2884               (setq ans (read-char))
2885               (cond ((= ans ?n)
2886                      (while (and groups
2887                                  (string-match prefix
2888                                                (setq group (car groups))))
2889                        (setq gnus-killed-list
2890                              (cons group gnus-killed-list))
2891                        (gnus-sethash group group gnus-killed-hashtb)
2892                        (setq groups (cdr groups)))
2893                      (setq starts (cdr starts)))
2894                     ((= ans ?s)
2895                      (while (and groups
2896                                  (string-match prefix
2897                                                (setq group (car groups))))
2898                        (gnus-sethash group group gnus-killed-hashtb)
2899                        (gnus-subscribe-alphabetically (car groups))
2900                        (setq groups (cdr groups)))
2901                      (setq starts (cdr starts)))
2902                     ((= ans ?q)
2903                      (while groups
2904                        (setq group (car groups))
2905                        (setq gnus-killed-list (cons group gnus-killed-list))
2906                        (gnus-sethash group group gnus-killed-hashtb)
2907                        (setq groups (cdr groups))))
2908                     (t nil)))
2909           (message "Subscribe %s? ([n]yq)" (car groups))
2910           (setq ans (read-char))
2911           (setq group (car groups))
2912           (cond ((= ans ?y)
2913                  (gnus-subscribe-alphabetically (car groups))
2914                  (gnus-sethash group group gnus-killed-hashtb))
2915                 ((= ans ?q)
2916                  (while groups
2917                    (setq group (car groups))
2918                    (setq gnus-killed-list (cons group gnus-killed-list))
2919                    (gnus-sethash group group gnus-killed-hashtb)
2920                    (setq groups (cdr groups))))
2921                 (t
2922                  (setq gnus-killed-list (cons group gnus-killed-list))
2923                  (gnus-sethash group group gnus-killed-hashtb)))
2924           (setq groups (cdr groups)))))))
2925
2926 (defun gnus-subscribe-randomly (newsgroup)
2927   "Subscribe new NEWSGROUP by making it the first newsgroup."
2928   (gnus-subscribe-newsgroup newsgroup))
2929
2930 (defun gnus-subscribe-alphabetically (newgroup)
2931   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2932   (let ((groups (cdr gnus-newsrc-alist))
2933         before)
2934     (while (and (not before) groups)
2935       (if (string< newgroup (caar groups))
2936           (setq before (caar groups))
2937         (setq groups (cdr groups))))
2938     (gnus-subscribe-newsgroup newgroup before)))
2939
2940 (defun gnus-subscribe-hierarchically (newgroup)
2941   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2942   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2943   (save-excursion
2944     (set-buffer (find-file-noselect gnus-current-startup-file))
2945     (let ((groupkey newgroup)
2946           before)
2947       (while (and (not before) groupkey)
2948         (goto-char (point-min))
2949         (let ((groupkey-re
2950                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2951           (while (and (re-search-forward groupkey-re nil t)
2952                       (progn
2953                         (setq before (match-string 1))
2954                         (string< before newgroup)))))
2955         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2956         (setq groupkey
2957               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2958                   (substring groupkey (match-beginning 1) (match-end 1)))))
2959       (gnus-subscribe-newsgroup newgroup before))))
2960
2961 (defun gnus-subscribe-interactively (group)
2962   "Subscribe the new GROUP interactively.
2963 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2964 it is killed."
2965   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2966       (gnus-subscribe-hierarchically group)
2967     (push group gnus-killed-list)))
2968
2969 (defun gnus-subscribe-zombies (group)
2970   "Make the new GROUP into a zombie group."
2971   (push group gnus-zombie-list))
2972
2973 (defun gnus-subscribe-killed (group)
2974   "Make the new GROUP a killed group."
2975   (push group gnus-killed-list))
2976
2977 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2978   "Subscribe new NEWSGROUP.
2979 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2980 the first newsgroup."
2981   ;; We subscribe the group by changing its level to `subscribed'.
2982   (gnus-group-change-level
2983    newsgroup gnus-level-default-subscribed
2984    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2985   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2986
2987 ;; For directories
2988
2989 (defun gnus-newsgroup-directory-form (newsgroup)
2990   "Make hierarchical directory name from NEWSGROUP name."
2991   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2992         (len (length newsgroup))
2993         idx)
2994     ;; If this is a foreign group, we don't want to translate the
2995     ;; entire name.
2996     (if (setq idx (string-match ":" newsgroup))
2997         (aset newsgroup idx ?/)
2998       (setq idx 0))
2999     ;; Replace all occurrences of `.' with `/'.
3000     (while (< idx len)
3001       (if (= (aref newsgroup idx) ?.)
3002           (aset newsgroup idx ?/))
3003       (setq idx (1+ idx)))
3004     newsgroup))
3005
3006 (defun gnus-newsgroup-savable-name (group)
3007   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3008   ;; with dots.
3009   (nnheader-replace-chars-in-string group ?/ ?.))
3010
3011 (defun gnus-make-directory (dir)
3012   "Make DIRECTORY recursively."
3013   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3014   ;; of the many mysteries of the universe.
3015   (let* ((dir (expand-file-name dir default-directory))
3016          dirs err)
3017     (if (string-match "/$" dir)
3018         (setq dir (substring dir 0 (match-beginning 0))))
3019     ;; First go down the path until we find a directory that exists.
3020     (while (not (file-exists-p dir))
3021       (setq dirs (cons dir dirs))
3022       (string-match "/[^/]+$" dir)
3023       (setq dir (substring dir 0 (match-beginning 0))))
3024     ;; Then create all the subdirs.
3025     (while (and dirs (not err))
3026       (condition-case ()
3027           (make-directory (car dirs))
3028         (error (setq err t)))
3029       (setq dirs (cdr dirs)))
3030     ;; We return whether we were successful or not.
3031     (not dirs)))
3032
3033 (defun gnus-capitalize-newsgroup (newsgroup)
3034   "Capitalize NEWSGROUP name."
3035   (and (not (zerop (length newsgroup)))
3036        (concat (char-to-string (upcase (aref newsgroup 0)))
3037                (substring newsgroup 1))))
3038
3039 ;; Various... things.
3040
3041 (defun gnus-simplify-subject (subject &optional re-only)
3042   "Remove `Re:' and words in parentheses.
3043 If RE-ONLY is non-nil, strip leading `Re:'s only."
3044   (let ((case-fold-search t))           ;Ignore case.
3045     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3046     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3047       (setq subject (substring subject (match-end 0))))
3048     ;; Remove uninteresting prefixes.
3049     (if (and (not re-only)
3050              gnus-simplify-ignored-prefixes
3051              (string-match gnus-simplify-ignored-prefixes subject))
3052         (setq subject (substring subject (match-end 0))))
3053     ;; Remove words in parentheses from end.
3054     (unless re-only
3055       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3056         (setq subject (substring subject 0 (match-beginning 0)))))
3057     ;; Return subject string.
3058     subject))
3059
3060 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3061 ;; all whitespace.
3062 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3063 (defun gnus-simplify-buffer-fuzzy ()
3064   (goto-char (point-min))
3065   (while (search-forward "\t" nil t)
3066     (replace-match " " t t))
3067   (goto-char (point-min))
3068   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3069   (goto-char (match-beginning 0))
3070   (while (or
3071           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3072           (looking-at "^[[].*: .*[]]$"))
3073     (goto-char (point-min))
3074     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3075                               nil t)
3076       (replace-match "" t t))
3077     (goto-char (point-min))
3078     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3079       (goto-char (match-end 0))
3080       (delete-char -1)
3081       (delete-region
3082        (progn (goto-char (match-beginning 0)))
3083        (re-search-forward ":"))))
3084   (goto-char (point-min))
3085   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3086     (replace-match "" t t))
3087   (goto-char (point-min))
3088   (while (re-search-forward "  +" nil t)
3089     (replace-match " " t t))
3090   (goto-char (point-min))
3091   (while (re-search-forward " $" nil t)
3092     (replace-match "" t t))
3093   (goto-char (point-min))
3094   (while (re-search-forward "^ +" nil t)
3095     (replace-match "" t t))
3096   (goto-char (point-min))
3097   (when gnus-simplify-subject-fuzzy-regexp
3098     (if (listp gnus-simplify-subject-fuzzy-regexp)
3099         (let ((list gnus-simplify-subject-fuzzy-regexp))
3100           (while list
3101             (goto-char (point-min))
3102             (while (re-search-forward (car list) nil t)
3103               (replace-match "" t t))
3104             (setq list (cdr list))))
3105       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3106         (replace-match "" t t)))))
3107
3108 (defun gnus-simplify-subject-fuzzy (subject)
3109   "Siplify a subject string fuzzily."
3110   (save-excursion
3111     (gnus-set-work-buffer)
3112     (let ((case-fold-search t))
3113       (insert subject)
3114       (inline (gnus-simplify-buffer-fuzzy))
3115       (buffer-string))))
3116
3117 ;; Add the current buffer to the list of buffers to be killed on exit.
3118 (defun gnus-add-current-to-buffer-list ()
3119   (or (memq (current-buffer) gnus-buffer-list)
3120       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3121
3122 (defun gnus-string> (s1 s2)
3123   (not (or (string< s1 s2)
3124            (string= s1 s2))))
3125
3126 ;;; General various misc type functions.
3127
3128 (defun gnus-clear-system ()
3129   "Clear all variables and buffers."
3130   ;; Clear Gnus variables.
3131   (let ((variables gnus-variable-list))
3132     (while variables
3133       (set (car variables) nil)
3134       (setq variables (cdr variables))))
3135   ;; Clear other internal variables.
3136   (setq gnus-list-of-killed-groups nil
3137         gnus-have-read-active-file nil
3138         gnus-newsrc-alist nil
3139         gnus-newsrc-hashtb nil
3140         gnus-killed-list nil
3141         gnus-zombie-list nil
3142         gnus-killed-hashtb nil
3143         gnus-active-hashtb nil
3144         gnus-moderated-list nil
3145         gnus-description-hashtb nil
3146         gnus-current-headers nil
3147         gnus-thread-indent-array nil
3148         gnus-newsgroup-headers nil
3149         gnus-newsgroup-name nil
3150         gnus-server-alist nil
3151         gnus-group-list-mode nil
3152         gnus-opened-servers nil
3153         gnus-current-select-method nil)
3154   (gnus-shutdown 'gnus)
3155   ;; Kill the startup file.
3156   (and gnus-current-startup-file
3157        (get-file-buffer gnus-current-startup-file)
3158        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3159   ;; Clear the dribble buffer.
3160   (gnus-dribble-clear)
3161   ;; Kill global KILL file buffer.
3162   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3163     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3164   (gnus-kill-buffer nntp-server-buffer)
3165   ;; Kill Gnus buffers.
3166   (while gnus-buffer-list
3167     (gnus-kill-buffer (pop gnus-buffer-list)))
3168   ;; Remove Gnus frames.
3169   (while gnus-created-frames
3170     (when (frame-live-p (car gnus-created-frames))
3171       ;; We slap a condition-case around this `delete-frame' to ensure 
3172       ;; agains errors if we try do delete the single frame that's left.
3173       (condition-case ()
3174           (delete-frame (car gnus-created-frames))
3175         (error nil)))
3176     (pop gnus-created-frames)))
3177
3178 (defun gnus-windows-old-to-new (setting)
3179   ;; First we take care of the really, really old Gnus 3 actions.
3180   (when (symbolp setting)
3181     (setq setting
3182           ;; Take care of ooold GNUS 3.x values.
3183           (cond ((eq setting 'SelectArticle) 'article)
3184                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3185                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3186                 (t setting))))
3187   (if (or (listp setting)
3188           (not (and gnus-window-configuration
3189                     (memq setting '(group summary article)))))
3190       setting
3191     (let* ((setting (if (eq setting 'group)
3192                         (if (assq 'newsgroup gnus-window-configuration)
3193                             'newsgroup
3194                           'newsgroups) setting))
3195            (elem (cadr (assq setting gnus-window-configuration)))
3196            (total (apply '+ elem))
3197            (types '(group summary article))
3198            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3199            (i 0)
3200            perc
3201            out)
3202       (while (< i 3)
3203         (or (not (numberp (nth i elem)))
3204             (zerop (nth i elem))
3205             (progn
3206               (setq perc  (/ (float (nth 0 elem)) total))
3207               (setq out (cons (if (eq pbuf (nth i types))
3208                                   (vector (nth i types) perc 'point)
3209                                 (vector (nth i types) perc))
3210                               out))))
3211         (setq i (1+ i)))
3212       (list (nreverse out)))))
3213
3214 ;;;###autoload
3215 (defun gnus-add-configuration (conf)
3216   "Add the window configuration CONF to `gnus-buffer-configuration'."
3217   (setq gnus-buffer-configuration
3218         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3219                          gnus-buffer-configuration))))
3220
3221 (defvar gnus-frame-list nil)
3222
3223 (defun gnus-configure-frame (split &optional window)
3224   "Split WINDOW according to SPLIT."
3225   (unless window
3226     (setq window (get-buffer-window (current-buffer))))
3227   (select-window window)
3228   ;; This might be an old-stylee buffer config.
3229   (when (vectorp split)
3230     (setq split (append split nil)))
3231   (when (or (consp (car split))
3232             (vectorp (car split)))
3233     (push 1.0 split)
3234     (push 'vertical split))
3235   ;; The SPLIT might be something that is to be evaled to
3236   ;; return a new SPLIT.
3237   (while (and (not (assq (car split) gnus-window-to-buffer))
3238               (gnus-functionp (car split)))
3239     (setq split (eval split)))
3240   (let* ((type (car split))
3241          (subs (cddr split))
3242          (len (if (eq type 'horizontal) (window-width) (window-height)))
3243          (total 0)
3244          (window-min-width (or gnus-window-min-width window-min-width))
3245          (window-min-height (or gnus-window-min-height window-min-height))
3246          s result new-win rest comp-subs size sub)
3247     (cond
3248      ;; Nothing to do here.
3249      ((null split))
3250      ;; Don't switch buffers.
3251      ((null type)
3252       (and (memq 'point split) window))
3253      ;; This is a buffer to be selected.
3254      ((not (memq type '(frame horizontal vertical)))
3255       (let ((buffer (cond ((stringp type) type)
3256                           (t (cdr (assq type gnus-window-to-buffer)))))
3257             buf)
3258         (unless buffer
3259           (error "Illegal buffer type: %s" type))
3260         (unless (setq buf (get-buffer (if (symbolp buffer)
3261                                           (symbol-value buffer) buffer)))
3262           (setq buf (get-buffer-create (if (symbolp buffer)
3263                                            (symbol-value buffer) buffer))))
3264         (switch-to-buffer buf)
3265         ;; We return the window if it has the `point' spec.
3266         (and (memq 'point split) window)))
3267      ;; This is a frame split.
3268      ((eq type 'frame)
3269       (unless gnus-frame-list
3270         (setq gnus-frame-list (list (window-frame
3271                                      (get-buffer-window (current-buffer))))))
3272       (let ((i 0)
3273             params frame fresult)
3274         (while (< i (length subs))
3275           ;; Frame parameter is gotten from the sub-split.
3276           (setq params (cadr (elt subs i)))
3277           ;; It should be a list.
3278           (unless (listp params)
3279             (setq params nil))
3280           ;; Create a new frame?
3281           (unless (setq frame (elt gnus-frame-list i))
3282             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3283             (push frame gnus-created-frames))
3284           ;; Is the old frame still alive?
3285           (unless (frame-live-p frame)
3286             (setcar (nthcdr i gnus-frame-list)
3287                     (setq frame (make-frame params))))
3288           ;; Select the frame in question and do more splits there.
3289           (select-frame frame)
3290           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3291           (incf i))
3292         ;; Select the frame that has the selected buffer.
3293         (when fresult
3294           (select-frame (window-frame fresult)))))
3295      ;; This is a normal split.
3296      (t
3297       (when (> (length subs) 0)
3298         ;; First we have to compute the sizes of all new windows.
3299         (while subs
3300           (setq sub (append (pop subs) nil))
3301           (while (and (not (assq (car sub) gnus-window-to-buffer))
3302                       (gnus-functionp (car sub)))
3303             (setq sub (eval sub)))
3304           (when sub
3305             (push sub comp-subs)
3306             (setq size (cadar comp-subs))
3307             (cond ((equal size 1.0)
3308                    (setq rest (car comp-subs))
3309                    (setq s 0))
3310                   ((floatp size)
3311                    (setq s (floor (* size len))))
3312                   ((integerp size)
3313                    (setq s size))
3314                   (t
3315                    (error "Illegal size: %s" size)))
3316             ;; Try to make sure that we are inside the safe limits.
3317             (cond ((zerop s))
3318                   ((eq type 'horizontal)
3319                    (setq s (max s window-min-width)))
3320                   ((eq type 'vertical)
3321                    (setq s (max s window-min-height))))
3322             (setcar (cdar comp-subs) s)
3323             (incf total s)))
3324         ;; Take care of the "1.0" spec.
3325         (if rest
3326             (setcar (cdr rest) (- len total))
3327           (error "No 1.0 specs in %s" split))
3328         ;; The we do the actual splitting in a nice recursive
3329         ;; fashion.
3330         (setq comp-subs (nreverse comp-subs))
3331         (while comp-subs
3332           (if (null (cdr comp-subs))
3333               (setq new-win window)
3334             (setq new-win
3335                   (split-window window (cadar comp-subs)
3336                                 (eq type 'horizontal))))
3337           (setq result (or (gnus-configure-frame
3338                             (car comp-subs) window) result))
3339           (select-window new-win)
3340           (setq window new-win)
3341           (setq comp-subs (cdr comp-subs))))
3342       ;; Return the proper window, if any.
3343       (when result
3344         (select-window result))))))
3345
3346 (defvar gnus-frame-split-p nil)
3347
3348 (defun gnus-configure-windows (setting &optional force)
3349   (setq setting (gnus-windows-old-to-new setting))
3350   (let ((split (if (symbolp setting)
3351                    (cadr (assq setting gnus-buffer-configuration))
3352                  setting))
3353         all-visible)
3354
3355     (setq gnus-frame-split-p nil)
3356
3357     (unless split
3358       (error "No such setting: %s" setting))
3359
3360     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3361              (not force))
3362         ;; All the windows mentioned are already visible, so we just
3363         ;; put point in the assigned buffer, and do not touch the
3364         ;; winconf.
3365         (select-window all-visible)
3366
3367       ;; Either remove all windows or just remove all Gnus windows.
3368       (let ((frame (selected-frame)))
3369         (unwind-protect
3370             (if gnus-use-full-window
3371                 ;; We want to remove all other windows.
3372                 (if (not gnus-frame-split-p)
3373                     ;; This is not a `frame' split, so we ignore the
3374                     ;; other frames.  
3375                     (delete-other-windows)
3376                   ;; This is a `frame' split, so we delete all windows
3377                   ;; on all frames.
3378                   (mapcar 
3379                    (lambda (frame)
3380                      (unless (eq (cdr (assq 'minibuffer
3381                                             (frame-parameters frame)))
3382                                  'only)
3383                        (select-frame frame)
3384                        (delete-other-windows)))
3385                    (frame-list)))
3386               ;; Just remove some windows.
3387               (gnus-remove-some-windows)
3388               (switch-to-buffer nntp-server-buffer))
3389           (select-frame frame)))
3390
3391       (switch-to-buffer nntp-server-buffer)
3392       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3393
3394 (defun gnus-all-windows-visible-p (split)
3395   "Say whether all buffers in SPLIT are currently visible.
3396 In particular, the value returned will be the window that
3397 should have point."
3398   (let ((stack (list split))
3399         (all-visible t)
3400         type buffer win buf)
3401     (while (and (setq split (pop stack))
3402                 all-visible)
3403       ;; Be backwards compatible.
3404       (when (vectorp split)
3405         (setq split (append split nil)))
3406       (when (or (consp (car split))
3407                 (vectorp (car split)))
3408         (push 1.0 split)
3409         (push 'vertical split))
3410       ;; The SPLIT might be something that is to be evaled to
3411       ;; return a new SPLIT.
3412       (while (and (not (assq (car split) gnus-window-to-buffer))
3413                   (gnus-functionp (car split)))
3414         (setq split (eval split)))
3415
3416       (setq type (elt split 0))
3417       (cond
3418        ;; Nothing here.
3419        ((null split) t)
3420        ;; A buffer.
3421        ((not (memq type '(horizontal vertical frame)))
3422         (setq buffer (cond ((stringp type) type)
3423                            (t (cdr (assq type gnus-window-to-buffer)))))
3424         (unless buffer
3425           (error "Illegal buffer type: %s" type))
3426         (when (setq buf (get-buffer (if (symbolp buffer)
3427                                         (symbol-value buffer)
3428                                       buffer)))
3429           (setq win (get-buffer-window buf t)))
3430         (if win
3431             (when (memq 'point split)
3432                 (setq all-visible win))
3433           (setq all-visible nil)))
3434        (t
3435         (when (eq type 'frame)
3436           (setq gnus-frame-split-p t))
3437         (setq stack (append (cddr split) stack)))))
3438     (unless (eq all-visible t)
3439       all-visible)))
3440
3441 (defun gnus-window-top-edge (&optional window)
3442   (nth 1 (window-edges window)))
3443
3444 (defun gnus-remove-some-windows ()
3445   (let ((buffers gnus-window-to-buffer)
3446         buf bufs lowest-buf lowest)
3447     (save-excursion
3448       ;; Remove windows on all known Gnus buffers.
3449       (while buffers
3450         (setq buf (cdar buffers))
3451         (if (symbolp buf)
3452             (setq buf (and (boundp buf) (symbol-value buf))))
3453         (and buf
3454              (get-buffer-window buf)
3455              (progn
3456                (setq bufs (cons buf bufs))
3457                (pop-to-buffer buf)
3458                (if (or (not lowest)
3459                        (< (gnus-window-top-edge) lowest))
3460                    (progn
3461                      (setq lowest (gnus-window-top-edge))
3462                      (setq lowest-buf buf)))))
3463         (setq buffers (cdr buffers)))
3464       ;; Remove windows on *all* summary buffers.
3465       (walk-windows
3466        (lambda (win)
3467          (let ((buf (window-buffer win)))
3468            (if (string-match    "^\\*Summary" (buffer-name buf))
3469                (progn
3470                  (setq bufs (cons buf bufs))
3471                  (pop-to-buffer buf)
3472                  (if (or (not lowest)
3473                          (< (gnus-window-top-edge) lowest))
3474                      (progn
3475                        (setq lowest-buf buf)
3476                        (setq lowest (gnus-window-top-edge)))))))))
3477       (and lowest-buf
3478            (progn
3479              (pop-to-buffer lowest-buf)
3480              (switch-to-buffer nntp-server-buffer)))
3481       (while bufs
3482         (and (not (eq (car bufs) lowest-buf))
3483              (delete-windows-on (car bufs)))
3484         (setq bufs (cdr bufs))))))
3485
3486 (defun gnus-version ()
3487   "Version numbers of this version of Gnus."
3488   (interactive)
3489   (let ((methods gnus-valid-select-methods)
3490         (mess gnus-version)
3491         meth)
3492     ;; Go through all the legal select methods and add their version
3493     ;; numbers to the total version string.  Only the backends that are
3494     ;; currently in use will have their message numbers taken into
3495     ;; consideration.
3496     (while methods
3497       (setq meth (intern (concat (caar methods) "-version")))
3498       (and (boundp meth)
3499            (stringp (symbol-value meth))
3500            (setq mess (concat mess "; " (symbol-value meth))))
3501       (setq methods (cdr methods)))
3502     (gnus-message 2 mess)))
3503
3504 (defun gnus-info-find-node ()
3505   "Find Info documentation of Gnus."
3506   (interactive)
3507   ;; Enlarge info window if needed.
3508   (let ((mode major-mode)
3509         gnus-info-buffer)
3510     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3511     (setq gnus-info-buffer (current-buffer))
3512     (gnus-configure-windows 'info)))
3513
3514 (defun gnus-days-between (date1 date2)
3515   ;; Return the number of days between date1 and date2.
3516   (- (gnus-day-number date1) (gnus-day-number date2)))
3517
3518 (defun gnus-day-number (date)
3519   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3520                      (timezone-parse-date date))))
3521     (timezone-absolute-from-gregorian
3522      (nth 1 dat) (nth 2 dat) (car dat))))
3523
3524 (defun gnus-encode-date (date)
3525   "Convert DATE to internal time."
3526   (let* ((parse (timezone-parse-date date))
3527          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3528          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3529     (encode-time (caddr time) (cadr time) (car time)
3530                  (caddr date) (cadr date) (car date) (nth 4 date))))
3531
3532 (defun gnus-time-minus (t1 t2)
3533   "Subtract two internal times."
3534   (let ((borrow (< (cadr t1) (cadr t2))))
3535     (list (- (car t1) (car t2) (if borrow 1 0))
3536           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3537
3538 (defun gnus-file-newer-than (file date)
3539   (let ((fdate (nth 5 (file-attributes file))))
3540     (or (> (car fdate) (car date))
3541         (and (= (car fdate) (car date))
3542              (> (nth 1 fdate) (nth 1 date))))))
3543
3544 (defmacro gnus-local-set-keys (&rest plist)
3545   "Set the keys in PLIST in the current keymap."
3546   `(gnus-define-keys-1 (current-local-map) ',plist))
3547
3548 (defmacro gnus-define-keys (keymap &rest plist)
3549   "Define all keys in PLIST in KEYMAP."
3550   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3551
3552 (put 'gnus-define-keys 'lisp-indent-function 1)
3553 (put 'gnus-define-keys 'lisp-indent-hook 1)
3554 (put 'gnus-define-keymap 'lisp-indent-function 1)
3555 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3556
3557 (defmacro gnus-define-keymap (keymap &rest plist)
3558   "Define all keys in PLIST in KEYMAP."
3559   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3560
3561 (defun gnus-define-keys-1 (keymap plist)
3562   (when (null keymap)
3563     (error "Can't set keys in a null keymap"))
3564   (cond ((symbolp keymap)
3565          (setq keymap (symbol-value keymap)))
3566         ((keymapp keymap))
3567         ((listp keymap)
3568          (set (car keymap) nil)
3569          (define-prefix-command (car keymap))
3570          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3571          (setq keymap (symbol-value (car keymap)))))
3572   (let (key)
3573     (while plist
3574       (when (symbolp (setq key (pop plist)))
3575         (setq key (symbol-value key)))
3576       (define-key keymap key (pop plist)))))
3577
3578 (defun gnus-group-read-only-p (&optional group)
3579   "Check whether GROUP supports editing or not.
3580 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3581 that that variable is buffer-local to the summary buffers."
3582   (let ((group (or group gnus-newsgroup-name)))
3583     (not (gnus-check-backend-function 'request-replace-article group))))
3584
3585 (defun gnus-group-total-expirable-p (group)
3586   "Check whether GROUP is total-expirable or not."
3587   (let ((params (gnus-info-params (gnus-get-info group))))
3588     (or (memq 'total-expire params)
3589         (cdr (assq 'total-expire params)) ; (total-expire . t)
3590         (and gnus-total-expirable-newsgroups ; Check var.
3591              (string-match gnus-total-expirable-newsgroups group)))))
3592
3593 (defun gnus-group-auto-expirable-p (group)
3594   "Check whether GROUP is total-expirable or not."
3595   (let ((params (gnus-info-params (gnus-get-info group))))
3596     (or (memq 'auto-expire params)
3597         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3598         (and gnus-auto-expirable-newsgroups ; Check var.
3599              (string-match gnus-auto-expirable-newsgroups group)))))
3600
3601 (defun gnus-virtual-group-p (group)
3602   "Say whether GROUP is virtual or not."
3603   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3604                         gnus-valid-select-methods)))
3605
3606 (defun gnus-news-group-p (group &optional article)
3607   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3608   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3609       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3610            (eq (gnus-request-type group article) 'news))))
3611
3612 (defsubst gnus-simplify-subject-fully (subject)
3613   "Simplify a subject string according to the user's wishes."
3614   (cond
3615    ((null gnus-summary-gather-subject-limit)
3616     (gnus-simplify-subject-re subject))
3617    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3618     (gnus-simplify-subject-fuzzy subject))
3619    ((numberp gnus-summary-gather-subject-limit)
3620     (gnus-limit-string (gnus-simplify-subject-re subject)
3621                        gnus-summary-gather-subject-limit))
3622    (t
3623     subject)))
3624
3625 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3626   "Check whether two subjects are equal.  If optional argument
3627 simple-first is t, first argument is already simplified."
3628   (cond
3629    ((null simple-first)
3630     (equal (gnus-simplify-subject-fully s1)
3631            (gnus-simplify-subject-fully s2)))
3632    (t
3633     (equal s1
3634            (gnus-simplify-subject-fully s2)))))
3635
3636 ;; Returns a list of writable groups.
3637 (defun gnus-writable-groups ()
3638   (let ((alist gnus-newsrc-alist)
3639         groups group)
3640     (while (setq group (car (pop alist)))
3641       (unless (gnus-group-read-only-p group)
3642         (push group groups)))
3643     (nreverse groups)))
3644
3645 (defun gnus-completing-read (default prompt &rest args)
3646   ;; Like `completing-read', except that DEFAULT is the default argument.
3647   (let* ((prompt (if default 
3648                      (concat prompt " (default " default ") ")
3649                    (concat prompt " ")))
3650          (answer (apply 'completing-read prompt args)))
3651     (if (or (null answer) (zerop (length answer)))
3652         default
3653       answer)))
3654
3655 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3656 ;; the echo area.
3657 (defun gnus-y-or-n-p (prompt)
3658   (prog1
3659       (y-or-n-p prompt)
3660     (message "")))
3661
3662 (defun gnus-yes-or-no-p (prompt)
3663   (prog1
3664       (yes-or-no-p prompt)
3665     (message "")))
3666
3667 ;; Check whether to use long file names.
3668 (defun gnus-use-long-file-name (symbol)
3669   ;; The variable has to be set...
3670   (and gnus-use-long-file-name
3671        ;; If it isn't a list, then we return t.
3672        (or (not (listp gnus-use-long-file-name))
3673            ;; If it is a list, and the list contains `symbol', we
3674            ;; return nil.
3675            (not (memq symbol gnus-use-long-file-name)))))
3676
3677 ;; I suspect there's a better way, but I haven't taken the time to do
3678 ;; it yet. -erik selberg@cs.washington.edu
3679 (defun gnus-dd-mmm (messy-date)
3680   "Return a string like DD-MMM from a big messy string"
3681   (let ((datevec (timezone-parse-date messy-date)))
3682     (format "%2s-%s"
3683             (condition-case ()
3684                 ;; Make sure leading zeroes are stripped.
3685                 (number-to-string (string-to-number (aref datevec 2)))
3686               (error "??"))
3687             (capitalize
3688              (or (car
3689                   (nth (1- (string-to-number (aref datevec 1)))
3690                        timezone-months-assoc))
3691                  "???")))))
3692
3693 ;; Make a hash table (default and minimum size is 255).
3694 ;; Optional argument HASHSIZE specifies the table size.
3695 (defun gnus-make-hashtable (&optional hashsize)
3696   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3697
3698 ;; Make a number that is suitable for hashing; bigger than MIN and one
3699 ;; less than 2^x.
3700 (defun gnus-create-hash-size (min)
3701   (let ((i 1))
3702     (while (< i min)
3703       (setq i (* 2 i)))
3704     (1- i)))
3705
3706 ;; Show message if message has a lower level than `gnus-verbose'.
3707 ;; Guideline for numbers:
3708 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3709 ;; for things that take a long time, 7 - not very important messages
3710 ;; on stuff, 9 - messages inside loops.
3711 (defun gnus-message (level &rest args)
3712   (if (<= level gnus-verbose)
3713       (apply 'message args)
3714     ;; We have to do this format thingy here even if the result isn't
3715     ;; shown - the return value has to be the same as the return value
3716     ;; from `message'.
3717     (apply 'format args)))
3718
3719 (defun gnus-error (level &rest args)
3720   "Beep an error if `gnus-verbose' is on LEVEL or less."
3721   (when (<= (floor level) gnus-verbose)
3722     (apply 'message args)
3723     (ding)
3724     (let (duration)
3725       (when (and (floatp level)
3726                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3727         (sit-for duration))))
3728   nil)
3729
3730 ;; Generate a unique new group name.
3731 (defun gnus-generate-new-group-name (leaf)
3732   (let ((name leaf)
3733         (num 0))
3734     (while (gnus-gethash name gnus-newsrc-hashtb)
3735       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3736     name))
3737
3738 (defsubst gnus-hide-text (b e props)
3739   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3740   (add-text-properties b e props)
3741   (when (memq 'intangible props)
3742     (put-text-property (max (1- b) (point-min))
3743                        b 'intangible (cddr (memq 'intangible props)))))
3744
3745 (defsubst gnus-unhide-text (b e)
3746   "Remove hidden text properties from region between B and E."
3747   (remove-text-properties b e gnus-hidden-properties)
3748   (when (memq 'intangible gnus-hidden-properties)
3749     (put-text-property (max (1- b) (point-min))
3750                        b 'intangible nil)))
3751
3752 (defun gnus-hide-text-type (b e type)
3753   "Hide text of TYPE between B and E."
3754   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3755
3756 ;; Find out whether the gnus-visual TYPE is wanted.
3757 (defun gnus-visual-p (&optional type class)
3758   (and gnus-visual                      ; Has to be non-nil, at least.
3759        (if (not type)                   ; We don't care about type.
3760            gnus-visual
3761          (if (listp gnus-visual)        ; It's a list, so we check it.
3762              (or (memq type gnus-visual)
3763                  (memq class gnus-visual))
3764            t))))
3765
3766 (defun gnus-parent-id (references)
3767   "Return the last Message-ID in REFERENCES."
3768   (when (and references
3769              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3770     (substring references (match-beginning 1) (match-end 1))))
3771
3772 (defun gnus-split-references (references)
3773   "Return a list of Message-IDs in REFERENCES."
3774   (let ((beg 0)
3775         ids)
3776     (while (string-match "<[^>]+>" references beg)
3777       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3778             ids))
3779     (nreverse ids)))
3780
3781 (defun gnus-buffer-live-p (buffer)
3782   "Say whether BUFFER is alive or not."
3783   (and buffer
3784        (get-buffer buffer)
3785        (buffer-name (get-buffer buffer))))
3786
3787 (defun gnus-ephemeral-group-p (group)
3788   "Say whether GROUP is ephemeral or not."
3789   (gnus-group-get-parameter group 'quit-config))
3790
3791 (defun gnus-group-quit-config (group)
3792   "Return the quit-config of GROUP."
3793   (gnus-group-get-parameter group 'quit-config))
3794
3795 (defun gnus-simplify-mode-line ()
3796   "Make mode lines a bit simpler."
3797   (setq mode-line-modified "-- ")
3798   (when (listp mode-line-format)
3799     (make-local-variable 'mode-line-format)
3800     (setq mode-line-format (copy-sequence mode-line-format))
3801     (when (equal (nth 3 mode-line-format) "   ")
3802       (setcar (nthcdr 3 mode-line-format) " "))))
3803
3804 ;;; List and range functions
3805
3806 (defun gnus-last-element (list)
3807   "Return last element of LIST."
3808   (while (cdr list)
3809     (setq list (cdr list)))
3810   (car list))
3811
3812 (defun gnus-copy-sequence (list)
3813   "Do a complete, total copy of a list."
3814   (if (and (consp list) (not (consp (cdr list))))
3815       (cons (car list) (cdr list))
3816     (mapcar (lambda (elem) (if (consp elem)
3817                                (if (consp (cdr elem))
3818                                    (gnus-copy-sequence elem)
3819                                  (cons (car elem) (cdr elem)))
3820                              elem))
3821             list)))
3822
3823 (defun gnus-set-difference (list1 list2)
3824   "Return a list of elements of LIST1 that do not appear in LIST2."
3825   (let ((list1 (copy-sequence list1)))
3826     (while list2
3827       (setq list1 (delq (car list2) list1))
3828       (setq list2 (cdr list2)))
3829     list1))
3830
3831 (defun gnus-sorted-complement (list1 list2)
3832   "Return a list of elements of LIST1 that do not appear in LIST2.
3833 Both lists have to be sorted over <."
3834   (let (out)
3835     (if (or (null list1) (null list2))
3836         (or list1 list2)
3837       (while (and list1 list2)
3838         (cond ((= (car list1) (car list2))
3839                (setq list1 (cdr list1)
3840                      list2 (cdr list2)))
3841               ((< (car list1) (car list2))
3842                (setq out (cons (car list1) out))
3843                (setq list1 (cdr list1)))
3844               (t
3845                (setq out (cons (car list2) out))
3846                (setq list2 (cdr list2)))))
3847       (nconc (nreverse out) (or list1 list2)))))
3848
3849 (defun gnus-intersection (list1 list2)
3850   (let ((result nil))
3851     (while list2
3852       (if (memq (car list2) list1)
3853           (setq result (cons (car list2) result)))
3854       (setq list2 (cdr list2)))
3855     result))
3856
3857 (defun gnus-sorted-intersection (list1 list2)
3858   ;; LIST1 and LIST2 have to be sorted over <.
3859   (let (out)
3860     (while (and list1 list2)
3861       (cond ((= (car list1) (car list2))
3862              (setq out (cons (car list1) out)
3863                    list1 (cdr list1)
3864                    list2 (cdr list2)))
3865             ((< (car list1) (car list2))
3866              (setq list1 (cdr list1)))
3867             (t
3868              (setq list2 (cdr list2)))))
3869     (nreverse out)))
3870
3871 (defun gnus-set-sorted-intersection (list1 list2)
3872   ;; LIST1 and LIST2 have to be sorted over <.
3873   ;; This function modifies LIST1.
3874   (let* ((top (cons nil list1))
3875          (prev top))
3876     (while (and list1 list2)
3877       (cond ((= (car list1) (car list2))
3878              (setq prev list1
3879                    list1 (cdr list1)
3880                    list2 (cdr list2)))
3881             ((< (car list1) (car list2))
3882              (setcdr prev (cdr list1))
3883              (setq list1 (cdr list1)))
3884             (t
3885              (setq list2 (cdr list2)))))
3886     (setcdr prev nil)
3887     (cdr top)))
3888
3889 (defun gnus-compress-sequence (numbers &optional always-list)
3890   "Convert list of numbers to a list of ranges or a single range.
3891 If ALWAYS-LIST is non-nil, this function will always release a list of
3892 ranges."
3893   (let* ((first (car numbers))
3894          (last (car numbers))
3895          result)
3896     (if (null numbers)
3897         nil
3898       (if (not (listp (cdr numbers)))
3899           numbers
3900         (while numbers
3901           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3902                 ((= (1+ last) (car numbers)) ;Still in sequence
3903                  (setq last (car numbers)))
3904                 (t                      ;End of one sequence
3905                  (setq result
3906                        (cons (if (= first last) first
3907                                (cons first last)) result))
3908                  (setq first (car numbers))
3909                  (setq last  (car numbers))))
3910           (setq numbers (cdr numbers)))
3911         (if (and (not always-list) (null result))
3912             (if (= first last) (list first) (cons first last))
3913           (nreverse (cons (if (= first last) first (cons first last))
3914                           result)))))))
3915
3916 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3917 (defun gnus-uncompress-range (ranges)
3918   "Expand a list of ranges into a list of numbers.
3919 RANGES is either a single range on the form `(num . num)' or a list of
3920 these ranges."
3921   (let (first last result)
3922     (cond
3923      ((null ranges)
3924       nil)
3925      ((not (listp (cdr ranges)))
3926       (setq first (car ranges))
3927       (setq last (cdr ranges))
3928       (while (<= first last)
3929         (setq result (cons first result))
3930         (setq first (1+ first)))
3931       (nreverse result))
3932      (t
3933       (while ranges
3934         (if (atom (car ranges))
3935             (if (numberp (car ranges))
3936                 (setq result (cons (car ranges) result)))
3937           (setq first (caar ranges))
3938           (setq last  (cdar ranges))
3939           (while (<= first last)
3940             (setq result (cons first result))
3941             (setq first (1+ first))))
3942         (setq ranges (cdr ranges)))
3943       (nreverse result)))))
3944
3945 (defun gnus-add-to-range (ranges list)
3946   "Return a list of ranges that has all articles from both RANGES and LIST.
3947 Note: LIST has to be sorted over `<'."
3948   (if (not ranges)
3949       (gnus-compress-sequence list t)
3950     (setq list (copy-sequence list))
3951     (or (listp (cdr ranges))
3952         (setq ranges (list ranges)))
3953     (let ((out ranges)
3954           ilist lowest highest temp)
3955       (while (and ranges list)
3956         (setq ilist list)
3957         (setq lowest (or (and (atom (car ranges)) (car ranges))
3958                          (caar ranges)))
3959         (while (and list (cdr list) (< (cadr list) lowest))
3960           (setq list (cdr list)))
3961         (if (< (car ilist) lowest)
3962             (progn
3963               (setq temp list)
3964               (setq list (cdr list))
3965               (setcdr temp nil)
3966               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3967         (setq highest (or (and (atom (car ranges)) (car ranges))
3968                           (cdar ranges)))
3969         (while (and list (<= (car list) highest))
3970           (setq list (cdr list)))
3971         (setq ranges (cdr ranges)))
3972       (if list
3973           (setq out (nconc (gnus-compress-sequence list t) out)))
3974       (setq out (sort out (lambda (r1 r2)
3975                             (< (or (and (atom r1) r1) (car r1))
3976                                (or (and (atom r2) r2) (car r2))))))
3977       (setq ranges out)
3978       (while ranges
3979         (if (atom (car ranges))
3980             (if (cdr ranges)
3981                 (if (atom (cadr ranges))
3982                     (if (= (1+ (car ranges)) (cadr ranges))
3983                         (progn
3984                           (setcar ranges (cons (car ranges)
3985                                                (cadr ranges)))
3986                           (setcdr ranges (cddr ranges))))
3987                   (if (= (1+ (car ranges)) (caadr ranges))
3988                       (progn
3989                         (setcar (cadr ranges) (car ranges))
3990                         (setcar ranges (cadr ranges))
3991                         (setcdr ranges (cddr ranges))))))
3992           (if (cdr ranges)
3993               (if (atom (cadr ranges))
3994                   (if (= (1+ (cdar ranges)) (cadr ranges))
3995                       (progn
3996                         (setcdr (car ranges) (cadr ranges))
3997                         (setcdr ranges (cddr ranges))))
3998                 (if (= (1+ (cdar ranges)) (caadr ranges))
3999                     (progn
4000                       (setcdr (car ranges) (cdadr ranges))
4001                       (setcdr ranges (cddr ranges)))))))
4002         (setq ranges (cdr ranges)))
4003       out)))
4004
4005 (defun gnus-remove-from-range (ranges list)
4006   "Return a list of ranges that has all articles from LIST removed from RANGES.
4007 Note: LIST has to be sorted over `<'."
4008   ;; !!! This function shouldn't look like this, but I've got a headache.
4009   (gnus-compress-sequence
4010    (gnus-sorted-complement
4011     (gnus-uncompress-range ranges) list)))
4012
4013 (defun gnus-member-of-range (number ranges)
4014   (if (not (listp (cdr ranges)))
4015       (and (>= number (car ranges))
4016            (<= number (cdr ranges)))
4017     (let ((not-stop t))
4018       (while (and ranges
4019                   (if (numberp (car ranges))
4020                       (>= number (car ranges))
4021                     (>= number (caar ranges)))
4022                   not-stop)
4023         (if (if (numberp (car ranges))
4024                 (= number (car ranges))
4025               (and (>= number (caar ranges))
4026                    (<= number (cdar ranges))))
4027             (setq not-stop nil))
4028         (setq ranges (cdr ranges)))
4029       (not not-stop))))
4030
4031 (defun gnus-range-length (range)
4032   "Return the length RANGE would have if uncompressed."
4033   (length (gnus-uncompress-range range)))
4034
4035 (defun gnus-sublist-p (list sublist)
4036   "Test whether all elements in SUBLIST are members of LIST."
4037   (let ((sublistp t))
4038     (while sublist
4039       (unless (memq (pop sublist) list)
4040         (setq sublistp nil
4041               sublist nil)))
4042     sublistp))
4043
4044 \f
4045 ;;;
4046 ;;; Gnus group mode
4047 ;;;
4048
4049 (defvar gnus-group-mode-map nil)
4050 (put 'gnus-group-mode 'mode-class 'special)
4051
4052 (unless gnus-group-mode-map
4053   (setq gnus-group-mode-map (make-keymap))
4054   (suppress-keymap gnus-group-mode-map)
4055
4056   (gnus-define-keys gnus-group-mode-map
4057     " " gnus-group-read-group
4058     "=" gnus-group-select-group
4059     "\r" gnus-group-select-group
4060     "\M-\r" gnus-group-quick-select-group
4061     "j" gnus-group-jump-to-group
4062     "n" gnus-group-next-unread-group
4063     "p" gnus-group-prev-unread-group
4064     "\177" gnus-group-prev-unread-group
4065     [delete] gnus-group-prev-unread-group
4066     "N" gnus-group-next-group
4067     "P" gnus-group-prev-group
4068     "\M-n" gnus-group-next-unread-group-same-level
4069     "\M-p" gnus-group-prev-unread-group-same-level
4070     "," gnus-group-best-unread-group
4071     "." gnus-group-first-unread-group
4072     "u" gnus-group-unsubscribe-current-group
4073     "U" gnus-group-unsubscribe-group
4074     "c" gnus-group-catchup-current
4075     "C" gnus-group-catchup-current-all
4076     "l" gnus-group-list-groups
4077     "L" gnus-group-list-all-groups
4078     "m" gnus-group-mail
4079     "g" gnus-group-get-new-news
4080     "\M-g" gnus-group-get-new-news-this-group
4081     "R" gnus-group-restart
4082     "r" gnus-group-read-init-file
4083     "B" gnus-group-browse-foreign-server
4084     "b" gnus-group-check-bogus-groups
4085     "F" gnus-find-new-newsgroups
4086     "\C-c\C-d" gnus-group-describe-group
4087     "\M-d" gnus-group-describe-all-groups
4088     "\C-c\C-a" gnus-group-apropos
4089     "\C-c\M-\C-a" gnus-group-description-apropos
4090     "a" gnus-group-post-news
4091     "\ek" gnus-group-edit-local-kill
4092     "\eK" gnus-group-edit-global-kill
4093     "\C-k" gnus-group-kill-group
4094     "\C-y" gnus-group-yank-group
4095     "\C-w" gnus-group-kill-region
4096     "\C-x\C-t" gnus-group-transpose-groups
4097     "\C-c\C-l" gnus-group-list-killed
4098     "\C-c\C-x" gnus-group-expire-articles
4099     "\C-c\M-\C-x" gnus-group-expire-all-groups
4100     "V" gnus-version
4101     "s" gnus-group-save-newsrc
4102     "z" gnus-group-suspend
4103     "Z" gnus-group-clear-dribble
4104     "q" gnus-group-exit
4105     "Q" gnus-group-quit
4106     "?" gnus-group-describe-briefly
4107     "\C-c\C-i" gnus-info-find-node
4108     "\M-e" gnus-group-edit-group-method
4109     "^" gnus-group-enter-server-mode
4110     gnus-mouse-2 gnus-mouse-pick-group
4111     "<" beginning-of-buffer
4112     ">" end-of-buffer
4113     "\C-c\C-b" gnus-bug
4114     "\C-c\C-s" gnus-group-sort-groups
4115     "t" gnus-topic-mode
4116     "\C-c\M-g" gnus-activate-all-groups
4117     "\M-&" gnus-group-universal-argument
4118     "#" gnus-group-mark-group
4119     "\M-#" gnus-group-unmark-group)
4120
4121   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4122     "m" gnus-group-mark-group
4123     "u" gnus-group-unmark-group
4124     "w" gnus-group-mark-region
4125     "m" gnus-group-mark-buffer
4126     "r" gnus-group-mark-regexp
4127     "U" gnus-group-unmark-all-groups)
4128
4129   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4130     "d" gnus-group-make-directory-group
4131     "h" gnus-group-make-help-group
4132     "a" gnus-group-make-archive-group
4133     "k" gnus-group-make-kiboze-group
4134     "m" gnus-group-make-group
4135     "E" gnus-group-edit-group
4136     "e" gnus-group-edit-group-method
4137     "p" gnus-group-edit-group-parameters
4138     "v" gnus-group-add-to-virtual
4139     "V" gnus-group-make-empty-virtual
4140     "D" gnus-group-enter-directory
4141     "f" gnus-group-make-doc-group
4142     "r" gnus-group-rename-group
4143     "\177" gnus-group-delete-group
4144     [delete] gnus-group-delete-group)
4145
4146    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4147      "b" gnus-group-brew-soup
4148      "w" gnus-soup-save-areas
4149      "s" gnus-soup-send-replies
4150      "p" gnus-soup-pack-packet
4151      "r" nnsoup-pack-replies)
4152
4153    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4154      "s" gnus-group-sort-groups
4155      "a" gnus-group-sort-groups-by-alphabet
4156      "u" gnus-group-sort-groups-by-unread
4157      "l" gnus-group-sort-groups-by-level
4158      "v" gnus-group-sort-groups-by-score
4159      "r" gnus-group-sort-groups-by-rank
4160      "m" gnus-group-sort-groups-by-method)
4161
4162    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4163      "k" gnus-group-list-killed
4164      "z" gnus-group-list-zombies
4165      "s" gnus-group-list-groups
4166      "u" gnus-group-list-all-groups
4167      "A" gnus-group-list-active
4168      "a" gnus-group-apropos
4169      "d" gnus-group-description-apropos
4170      "m" gnus-group-list-matching
4171      "M" gnus-group-list-all-matching
4172      "l" gnus-group-list-level)
4173
4174    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4175      "f" gnus-score-flush-cache)
4176
4177    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4178      "f" gnus-group-fetch-faq)
4179
4180    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4181      "l" gnus-group-set-current-level
4182      "t" gnus-group-unsubscribe-current-group
4183      "s" gnus-group-unsubscribe-group
4184      "k" gnus-group-kill-group
4185      "y" gnus-group-yank-group
4186      "w" gnus-group-kill-region
4187      "\C-k" gnus-group-kill-level
4188      "z" gnus-group-kill-all-zombies))
4189
4190 (defun gnus-group-mode ()
4191   "Major mode for reading news.
4192
4193 All normal editing commands are switched off.
4194 \\<gnus-group-mode-map>
4195 The group buffer lists (some of) the groups available.  For instance,
4196 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4197 lists all zombie groups.
4198
4199 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4200 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4201
4202 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4203
4204 The following commands are available:
4205
4206 \\{gnus-group-mode-map}"
4207   (interactive)
4208   (when (and menu-bar-mode
4209              (gnus-visual-p 'group-menu 'menu))
4210     (gnus-group-make-menu-bar))
4211   (kill-all-local-variables)
4212   (gnus-simplify-mode-line)
4213   (setq major-mode 'gnus-group-mode)
4214   (setq mode-name "Group")
4215   (gnus-group-set-mode-line)
4216   (setq mode-line-process nil)
4217   (use-local-map gnus-group-mode-map)
4218   (buffer-disable-undo (current-buffer))
4219   (setq truncate-lines t)
4220   (setq buffer-read-only t)
4221   (run-hooks 'gnus-group-mode-hook))
4222
4223 (defun gnus-mouse-pick-group (e)
4224   "Enter the group under the mouse pointer."
4225   (interactive "e")
4226   (mouse-set-point e)
4227   (gnus-group-read-group nil))
4228
4229 ;; Look at LEVEL and find out what the level is really supposed to be.
4230 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4231 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4232 (defun gnus-group-default-level (&optional level number-or-nil)
4233   (cond
4234    (gnus-group-use-permanent-levels
4235 ;    (setq gnus-group-default-list-level
4236 ;         (or level gnus-group-default-list-level))
4237     (or level gnus-group-default-list-level gnus-level-subscribed))
4238    (number-or-nil
4239     level)
4240    (t
4241     (or level gnus-group-default-list-level gnus-level-subscribed))))
4242
4243 ;;;###autoload
4244 (defun gnus-slave-no-server (&optional arg)
4245   "Read network news as a slave, without connecting to local server"
4246   (interactive "P")
4247   (gnus-no-server arg t))
4248
4249 ;;;###autoload
4250 (defun gnus-no-server (&optional arg slave)
4251   "Read network news.
4252 If ARG is a positive number, Gnus will use that as the
4253 startup level.  If ARG is nil, Gnus will be started at level 2.
4254 If ARG is non-nil and not a positive number, Gnus will
4255 prompt the user for the name of an NNTP server to use.
4256 As opposed to `gnus', this command will not connect to the local server."
4257   (interactive "P")
4258   (let ((gnus-group-use-permanent-levels t))
4259     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4260   (make-local-variable 'gnus-group-use-permanent-levels)
4261   (setq gnus-group-use-permanent-levels t))
4262
4263 ;;;###autoload
4264 (defun gnus-slave (&optional arg)
4265   "Read news as a slave."
4266   (interactive "P")
4267   (gnus arg nil 'slave))
4268
4269 ;;;###autoload
4270 (defun gnus-other-frame (&optional arg)
4271   "Pop up a frame to read news."
4272   (interactive "P")
4273   (if (get-buffer gnus-group-buffer)
4274       (let ((pop-up-frames t))
4275         (gnus arg))
4276     (select-frame (make-frame))
4277     (gnus arg)))
4278
4279 ;;;###autoload
4280 (defun gnus (&optional arg dont-connect slave)
4281   "Read network news.
4282 If ARG is non-nil and a positive number, Gnus will use that as the
4283 startup level.  If ARG is non-nil and not a positive number, Gnus will
4284 prompt the user for the name of an NNTP server to use."
4285   (interactive "P")
4286
4287   (if (get-buffer gnus-group-buffer)
4288       (progn
4289         (switch-to-buffer gnus-group-buffer)
4290         (gnus-group-get-new-news))
4291
4292     (gnus-clear-system)
4293     (nnheader-init-server-buffer)
4294     (gnus-read-init-file)
4295     (setq gnus-slave slave)
4296
4297     (gnus-group-setup-buffer)
4298     (let ((buffer-read-only nil))
4299       (erase-buffer)
4300       (if (not gnus-inhibit-startup-message)
4301           (progn
4302             (gnus-group-startup-message)
4303             (sit-for 0))))
4304
4305     (let ((level (and (numberp arg) (> arg 0) arg))
4306           did-connect)
4307       (unwind-protect
4308           (progn
4309             (or dont-connect
4310                 (setq did-connect
4311                       (gnus-start-news-server (and arg (not level))))))
4312         (if (and (not dont-connect)
4313                  (not did-connect))
4314             (gnus-group-quit)
4315           (run-hooks 'gnus-startup-hook)
4316           ;; NNTP server is successfully open.
4317
4318           ;; Find the current startup file name.
4319           (setq gnus-current-startup-file
4320                 (gnus-make-newsrc-file gnus-startup-file))
4321
4322           ;; Read the dribble file.
4323           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4324
4325           ;; Allow using GroupLens predictions.
4326           (when gnus-use-grouplens
4327             (bbb-login)
4328             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4329
4330           (gnus-summary-make-display-table)
4331           ;; Do the actual startup.
4332           (gnus-setup-news nil level dont-connect)
4333           ;; Generate the group buffer.
4334           (gnus-group-list-groups level)
4335           (gnus-group-first-unread-group)
4336           (gnus-configure-windows 'group)
4337           (gnus-group-set-mode-line))))))
4338
4339 (defun gnus-unload ()
4340   "Unload all Gnus features."
4341   (interactive)
4342   (or (boundp 'load-history)
4343       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4344   (let ((history load-history)
4345         feature)
4346     (while history
4347       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4348            (setq feature (cdr (assq 'provide (car history))))
4349            (unload-feature feature 'force))
4350       (setq history (cdr history)))))
4351
4352 (defun gnus-compile ()
4353   "Byte-compile the user-defined format specs."
4354   (interactive)
4355   (let ((entries gnus-format-specs)
4356         entry gnus-tmp-func)
4357     (save-excursion
4358       (gnus-message 7 "Compiling format specs...")
4359
4360       (while entries
4361         (setq entry (pop entries))
4362         (if (eq (car entry) 'version)
4363             (setq gnus-format-specs (delq entry gnus-format-specs))
4364           (when (and (listp (caddr entry))
4365                      (not (eq 'byte-code (caaddr entry))))
4366             (fset 'gnus-tmp-func
4367                   `(lambda () ,(caddr entry)))
4368             (byte-compile 'gnus-tmp-func)
4369             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4370
4371       (push (cons 'version emacs-version) gnus-format-specs)
4372
4373       (gnus-message 7 "Compiling user specs...done"))))
4374
4375 (defun gnus-indent-rigidly (start end arg)
4376   "Indent rigidly using only spaces and no tabs."
4377   (save-excursion
4378     (save-restriction
4379       (narrow-to-region start end)
4380       (indent-rigidly start end arg)
4381       (goto-char (point-min))
4382       (while (search-forward "\t" nil t)
4383         (replace-match "        " t t)))))
4384
4385 (defun gnus-group-startup-message (&optional x y)
4386   "Insert startup message in current buffer."
4387   ;; Insert the message.
4388   (erase-buffer)
4389   (insert
4390    (format "              %s
4391           _    ___ _             _
4392           _ ___ __ ___  __    _ ___
4393           __   _     ___    __  ___
4394               _           ___     _
4395              _  _ __             _
4396              ___   __            _
4397                    __           _
4398                     _      _   _
4399                    _      _    _
4400                       _  _    _
4401                   __  ___
4402                  _   _ _     _
4403                 _   _
4404               _    _
4405              _    _
4406             _
4407           __
4408
4409 "
4410            ""))
4411   ;; And then hack it.
4412   (gnus-indent-rigidly (point-min) (point-max)
4413                        (/ (max (- (window-width) (or x 46)) 0) 2))
4414   (goto-char (point-min))
4415   (forward-line 1)
4416   (let* ((pheight (count-lines (point-min) (point-max)))
4417          (wheight (window-height))
4418          (rest (- wheight pheight)))
4419     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4420   ;; Fontify some.
4421   (goto-char (point-min))
4422   (and (search-forward "Praxis" nil t)
4423        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4424   (goto-char (point-min))
4425   (let* ((mode-string (gnus-group-set-mode-line)))
4426     (setq mode-line-buffer-identification
4427           (list (concat gnus-version (substring (car mode-string) 4))))
4428     (set-buffer-modified-p t)))
4429
4430 (defun gnus-group-setup-buffer ()
4431   (or (get-buffer gnus-group-buffer)
4432       (progn
4433         (switch-to-buffer gnus-group-buffer)
4434         (gnus-add-current-to-buffer-list)
4435         (gnus-group-mode)
4436         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4437
4438 (defun gnus-group-list-groups (&optional level unread lowest)
4439   "List newsgroups with level LEVEL or lower that have unread articles.
4440 Default is all subscribed groups.
4441 If argument UNREAD is non-nil, groups with no unread articles are also
4442 listed."
4443   (interactive (list (if current-prefix-arg
4444                          (prefix-numeric-value current-prefix-arg)
4445                        (or
4446                         (gnus-group-default-level nil t)
4447                         gnus-group-default-list-level
4448                         gnus-level-subscribed))))
4449   (or level
4450       (setq level (car gnus-group-list-mode)
4451             unread (cdr gnus-group-list-mode)))
4452   (setq level (gnus-group-default-level level))
4453   (gnus-group-setup-buffer)             ;May call from out of group buffer
4454   (gnus-update-format-specifications)
4455   (let ((case-fold-search nil)
4456         (props (text-properties-at (gnus-point-at-bol)))
4457         (group (gnus-group-group-name)))
4458     (set-buffer gnus-group-buffer)
4459     (funcall gnus-group-prepare-function level unread lowest)
4460     (if (zerop (buffer-size))
4461         (gnus-message 5 gnus-no-groups-message)
4462       (goto-char (point-max))
4463       (when (or (not gnus-group-goto-next-group-function)
4464                 (not (funcall gnus-group-goto-next-group-function 
4465                               group props)))
4466         (if (not group)
4467             ;; Go to the first group with unread articles.
4468             (gnus-group-search-forward t)
4469           ;; Find the right group to put point on.  If the current group
4470           ;; has disappeared in the new listing, try to find the next
4471           ;; one.        If no next one can be found, just leave point at the
4472           ;; first newsgroup in the buffer.
4473           (if (not (gnus-goto-char
4474                     (text-property-any
4475                      (point-min) (point-max)
4476                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4477               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4478                 (while (and newsrc
4479                             (not (gnus-goto-char
4480                                   (text-property-any
4481                                    (point-min) (point-max) 'gnus-group
4482                                    (gnus-intern-safe
4483                                     (caar newsrc) gnus-active-hashtb)))))
4484                   (setq newsrc (cdr newsrc)))
4485                 (or newsrc (progn (goto-char (point-max))
4486                                   (forward-line -1)))))))
4487       ;; Adjust cursor point.
4488       (gnus-group-position-point))))
4489
4490 (defun gnus-group-list-level (level &optional all)
4491   "List groups on LEVEL.
4492 If ALL (the prefix), also list groups that have no unread articles."
4493   (interactive "nList groups on level: \nP")
4494   (gnus-group-list-groups level all level))
4495
4496 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4497   "List all newsgroups with unread articles of level LEVEL or lower.
4498 If ALL is non-nil, list groups that have no unread articles.
4499 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4500 If REGEXP, only list groups matching REGEXP."
4501   (set-buffer gnus-group-buffer)
4502   (let ((buffer-read-only nil)
4503         (newsrc (cdr gnus-newsrc-alist))
4504         (lowest (or lowest 1))
4505         info clevel unread group params)
4506     (erase-buffer)
4507     (if (< lowest gnus-level-zombie)
4508         ;; List living groups.
4509         (while newsrc
4510           (setq info (car newsrc)
4511                 group (gnus-info-group info)
4512                 params (gnus-info-params info)
4513                 newsrc (cdr newsrc)
4514                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4515           (and unread                   ; This group might be bogus
4516                (or (not regexp)
4517                    (string-match regexp group))
4518                (<= (setq clevel (gnus-info-level info)) level)
4519                (>= clevel lowest)
4520                (or all                  ; We list all groups?
4521                    (if (eq unread t)    ; Unactivated?
4522                        gnus-group-list-inactive-groups ; We list unactivated 
4523                      (> unread 0))      ; We list groups with unread articles
4524                    (and gnus-list-groups-with-ticked-articles
4525                         (cdr (assq 'tick (gnus-info-marks info))))
4526                                         ; And groups with tickeds
4527                    ;; Check for permanent visibility.
4528                    (and gnus-permanently-visible-groups
4529                         (string-match gnus-permanently-visible-groups
4530                                       group))
4531                    (memq 'visible params)
4532                    (cdr (assq 'visible params)))
4533                (gnus-group-insert-group-line
4534                 group (gnus-info-level info)
4535                 (gnus-info-marks info) unread (gnus-info-method info)))))
4536
4537     ;; List dead groups.
4538     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4539          (gnus-group-prepare-flat-list-dead
4540           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4541           gnus-level-zombie ?Z
4542           regexp))
4543     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4544          (gnus-group-prepare-flat-list-dead
4545           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4546           gnus-level-killed ?K regexp))
4547
4548     (gnus-group-set-mode-line)
4549     (setq gnus-group-list-mode (cons level all))
4550     (run-hooks 'gnus-group-prepare-hook)))
4551
4552 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4553   ;; List zombies and killed lists somewhat faster, which was
4554   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4555   ;; this by ignoring the group format specification altogether.
4556   (let (group)
4557     (if regexp
4558         ;; This loop is used when listing groups that match some
4559         ;; regexp.
4560         (while groups
4561           (setq group (pop groups))
4562           (when (string-match regexp group)
4563             (add-text-properties
4564              (point) (prog1 (1+ (point))
4565                        (insert " " mark "     *: " group "\n"))
4566              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4567                    'gnus-unread t
4568                    'gnus-level level))))
4569       ;; This loop is used when listing all groups.
4570       (while groups
4571         (add-text-properties
4572          (point) (prog1 (1+ (point))
4573                    (insert " " mark "     *: "
4574                            (setq group (pop groups)) "\n"))
4575          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4576                'gnus-unread t
4577                'gnus-level level))))))
4578
4579 (defmacro gnus-group-real-name (group)
4580   "Find the real name of a foreign newsgroup."
4581   `(let ((gname ,group))
4582      (if (string-match ":[^:]+$" gname)
4583          (substring gname (1+ (match-beginning 0)))
4584        gname)))
4585
4586 (defsubst gnus-server-add-address (method)
4587   (let ((method-name (symbol-name (car method))))
4588     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4589              (not (assq (intern (concat method-name "-address")) method)))
4590         (append method (list (list (intern (concat method-name "-address"))
4591                                    (nth 1 method))))
4592       method)))
4593
4594 (defsubst gnus-server-get-method (group method)
4595   ;; Input either a server name, and extended server name, or a
4596   ;; select method, and return a select method.
4597   (cond ((stringp method)
4598          (gnus-server-to-method method))
4599         ((equal method gnus-select-method)
4600          gnus-select-method)
4601         ((and (stringp (car method)) group)
4602          (gnus-server-extend-method group method))
4603         ((and method (not group)
4604               (equal (cadr method) ""))
4605          method)
4606         (t
4607          (gnus-server-add-address method))))
4608
4609 (defun gnus-server-to-method (server)
4610   "Map virtual server names to select methods."
4611   (or 
4612    ;; Perhaps this is the native server?
4613    (and (equal server "native") gnus-select-method)
4614    ;; It should be in the server alist.
4615    (cdr (assoc server gnus-server-alist))
4616    ;; If not, we look through all the opened server
4617    ;; to see whether we can find it there.
4618    (let ((opened gnus-opened-servers))
4619      (while (and opened
4620                  (not (equal server (format "%s:%s" (caaar opened)
4621                                             (cadaar opened)))))
4622        (pop opened))
4623      (caar opened))))
4624
4625 (defmacro gnus-method-equal (ss1 ss2)
4626   "Say whether two servers are equal."
4627   `(let ((s1 ,ss1)
4628          (s2 ,ss2))
4629      (or (equal s1 s2)
4630          (and (= (length s1) (length s2))
4631               (progn
4632                 (while (and s1 (member (car s1) s2))
4633                   (setq s1 (cdr s1)))
4634                 (null s1))))))
4635
4636 (defun gnus-server-equal (m1 m2)
4637   "Say whether two methods are equal."
4638   (let ((m1 (cond ((null m1) gnus-select-method)
4639                   ((stringp m1) (gnus-server-to-method m1))
4640                   (t m1)))
4641         (m2 (cond ((null m2) gnus-select-method)
4642                   ((stringp m2) (gnus-server-to-method m2))
4643                   (t m2))))
4644     (gnus-method-equal m1 m2)))
4645
4646 (defun gnus-servers-using-backend (backend)
4647   "Return a list of known servers using BACKEND."
4648   (let ((opened gnus-opened-servers)
4649         out)
4650     (while opened
4651       (when (eq backend (caaar opened))
4652         (push (caar opened) out))
4653       (pop opened))
4654     out))
4655
4656 (defun gnus-group-prefixed-name (group method)
4657   "Return the whole name from GROUP and METHOD."
4658   (and (stringp method) (setq method (gnus-server-to-method method)))
4659   (concat (format "%s" (car method))
4660           (if (and
4661                (or (assoc (format "%s" (car method)) 
4662                           (gnus-methods-using 'address))
4663                    (gnus-server-equal method gnus-message-archive-method))
4664                (nth 1 method)
4665                (not (string= (nth 1 method) "")))
4666               (concat "+" (nth 1 method)))
4667           ":" group))
4668
4669 (defun gnus-group-real-prefix (group)
4670   "Return the prefix of the current group name."
4671   (if (string-match "^[^:]+:" group)
4672       (substring group 0 (match-end 0))
4673     ""))
4674
4675 (defun gnus-group-method (group)
4676   "Return the server or method used for selecting GROUP."
4677   (let ((prefix (gnus-group-real-prefix group)))
4678     (if (equal prefix "")
4679         gnus-select-method
4680       (let ((servers gnus-opened-servers)
4681             (server "")
4682             backend possible found)
4683         (if (string-match "^[^\\+]+\\+" prefix)
4684             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4685                   server (substring prefix (match-end 0) (1- (length prefix))))
4686           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4687         (while servers
4688           (when (eq (caaar servers) backend)
4689             (setq possible (caar servers))
4690             (when (equal (cadaar servers) server)
4691               (setq found (caar servers))))
4692           (pop servers))
4693         (or (car (rassoc found gnus-server-alist))
4694             found
4695             (car (rassoc possible gnus-server-alist))
4696             possible
4697             (list backend server))))))
4698
4699 (defsubst gnus-secondary-method-p (method)
4700   "Return whether METHOD is a secondary select method."
4701   (let ((methods gnus-secondary-select-methods)
4702         (gmethod (gnus-server-get-method nil method)))
4703     (while (and methods
4704                 (not (equal (gnus-server-get-method nil (car methods))
4705                             gmethod)))
4706       (setq methods (cdr methods)))
4707     methods))
4708
4709 (defun gnus-group-foreign-p (group)
4710   "Say whether a group is foreign or not."
4711   (and (not (gnus-group-native-p group))
4712        (not (gnus-group-secondary-p group))))
4713
4714 (defun gnus-group-native-p (group)
4715   "Say whether the group is native or not."
4716   (not (string-match ":" group)))
4717
4718 (defun gnus-group-secondary-p (group)
4719   "Say whether the group is secondary or not."
4720   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4721
4722 (defun gnus-group-get-parameter (group &optional symbol)
4723   "Returns the group parameters for GROUP.
4724 If SYMBOL, return the value of that symbol in the group parameters."
4725   (let ((params (gnus-info-params (gnus-get-info group))))
4726     (if symbol
4727         (gnus-group-parameter-value params symbol)
4728       params)))
4729
4730 (defun gnus-group-parameter-value (params symbol)
4731   "Return the value of SYMBOL in group PARAMS."
4732   (or (car (memq symbol params))        ; It's either a simple symbol
4733       (cdr (assq symbol params))))      ; or a cons.
4734
4735 (defun gnus-group-add-parameter (group param)
4736   "Add parameter PARAM to GROUP."
4737   (let ((info (gnus-get-info group)))
4738     (if (not info)
4739         () ; This is a dead group.  We just ignore it.
4740       ;; Cons the new param to the old one and update.
4741       (gnus-group-set-info (cons param (gnus-info-params info))
4742                            group 'params))))
4743
4744 (defun gnus-group-set-parameter (group name value)
4745   "Set parameter NAME to VALUE in GROUP."
4746   (let ((info (gnus-get-info group)))
4747     (if (not info)
4748         () ; This is a dead group.  We just ignore it.
4749       (let ((old-params (gnus-info-params info))
4750             (new-params (list (cons name value))))
4751         (while old-params
4752           (if (or (not (listp (car old-params)))
4753                   (not (eq (caar old-params) name)))
4754               (setq new-params (append new-params (list (car old-params)))))
4755           (setq old-params (cdr old-params)))
4756         (gnus-group-set-info new-params group 'params)))))
4757
4758 (defun gnus-group-add-score (group &optional score)
4759   "Add SCORE to the GROUP score.
4760 If SCORE is nil, add 1 to the score of GROUP."
4761   (let ((info (gnus-get-info group)))
4762     (when info
4763       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4764
4765 (defun gnus-summary-bubble-group ()
4766   "Increase the score of the current group.
4767 This is a handy function to add to `gnus-summary-exit-hook' to
4768 increase the score of each group you read."
4769   (gnus-group-add-score gnus-newsgroup-name))
4770
4771 (defun gnus-group-set-info (info &optional method-only-group part)
4772   (let* ((entry (gnus-gethash
4773                  (or method-only-group (gnus-info-group info))
4774                  gnus-newsrc-hashtb))
4775          (part-info info)
4776          (info (if method-only-group (nth 2 entry) info))
4777          method)
4778     (when method-only-group
4779       (unless entry
4780         (error "Trying to change non-existent group %s" method-only-group))
4781       ;; We have received parts of the actual group info - either the
4782       ;; select method or the group parameters.  We first check
4783       ;; whether we have to extend the info, and if so, do that.
4784       (let ((len (length info))
4785             (total (if (eq part 'method) 5 6)))
4786         (when (< len total)
4787           (setcdr (nthcdr (1- len) info)
4788                   (make-list (- total len) nil)))
4789         ;; Then we enter the new info.
4790         (setcar (nthcdr (1- total) info) part-info)))
4791     (unless entry
4792       ;; This is a new group, so we just create it.
4793       (save-excursion
4794         (set-buffer gnus-group-buffer)
4795         (setq method (gnus-info-method info))
4796         (when (gnus-server-equal method "native")
4797           (setq method nil))
4798         (save-excursion
4799           (set-buffer gnus-group-buffer)
4800           (if method
4801               ;; It's a foreign group...
4802               (gnus-group-make-group
4803                (gnus-group-real-name (gnus-info-group info))
4804                (if (stringp method) method
4805                  (prin1-to-string (car method)))
4806                (and (consp method)
4807                     (nth 1 (gnus-info-method info))))
4808             ;; It's a native group.
4809             (gnus-group-make-group (gnus-info-group info))))
4810         (gnus-message 6 "Note: New group created")
4811         (setq entry
4812               (gnus-gethash (gnus-group-prefixed-name
4813                              (gnus-group-real-name (gnus-info-group info))
4814                              (or (gnus-info-method info) gnus-select-method))
4815                             gnus-newsrc-hashtb))))
4816     ;; Whether it was a new group or not, we now have the entry, so we
4817     ;; can do the update.
4818     (if entry
4819         (progn
4820           (setcar (nthcdr 2 entry) info)
4821           (when (and (not (eq (car entry) t))
4822                      (gnus-active (gnus-info-group info)))
4823             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4824       (error "No such group: %s" (gnus-info-group info)))))
4825
4826 (defun gnus-group-set-method-info (group select-method)
4827   (gnus-group-set-info select-method group 'method))
4828
4829 (defun gnus-group-set-params-info (group params)
4830   (gnus-group-set-info params group 'params))
4831
4832 (defun gnus-group-update-group-line ()
4833   "Update the current line in the group buffer."
4834   (let* ((buffer-read-only nil)
4835          (group (gnus-group-group-name))
4836          (gnus-group-indentation (gnus-group-group-indentation))
4837          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4838     (and entry
4839          (not (gnus-ephemeral-group-p group))
4840          (gnus-dribble-enter
4841           (concat "(gnus-group-set-info '"
4842                   (prin1-to-string (nth 2 entry)) ")")))
4843     (gnus-delete-line)
4844     (gnus-group-insert-group-line-info group)
4845     (forward-line -1)
4846     (gnus-group-position-point)))
4847
4848 (defun gnus-group-insert-group-line-info (group)
4849   "Insert GROUP on the current line."
4850   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4851         active info)
4852     (setq gnus-group-indentation (gnus-group-group-indentation))
4853     (if entry
4854         (progn
4855           ;; (Un)subscribed group.
4856           (setq info (nth 2 entry))
4857           (gnus-group-insert-group-line
4858            group (gnus-info-level info) (gnus-info-marks info)
4859            (or (car entry) t) (gnus-info-method info)))
4860       ;; This group is dead.
4861       (gnus-group-insert-group-line
4862        group
4863        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4864        nil
4865        (if (setq active (gnus-active group))
4866            (- (1+ (cdr active)) (car active)) 0)
4867        nil))))
4868
4869 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4870                                                     gnus-tmp-marked number
4871                                                     gnus-tmp-method)
4872   "Insert a group line in the group buffer."
4873   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4874          (gnus-tmp-number-total
4875           (if gnus-tmp-active
4876               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4877             0))
4878          (gnus-tmp-number-of-unread
4879           (if (numberp number) (int-to-string (max 0 number))
4880             "*"))
4881          (gnus-tmp-number-of-read
4882           (if (numberp number)
4883               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4884             "*"))
4885          (gnus-tmp-subscribed
4886           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4887                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4888                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4889                 (t ?K)))
4890          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4891          (gnus-tmp-newsgroup-description
4892           (if gnus-description-hashtb
4893               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4894             ""))
4895          (gnus-tmp-moderated
4896           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4897          (gnus-tmp-moderated-string
4898           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4899          (gnus-tmp-method
4900           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4901          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4902          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4903          (gnus-tmp-news-method-string
4904           (if gnus-tmp-method
4905               (format "(%s:%s)" (car gnus-tmp-method)
4906                       (cadr gnus-tmp-method)) ""))
4907          (gnus-tmp-marked-mark
4908           (if (and (numberp number)
4909                    (zerop number)
4910                    (cdr (assq 'tick gnus-tmp-marked)))
4911               ?* ? ))
4912          (gnus-tmp-process-marked
4913           (if (member gnus-tmp-group gnus-group-marked)
4914               gnus-process-mark ? ))
4915          (gnus-tmp-grouplens
4916           (or (and gnus-use-grouplens
4917                    (bbb-grouplens-group-p gnus-tmp-group))
4918               ""))
4919          (buffer-read-only nil)
4920          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4921     (beginning-of-line)
4922     (add-text-properties
4923      (point)
4924      (prog1 (1+ (point))
4925        ;; Insert the text.
4926        (eval gnus-group-line-format-spec))
4927      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4928        gnus-unread ,(if (numberp number)
4929                         (string-to-int gnus-tmp-number-of-unread)
4930                       t)
4931        gnus-marked ,gnus-tmp-marked-mark
4932        gnus-indentation ,gnus-group-indentation
4933        gnus-level ,gnus-tmp-level))
4934     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4935       (forward-line -1)
4936       (run-hooks 'gnus-group-update-hook)
4937       (forward-line))
4938     ;; Allow XEmacs to remove front-sticky text properties.
4939     (gnus-group-remove-excess-properties)))
4940
4941 (defun gnus-group-update-group (group &optional visible-only)
4942   "Update all lines where GROUP appear.
4943 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4944 already."
4945   (save-excursion
4946     (set-buffer gnus-group-buffer)
4947     ;; The buffer may be narrowed.
4948     (save-restriction
4949       (widen)
4950       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4951             (loc (point-min))
4952             found buffer-read-only)
4953         ;; Enter the current status into the dribble buffer.
4954         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4955           (if (and entry (not (gnus-ephemeral-group-p group)))
4956               (gnus-dribble-enter
4957                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4958                        ")"))))
4959         ;; Find all group instances.  If topics are in use, each group
4960         ;; may be listed in more than once.
4961         (while (setq loc (text-property-any
4962                           loc (point-max) 'gnus-group ident))
4963           (setq found t)
4964           (goto-char loc)
4965           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4966             (gnus-delete-line)
4967             (gnus-group-insert-group-line-info group))
4968           (setq loc (1+ loc)))
4969         (unless (or found visible-only)
4970           ;; No such line in the buffer, find out where it's supposed to
4971           ;; go, and insert it there (or at the end of the buffer).
4972           (if gnus-goto-missing-group-function
4973               (funcall gnus-goto-missing-group-function group)
4974             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4975               (while (and entry (car entry)
4976                           (not
4977                            (gnus-goto-char
4978                             (text-property-any
4979                              (point-min) (point-max)
4980                              'gnus-group (gnus-intern-safe
4981                                           (caar entry) gnus-active-hashtb)))))
4982                 (setq entry (cdr entry)))
4983               (or entry (goto-char (point-max)))))
4984           ;; Finally insert the line.
4985           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4986             (gnus-group-insert-group-line-info group)))
4987         (gnus-group-set-mode-line)))))
4988
4989 (defun gnus-group-set-mode-line ()
4990   (when (memq 'group gnus-updated-mode-lines)
4991     (let* ((gformat (or gnus-group-mode-line-format-spec
4992                         (setq gnus-group-mode-line-format-spec
4993                               (gnus-parse-format
4994                                gnus-group-mode-line-format
4995                                gnus-group-mode-line-format-alist))))
4996            (gnus-tmp-news-server (cadr gnus-select-method))
4997            (gnus-tmp-news-method (car gnus-select-method))
4998            (max-len 60)
4999            gnus-tmp-header                      ;Dummy binding for user-defined formats
5000            ;; Get the resulting string.
5001            (mode-string (eval gformat)))
5002       ;; If the line is too long, we chop it off.
5003       (when (> (length mode-string) max-len)
5004         (setq mode-string (substring mode-string 0 (- max-len 4))))
5005       (prog1
5006           (setq mode-line-buffer-identification (list mode-string))
5007         (set-buffer-modified-p t)))))
5008
5009 (defun gnus-group-group-name ()
5010   "Get the name of the newsgroup on the current line."
5011   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5012     (and group (symbol-name group))))
5013
5014 (defun gnus-group-group-level ()
5015   "Get the level of the newsgroup on the current line."
5016   (get-text-property (gnus-point-at-bol) 'gnus-level))
5017
5018 (defun gnus-group-group-indentation ()
5019   "Get the indentation of the newsgroup on the current line."
5020   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5021       (and gnus-group-indentation-function
5022            (funcall gnus-group-indentation-function))
5023       ""))
5024
5025 (defun gnus-group-group-unread ()
5026   "Get the number of unread articles of the newsgroup on the current line."
5027   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5028
5029 (defun gnus-group-search-forward (&optional backward all level first-too)
5030   "Find the next newsgroup with unread articles.
5031 If BACKWARD is non-nil, find the previous newsgroup instead.
5032 If ALL is non-nil, just find any newsgroup.
5033 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5034 group exists.
5035 If FIRST-TOO, the current line is also eligible as a target."
5036   (let ((way (if backward -1 1))
5037         (low gnus-level-killed)
5038         (beg (point))
5039         pos found lev)
5040     (if (and backward (progn (beginning-of-line)) (bobp))
5041         nil
5042       (or first-too (forward-line way))
5043       (while (and
5044               (not (eobp))
5045               (not (setq
5046                     found
5047                     (and (or all
5048                              (and
5049                               (let ((unread
5050                                      (get-text-property (point) 'gnus-unread)))
5051                                 (and (numberp unread) (> unread 0)))
5052                               (setq lev (get-text-property (point)
5053                                                            'gnus-level))
5054                               (<= lev gnus-level-subscribed)))
5055                          (or (not level)
5056                              (and (setq lev (get-text-property (point)
5057                                                                'gnus-level))
5058                                   (or (= lev level)
5059                                       (and (< lev low)
5060                                            (< level lev)
5061                                            (progn
5062                                              (setq low lev)
5063                                              (setq pos (point))
5064                                              nil))))))))
5065               (zerop (forward-line way)))))
5066     (if found
5067         (progn (gnus-group-position-point) t)
5068       (goto-char (or pos beg))
5069       (and pos t))))
5070
5071 ;;; Gnus group mode commands
5072
5073 ;; Group marking.
5074
5075 (defun gnus-group-mark-group (n &optional unmark no-advance)
5076   "Mark the current group."
5077   (interactive "p")
5078   (let ((buffer-read-only nil)
5079         group)
5080     (while
5081         (and (> n 0)
5082              (setq group (gnus-group-group-name))
5083              (progn
5084                (beginning-of-line)
5085                (forward-char
5086                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5087                (delete-char 1)
5088                (if unmark
5089                    (progn
5090                      (insert " ")
5091                      (setq gnus-group-marked (delete group gnus-group-marked)))
5092                  (insert "#")
5093                  (setq gnus-group-marked
5094                        (cons group (delete group gnus-group-marked))))
5095                t)
5096              (or no-advance (zerop (gnus-group-next-group 1))))
5097       (setq n (1- n)))
5098     (gnus-summary-position-point)
5099     n))
5100
5101 (defun gnus-group-unmark-group (n)
5102   "Remove the mark from the current group."
5103   (interactive "p")
5104   (gnus-group-mark-group n 'unmark)
5105   (gnus-group-position-point))
5106
5107 (defun gnus-group-unmark-all-groups ()
5108   "Unmark all groups."
5109   (interactive)
5110   (let ((groups gnus-group-marked))
5111     (save-excursion
5112       (while groups
5113         (gnus-group-remove-mark (pop groups)))))
5114   (gnus-group-position-point))
5115
5116 (defun gnus-group-mark-region (unmark beg end)
5117   "Mark all groups between point and mark.
5118 If UNMARK, remove the mark instead."
5119   (interactive "P\nr")
5120   (let ((num (count-lines beg end)))
5121     (save-excursion
5122       (goto-char beg)
5123       (- num (gnus-group-mark-group num unmark)))))
5124
5125 (defun gnus-group-mark-buffer (&optional unmark)
5126   "Mark all groups in the buffer.
5127 If UNMARK, remove the mark instead."
5128   (interactive "P")
5129   (gnus-group-mark-region unmark (point-min) (point-max)))
5130
5131 (defun gnus-group-mark-regexp (regexp)
5132   "Mark all groups that match some regexp."
5133   (interactive "sMark (regexp): ")
5134   (let ((alist (cdr gnus-newsrc-alist))
5135         group)
5136     (while alist
5137       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5138         (gnus-group-set-mark group))))
5139   (gnus-group-position-point))
5140
5141 (defun gnus-group-remove-mark (group)
5142   "Remove the process mark from GROUP and move point there.
5143 Return nil if the group isn't displayed."
5144   (if (gnus-group-goto-group group)
5145       (save-excursion
5146         (gnus-group-mark-group 1 'unmark t)
5147         t)
5148     (setq gnus-group-marked
5149           (delete group gnus-group-marked))
5150     nil))
5151
5152 (defun gnus-group-set-mark (group)
5153   "Set the process mark on GROUP."
5154   (if (gnus-group-goto-group group) 
5155       (save-excursion
5156         (gnus-group-mark-group 1 nil t))
5157     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5158
5159 (defun gnus-group-universal-argument (arg &optional groups func)
5160   "Perform any command on all groups accoring to the process/prefix convention."
5161   (interactive "P")
5162   (let ((groups (or groups (gnus-group-process-prefix arg)))
5163         group func)
5164     (if (eq (setq func (or func
5165                            (key-binding
5166                             (read-key-sequence
5167                              (substitute-command-keys
5168                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5169             'undefined)
5170         (gnus-error 1 "Undefined key")
5171       (while groups
5172         (gnus-group-remove-mark (setq group (pop groups)))
5173         (command-execute func))))
5174   (gnus-group-position-point))
5175
5176 (defun gnus-group-process-prefix (n)
5177   "Return a list of groups to work on.
5178 Take into consideration N (the prefix) and the list of marked groups."
5179   (cond
5180    (n
5181     (setq n (prefix-numeric-value n))
5182     ;; There is a prefix, so we return a list of the N next
5183     ;; groups.
5184     (let ((way (if (< n 0) -1 1))
5185           (n (abs n))
5186           group groups)
5187       (save-excursion
5188         (while (and (> n 0)
5189                     (setq group (gnus-group-group-name)))
5190           (setq groups (cons group groups))
5191           (setq n (1- n))
5192           (gnus-group-next-group way)))
5193       (nreverse groups)))
5194    ((and (boundp 'transient-mark-mode)
5195          transient-mark-mode
5196          (boundp 'mark-active)
5197          mark-active)
5198     ;; Work on the region between point and mark.
5199     (let ((max (max (point) (mark)))
5200           groups)
5201       (save-excursion
5202         (goto-char (min (point) (mark)))
5203         (while
5204             (and
5205              (push (gnus-group-group-name) groups)
5206              (zerop (gnus-group-next-group 1))
5207              (< (point) max)))
5208         (nreverse groups))))
5209    (gnus-group-marked
5210     ;; No prefix, but a list of marked articles.
5211     (reverse gnus-group-marked))
5212    (t
5213     ;; Neither marked articles or a prefix, so we return the
5214     ;; current group.
5215     (let ((group (gnus-group-group-name)))
5216       (and group (list group))))))
5217
5218 ;; Selecting groups.
5219
5220 (defun gnus-group-read-group (&optional all no-article group)
5221   "Read news in this newsgroup.
5222 If the prefix argument ALL is non-nil, already read articles become
5223 readable.  IF ALL is a number, fetch this number of articles.  If the
5224 optional argument NO-ARTICLE is non-nil, no article will be
5225 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5226 group."
5227   (interactive "P")
5228   (let ((group (or group (gnus-group-group-name)))
5229         number active marked entry)
5230     (or group (error "No group on current line"))
5231     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5232                                             group gnus-newsrc-hashtb)))))
5233     ;; This group might be a dead group.  In that case we have to get
5234     ;; the number of unread articles from `gnus-active-hashtb'.
5235     (setq number
5236           (cond ((numberp all) all)
5237                 (entry (car entry))
5238                 ((setq active (gnus-active group))
5239                  (- (1+ (cdr active)) (car active)))))
5240     (gnus-summary-read-group
5241      group (or all (and (numberp number)
5242                         (zerop (+ number (length (cdr (assq 'tick marked)))
5243                                   (length (cdr (assq 'dormant marked)))))))
5244      no-article)))
5245
5246 (defun gnus-group-select-group (&optional all)
5247   "Select this newsgroup.
5248 No article is selected automatically.
5249 If ALL is non-nil, already read articles become readable.
5250 If ALL is a number, fetch this number of articles."
5251   (interactive "P")
5252   (gnus-group-read-group all t))
5253
5254 (defun gnus-group-quick-select-group (&optional all)
5255   "Select the current group \"quickly\".
5256 This means that no highlighting or scoring will be performed."
5257   (interactive "P")
5258   (let (gnus-visual
5259         gnus-score-find-score-files-function
5260         gnus-apply-kill-hook
5261         gnus-summary-expunge-below)
5262     (gnus-group-read-group all t)))
5263
5264 (defun gnus-group-visible-select-group (&optional all)
5265   "Select the current group without hiding any articles."
5266   (interactive "P")
5267   (let ((gnus-inhibit-limiting t))
5268     (gnus-group-read-group all t)))
5269
5270 ;;;###autoload
5271 (defun gnus-fetch-group (group)
5272   "Start Gnus if necessary and enter GROUP.
5273 Returns whether the fetching was successful or not."
5274   (interactive "sGroup name: ")
5275   (or (get-buffer gnus-group-buffer)
5276       (gnus))
5277   (gnus-group-read-group nil nil group))
5278
5279 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5280 ;; if selection was successful.
5281 (defun gnus-group-read-ephemeral-group
5282   (group method &optional activate quit-config)
5283   (let ((group (if (gnus-group-foreign-p group) group
5284                  (gnus-group-prefixed-name group method))))
5285     (gnus-sethash
5286      group
5287      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5288                      ((quit-config . ,(if quit-config quit-config
5289                                         (cons (current-buffer) 'summary))))))
5290      gnus-newsrc-hashtb)
5291     (set-buffer gnus-group-buffer)
5292     (or (gnus-check-server method)
5293         (error "Unable to contact server: %s" (gnus-status-message method)))
5294     (if activate (or (gnus-request-group group)
5295                      (error "Couldn't request group")))
5296     (condition-case ()
5297         (gnus-group-read-group t t group)
5298       (error nil)
5299       (quit nil))))
5300
5301 (defun gnus-group-jump-to-group (group)
5302   "Jump to newsgroup GROUP."
5303   (interactive
5304    (list (completing-read
5305           "Group: " gnus-active-hashtb nil
5306           (memq gnus-select-method gnus-have-read-active-file)
5307           nil
5308           'gnus-group-history)))
5309
5310   (when (equal group "")
5311     (error "Empty group name"))
5312
5313   (when (string-match "[\000-\032]" group)
5314     (error "Control characters in group: %s" group))
5315
5316   (let ((b (text-property-any
5317             (point-min) (point-max)
5318             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5319     (unless (gnus-ephemeral-group-p group)
5320       (if b
5321           ;; Either go to the line in the group buffer...
5322           (goto-char b)
5323         ;; ... or insert the line.
5324         (or
5325          (gnus-active group)
5326          (gnus-activate-group group)
5327          (error "%s error: %s" group (gnus-status-message group)))
5328
5329         (gnus-group-update-group group)
5330         (goto-char (text-property-any
5331                     (point-min) (point-max)
5332                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5333     ;; Adjust cursor point.
5334     (gnus-group-position-point)))
5335
5336 (defun gnus-group-goto-group (group)
5337   "Goto to newsgroup GROUP."
5338   (when group
5339     (let ((b (text-property-any (point-min) (point-max)
5340                                 'gnus-group (gnus-intern-safe
5341                                              group gnus-active-hashtb))))
5342       (and b (goto-char b)))))
5343
5344 (defun gnus-group-next-group (n)
5345   "Go to next N'th newsgroup.
5346 If N is negative, search backward instead.
5347 Returns the difference between N and the number of skips actually
5348 done."
5349   (interactive "p")
5350   (gnus-group-next-unread-group n t))
5351
5352 (defun gnus-group-next-unread-group (n &optional all level)
5353   "Go to next N'th unread newsgroup.
5354 If N is negative, search backward instead.
5355 If ALL is non-nil, choose any newsgroup, unread or not.
5356 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5357 such group can be found, the next group with a level higher than
5358 LEVEL.
5359 Returns the difference between N and the number of skips actually
5360 made."
5361   (interactive "p")
5362   (let ((backward (< n 0))
5363         (n (abs n)))
5364     (while (and (> n 0)
5365                 (gnus-group-search-forward
5366                  backward (or (not gnus-group-goto-unread) all) level))
5367       (setq n (1- n)))
5368     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5369                                (if level " on this level or higher" "")))
5370     n))
5371
5372 (defun gnus-group-prev-group (n)
5373   "Go to previous N'th newsgroup.
5374 Returns the difference between N and the number of skips actually
5375 done."
5376   (interactive "p")
5377   (gnus-group-next-unread-group (- n) t))
5378
5379 (defun gnus-group-prev-unread-group (n)
5380   "Go to previous N'th unread newsgroup.
5381 Returns the difference between N and the number of skips actually
5382 done."
5383   (interactive "p")
5384   (gnus-group-next-unread-group (- n)))
5385
5386 (defun gnus-group-next-unread-group-same-level (n)
5387   "Go to next N'th unread newsgroup on the same level.
5388 If N is negative, search backward instead.
5389 Returns the difference between N and the number of skips actually
5390 done."
5391   (interactive "p")
5392   (gnus-group-next-unread-group n t (gnus-group-group-level))
5393   (gnus-group-position-point))
5394
5395 (defun gnus-group-prev-unread-group-same-level (n)
5396   "Go to next N'th unread newsgroup on the same level.
5397 Returns the difference between N and the number of skips actually
5398 done."
5399   (interactive "p")
5400   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5401   (gnus-group-position-point))
5402
5403 (defun gnus-group-best-unread-group (&optional exclude-group)
5404   "Go to the group with the highest level.
5405 If EXCLUDE-GROUP, do not go to that group."
5406   (interactive)
5407   (goto-char (point-min))
5408   (let ((best 100000)
5409         unread best-point)
5410     (while (not (eobp))
5411       (setq unread (get-text-property (point) 'gnus-unread))
5412       (if (and (numberp unread) (> unread 0))
5413           (progn
5414             (if (and (get-text-property (point) 'gnus-level)
5415                      (< (get-text-property (point) 'gnus-level) best)
5416                      (or (not exclude-group)
5417                          (not (equal exclude-group (gnus-group-group-name)))))
5418                 (progn
5419                   (setq best (get-text-property (point) 'gnus-level))
5420                   (setq best-point (point))))))
5421       (forward-line 1))
5422     (if best-point (goto-char best-point))
5423     (gnus-summary-position-point)
5424     (and best-point (gnus-group-group-name))))
5425
5426 (defun gnus-group-first-unread-group ()
5427   "Go to the first group with unread articles."
5428   (interactive)
5429   (prog1
5430       (let ((opoint (point))
5431             unread)
5432         (goto-char (point-min))
5433         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5434                 (and (numberp unread)   ; Not a topic.
5435                      (not (zerop unread))) ; Has unread articles.
5436                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5437             (point)                     ; Success.
5438           (goto-char opoint)
5439           nil))                         ; Not success.
5440     (gnus-group-position-point)))
5441
5442 (defun gnus-group-enter-server-mode ()
5443   "Jump to the server buffer."
5444   (interactive)
5445   (gnus-enter-server-buffer))
5446
5447 (defun gnus-group-make-group (name &optional method address)
5448   "Add a new newsgroup.
5449 The user will be prompted for a NAME, for a select METHOD, and an
5450 ADDRESS."
5451   (interactive
5452    (cons
5453     (read-string "Group name: ")
5454     (let ((method
5455            (completing-read
5456             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5457             nil t nil 'gnus-method-history)))
5458       (cond ((assoc method gnus-valid-select-methods)
5459              (list method
5460                    (if (memq 'prompt-address
5461                              (assoc method gnus-valid-select-methods))
5462                        (read-string "Address: ")
5463                      "")))
5464             ((assoc method gnus-server-alist)
5465              (list method))
5466             (t
5467              (list method ""))))))
5468
5469   (let* ((meth (and method (if address (list (intern method) address)
5470                              method)))
5471          (nname (if method (gnus-group-prefixed-name name meth) name))
5472          backend info)
5473     (when (gnus-gethash nname gnus-newsrc-hashtb)
5474       (error "Group %s already exists" nname))
5475     ;; Subscribe to the new group.
5476     (gnus-group-change-level
5477      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5478      gnus-level-default-subscribed gnus-level-killed
5479      (and (gnus-group-group-name)
5480           (gnus-gethash (gnus-group-group-name)
5481                         gnus-newsrc-hashtb))
5482      t)
5483     ;; Make it active.
5484     (gnus-set-active nname (cons 1 0))
5485     (or (gnus-ephemeral-group-p name)
5486         (gnus-dribble-enter
5487          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5488     ;; Insert the line.
5489     (gnus-group-insert-group-line-info nname)
5490     (forward-line -1)
5491     (gnus-group-position-point)
5492
5493     ;; Load the backend and try to make the backend create
5494     ;; the group as well.
5495     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5496                                                   nil meth))))
5497                  gnus-valid-select-methods)
5498       (require backend))
5499     (gnus-check-server meth)
5500     (and (gnus-check-backend-function 'request-create-group nname)
5501          (gnus-request-create-group nname))
5502     t))
5503
5504 (defun gnus-group-delete-group (group &optional force)
5505   "Delete the current group.
5506 If FORCE (the prefix) is non-nil, all the articles in the group will
5507 be deleted.  This is \"deleted\" as in \"removed forever from the face
5508 of the Earth\".  There is no undo."
5509   (interactive
5510    (list (gnus-group-group-name)
5511          current-prefix-arg))
5512   (or group (error "No group to rename"))
5513   (or (gnus-check-backend-function 'request-delete-group group)
5514       (error "This backend does not support group deletion"))
5515   (prog1
5516       (if (not (gnus-yes-or-no-p
5517                 (format
5518                  "Do you really want to delete %s%s? "
5519                  group (if force " and all its contents" ""))))
5520           () ; Whew!
5521         (gnus-message 6 "Deleting group %s..." group)
5522         (if (not (gnus-request-delete-group group force))
5523             (gnus-error 3 "Couldn't delete group %s" group)
5524           (gnus-message 6 "Deleting group %s...done" group)
5525           (gnus-group-goto-group group)
5526           (gnus-group-kill-group 1 t)
5527           (gnus-sethash group nil gnus-active-hashtb)
5528           t))
5529     (gnus-group-position-point)))
5530
5531 (defun gnus-group-rename-group (group new-name)
5532   (interactive
5533    (list
5534     (gnus-group-group-name)
5535     (progn
5536       (or (gnus-check-backend-function
5537            'request-rename-group (gnus-group-group-name))
5538           (error "This backend does not support renaming groups"))
5539       (read-string "New group name: "))))
5540
5541   (or (gnus-check-backend-function 'request-rename-group group)
5542       (error "This backend does not support renaming groups"))
5543
5544   (or group (error "No group to rename"))
5545   (and (string-match "^[ \t]*$" new-name)
5546        (error "Not a valid group name"))
5547
5548   ;; We find the proper prefixed name.
5549   (setq new-name
5550         (gnus-group-prefixed-name
5551          (gnus-group-real-name new-name)
5552          (gnus-info-method (gnus-get-info group))))
5553
5554   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5555   (prog1
5556       (if (not (gnus-request-rename-group group new-name))
5557           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5558         ;; We rename the group internally by killing it...
5559         (gnus-group-goto-group group)
5560         (gnus-group-kill-group)
5561         ;; ... changing its name ...
5562         (setcar (cdar gnus-list-of-killed-groups) new-name)
5563         ;; ... and then yanking it.  Magic!
5564         (gnus-group-yank-group)
5565         (gnus-set-active new-name (gnus-active group))
5566         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5567         new-name)
5568     (gnus-group-position-point)))
5569
5570 (defun gnus-group-edit-group (group &optional part)
5571   "Edit the group on the current line."
5572   (interactive (list (gnus-group-group-name)))
5573   (let* ((part (or part 'info))
5574          (done-func `(lambda ()
5575                        "Exit editing mode and update the information."
5576                        (interactive)
5577                        (gnus-group-edit-group-done ',part ,group)))
5578          (winconf (current-window-configuration))
5579          info)
5580     (or group (error "No group on current line"))
5581     (or (setq info (gnus-get-info group))
5582         (error "Killed group; can't be edited"))
5583     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5584     (gnus-configure-windows 'edit-group)
5585     (gnus-add-current-to-buffer-list)
5586     (emacs-lisp-mode)
5587     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5588     (use-local-map (copy-keymap emacs-lisp-mode-map))
5589     (local-set-key "\C-c\C-c" done-func)
5590     (make-local-variable 'gnus-prev-winconf)
5591     (setq gnus-prev-winconf winconf)
5592     (erase-buffer)
5593     (insert
5594      (cond
5595       ((eq part 'method)
5596        ";; Type `C-c C-c' after editing the select method.\n\n")
5597       ((eq part 'params)
5598        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5599       ((eq part 'info)
5600        ";; Type `C-c C-c' after editing the group info.\n\n")))
5601     (insert
5602      (pp-to-string
5603       (cond ((eq part 'method)
5604              (or (gnus-info-method info) "native"))
5605             ((eq part 'params)
5606              (gnus-info-params info))
5607             (t info)))
5608      "\n")))
5609
5610 (defun gnus-group-edit-group-method (group)
5611   "Edit the select method of GROUP."
5612   (interactive (list (gnus-group-group-name)))
5613   (gnus-group-edit-group group 'method))
5614
5615 (defun gnus-group-edit-group-parameters (group)
5616   "Edit the group parameters of GROUP."
5617   (interactive (list (gnus-group-group-name)))
5618   (gnus-group-edit-group group 'params))
5619
5620 (defun gnus-group-edit-group-done (part group)
5621   "Get info from buffer, update variables and jump to the group buffer."
5622   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5623   (goto-char (point-min))
5624   (let* ((form (read (current-buffer)))
5625          (winconf gnus-prev-winconf)
5626          (method (cond ((eq part 'info) (nth 4 form))
5627                        ((eq part 'method) form)
5628                        (t nil)))
5629          (info (cond ((eq part 'info) form)
5630                      ((eq part 'method) (gnus-get-info group))
5631                      (t nil)))
5632          (new-group (if info
5633                       (if (or (not method)
5634                               (gnus-server-equal
5635                                gnus-select-method method))
5636                           (gnus-group-real-name (car info))
5637                         (gnus-group-prefixed-name
5638                          (gnus-group-real-name (car info)) method))
5639                       nil)))
5640     (when (and new-group
5641                (not (equal new-group group)))
5642       (when (gnus-group-goto-group group)
5643         (gnus-group-kill-group 1))
5644       (gnus-activate-group new-group))
5645     ;; Set the info.
5646     (if (and info new-group)
5647         (progn
5648           (setq info (gnus-copy-sequence info))
5649           (setcar info new-group)
5650           (unless (gnus-server-equal method "native")
5651             (unless (nthcdr 3 info)
5652               (nconc info (list nil nil)))
5653             (unless (nthcdr 4 info)
5654               (nconc info (list nil)))
5655             (gnus-info-set-method info method))
5656           (gnus-group-set-info info))
5657       (gnus-group-set-info form (or new-group group) part))
5658     (kill-buffer (current-buffer))
5659     (and winconf (set-window-configuration winconf))
5660     (set-buffer gnus-group-buffer)
5661     (gnus-group-update-group (or new-group group))
5662     (gnus-group-position-point)))
5663
5664 (defun gnus-group-make-help-group ()
5665   "Create the Gnus documentation group."
5666   (interactive)
5667   (let ((path load-path)
5668         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5669         file dir)
5670     (and (gnus-gethash name gnus-newsrc-hashtb)
5671          (error "Documentation group already exists"))
5672     (while path
5673       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5674             file nil)
5675       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5676                 (file-exists-p
5677                  (setq file (concat (file-name-directory
5678                                      (directory-file-name dir))
5679                                     "etc/gnus-tut.txt"))))
5680         (setq path nil)))
5681     (if (not file)
5682         (gnus-message 1 "Couldn't find doc group")
5683       (gnus-group-make-group
5684        (gnus-group-real-name name)
5685        (list 'nndoc "gnus-help"
5686              (list 'nndoc-address file)
5687              (list 'nndoc-article-type 'mbox)))))
5688   (gnus-group-position-point))
5689
5690 (defun gnus-group-make-doc-group (file type)
5691   "Create a group that uses a single file as the source."
5692   (interactive
5693    (list (read-file-name "File name: ")
5694          (and current-prefix-arg 'ask)))
5695   (when (eq type 'ask)
5696     (let ((err "")
5697           char found)
5698       (while (not found)
5699         (message
5700          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5701          err)
5702         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5703                           ((= char ?b) 'babyl)
5704                           ((= char ?d) 'digest)
5705                           ((= char ?f) 'forward)
5706                           ((= char ?a) 'mmfd)
5707                           (t (setq err (format "%c unknown. " char))
5708                              nil))))
5709       (setq type found)))
5710   (let* ((file (expand-file-name file))
5711          (name (gnus-generate-new-group-name
5712                 (gnus-group-prefixed-name
5713                  (file-name-nondirectory file) '(nndoc "")))))
5714     (gnus-group-make-group
5715      (gnus-group-real-name name)
5716      (list 'nndoc (file-name-nondirectory file)
5717            (list 'nndoc-address file)
5718            (list 'nndoc-article-type (or type 'guess))))))
5719
5720 (defun gnus-group-make-archive-group (&optional all)
5721   "Create the (ding) Gnus archive group of the most recent articles.
5722 Given a prefix, create a full group."
5723   (interactive "P")
5724   (let ((group (gnus-group-prefixed-name
5725                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5726     (and (gnus-gethash group gnus-newsrc-hashtb)
5727          (error "Archive group already exists"))
5728     (gnus-group-make-group
5729      (gnus-group-real-name group)
5730      (list 'nndir (if all "hpc" "edu")
5731            (list 'nndir-directory
5732                  (if all gnus-group-archive-directory
5733                    gnus-group-recent-archive-directory))))))
5734
5735 (defun gnus-group-make-directory-group (dir)
5736   "Create an nndir group.
5737 The user will be prompted for a directory.  The contents of this
5738 directory will be used as a newsgroup.  The directory should contain
5739 mail messages or news articles in files that have numeric names."
5740   (interactive
5741    (list (read-file-name "Create group from directory: ")))
5742   (or (file-exists-p dir) (error "No such directory"))
5743   (or (file-directory-p dir) (error "Not a directory"))
5744   (let ((ext "")
5745         (i 0)
5746         group)
5747     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5748       (setq group
5749             (gnus-group-prefixed-name
5750              (concat (file-name-as-directory (directory-file-name dir))
5751                      ext)
5752              '(nndir "")))
5753       (setq ext (format "<%d>" (setq i (1+ i)))))
5754     (gnus-group-make-group
5755      (gnus-group-real-name group)
5756      (list 'nndir group (list 'nndir-directory dir)))))
5757
5758 (defun gnus-group-make-kiboze-group (group address scores)
5759   "Create an nnkiboze group.
5760 The user will be prompted for a name, a regexp to match groups, and
5761 score file entries for articles to include in the group."
5762   (interactive
5763    (list
5764     (read-string "nnkiboze group name: ")
5765     (read-string "Source groups (regexp): ")
5766     (let ((headers (mapcar (lambda (group) (list group))
5767                            '("subject" "from" "number" "date" "message-id"
5768                              "references" "chars" "lines" "xref"
5769                              "followup" "all" "body" "head")))
5770           scores header regexp regexps)
5771       (while (not (equal "" (setq header (completing-read
5772                                           "Match on header: " headers nil t))))
5773         (setq regexps nil)
5774         (while (not (equal "" (setq regexp (read-string
5775                                             (format "Match on %s (string): "
5776                                                     header)))))
5777           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5778         (setq scores (cons (cons header regexps) scores)))
5779       scores)))
5780   (gnus-group-make-group group "nnkiboze" address)
5781   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5782     (let (emacs-lisp-mode-hook)
5783       (pp scores (current-buffer)))))
5784
5785 (defun gnus-group-add-to-virtual (n vgroup)
5786   "Add the current group to a virtual group."
5787   (interactive
5788    (list current-prefix-arg
5789          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5790                           "nnvirtual:")))
5791   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5792       (error "%s is not an nnvirtual group" vgroup))
5793   (let* ((groups (gnus-group-process-prefix n))
5794          (method (gnus-info-method (gnus-get-info vgroup))))
5795     (setcar (cdr method)
5796             (concat
5797              (nth 1 method) "\\|"
5798              (mapconcat
5799               (lambda (s)
5800                 (gnus-group-remove-mark s)
5801                 (concat "\\(^" (regexp-quote s) "$\\)"))
5802               groups "\\|"))))
5803   (gnus-group-position-point))
5804
5805 (defun gnus-group-make-empty-virtual (group)
5806   "Create a new, fresh, empty virtual group."
5807   (interactive "sCreate new, empty virtual group: ")
5808   (let* ((method (list 'nnvirtual "^$"))
5809          (pgroup (gnus-group-prefixed-name group method)))
5810     ;; Check whether it exists already.
5811     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5812          (error "Group %s already exists." pgroup))
5813     ;; Subscribe the new group after the group on the current line.
5814     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5815     (gnus-group-update-group pgroup)
5816     (forward-line -1)
5817     (gnus-group-position-point)))
5818
5819 (defun gnus-group-enter-directory (dir)
5820   "Enter an ephemeral nneething group."
5821   (interactive "DDirectory to read: ")
5822   (let* ((method (list 'nneething dir))
5823          (leaf (gnus-group-prefixed-name
5824                 (file-name-nondirectory (directory-file-name dir))
5825                 method))
5826          (name (gnus-generate-new-group-name leaf)))
5827     (let ((nneething-read-only t))
5828       (or (gnus-group-read-ephemeral-group
5829            name method t
5830            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5831                                       'summary 'group)))
5832           (error "Couldn't enter %s" dir)))))
5833
5834 ;; Group sorting commands
5835 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5836
5837 (defun gnus-group-sort-groups (func &optional reverse)
5838   "Sort the group buffer according to FUNC.
5839 If REVERSE, reverse the sorting order."
5840   (interactive (list gnus-group-sort-function
5841                      current-prefix-arg))
5842   (let ((func (cond 
5843                ((not (listp func)) func)
5844                ((null func) func)
5845                ((= 1 (length func)) (car func))
5846                (t `(lambda (t1 t2)
5847                      ,(gnus-make-sort-function 
5848                        (reverse func)))))))
5849     ;; We peel off the dummy group from the alist.
5850     (when func
5851       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5852         (pop gnus-newsrc-alist))
5853       ;; Do the sorting.
5854       (setq gnus-newsrc-alist
5855             (sort gnus-newsrc-alist func))
5856       (when reverse
5857         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5858       ;; Regenerate the hash table.
5859       (gnus-make-hashtable-from-newsrc-alist)
5860       (gnus-group-list-groups))))
5861
5862 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5863   "Sort the group buffer alphabetically by group name.
5864 If REVERSE, sort in reverse order."
5865   (interactive "P")
5866   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5867
5868 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5869   "Sort the group buffer by number of unread articles.
5870 If REVERSE, sort in reverse order."
5871   (interactive "P")
5872   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5873
5874 (defun gnus-group-sort-groups-by-level (&optional reverse)
5875   "Sort the group buffer by group level.
5876 If REVERSE, sort in reverse order."
5877   (interactive "P")
5878   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5879
5880 (defun gnus-group-sort-groups-by-score (&optional reverse)
5881   "Sort the group buffer by group score.
5882 If REVERSE, sort in reverse order."
5883   (interactive "P")
5884   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5885
5886 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5887   "Sort the group buffer by group rank.
5888 If REVERSE, sort in reverse order."
5889   (interactive "P")
5890   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5891
5892 (defun gnus-group-sort-groups-by-method (&optional reverse)
5893   "Sort the group buffer alphabetically by backend name.
5894 If REVERSE, sort in reverse order."
5895   (interactive "P")
5896   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5897
5898 (defun gnus-group-sort-by-alphabet (info1 info2)
5899   "Sort alphabetically."
5900   (string< (gnus-info-group info1) (gnus-info-group info2)))
5901
5902 (defun gnus-group-sort-by-unread (info1 info2)
5903   "Sort by number of unread articles."
5904   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5905         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5906     (< (or (and (numberp n1) n1) 0)
5907        (or (and (numberp n2) n2) 0))))
5908
5909 (defun gnus-group-sort-by-level (info1 info2)
5910   "Sort by level."
5911   (< (gnus-info-level info1) (gnus-info-level info2)))
5912
5913 (defun gnus-group-sort-by-method (info1 info2)
5914   "Sort alphabetically by backend name."
5915   (string< (symbol-name (car (gnus-find-method-for-group
5916                               (gnus-info-group info1) info1)))
5917            (symbol-name (car (gnus-find-method-for-group
5918                               (gnus-info-group info2) info2)))))
5919
5920 (defun gnus-group-sort-by-score (info1 info2)
5921   "Sort by group score."
5922   (< (gnus-info-score info1) (gnus-info-score info2)))
5923
5924 (defun gnus-group-sort-by-rank (info1 info2)
5925   "Sort by level and score."
5926   (let ((level1 (gnus-info-level info1))
5927         (level2 (gnus-info-level info2)))
5928     (or (< level1 level2)
5929         (and (= level1 level2)
5930              (> (gnus-info-score info1) (gnus-info-score info2))))))
5931
5932 ;; Group catching up.
5933
5934 (defun gnus-group-clear-data (n)
5935   "Clear all marks and read ranges from the current group."
5936   (interactive "P")
5937   (let ((groups (gnus-group-process-prefix n))
5938         group info)
5939     (while (setq group (pop groups))
5940       (setq info (gnus-get-info group))
5941       (gnus-info-set-read info nil)
5942       (when (gnus-info-marks info)
5943         (gnus-info-set-marks info nil))
5944       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5945       (when (gnus-group-goto-group group)
5946         (gnus-group-remove-mark group)
5947         (gnus-group-update-group-line)))))
5948
5949 (defun gnus-group-catchup-current (&optional n all)
5950   "Mark all articles not marked as unread in current newsgroup as read.
5951 If prefix argument N is numeric, the ARG next newsgroups will be
5952 caught up.  If ALL is non-nil, marked articles will also be marked as
5953 read.  Cross references (Xref: header) of articles are ignored.
5954 The difference between N and actual number of newsgroups that were
5955 caught up is returned."
5956   (interactive "P")
5957   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5958                gnus-expert-user
5959                (gnus-y-or-n-p
5960                 (if all
5961                     "Do you really want to mark all articles as read? "
5962                   "Mark all unread articles as read? "))))
5963       n
5964     (let ((groups (gnus-group-process-prefix n))
5965           (ret 0))
5966       (while groups
5967         ;; Virtual groups have to be given special treatment.
5968         (let ((method (gnus-find-method-for-group (car groups))))
5969           (if (eq 'nnvirtual (car method))
5970               (nnvirtual-catchup-group
5971                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5972         (gnus-group-remove-mark (car groups))
5973         (if (>= (gnus-group-group-level) gnus-level-zombie)
5974             (gnus-message 2 "Dead groups can't be caught up")
5975           (if (prog1
5976                   (gnus-group-goto-group (car groups))
5977                 (gnus-group-catchup (car groups) all))
5978               (gnus-group-update-group-line)
5979             (setq ret (1+ ret))))
5980         (setq groups (cdr groups)))
5981       (gnus-group-next-unread-group 1)
5982       ret)))
5983
5984 (defun gnus-group-catchup-current-all (&optional n)
5985   "Mark all articles in current newsgroup as read.
5986 Cross references (Xref: header) of articles are ignored."
5987   (interactive "P")
5988   (gnus-group-catchup-current n 'all))
5989
5990 (defun gnus-group-catchup (group &optional all)
5991   "Mark all articles in GROUP as read.
5992 If ALL is non-nil, all articles are marked as read.
5993 The return value is the number of articles that were marked as read,
5994 or nil if no action could be taken."
5995   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5996          (num (car entry)))
5997     ;; Do the updating only if the newsgroup isn't killed.
5998     (if (not (numberp (car entry)))
5999         (gnus-message 1 "Can't catch up; non-active group")
6000       ;; Do auto-expirable marks if that's required.
6001       (when (gnus-group-auto-expirable-p group)
6002         (gnus-add-marked-articles
6003          group 'expire (gnus-list-of-unread-articles group))
6004         (when all
6005           (let ((marks (nth 3 (nth 2 entry))))
6006             (gnus-add-marked-articles
6007              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6008             (gnus-add-marked-articles
6009              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6010       (when entry
6011         (gnus-update-read-articles group nil)
6012         ;; Also nix out the lists of marks and dormants.
6013         (when all
6014           (gnus-add-marked-articles group 'tick nil nil 'force)
6015           (gnus-add-marked-articles group 'dormant nil nil 'force))
6016         (run-hooks 'gnus-group-catchup-group-hook)
6017         num))))
6018
6019 (defun gnus-group-expire-articles (&optional n)
6020   "Expire all expirable articles in the current newsgroup."
6021   (interactive "P")
6022   (let ((groups (gnus-group-process-prefix n))
6023         group)
6024     (unless groups
6025       (error "No groups to expire"))
6026     (while (setq group (pop groups))
6027       (gnus-group-remove-mark group)
6028       (when (gnus-check-backend-function 'request-expire-articles group)
6029         (gnus-message 6 "Expiring articles in %s..." group)
6030         (let* ((info (gnus-get-info group))
6031                (expirable (if (gnus-group-total-expirable-p group)
6032                               (cons nil (gnus-list-of-read-articles group))
6033                             (assq 'expire (gnus-info-marks info))))
6034                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6035           (when expirable
6036             (setcdr
6037              expirable
6038              (gnus-compress-sequence
6039               (if expiry-wait
6040                   ;; We set the expiry variables to the groupp
6041                   ;; parameter. 
6042                   (let ((nnmail-expiry-wait-function nil)
6043                         (nnmail-expiry-wait expiry-wait))
6044                     (gnus-request-expire-articles
6045                      (gnus-uncompress-sequence (cdr expirable)) group))
6046                 ;; Just expire using the normal expiry values.
6047                 (gnus-request-expire-articles
6048                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6049           (gnus-message 6 "Expiring articles in %s...done" group)))
6050       (gnus-group-position-point))))
6051
6052 (defun gnus-group-expire-all-groups ()
6053   "Expire all expirable articles in all newsgroups."
6054   (interactive)
6055   (save-excursion
6056     (gnus-message 5 "Expiring...")
6057     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6058                                      (cdr gnus-newsrc-alist))))
6059       (gnus-group-expire-articles nil)))
6060   (gnus-group-position-point)
6061   (gnus-message 5 "Expiring...done"))
6062
6063 (defun gnus-group-set-current-level (n level)
6064   "Set the level of the next N groups to LEVEL."
6065   (interactive
6066    (list
6067     current-prefix-arg
6068     (string-to-int
6069      (let ((s (read-string
6070                (format "Level (default %s): "
6071                        (or (gnus-group-group-level) 
6072                            gnus-level-default-subscribed)))))
6073        (if (string-match "^\\s-*$" s)
6074            (int-to-string (or (gnus-group-group-level) 
6075                               gnus-level-default-subscribed))
6076          s)))))
6077   (or (and (>= level 1) (<= level gnus-level-killed))
6078       (error "Illegal level: %d" level))
6079   (let ((groups (gnus-group-process-prefix n))
6080         group)
6081     (while (setq group (pop groups))
6082       (gnus-group-remove-mark group)
6083       (gnus-message 6 "Changed level of %s from %d to %d"
6084                     group (or (gnus-group-group-level) gnus-level-killed)
6085                     level)
6086       (gnus-group-change-level
6087        group level (or (gnus-group-group-level) gnus-level-killed))
6088       (gnus-group-update-group-line)))
6089   (gnus-group-position-point))
6090
6091 (defun gnus-group-unsubscribe-current-group (&optional n)
6092   "Toggle subscription of the current group.
6093 If given numerical prefix, toggle the N next groups."
6094   (interactive "P")
6095   (let ((groups (gnus-group-process-prefix n))
6096         group)
6097     (while groups
6098       (setq group (car groups)
6099             groups (cdr groups))
6100       (gnus-group-remove-mark group)
6101       (gnus-group-unsubscribe-group
6102        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6103                  gnus-level-default-unsubscribed
6104                gnus-level-default-subscribed) t)
6105       (gnus-group-update-group-line))
6106     (gnus-group-next-group 1)))
6107
6108 (defun gnus-group-unsubscribe-group (group &optional level silent)
6109   "Toggle subscription to GROUP.
6110 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6111 group line."
6112   (interactive
6113    (list (completing-read
6114           "Group: " gnus-active-hashtb nil
6115           (memq gnus-select-method gnus-have-read-active-file)
6116           nil 
6117           'gnus-group-history)))
6118   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6119     (cond
6120      ((string-match "^[ \t]$" group)
6121       (error "Empty group name"))
6122      (newsrc
6123       ;; Toggle subscription flag.
6124       (gnus-group-change-level
6125        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6126                                       gnus-level-subscribed)
6127                                   (1+ gnus-level-subscribed)
6128                                 gnus-level-default-subscribed)))
6129       (unless silent
6130         (gnus-group-update-group group)))
6131      ((and (stringp group)
6132            (or (not (memq gnus-select-method gnus-have-read-active-file))
6133                (gnus-active group)))
6134       ;; Add new newsgroup.
6135       (gnus-group-change-level
6136        group
6137        (if level level gnus-level-default-subscribed)
6138        (or (and (member group gnus-zombie-list)
6139                 gnus-level-zombie)
6140            gnus-level-killed)
6141        (and (gnus-group-group-name)
6142             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6143       (unless silent
6144         (gnus-group-update-group group)))
6145      (t (error "No such newsgroup: %s" group)))
6146     (gnus-group-position-point)))
6147
6148 (defun gnus-group-transpose-groups (n)
6149   "Move the current newsgroup up N places.
6150 If given a negative prefix, move down instead.  The difference between
6151 N and the number of steps taken is returned."
6152   (interactive "p")
6153   (or (gnus-group-group-name)
6154       (error "No group on current line"))
6155   (gnus-group-kill-group 1)
6156   (prog1
6157       (forward-line (- n))
6158     (gnus-group-yank-group)
6159     (gnus-group-position-point)))
6160
6161 (defun gnus-group-kill-all-zombies ()
6162   "Kill all zombie newsgroups."
6163   (interactive)
6164   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6165   (setq gnus-zombie-list nil)
6166   (gnus-group-list-groups))
6167
6168 (defun gnus-group-kill-region (begin end)
6169   "Kill newsgroups in current region (excluding current point).
6170 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6171   (interactive "r")
6172   (let ((lines
6173          ;; Count lines.
6174          (save-excursion
6175            (count-lines
6176             (progn
6177               (goto-char begin)
6178               (beginning-of-line)
6179               (point))
6180             (progn
6181               (goto-char end)
6182               (beginning-of-line)
6183               (point))))))
6184     (goto-char begin)
6185     (beginning-of-line)                 ;Important when LINES < 1
6186     (gnus-group-kill-group lines)))
6187
6188 (defun gnus-group-kill-group (&optional n discard)
6189   "Kill the next N groups.
6190 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6191 However, only groups that were alive can be yanked; already killed
6192 groups or zombie groups can't be yanked.
6193 The return value is the name of the group that was killed, or a list
6194 of groups killed."
6195   (interactive "P")
6196   (let ((buffer-read-only nil)
6197         (groups (gnus-group-process-prefix n))
6198         group entry level out)
6199     (if (< (length groups) 10)
6200         ;; This is faster when there are few groups.
6201         (while groups
6202           (push (setq group (pop groups)) out)
6203           (gnus-group-remove-mark group)
6204           (setq level (gnus-group-group-level))
6205           (gnus-delete-line)
6206           (when (and (not discard)
6207                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6208             (push (cons (car entry) (nth 2 entry))
6209                   gnus-list-of-killed-groups))
6210           (gnus-group-change-level
6211            (if entry entry group) gnus-level-killed (if entry nil level)))
6212       ;; If there are lots and lots of groups to be killed, we use
6213       ;; this thing instead.
6214       (let (entry)
6215         (setq groups (nreverse groups))
6216         (while groups
6217           (gnus-group-remove-mark (setq group (pop groups)))
6218           (gnus-delete-line)
6219           (cond
6220            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6221             (push (cons (car entry) (nth 2 entry))
6222                   gnus-list-of-killed-groups)
6223             (setcdr (cdr entry) (cdddr entry)))
6224            ((member group gnus-zombie-list)
6225             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6226         (gnus-make-hashtable-from-newsrc-alist)))
6227
6228     (gnus-group-position-point)
6229     (if (< (length out) 2) (car out) (nreverse out))))
6230
6231 (defun gnus-group-yank-group (&optional arg)
6232   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6233 inserting it before the current newsgroup.  The numeric ARG specifies
6234 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6235 is returned, or (if several groups are yanked) a list of yanked groups
6236 is returned."
6237   (interactive "p")
6238   (setq arg (or arg 1))
6239   (let (info group prev out)
6240     (while (>= (decf arg) 0)
6241       (if (not (setq info (pop gnus-list-of-killed-groups)))
6242           (error "No more newsgroups to yank"))
6243       (push (setq group (nth 1 info)) out)
6244       ;; Find which newsgroup to insert this one before - search
6245       ;; backward until something suitable is found.  If there are no
6246       ;; other newsgroups in this buffer, just make this newsgroup the
6247       ;; first newsgroup.
6248       (setq prev (gnus-group-group-name))
6249       (gnus-group-change-level
6250        info (gnus-info-level (cdr info)) gnus-level-killed
6251        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6252        t)
6253       (gnus-group-insert-group-line-info group))
6254     (forward-line -1)
6255     (gnus-group-position-point)
6256     (if (< (length out) 2) (car out) (nreverse out))))
6257
6258 (defun gnus-group-kill-level (level)
6259   "Kill all groups that is on a certain LEVEL."
6260   (interactive "nKill all groups on level: ")
6261   (cond
6262    ((= level gnus-level-zombie)
6263     (setq gnus-killed-list
6264           (nconc gnus-zombie-list gnus-killed-list))
6265     (setq gnus-zombie-list nil))
6266    ((and (< level gnus-level-zombie)
6267          (> level 0)
6268          (or gnus-expert-user
6269              (gnus-yes-or-no-p
6270               (format
6271                "Do you really want to kill all groups on level %d? "
6272                level))))
6273     (let* ((prev gnus-newsrc-alist)
6274            (alist (cdr prev)))
6275       (while alist
6276         (if (= (gnus-info-level level) level)
6277             (setcdr prev (cdr alist))
6278           (setq prev alist))
6279         (setq alist (cdr alist)))
6280       (gnus-make-hashtable-from-newsrc-alist)
6281       (gnus-group-list-groups)))
6282    (t
6283     (error "Can't kill; illegal level: %d" level))))
6284
6285 (defun gnus-group-list-all-groups (&optional arg)
6286   "List all newsgroups with level ARG or lower.
6287 Default is gnus-level-unsubscribed, which lists all subscribed and most
6288 unsubscribed groups."
6289   (interactive "P")
6290   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6291
6292 ;; Redefine this to list ALL killed groups if prefix arg used.
6293 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6294 (defun gnus-group-list-killed (&optional arg)
6295   "List all killed newsgroups in the group buffer.
6296 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6297 entail asking the server for the groups."
6298   (interactive "P")
6299   ;; Find all possible killed newsgroups if arg.
6300   (when arg
6301     ;; First make sure active file has been read.
6302     (unless gnus-have-read-active-file
6303       (let ((gnus-read-active-file t))
6304         (gnus-read-active-file)))
6305     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6306     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6307     (mapatoms
6308      (lambda (sym)
6309        (let ((groups 0)
6310              (group (symbol-name sym)))
6311          (if (or (null group)
6312                  (gnus-gethash group gnus-killed-hashtb)
6313                  (gnus-gethash group gnus-newsrc-hashtb))
6314              ()
6315            (let ((do-sub (gnus-matches-options-n group)))
6316              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6317                  ()
6318                (setq groups (1+ groups))
6319                (setq gnus-killed-list
6320                      (cons group gnus-killed-list))
6321                (gnus-sethash group group gnus-killed-hashtb))))))
6322      gnus-active-hashtb))
6323   (if (not gnus-killed-list)
6324       (gnus-message 6 "No killed groups")
6325     (let (gnus-group-list-mode)
6326       (funcall gnus-group-prepare-function
6327                gnus-level-killed t gnus-level-killed))
6328     (goto-char (point-min)))
6329   (gnus-group-position-point))
6330
6331 (defun gnus-group-list-zombies ()
6332   "List all zombie newsgroups in the group buffer."
6333   (interactive)
6334   (if (not gnus-zombie-list)
6335       (gnus-message 6 "No zombie groups")
6336     (let (gnus-group-list-mode)
6337       (funcall gnus-group-prepare-function
6338                gnus-level-zombie t gnus-level-zombie))
6339     (goto-char (point-min)))
6340   (gnus-group-position-point))
6341
6342 (defun gnus-group-list-active ()
6343   "List all groups that are available from the server(s)."
6344   (interactive)
6345   ;; First we make sure that we have really read the active file.
6346   (unless gnus-have-read-active-file
6347     (let ((gnus-read-active-file t))
6348       (gnus-read-active-file)))
6349   ;; Find all groups and sort them.
6350   (let ((groups
6351          (sort
6352           (let (list)
6353             (mapatoms
6354              (lambda (sym)
6355                (and (symbol-value sym)
6356                     (setq list (cons (symbol-name sym) list))))
6357              gnus-active-hashtb)
6358             list)
6359           'string<))
6360         (buffer-read-only nil))
6361     (erase-buffer)
6362     (while groups
6363       (gnus-group-insert-group-line-info (pop groups)))
6364     (goto-char (point-min))))
6365
6366 (defun gnus-activate-all-groups (level)
6367   "Activate absolutely all groups."
6368   (interactive (list 7))
6369   (let ((gnus-activate-level level)
6370         (gnus-activate-foreign-newsgroups level))
6371     (gnus-group-get-new-news)))
6372
6373 (defun gnus-group-get-new-news (&optional arg)
6374   "Get newly arrived articles.
6375 If ARG is a number, it specifies which levels you are interested in
6376 re-scanning.  If ARG is non-nil and not a number, this will force
6377 \"hard\" re-reading of the active files from all servers."
6378   (interactive "P")
6379   (run-hooks 'gnus-get-new-news-hook)
6380   ;; We might read in new NoCeM messages here.
6381   (when (and gnus-use-nocem 
6382              (null arg))
6383     (gnus-nocem-scan-groups))
6384   ;; If ARG is not a number, then we read the active file.
6385   (when (and arg (not (numberp arg)))
6386     (let ((gnus-read-active-file t))
6387       (gnus-read-active-file))
6388     (setq arg nil))
6389
6390   (setq arg (gnus-group-default-level arg t))
6391   (if (and gnus-read-active-file (not arg))
6392       (progn
6393         (gnus-read-active-file)
6394         (gnus-get-unread-articles arg))
6395     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6396       (gnus-get-unread-articles arg)))
6397   (run-hooks 'gnus-after-getting-new-news-hook)
6398   (gnus-group-list-groups))
6399
6400 (defun gnus-group-get-new-news-this-group (&optional n)
6401   "Check for newly arrived news in the current group (and the N-1 next groups).
6402 The difference between N and the number of newsgroup checked is returned.
6403 If N is negative, this group and the N-1 previous groups will be checked."
6404   (interactive "P")
6405   (let* ((groups (gnus-group-process-prefix n))
6406          (ret (if (numberp n) (- n (length groups)) 0))
6407          (beg (unless n (point)))
6408          group)
6409     (while (setq group (pop groups))
6410       (gnus-group-remove-mark group)
6411       (if (gnus-activate-group group 'scan)
6412           (progn
6413             (gnus-get-unread-articles-in-group
6414              (gnus-get-info group) (gnus-active group) t)
6415             (unless (gnus-virtual-group-p group)
6416               (gnus-close-group group))
6417             (gnus-group-update-group group))
6418         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6419     (when beg (goto-char beg))
6420     (when gnus-goto-next-group-when-activating
6421       (gnus-group-next-unread-group 1 t))
6422     (gnus-summary-position-point)
6423     ret))
6424
6425 (defun gnus-group-fetch-faq (group &optional faq-dir)
6426   "Fetch the FAQ for the current group."
6427   (interactive
6428    (list
6429     (gnus-group-real-name (gnus-group-group-name))
6430     (cond (current-prefix-arg
6431            (completing-read
6432             "Faq dir: " (and (listp gnus-group-faq-directory)
6433                              gnus-group-faq-directory))))))
6434   (or faq-dir
6435       (setq faq-dir (if (listp gnus-group-faq-directory)
6436                         (car gnus-group-faq-directory)
6437                       gnus-group-faq-directory)))
6438   (or group (error "No group name given"))
6439   (let ((file (concat (file-name-as-directory faq-dir)
6440                       (gnus-group-real-name group))))
6441     (if (not (file-exists-p file))
6442         (error "No such file: %s" file)
6443       (find-file file))))
6444
6445 (defun gnus-group-describe-group (force &optional group)
6446   "Display a description of the current newsgroup."
6447   (interactive (list current-prefix-arg (gnus-group-group-name)))
6448   (and force (setq gnus-description-hashtb nil))
6449   (let ((method (gnus-find-method-for-group group))
6450         desc)
6451     (or group (error "No group name given"))
6452     (and (or (and gnus-description-hashtb
6453                   ;; We check whether this group's method has been
6454                   ;; queried for a description file.
6455                   (gnus-gethash
6456                    (gnus-group-prefixed-name "" method)
6457                    gnus-description-hashtb))
6458              (setq desc (gnus-group-get-description group))
6459              (gnus-read-descriptions-file method))
6460          (gnus-message 1
6461           (or desc (gnus-gethash group gnus-description-hashtb)
6462               "No description available")))))
6463
6464 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6465 (defun gnus-group-describe-all-groups (&optional force)
6466   "Pop up a buffer with descriptions of all newsgroups."
6467   (interactive "P")
6468   (and force (setq gnus-description-hashtb nil))
6469   (if (not (or gnus-description-hashtb
6470                (gnus-read-all-descriptions-files)))
6471       (error "Couldn't request descriptions file"))
6472   (let ((buffer-read-only nil)
6473         b)
6474     (erase-buffer)
6475     (mapatoms
6476      (lambda (group)
6477        (setq b (point))
6478        (insert (format "      *: %-20s %s\n" (symbol-name group)
6479                        (symbol-value group)))
6480        (add-text-properties
6481         b (1+ b) (list 'gnus-group group
6482                        'gnus-unread t 'gnus-marked nil
6483                        'gnus-level (1+ gnus-level-subscribed))))
6484      gnus-description-hashtb)
6485     (goto-char (point-min))
6486     (gnus-group-position-point)))
6487
6488 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6489 (defun gnus-group-apropos (regexp &optional search-description)
6490   "List all newsgroups that have names that match a regexp."
6491   (interactive "sGnus apropos (regexp): ")
6492   (let ((prev "")
6493         (obuf (current-buffer))
6494         groups des)
6495     ;; Go through all newsgroups that are known to Gnus.
6496     (mapatoms
6497      (lambda (group)
6498        (and (symbol-name group)
6499             (string-match regexp (symbol-name group))
6500             (setq groups (cons (symbol-name group) groups))))
6501      gnus-active-hashtb)
6502     ;; Also go through all descriptions that are known to Gnus.
6503     (when search-description
6504       (mapatoms
6505        (lambda (group)
6506          (and (string-match regexp (symbol-value group))
6507               (gnus-active (symbol-name group))
6508               (setq groups (cons (symbol-name group) groups))))
6509        gnus-description-hashtb))
6510     (if (not groups)
6511         (gnus-message 3 "No groups matched \"%s\"." regexp)
6512       ;; Print out all the groups.
6513       (save-excursion
6514         (pop-to-buffer "*Gnus Help*")
6515         (buffer-disable-undo (current-buffer))
6516         (erase-buffer)
6517         (setq groups (sort groups 'string<))
6518         (while groups
6519           ;; Groups may be entered twice into the list of groups.
6520           (if (not (string= (car groups) prev))
6521               (progn
6522                 (insert (setq prev (car groups)) "\n")
6523                 (if (and gnus-description-hashtb
6524                          (setq des (gnus-gethash (car groups)
6525                                                  gnus-description-hashtb)))
6526                     (insert "  " des "\n"))))
6527           (setq groups (cdr groups)))
6528         (goto-char (point-min))))
6529     (pop-to-buffer obuf)))
6530
6531 (defun gnus-group-description-apropos (regexp)
6532   "List all newsgroups that have names or descriptions that match a regexp."
6533   (interactive "sGnus description apropos (regexp): ")
6534   (if (not (or gnus-description-hashtb
6535                (gnus-read-all-descriptions-files)))
6536       (error "Couldn't request descriptions file"))
6537   (gnus-group-apropos regexp t))
6538
6539 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6540 (defun gnus-group-list-matching (level regexp &optional all lowest)
6541   "List all groups with unread articles that match REGEXP.
6542 If the prefix LEVEL is non-nil, it should be a number that says which
6543 level to cut off listing groups.
6544 If ALL, also list groups with no unread articles.
6545 If LOWEST, don't list groups with level lower than LOWEST."
6546   (interactive "P\nsList newsgroups matching: ")
6547   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6548                            all (or lowest 1) regexp)
6549   (goto-char (point-min))
6550   (gnus-group-position-point))
6551
6552 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6553   "List all groups that match REGEXP.
6554 If the prefix LEVEL is non-nil, it should be a number that says which
6555 level to cut off listing groups.
6556 If LOWEST, don't list groups with level lower than LOWEST."
6557   (interactive "P\nsList newsgroups matching: ")
6558   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6559
6560 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6561 (defun gnus-group-save-newsrc (&optional force)
6562   "Save the Gnus startup files.
6563 If FORCE, force saving whether it is necessary or not."
6564   (interactive "P")
6565   (gnus-save-newsrc-file force))
6566
6567 (defun gnus-group-restart (&optional arg)
6568   "Force Gnus to read the .newsrc file."
6569   (interactive "P")
6570   (when (gnus-yes-or-no-p
6571          (format "Are you sure you want to read %s? "
6572                  gnus-current-startup-file))
6573     (gnus-save-newsrc-file)
6574     (gnus-setup-news 'force)
6575     (gnus-group-list-groups arg)))
6576
6577 (defun gnus-group-read-init-file ()
6578   "Read the Gnus elisp init file."
6579   (interactive)
6580   (gnus-read-init-file))
6581
6582 (defun gnus-group-check-bogus-groups (&optional silent)
6583   "Check bogus newsgroups.
6584 If given a prefix, don't ask for confirmation before removing a bogus
6585 group."
6586   (interactive "P")
6587   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6588   (gnus-group-list-groups))
6589
6590 (defun gnus-group-edit-global-kill (&optional article group)
6591   "Edit the global kill file.
6592 If GROUP, edit that local kill file instead."
6593   (interactive "P")
6594   (setq gnus-current-kill-article article)
6595   (gnus-kill-file-edit-file group)
6596   (gnus-message
6597    6
6598    (substitute-command-keys
6599     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6600             (if group "local" "global")))))
6601
6602 (defun gnus-group-edit-local-kill (article group)
6603   "Edit a local kill file."
6604   (interactive (list nil (gnus-group-group-name)))
6605   (gnus-group-edit-global-kill article group))
6606
6607 (defun gnus-group-force-update ()
6608   "Update `.newsrc' file."
6609   (interactive)
6610   (gnus-save-newsrc-file))
6611
6612 (defun gnus-group-suspend ()
6613   "Suspend the current Gnus session.
6614 In fact, cleanup buffers except for group mode buffer.
6615 The hook gnus-suspend-gnus-hook is called before actually suspending."
6616   (interactive)
6617   (run-hooks 'gnus-suspend-gnus-hook)
6618   ;; Kill Gnus buffers except for group mode buffer.
6619   (let ((group-buf (get-buffer gnus-group-buffer)))
6620     ;; Do this on a separate list in case the user does a ^G before we finish
6621     (let ((gnus-buffer-list
6622            (delq group-buf (delq gnus-dribble-buffer
6623                                  (append gnus-buffer-list nil)))))
6624       (while gnus-buffer-list
6625         (gnus-kill-buffer (car gnus-buffer-list))
6626         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6627     (if group-buf
6628         (progn
6629           (setq gnus-buffer-list (list group-buf))
6630           (bury-buffer group-buf)
6631           (delete-windows-on group-buf t)))))
6632
6633 (defun gnus-group-clear-dribble ()
6634   "Clear all information from the dribble buffer."
6635   (interactive)
6636   (gnus-dribble-clear)
6637   (gnus-message 7 "Cleared dribble buffer"))
6638
6639 (defun gnus-group-exit ()
6640   "Quit reading news after updating .newsrc.eld and .newsrc.
6641 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6642   (interactive)
6643   (when 
6644       (or noninteractive                ;For gnus-batch-kill
6645           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6646           (not gnus-interactive-exit)   ;Without confirmation
6647           gnus-expert-user
6648           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6649     (run-hooks 'gnus-exit-gnus-hook)
6650     ;; Offer to save data from non-quitted summary buffers.
6651     (gnus-offer-save-summaries)
6652     ;; Save the newsrc file(s).
6653     (gnus-save-newsrc-file)
6654     ;; Kill-em-all.
6655     (gnus-close-backends)
6656     ;; Reset everything.
6657     (gnus-clear-system)
6658     ;; Allow the user to do things after cleaning up.
6659     (run-hooks 'gnus-after-exiting-gnus-hook)))
6660
6661 (defun gnus-close-backends ()
6662   ;; Send a close request to all backends that support such a request.
6663   (let ((methods gnus-valid-select-methods)
6664         func)
6665     (while methods
6666       (if (fboundp (setq func (intern (concat (caar methods)
6667                                               "-request-close"))))
6668           (funcall func))
6669       (setq methods (cdr methods)))))
6670
6671 (defun gnus-group-quit ()
6672   "Quit reading news without updating .newsrc.eld or .newsrc.
6673 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6674   (interactive)
6675   (when (or noninteractive              ;For gnus-batch-kill
6676             (zerop (buffer-size))
6677             (not (gnus-server-opened gnus-select-method))
6678             gnus-expert-user
6679             (not gnus-current-startup-file)
6680             (gnus-yes-or-no-p
6681              (format "Quit reading news without saving %s? "
6682                      (file-name-nondirectory gnus-current-startup-file))))
6683     (run-hooks 'gnus-exit-gnus-hook)
6684     (if gnus-use-full-window
6685         (delete-other-windows)
6686       (gnus-remove-some-windows))
6687     (gnus-dribble-save)
6688     (gnus-close-backends)
6689     (gnus-clear-system)
6690     ;; Allow the user to do things after cleaning up.
6691     (run-hooks 'gnus-after-exiting-gnus-hook)))
6692
6693 (defun gnus-offer-save-summaries ()
6694   "Offer to save all active summary buffers."
6695   (save-excursion
6696     (let ((buflist (buffer-list))
6697           buffers bufname)
6698       ;; Go through all buffers and find all summaries.
6699       (while buflist
6700         (and (setq bufname (buffer-name (car buflist)))
6701              (string-match "Summary" bufname)
6702              (save-excursion
6703                (set-buffer bufname)
6704                ;; We check that this is, indeed, a summary buffer.
6705                (and (eq major-mode 'gnus-summary-mode)
6706                     ;; Also make sure this isn't bogus.
6707                     gnus-newsgroup-prepared))
6708              (push bufname buffers))
6709         (setq buflist (cdr buflist)))
6710       ;; Go through all these summary buffers and offer to save them.
6711       (when buffers
6712         (map-y-or-n-p
6713          "Update summary buffer %s? "
6714          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6715          buffers)))))
6716
6717 (defun gnus-group-describe-briefly ()
6718   "Give a one line description of the group mode commands."
6719   (interactive)
6720   (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")))
6721
6722 (defun gnus-group-browse-foreign-server (method)
6723   "Browse a foreign news server.
6724 If called interactively, this function will ask for a select method
6725  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6726 If not, METHOD should be a list where the first element is the method
6727 and the second element is the address."
6728   (interactive
6729    (list (let ((how (completing-read
6730                      "Which backend: "
6731                      (append gnus-valid-select-methods gnus-server-alist)
6732                      nil t (cons "nntp" 0) 'gnus-method-history)))
6733            ;; We either got a backend name or a virtual server name.
6734            ;; If the first, we also need an address.
6735            (if (assoc how gnus-valid-select-methods)
6736                (list (intern how)
6737                      ;; Suggested by mapjph@bath.ac.uk.
6738                      (completing-read
6739                       "Address: "
6740                       (mapcar (lambda (server) (list server))
6741                               gnus-secondary-servers)))
6742              ;; We got a server name, so we find the method.
6743              (gnus-server-to-method how)))))
6744   (gnus-browse-foreign-server method))
6745
6746 \f
6747 ;;;
6748 ;;; Gnus summary mode
6749 ;;;
6750
6751 (defvar gnus-summary-mode-map nil)
6752
6753 (put 'gnus-summary-mode 'mode-class 'special)
6754
6755 (unless gnus-summary-mode-map
6756   (setq gnus-summary-mode-map (make-keymap))
6757   (suppress-keymap gnus-summary-mode-map)
6758
6759   ;; Non-orthogonal keys
6760
6761   (gnus-define-keys gnus-summary-mode-map
6762     " " gnus-summary-next-page
6763     "\177" gnus-summary-prev-page
6764     [delete] gnus-summary-prev-page
6765     "\r" gnus-summary-scroll-up
6766     "n" gnus-summary-next-unread-article
6767     "p" gnus-summary-prev-unread-article
6768     "N" gnus-summary-next-article
6769     "P" gnus-summary-prev-article
6770     "\M-\C-n" gnus-summary-next-same-subject
6771     "\M-\C-p" gnus-summary-prev-same-subject
6772     "\M-n" gnus-summary-next-unread-subject
6773     "\M-p" gnus-summary-prev-unread-subject
6774     "." gnus-summary-first-unread-article
6775     "," gnus-summary-best-unread-article
6776     "\M-s" gnus-summary-search-article-forward
6777     "\M-r" gnus-summary-search-article-backward
6778     "<" gnus-summary-beginning-of-article
6779     ">" gnus-summary-end-of-article
6780     "j" gnus-summary-goto-article
6781     "^" gnus-summary-refer-parent-article
6782     "\M-^" gnus-summary-refer-article
6783     "u" gnus-summary-tick-article-forward
6784     "!" gnus-summary-tick-article-forward
6785     "U" gnus-summary-tick-article-backward
6786     "d" gnus-summary-mark-as-read-forward
6787     "D" gnus-summary-mark-as-read-backward
6788     "E" gnus-summary-mark-as-expirable
6789     "\M-u" gnus-summary-clear-mark-forward
6790     "\M-U" gnus-summary-clear-mark-backward
6791     "k" gnus-summary-kill-same-subject-and-select
6792     "\C-k" gnus-summary-kill-same-subject
6793     "\M-\C-k" gnus-summary-kill-thread
6794     "\M-\C-l" gnus-summary-lower-thread
6795     "e" gnus-summary-edit-article
6796     "#" gnus-summary-mark-as-processable
6797     "\M-#" gnus-summary-unmark-as-processable
6798     "\M-\C-t" gnus-summary-toggle-threads
6799     "\M-\C-s" gnus-summary-show-thread
6800     "\M-\C-h" gnus-summary-hide-thread
6801     "\M-\C-f" gnus-summary-next-thread
6802     "\M-\C-b" gnus-summary-prev-thread
6803     "\M-\C-u" gnus-summary-up-thread
6804     "\M-\C-d" gnus-summary-down-thread
6805     "&" gnus-summary-execute-command
6806     "c" gnus-summary-catchup-and-exit
6807     "\C-w" gnus-summary-mark-region-as-read
6808     "\C-t" gnus-summary-toggle-truncation
6809     "?" gnus-summary-mark-as-dormant
6810     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6811     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6812     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6813     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6814     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6815     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6816     "=" gnus-summary-expand-window
6817     "\C-x\C-s" gnus-summary-reselect-current-group
6818     "\M-g" gnus-summary-rescan-group
6819     "w" gnus-summary-stop-page-breaking
6820     "\C-c\C-r" gnus-summary-caesar-message
6821     "\M-t" gnus-summary-toggle-mime
6822     "f" gnus-summary-followup
6823     "F" gnus-summary-followup-with-original
6824     "C" gnus-summary-cancel-article
6825     "r" gnus-summary-reply
6826     "R" gnus-summary-reply-with-original
6827     "\C-c\C-f" gnus-summary-mail-forward
6828     "o" gnus-summary-save-article
6829     "\C-o" gnus-summary-save-article-mail
6830     "|" gnus-summary-pipe-output
6831     "\M-k" gnus-summary-edit-local-kill
6832     "\M-K" gnus-summary-edit-global-kill
6833     "V" gnus-version
6834     "\C-c\C-d" gnus-summary-describe-group
6835     "q" gnus-summary-exit
6836     "Q" gnus-summary-exit-no-update
6837     "\C-c\C-i" gnus-info-find-node
6838     gnus-mouse-2 gnus-mouse-pick-article
6839     "m" gnus-summary-mail-other-window
6840     "a" gnus-summary-post-news
6841     "x" gnus-summary-limit-to-unread
6842     "s" gnus-summary-isearch-article
6843     "t" gnus-article-hide-headers
6844     "g" gnus-summary-show-article
6845     "l" gnus-summary-goto-last-article
6846     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6847     "\C-d" gnus-summary-enter-digest-group
6848     "\C-c\C-b" gnus-bug
6849     "*" gnus-cache-enter-article
6850     "\M-*" gnus-cache-remove-article
6851     "\M-&" gnus-summary-universal-argument
6852     "\C-l" gnus-recenter
6853     "I" gnus-summary-increase-score
6854     "L" gnus-summary-lower-score
6855
6856     "V" gnus-summary-score-map
6857     "X" gnus-uu-extract-map
6858     "S" gnus-summary-send-map)
6859
6860   ;; Sort of orthogonal keymap
6861   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6862     "t" gnus-summary-tick-article-forward
6863     "!" gnus-summary-tick-article-forward
6864     "d" gnus-summary-mark-as-read-forward
6865     "r" gnus-summary-mark-as-read-forward
6866     "c" gnus-summary-clear-mark-forward
6867     " " gnus-summary-clear-mark-forward
6868     "e" gnus-summary-mark-as-expirable
6869     "x" gnus-summary-mark-as-expirable
6870     "?" gnus-summary-mark-as-dormant
6871     "b" gnus-summary-set-bookmark
6872     "B" gnus-summary-remove-bookmark
6873     "#" gnus-summary-mark-as-processable
6874     "\M-#" gnus-summary-unmark-as-processable
6875     "S" gnus-summary-limit-include-expunged
6876     "C" gnus-summary-catchup
6877     "H" gnus-summary-catchup-to-here
6878     "\C-c" gnus-summary-catchup-all
6879     "k" gnus-summary-kill-same-subject-and-select
6880     "K" gnus-summary-kill-same-subject
6881     "P" gnus-uu-mark-map)
6882
6883   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6884     "c" gnus-summary-clear-above
6885     "u" gnus-summary-tick-above
6886     "m" gnus-summary-mark-above
6887     "k" gnus-summary-kill-below)
6888
6889   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6890     "/" gnus-summary-limit-to-subject
6891     "n" gnus-summary-limit-to-articles
6892     "w" gnus-summary-pop-limit
6893     "s" gnus-summary-limit-to-subject
6894     "a" gnus-summary-limit-to-author
6895     "u" gnus-summary-limit-to-unread
6896     "m" gnus-summary-limit-to-marks
6897     "v" gnus-summary-limit-to-score
6898     "D" gnus-summary-limit-include-dormant
6899     "d" gnus-summary-limit-exclude-dormant
6900     ;;  "t" gnus-summary-limit-exclude-thread
6901     "E" gnus-summary-limit-include-expunged
6902     "c" gnus-summary-limit-exclude-childless-dormant
6903     "C" gnus-summary-limit-mark-excluded-as-read)
6904
6905   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6906     "n" gnus-summary-next-unread-article
6907     "p" gnus-summary-prev-unread-article
6908     "N" gnus-summary-next-article
6909     "P" gnus-summary-prev-article
6910     "\C-n" gnus-summary-next-same-subject
6911     "\C-p" gnus-summary-prev-same-subject
6912     "\M-n" gnus-summary-next-unread-subject
6913     "\M-p" gnus-summary-prev-unread-subject
6914     "f" gnus-summary-first-unread-article
6915     "b" gnus-summary-best-unread-article
6916     "j" gnus-summary-goto-article
6917     "g" gnus-summary-goto-subject
6918     "l" gnus-summary-goto-last-article
6919     "p" gnus-summary-pop-article)
6920
6921   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6922     "k" gnus-summary-kill-thread
6923     "l" gnus-summary-lower-thread
6924     "i" gnus-summary-raise-thread
6925     "T" gnus-summary-toggle-threads
6926     "t" gnus-summary-rethread-current
6927     "^" gnus-summary-reparent-thread
6928     "s" gnus-summary-show-thread
6929     "S" gnus-summary-show-all-threads
6930     "h" gnus-summary-hide-thread
6931     "H" gnus-summary-hide-all-threads
6932     "n" gnus-summary-next-thread
6933     "p" gnus-summary-prev-thread
6934     "u" gnus-summary-up-thread
6935     "o" gnus-summary-top-thread
6936     "d" gnus-summary-down-thread
6937     "#" gnus-uu-mark-thread
6938     "\M-#" gnus-uu-unmark-thread)
6939
6940   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6941     "c" gnus-summary-catchup-and-exit
6942     "C" gnus-summary-catchup-all-and-exit
6943     "E" gnus-summary-exit-no-update
6944     "Q" gnus-summary-exit
6945     "Z" gnus-summary-exit
6946     "n" gnus-summary-catchup-and-goto-next-group
6947     "R" gnus-summary-reselect-current-group
6948     "G" gnus-summary-rescan-group
6949     "N" gnus-summary-next-group
6950     "P" gnus-summary-prev-group)
6951
6952   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6953     " " gnus-summary-next-page
6954     "n" gnus-summary-next-page
6955     "\177" gnus-summary-prev-page
6956     [delete] gnus-summary-prev-page
6957     "p" gnus-summary-prev-page
6958     "\r" gnus-summary-scroll-up
6959     "<" gnus-summary-beginning-of-article
6960     ">" gnus-summary-end-of-article
6961     "b" gnus-summary-beginning-of-article
6962     "e" gnus-summary-end-of-article
6963     "^" gnus-summary-refer-parent-article
6964     "r" gnus-summary-refer-parent-article
6965     "R" gnus-summary-refer-references
6966     "g" gnus-summary-show-article
6967     "s" gnus-summary-isearch-article)
6968
6969   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6970     "b" gnus-article-add-buttons
6971     "B" gnus-article-add-buttons-to-head
6972     "o" gnus-article-treat-overstrike
6973     ;;  "w" gnus-article-word-wrap
6974     "w" gnus-article-fill-cited-article
6975     "c" gnus-article-remove-cr
6976     "L" gnus-article-remove-trailing-blank-lines
6977     "q" gnus-article-de-quoted-unreadable
6978     "f" gnus-article-display-x-face
6979     "l" gnus-summary-stop-page-breaking
6980     "r" gnus-summary-caesar-message
6981     "t" gnus-article-hide-headers
6982     "v" gnus-summary-verbose-headers
6983     "m" gnus-summary-toggle-mime)
6984
6985   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6986     "a" gnus-article-hide
6987     "h" gnus-article-hide-headers
6988     "b" gnus-article-hide-boring-headers
6989     "s" gnus-article-hide-signature
6990     "c" gnus-article-hide-citation
6991     "p" gnus-article-hide-pgp
6992     "\C-c" gnus-article-hide-citation-maybe)
6993
6994   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6995     "a" gnus-article-highlight
6996     "h" gnus-article-highlight-headers
6997     "c" gnus-article-highlight-citation
6998     "s" gnus-article-highlight-signature)
6999
7000   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7001     "z" gnus-article-date-ut
7002     "u" gnus-article-date-ut
7003     "l" gnus-article-date-local
7004     "e" gnus-article-date-lapsed
7005     "o" gnus-article-date-original)
7006
7007   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7008     "v" gnus-version
7009     "f" gnus-summary-fetch-faq
7010     "d" gnus-summary-describe-group
7011     "h" gnus-summary-describe-briefly
7012     "i" gnus-info-find-node)
7013
7014   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7015     "e" gnus-summary-expire-articles
7016     "\M-\C-e" gnus-summary-expire-articles-now
7017     "\177" gnus-summary-delete-article
7018     [delete] gnus-summary-delete-article
7019     "m" gnus-summary-move-article
7020     "r" gnus-summary-respool-article
7021     "w" gnus-summary-edit-article
7022     "c" gnus-summary-copy-article
7023     "B" gnus-summary-crosspost-article
7024     "q" gnus-summary-respool-query
7025     "i" gnus-summary-import-article)
7026
7027   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7028     "o" gnus-summary-save-article
7029     "m" gnus-summary-save-article-mail
7030     "r" gnus-summary-save-article-rmail
7031     "f" gnus-summary-save-article-file
7032     "b" gnus-summary-save-article-body-file
7033     "h" gnus-summary-save-article-folder
7034     "v" gnus-summary-save-article-vm
7035     "p" gnus-summary-pipe-output
7036     "s" gnus-soup-add-article)
7037   )
7038
7039 \f
7040
7041 (defun gnus-summary-mode (&optional group)
7042   "Major mode for reading articles.
7043
7044 All normal editing commands are switched off.
7045 \\<gnus-summary-mode-map>
7046 Each line in this buffer represents one article.  To read an
7047 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7048 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7049 respectively.
7050
7051 You can also post articles and send mail from this buffer.  To
7052 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7053 of an article, type `\\[gnus-summary-reply]'.
7054
7055 There are approx. one gazillion commands you can execute in this
7056 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7057
7058 The following commands are available:
7059
7060 \\{gnus-summary-mode-map}"
7061   (interactive)
7062   (when (and menu-bar-mode
7063              (gnus-visual-p 'summary-menu 'menu))
7064     (gnus-summary-make-menu-bar))
7065   (kill-all-local-variables)
7066   (let ((locals gnus-summary-local-variables))
7067     (while locals
7068       (if (consp (car locals))
7069           (progn
7070             (make-local-variable (caar locals))
7071             (set (caar locals) (eval (cdar locals))))
7072         (make-local-variable (car locals))
7073         (set (car locals) nil))
7074       (setq locals (cdr locals))))
7075   (gnus-make-thread-indent-array)
7076   (gnus-simplify-mode-line)
7077   (setq major-mode 'gnus-summary-mode)
7078   (setq mode-name "Summary")
7079   (make-local-variable 'minor-mode-alist)
7080   (use-local-map gnus-summary-mode-map)
7081   (buffer-disable-undo (current-buffer))
7082   (setq buffer-read-only t)             ;Disable modification
7083   (setq truncate-lines t)
7084   (setq selective-display t)
7085   (setq selective-display-ellipses t)   ;Display `...'
7086   (setq buffer-display-table gnus-summary-display-table)
7087   (setq gnus-newsgroup-name group)
7088   (make-local-variable 'gnus-summary-line-format)
7089   (make-local-variable 'gnus-summary-line-format-spec)
7090   (make-local-variable 'gnus-summary-mark-positions)
7091   (run-hooks 'gnus-summary-mode-hook))
7092
7093 (defun gnus-summary-make-display-table ()
7094   ;; Change the display table.  Odd characters have a tendency to mess
7095   ;; up nicely formatted displays - we make all possible glyphs
7096   ;; display only a single character.
7097
7098   ;; We start from the standard display table, if any.
7099   (setq gnus-summary-display-table
7100         (or (copy-sequence standard-display-table)
7101             (make-display-table)))
7102   ;; Nix out all the control chars...
7103   (let ((i 32))
7104     (while (>= (setq i (1- i)) 0)
7105       (aset gnus-summary-display-table i [??])))
7106   ;; ... but not newline and cr, of course. (cr is necessary for the
7107   ;; selective display).
7108   (aset gnus-summary-display-table ?\n nil)
7109   (aset gnus-summary-display-table ?\r nil)
7110   ;; We nix out any glyphs over 126 that are not set already.
7111   (let ((i 256))
7112     (while (>= (setq i (1- i)) 127)
7113       ;; Only modify if the entry is nil.
7114       (or (aref gnus-summary-display-table i)
7115           (aset gnus-summary-display-table i [??])))))
7116
7117 (defun gnus-summary-clear-local-variables ()
7118   (let ((locals gnus-summary-local-variables))
7119     (while locals
7120       (if (consp (car locals))
7121           (and (vectorp (caar locals))
7122                (set (caar locals) nil))
7123         (and (vectorp (car locals))
7124              (set (car locals) nil)))
7125       (setq locals (cdr locals)))))
7126
7127 ;; Summary data functions.
7128
7129 (defmacro gnus-data-number (data)
7130   `(car ,data))
7131
7132 (defmacro gnus-data-set-number (data number)
7133   `(setcar ,data ,number))
7134
7135 (defmacro gnus-data-mark (data)
7136   `(nth 1 ,data))
7137
7138 (defmacro gnus-data-set-mark (data mark)
7139   `(setcar (nthcdr 1 ,data) ,mark))
7140
7141 (defmacro gnus-data-pos (data)
7142   `(nth 2 ,data))
7143
7144 (defmacro gnus-data-set-pos (data pos)
7145   `(setcar (nthcdr 2 ,data) ,pos))
7146
7147 (defmacro gnus-data-header (data)
7148   `(nth 3 ,data))
7149
7150 (defmacro gnus-data-level (data)
7151   `(nth 4 ,data))
7152
7153 (defmacro gnus-data-unread-p (data)
7154   `(= (nth 1 ,data) gnus-unread-mark))
7155
7156 (defmacro gnus-data-pseudo-p (data)
7157   `(consp (nth 3 ,data)))
7158
7159 (defmacro gnus-data-find (number)
7160   `(assq ,number gnus-newsgroup-data))
7161
7162 (defmacro gnus-data-find-list (number &optional data)
7163   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7164      (memq (assq ,number bdata)
7165            bdata)))
7166
7167 (defmacro gnus-data-make (number mark pos header level)
7168   `(list ,number ,mark ,pos ,header ,level))
7169
7170 (defun gnus-data-enter (after-article number mark pos header level offset)
7171   (let ((data (gnus-data-find-list after-article)))
7172     (or data (error "No such article: %d" after-article))
7173     (setcdr data (cons (gnus-data-make number mark pos header level)
7174                        (cdr data)))
7175     (setq gnus-newsgroup-data-reverse nil)
7176     (gnus-data-update-list (cddr data) offset)))
7177
7178 (defun gnus-data-enter-list (after-article list &optional offset)
7179   (when list
7180     (let ((data (and after-article (gnus-data-find-list after-article)))
7181           (ilist list))
7182       (or data (not after-article) (error "No such article: %d" after-article))
7183       ;; Find the last element in the list to be spliced into the main
7184       ;; list.
7185       (while (cdr list)
7186         (setq list (cdr list)))
7187       (if (not data)
7188           (progn
7189             (setcdr list gnus-newsgroup-data)
7190             (setq gnus-newsgroup-data ilist)
7191             (and offset (gnus-data-update-list (cdr list) offset)))
7192         (setcdr list (cdr data))
7193         (setcdr data ilist)
7194         (and offset (gnus-data-update-list (cdr data) offset)))
7195       (setq gnus-newsgroup-data-reverse nil))))
7196
7197 (defun gnus-data-remove (article &optional offset)
7198   (let ((data gnus-newsgroup-data))
7199     (if (= (gnus-data-number (car data)) article)
7200         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7201               gnus-newsgroup-data-reverse nil)
7202       (while (cdr data)
7203         (and (= (gnus-data-number (cadr data)) article)
7204              (progn
7205                (setcdr data (cddr data))
7206                (and offset (gnus-data-update-list (cdr data) offset))
7207                (setq data nil
7208                      gnus-newsgroup-data-reverse nil)))
7209         (setq data (cdr data))))))
7210
7211 (defmacro gnus-data-list (backward)
7212   `(if ,backward
7213        (or gnus-newsgroup-data-reverse
7214            (setq gnus-newsgroup-data-reverse
7215                  (reverse gnus-newsgroup-data)))
7216      gnus-newsgroup-data))
7217
7218 (defun gnus-data-update-list (data offset)
7219   "Add OFFSET to the POS of all data entries in DATA."
7220   (while data
7221     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7222     (setq data (cdr data))))
7223
7224 (defun gnus-data-compute-positions ()
7225   "Compute the positions of all articles."
7226   (let ((data gnus-newsgroup-data)
7227         pos)
7228     (while data
7229       (when (setq pos (text-property-any
7230                        (point-min) (point-max)
7231                        'gnus-number (gnus-data-number (car data))))
7232         (gnus-data-set-pos (car data) (+ pos 3)))
7233       (setq data (cdr data)))))
7234
7235 (defun gnus-summary-article-pseudo-p (article)
7236   "Say whether this article is a pseudo article or not."
7237   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7238
7239 (defun gnus-article-parent-p (number)
7240   "Say whether this article is a parent or not."
7241   (let ((data (gnus-data-find-list number)))
7242     (and (cdr data)                     ; There has to be an article after...
7243          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7244             (gnus-data-level (nth 1 data))))))
7245
7246 (defun gnus-article-children (number)
7247   "Return a list of all children to NUMBER."
7248   (let* ((data (gnus-data-find-list number))
7249          (level (gnus-data-level (car data)))
7250          children)
7251     (setq data (cdr data))
7252     (while (and data            
7253                 (= (gnus-data-level (car data)) (1+ level)))
7254       (push (gnus-data-number (car data)) children)
7255       (setq data (cdr data)))
7256     children))
7257
7258 (defmacro gnus-summary-skip-intangible ()
7259   "If the current article is intangible, then jump to a different article."
7260   '(let ((to (get-text-property (point) 'gnus-intangible)))
7261     (and to (gnus-summary-goto-subject to))))
7262
7263 (defmacro gnus-summary-article-intangible-p ()
7264   "Say whether this article is intangible or not."
7265   '(get-text-property (point) 'gnus-intangible))
7266
7267 ;; Some summary mode macros.
7268
7269 (defmacro gnus-summary-article-number ()
7270   "The article number of the article on the current line.
7271 If there isn's an article number here, then we return the current
7272 article number."
7273   '(progn
7274      (gnus-summary-skip-intangible)
7275      (or (get-text-property (point) 'gnus-number)
7276          (gnus-summary-last-subject))))
7277
7278 (defmacro gnus-summary-article-header (&optional number)
7279   `(gnus-data-header (gnus-data-find
7280                       ,(or number '(gnus-summary-article-number)))))
7281
7282 (defmacro gnus-summary-thread-level (&optional number)
7283   `(if (and (eq gnus-summary-make-false-root 'dummy)
7284             (get-text-property (point) 'gnus-intangible))
7285        0
7286      (gnus-data-level (gnus-data-find
7287                        ,(or number '(gnus-summary-article-number))))))
7288
7289 (defmacro gnus-summary-article-mark (&optional number)
7290   `(gnus-data-mark (gnus-data-find
7291                     ,(or number '(gnus-summary-article-number)))))
7292
7293 (defmacro gnus-summary-article-pos (&optional number)
7294   `(gnus-data-pos (gnus-data-find
7295                    ,(or number '(gnus-summary-article-number)))))
7296
7297 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7298 (defmacro gnus-summary-article-subject (&optional number)
7299   "Return current subject string or nil if nothing."
7300   `(let ((headers
7301           ,(if number
7302                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7303              '(gnus-data-header (assq (gnus-summary-article-number)
7304                                       gnus-newsgroup-data)))))
7305      (and headers
7306           (vectorp headers)
7307           (mail-header-subject headers))))
7308
7309 (defmacro gnus-summary-article-score (&optional number)
7310   "Return current article score."
7311   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7312                   gnus-newsgroup-scored))
7313        gnus-summary-default-score 0))
7314
7315 (defun gnus-summary-article-children (&optional number)
7316   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7317          (level (gnus-data-level (car data)))
7318          l children)
7319     (while (and (setq data (cdr data))
7320                 (> (setq l (gnus-data-level (car data))) level))
7321       (and (= (1+ level) l)
7322            (setq children (cons (gnus-data-number (car data))
7323                                 children))))
7324     (nreverse children)))
7325
7326 (defun gnus-summary-article-parent (&optional number)
7327   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7328                                     (gnus-data-list t)))
7329          (level (gnus-data-level (car data))))
7330     (if (zerop level)
7331         () ; This is a root.
7332       ;; We search until we find an article with a level less than
7333       ;; this one.  That function has to be the parent.
7334       (while (and (setq data (cdr data))
7335                   (not (< (gnus-data-level (car data)) level))))
7336       (and data (gnus-data-number (car data))))))
7337
7338 (defun gnus-unread-mark-p (mark)
7339   "Say whether MARK is the unread mark."
7340   (= mark gnus-unread-mark))
7341
7342 (defun gnus-read-mark-p (mark)
7343   "Say whether MARK is one of the marks that mark as read.
7344 This is all marks except unread, ticked, dormant, and expirable."
7345   (not (or (= mark gnus-unread-mark)
7346            (= mark gnus-ticked-mark)
7347            (= mark gnus-dormant-mark)
7348            (= mark gnus-expirable-mark))))
7349
7350 ;; Various summary mode internalish functions.
7351
7352 (defun gnus-mouse-pick-article (e)
7353   (interactive "e")
7354   (mouse-set-point e)
7355   (gnus-summary-next-page nil t))
7356
7357 (defun gnus-summary-setup-buffer (group)
7358   "Initialize summary buffer."
7359   (let ((buffer (concat "*Summary " group "*")))
7360     (if (get-buffer buffer)
7361         (progn
7362           (set-buffer buffer)
7363           (setq gnus-summary-buffer (current-buffer))
7364           (not gnus-newsgroup-prepared))
7365       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7366       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7367       (gnus-add-current-to-buffer-list)
7368       (gnus-summary-mode group)
7369       (when gnus-carpal
7370         (gnus-carpal-setup-buffer 'summary))
7371       (unless gnus-single-article-buffer
7372         (make-local-variable 'gnus-article-buffer)
7373         (make-local-variable 'gnus-article-current)
7374         (make-local-variable 'gnus-original-article-buffer))
7375       (setq gnus-newsgroup-name group)
7376       t)))
7377
7378 (defun gnus-set-global-variables ()
7379   ;; Set the global equivalents of the summary buffer-local variables
7380   ;; to the latest values they had.  These reflect the summary buffer
7381   ;; that was in action when the last article was fetched.
7382   (when (eq major-mode 'gnus-summary-mode)
7383     (setq gnus-summary-buffer (current-buffer))
7384     (let ((name gnus-newsgroup-name)
7385           (marked gnus-newsgroup-marked)
7386           (unread gnus-newsgroup-unreads)
7387           (headers gnus-current-headers)
7388           (data gnus-newsgroup-data)
7389           (summary gnus-summary-buffer)
7390           (article-buffer gnus-article-buffer)
7391           (original gnus-original-article-buffer)
7392           (gac gnus-article-current)
7393           (score-file gnus-current-score-file))
7394       (save-excursion
7395         (set-buffer gnus-group-buffer)
7396         (setq gnus-newsgroup-name name)
7397         (setq gnus-newsgroup-marked marked)
7398         (setq gnus-newsgroup-unreads unread)
7399         (setq gnus-current-headers headers)
7400         (setq gnus-newsgroup-data data)
7401         (setq gnus-article-current gac)
7402         (setq gnus-summary-buffer summary)
7403         (setq gnus-article-buffer article-buffer)
7404         (setq gnus-original-article-buffer original)
7405         (setq gnus-current-score-file score-file)))))
7406
7407 (defun gnus-summary-last-article-p (&optional article)
7408   "Return whether ARTICLE is the last article in the buffer."
7409   (if (not (setq article (or article (gnus-summary-article-number))))
7410       t ; All non-existant numbers are the last article. :-)
7411     (not (cdr (gnus-data-find-list article)))))
7412
7413 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7414   "Insert a dummy root in the summary buffer."
7415   (beginning-of-line)
7416   (add-text-properties
7417    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7418    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7419
7420 (defun gnus-make-thread-indent-array ()
7421   (let ((n 200))
7422     (unless (and gnus-thread-indent-array
7423                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7424       (setq gnus-thread-indent-array (make-vector 201 "")
7425             gnus-thread-indent-array-level gnus-thread-indent-level)
7426       (while (>= n 0)
7427         (aset gnus-thread-indent-array n
7428               (make-string (* n gnus-thread-indent-level) ? ))
7429         (setq n (1- n))))))
7430
7431 (defun gnus-summary-insert-line
7432   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7433                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7434                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7435   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7436          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7437          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7438          (gnus-tmp-score-char
7439           (if (or (null gnus-summary-default-score)
7440                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7441                       gnus-summary-zcore-fuzz)) ? 
7442             (if (< gnus-tmp-score gnus-summary-default-score)
7443                 gnus-score-below-mark gnus-score-over-mark)))
7444          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7445                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7446                                   gnus-cached-mark)
7447                                  (gnus-tmp-replied gnus-replied-mark)
7448                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7449                                   gnus-saved-mark)
7450                                  (t gnus-unread-mark)))
7451          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7452          (gnus-tmp-name
7453           (cond
7454            ((string-match "(.+)" gnus-tmp-from)
7455             (substring gnus-tmp-from
7456                        (1+ (match-beginning 0)) (1- (match-end 0))))
7457            ((string-match "<[^>]+> *$" gnus-tmp-from)
7458             (let ((beg (match-beginning 0)))
7459               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7460                        (substring gnus-tmp-from (1+ (match-beginning 0))
7461                                   (1- (match-end 0))))
7462                   (substring gnus-tmp-from 0 beg))))
7463            (t gnus-tmp-from)))
7464          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7465          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7466          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7467          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7468          (buffer-read-only nil))
7469     (when (string= gnus-tmp-name "")
7470       (setq gnus-tmp-name gnus-tmp-from))
7471     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7472     (put-text-property
7473      (point)
7474      (progn (eval gnus-summary-line-format-spec) (point))
7475      'gnus-number gnus-tmp-number)
7476     (when (gnus-visual-p 'summary-highlight 'highlight)
7477       (forward-line -1)
7478       (run-hooks 'gnus-summary-update-hook)
7479       (forward-line 1))))
7480
7481 (defun gnus-summary-update-line (&optional dont-update)
7482   ;; Update summary line after change.
7483   (when (and gnus-summary-default-score
7484              (not gnus-summary-inhibit-highlight))
7485     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7486            (article (gnus-summary-article-number))
7487            (score (gnus-summary-article-score article)))
7488       (unless dont-update
7489         (if (and gnus-summary-mark-below
7490                  (< (gnus-summary-article-score)
7491                     gnus-summary-mark-below))
7492             ;; This article has a low score, so we mark it as read.
7493             (when (memq article gnus-newsgroup-unreads)
7494               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7495           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7496             ;; This article was previously marked as read on account
7497             ;; of a low score, but now it has risen, so we mark it as
7498             ;; unread.
7499             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7500         (gnus-summary-update-mark
7501          (if (or (null gnus-summary-default-score)
7502                  (<= (abs (- score gnus-summary-default-score))
7503                      gnus-summary-zcore-fuzz)) ? 
7504            (if (< score gnus-summary-default-score)
7505                gnus-score-below-mark gnus-score-over-mark)) 'score))
7506       ;; Do visual highlighting.
7507       (when (gnus-visual-p 'summary-highlight 'highlight)
7508         (run-hooks 'gnus-summary-update-hook)))))
7509
7510 (defvar gnus-tmp-new-adopts nil)
7511
7512 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7513   ;; Sum up all elements (and sub-elements) in a list.
7514   (let* ((number
7515           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7516           (cond
7517            ((and (consp thread) (cdr thread))
7518             (apply
7519              '+ 1 (mapcar
7520                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7521            ((null thread)
7522             1)
7523            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7524             1)
7525            (t 1))))
7526     (when (and level (zerop level) gnus-tmp-new-adopts)
7527       (incf number
7528             (apply '+ (mapcar
7529                        'gnus-summary-number-of-articles-in-thread
7530                        gnus-tmp-new-adopts))))
7531     (if char
7532         (if (> number 1) gnus-not-empty-thread-mark
7533           gnus-empty-thread-mark)
7534       number)))
7535
7536 (defun gnus-summary-set-local-parameters (group)
7537  "Go through the local params of GROUP and set all variable specs in that list."
7538   (let ((params (gnus-info-params (gnus-get-info group)))
7539         elem)
7540     (while params
7541       (setq elem (car params)
7542             params (cdr params))
7543       (and (consp elem)                 ; Has to be a cons.
7544            (consp (cdr elem))           ; The cdr has to be a list.
7545            (symbolp (car elem))         ; Has to be a symbol in there.
7546            (not (memq (car elem) 
7547                       '(quit-config to-address to-list to-group)))
7548            (progn                       ; So we set it.
7549              (make-local-variable (car elem))
7550              (set (car elem) (eval (nth 1 elem))))))))
7551
7552 (defun gnus-summary-read-group (group &optional show-all no-article
7553                                       kill-buffer no-display)
7554   "Start reading news in newsgroup GROUP.
7555 If SHOW-ALL is non-nil, already read articles are also listed.
7556 If NO-ARTICLE is non-nil, no article is selected initially.
7557 If NO-DISPLAY, don't generate a summary buffer."
7558   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7559   (let* ((new-group (gnus-summary-setup-buffer group))
7560          (quit-config (gnus-group-quit-config group))
7561          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7562     (cond
7563      ;; This summary buffer exists already, so we just select it.
7564      ((not new-group)
7565       (gnus-set-global-variables)
7566       (when kill-buffer
7567         (gnus-kill-or-deaden-summary kill-buffer))
7568       (gnus-configure-windows 'summary 'force)
7569       (gnus-set-mode-line 'summary)
7570       (gnus-summary-position-point)
7571       (message "")
7572       t)
7573      ;; We couldn't select this group.
7574      ((null did-select)
7575       (when (and (eq major-mode 'gnus-summary-mode)
7576                  (not (equal (current-buffer) kill-buffer)))
7577         (kill-buffer (current-buffer))
7578         (if (not quit-config)
7579             (progn
7580               (set-buffer gnus-group-buffer)
7581               (gnus-group-jump-to-group group)
7582               (gnus-group-next-unread-group 1))
7583           (if (not (buffer-name (car quit-config)))
7584               (gnus-configure-windows 'group 'force)
7585             (set-buffer (car quit-config))
7586             (and (eq major-mode 'gnus-summary-mode)
7587                  (gnus-set-global-variables))
7588             (gnus-configure-windows (cdr quit-config)))))
7589       (gnus-message 3 "Can't select group")
7590       nil)
7591      ;; The user did a `C-g' while prompting for number of articles,
7592      ;; so we exit this group.
7593      ((eq did-select 'quit)
7594       (and (eq major-mode 'gnus-summary-mode)
7595            (not (equal (current-buffer) kill-buffer))
7596            (kill-buffer (current-buffer)))
7597       (when kill-buffer
7598         (gnus-kill-or-deaden-summary kill-buffer))
7599       (if (not quit-config)
7600           (progn
7601             (set-buffer gnus-group-buffer)
7602             (gnus-group-jump-to-group group)
7603             (gnus-group-next-unread-group 1)
7604             (gnus-configure-windows 'group 'force))
7605         (if (not (buffer-name (car quit-config)))
7606             (gnus-configure-windows 'group 'force)
7607           (set-buffer (car quit-config))
7608           (and (eq major-mode 'gnus-summary-mode)
7609                (gnus-set-global-variables))
7610           (gnus-configure-windows (cdr quit-config))))
7611       ;; Finally signal the quit.
7612       (signal 'quit nil))
7613      ;; The group was successfully selected.
7614      (t
7615       (gnus-set-global-variables)
7616       ;; Save the active value in effect when the group was entered.
7617       (setq gnus-newsgroup-active
7618             (gnus-copy-sequence
7619              (gnus-active gnus-newsgroup-name)))
7620       ;; You can change the summary buffer in some way with this hook.
7621       (run-hooks 'gnus-select-group-hook)
7622       ;; Set any local variables in the group parameters.
7623       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7624       (gnus-update-format-specifications)
7625       ;; Do score processing.
7626       (when gnus-use-scoring
7627         (gnus-possibly-score-headers))
7628       ;; Check whether to fill in the gaps in the threads.
7629       (when gnus-build-sparse-threads
7630         (gnus-build-sparse-threads))
7631       ;; Find the initial limit.
7632       (if gnus-show-threads
7633           (if show-all
7634               (let ((gnus-newsgroup-dormant nil))
7635                 (gnus-summary-initial-limit show-all))
7636             (gnus-summary-initial-limit show-all))
7637         (setq gnus-newsgroup-limit 
7638               (mapcar 
7639                (lambda (header) (mail-header-number header))
7640                gnus-newsgroup-headers)))
7641       ;; Generate the summary buffer.
7642       (unless no-display
7643         (gnus-summary-prepare))
7644       (when gnus-use-trees
7645         (gnus-tree-open group)
7646         (setq gnus-summary-highlight-line-function
7647               'gnus-tree-highlight-article))
7648       ;; If the summary buffer is empty, but there are some low-scored
7649       ;; articles or some excluded dormants, we include these in the
7650       ;; buffer.
7651       (when (and (zerop (buffer-size))
7652                  (not no-display))
7653         (cond (gnus-newsgroup-dormant
7654                (gnus-summary-limit-include-dormant))
7655               ((and gnus-newsgroup-scored show-all)
7656                (gnus-summary-limit-include-expunged))))
7657       ;; Function `gnus-apply-kill-file' must be called in this hook.
7658       (run-hooks 'gnus-apply-kill-hook)
7659       (if (and (zerop (buffer-size))
7660                (not no-display))
7661           (progn
7662             ;; This newsgroup is empty.
7663             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7664             (gnus-message 6 "No unread news")
7665             (when kill-buffer
7666               (gnus-kill-or-deaden-summary kill-buffer))
7667             ;; Return nil from this function.
7668             nil)
7669         ;; Hide conversation thread subtrees.  We cannot do this in
7670         ;; gnus-summary-prepare-hook since kill processing may not
7671         ;; work with hidden articles.
7672         (and gnus-show-threads
7673              gnus-thread-hide-subtree
7674              (gnus-summary-hide-all-threads))
7675         ;; Show first unread article if requested.
7676         (if (and (not no-article)
7677                  (not no-display)
7678                  gnus-newsgroup-unreads
7679                  gnus-auto-select-first)
7680             (unless (if (eq gnus-auto-select-first 'best)
7681                         (gnus-summary-best-unread-article)
7682                       (gnus-summary-first-unread-article))
7683               (gnus-configure-windows 'summary))
7684           ;; Don't select any articles, just move point to the first
7685           ;; article in the group.
7686           (goto-char (point-min))
7687           (gnus-summary-position-point)
7688           (gnus-set-mode-line 'summary)
7689           (gnus-configure-windows 'summary 'force))
7690         ;; If we are in async mode, we send some info to the backend.
7691         (when gnus-newsgroup-async
7692           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7693         (when kill-buffer
7694           (gnus-kill-or-deaden-summary kill-buffer))
7695         (when (get-buffer-window gnus-group-buffer t)
7696           ;; Gotta use windows, because recenter does wierd stuff if
7697           ;; the current buffer ain't the displayed window.
7698           (let ((owin (selected-window)))
7699             (select-window (get-buffer-window gnus-group-buffer t))
7700             (when (gnus-group-goto-group group)
7701               (recenter))
7702             (select-window owin))))
7703       ;; Mark this buffer as "prepared".
7704       (setq gnus-newsgroup-prepared t)
7705       t))))
7706
7707 (defun gnus-summary-prepare ()
7708   "Generate the summary buffer."
7709   (let ((buffer-read-only nil))
7710     (erase-buffer)
7711     (setq gnus-newsgroup-data nil
7712           gnus-newsgroup-data-reverse nil)
7713     (run-hooks 'gnus-summary-generate-hook)
7714     ;; Generate the buffer, either with threads or without.
7715     (when gnus-newsgroup-headers
7716       (gnus-summary-prepare-threads
7717        (if gnus-show-threads
7718            (gnus-sort-gathered-threads
7719             (funcall gnus-summary-thread-gathering-function
7720                      (gnus-sort-threads
7721                       (gnus-cut-threads (gnus-make-threads)))))
7722          ;; Unthreaded display.
7723          (gnus-sort-articles gnus-newsgroup-headers))))
7724     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7725     ;; Call hooks for modifying summary buffer.
7726     (goto-char (point-min))
7727     (run-hooks 'gnus-summary-prepare-hook)))
7728
7729 (defun gnus-gather-threads-by-subject (threads)
7730   "Gather threads by looking at Subject headers."
7731   (if (not gnus-summary-make-false-root)
7732       threads
7733     (let ((hashtb (gnus-make-hashtable 1023))
7734           (prev threads)
7735           (result threads)
7736           subject hthread whole-subject)
7737       (while threads
7738         (setq whole-subject (mail-header-subject (caar threads)))
7739         (setq subject
7740               (cond
7741                ;; Truncate the subject.
7742                ((numberp gnus-summary-gather-subject-limit)
7743                 (setq subject (gnus-simplify-subject-re whole-subject))
7744                 (if (> (length subject) gnus-summary-gather-subject-limit)
7745                     (substring subject 0 gnus-summary-gather-subject-limit)
7746                   subject))
7747                ;; Fuzzily simplify it.
7748                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7749                 (gnus-simplify-subject-fuzzy whole-subject))
7750                ;; Just remove the leading "Re:".
7751                (t
7752                 (gnus-simplify-subject-re whole-subject))))
7753
7754         (if (and gnus-summary-gather-exclude-subject
7755                  (string-match gnus-summary-gather-exclude-subject
7756                                subject))
7757             ()          ; We don't want to do anything with this article.
7758           ;; We simplify the subject before looking it up in the
7759           ;; hash table.
7760
7761           (if (setq hthread (gnus-gethash subject hashtb))
7762               (progn
7763                 ;; We enter a dummy root into the thread, if we
7764                 ;; haven't done that already.
7765                 (unless (stringp (caar hthread))
7766                   (setcar hthread (list whole-subject (car hthread))))
7767                 ;; We add this new gathered thread to this gathered
7768                 ;; thread.
7769                 (setcdr (car hthread)
7770                         (nconc (cdar hthread) (list (car threads))))
7771                 ;; Remove it from the list of threads.
7772                 (setcdr prev (cdr threads))
7773                 (setq threads prev))
7774             ;; Enter this thread into the hash table.
7775             (gnus-sethash subject threads hashtb)))
7776         (setq prev threads)
7777         (setq threads (cdr threads)))
7778       result)))
7779
7780 (defun gnus-gather-threads-by-references (threads)
7781   "Gather threads by looking at References headers."
7782   (let ((idhashtb (gnus-make-hashtable 1023))
7783         (thhashtb (gnus-make-hashtable 1023))
7784         (prev threads)
7785         (result threads)
7786         ids references id gthread gid entered)
7787     (while threads
7788       (when (setq references (mail-header-references (caar threads)))
7789         (setq id (mail-header-id (caar threads)))
7790         (setq ids (gnus-split-references references))
7791         (setq entered nil)
7792         (while ids
7793           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7794               (progn
7795                 (gnus-sethash (car ids) id idhashtb)
7796                 (gnus-sethash id threads thhashtb))
7797             (setq gthread (gnus-gethash gid thhashtb))
7798             (unless entered
7799               ;; We enter a dummy root into the thread, if we
7800               ;; haven't done that already.
7801               (unless (stringp (caar gthread))
7802                 (setcar gthread (list (mail-header-subject (caar gthread))
7803                                       (car gthread))))
7804               ;; We add this new gathered thread to this gathered
7805               ;; thread.
7806               (setcdr (car gthread)
7807                       (nconc (cdar gthread) (list (car threads)))))
7808             ;; Add it into the thread hash table.
7809             (gnus-sethash id gthread thhashtb)
7810             (setq entered t)
7811             ;; Remove it from the list of threads.
7812             (setcdr prev (cdr threads))
7813             (setq threads prev))
7814           (setq ids (cdr ids))))
7815       (setq prev threads)
7816       (setq threads (cdr threads)))
7817     result))
7818
7819 (defun gnus-sort-gathered-threads (threads)
7820   "Sort subtreads inside each gathered thread by article number."
7821   (let ((result threads))
7822     (while threads
7823       (when (stringp (caar threads))
7824         (setcdr (car threads)
7825                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7826       (setq threads (cdr threads)))
7827     result))
7828
7829 (defun gnus-make-threads ()
7830   "Go through the dependency hashtb and find the roots.  Return all threads."
7831   (let (threads)
7832     (mapatoms
7833      (lambda (refs)
7834        (unless (car (symbol-value refs))
7835          ;; These threads do not refer back to any other articles,
7836          ;; so they're roots.
7837          (setq threads (append (cdr (symbol-value refs)) threads))))
7838      gnus-newsgroup-dependencies)
7839     threads))
7840
7841 (defun gnus-build-sparse-threads ()
7842   (let ((headers gnus-newsgroup-headers)
7843         (deps gnus-newsgroup-dependencies)
7844         header references generation relations 
7845         cthread subject child end pthread relation)
7846     ;; First we create an alist of generations/relations, where 
7847     ;; generations is how much we trust the ralation, and the relation
7848     ;; is parent/child.
7849     (gnus-message 7 "Making sparse threads...")
7850     (save-excursion
7851       (nnheader-set-temp-buffer " *gnus sparse threads*")
7852       (while (setq header (pop headers))
7853         (when (and (setq references (mail-header-references header))
7854                    (not (string= references "")))
7855           (insert references)
7856           (setq child (mail-header-id header)
7857                 subject (mail-header-subject header))
7858           (setq generation 0)
7859           (while (search-backward ">" nil t)
7860             (setq end (1+ (point)))
7861             (when (search-backward "<" nil t)
7862               (push (list (incf generation) 
7863                           child (setq child (buffer-substring (point) end))
7864                           subject)
7865                     relations)))
7866           (push (list (1+ generation) child nil subject) relations)
7867           (erase-buffer)))
7868       (kill-buffer (current-buffer)))
7869     ;; Sort over trustworthiness.
7870     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7871     (while (setq relation (pop relations))
7872       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7873                 (unless (car (symbol-value cthread))
7874                   ;; Make this article the parent of these threads.
7875                   (setcar (symbol-value cthread)
7876                           (vector gnus-reffed-article-number 
7877                                   (cadddr relation) 
7878                                   "" ""
7879                                   (cadr relation) 
7880                                   (or (caddr relation) "") 0 0 "")))
7881               (set cthread (list (vector gnus-reffed-article-number
7882                                          (cadddr relation) 
7883                                          "" "" (cadr relation) 
7884                                          (or (caddr relation) "") 0 0 ""))))
7885         (push gnus-reffed-article-number gnus-newsgroup-limit)
7886         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7887         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7888               gnus-newsgroup-reads)
7889         (decf gnus-reffed-article-number)
7890         ;; Make this new thread the child of its parent.
7891         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7892             (setcdr (symbol-value pthread)
7893                     (nconc (cdr (symbol-value pthread))
7894                            (list (symbol-value cthread))))
7895           (set pthread (list nil (symbol-value cthread))))))
7896     (gnus-message 7 "Making sparse threads...done")))
7897
7898 (defun gnus-build-old-threads ()
7899   ;; Look at all the articles that refer back to old articles, and
7900   ;; fetch the headers for the articles that aren't there.  This will
7901   ;; build complete threads - if the roots haven't been expired by the
7902   ;; server, that is.
7903   (let (id heads)
7904     (mapatoms
7905      (lambda (refs)
7906        (when (not (car (symbol-value refs)))
7907          (setq heads (cdr (symbol-value refs)))
7908          (while heads
7909            (if (memq (mail-header-number (caar heads))
7910                      gnus-newsgroup-dormant)
7911                (setq heads (cdr heads))
7912              (setq id (symbol-name refs))
7913              (while (and (setq id (gnus-build-get-header id))
7914                          (not (car (gnus-gethash
7915                                     id gnus-newsgroup-dependencies)))))
7916              (setq heads nil)))))
7917      gnus-newsgroup-dependencies)))
7918
7919 (defun gnus-build-get-header (id)
7920   ;; Look through the buffer of NOV lines and find the header to
7921   ;; ID.  Enter this line into the dependencies hash table, and return
7922   ;; the id of the parent article (if any).
7923   (let ((deps gnus-newsgroup-dependencies)
7924         found header)
7925     (prog1
7926         (save-excursion
7927           (set-buffer nntp-server-buffer)
7928           (goto-char (point-min))
7929           (while (and (not found) (search-forward id nil t))
7930             (beginning-of-line)
7931             (setq found (looking-at
7932                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7933                                  (regexp-quote id))))
7934             (or found (beginning-of-line 2)))
7935           (when found
7936             (beginning-of-line)
7937             (and
7938              (setq header (gnus-nov-parse-line
7939                            (read (current-buffer)) deps))
7940              (gnus-parent-id (mail-header-references header)))))
7941       (when header
7942         (let ((number (mail-header-number header)))
7943           (push number gnus-newsgroup-limit)
7944           (push header gnus-newsgroup-headers)
7945           (if (memq number gnus-newsgroup-unselected)
7946               (progn
7947                 (push number gnus-newsgroup-unreads)
7948                 (setq gnus-newsgroup-unselected
7949                       (delq number gnus-newsgroup-unselected)))
7950             (push number gnus-newsgroup-ancient)))))))
7951
7952 (defun gnus-summary-update-article (article &optional header)
7953   "Update ARTICLE in the summary buffer."
7954   (set-buffer gnus-summary-buffer)
7955   (let* ((header (or header (gnus-summary-article-header article)))
7956          (id (mail-header-id header))
7957          (data (gnus-data-find article))
7958          (thread (gnus-id-to-thread id))
7959          (parent
7960           (gnus-id-to-thread (or (gnus-parent-id 
7961                                   (mail-header-references header))
7962                                  "tull")))
7963          (buffer-read-only nil)
7964          (old (car thread))
7965          (number (mail-header-number header))
7966          pos)
7967     (when thread
7968       (setcar thread nil)
7969       (when parent
7970         (delq thread parent))
7971       (if (gnus-summary-insert-subject id header)
7972           ;; Set the (possibly) new article number in the data structure.
7973           (gnus-data-set-number data (gnus-id-to-article id))
7974         (setcar thread old)
7975         nil))))
7976
7977 (defun gnus-rebuild-thread (id)
7978   "Rebuild the thread containing ID."
7979   (let ((buffer-read-only nil)
7980         current thread data)
7981     (if (not gnus-show-threads)
7982         (setq thread (list (car (gnus-id-to-thread id))))
7983       ;; Get the thread this article is part of.
7984       (setq thread (gnus-remove-thread id)))
7985     (setq current (save-excursion
7986                     (and (zerop (forward-line -1))
7987                          (gnus-summary-article-number))))
7988     ;; If this is a gathered thread, we have to go some re-gathering.
7989     (when (stringp (car thread))
7990       (let ((subject (car thread))
7991             roots thr)
7992         (setq thread (cdr thread))
7993         (while thread
7994           (unless (memq (setq thr (gnus-id-to-thread
7995                                       (gnus-root-id
7996                                        (mail-header-id (caar thread)))))
7997                         roots)
7998             (push thr roots))
7999           (setq thread (cdr thread)))
8000         ;; We now have all (unique) roots.
8001         (if (= (length roots) 1)
8002             ;; All the loose roots are now one solid root.
8003             (setq thread (car roots))
8004           (setq thread (cons subject (gnus-sort-threads roots))))))
8005     (let (threads)
8006       ;; We then insert this thread into the summary buffer.
8007       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8008         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8009         (setq data (nreverse gnus-newsgroup-data))
8010         (setq threads gnus-newsgroup-threads))
8011       ;; We splice the new data into the data structure.
8012       (gnus-data-enter-list current data)
8013       (gnus-data-compute-positions)
8014       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8015
8016 (defun gnus-id-to-thread (id)
8017   "Return the (sub-)thread where ID appears."
8018   (gnus-gethash id gnus-newsgroup-dependencies))
8019
8020 (defun gnus-id-to-article (id)
8021   "Return the article number of ID."
8022   (let ((thread (gnus-id-to-thread id)))
8023     (when thread
8024       (mail-header-number (car thread)))))
8025
8026 (defun gnus-id-to-header (id)
8027   "Return the article headers of ID."
8028   (car (gnus-id-to-thread id)))
8029
8030 (defun gnus-article-displayed-root-p (article)
8031   "Say whether ARTICLE is a root(ish) article."
8032   (let ((level (gnus-summary-thread-level article))
8033         particle)
8034     (cond 
8035      ((null level) nil)
8036      ((zerop level) t)
8037      ((and (= 1 level)
8038            (null (setq particle (gnus-id-to-article
8039                                  (gnus-parent-id 
8040                                   (mail-header-references 
8041                                    (gnus-summary-article-header article))))))
8042            (null (gnus-summary-thread-level particle)))))))
8043
8044 (defun gnus-root-id (id)
8045   "Return the id of the root of the thread where ID appears."
8046   (let (last-id prev)
8047     (while (and id (setq prev (car (gnus-gethash 
8048                                     id gnus-newsgroup-dependencies))))
8049       (setq last-id id
8050             id (gnus-parent-id (mail-header-references prev))))
8051     last-id))
8052
8053 (defun gnus-remove-thread (id &optional dont-remove)
8054   "Remove the thread that has ID in it."
8055   (let ((dep gnus-newsgroup-dependencies)
8056         headers thread last-id)
8057     ;; First go up in this thread until we find the root.
8058     (setq last-id (gnus-root-id id))
8059     (setq headers (list (car (gnus-id-to-thread last-id))
8060                         (caadr (gnus-id-to-thread last-id))))
8061     ;; We have now found the real root of this thread.  It might have
8062     ;; been gathered into some loose thread, so we have to search
8063     ;; through the threads to find the thread we wanted.
8064     (let ((threads gnus-newsgroup-threads)
8065           sub)
8066       (while threads
8067         (setq sub (car threads))
8068         (if (stringp (car sub))
8069             ;; This is a gathered threads, so we look at the roots
8070             ;; below it to find whether this article in in this
8071             ;; gathered root.
8072             (progn
8073               (setq sub (cdr sub))
8074               (while sub
8075                 (when (member (caar sub) headers)
8076                   (setq thread (car threads)
8077                         threads nil
8078                         sub nil))
8079                 (setq sub (cdr sub))))
8080           ;; It's an ordinary thread, so we check it.
8081           (when (eq (car sub) (car headers))
8082             (setq thread sub
8083                   threads nil)))
8084         (setq threads (cdr threads)))
8085       ;; If this article is in no thread, then it's a root.
8086       (if thread
8087           (unless dont-remove
8088             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8089         (setq thread (gnus-gethash last-id dep)))
8090       (when thread
8091         (prog1
8092             thread ; We return this thread.
8093           (unless dont-remove
8094             (if (stringp (car thread))
8095                 (progn
8096                   ;; If we use dummy roots, then we have to remove the
8097                   ;; dummy root as well.
8098                   (when (eq gnus-summary-make-false-root 'dummy)
8099                     ;; Uhm.
8100                     )
8101                   (setq thread (cdr thread))
8102                   (while thread
8103                     (gnus-remove-thread-1 (car thread))
8104                     (setq thread (cdr thread))))
8105               (gnus-remove-thread-1 thread))))))))
8106
8107 (defun gnus-remove-thread-1 (thread)
8108   "Remove the thread THREAD recursively."
8109   (let ((number (mail-header-number (car thread)))
8110         pos)
8111     (when (setq pos (text-property-any
8112                      (point-min) (point-max) 'gnus-number number))
8113       (goto-char pos)
8114       (gnus-delete-line)
8115       (gnus-data-remove number))
8116     (setq thread (cdr thread))
8117     (while thread
8118       (gnus-remove-thread-1 (pop thread)))))
8119
8120 (defun gnus-sort-threads (threads)
8121   "Sort THREADS."
8122   (if (not gnus-thread-sort-functions)
8123       threads
8124     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8125                     (car gnus-thread-sort-functions)
8126                   `(lambda (t1 t2)
8127                      ,(gnus-make-sort-function 
8128                        (reverse gnus-thread-sort-functions))))))
8129       (gnus-message 7 "Sorting threads...")
8130       (prog1
8131           (sort threads func)
8132         (gnus-message 7 "Sorting threads...done")))))
8133
8134 (defun gnus-sort-articles (articles)
8135   "Sort ARTICLES."
8136   (when gnus-article-sort-functions
8137     (let ((func (if (= 1 (length gnus-article-sort-functions))
8138                     (car gnus-article-sort-functions)
8139                   `(lambda (t1 t2)
8140                      ,(gnus-make-sort-function 
8141                        (reverse gnus-article-sort-functions))))))
8142       (gnus-message 7 "Sorting articles...")
8143       (prog1
8144           (setq gnus-newsgroup-headers (sort articles func))
8145         (gnus-message 7 "Sorting articles...done")))))
8146
8147 (defun gnus-make-sort-function (funs)
8148   "Return a composite sort condition based on the functions in FUNC."
8149   (if (cdr funs)
8150       `(or (,(car funs) t1 t2)
8151            (and (not (,(car funs) t2 t1))
8152                 ,(gnus-make-sort-function (cdr funs))))
8153     `(,(car funs) t1 t2)))
8154                  
8155 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8156 (defmacro gnus-thread-header (thread)
8157   ;; Return header of first article in THREAD.
8158   ;; Note that THREAD must never, ever be anything else than a variable -
8159   ;; using some other form will lead to serious barfage.
8160   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8161   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8162   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8163         (vector thread) 2))
8164
8165 (defsubst gnus-article-sort-by-number (h1 h2)
8166   "Sort articles by article number."
8167   (< (mail-header-number h1)
8168      (mail-header-number h2)))
8169
8170 (defun gnus-thread-sort-by-number (h1 h2)
8171   "Sort threads by root article number."
8172   (gnus-article-sort-by-number
8173    (gnus-thread-header h1) (gnus-thread-header h2)))
8174
8175 (defsubst gnus-article-sort-by-author (h1 h2)
8176   "Sort articles by root author."
8177   (string-lessp
8178    (let ((extract (funcall
8179                    gnus-extract-address-components
8180                    (mail-header-from h1))))
8181      (or (car extract) (cdr extract)))
8182    (let ((extract (funcall
8183                    gnus-extract-address-components
8184                    (mail-header-from h2))))
8185      (or (car extract) (cdr extract)))))
8186
8187 (defun gnus-thread-sort-by-author (h1 h2)
8188   "Sort threads by root author."
8189   (gnus-article-sort-by-author
8190    (gnus-thread-header h1)  (gnus-thread-header h2)))
8191
8192 (defsubst gnus-article-sort-by-subject (h1 h2)
8193   "Sort articles by root subject."
8194   (string-lessp
8195    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8196    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8197
8198 (defun gnus-thread-sort-by-subject (h1 h2)
8199   "Sort threads by root subject."
8200   (gnus-article-sort-by-subject
8201    (gnus-thread-header h1) (gnus-thread-header h2)))
8202
8203 (defsubst gnus-article-sort-by-date (h1 h2)
8204   "Sort articles by root article date."
8205   (string-lessp
8206    (gnus-sortable-date (mail-header-date h1))
8207    (gnus-sortable-date (mail-header-date h2))))
8208
8209 (defun gnus-thread-sort-by-date (h1 h2)
8210   "Sort threads by root article date."
8211   (gnus-article-sort-by-date
8212    (gnus-thread-header h1) (gnus-thread-header h2)))
8213
8214 (defsubst gnus-article-sort-by-score (h1 h2)
8215   "Sort articles by root article score.
8216 Unscored articles will be counted as having a score of zero."
8217   (> (or (cdr (assq (mail-header-number h1)
8218                     gnus-newsgroup-scored))
8219          gnus-summary-default-score 0)
8220      (or (cdr (assq (mail-header-number h2)
8221                     gnus-newsgroup-scored))
8222          gnus-summary-default-score 0)))
8223
8224 (defun gnus-thread-sort-by-score (h1 h2)
8225   "Sort threads by root article score."
8226   (gnus-article-sort-by-score
8227    (gnus-thread-header h1) (gnus-thread-header h2)))
8228
8229 (defun gnus-thread-sort-by-total-score (h1 h2)
8230   "Sort threads by the sum of all scores in the thread.
8231 Unscored articles will be counted as having a score of zero."
8232   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8233
8234 (defun gnus-thread-total-score (thread)
8235   ;;  This function find the total score of THREAD.
8236   (if (consp thread)
8237       (if (stringp (car thread))
8238           (apply gnus-thread-score-function 0
8239                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8240         (gnus-thread-total-score-1 thread))
8241     (gnus-thread-total-score-1 (list thread))))
8242
8243 (defun gnus-thread-total-score-1 (root)
8244   ;; This function find the total score of the thread below ROOT.
8245   (setq root (car root))
8246   (apply gnus-thread-score-function
8247          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8248              gnus-summary-default-score 0)
8249          (mapcar 'gnus-thread-total-score
8250                  (cdr (gnus-gethash (mail-header-id root)
8251                                     gnus-newsgroup-dependencies)))))
8252
8253 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8254 (defvar gnus-tmp-prev-subject nil)
8255 (defvar gnus-tmp-false-parent nil)
8256 (defvar gnus-tmp-root-expunged nil)
8257 (defvar gnus-tmp-dummy-line nil)
8258
8259 (defun gnus-summary-prepare-threads (threads)
8260   "Prepare summary buffer from THREADS and indentation LEVEL.
8261 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8262 or a straight list of headers."
8263   (gnus-message 7 "Generating summary...")
8264
8265   (setq gnus-newsgroup-threads threads)
8266   (beginning-of-line)
8267
8268   (let ((gnus-tmp-level 0)
8269         (default-score (or gnus-summary-default-score 0))
8270         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8271         thread number subject stack state gnus-tmp-gathered beg-match
8272         new-roots gnus-tmp-new-adopts thread-end
8273         gnus-tmp-header gnus-tmp-unread
8274         gnus-tmp-replied gnus-tmp-subject-or-nil
8275         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8276         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8277         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8278
8279     (setq gnus-tmp-prev-subject nil)
8280
8281     (if (vectorp (car threads))
8282         ;; If this is a straight (sic) list of headers, then a
8283         ;; threaded summary display isn't required, so we just create
8284         ;; an unthreaded one.
8285         (gnus-summary-prepare-unthreaded threads)
8286
8287       ;; Do the threaded display.
8288
8289       (while (or threads stack gnus-tmp-new-adopts new-roots)
8290
8291         (if (and (= gnus-tmp-level 0)
8292                  (not (setq gnus-tmp-dummy-line nil))
8293                  (or (not stack)
8294                      (= (caar stack) 0))
8295                  (not gnus-tmp-false-parent)
8296                  (or gnus-tmp-new-adopts new-roots))
8297             (if gnus-tmp-new-adopts
8298                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8299                       thread (list (car gnus-tmp-new-adopts))
8300                       gnus-tmp-header (caar thread)
8301                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8302               (if new-roots
8303                   (setq thread (list (car new-roots))
8304                         gnus-tmp-header (caar thread)
8305                         new-roots (cdr new-roots))))
8306
8307           (if threads
8308               ;; If there are some threads, we do them before the
8309               ;; threads on the stack.
8310               (setq thread threads
8311                     gnus-tmp-header (caar thread))
8312             ;; There were no current threads, so we pop something off
8313             ;; the stack.
8314             (setq state (car stack)
8315                   gnus-tmp-level (car state)
8316                   thread (cdr state)
8317                   stack (cdr stack)
8318                   gnus-tmp-header (caar thread))))
8319
8320         (setq gnus-tmp-false-parent nil)
8321         (setq gnus-tmp-root-expunged nil)
8322         (setq thread-end nil)
8323
8324         (if (stringp gnus-tmp-header)
8325             ;; The header is a dummy root.
8326             (cond
8327              ((eq gnus-summary-make-false-root 'adopt)
8328               ;; We let the first article adopt the rest.
8329               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8330                                                (cddar thread)))
8331               (setq gnus-tmp-gathered
8332                     (nconc (mapcar
8333                             (lambda (h) (mail-header-number (car h)))
8334                             (cddar thread))
8335                            gnus-tmp-gathered))
8336               (setq thread (cons (list (caar thread)
8337                                        (cadar thread))
8338                                  (cdr thread)))
8339               (setq gnus-tmp-level -1
8340                     gnus-tmp-false-parent t))
8341              ((eq gnus-summary-make-false-root 'empty)
8342               ;; We print adopted articles with empty subject fields.
8343               (setq gnus-tmp-gathered
8344                     (nconc (mapcar
8345                             (lambda (h) (mail-header-number (car h)))
8346                             (cddar thread))
8347                            gnus-tmp-gathered))
8348               (setq gnus-tmp-level -1))
8349              ((eq gnus-summary-make-false-root 'dummy)
8350               ;; We remember that we probably want to output a dummy
8351               ;; root.
8352               (setq gnus-tmp-dummy-line gnus-tmp-header)
8353               (setq gnus-tmp-prev-subject gnus-tmp-header))
8354              (t
8355               ;; We do not make a root for the gathered
8356               ;; sub-threads at all.
8357               (setq gnus-tmp-level -1)))
8358
8359           (setq number (mail-header-number gnus-tmp-header)
8360                 subject (mail-header-subject gnus-tmp-header))
8361
8362           (cond
8363            ;; If the thread has changed subject, we might want to make
8364            ;; this subthread into a root.
8365            ((and (null gnus-thread-ignore-subject)
8366                  (not (zerop gnus-tmp-level))
8367                  gnus-tmp-prev-subject
8368                  (not (inline
8369                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8370             (setq new-roots (nconc new-roots (list (car thread)))
8371                   thread-end t
8372                   gnus-tmp-header nil))
8373            ;; If the article lies outside the current limit,
8374            ;; then we do not display it.
8375            ((and (not (memq number gnus-newsgroup-limit))
8376                  (not gnus-tmp-dummy-line))
8377             (setq gnus-tmp-gathered
8378                   (nconc (mapcar
8379                           (lambda (h) (mail-header-number (car h)))
8380                           (cdar thread))
8381                          gnus-tmp-gathered))
8382             (setq gnus-tmp-new-adopts (if (cdar thread)
8383                                           (append gnus-tmp-new-adopts
8384                                                   (cdar thread))
8385                                         gnus-tmp-new-adopts)
8386                   thread-end t
8387                   gnus-tmp-header nil)
8388             (when (zerop gnus-tmp-level)
8389               (setq gnus-tmp-root-expunged t)))
8390            ;; Perhaps this article is to be marked as read?
8391            ((and gnus-summary-mark-below
8392                  (< (or (cdr (assq number gnus-newsgroup-scored))
8393                         default-score)
8394                     gnus-summary-mark-below)
8395                  ;; Don't touch sparse articles.
8396                  (not (memq number gnus-newsgroup-sparse)))
8397             (setq gnus-newsgroup-unreads
8398                   (delq number gnus-newsgroup-unreads))
8399             (if gnus-newsgroup-auto-expire
8400                 (push number gnus-newsgroup-expirable)
8401               (push (cons number gnus-low-score-mark)
8402                     gnus-newsgroup-reads))))
8403
8404           (when gnus-tmp-header
8405             ;; We may have an old dummy line to output before this
8406             ;; article.
8407             (when gnus-tmp-dummy-line
8408               (gnus-summary-insert-dummy-line
8409                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8410               (setq gnus-tmp-dummy-line nil))
8411
8412             ;; Compute the mark.
8413             (setq
8414              gnus-tmp-unread
8415              (cond
8416               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8417               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8418               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8419               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8420               (t (or (cdr (assq number gnus-newsgroup-reads))
8421                      gnus-ancient-mark))))
8422
8423             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8424                                   gnus-tmp-header gnus-tmp-level)
8425                   gnus-newsgroup-data)
8426
8427             ;; Actually insert the line.
8428             (setq
8429              gnus-tmp-subject-or-nil
8430              (cond
8431               ((and gnus-thread-ignore-subject
8432                     gnus-tmp-prev-subject
8433                     (not (inline (gnus-subject-equal
8434                                   gnus-tmp-prev-subject subject))))
8435                subject)
8436               ((zerop gnus-tmp-level)
8437                (if (and (eq gnus-summary-make-false-root 'empty)
8438                         (memq number gnus-tmp-gathered)
8439                         gnus-tmp-prev-subject
8440                         (inline (gnus-subject-equal
8441                                  gnus-tmp-prev-subject subject)))
8442                    gnus-summary-same-subject
8443                  subject))
8444               (t gnus-summary-same-subject)))
8445             (if (and (eq gnus-summary-make-false-root 'adopt)
8446                      (= gnus-tmp-level 1)
8447                      (memq number gnus-tmp-gathered))
8448                 (setq gnus-tmp-opening-bracket ?\<
8449                       gnus-tmp-closing-bracket ?\>)
8450               (setq gnus-tmp-opening-bracket ?\[
8451                     gnus-tmp-closing-bracket ?\]))
8452             (setq
8453              gnus-tmp-indentation
8454              (aref gnus-thread-indent-array gnus-tmp-level)
8455              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8456              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8457                                 gnus-summary-default-score 0)
8458              gnus-tmp-score-char
8459              (if (or (null gnus-summary-default-score)
8460                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8461                          gnus-summary-zcore-fuzz)) ? 
8462                (if (< gnus-tmp-score gnus-summary-default-score)
8463                    gnus-score-below-mark gnus-score-over-mark))
8464              gnus-tmp-replied
8465              (cond ((memq number gnus-newsgroup-processable)
8466                     gnus-process-mark)
8467                    ((memq number gnus-newsgroup-cached)
8468                     gnus-cached-mark)
8469                    ((memq number gnus-newsgroup-replied)
8470                     gnus-replied-mark)
8471                    (t gnus-unread-mark))
8472              gnus-tmp-from (mail-header-from gnus-tmp-header)
8473              gnus-tmp-name
8474              (cond
8475               ((string-match "(.+)" gnus-tmp-from)
8476                (substring gnus-tmp-from
8477                           (1+ (match-beginning 0)) (1- (match-end 0))))
8478               ((string-match "<[^>]+> *$" gnus-tmp-from)
8479                (setq beg-match (match-beginning 0))
8480                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8481                         (substring gnus-tmp-from (1+ (match-beginning 0))
8482                                    (1- (match-end 0))))
8483                    (substring gnus-tmp-from 0 beg-match)))
8484               (t gnus-tmp-from)))
8485             (when (string= gnus-tmp-name "")
8486               (setq gnus-tmp-name gnus-tmp-from))
8487             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8488             (put-text-property
8489              (point)
8490              (progn (eval gnus-summary-line-format-spec) (point))
8491              'gnus-number number)
8492             (when gnus-visual-p
8493               (forward-line -1)
8494               (run-hooks 'gnus-summary-update-hook)
8495               (forward-line 1))
8496
8497             (setq gnus-tmp-prev-subject subject)))
8498
8499         (when (nth 1 thread)
8500           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8501         (incf gnus-tmp-level)
8502         (setq threads (if thread-end nil (cdar thread)))
8503         (unless threads
8504           (setq gnus-tmp-level 0)))))
8505   (gnus-message 7 "Generating summary...done"))
8506
8507 (defun gnus-summary-prepare-unthreaded (headers)
8508   "Generate an unthreaded summary buffer based on HEADERS."
8509   (let (header number mark)
8510
8511     (while headers
8512       ;; We may have to root out some bad articles...
8513       (when (memq (setq number (mail-header-number
8514                                 (setq header (pop headers))))
8515                   gnus-newsgroup-limit)
8516         ;; Mark article as read when it has a low score.
8517         (when (and gnus-summary-mark-below
8518                    (< (or (cdr (assq number gnus-newsgroup-scored))
8519                           gnus-summary-default-score 0)
8520                       gnus-summary-mark-below))
8521           (setq gnus-newsgroup-unreads
8522                 (delq number gnus-newsgroup-unreads))
8523           (if gnus-newsgroup-auto-expire
8524               (push number gnus-newsgroup-expirable)
8525             (push (cons number gnus-low-score-mark)
8526                   gnus-newsgroup-reads)))
8527
8528         (setq mark
8529               (cond
8530                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8531                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8532                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8533                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8534                (t (or (cdr (assq number gnus-newsgroup-reads))
8535                       gnus-ancient-mark))))
8536         (setq gnus-newsgroup-data
8537               (cons (gnus-data-make number mark (1+ (point)) header 0)
8538                     gnus-newsgroup-data))
8539         (gnus-summary-insert-line
8540          header 0 nil mark (memq number gnus-newsgroup-replied)
8541          (memq number gnus-newsgroup-expirable)
8542          (mail-header-subject header) nil
8543          (cdr (assq number gnus-newsgroup-scored))
8544          (memq number gnus-newsgroup-processable))))))
8545
8546 (defun gnus-select-newsgroup (group &optional read-all)
8547   "Select newsgroup GROUP.
8548 If READ-ALL is non-nil, all articles in the group are selected."
8549   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8550          (info (nth 2 entry))
8551          articles fetched-articles cached)
8552
8553     (or (gnus-check-server
8554          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8555         (error "Couldn't open server"))
8556
8557     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8558         (gnus-activate-group group) ; Or we can activate it...
8559         (progn ; Or we bug out.
8560           (when (equal major-mode 'gnus-summary-mode)
8561             (kill-buffer (current-buffer)))
8562           (error "Couldn't request group %s: %s"
8563                  group (gnus-status-message group))))
8564
8565     (setq gnus-newsgroup-name group)
8566     (setq gnus-newsgroup-unselected nil)
8567     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8568
8569     (and gnus-asynchronous
8570          (gnus-check-backend-function
8571           'request-asynchronous gnus-newsgroup-name)
8572          (setq gnus-newsgroup-async
8573                (gnus-request-asynchronous gnus-newsgroup-name)))
8574
8575     ;; Adjust and set lists of article marks.
8576     (when info
8577       (gnus-adjust-marked-articles info))
8578
8579     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8580     (when (gnus-virtual-group-p group)
8581       (setq cached gnus-newsgroup-cached))
8582
8583     (setq gnus-newsgroup-unreads
8584           (gnus-set-difference
8585            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8586            gnus-newsgroup-dormant))
8587
8588     (setq gnus-newsgroup-processable nil)
8589
8590     (setq articles (gnus-articles-to-read group read-all))
8591
8592     (cond
8593      ((null articles)
8594       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8595       'quit)
8596      ((eq articles 0) nil)
8597      (t
8598       ;; Init the dependencies hash table.
8599       (setq gnus-newsgroup-dependencies
8600             (gnus-make-hashtable (length articles)))
8601       ;; Retrieve the headers and read them in.
8602       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8603       (setq gnus-newsgroup-headers
8604             (if (eq 'nov
8605                     (setq gnus-headers-retrieved-by
8606                           (gnus-retrieve-headers
8607                            articles gnus-newsgroup-name
8608                            ;; We might want to fetch old headers, but
8609                            ;; not if there is only 1 article.
8610                            (and gnus-fetch-old-headers
8611                                 (or (and
8612                                      (not (eq gnus-fetch-old-headers 'some))
8613                                      (not (numberp gnus-fetch-old-headers)))
8614                                     (> (length articles) 1))))))
8615                 (gnus-get-newsgroup-headers-xover articles)
8616               (gnus-get-newsgroup-headers)))
8617       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8618
8619       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8620       (when cached
8621         (setq gnus-newsgroup-cached cached))
8622
8623       ;; Set the initial limit.
8624       (setq gnus-newsgroup-limit (copy-sequence articles))
8625       ;; Remove canceled articles from the list of unread articles.
8626       (setq gnus-newsgroup-unreads
8627             (gnus-set-sorted-intersection
8628              gnus-newsgroup-unreads
8629              (setq fetched-articles
8630                    (mapcar (lambda (headers) (mail-header-number headers))
8631                            gnus-newsgroup-headers))))
8632       ;; Removed marked articles that do not exist.
8633       (gnus-update-missing-marks
8634        (gnus-sorted-complement fetched-articles articles))
8635       ;; We might want to build some more threads first.
8636       (and gnus-fetch-old-headers
8637            (eq gnus-headers-retrieved-by 'nov)
8638            (gnus-build-old-threads))
8639       ;; Check whether auto-expire is to be done in this group.
8640       (setq gnus-newsgroup-auto-expire
8641             (gnus-group-auto-expirable-p group))
8642       ;; Set up the article buffer now, if necessary.
8643       (unless gnus-single-article-buffer
8644         (gnus-article-setup-buffer))
8645       ;; First and last article in this newsgroup.
8646       (and gnus-newsgroup-headers
8647            (setq gnus-newsgroup-begin
8648                  (mail-header-number (car gnus-newsgroup-headers)))
8649            (setq gnus-newsgroup-end
8650                  (mail-header-number
8651                   (gnus-last-element gnus-newsgroup-headers))))
8652       (setq gnus-reffed-article-number -1)
8653       ;; GROUP is successfully selected.
8654       (or gnus-newsgroup-headers t)))))
8655
8656 (defun gnus-articles-to-read (group read-all)
8657   ;; Find out what articles the user wants to read.
8658   (let* ((articles
8659           ;; Select all articles if `read-all' is non-nil, or if there
8660           ;; are no unread articles.
8661           (if (or read-all
8662                   (and (zerop (length gnus-newsgroup-marked))
8663                        (zerop (length gnus-newsgroup-unreads))))
8664               (gnus-uncompress-range (gnus-active group))
8665             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8666                           (copy-sequence gnus-newsgroup-unreads))
8667                   '<)))
8668          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8669          (scored (length scored-list))
8670          (number (length articles))
8671          (marked (+ (length gnus-newsgroup-marked)
8672                     (length gnus-newsgroup-dormant)))
8673          (select
8674           (cond
8675            ((numberp read-all)
8676             read-all)
8677            (t
8678             (condition-case ()
8679                 (cond
8680                  ((and (or (<= scored marked) (= scored number))
8681                        (numberp gnus-large-newsgroup)
8682                        (> number gnus-large-newsgroup))
8683                   (let ((input
8684                          (read-string
8685                           (format
8686                            "How many articles from %s (default %d): "
8687                            gnus-newsgroup-name number))))
8688                     (if (string-match "^[ \t]*$" input) number input)))
8689                  ((and (> scored marked) (< scored number))
8690                   (let ((input
8691                          (read-string
8692                           (format "%s %s (%d scored, %d total): "
8693                                   "How many articles from"
8694                                   group scored number))))
8695                     (if (string-match "^[ \t]*$" input)
8696                         number input)))
8697                  (t number))
8698               (quit nil))))))
8699     (setq select (if (stringp select) (string-to-number select) select))
8700     (if (or (null select) (zerop select))
8701         select
8702       (if (and (not (zerop scored)) (<= (abs select) scored))
8703           (progn
8704             (setq articles (sort scored-list '<))
8705             (setq number (length articles)))
8706         (setq articles (copy-sequence articles)))
8707
8708       (if (< (abs select) number)
8709           (if (< select 0)
8710               ;; Select the N oldest articles.
8711               (setcdr (nthcdr (1- (abs select)) articles) nil)
8712             ;; Select the N most recent articles.
8713             (setq articles (nthcdr (- number select) articles))))
8714       (setq gnus-newsgroup-unselected
8715             (gnus-sorted-intersection
8716              gnus-newsgroup-unreads
8717              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8718       articles)))
8719
8720 (defun gnus-killed-articles (killed articles)
8721   (let (out)
8722     (while articles
8723       (if (inline (gnus-member-of-range (car articles) killed))
8724           (setq out (cons (car articles) out)))
8725       (setq articles (cdr articles)))
8726     out))
8727
8728 (defun gnus-uncompress-marks (marks)
8729   "Uncompress the mark ranges in MARKS."
8730   (let ((uncompressed '(score bookmark))
8731         out)
8732     (while marks
8733       (if (memq (caar marks) uncompressed)
8734           (push (car marks) out)
8735         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8736       (setq marks (cdr marks)))
8737     out))
8738
8739 (defun gnus-adjust-marked-articles (info)
8740   "Set all article lists and remove all marks that are no longer legal."
8741   (let* ((marked-lists (gnus-info-marks info))
8742          (active (gnus-active (gnus-info-group info)))
8743          (min (car active))
8744          (max (cdr active))
8745          (types gnus-article-mark-lists)
8746          (uncompressed '(score bookmark))
8747          marks var articles article mark)
8748
8749     (while marked-lists
8750       (setq marks (pop marked-lists))
8751       (set (setq var (intern (format "gnus-newsgroup-%s"
8752                                      (car (rassq (setq mark (car marks))
8753                                                  types)))))
8754            (if (memq (car marks) uncompressed) (cdr marks)
8755              (gnus-uncompress-range (cdr marks))))
8756
8757       (setq articles (symbol-value var))
8758
8759       ;; All articles have to be subsets of the active articles.
8760       (cond
8761        ;; Adjust "simple" lists.
8762        ((memq mark '(tick dormant expirable reply killed save))
8763         (while articles
8764           (when (or (< (setq article (pop articles)) min) (> article max))
8765             (set var (delq article (symbol-value var))))))
8766        ;; Adjust assocs.
8767        ((memq mark '(score bookmark))
8768         (while articles
8769           (when (or (< (car (setq article (pop articles))) min)
8770                     (> (car article) max))
8771             (set var (delq article (symbol-value var))))))))))
8772
8773 (defun gnus-update-missing-marks (missing)
8774   "Go through the list of MISSING articles and remove them mark lists."
8775   (when missing
8776     (let ((types gnus-article-mark-lists)
8777           var m)
8778       ;; Go through all types.
8779       (while types
8780         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8781         (when (symbol-value var)
8782           ;; This list has articles.  So we delete all missing articles
8783           ;; from it.
8784           (setq m missing)
8785           (while m
8786             (set var (delq (pop m) (symbol-value var)))))))))
8787
8788 (defun gnus-update-marks ()
8789   "Enter the various lists of marked articles into the newsgroup info list."
8790   (let ((types gnus-article-mark-lists)
8791         (info (gnus-get-info gnus-newsgroup-name))
8792         (uncompressed '(score bookmark killed))
8793         type list newmarked symbol)
8794     (when info
8795       ;; Add all marks lists that are non-nil to the list of marks lists.
8796       (while types
8797         (setq type (pop types))
8798         (when (setq list (symbol-value
8799                           (setq symbol
8800                                 (intern (format "gnus-newsgroup-%s"
8801                                                 (car type))))))
8802           (push (cons (cdr type)
8803                       (if (memq (cdr type) uncompressed) list
8804                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8805                 newmarked)))
8806
8807       ;; Enter these new marks into the info of the group.
8808       (if (nthcdr 3 info)
8809           (setcar (nthcdr 3 info) newmarked)
8810         ;; Add the marks lists to the end of the info.
8811         (when newmarked
8812           (setcdr (nthcdr 2 info) (list newmarked))))
8813
8814       ;; Cut off the end of the info if there's nothing else there.
8815       (let ((i 5))
8816         (while (and (> i 2)
8817                     (not (nth i info)))
8818           (when (nthcdr (decf i) info)
8819             (setcdr (nthcdr i info) nil)))))))
8820
8821 (defun gnus-add-marked-articles (group type articles &optional info force)
8822   ;; Add ARTICLES of TYPE to the info of GROUP.
8823   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8824   ;; add, but replace marked articles of TYPE with ARTICLES.
8825   (let ((info (or info (gnus-get-info group)))
8826         (uncompressed '(score bookmark killed))
8827         marked m)
8828     (or (not info)
8829         (and (not (setq marked (nthcdr 3 info)))
8830              (or (null articles)
8831                  (setcdr (nthcdr 2 info)
8832                          (list (list (cons type (gnus-compress-sequence
8833                                                  articles t)))))))
8834         (and (not (setq m (assq type (car marked))))
8835              (or (null articles)
8836                  (setcar marked
8837                          (cons (cons type (gnus-compress-sequence articles t) )
8838                                (car marked)))))
8839         (if force
8840             (if (null articles)
8841                 (setcar (nthcdr 3 info)
8842                         (delq (assq type (car marked)) (car marked)))
8843               (setcdr m (gnus-compress-sequence articles t)))
8844           (setcdr m (gnus-compress-sequence
8845                      (sort (nconc (gnus-uncompress-range (cdr m))
8846                                   (copy-sequence articles)) '<) t))))))
8847
8848 (defun gnus-set-mode-line (where)
8849   "This function sets the mode line of the article or summary buffers.
8850 If WHERE is `summary', the summary mode line format will be used."
8851   ;; Is this mode line one we keep updated?
8852   (when (memq where gnus-updated-mode-lines)
8853     (let (mode-string)
8854       (save-excursion
8855         ;; We evaluate this in the summary buffer since these
8856         ;; variables are buffer-local to that buffer.
8857         (set-buffer gnus-summary-buffer)
8858         ;; We bind all these variables that are used in the `eval' form
8859         ;; below.
8860         (let* ((mformat (symbol-value
8861                          (intern
8862                           (format "gnus-%s-mode-line-format-spec" where))))
8863                (gnus-tmp-group-name gnus-newsgroup-name)
8864                (gnus-tmp-article-number (or gnus-current-article 0))
8865                (gnus-tmp-unread gnus-newsgroup-unreads)
8866                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8867                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8868                (gnus-tmp-unread-and-unselected
8869                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8870                             (zerop gnus-tmp-unselected)) "")
8871                       ((zerop gnus-tmp-unselected)
8872                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8873                       (t (format "{%d(+%d) more}"
8874                                  gnus-tmp-unread-and-unticked
8875                                  gnus-tmp-unselected))))
8876                (gnus-tmp-subject
8877                 (if (and gnus-current-headers
8878                          (vectorp gnus-current-headers))
8879                     (mail-header-subject gnus-current-headers) ""))
8880                max-len
8881                gnus-tmp-header);; passed as argument to any user-format-funcs
8882           (setq mode-string (eval mformat))
8883           (setq max-len (max 4 (if gnus-mode-non-string-length
8884                                    (- (window-width)
8885                                       gnus-mode-non-string-length)
8886                                  (length mode-string))))
8887           ;; We might have to chop a bit of the string off...
8888           (when (> (length mode-string) max-len)
8889             (setq mode-string
8890                   (concat (gnus-truncate-string mode-string (- max-len 3))
8891                           "...")))
8892           ;; Pad the mode string a bit.
8893           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8894       ;; Update the mode line.
8895       (setq mode-line-buffer-identification (list mode-string))
8896       (set-buffer-modified-p t))))
8897
8898 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8899   "Go through the HEADERS list and add all Xrefs to a hash table.
8900 The resulting hash table is returned, or nil if no Xrefs were found."
8901   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8902          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8903          (xref-hashtb (make-vector 63 0))
8904          start group entry number xrefs header)
8905     (while headers
8906       (setq header (pop headers))
8907       (when (and (setq xrefs (mail-header-xref header))
8908                  (not (memq (setq number (mail-header-number header))
8909                             unreads)))
8910         (setq start 0)
8911         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8912           (setq start (match-end 0))
8913           (setq group (if prefix
8914                           (concat prefix (substring xrefs (match-beginning 1)
8915                                                     (match-end 1)))
8916                         (substring xrefs (match-beginning 1) (match-end 1))))
8917           (setq number
8918                 (string-to-int (substring xrefs (match-beginning 2)
8919                                           (match-end 2))))
8920           (if (setq entry (gnus-gethash group xref-hashtb))
8921               (setcdr entry (cons number (cdr entry)))
8922             (gnus-sethash group (cons number nil) xref-hashtb)))))
8923     (and start xref-hashtb)))
8924
8925 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8926   "Look through all the headers and mark the Xrefs as read."
8927   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8928         name entry info xref-hashtb idlist method nth4)
8929     (save-excursion
8930       (set-buffer gnus-group-buffer)
8931       (when (setq xref-hashtb
8932                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8933         (mapatoms
8934          (lambda (group)
8935            (unless (string= from-newsgroup (setq name (symbol-name group)))
8936              (setq idlist (symbol-value group))
8937              ;; Dead groups are not updated.
8938              (and (prog1
8939                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8940                             info (nth 2 entry))
8941                     (if (stringp (setq nth4 (gnus-info-method info)))
8942                         (setq nth4 (gnus-server-to-method nth4))))
8943                   ;; Only do the xrefs if the group has the same
8944                   ;; select method as the group we have just read.
8945                   (or (gnus-methods-equal-p
8946                        nth4 (gnus-find-method-for-group from-newsgroup))
8947                       virtual
8948                       (equal nth4 (setq method (gnus-find-method-for-group
8949                                                 from-newsgroup)))
8950                       (and (equal (car nth4) (car method))
8951                            (equal (nth 1 nth4) (nth 1 method))))
8952                   gnus-use-cross-reference
8953                   (or (not (eq gnus-use-cross-reference t))
8954                       virtual
8955                       ;; Only do cross-references on subscribed
8956                       ;; groups, if that is what is wanted.
8957                       (<= (gnus-info-level info) gnus-level-subscribed))
8958                   (gnus-group-make-articles-read name idlist))))
8959          xref-hashtb)))))
8960
8961 (defun gnus-group-make-articles-read (group articles)
8962   (let* ((num 0)
8963          (entry (gnus-gethash group gnus-newsrc-hashtb))
8964          (info (nth 2 entry))
8965          (active (gnus-active group))
8966          range)
8967     ;; First peel off all illegal article numbers.
8968     (if active
8969         (let ((ids articles)
8970               id first)
8971           (while ids
8972             (setq id (car ids))
8973             (if (and first (> id (cdr active)))
8974                 (progn
8975                   ;; We'll end up in this situation in one particular
8976                   ;; obscure situation.  If you re-scan a group and get
8977                   ;; a new article that is cross-posted to a different
8978                   ;; group that has not been re-scanned, you might get
8979                   ;; crossposted article that has a higher number than
8980                   ;; Gnus believes possible.  So we re-activate this
8981                   ;; group as well.  This might mean doing the
8982                   ;; crossposting thingy will *increase* the number
8983                   ;; of articles in some groups.  Tsk, tsk.
8984                   (setq active (or (gnus-activate-group group) active))))
8985             (if (or (> id (cdr active))
8986                     (< id (car active)))
8987                 (setq articles (delq id articles)))
8988             (setq ids (cdr ids)))))
8989     ;; If the read list is nil, we init it.
8990     (and active
8991          (null (gnus-info-read info))
8992          (> (car active) 1)
8993          (gnus-info-set-read info (cons 1 (1- (car active)))))
8994     ;; Then we add the read articles to the range.
8995     (gnus-info-set-read
8996      info
8997      (setq range
8998            (gnus-add-to-range
8999             (gnus-info-read info) (setq articles (sort articles '<)))))
9000     ;; Then we have to re-compute how many unread
9001     ;; articles there are in this group.
9002     (if active
9003         (progn
9004           (cond
9005            ((not range)
9006             (setq num (- (1+ (cdr active)) (car active))))
9007            ((not (listp (cdr range)))
9008             (setq num (- (cdr active) (- (1+ (cdr range))
9009                                          (car range)))))
9010            (t
9011             (while range
9012               (if (numberp (car range))
9013                   (setq num (1+ num))
9014                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9015               (setq range (cdr range)))
9016             (setq num (- (cdr active) num))))
9017           ;; Update the number of unread articles.
9018           (setcar entry num)
9019           ;; Update the group buffer.
9020           (gnus-group-update-group group t)))))
9021
9022 (defun gnus-methods-equal-p (m1 m2)
9023   (let ((m1 (or m1 gnus-select-method))
9024         (m2 (or m2 gnus-select-method)))
9025     (or (equal m1 m2)
9026         (and (eq (car m1) (car m2))
9027              (or (not (memq 'address (assoc (symbol-name (car m1))
9028                                             gnus-valid-select-methods)))
9029                  (equal (nth 1 m1) (nth 1 m2)))))))
9030
9031 (defsubst gnus-header-value ()
9032   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9033
9034 (defvar gnus-newsgroup-none-id 0)
9035
9036 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9037   (let ((cur nntp-server-buffer)
9038         (dependencies
9039          (or dependencies
9040              (save-excursion (set-buffer gnus-summary-buffer)
9041                              gnus-newsgroup-dependencies)))
9042         headers id id-dep ref-dep end ref)
9043     (save-excursion
9044       (set-buffer nntp-server-buffer)
9045       (run-hooks 'gnus-parse-headers-hook)
9046       (let ((case-fold-search t)
9047             in-reply-to header p lines)
9048         (goto-char (point-min))
9049         ;; Search to the beginning of the next header.  Error messages
9050         ;; do not begin with 2 or 3.
9051         (while (re-search-forward "^[23][0-9]+ " nil t)
9052           (setq id nil
9053                 ref nil)
9054           ;; This implementation of this function, with nine
9055           ;; search-forwards instead of the one re-search-forward and
9056           ;; a case (which basically was the old function) is actually
9057           ;; about twice as fast, even though it looks messier.  You
9058           ;; can't have everything, I guess.  Speed and elegance
9059           ;; doesn't always go hand in hand.
9060           (setq
9061            header
9062            (vector
9063             ;; Number.
9064             (prog1
9065                 (read cur)
9066               (end-of-line)
9067               (setq p (point))
9068               (narrow-to-region (point)
9069                                 (or (and (search-forward "\n.\n" nil t)
9070                                          (- (point) 2))
9071                                     (point))))
9072             ;; Subject.
9073             (progn
9074               (goto-char p)
9075               (if (search-forward "\nsubject: " nil t)
9076                   (gnus-header-value) "(none)"))
9077             ;; From.
9078             (progn
9079               (goto-char p)
9080               (if (search-forward "\nfrom: " nil t)
9081                   (gnus-header-value) "(nobody)"))
9082             ;; Date.
9083             (progn
9084               (goto-char p)
9085               (if (search-forward "\ndate: " nil t)
9086                   (gnus-header-value) ""))
9087             ;; Message-ID.
9088             (progn
9089               (goto-char p)
9090               (if (search-forward "\nmessage-id: " nil t)
9091                   (setq id (gnus-header-value))
9092                 ;; If there was no message-id, we just fake one to make
9093                 ;; subsequent routines simpler.
9094                 (setq id (concat "none+"
9095                                  (int-to-string
9096                                   (setq gnus-newsgroup-none-id
9097                                         (1+ gnus-newsgroup-none-id)))))))
9098             ;; References.
9099             (progn
9100               (goto-char p)
9101               (if (search-forward "\nreferences: " nil t)
9102                   (prog1
9103                       (gnus-header-value)
9104                     (setq end (match-end 0))
9105                     (save-excursion
9106                       (setq ref
9107                             (buffer-substring
9108                              (progn
9109                                (end-of-line)
9110                                (search-backward ">" end t)
9111                                (1+ (point)))
9112                              (progn
9113                                (search-backward "<" end t)
9114                                (point))))))
9115                 ;; Get the references from the in-reply-to header if there
9116                 ;; were no references and the in-reply-to header looks
9117                 ;; promising.
9118                 (if (and (search-forward "\nin-reply-to: " nil t)
9119                          (setq in-reply-to (gnus-header-value))
9120                          (string-match "<[^>]+>" in-reply-to))
9121                     (setq ref (substring in-reply-to (match-beginning 0)
9122                                          (match-end 0)))
9123                   (setq ref ""))))
9124             ;; Chars.
9125             0
9126             ;; Lines.
9127             (progn
9128               (goto-char p)
9129               (if (search-forward "\nlines: " nil t)
9130                   (if (numberp (setq lines (read cur)))
9131                       lines 0)
9132                 0))
9133             ;; Xref.
9134             (progn
9135               (goto-char p)
9136               (and (search-forward "\nxref: " nil t)
9137                    (gnus-header-value)))))
9138           ;; We do the threading while we read the headers.  The
9139           ;; message-id and the last reference are both entered into
9140           ;; the same hash table.  Some tippy-toeing around has to be
9141           ;; done in case an article has arrived before the article
9142           ;; which it refers to.
9143           (if (boundp (setq id-dep (intern id dependencies)))
9144               (if (and (car (symbol-value id-dep))
9145                        (not force-new))
9146                   ;; An article with this Message-ID has already
9147                   ;; been seen, so we ignore this one, except we add
9148                   ;; any additional Xrefs (in case the two articles
9149                   ;; came from different servers).
9150                   (progn
9151                     (mail-header-set-xref
9152                      (car (symbol-value id-dep))
9153                      (concat (or (mail-header-xref
9154                                   (car (symbol-value id-dep))) "")
9155                              (or (mail-header-xref header) "")))
9156                     (setq header nil))
9157                 (setcar (symbol-value id-dep) header))
9158             (set id-dep (list header)))
9159           (when header
9160             (if (boundp (setq ref-dep (intern ref dependencies)))
9161                 (setcdr (symbol-value ref-dep)
9162                         (nconc (cdr (symbol-value ref-dep))
9163                                (list (symbol-value id-dep))))
9164               (set ref-dep (list nil (symbol-value id-dep))))
9165             (setq headers (cons header headers)))
9166           (goto-char (point-max))
9167           (widen))
9168         (nreverse headers)))))
9169
9170 ;; The following macros and functions were written by Felix Lee
9171 ;; <flee@cse.psu.edu>.
9172
9173 (defmacro gnus-nov-read-integer ()
9174   '(prog1
9175        (if (= (following-char) ?\t)
9176            0
9177          (let ((num (condition-case nil (read buffer) (error nil))))
9178            (if (numberp num) num 0)))
9179      (or (eobp) (forward-char 1))))
9180
9181 (defmacro gnus-nov-skip-field ()
9182   '(search-forward "\t" eol 'move))
9183
9184 (defmacro gnus-nov-field ()
9185   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9186
9187 ;; Goes through the xover lines and returns a list of vectors
9188 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9189                                                   force-new dependencies)
9190   "Parse the news overview data in the server buffer, and return a
9191 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9192   ;; Get the Xref when the users reads the articles since most/some
9193   ;; NNTP servers do not include Xrefs when using XOVER.
9194   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9195   (let ((cur nntp-server-buffer)
9196         (dependencies (or dependencies gnus-newsgroup-dependencies))
9197         number headers header)
9198     (save-excursion
9199       (set-buffer nntp-server-buffer)
9200       ;; Allow the user to mangle the headers before parsing them.
9201       (run-hooks 'gnus-parse-headers-hook)
9202       (goto-char (point-min))
9203       (while (and sequence (not (eobp)))
9204         (setq number (read cur))
9205         (while (and sequence (< (car sequence) number))
9206           (setq sequence (cdr sequence)))
9207         (and sequence
9208              (eq number (car sequence))
9209              (progn
9210                (setq sequence (cdr sequence))
9211                (if (setq header
9212                          (inline (gnus-nov-parse-line
9213                                   number dependencies force-new)))
9214                    (setq headers (cons header headers)))))
9215         (forward-line 1))
9216       (setq headers (nreverse headers)))
9217     headers))
9218
9219 ;; This function has to be called with point after the article number
9220 ;; on the beginning of the line.
9221 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9222   (let ((none 0)
9223         (eol (gnus-point-at-eol))
9224         (buffer (current-buffer))
9225         header ref id id-dep ref-dep)
9226
9227     ;; overview: [num subject from date id refs chars lines misc]
9228     (narrow-to-region (point) eol)
9229     (or (eobp) (forward-char))
9230
9231     (condition-case nil
9232         (setq header
9233               (vector
9234                number                   ; number
9235                (gnus-nov-field)         ; subject
9236                (gnus-nov-field)         ; from
9237                (gnus-nov-field)         ; date
9238                (setq id (or (gnus-nov-field)
9239                             (concat "none+"
9240                                     (int-to-string
9241                                      (setq none (1+ none)))))) ; id
9242                (progn
9243                  (save-excursion
9244                    (let ((beg (point)))
9245                      (search-forward "\t" eol)
9246                      (if (search-backward ">" beg t)
9247                          (setq ref
9248                                (buffer-substring
9249                                 (1+ (point))
9250                                 (search-backward "<" beg t)))
9251                        (setq ref nil))))
9252                  (gnus-nov-field))      ; refs
9253                (gnus-nov-read-integer)  ; chars
9254                (gnus-nov-read-integer)  ; lines
9255                (if (= (following-char) ?\n)
9256                    nil
9257                  (gnus-nov-field))      ; misc
9258                ))
9259       (error (progn
9260                (gnus-error 4 "Strange nov line")
9261                (setq header nil)
9262                (goto-char eol))))
9263
9264     (widen)
9265
9266     ;; We build the thread tree.
9267     (when header
9268       (if (boundp (setq id-dep (intern id dependencies)))
9269           (if (and (car (symbol-value id-dep))
9270                    (not force-new))
9271               ;; An article with this Message-ID has already been seen,
9272               ;; so we ignore this one, except we add any additional
9273               ;; Xrefs (in case the two articles came from different
9274               ;; servers.
9275               (progn
9276                 (mail-header-set-xref
9277                  (car (symbol-value id-dep))
9278                  (concat (or (mail-header-xref
9279                               (car (symbol-value id-dep))) "")
9280                          (or (mail-header-xref header) "")))
9281                 (setq header nil))
9282             (setcar (symbol-value id-dep) header))
9283         (set id-dep (list header))))
9284     (if header
9285         (progn
9286           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9287               (setcdr (symbol-value ref-dep)
9288                       (nconc (cdr (symbol-value ref-dep))
9289                              (list (symbol-value id-dep))))
9290             (set ref-dep (list nil (symbol-value id-dep))))))
9291     header))
9292
9293 (defun gnus-article-get-xrefs ()
9294   "Fill in the Xref value in `gnus-current-headers', if necessary.
9295 This is meant to be called in `gnus-article-internal-prepare-hook'."
9296   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9297                                  gnus-current-headers)))
9298     (or (not gnus-use-cross-reference)
9299         (not headers)
9300         (and (mail-header-xref headers)
9301              (not (string= (mail-header-xref headers) "")))
9302         (let ((case-fold-search t)
9303               xref)
9304           (save-restriction
9305             (nnheader-narrow-to-headers)
9306             (goto-char (point-min))
9307             (if (or (and (eq (downcase (following-char)) ?x)
9308                          (looking-at "Xref:"))
9309                     (search-forward "\nXref:" nil t))
9310                 (progn
9311                   (goto-char (1+ (match-end 0)))
9312                   (setq xref (buffer-substring (point)
9313                                                (progn (end-of-line) (point))))
9314                   (mail-header-set-xref headers xref))))))))
9315
9316 (defun gnus-summary-insert-subject (id &optional old-header)
9317   "Find article ID and insert the summary line for that article."
9318   (let ((header (gnus-read-header id))
9319         (number (and (numberp id) id))
9320         pos)
9321     (when header
9322       ;; Rebuild the thread that this article is part of and go to the
9323       ;; article we have fetched.
9324       (when old-header
9325         (when (setq pos (text-property-any
9326                          (point-min) (point-max) 'gnus-number 
9327                          (mail-header-number old-header)))
9328           (goto-char pos)
9329           (gnus-delete-line)
9330           (gnus-data-remove (mail-header-number old-header))))
9331       (gnus-rebuild-thread (mail-header-id header))
9332       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9333     (when (and (numberp number)
9334                (> number 0))
9335       ;; We have to update the boundaries even if we can't fetch the
9336       ;; article if ID is a number -- so that the next `P' or `N'
9337       ;; command will fetch the previous (or next) article even
9338       ;; if the one we tried to fetch this time has been canceled.
9339       (and (> number gnus-newsgroup-end)
9340            (setq gnus-newsgroup-end number))
9341       (and (< number gnus-newsgroup-begin)
9342            (setq gnus-newsgroup-begin number))
9343       (setq gnus-newsgroup-unselected
9344             (delq number gnus-newsgroup-unselected)))
9345     ;; Report back a success?
9346     (and header (mail-header-number header))))
9347
9348 (defun gnus-summary-work-articles (n)
9349   "Return a list of articles to be worked upon.  The prefix argument,
9350 the list of process marked articles, and the current article will be
9351 taken into consideration."
9352   (cond
9353    ((and n (numberp n))
9354     ;; A numerical prefix has been given.
9355     (let ((backward (< n 0))
9356           (n (abs n))
9357           articles article)
9358       (save-excursion
9359         (while
9360             (and (> n 0)
9361                  (push (setq article (gnus-summary-article-number))
9362                        articles)
9363                  (if backward
9364                      (gnus-summary-find-prev nil article)
9365                    (gnus-summary-find-next nil article)))
9366           (decf n)))
9367       (nreverse articles)))
9368    ((and (boundp 'transient-mark-mode)
9369          transient-mark-mode
9370          mark-active)
9371     ;; Work on the region between point and mark.
9372     (let ((max (max (point) (mark)))
9373           articles article)
9374       (save-excursion
9375         (goto-char (min (point) (mark)))
9376         (while
9377             (and
9378              (push (setq article (gnus-summary-article-number)) articles)
9379              (gnus-summary-find-next nil article)
9380              (< (point) max)))
9381         (nreverse articles))))
9382    (gnus-newsgroup-processable
9383     ;; There are process-marked articles present.
9384     (reverse gnus-newsgroup-processable))
9385    (t
9386     ;; Just return the current article.
9387     (list (gnus-summary-article-number)))))
9388
9389 (defun gnus-summary-search-group (&optional backward use-level)
9390   "Search for next unread newsgroup.
9391 If optional argument BACKWARD is non-nil, search backward instead."
9392   (save-excursion
9393     (set-buffer gnus-group-buffer)
9394     (if (gnus-group-search-forward
9395          backward nil (if use-level (gnus-group-group-level) nil))
9396         (gnus-group-group-name))))
9397
9398 (defun gnus-summary-best-group (&optional exclude-group)
9399   "Find the name of the best unread group.
9400 If EXCLUDE-GROUP, do not go to this group."
9401   (save-excursion
9402     (set-buffer gnus-group-buffer)
9403     (save-excursion
9404       (gnus-group-best-unread-group exclude-group))))
9405
9406 (defun gnus-summary-find-next (&optional unread article backward)
9407   (if backward (gnus-summary-find-prev)
9408     (let* ((dummy (gnus-summary-article-intangible-p))
9409            (article (or article (gnus-summary-article-number)))
9410            (arts (gnus-data-find-list article))
9411            result)
9412       (when (and (not dummy)
9413                  (or (not gnus-summary-check-current)
9414                      (not unread)
9415                      (not (gnus-data-unread-p (car arts)))))
9416         (setq arts (cdr arts)))
9417       (when (setq result
9418                   (if unread
9419                       (progn
9420                         (while arts
9421                           (when (gnus-data-unread-p (car arts))
9422                             (setq result (car arts)
9423                                   arts nil))
9424                           (setq arts (cdr arts)))
9425                         result)
9426                     (car arts)))
9427         (goto-char (gnus-data-pos result))
9428         (gnus-data-number result)))))
9429
9430 (defun gnus-summary-find-prev (&optional unread article)
9431   (let* ((article (or article (gnus-summary-article-number)))
9432          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9433          result)
9434     (when (or (not gnus-summary-check-current)
9435               (not unread)
9436               (not (gnus-data-unread-p (car arts))))
9437       (setq arts (cdr arts)))
9438     (if (setq result
9439               (if unread
9440                   (progn
9441                     (while arts
9442                       (and (gnus-data-unread-p (car arts))
9443                            (setq result (car arts)
9444                                  arts nil))
9445                       (setq arts (cdr arts)))
9446                     result)
9447                 (car arts)))
9448         (progn
9449           (goto-char (gnus-data-pos result))
9450           (gnus-data-number result)))))
9451
9452 (defun gnus-summary-find-subject (subject &optional unread backward article)
9453   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9454          (article (or article (gnus-summary-article-number)))
9455          (articles (gnus-data-list backward))
9456          (arts (gnus-data-find-list article articles))
9457          result)
9458     (when (or (not gnus-summary-check-current)
9459               (not unread)
9460               (not (gnus-data-unread-p (car arts))))
9461       (setq arts (cdr arts)))
9462     (while arts
9463       (and (or (not unread)
9464                (gnus-data-unread-p (car arts)))
9465            (vectorp (gnus-data-header (car arts)))
9466            (gnus-subject-equal
9467             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9468            (setq result (car arts)
9469                  arts nil))
9470       (setq arts (cdr arts)))
9471     (and result
9472          (goto-char (gnus-data-pos result))
9473          (gnus-data-number result))))
9474
9475 (defun gnus-summary-search-forward (&optional unread subject backward)
9476   "Search forward for an article.
9477 If UNREAD, look for unread articles.  If SUBJECT, look for
9478 articles with that subject.  If BACKWARD, search backward instead."
9479   (cond (subject (gnus-summary-find-subject subject unread backward))
9480         (backward (gnus-summary-find-prev unread))
9481         (t (gnus-summary-find-next unread))))
9482
9483 (defun gnus-recenter (&optional n)
9484   "Center point in window and redisplay frame.
9485 Also do horizontal recentering."
9486   (interactive "P")
9487   (when (and gnus-auto-center-summary
9488              (not (eq gnus-auto-center-summary 'vertical)))
9489     (gnus-horizontal-recenter))
9490   (recenter n))
9491
9492 (defun gnus-summary-recenter ()
9493   "Center point in the summary window.
9494 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9495 displayed, no centering will be performed."
9496   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9497   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9498   (let* ((top (cond ((< (window-height) 4) 0)
9499                     ((< (window-height) 7) 1)
9500                     (t 2)))
9501          (height (1- (window-height)))
9502          (bottom (save-excursion (goto-char (point-max))
9503                                  (forward-line (- height))
9504                                  (point)))
9505          (window (get-buffer-window (current-buffer))))
9506     ;; The user has to want it.
9507     (when gnus-auto-center-summary
9508       (when (get-buffer-window gnus-article-buffer)
9509        ;; Only do recentering when the article buffer is displayed,
9510        ;; Set the window start to either `bottom', which is the biggest
9511        ;; possible valid number, or the second line from the top,
9512        ;; whichever is the least.
9513        (set-window-start
9514         window (min bottom (save-excursion 
9515                              (forward-line (- top)) (point)))))
9516       ;; Do horizontal recentering while we're at it.
9517       (when (and (get-buffer-window (current-buffer) t)
9518                  (not (eq gnus-auto-center-summary 'vertical)))
9519         (let ((selected (selected-window)))
9520           (select-window (get-buffer-window (current-buffer) t))
9521           (gnus-summary-position-point)
9522           (gnus-horizontal-recenter)
9523           (select-window selected))))))
9524
9525 (defun gnus-horizontal-recenter ()
9526   "Recenter the current buffer horizontally."
9527   (if (< (current-column) (/ (window-width) 2))
9528       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9529     (let* ((orig (point))
9530            (end (window-end (get-buffer-window (current-buffer) t)))
9531            (max 0))
9532       ;; Find the longest line currently displayed in the window.
9533       (goto-char (window-start))
9534       (while (and (not (eobp)) 
9535                   (< (point) end))
9536         (end-of-line)
9537         (setq max (max max (current-column)))
9538         (forward-line 1))
9539       (goto-char orig)
9540       ;; Scroll horizontally to center (sort of) the point.
9541       (if (> max (window-width))
9542           (set-window-hscroll 
9543            (get-buffer-window (current-buffer) t)
9544            (min (- (current-column) (/ (window-width) 3))
9545                 (+ 2 (- max (window-width)))))
9546         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9547       max)))
9548
9549 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9550 (defun gnus-short-group-name (group &optional levels)
9551   "Collapse GROUP name LEVELS."
9552   (let* ((name "") 
9553          (foreign "")
9554          (depth 0) 
9555          (skip 1)
9556          (levels (or levels
9557                      (progn
9558                        (while (string-match "\\." group skip)
9559                          (setq skip (match-end 0)
9560                                depth (+ depth 1)))
9561                        depth))))
9562     (if (string-match ":" group)
9563         (setq foreign (substring group 0 (match-end 0))
9564               group (substring group (match-end 0))))
9565     (while group
9566       (if (and (string-match "\\." group)
9567                (> levels (- gnus-group-uncollapsed-levels 1)))
9568           (setq name (concat name (substring group 0 1))
9569                 group (substring group (match-end 0))
9570                 levels (- levels 1)
9571                 name (concat name "."))
9572         (setq name (concat foreign name group)
9573               group nil)))
9574     name))
9575
9576 (defun gnus-summary-jump-to-group (newsgroup)
9577   "Move point to NEWSGROUP in group mode buffer."
9578   ;; Keep update point of group mode buffer if visible.
9579   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9580       (save-window-excursion
9581         ;; Take care of tree window mode.
9582         (if (get-buffer-window gnus-group-buffer)
9583             (pop-to-buffer gnus-group-buffer))
9584         (gnus-group-jump-to-group newsgroup))
9585     (save-excursion
9586       ;; Take care of tree window mode.
9587       (if (get-buffer-window gnus-group-buffer)
9588           (pop-to-buffer gnus-group-buffer)
9589         (set-buffer gnus-group-buffer))
9590       (gnus-group-jump-to-group newsgroup))))
9591
9592 ;; This function returns a list of article numbers based on the
9593 ;; difference between the ranges of read articles in this group and
9594 ;; the range of active articles.
9595 (defun gnus-list-of-unread-articles (group)
9596   (let* ((read (gnus-info-read (gnus-get-info group)))
9597          (active (gnus-active group))
9598          (last (cdr active))
9599          first nlast unread)
9600     ;; If none are read, then all are unread.
9601     (if (not read)
9602         (setq first (car active))
9603       ;; If the range of read articles is a single range, then the
9604       ;; first unread article is the article after the last read
9605       ;; article.  Sounds logical, doesn't it?
9606       (if (not (listp (cdr read)))
9607           (setq first (1+ (cdr read)))
9608         ;; `read' is a list of ranges.
9609         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9610                                 (caar read))) 1)
9611             (setq first 1))
9612         (while read
9613           (if first
9614               (while (< first nlast)
9615                 (setq unread (cons first unread))
9616                 (setq first (1+ first))))
9617           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9618           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9619           (setq read (cdr read)))))
9620     ;; And add the last unread articles.
9621     (while (<= first last)
9622       (setq unread (cons first unread))
9623       (setq first (1+ first)))
9624     ;; Return the list of unread articles.
9625     (nreverse unread)))
9626
9627 (defun gnus-list-of-read-articles (group)
9628   "Return a list of unread, unticked and non-dormant articles."
9629   (let* ((info (gnus-get-info group))
9630          (marked (gnus-info-marks info))
9631          (active (gnus-active group)))
9632     (and info active
9633          (gnus-set-difference
9634           (gnus-sorted-complement
9635            (gnus-uncompress-range active)
9636            (gnus-list-of-unread-articles group))
9637           (append
9638            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9639            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9640
9641 ;; Various summary commands
9642
9643 (defun gnus-summary-universal-argument (arg)
9644   "Perform any operation on all articles that are process/prefixed."
9645   (interactive "P")
9646   (gnus-set-global-variables)
9647   (let ((articles (gnus-summary-work-articles arg))
9648         func article)
9649     (if (eq
9650          (setq
9651           func
9652           (key-binding
9653            (read-key-sequence
9654             (substitute-command-keys
9655              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9656              ))))
9657          'undefined)
9658         (gnus-error 1 "Undefined key")
9659       (save-excursion
9660         (while articles
9661           (gnus-summary-goto-subject (setq article (pop articles)))
9662           (command-execute func)
9663           (gnus-summary-remove-process-mark article)))))
9664   (gnus-summary-position-point))
9665
9666 (defun gnus-summary-toggle-truncation (&optional arg)
9667   "Toggle truncation of summary lines.
9668 With arg, turn line truncation on iff arg is positive."
9669   (interactive "P")
9670   (setq truncate-lines
9671         (if (null arg) (not truncate-lines)
9672           (> (prefix-numeric-value arg) 0)))
9673   (redraw-display))
9674
9675 (defun gnus-summary-reselect-current-group (&optional all rescan)
9676   "Exit and then reselect the current newsgroup.
9677 The prefix argument ALL means to select all articles."
9678   (interactive "P")
9679   (gnus-set-global-variables)
9680   (let ((current-subject (gnus-summary-article-number))
9681         (group gnus-newsgroup-name))
9682     (setq gnus-newsgroup-begin nil)
9683     (gnus-summary-exit)
9684     ;; We have to adjust the point of group mode buffer because the
9685     ;; current point was moved to the next unread newsgroup by
9686     ;; exiting.
9687     (gnus-summary-jump-to-group group)
9688     (when rescan
9689       (save-excursion
9690         (gnus-group-get-new-news-this-group 1)))
9691     (gnus-group-read-group all t)
9692     (gnus-summary-goto-subject current-subject)))
9693
9694 (defun gnus-summary-rescan-group (&optional all)
9695   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9696   (interactive "P")
9697   (gnus-summary-reselect-current-group all t))
9698
9699 (defun gnus-summary-update-info ()
9700   (let* ((group gnus-newsgroup-name))
9701     (when gnus-newsgroup-kill-headers
9702       (setq gnus-newsgroup-killed
9703             (gnus-compress-sequence
9704              (nconc
9705               (gnus-set-sorted-intersection
9706                (gnus-uncompress-range gnus-newsgroup-killed)
9707                (setq gnus-newsgroup-unselected
9708                      (sort gnus-newsgroup-unselected '<)))
9709               (setq gnus-newsgroup-unreads
9710                     (sort gnus-newsgroup-unreads '<))) t)))
9711     (unless (listp (cdr gnus-newsgroup-killed))
9712       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9713     (let ((headers gnus-newsgroup-headers))
9714       (run-hooks 'gnus-exit-group-hook)
9715       (unless gnus-save-score
9716         (setq gnus-newsgroup-scored nil))
9717       ;; Set the new ranges of read articles.
9718       (gnus-update-read-articles
9719        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9720       ;; Set the current article marks.
9721       (gnus-update-marks)
9722       ;; Do the cross-ref thing.
9723       (when gnus-use-cross-reference
9724         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9725       ;; Do adaptive scoring, and possibly save score files.
9726       (when gnus-newsgroup-adaptive
9727         (gnus-score-adaptive))
9728       (when gnus-use-scoring
9729         (gnus-score-save))
9730       ;; Do not switch windows but change the buffer to work.
9731       (set-buffer gnus-group-buffer)
9732       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9733           (gnus-group-update-group group)))))
9734
9735 (defun gnus-summary-exit (&optional temporary)
9736   "Exit reading current newsgroup, and then return to group selection mode.
9737 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9738   (interactive)
9739   (gnus-set-global-variables)
9740   (gnus-kill-save-kill-buffer)
9741   (let* ((group gnus-newsgroup-name)
9742          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9743          (mode major-mode)
9744          (buf (current-buffer)))
9745     (run-hooks 'gnus-summary-prepare-exit-hook)
9746     ;; If we have several article buffers, we kill them at exit.
9747     (unless gnus-single-article-buffer
9748       (gnus-kill-buffer gnus-article-buffer)
9749       (gnus-kill-buffer gnus-original-article-buffer)
9750       (setq gnus-article-current nil))
9751     (when gnus-use-cache
9752       (gnus-cache-possibly-remove-articles)
9753       (gnus-cache-save-buffers))
9754     (when gnus-use-trees
9755       (gnus-tree-close group))
9756     ;; Make all changes in this group permanent.
9757     (unless quit-config
9758       (gnus-summary-update-info))
9759     (gnus-close-group group)
9760     ;; Make sure where I was, and go to next newsgroup.
9761     (set-buffer gnus-group-buffer)
9762     (unless quit-config
9763       (gnus-group-jump-to-group group)
9764       (gnus-group-next-unread-group 1))
9765     (run-hooks 'gnus-summary-exit-hook)
9766     (unless gnus-single-article-buffer
9767       (setq gnus-article-current nil))
9768     (if temporary
9769         nil                             ;Nothing to do.
9770       ;; If we have several article buffers, we kill them at exit.
9771       (unless gnus-single-article-buffer
9772         (gnus-kill-buffer gnus-article-buffer)
9773         (gnus-kill-buffer gnus-original-article-buffer)
9774         (setq gnus-article-current nil))
9775       (set-buffer buf)
9776       (if (not gnus-kill-summary-on-exit)
9777           (gnus-deaden-summary)
9778         ;; We set all buffer-local variables to nil.  It is unclear why
9779         ;; this is needed, but if we don't, buffer-local variables are
9780         ;; not garbage-collected, it seems.  This would the lead to en
9781         ;; ever-growing Emacs.
9782         (gnus-summary-clear-local-variables)
9783         (when (get-buffer gnus-article-buffer)
9784           (bury-buffer gnus-article-buffer))
9785         ;; We clear the global counterparts of the buffer-local
9786         ;; variables as well, just to be on the safe side.
9787         (gnus-configure-windows 'group 'force)
9788         (gnus-summary-clear-local-variables)
9789         ;; Return to group mode buffer.
9790         (if (eq mode 'gnus-summary-mode)
9791             (gnus-kill-buffer buf)))
9792       (setq gnus-current-select-method gnus-select-method)
9793       (pop-to-buffer gnus-group-buffer)
9794       ;; Clear the current group name.
9795       (if (not quit-config)
9796           (progn
9797             (gnus-group-jump-to-group group)
9798             (gnus-group-next-unread-group 1)
9799             (gnus-configure-windows 'group 'force))
9800         (if (not (buffer-name (car quit-config)))
9801             (gnus-configure-windows 'group 'force)
9802           (set-buffer (car quit-config))
9803           (and (eq major-mode 'gnus-summary-mode)
9804                (gnus-set-global-variables))
9805           (gnus-configure-windows (cdr quit-config))))
9806       (unless quit-config
9807         (setq gnus-newsgroup-name nil)))))
9808
9809 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9810 (defun gnus-summary-exit-no-update (&optional no-questions)
9811   "Quit reading current newsgroup without updating read article info."
9812   (interactive)
9813   (gnus-set-global-variables)
9814   (let* ((group gnus-newsgroup-name)
9815          (quit-config (gnus-group-quit-config group)))
9816     (when (or no-questions
9817               gnus-expert-user
9818               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9819       ;; If we have several article buffers, we kill them at exit.
9820       (unless gnus-single-article-buffer
9821         (gnus-kill-buffer gnus-article-buffer)
9822         (gnus-kill-buffer gnus-original-article-buffer)
9823         (setq gnus-article-current nil))
9824       (if (not gnus-kill-summary-on-exit)
9825           (gnus-deaden-summary)
9826         (gnus-close-group group)
9827         (gnus-summary-clear-local-variables)
9828         (set-buffer gnus-group-buffer)
9829         (gnus-summary-clear-local-variables)
9830         (when (get-buffer gnus-summary-buffer)
9831           (kill-buffer gnus-summary-buffer)))
9832       (unless gnus-single-article-buffer
9833         (setq gnus-article-current nil))
9834       (when gnus-use-trees
9835         (gnus-tree-close group))
9836       (when (get-buffer gnus-article-buffer)
9837         (bury-buffer gnus-article-buffer))
9838       ;; Return to the group buffer.
9839       (gnus-configure-windows 'group 'force)
9840       ;; Clear the current group name.
9841       (setq gnus-newsgroup-name nil)
9842       (when (equal (gnus-group-group-name) group)
9843         (gnus-group-next-unread-group 1))
9844       (when quit-config
9845         (if (not (buffer-name (car quit-config)))
9846             (gnus-configure-windows 'group 'force)
9847           (set-buffer (car quit-config))
9848           (when (eq major-mode 'gnus-summary-mode)
9849             (gnus-set-global-variables))
9850           (gnus-configure-windows (cdr quit-config)))))))
9851
9852 ;;; Dead summaries.
9853
9854 (defvar gnus-dead-summary-mode-map nil)
9855
9856 (if gnus-dead-summary-mode-map
9857     nil
9858   (setq gnus-dead-summary-mode-map (make-keymap))
9859   (suppress-keymap gnus-dead-summary-mode-map)
9860   (substitute-key-definition
9861    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9862   (let ((keys '("\C-d" "\r" "\177")))
9863     (while keys
9864       (define-key gnus-dead-summary-mode-map
9865         (pop keys) 'gnus-summary-wake-up-the-dead))))
9866
9867 (defvar gnus-dead-summary-mode nil
9868   "Minor mode for Gnus summary buffers.")
9869
9870 (defun gnus-dead-summary-mode (&optional arg)
9871   "Minor mode for Gnus summary buffers."
9872   (interactive "P")
9873   (when (eq major-mode 'gnus-summary-mode)
9874     (make-local-variable 'gnus-dead-summary-mode)
9875     (setq gnus-dead-summary-mode
9876           (if (null arg) (not gnus-dead-summary-mode)
9877             (> (prefix-numeric-value arg) 0)))
9878     (when gnus-dead-summary-mode
9879       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9880         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9881       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9882         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9883               minor-mode-map-alist)))))
9884
9885 (defun gnus-deaden-summary ()
9886   "Make the current summary buffer into a dead summary buffer."
9887   ;; Kill any previous dead summary buffer.
9888   (when (and gnus-dead-summary
9889              (buffer-name gnus-dead-summary))
9890     (save-excursion
9891       (set-buffer gnus-dead-summary)
9892       (when gnus-dead-summary-mode
9893         (kill-buffer (current-buffer)))))
9894   ;; Make this the current dead summary.
9895   (setq gnus-dead-summary (current-buffer))
9896   (gnus-dead-summary-mode 1)
9897   (let ((name (buffer-name)))
9898     (when (string-match "Summary" name)
9899       (rename-buffer
9900        (concat (substring name 0 (match-beginning 0)) "Dead "
9901                (substring name (match-beginning 0))) t))))
9902
9903 (defun gnus-kill-or-deaden-summary (buffer)
9904   "Kill or deaden the summary BUFFER."
9905   (cond (gnus-kill-summary-on-exit
9906          (when (and gnus-use-trees
9907                     (and (get-buffer buffer)
9908                          (buffer-name (get-buffer buffer))))
9909            (save-excursion
9910              (set-buffer (get-buffer buffer))
9911              (gnus-tree-close gnus-newsgroup-name)))
9912          (gnus-kill-buffer buffer))
9913         ((and (get-buffer buffer)
9914               (buffer-name (get-buffer buffer)))
9915          (save-excursion
9916            (set-buffer buffer)
9917            (gnus-deaden-summary)))))
9918
9919 (defun gnus-summary-wake-up-the-dead (&rest args)
9920   "Wake up the dead summary buffer."
9921   (interactive)
9922   (gnus-dead-summary-mode -1)
9923   (let ((name (buffer-name)))
9924     (when (string-match "Dead " name)
9925       (rename-buffer
9926        (concat (substring name 0 (match-beginning 0))
9927                (substring name (match-end 0))) t)))
9928   (gnus-message 3 "This dead summary is now alive again"))
9929
9930 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9931 (defun gnus-summary-fetch-faq (&optional faq-dir)
9932   "Fetch the FAQ for the current group.
9933 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9934 in."
9935   (interactive
9936    (list
9937     (if current-prefix-arg
9938         (completing-read
9939          "Faq dir: " (and (listp gnus-group-faq-directory)
9940                           gnus-group-faq-directory)))))
9941   (let (gnus-faq-buffer)
9942     (and (setq gnus-faq-buffer
9943                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9944          (gnus-configure-windows 'summary-faq))))
9945
9946 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9947 (defun gnus-summary-describe-group (&optional force)
9948   "Describe the current newsgroup."
9949   (interactive "P")
9950   (gnus-group-describe-group force gnus-newsgroup-name))
9951
9952 (defun gnus-summary-describe-briefly ()
9953   "Describe summary mode commands briefly."
9954   (interactive)
9955   (gnus-message 6
9956                 (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")))
9957
9958 ;; Walking around group mode buffer from summary mode.
9959
9960 (defun gnus-summary-next-group (&optional no-article target-group backward)
9961   "Exit current newsgroup and then select next unread newsgroup.
9962 If prefix argument NO-ARTICLE is non-nil, no article is selected
9963 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9964 previous group instead."
9965   (interactive "P")
9966   (gnus-set-global-variables)
9967   (let ((current-group gnus-newsgroup-name)
9968         (current-buffer (current-buffer))
9969         entered)
9970     ;; First we semi-exit this group to update Xrefs and all variables.
9971     ;; We can't do a real exit, because the window conf must remain
9972     ;; the same in case the user is prompted for info, and we don't
9973     ;; want the window conf to change before that...
9974     (gnus-summary-exit t)
9975     (while (not entered)
9976       ;; Then we find what group we are supposed to enter.
9977       (set-buffer gnus-group-buffer)
9978       (gnus-group-jump-to-group current-group)
9979       (setq target-group
9980             (or target-group
9981                 (if (eq gnus-keep-same-level 'best)
9982                     (gnus-summary-best-group gnus-newsgroup-name)
9983                   (gnus-summary-search-group backward gnus-keep-same-level))))
9984       (if (not target-group)
9985           ;; There are no further groups, so we return to the group
9986           ;; buffer.
9987           (progn
9988             (gnus-message 5 "Returning to the group buffer")
9989             (setq entered t)
9990             (set-buffer current-buffer)
9991             (gnus-summary-exit))
9992         ;; We try to enter the target group.
9993         (gnus-group-jump-to-group target-group)
9994         (let ((unreads (gnus-group-group-unread)))
9995           (if (and (or (eq t unreads)
9996                        (and unreads (not (zerop unreads))))
9997                    (gnus-summary-read-group
9998                     target-group nil no-article current-buffer))
9999               (setq entered t)
10000             (setq current-group target-group
10001                   target-group nil)))))))
10002
10003 (defun gnus-summary-prev-group (&optional no-article)
10004   "Exit current newsgroup and then select previous unread newsgroup.
10005 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10006   (interactive "P")
10007   (gnus-summary-next-group no-article nil t))
10008
10009 ;; Walking around summary lines.
10010
10011 (defun gnus-summary-first-subject (&optional unread)
10012   "Go to the first unread subject.
10013 If UNREAD is non-nil, go to the first unread article.
10014 Returns the article selected or nil if there are no unread articles."
10015   (interactive "P")
10016   (prog1
10017       (cond
10018        ;; Empty summary.
10019        ((null gnus-newsgroup-data)
10020         (gnus-message 3 "No articles in the group")
10021         nil)
10022        ;; Pick the first article.
10023        ((not unread)
10024         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10025         (gnus-data-number (car gnus-newsgroup-data)))
10026        ;; No unread articles.
10027        ((null gnus-newsgroup-unreads)
10028         (gnus-message 3 "No more unread articles")
10029         nil)
10030        ;; Find the first unread article.
10031        (t
10032         (let ((data gnus-newsgroup-data))
10033           (while (and data
10034                       (not (gnus-data-unread-p (car data))))
10035             (setq data (cdr data)))
10036           (if data
10037               (progn
10038                 (goto-char (gnus-data-pos (car data)))
10039                 (gnus-data-number (car data)))))))
10040     (gnus-summary-position-point)))
10041
10042 (defun gnus-summary-next-subject (n &optional unread dont-display)
10043   "Go to next N'th summary line.
10044 If N is negative, go to the previous N'th subject line.
10045 If UNREAD is non-nil, only unread articles are selected.
10046 The difference between N and the actual number of steps taken is
10047 returned."
10048   (interactive "p")
10049   (let ((backward (< n 0))
10050         (n (abs n)))
10051     (while (and (> n 0)
10052                 (if backward
10053                     (gnus-summary-find-prev unread)
10054                   (gnus-summary-find-next unread)))
10055       (setq n (1- n)))
10056     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10057                                (if unread " unread" "")))
10058     (unless dont-display
10059       (gnus-summary-recenter)
10060       (gnus-summary-position-point))
10061     n))
10062
10063 (defun gnus-summary-next-unread-subject (n)
10064   "Go to next N'th unread summary line."
10065   (interactive "p")
10066   (gnus-summary-next-subject n t))
10067
10068 (defun gnus-summary-prev-subject (n &optional unread)
10069   "Go to previous N'th summary line.
10070 If optional argument UNREAD is non-nil, only unread article is selected."
10071   (interactive "p")
10072   (gnus-summary-next-subject (- n) unread))
10073
10074 (defun gnus-summary-prev-unread-subject (n)
10075   "Go to previous N'th unread summary line."
10076   (interactive "p")
10077   (gnus-summary-next-subject (- n) t))
10078
10079 (defun gnus-summary-goto-subject (article &optional force silent)
10080   "Go the subject line of ARTICLE.
10081 If FORCE, also allow jumping to articles not currently shown."
10082   (let ((b (point))
10083         (data (gnus-data-find article)))
10084     ;; We read in the article if we have to.
10085     (and (not data)
10086          force
10087          (gnus-summary-insert-subject article)
10088          (setq data (gnus-data-find article)))
10089     (goto-char b)
10090     (if (not data)
10091         (progn
10092           (unless silent
10093             (gnus-message 3 "Can't find article %d" article))
10094           nil)
10095       (goto-char (gnus-data-pos data))
10096       article)))
10097
10098 ;; Walking around summary lines with displaying articles.
10099
10100 (defun gnus-summary-expand-window (&optional arg)
10101   "Make the summary buffer take up the entire Emacs frame.
10102 Given a prefix, will force an `article' buffer configuration."
10103   (interactive "P")
10104   (gnus-set-global-variables)
10105   (if arg
10106       (gnus-configure-windows 'article 'force)
10107     (gnus-configure-windows 'summary 'force)))
10108
10109 (defun gnus-summary-display-article (article &optional all-header)
10110   "Display ARTICLE in article buffer."
10111   (gnus-set-global-variables)
10112   (if (null article)
10113       nil
10114     (prog1
10115         (if gnus-summary-display-article-function
10116             (funcall gnus-summary-display-article-function article all-header)
10117           (gnus-article-prepare article all-header))
10118       (run-hooks 'gnus-select-article-hook)
10119       (gnus-summary-recenter)
10120       (gnus-summary-goto-subject article)
10121       (when gnus-use-trees
10122         (gnus-possibly-generate-tree article)
10123         (gnus-highlight-selected-tree article))
10124       ;; Successfully display article.
10125       (gnus-article-set-window-start
10126        (cdr (assq article gnus-newsgroup-bookmarks)))
10127       t)))
10128
10129 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10130   "Select the current article.
10131 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10132 non-nil, the article will be re-fetched even if it already present in
10133 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10134 be displayed."
10135   (let ((article (or article (gnus-summary-article-number)))
10136         (all-headers (not (not all-headers))) ;Must be T or NIL.
10137         gnus-summary-display-article-function
10138         did)
10139     (and (not pseudo)
10140          (gnus-summary-article-pseudo-p article)
10141          (error "This is a pseudo-article."))
10142     (prog1
10143         (save-excursion
10144           (set-buffer gnus-summary-buffer)
10145           (if (or (and gnus-single-article-buffer
10146                        (or (null gnus-current-article)
10147                            (null gnus-article-current)
10148                            (null (get-buffer gnus-article-buffer))
10149                            (not (eq article (cdr gnus-article-current)))
10150                            (not (equal (car gnus-article-current)
10151                                        gnus-newsgroup-name))))
10152                   (and (not gnus-single-article-buffer)
10153                        (or (null gnus-current-article)
10154                            (not (eq gnus-current-article article))))
10155                   force)
10156               ;; The requested article is different from the current article.
10157               (prog1
10158                   (gnus-summary-display-article article all-headers)
10159                 (setq did article))
10160             (if (or all-headers gnus-show-all-headers)
10161                 (gnus-article-show-all-headers))
10162             'old))
10163       (if did
10164           (gnus-article-set-window-start
10165            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10166
10167 (defun gnus-summary-set-current-mark (&optional current-mark)
10168   "Obsolete function."
10169   nil)
10170
10171 (defun gnus-summary-next-article (&optional unread subject backward push)
10172   "Select the next article.
10173 If UNREAD, only unread articles are selected.
10174 If SUBJECT, only articles with SUBJECT are selected.
10175 If BACKWARD, the previous article is selected instead of the next."
10176   (interactive "P")
10177   (gnus-set-global-variables)
10178   (cond
10179    ;; Is there such an article?
10180    ((and (gnus-summary-search-forward unread subject backward)
10181          (or (gnus-summary-display-article (gnus-summary-article-number))
10182              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10183     (gnus-summary-position-point))
10184    ;; If not, we try the first unread, if that is wanted.
10185    ((and subject
10186          gnus-auto-select-same
10187          (or (gnus-summary-first-unread-article)
10188              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10189     (gnus-summary-position-point)
10190     (gnus-message 6 "Wrapped"))
10191    ;; Try to get next/previous article not displayed in this group.
10192    ((and gnus-auto-extend-newsgroup
10193          (not unread) (not subject))
10194     (gnus-summary-goto-article
10195      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10196      nil t))
10197    ;; Go to next/previous group.
10198    (t
10199     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10200         (gnus-summary-jump-to-group gnus-newsgroup-name))
10201     (let ((cmd last-command-char)
10202           (group
10203            (if (eq gnus-keep-same-level 'best)
10204                (gnus-summary-best-group gnus-newsgroup-name)
10205              (gnus-summary-search-group backward gnus-keep-same-level))))
10206       ;; For some reason, the group window gets selected.  We change
10207       ;; it back.
10208       (select-window (get-buffer-window (current-buffer)))
10209       ;; Select next unread newsgroup automagically.
10210       (cond
10211        ((not gnus-auto-select-next)
10212         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10213        ((or (eq gnus-auto-select-next 'quietly)
10214             (and (eq gnus-auto-select-next 'slightly-quietly)
10215                  push)
10216             (and (eq gnus-auto-select-next 'almost-quietly)
10217                  (gnus-summary-last-article-p)))
10218         ;; Select quietly.
10219         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10220             (gnus-summary-exit)
10221           (gnus-message 7 "No more%s articles (%s)..."
10222                         (if unread " unread" "")
10223                         (if group (concat "selecting " group)
10224                           "exiting"))
10225           (gnus-summary-next-group nil group backward)))
10226        (t
10227         (gnus-summary-walk-group-buffer
10228          gnus-newsgroup-name cmd unread backward)))))))
10229
10230 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10231   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10232                       (?\C-p (gnus-group-prev-unread-group 1))))
10233         keve key group ended)
10234     (save-excursion
10235       (set-buffer gnus-group-buffer)
10236       (gnus-summary-jump-to-group from-group)
10237       (setq group
10238             (if (eq gnus-keep-same-level 'best)
10239                 (gnus-summary-best-group gnus-newsgroup-name)
10240               (gnus-summary-search-group backward gnus-keep-same-level))))
10241     (while (not ended)
10242       (gnus-message
10243        5 "No more%s articles%s" (if unread " unread" "")
10244        (if (and group
10245                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10246            (format " (Type %s for %s [%s])"
10247                    (single-key-description cmd) group
10248                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10249          (format " (Type %s to exit %s)"
10250                  (single-key-description cmd)
10251                  gnus-newsgroup-name)))
10252       ;; Confirm auto selection.
10253       (setq key (car (setq keve (gnus-read-event-char))))
10254       (setq ended t)
10255       (cond
10256        ((assq key keystrokes)
10257         (let ((obuf (current-buffer)))
10258           (switch-to-buffer gnus-group-buffer)
10259           (and group
10260                (gnus-group-jump-to-group group))
10261           (eval (cadr (assq key keystrokes)))
10262           (setq group (gnus-group-group-name))
10263           (switch-to-buffer obuf))
10264         (setq ended nil))
10265        ((equal key cmd)
10266         (if (or (not group)
10267                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10268             (gnus-summary-exit)
10269           (gnus-summary-next-group nil group backward)))
10270        (t
10271         (push (cdr keve) unread-command-events))))))
10272
10273 (defun gnus-read-event-char ()
10274   "Get the next event."
10275   (let ((event (read-event)))
10276     (cons (and (numberp event) event) event)))
10277
10278 (defun gnus-summary-next-unread-article ()
10279   "Select unread article after current one."
10280   (interactive)
10281   (gnus-summary-next-article t (and gnus-auto-select-same
10282                                     (gnus-summary-article-subject))))
10283
10284 (defun gnus-summary-prev-article (&optional unread subject)
10285   "Select the article after the current one.
10286 If UNREAD is non-nil, only unread articles are selected."
10287   (interactive "P")
10288   (gnus-summary-next-article unread subject t))
10289
10290 (defun gnus-summary-prev-unread-article ()
10291   "Select unred article before current one."
10292   (interactive)
10293   (gnus-summary-prev-article t (and gnus-auto-select-same
10294                                     (gnus-summary-article-subject))))
10295
10296 (defun gnus-summary-next-page (&optional lines circular)
10297   "Show next page of the selected article.
10298 If at the end of the current article, select the next article.
10299 LINES says how many lines should be scrolled up.
10300
10301 If CIRCULAR is non-nil, go to the start of the article instead of
10302 selecting the next article when reaching the end of the current
10303 article."
10304   (interactive "P")
10305   (setq gnus-summary-buffer (current-buffer))
10306   (gnus-set-global-variables)
10307   (let ((article (gnus-summary-article-number))
10308         (endp nil))
10309     (gnus-configure-windows 'article)
10310     (if (or (null gnus-current-article)
10311             (null gnus-article-current)
10312             (/= article (cdr gnus-article-current))
10313             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10314         ;; Selected subject is different from current article's.
10315         (gnus-summary-display-article article)
10316       (gnus-eval-in-buffer-window
10317        gnus-article-buffer
10318        (setq endp (gnus-article-next-page lines)))
10319       (if endp
10320           (cond (circular
10321                  (gnus-summary-beginning-of-article))
10322                 (lines
10323                  (gnus-message 3 "End of message"))
10324                 ((null lines)
10325                  (if (and (eq gnus-summary-goto-unread 'never)
10326                           (not (gnus-summary-last-article-p article)))
10327                      (gnus-summary-next-article)
10328                    (gnus-summary-next-unread-article))))))
10329     (gnus-summary-recenter)
10330     (gnus-summary-position-point)))
10331
10332 (defun gnus-summary-prev-page (&optional lines)
10333   "Show previous page of selected article.
10334 Argument LINES specifies lines to be scrolled down."
10335   (interactive "P")
10336   (gnus-set-global-variables)
10337   (let ((article (gnus-summary-article-number)))
10338     (gnus-configure-windows 'article)
10339     (if (or (null gnus-current-article)
10340             (null gnus-article-current)
10341             (/= article (cdr gnus-article-current))
10342             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10343         ;; Selected subject is different from current article's.
10344         (gnus-summary-display-article article)
10345       (gnus-summary-recenter)
10346       (gnus-eval-in-buffer-window gnus-article-buffer
10347                                   (gnus-article-prev-page lines))))
10348   (gnus-summary-position-point))
10349
10350 (defun gnus-summary-scroll-up (lines)
10351   "Scroll up (or down) one line current article.
10352 Argument LINES specifies lines to be scrolled up (or down if negative)."
10353   (interactive "p")
10354   (gnus-set-global-variables)
10355   (gnus-configure-windows 'article)
10356   (gnus-summary-show-thread)
10357   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10358     (gnus-eval-in-buffer-window
10359      gnus-article-buffer
10360      (cond ((> lines 0)
10361             (if (gnus-article-next-page lines)
10362                 (gnus-message 3 "End of message")))
10363            ((< lines 0)
10364             (gnus-article-prev-page (- lines))))))
10365   (gnus-summary-recenter)
10366   (gnus-summary-position-point))
10367
10368 (defun gnus-summary-next-same-subject ()
10369   "Select next article which has the same subject as current one."
10370   (interactive)
10371   (gnus-set-global-variables)
10372   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10373
10374 (defun gnus-summary-prev-same-subject ()
10375   "Select previous article which has the same subject as current one."
10376   (interactive)
10377   (gnus-set-global-variables)
10378   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10379
10380 (defun gnus-summary-next-unread-same-subject ()
10381   "Select next unread article which has the same subject as current one."
10382   (interactive)
10383   (gnus-set-global-variables)
10384   (gnus-summary-next-article t (gnus-summary-article-subject)))
10385
10386 (defun gnus-summary-prev-unread-same-subject ()
10387   "Select previous unread article which has the same subject as current one."
10388   (interactive)
10389   (gnus-set-global-variables)
10390   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10391
10392 (defun gnus-summary-first-unread-article ()
10393   "Select the first unread article.
10394 Return nil if there are no unread articles."
10395   (interactive)
10396   (gnus-set-global-variables)
10397   (prog1
10398       (if (gnus-summary-first-subject t)
10399           (progn
10400             (gnus-summary-show-thread)
10401             (gnus-summary-first-subject t)
10402             (gnus-summary-display-article (gnus-summary-article-number))))
10403     (gnus-summary-position-point)))
10404
10405 (defun gnus-summary-best-unread-article ()
10406   "Select the unread article with the highest score."
10407   (interactive)
10408   (gnus-set-global-variables)
10409   (let ((best -1000000)
10410         (data gnus-newsgroup-data)
10411         article score)
10412     (while data
10413       (and (gnus-data-unread-p (car data))
10414            (> (setq score
10415                     (gnus-summary-article-score (gnus-data-number (car data))))
10416               best)
10417            (setq best score
10418                  article (gnus-data-number (car data))))
10419       (setq data (cdr data)))
10420     (prog1
10421         (if article
10422             (gnus-summary-goto-article article)
10423           (error "No unread articles"))
10424       (gnus-summary-position-point))))
10425
10426 (defun gnus-summary-last-subject ()
10427   "Go to the last displayed subject line in the group."
10428   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10429     (when article
10430       (gnus-summary-goto-subject article))))
10431
10432 (defun gnus-summary-goto-article (article &optional all-headers force)
10433   "Fetch ARTICLE and display it if it exists.
10434 If ALL-HEADERS is non-nil, no header lines are hidden."
10435   (interactive
10436    (list
10437     (string-to-int
10438      (completing-read
10439       "Article number: "
10440       (mapcar (lambda (number) (list (int-to-string number)))
10441               gnus-newsgroup-limit)))
10442     current-prefix-arg
10443     t))
10444   (prog1
10445       (if (gnus-summary-goto-subject article force)
10446           (gnus-summary-display-article article all-headers)
10447         (gnus-message 4 "Couldn't go to article %s" article) nil)
10448     (gnus-summary-position-point)))
10449
10450 (defun gnus-summary-goto-last-article ()
10451   "Go to the previously read article."
10452   (interactive)
10453   (prog1
10454       (and gnus-last-article
10455            (gnus-summary-goto-article gnus-last-article))
10456     (gnus-summary-position-point)))
10457
10458 (defun gnus-summary-pop-article (number)
10459   "Pop one article off the history and go to the previous.
10460 NUMBER articles will be popped off."
10461   (interactive "p")
10462   (let (to)
10463     (setq gnus-newsgroup-history
10464           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10465     (if to
10466         (gnus-summary-goto-article (car to))
10467       (error "Article history empty")))
10468   (gnus-summary-position-point))
10469
10470 ;; Summary commands and functions for limiting the summary buffer.
10471
10472 (defun gnus-summary-limit-to-articles (n)
10473   "Limit the summary buffer to the next N articles.
10474 If not given a prefix, use the process marked articles instead."
10475   (interactive "P")
10476   (gnus-set-global-variables)
10477   (prog1
10478       (let ((articles (gnus-summary-work-articles n)))
10479         (setq gnus-newsgroup-processable nil)
10480         (gnus-summary-limit articles))
10481     (gnus-summary-position-point)))
10482
10483 (defun gnus-summary-pop-limit (&optional total)
10484   "Restore the previous limit.
10485 If given a prefix, remove all limits."
10486   (interactive "P")
10487   (gnus-set-global-variables)
10488   (when total 
10489     (setq gnus-newsgroup-limits
10490           (list (mapcar (lambda (h) (mail-header-number h))
10491                         gnus-newsgroup-headers))))
10492   (unless gnus-newsgroup-limits
10493     (error "No limit to pop"))
10494   (prog1
10495       (gnus-summary-limit nil 'pop)
10496     (gnus-summary-position-point)))
10497
10498 (defun gnus-summary-limit-to-subject (subject &optional header)
10499   "Limit the summary buffer to articles that have subjects that match a regexp."
10500   (interactive "sRegexp: ")
10501   (unless header
10502     (setq header "subject"))
10503   (when (not (equal "" subject))
10504     (prog1
10505         (let ((articles (gnus-summary-find-matching
10506                          (or header "subject") subject 'all)))
10507           (or articles (error "Found no matches for \"%s\"" subject))
10508           (gnus-summary-limit articles))
10509       (gnus-summary-position-point))))
10510
10511 (defun gnus-summary-limit-to-author (from)
10512   "Limit the summary buffer to articles that have authors that match a regexp."
10513   (interactive "sRegexp: ")
10514   (gnus-summary-limit-to-subject from "from"))
10515
10516 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10517 (make-obsolete
10518  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10519
10520 (defun gnus-summary-limit-to-unread (&optional all)
10521   "Limit the summary buffer to articles that are not marked as read.
10522 If ALL is non-nil, limit strictly to unread articles."
10523   (interactive "P")
10524   (if all
10525       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10526     (gnus-summary-limit-to-marks
10527      ;; Concat all the marks that say that an article is read and have
10528      ;; those removed.
10529      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10530            gnus-killed-mark gnus-kill-file-mark
10531            gnus-low-score-mark gnus-expirable-mark
10532            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10533      'reverse)))
10534
10535 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10536 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10537
10538 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10539   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10540 If REVERSE, limit the summary buffer to articles that are not marked
10541 with MARKS.  MARKS can either be a string of marks or a list of marks.
10542 Returns how many articles were removed."
10543   (interactive "sMarks: ")
10544   (gnus-set-global-variables)
10545   (prog1
10546       (let ((data gnus-newsgroup-data)
10547             (marks (if (listp marks) marks
10548                      (append marks nil))) ; Transform to list.
10549             articles)
10550         (while data
10551           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10552                  (memq (gnus-data-mark (car data)) marks))
10553                (setq articles (cons (gnus-data-number (car data)) articles)))
10554           (setq data (cdr data)))
10555         (gnus-summary-limit articles))
10556     (gnus-summary-position-point)))
10557
10558 (defun gnus-summary-limit-to-score (&optional score)
10559   "Limit to articles with score at or above SCORE."
10560   (interactive "P")
10561   (gnus-set-global-variables)
10562   (setq score (if score
10563                   (prefix-numeric-value score)
10564                 (or gnus-summary-default-score 0)))
10565   (let ((data gnus-newsgroup-data)
10566         articles)
10567     (while data
10568       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10569                 score)
10570         (push (gnus-data-number (car data)) articles))
10571       (setq data (cdr data)))
10572     (prog1
10573         (gnus-summary-limit articles)
10574       (gnus-summary-position-point))))
10575
10576 (defun gnus-summary-limit-include-dormant ()
10577   "Display all the hidden articles that are marked as dormant."
10578   (interactive)
10579   (gnus-set-global-variables)
10580   (or gnus-newsgroup-dormant
10581       (error "There are no dormant articles in this group"))
10582   (prog1
10583       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10584     (gnus-summary-position-point)))
10585
10586 (defun gnus-summary-limit-exclude-dormant ()
10587   "Hide all dormant articles."
10588   (interactive)
10589   (gnus-set-global-variables)
10590   (prog1
10591       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10592     (gnus-summary-position-point)))
10593
10594 (defun gnus-summary-limit-exclude-childless-dormant ()
10595   "Hide all dormant articles that have no children."
10596   (interactive)
10597   (gnus-set-global-variables)
10598   (let ((data (gnus-data-list t))
10599         articles d children)
10600     ;; Find all articles that are either not dormant or have
10601     ;; children.
10602     (while (setq d (pop data))
10603       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10604                 (and (setq children 
10605                            (gnus-article-children (gnus-data-number d)))
10606                      (let (found)
10607                        (while children
10608                          (when (memq (car children) articles)
10609                            (setq children nil
10610                                  found t))
10611                          (pop children))
10612                        found)))
10613         (push (gnus-data-number d) articles)))
10614     ;; Do the limiting.
10615     (prog1
10616         (gnus-summary-limit articles)
10617       (gnus-summary-position-point))))
10618
10619 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10620   "Mark all unread excluded articles as read.
10621 If ALL, mark even excluded ticked and dormants as read."
10622   (interactive "P")
10623   (let ((articles (gnus-sorted-complement
10624                    (sort
10625                     (mapcar (lambda (h) (mail-header-number h))
10626                             gnus-newsgroup-headers)
10627                     '<)
10628                    (sort gnus-newsgroup-limit '<)))
10629         article)
10630     (setq gnus-newsgroup-unreads nil)
10631     (if all
10632         (setq gnus-newsgroup-dormant nil
10633               gnus-newsgroup-marked nil
10634               gnus-newsgroup-reads
10635               (nconc
10636                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10637                gnus-newsgroup-reads))
10638       (while (setq article (pop articles))
10639         (unless (or (memq article gnus-newsgroup-dormant)
10640                     (memq article gnus-newsgroup-marked))
10641           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10642
10643 (defun gnus-summary-limit (articles &optional pop)
10644   (if pop
10645       ;; We pop the previous limit off the stack and use that.
10646       (setq articles (car gnus-newsgroup-limits)
10647             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10648     ;; We use the new limit, so we push the old limit on the stack.
10649     (setq gnus-newsgroup-limits
10650           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10651   ;; Set the limit.
10652   (setq gnus-newsgroup-limit articles)
10653   (let ((total (length gnus-newsgroup-data))
10654         (data (gnus-data-find-list (gnus-summary-article-number)))
10655         found)
10656     ;; This will do all the work of generating the new summary buffer
10657     ;; according to the new limit.
10658     (gnus-summary-prepare)
10659     ;; Hide any threads, possibly.
10660     (and gnus-show-threads
10661          gnus-thread-hide-subtree
10662          (gnus-summary-hide-all-threads))
10663     ;; Try to return to the article you were at, or one in the
10664     ;; neighborhood.
10665     (if data
10666         ;; We try to find some article after the current one.
10667         (while data
10668           (and (gnus-summary-goto-subject
10669                 (gnus-data-number (car data)) nil t)
10670                (setq data nil
10671                      found t))
10672           (setq data (cdr data))))
10673     (or found
10674         ;; If there is no data, that means that we were after the last
10675         ;; article.  The same goes when we can't find any articles
10676         ;; after the current one.
10677         (progn
10678           (goto-char (point-max))
10679           (gnus-summary-find-prev)))
10680     ;; We return how many articles were removed from the summary
10681     ;; buffer as a result of the new limit.
10682     (- total (length gnus-newsgroup-data))))
10683
10684 (defsubst gnus-cut-thread (thread)
10685   "Go forwards in the thread until we find an article that we want to display."
10686   (when (eq gnus-fetch-old-headers 'some)
10687     ;; Deal with old-fetched headers.
10688     (while (and thread
10689                 (memq (mail-header-number (car thread)) 
10690                       gnus-newsgroup-ancient)
10691                 (<= (length (cdr thread)) 1))
10692       (setq thread (cadr thread))))
10693   ;; Deal with sparse threads.
10694   (when (or (eq gnus-build-sparse-threads 'some)
10695             (eq gnus-build-sparse-threads 'more))
10696     (while (and thread
10697                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10698                 (= (length (cdr thread)) 1))
10699       (setq thread (cadr thread))))
10700   thread)
10701
10702 (defun gnus-cut-threads (threads)
10703   "Cut off all uninteresting articles from the beginning of threads."
10704   (when (or (eq gnus-fetch-old-headers 'some)
10705             (eq gnus-build-sparse-threads 'some)
10706             (eq gnus-build-sparse-threads 'more))
10707     (let ((th threads))
10708       (while th
10709         (setcar th (gnus-cut-thread (car th)))
10710         (setq th (cdr th)))))
10711   ;; Remove nixed out threads.
10712   (delq nil threads))
10713
10714 (defun gnus-summary-initial-limit (&optional show-if-empty)
10715   "Figure out what the initial limit is supposed to be on group entry.
10716 This entails weeding out unwanted dormants, low-scored articles,
10717 fetch-old-headers verbiage, and so on."
10718   ;; Most groups have nothing to remove.
10719   (if (or gnus-inhibit-limiting
10720           (and (null gnus-newsgroup-dormant)
10721                (not (eq gnus-fetch-old-headers 'some))
10722                (null gnus-summary-expunge-below)
10723                (not (eq gnus-build-sparse-threads 'some))
10724                (not (eq gnus-build-sparse-threads 'more))
10725                (null gnus-thread-expunge-below)
10726                (not gnus-use-nocem)))
10727       () ; Do nothing.
10728     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10729     (setq gnus-newsgroup-limit nil)
10730     (mapatoms
10731      (lambda (node)
10732        (unless (car (symbol-value node))
10733          ;; These threads have no parents -- they are roots.
10734          (let ((nodes (cdr (symbol-value node)))
10735                thread)
10736            (while nodes
10737              (if (and gnus-thread-expunge-below
10738                       (< (gnus-thread-total-score (car nodes))
10739                          gnus-thread-expunge-below))
10740                  (gnus-expunge-thread (pop nodes))
10741                (setq thread (pop nodes))
10742                (gnus-summary-limit-children thread))))))
10743      gnus-newsgroup-dependencies)
10744     ;; If this limitation resulted in an empty group, we might
10745     ;; pop the previous limit and use it instead.
10746     (when (and (not gnus-newsgroup-limit)
10747                show-if-empty)
10748       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10749     gnus-newsgroup-limit))
10750
10751 (defun gnus-summary-limit-children (thread)
10752   "Return 1 if this subthread is visible and 0 if it is not."
10753   ;; First we get the number of visible children to this thread.  This
10754   ;; is done by recursing down the thread using this function, so this
10755   ;; will really go down to a leaf article first, before slowly
10756   ;; working its way up towards the root.
10757   (when thread
10758     (let ((children
10759            (if (cdr thread)
10760                (apply '+ (mapcar 'gnus-summary-limit-children
10761                                  (cdr thread)))
10762              0))
10763           (number (mail-header-number (car thread)))
10764           score)
10765       (if (or
10766            ;; If this article is dormant and has absolutely no visible
10767            ;; children, then this article isn't visible.
10768            (and (memq number gnus-newsgroup-dormant)
10769                 (= children 0))
10770            ;; If this is a "fetch-old-headered" and there is only one
10771            ;; visible child (or less), then we don't want this article.
10772            (and (eq gnus-fetch-old-headers 'some)
10773                 (memq number gnus-newsgroup-ancient)
10774                 (zerop children))
10775            ;; If this is a sparsely inserted article with no children,
10776            ;; we don't want it.
10777            (and (eq gnus-build-sparse-threads 'some)
10778                 (memq number gnus-newsgroup-sparse)
10779                 (zerop children))
10780            ;; If we use expunging, and this article is really
10781            ;; low-scored, then we don't want this article.
10782            (when (and gnus-summary-expunge-below
10783                       (< (setq score
10784                                (or (cdr (assq number gnus-newsgroup-scored))
10785                                    gnus-summary-default-score))
10786                          gnus-summary-expunge-below))
10787              ;; We increase the expunge-tally here, but that has
10788              ;; nothing to do with the limits, really.
10789              (incf gnus-newsgroup-expunged-tally)
10790              ;; We also mark as read here, if that's wanted.
10791              (when (and gnus-summary-mark-below
10792                         (< score gnus-summary-mark-below))
10793                (setq gnus-newsgroup-unreads
10794                      (delq number gnus-newsgroup-unreads))
10795                (if gnus-newsgroup-auto-expire
10796                    (push number gnus-newsgroup-expirable)
10797                  (push (cons number gnus-low-score-mark)
10798                        gnus-newsgroup-reads)))
10799              t)
10800            (and gnus-use-nocem
10801                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10802           ;; Nope, invisible article.
10803           0
10804         ;; Ok, this article is to be visible, so we add it to the limit
10805         ;; and return 1.
10806         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10807         1))))
10808
10809 (defun gnus-expunge-thread (thread)
10810   "Mark all articles in THREAD as read."
10811   (let* ((number (mail-header-number (car thread))))
10812     (incf gnus-newsgroup-expunged-tally)
10813     ;; We also mark as read here, if that's wanted.
10814     (setq gnus-newsgroup-unreads
10815           (delq number gnus-newsgroup-unreads))
10816     (if gnus-newsgroup-auto-expire
10817         (push number gnus-newsgroup-expirable)
10818       (push (cons number gnus-low-score-mark)
10819             gnus-newsgroup-reads)))
10820   ;; Go recursively through all subthreads.
10821   (mapcar 'gnus-expunge-thread (cdr thread)))
10822
10823 ;; Summary article oriented commands
10824
10825 (defun gnus-summary-refer-parent-article (n)
10826   "Refer parent article N times.
10827 The difference between N and the number of articles fetched is returned."
10828   (interactive "p")
10829   (gnus-set-global-variables)
10830   (while
10831       (and
10832        (> n 0)
10833        (let* ((header (gnus-summary-article-header))
10834               (ref
10835                ;; If we try to find the parent of the currently
10836                ;; displayed article, then we take a look at the actual
10837                ;; References header, since this is slightly more
10838                ;; reliable than the References field we got from the
10839                ;; server.
10840                (if (and (eq (mail-header-number header)
10841                             (cdr gnus-article-current))
10842                         (equal gnus-newsgroup-name
10843                                (car gnus-article-current)))
10844                    (save-excursion
10845                      (set-buffer gnus-original-article-buffer)
10846                      (nnheader-narrow-to-headers)
10847                      (prog1
10848                          (mail-fetch-field "references")
10849                        (widen)))
10850                  ;; It's not the current article, so we take a bet on
10851                  ;; the value we got from the server.
10852                  (mail-header-references header))))
10853          (if (setq ref (or ref (mail-header-references header)))
10854              (or (gnus-summary-refer-article (gnus-parent-id ref))
10855                  (gnus-message 1 "Couldn't find parent"))
10856            (gnus-message 1 "No references in article %d"
10857                          (gnus-summary-article-number))
10858            nil)))
10859     (setq n (1- n)))
10860   (gnus-summary-position-point)
10861   n)
10862
10863 (defun gnus-summary-refer-references ()
10864   "Fetch all articles mentioned in the References header.
10865 Return how many articles were fetched."
10866   (interactive)
10867   (gnus-set-global-variables)
10868   (let ((ref (mail-header-references (gnus-summary-article-header)))
10869         (current (gnus-summary-article-number))
10870         (n 0))
10871     ;; For each Message-ID in the References header...
10872     (while (string-match "<[^>]*>" ref)
10873       (incf n)
10874       ;; ... fetch that article.
10875       (gnus-summary-refer-article
10876        (prog1 (match-string 0 ref)
10877          (setq ref (substring ref (match-end 0))))))
10878     (gnus-summary-goto-subject current)
10879     (gnus-summary-position-point)
10880     n))
10881
10882 (defun gnus-summary-refer-article (message-id)
10883   "Fetch an article specified by MESSAGE-ID."
10884   (interactive "sMessage-ID: ")
10885   (when (and (stringp message-id)
10886              (not (zerop (length message-id))))
10887     ;; Construct the correct Message-ID if necessary.
10888     ;; Suggested by tale@pawl.rpi.edu.
10889     (unless (string-match "^<" message-id)
10890       (setq message-id (concat "<" message-id)))
10891     (unless (string-match ">$" message-id)
10892       (setq message-id (concat message-id ">")))
10893     (let ((header (car (gnus-gethash message-id
10894                                      gnus-newsgroup-dependencies))))
10895       (if header
10896           ;; The article is present in the buffer, to we just go to it.
10897           (gnus-summary-goto-article (mail-header-number header) nil t)
10898         ;; We fetch the article
10899         (let ((gnus-override-method 
10900                (and (gnus-news-group-p gnus-newsgroup-name)
10901                     gnus-refer-article-method))
10902               number)
10903           ;; Start the special refer-article method, if necessary.
10904           (when gnus-refer-article-method
10905             (gnus-check-server gnus-refer-article-method))
10906           ;; Fetch the header, and display the article.
10907           (if (setq number (gnus-summary-insert-subject message-id))
10908               (gnus-summary-select-article nil nil nil number)
10909             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10910
10911 (defun gnus-summary-enter-digest-group (&optional force)
10912   "Enter a digest group based on the current article."
10913   (interactive "P")
10914   (gnus-set-global-variables)
10915   (gnus-summary-select-article)
10916   (let ((name (format "%s-%d"
10917                       (gnus-group-prefixed-name
10918                        gnus-newsgroup-name (list 'nndoc ""))
10919                       gnus-current-article))
10920         (ogroup gnus-newsgroup-name)
10921         (case-fold-search t)
10922         (buf (current-buffer))
10923         dig)
10924     (save-excursion
10925       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10926       (insert-buffer-substring gnus-original-article-buffer)
10927       (narrow-to-region
10928        (goto-char (point-min))
10929        (or (search-forward "\n\n" nil t) (point)))
10930       (goto-char (point-min))
10931       (delete-matching-lines "^\\(Path\\):\\|^From ")
10932       (widen))
10933     (unwind-protect
10934         (if (gnus-group-read-ephemeral-group
10935              name `(nndoc ,name (nndoc-address
10936                                  ,(get-buffer dig))
10937                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10938             ;; Make all postings to this group go to the parent group.
10939             (nconc (gnus-info-params (gnus-get-info name))
10940                    (list (cons 'to-group ogroup)))
10941           ;; Couldn't select this doc group.
10942           (switch-to-buffer buf)
10943           (gnus-set-global-variables)
10944           (gnus-configure-windows 'summary)
10945           (gnus-message 3 "Article couldn't be entered?"))
10946       (kill-buffer dig))))
10947
10948 (defun gnus-summary-isearch-article (&optional regexp-p)
10949   "Do incremental search forward on the current article.
10950 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10951   (interactive "P")
10952   (gnus-set-global-variables)
10953   (gnus-summary-select-article)
10954   (gnus-configure-windows 'article)
10955   (gnus-eval-in-buffer-window
10956    gnus-article-buffer
10957    (goto-char (point-min))
10958    (isearch-forward regexp-p)))
10959
10960 (defun gnus-summary-search-article-forward (regexp &optional backward)
10961   "Search for an article containing REGEXP forward.
10962 If BACKWARD, search backward instead."
10963   (interactive
10964    (list (read-string
10965           (format "Search article %s (regexp%s): "
10966                   (if current-prefix-arg "backward" "forward")
10967                   (if gnus-last-search-regexp
10968                       (concat ", default " gnus-last-search-regexp)
10969                     "")))
10970          current-prefix-arg))
10971   (gnus-set-global-variables)
10972   (if (string-equal regexp "")
10973       (setq regexp (or gnus-last-search-regexp ""))
10974     (setq gnus-last-search-regexp regexp))
10975   (if (gnus-summary-search-article regexp backward)
10976       (gnus-article-set-window-start
10977        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10978     (error "Search failed: \"%s\"" regexp)))
10979
10980 (defun gnus-summary-search-article-backward (regexp)
10981   "Search for an article containing REGEXP backward."
10982   (interactive
10983    (list (read-string
10984           (format "Search article backward (regexp%s): "
10985                   (if gnus-last-search-regexp
10986                       (concat ", default " gnus-last-search-regexp)
10987                     "")))))
10988   (gnus-summary-search-article-forward regexp 'backward))
10989
10990 (defun gnus-summary-search-article (regexp &optional backward)
10991   "Search for an article containing REGEXP.
10992 Optional argument BACKWARD means do search for backward.
10993 gnus-select-article-hook is not called during the search."
10994   (let ((gnus-select-article-hook nil)  ;Disable hook.
10995         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10996         (re-search
10997          (if backward
10998              (function re-search-backward) (function re-search-forward)))
10999         (found nil)
11000         (last nil))
11001     ;; Hidden thread subtrees must be searched for ,too.
11002     (gnus-summary-show-all-threads)
11003     ;; First of all, search current article.
11004     ;; We don't want to read article again from NNTP server nor reset
11005     ;; current point.
11006     (gnus-summary-select-article)
11007     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11008     (setq last gnus-current-article)
11009     (gnus-eval-in-buffer-window
11010      gnus-article-buffer
11011      (save-restriction
11012        (widen)
11013        ;; Begin search from current point.
11014        (setq found (funcall re-search regexp nil t))))
11015     ;; Then search next articles.
11016     (while (and (not found)
11017                 (gnus-summary-display-article
11018                  (if backward (gnus-summary-find-prev)
11019                    (gnus-summary-find-next))))
11020       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11021       (gnus-eval-in-buffer-window
11022        gnus-article-buffer
11023        (save-restriction
11024          (widen)
11025          (goto-char (if backward (point-max) (point-min)))
11026          (setq found (funcall re-search regexp nil t)))))
11027     (message "")
11028     ;; Adjust article pointer.
11029     (or (eq last gnus-current-article)
11030         (setq gnus-last-article last))
11031     ;; Return T if found such article.
11032     found))
11033
11034 (defun gnus-summary-find-matching (header regexp &optional backward unread
11035                                           not-case-fold)
11036   "Return a list of all articles that match REGEXP on HEADER.
11037 The search stars on the current article and goes forwards unless
11038 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11039 If UNREAD is non-nil, only unread articles will
11040 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11041 in the comparisons."
11042   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11043                 (gnus-data-find-list
11044                  (gnus-summary-article-number) (gnus-data-list backward))))
11045         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11046         (case-fold-search (not not-case-fold))
11047         articles d)
11048     (or (fboundp (intern (concat "mail-header-" header)))
11049         (error "%s is not a valid header" header))
11050     (while data
11051       (setq d (car data))
11052       (and (or (not unread)             ; We want all articles...
11053                (gnus-data-unread-p d))  ; Or just unreads.
11054            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11055            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11056            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11057       (setq data (cdr data)))
11058     (nreverse articles)))
11059
11060 (defun gnus-summary-execute-command (header regexp command &optional backward)
11061   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11062 If HEADER is an empty string (or nil), the match is done on the entire
11063 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11064   (interactive
11065    (list (let ((completion-ignore-case t))
11066            (completing-read
11067             "Header name: "
11068             (mapcar (lambda (string) (list string))
11069                     '("Number" "Subject" "From" "Lines" "Date"
11070                       "Message-ID" "Xref" "References" "Body"))
11071             nil 'require-match))
11072          (read-string "Regexp: ")
11073          (read-key-sequence "Command: ")
11074          current-prefix-arg))
11075   (when (equal header "Body")
11076     (setq header ""))
11077   (gnus-set-global-variables)
11078   ;; Hidden thread subtrees must be searched as well.
11079   (gnus-summary-show-all-threads)
11080   ;; We don't want to change current point nor window configuration.
11081   (save-excursion
11082     (save-window-excursion
11083       (gnus-message 6 "Executing %s..." (key-description command))
11084       ;; We'd like to execute COMMAND interactively so as to give arguments.
11085       (gnus-execute header regexp
11086                     `(lambda () (call-interactively ',(key-binding command)))
11087                     backward)
11088       (gnus-message 6 "Executing %s...done" (key-description command)))))
11089
11090 (defun gnus-summary-beginning-of-article ()
11091   "Scroll the article back to the beginning."
11092   (interactive)
11093   (gnus-set-global-variables)
11094   (gnus-summary-select-article)
11095   (gnus-configure-windows 'article)
11096   (gnus-eval-in-buffer-window
11097    gnus-article-buffer
11098    (widen)
11099    (goto-char (point-min))
11100    (and gnus-break-pages (gnus-narrow-to-page))))
11101
11102 (defun gnus-summary-end-of-article ()
11103   "Scroll to the end of the article."
11104   (interactive)
11105   (gnus-set-global-variables)
11106   (gnus-summary-select-article)
11107   (gnus-configure-windows 'article)
11108   (gnus-eval-in-buffer-window
11109    gnus-article-buffer
11110    (widen)
11111    (goto-char (point-max))
11112    (recenter -3)
11113    (and gnus-break-pages (gnus-narrow-to-page))))
11114
11115 (defun gnus-summary-show-article (&optional arg)
11116   "Force re-fetching of the current article.
11117 If ARG (the prefix) is non-nil, show the raw article without any
11118 article massaging functions being run."
11119   (interactive "P")
11120   (gnus-set-global-variables)
11121   (if (not arg)
11122       ;; Select the article the normal way.
11123       (gnus-summary-select-article nil 'force)
11124     ;; Bind the article treatment functions to nil.
11125     (let ((gnus-have-all-headers t)
11126           gnus-article-display-hook
11127           gnus-article-prepare-hook
11128           gnus-break-pages
11129           gnus-visual)
11130       (gnus-summary-select-article nil 'force)))
11131 ;  (gnus-configure-windows 'article)
11132   (gnus-summary-position-point))
11133
11134 (defun gnus-summary-verbose-headers (&optional arg)
11135   "Toggle permanent full header display.
11136 If ARG is a positive number, turn header display on.
11137 If ARG is a negative number, turn header display off."
11138   (interactive "P")
11139   (gnus-set-global-variables)
11140   (gnus-summary-toggle-header arg)
11141   (setq gnus-show-all-headers
11142         (cond ((or (not (numberp arg))
11143                    (zerop arg))
11144                (not gnus-show-all-headers))
11145               ((natnump arg)
11146                t))))
11147
11148 (defun gnus-summary-toggle-header (&optional arg)
11149   "Show the headers if they are hidden, or hide them if they are shown.
11150 If ARG is a positive number, show the entire header.
11151 If ARG is a negative number, hide the unwanted header lines."
11152   (interactive "P")
11153   (gnus-set-global-variables)
11154   (save-excursion
11155     (set-buffer gnus-article-buffer)
11156     (let* ((buffer-read-only nil)
11157            (inhibit-point-motion-hooks t)
11158            (hidden (text-property-any
11159                     (goto-char (point-min)) (search-forward "\n\n")
11160                     'invisible t))
11161            e)
11162       (goto-char (point-min))
11163       (when (search-forward "\n\n" nil t)
11164         (delete-region (point-min) (1- (point))))
11165       (goto-char (point-min))
11166       (save-excursion
11167         (set-buffer gnus-original-article-buffer)
11168         (goto-char (point-min))
11169         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11170       (insert-buffer-substring gnus-original-article-buffer 1 e)
11171       (let ((gnus-inhibit-hiding t))
11172         (run-hooks 'gnus-article-display-hook))
11173       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11174           (gnus-article-hide-headers)))))
11175
11176 (defun gnus-summary-show-all-headers ()
11177   "Make all header lines visible."
11178   (interactive)
11179   (gnus-set-global-variables)
11180   (gnus-article-show-all-headers))
11181
11182 (defun gnus-summary-toggle-mime (&optional arg)
11183   "Toggle MIME processing.
11184 If ARG is a positive number, turn MIME processing on."
11185   (interactive "P")
11186   (gnus-set-global-variables)
11187   (setq gnus-show-mime
11188         (if (null arg) (not gnus-show-mime)
11189           (> (prefix-numeric-value arg) 0)))
11190   (gnus-summary-select-article t 'force))
11191
11192 (defun gnus-summary-caesar-message (&optional arg)
11193   "Caesar rotate the current article by 13.
11194 The numerical prefix specifies how manu places to rotate each letter
11195 forward."
11196   (interactive "P")
11197   (gnus-set-global-variables)
11198   (gnus-summary-select-article)
11199   (let ((mail-header-separator ""))
11200     (gnus-eval-in-buffer-window
11201      gnus-article-buffer
11202      (save-restriction
11203        (widen)
11204        (let ((start (window-start)))
11205          (news-caesar-buffer-body arg)
11206          (set-window-start (get-buffer-window (current-buffer)) start))))))
11207
11208 (defun gnus-summary-stop-page-breaking ()
11209   "Stop page breaking in the current article."
11210   (interactive)
11211   (gnus-set-global-variables)
11212   (gnus-summary-select-article)
11213   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11214
11215 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11216   "Move the current article to a different newsgroup.
11217 If N is a positive number, move the N next articles.
11218 If N is a negative number, move the N previous articles.
11219 If N is nil and any articles have been marked with the process mark,
11220 move those articles instead.
11221 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11222 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11223 re-spool using this method.
11224
11225 For this function to work, both the current newsgroup and the
11226 newsgroup that you want to move to have to support the `request-move'
11227 and `request-accept' functions."
11228   (interactive "P")
11229   (unless action (setq action 'move))
11230   (gnus-set-global-variables)
11231   ;; Check whether the source group supports the required functions.
11232   (cond ((and (eq action 'move)
11233               (not (gnus-check-backend-function
11234                     'request-move-article gnus-newsgroup-name)))
11235          (error "The current group does not support article moving"))
11236         ((and (eq action 'crosspost)
11237               (not (gnus-check-backend-function
11238                     'request-replace-article gnus-newsgroup-name)))
11239          (error "The current group does not support article editing")))
11240   (let ((articles (gnus-summary-work-articles n))
11241         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11242         (names '((move "Move" "Moving")
11243                  (copy "Copy" "Copying")
11244                  (crosspost "Crosspost" "Crossposting")))
11245         (copy-buf (save-excursion
11246                     (nnheader-set-temp-buffer " *copy article*")))
11247         art-group to-method new-xref article to-groups)
11248     (unless (assq action names)
11249       (error "Unknown action %s" action))
11250     ;; Read the newsgroup name.
11251     (when (and (not to-newsgroup)
11252                (not select-method))
11253       (setq to-newsgroup
11254             (gnus-read-move-group-name
11255              (cadr (assq action names))
11256              (symbol-value (intern (format "gnus-current-%s-group" action)))
11257              articles prefix))
11258       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11259     (setq to-method (or select-method 
11260                         (gnus-find-method-for-group to-newsgroup)))
11261     ;; Check the method we are to move this article to...
11262     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11263         (error "%s does not support article copying" (car to-method)))
11264     (or (gnus-check-server to-method)
11265         (error "Can't open server %s" (car to-method)))
11266     (gnus-message 6 "%s to %s: %s..."
11267                   (caddr (assq action names))
11268                   (or (car select-method) to-newsgroup) articles)
11269     (while articles
11270       (setq article (pop articles))
11271       (setq
11272        art-group
11273        (cond
11274         ;; Move the article.
11275         ((eq action 'move)
11276          (gnus-request-move-article
11277           article                       ; Article to move
11278           gnus-newsgroup-name           ; From newsgrouo
11279           (nth 1 (gnus-find-method-for-group
11280                   gnus-newsgroup-name)) ; Server
11281           (list 'gnus-request-accept-article
11282                 to-newsgroup (list 'quote select-method)
11283                 (not articles))         ; Accept form
11284           (not articles)))              ; Only save nov last time
11285         ;; Copy the article.
11286         ((eq action 'copy)
11287          (save-excursion
11288            (set-buffer copy-buf)
11289            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11290            (gnus-request-accept-article
11291             to-newsgroup select-method (not articles))))
11292         ;; Crosspost the article.
11293         ((eq action 'crosspost)
11294          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11295            (setq new-xref (concat gnus-newsgroup-name ":" article))
11296            (if (and xref (not (string= xref "")))
11297                (progn
11298                  (when (string-match "^Xref: " xref)
11299                    (setq xref (substring xref (match-end 0))))
11300                  (setq new-xref (concat xref " " new-xref)))
11301              (setq new-xref (concat (system-name) " " new-xref)))
11302            (save-excursion
11303              (set-buffer copy-buf)
11304              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11305              (nnheader-replace-header "xref" new-xref)
11306              (gnus-request-accept-article
11307               to-newsgroup select-method (not articles)))))))
11308       (if (not art-group)
11309           (gnus-message 1 "Couldn't %s article %s"
11310                         (cadr (assq action names)) article)
11311         (let* ((entry
11312                 (or
11313                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11314                  (gnus-gethash
11315                   (gnus-group-prefixed-name
11316                    (car art-group)
11317                    (or select-method 
11318                        (gnus-find-method-for-group to-newsgroup)))
11319                   gnus-newsrc-hashtb)))
11320                (info (nth 2 entry))
11321                (to-group (gnus-info-group info)))
11322           ;; Update the group that has been moved to.
11323           (when (and info
11324                      (memq action '(move copy)))
11325             (unless (member to-group to-groups)
11326               (push to-group to-groups))
11327
11328             (unless (memq article gnus-newsgroup-unreads)
11329               (gnus-info-set-read
11330                info (gnus-add-to-range (gnus-info-read info)
11331                                        (list (cdr art-group)))))
11332
11333             ;; Copy any marks over to the new group.
11334             (let ((marks gnus-article-mark-lists)
11335                   (to-article (cdr art-group)))
11336
11337               ;; See whether the article is to be put in the cache.
11338               (when gnus-use-cache
11339                 (gnus-cache-possibly-enter-article
11340                  to-group to-article
11341                  (let ((header (copy-sequence
11342                                 (gnus-summary-article-header article))))
11343                    (mail-header-set-number header to-article)
11344                    header)
11345                  (memq article gnus-newsgroup-marked)
11346                  (memq article gnus-newsgroup-dormant)
11347                  (memq article gnus-newsgroup-unreads)))
11348
11349               (while marks
11350                 (when (memq article (symbol-value
11351                                      (intern (format "gnus-newsgroup-%s"
11352                                                      (caar marks)))))
11353                   ;; If the other group is the same as this group,
11354                   ;; then we have to add the mark to the list.
11355                   (when (equal to-group gnus-newsgroup-name)
11356                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11357                          (cons to-article
11358                                (symbol-value
11359                                 (intern (format "gnus-newsgroup-%s"
11360                                                 (caar marks)))))))
11361                   ;; Copy mark to other group.
11362                   (gnus-add-marked-articles
11363                    to-group (cdar marks) (list to-article) info))
11364                 (setq marks (cdr marks)))))
11365
11366           ;; Update the Xref header in this article to point to
11367           ;; the new crossposted article we have just created.
11368           (when (eq action 'crosspost)
11369             (save-excursion
11370               (set-buffer copy-buf)
11371               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11372               (nnheader-replace-header
11373                "xref" (concat new-xref " " (gnus-group-prefixed-name
11374                                             (car art-group) to-method)
11375                               ":" (cdr art-group)))
11376               (gnus-request-replace-article
11377                article gnus-newsgroup-name (current-buffer)))))
11378
11379         (gnus-summary-goto-subject article)
11380         (when (eq action 'move)
11381           (gnus-summary-mark-article article gnus-canceled-mark)))
11382       (gnus-summary-remove-process-mark article))
11383     ;; Re-activate all groups that have been moved to.
11384     (while to-groups
11385       (gnus-activate-group (pop to-groups)))
11386     
11387     (gnus-kill-buffer copy-buf)
11388     (gnus-summary-position-point)
11389     (gnus-set-mode-line 'summary)))
11390
11391 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11392   "Move the current article to a different newsgroup.
11393 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11394 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11395 re-spool using this method."
11396   (interactive "P")
11397   (gnus-summary-move-article n nil select-method 'copy))
11398
11399 (defun gnus-summary-crosspost-article (&optional n)
11400   "Crosspost the current article to some other group."
11401   (interactive "P")
11402   (gnus-summary-move-article n nil nil 'crosspost))
11403
11404 (defvar gnus-summary-respool-default-method nil
11405   "Default method for respooling an article.  
11406 If nil, use to the current newsgroup method.")
11407
11408 (defun gnus-summary-respool-article (&optional n method)
11409   "Respool the current article.
11410 The article will be squeezed through the mail spooling process again,
11411 which means that it will be put in some mail newsgroup or other
11412 depending on `nnmail-split-methods'.
11413 If N is a positive number, respool the N next articles.
11414 If N is a negative number, respool the N previous articles.
11415 If N is nil and any articles have been marked with the process mark,
11416 respool those articles instead.
11417
11418 Respooling can be done both from mail groups and \"real\" newsgroups.
11419 In the former case, the articles in question will be moved from the
11420 current group into whatever groups they are destined to.  In the
11421 latter case, they will be copied into the relevant groups."
11422   (interactive 
11423    (list current-prefix-arg
11424          (let* ((methods (gnus-methods-using 'respool))
11425                 (methname
11426                  (symbol-name (or gnus-summary-respool-default-method
11427                                   (car (gnus-find-method-for-group
11428                                         gnus-newsgroup-name)))))
11429                 (method
11430                  (gnus-completing-read 
11431                   methname "What backend do you want to use when? "
11432                   methods nil t nil 'gnus-method-history))
11433                 ms)
11434            (cond
11435             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11436              (list (intern method) ""))
11437             ((= 1 (length ms))
11438              (car ms))
11439             (t
11440              (cdr (completing-read 
11441                    "Server name: "
11442                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11443   (gnus-set-global-variables)
11444   (unless method
11445     (error "No method given for respooling"))
11446   (if (assoc (symbol-name
11447               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11448              (gnus-methods-using 'respool))
11449       (gnus-summary-move-article n nil method)
11450     (gnus-summary-copy-article n nil method)))
11451
11452 (defun gnus-summary-import-article (file)
11453   "Import a random file into a mail newsgroup."
11454   (interactive "fImport file: ")
11455   (gnus-set-global-variables)
11456   (let ((group gnus-newsgroup-name)
11457         (now (current-time))
11458         atts lines)
11459     (or (gnus-check-backend-function 'request-accept-article group)
11460         (error "%s does not support article importing" group))
11461     (or (file-readable-p file)
11462         (not (file-regular-p file))
11463         (error "Can't read %s" file))
11464     (save-excursion
11465       (set-buffer (get-buffer-create " *import file*"))
11466       (buffer-disable-undo (current-buffer))
11467       (erase-buffer)
11468       (insert-file-contents file)
11469       (goto-char (point-min))
11470       (unless (nnheader-article-p)
11471         ;; This doesn't look like an article, so we fudge some headers.
11472         (setq atts (file-attributes file)
11473               lines (count-lines (point-min) (point-max)))
11474         (insert "From: " (read-string "From: ") "\n"
11475                 "Subject: " (read-string "Subject: ") "\n"
11476                 "Date: " (timezone-make-date-arpa-standard
11477                           (current-time-string (nth 5 atts))
11478                           (current-time-zone now)
11479                           (current-time-zone now)) "\n"
11480                 "Message-ID: " (gnus-inews-message-id) "\n"
11481                 "Lines: " (int-to-string lines) "\n"
11482                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11483       (gnus-request-accept-article group nil t)
11484       (kill-buffer (current-buffer)))))
11485
11486 (defun gnus-summary-expire-articles ()
11487   "Expire all articles that are marked as expirable in the current group."
11488   (interactive)
11489   (gnus-set-global-variables)
11490   (when (gnus-check-backend-function
11491          'request-expire-articles gnus-newsgroup-name)
11492     ;; This backend supports expiry.
11493     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11494            (expirable (if total
11495                           (gnus-list-of-read-articles gnus-newsgroup-name)
11496                         (setq gnus-newsgroup-expirable
11497                               (sort gnus-newsgroup-expirable '<))))
11498            (expiry-wait (gnus-group-get-parameter
11499                          gnus-newsgroup-name 'expiry-wait))
11500            es)
11501       (when expirable
11502         ;; There are expirable articles in this group, so we run them
11503         ;; through the expiry process.
11504         (gnus-message 6 "Expiring articles...")
11505         ;; The list of articles that weren't expired is returned.
11506         (if expiry-wait
11507             (let ((nnmail-expiry-wait-function nil)
11508                   (nnmail-expiry-wait expiry-wait))
11509               (setq es (gnus-request-expire-articles
11510                         expirable gnus-newsgroup-name)))
11511           (setq es (gnus-request-expire-articles
11512                     expirable gnus-newsgroup-name)))
11513         (or total (setq gnus-newsgroup-expirable es))
11514         ;; We go through the old list of expirable, and mark all
11515         ;; really expired articles as nonexistent.
11516         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11517           (let ((gnus-use-cache nil))
11518             (while expirable
11519               (unless (memq (car expirable) es)
11520                 (when (gnus-data-find (car expirable))
11521                   (gnus-summary-mark-article
11522                    (car expirable) gnus-canceled-mark)))
11523               (setq expirable (cdr expirable)))))
11524         (gnus-message 6 "Expiring articles...done")))))
11525
11526 (defun gnus-summary-expire-articles-now ()
11527   "Expunge all expirable articles in the current group.
11528 This means that *all* articles that are marked as expirable will be
11529 deleted forever, right now."
11530   (interactive)
11531   (gnus-set-global-variables)
11532   (or gnus-expert-user
11533       (gnus-y-or-n-p
11534        "Are you really, really, really sure you want to expunge? ")
11535       (error "Phew!"))
11536   (let ((nnmail-expiry-wait 'immediate)
11537         (nnmail-expiry-wait-function nil))
11538     (gnus-summary-expire-articles)))
11539
11540 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11541 (defun gnus-summary-delete-article (&optional n)
11542   "Delete the N next (mail) articles.
11543 This command actually deletes articles.  This is not a marking
11544 command.  The article will disappear forever from your life, never to
11545 return.
11546 If N is negative, delete backwards.
11547 If N is nil and articles have been marked with the process mark,
11548 delete these instead."
11549   (interactive "P")
11550   (gnus-set-global-variables)
11551   (or (gnus-check-backend-function 'request-expire-articles
11552                                    gnus-newsgroup-name)
11553       (error "The current newsgroup does not support article deletion."))
11554   ;; Compute the list of articles to delete.
11555   (let ((articles (gnus-summary-work-articles n))
11556         not-deleted)
11557     (if (and gnus-novice-user
11558              (not (gnus-y-or-n-p
11559                    (format "Do you really want to delete %s forever? "
11560                            (if (> (length articles) 1) "these articles"
11561                              "this article")))))
11562         ()
11563       ;; Delete the articles.
11564       (setq not-deleted (gnus-request-expire-articles
11565                          articles gnus-newsgroup-name 'force))
11566       (while articles
11567         (gnus-summary-remove-process-mark (car articles))
11568         ;; The backend might not have been able to delete the article
11569         ;; after all.
11570         (or (memq (car articles) not-deleted)
11571             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11572         (setq articles (cdr articles))))
11573     (gnus-summary-position-point)
11574     (gnus-set-mode-line 'summary)
11575     not-deleted))
11576
11577 (defun gnus-summary-edit-article (&optional force)
11578   "Enter into a buffer and edit the current article.
11579 This will have permanent effect only in mail groups.
11580 If FORCE is non-nil, allow editing of articles even in read-only
11581 groups."
11582   (interactive "P")
11583   (save-excursion
11584     (set-buffer gnus-summary-buffer)
11585     (gnus-set-global-variables)
11586     (when (and (not force)
11587                (gnus-group-read-only-p))
11588       (error "The current newsgroup does not support article editing."))
11589     (gnus-summary-select-article t nil t)
11590     (gnus-configure-windows 'article)
11591     (select-window (get-buffer-window gnus-article-buffer))
11592     (gnus-message 6 "C-c C-c to end edits")
11593     (setq buffer-read-only nil)
11594     (text-mode)
11595     (use-local-map (copy-keymap (current-local-map)))
11596     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11597     (buffer-enable-undo)
11598     (widen)
11599     (goto-char (point-min))
11600     (search-forward "\n\n" nil t)))
11601
11602 (defun gnus-summary-edit-article-done ()
11603   "Make edits to the current article permanent."
11604   (interactive)
11605   (if (gnus-group-read-only-p)
11606       (progn
11607         (gnus-summary-edit-article-postpone)
11608         (gnus-error
11609          1 "The current newsgroup does not support article editing."))
11610     (let ((buf (format "%s" (buffer-string))))
11611       (erase-buffer)
11612       (insert buf)
11613       (if (not (gnus-request-replace-article
11614                 (cdr gnus-article-current) (car gnus-article-current)
11615                 (current-buffer)))
11616           (error "Couldn't replace article.")
11617         (gnus-article-mode)
11618         (use-local-map gnus-article-mode-map)
11619         (setq buffer-read-only t)
11620         (buffer-disable-undo (current-buffer))
11621         (gnus-configure-windows 'summary)
11622         (gnus-summary-update-article (cdr gnus-article-current))
11623         (when gnus-use-cache
11624           (gnus-cache-update-article 
11625            (cdr gnus-article-current) (car gnus-article-current))))
11626       (save-excursion
11627         (when (get-buffer gnus-original-article-buffer)
11628           (set-buffer gnus-original-article-buffer)
11629           (setq gnus-original-article nil)))
11630       (setq gnus-article-current nil
11631             gnus-current-article nil)
11632       (run-hooks 'gnus-article-display-hook)
11633       (and (gnus-visual-p 'summary-highlight 'highlight)
11634            (run-hooks 'gnus-visual-mark-article-hook)))))
11635
11636 (defun gnus-summary-edit-article-postpone ()
11637   "Postpone changes to the current article."
11638   (interactive)
11639   (gnus-article-mode)
11640   (use-local-map gnus-article-mode-map)
11641   (setq buffer-read-only t)
11642   (buffer-disable-undo (current-buffer))
11643   (gnus-configure-windows 'summary)
11644   (and (gnus-visual-p 'summary-highlight 'highlight)
11645        (run-hooks 'gnus-visual-mark-article-hook)))
11646
11647 (defun gnus-summary-respool-query ()
11648   "Query where the respool algorithm would put this article."
11649   (interactive)
11650   (gnus-set-global-variables)
11651   (gnus-summary-select-article)
11652   (save-excursion
11653     (set-buffer gnus-article-buffer)
11654     (save-restriction
11655       (goto-char (point-min))
11656       (search-forward "\n\n")
11657       (narrow-to-region (point-min) (point))
11658       (pp-eval-expression
11659        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11660
11661 ;; Summary score commands.
11662
11663 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11664
11665 (defun gnus-summary-raise-score (n)
11666   "Raise the score of the current article by N."
11667   (interactive "p")
11668   (gnus-set-global-variables)
11669   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11670
11671 (defun gnus-summary-set-score (n)
11672   "Set the score of the current article to N."
11673   (interactive "p")
11674   (gnus-set-global-variables)
11675   (save-excursion
11676     (gnus-summary-show-thread)
11677     (let ((buffer-read-only nil))
11678       ;; Set score.
11679       (gnus-summary-update-mark
11680        (if (= n (or gnus-summary-default-score 0)) ? 
11681          (if (< n (or gnus-summary-default-score 0))
11682              gnus-score-below-mark gnus-score-over-mark)) 'score))
11683     (let* ((article (gnus-summary-article-number))
11684            (score (assq article gnus-newsgroup-scored)))
11685       (if score (setcdr score n)
11686         (setq gnus-newsgroup-scored
11687               (cons (cons article n) gnus-newsgroup-scored))))
11688     (gnus-summary-update-line)))
11689
11690 (defun gnus-summary-current-score ()
11691   "Return the score of the current article."
11692   (interactive)
11693   (gnus-set-global-variables)
11694   (gnus-message 1 "%s" (gnus-summary-article-score)))
11695
11696 ;; Summary marking commands.
11697
11698 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11699   "Mark articles which has the same subject as read, and then select the next.
11700 If UNMARK is positive, remove any kind of mark.
11701 If UNMARK is negative, tick articles."
11702   (interactive "P")
11703   (gnus-set-global-variables)
11704   (if unmark
11705       (setq unmark (prefix-numeric-value unmark)))
11706   (let ((count
11707          (gnus-summary-mark-same-subject
11708           (gnus-summary-article-subject) unmark)))
11709     ;; Select next unread article.  If auto-select-same mode, should
11710     ;; select the first unread article.
11711     (gnus-summary-next-article t (and gnus-auto-select-same
11712                                       (gnus-summary-article-subject)))
11713     (gnus-message 7 "%d article%s marked as %s"
11714                   count (if (= count 1) " is" "s are")
11715                   (if unmark "unread" "read"))))
11716
11717 (defun gnus-summary-kill-same-subject (&optional unmark)
11718   "Mark articles which has the same subject as read.
11719 If UNMARK is positive, remove any kind of mark.
11720 If UNMARK is negative, tick articles."
11721   (interactive "P")
11722   (gnus-set-global-variables)
11723   (if unmark
11724       (setq unmark (prefix-numeric-value unmark)))
11725   (let ((count
11726          (gnus-summary-mark-same-subject
11727           (gnus-summary-article-subject) unmark)))
11728     ;; If marked as read, go to next unread subject.
11729     (if (null unmark)
11730         ;; Go to next unread subject.
11731         (gnus-summary-next-subject 1 t))
11732     (gnus-message 7 "%d articles are marked as %s"
11733                   count (if unmark "unread" "read"))))
11734
11735 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11736   "Mark articles with same SUBJECT as read, and return marked number.
11737 If optional argument UNMARK is positive, remove any kinds of marks.
11738 If optional argument UNMARK is negative, mark articles as unread instead."
11739   (let ((count 1))
11740     (save-excursion
11741       (cond
11742        ((null unmark)                   ; Mark as read.
11743         (while (and
11744                 (progn
11745                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11746                   (gnus-summary-show-thread) t)
11747                 (gnus-summary-find-subject subject))
11748           (setq count (1+ count))))
11749        ((> unmark 0)                    ; Tick.
11750         (while (and
11751                 (progn
11752                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11753                   (gnus-summary-show-thread) t)
11754                 (gnus-summary-find-subject subject))
11755           (setq count (1+ count))))
11756        (t                               ; Mark as unread.
11757         (while (and
11758                 (progn
11759                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11760                   (gnus-summary-show-thread) t)
11761                 (gnus-summary-find-subject subject))
11762           (setq count (1+ count)))))
11763       (gnus-set-mode-line 'summary)
11764       ;; Return the number of marked articles.
11765       count)))
11766
11767 (defun gnus-summary-mark-as-processable (n &optional unmark)
11768   "Set the process mark on the next N articles.
11769 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11770 the process mark instead.  The difference between N and the actual
11771 number of articles marked is returned."
11772   (interactive "p")
11773   (gnus-set-global-variables)
11774   (let ((backward (< n 0))
11775         (n (abs n)))
11776     (while (and
11777             (> n 0)
11778             (if unmark
11779                 (gnus-summary-remove-process-mark
11780                  (gnus-summary-article-number))
11781               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11782             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11783       (setq n (1- n)))
11784     (if (/= 0 n) (gnus-message 7 "No more articles"))
11785     (gnus-summary-recenter)
11786     (gnus-summary-position-point)
11787     n))
11788
11789 (defun gnus-summary-unmark-as-processable (n)
11790   "Remove the process mark from the next N articles.
11791 If N is negative, mark backward instead.  The difference between N and
11792 the actual number of articles marked is returned."
11793   (interactive "p")
11794   (gnus-set-global-variables)
11795   (gnus-summary-mark-as-processable n t))
11796
11797 (defun gnus-summary-unmark-all-processable ()
11798   "Remove the process mark from all articles."
11799   (interactive)
11800   (gnus-set-global-variables)
11801   (save-excursion
11802     (while gnus-newsgroup-processable
11803       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11804   (gnus-summary-position-point))
11805
11806 (defun gnus-summary-mark-as-expirable (n)
11807   "Mark N articles forward as expirable.
11808 If N is negative, mark backward instead.  The difference between N and
11809 the actual number of articles marked is returned."
11810   (interactive "p")
11811   (gnus-set-global-variables)
11812   (gnus-summary-mark-forward n gnus-expirable-mark))
11813
11814 (defun gnus-summary-mark-article-as-replied (article)
11815   "Mark ARTICLE replied and update the summary line."
11816   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11817   (let ((buffer-read-only nil))
11818     (when (gnus-summary-goto-subject article)
11819       (gnus-summary-update-secondary-mark article))))
11820
11821 (defun gnus-summary-set-bookmark (article)
11822   "Set a bookmark in current article."
11823   (interactive (list (gnus-summary-article-number)))
11824   (gnus-set-global-variables)
11825   (if (or (not (get-buffer gnus-article-buffer))
11826           (not gnus-current-article)
11827           (not gnus-article-current)
11828           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11829       (error "No current article selected"))
11830   ;; Remove old bookmark, if one exists.
11831   (let ((old (assq article gnus-newsgroup-bookmarks)))
11832     (if old (setq gnus-newsgroup-bookmarks
11833                   (delq old gnus-newsgroup-bookmarks))))
11834   ;; Set the new bookmark, which is on the form
11835   ;; (article-number . line-number-in-body).
11836   (setq gnus-newsgroup-bookmarks
11837         (cons
11838          (cons article
11839                (save-excursion
11840                  (set-buffer gnus-article-buffer)
11841                  (count-lines
11842                   (min (point)
11843                        (save-excursion
11844                          (goto-char (point-min))
11845                          (search-forward "\n\n" nil t)
11846                          (point)))
11847                   (point))))
11848          gnus-newsgroup-bookmarks))
11849   (gnus-message 6 "A bookmark has been added to the current article."))
11850
11851 (defun gnus-summary-remove-bookmark (article)
11852   "Remove the bookmark from the current article."
11853   (interactive (list (gnus-summary-article-number)))
11854   (gnus-set-global-variables)
11855   ;; Remove old bookmark, if one exists.
11856   (let ((old (assq article gnus-newsgroup-bookmarks)))
11857     (if old
11858         (progn
11859           (setq gnus-newsgroup-bookmarks
11860                 (delq old gnus-newsgroup-bookmarks))
11861           (gnus-message 6 "Removed bookmark."))
11862       (gnus-message 6 "No bookmark in current article."))))
11863
11864 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11865 (defun gnus-summary-mark-as-dormant (n)
11866   "Mark N articles forward as dormant.
11867 If N is negative, mark backward instead.  The difference between N and
11868 the actual number of articles marked is returned."
11869   (interactive "p")
11870   (gnus-set-global-variables)
11871   (gnus-summary-mark-forward n gnus-dormant-mark))
11872
11873 (defun gnus-summary-set-process-mark (article)
11874   "Set the process mark on ARTICLE and update the summary line."
11875   (setq gnus-newsgroup-processable
11876         (cons article
11877               (delq article gnus-newsgroup-processable)))
11878   (when (gnus-summary-goto-subject article)
11879     (gnus-summary-show-thread)
11880     (gnus-summary-update-secondary-mark article)))
11881
11882 (defun gnus-summary-remove-process-mark (article)
11883   "Remove the process mark from ARTICLE and update the summary line."
11884   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11885   (when (gnus-summary-goto-subject article)
11886     (gnus-summary-show-thread)
11887     (gnus-summary-update-secondary-mark article)))
11888
11889 (defun gnus-summary-set-saved-mark (article)
11890   "Set the process mark on ARTICLE and update the summary line."
11891   (push article gnus-newsgroup-saved)
11892   (when (gnus-summary-goto-subject article)
11893     (gnus-summary-update-secondary-mark article)))
11894
11895 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11896   "Mark N articles as read forwards.
11897 If N is negative, mark backwards instead.
11898 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11899 marked as unread.
11900 The difference between N and the actual number of articles marked is
11901 returned."
11902   (interactive "p")
11903   (gnus-set-global-variables)
11904   (let ((backward (< n 0))
11905         (gnus-summary-goto-unread
11906          (and gnus-summary-goto-unread
11907               (not (eq gnus-summary-goto-unread 'never))
11908               (not (memq mark (list gnus-unread-mark
11909                                     gnus-ticked-mark gnus-dormant-mark)))))
11910         (n (abs n))
11911         (mark (or mark gnus-del-mark)))
11912     (while (and (> n 0)
11913                 (gnus-summary-mark-article nil mark no-expire)
11914                 (zerop (gnus-summary-next-subject
11915                         (if backward -1 1)
11916                         (and gnus-summary-goto-unread
11917                              (not (eq gnus-summary-goto-unread 'never)))
11918                         t)))
11919       (setq n (1- n)))
11920     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11921     (gnus-summary-recenter)
11922     (gnus-summary-position-point)
11923     (gnus-set-mode-line 'summary)
11924     n))
11925
11926 (defun gnus-summary-mark-article-as-read (mark)
11927   "Mark the current article quickly as read with MARK."
11928   (let ((article (gnus-summary-article-number)))
11929     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11930     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11931     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11932     (setq gnus-newsgroup-reads
11933           (cons (cons article mark) gnus-newsgroup-reads))
11934     ;; Possibly remove from cache, if that is used.
11935     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11936     ;; Allow the backend to change the mark.
11937     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11938     ;; Check for auto-expiry.
11939     (when (and gnus-newsgroup-auto-expire
11940                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11941                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11942                    (= mark gnus-ancient-mark)
11943                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11944       (setq mark gnus-expirable-mark)
11945       (push article gnus-newsgroup-expirable))
11946     ;; Set the mark in the buffer.
11947     (gnus-summary-update-mark mark 'unread)
11948     t))
11949
11950 (defun gnus-summary-mark-article-as-unread (mark)
11951   "Mark the current article quickly as unread with MARK."
11952   (let ((article (gnus-summary-article-number)))
11953     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11954     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11955     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11956     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11957     (cond ((= mark gnus-ticked-mark)
11958            (push article gnus-newsgroup-marked))
11959           ((= mark gnus-dormant-mark)
11960            (push article gnus-newsgroup-dormant))
11961           (t
11962            (push article gnus-newsgroup-unreads)))
11963     (setq gnus-newsgroup-reads
11964           (delq (assq article gnus-newsgroup-reads)
11965                 gnus-newsgroup-reads))
11966
11967     ;; See whether the article is to be put in the cache.
11968     (and gnus-use-cache
11969          (vectorp (gnus-summary-article-header article))
11970          (save-excursion
11971            (gnus-cache-possibly-enter-article
11972             gnus-newsgroup-name article
11973             (gnus-summary-article-header article)
11974             (= mark gnus-ticked-mark)
11975             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11976
11977     ;; Fix the mark.
11978     (gnus-summary-update-mark mark 'unread)
11979     t))
11980
11981 (defun gnus-summary-mark-article (&optional article mark no-expire)
11982   "Mark ARTICLE with MARK.  MARK can be any character.
11983 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11984 `??' (dormant) and `?E' (expirable).
11985 If MARK is nil, then the default character `?D' is used.
11986 If ARTICLE is nil, then the article on the current line will be
11987 marked."
11988   ;; The mark might be a string.
11989   (and (stringp mark)
11990        (setq mark (aref mark 0)))
11991   ;; If no mark is given, then we check auto-expiring.
11992   (and (not no-expire)
11993        gnus-newsgroup-auto-expire
11994        (or (not mark)
11995            (and (numberp mark)
11996                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11997                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11998                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11999        (setq mark gnus-expirable-mark))
12000   (let* ((mark (or mark gnus-del-mark))
12001          (article (or article (gnus-summary-article-number))))
12002     (or article (error "No article on current line"))
12003     (if (or (= mark gnus-unread-mark)
12004             (= mark gnus-ticked-mark)
12005             (= mark gnus-dormant-mark))
12006         (gnus-mark-article-as-unread article mark)
12007       (gnus-mark-article-as-read article mark))
12008
12009     ;; See whether the article is to be put in the cache.
12010     (and gnus-use-cache
12011          (not (= mark gnus-canceled-mark))
12012          (vectorp (gnus-summary-article-header article))
12013          (save-excursion
12014            (gnus-cache-possibly-enter-article
12015             gnus-newsgroup-name article
12016             (gnus-summary-article-header article)
12017             (= mark gnus-ticked-mark)
12018             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12019
12020     (if (gnus-summary-goto-subject article nil t)
12021         (let ((buffer-read-only nil))
12022           (gnus-summary-show-thread)
12023           ;; Fix the mark.
12024           (gnus-summary-update-mark mark 'unread)
12025           t))))
12026
12027 (defun gnus-summary-update-secondary-mark (article)
12028   "Update the secondary (read, process, cache) mark."
12029   (gnus-summary-update-mark
12030    (cond ((memq article gnus-newsgroup-processable)
12031           gnus-process-mark)
12032          ((memq article gnus-newsgroup-cached)
12033           gnus-cached-mark)
12034          ((memq article gnus-newsgroup-replied)
12035           gnus-replied-mark)
12036          ((memq article gnus-newsgroup-saved)
12037           gnus-saved-mark)
12038          (t gnus-unread-mark))
12039    'replied)
12040   (when (gnus-visual-p 'summary-highlight 'highlight)
12041     (run-hooks 'gnus-summary-update-hook))
12042   t)
12043
12044 (defun gnus-summary-update-mark (mark type)
12045   (beginning-of-line)
12046   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12047         (buffer-read-only nil))
12048     (when (and forward
12049                (<= (+ forward (point)) (point-max)))
12050       ;; Go to the right position on the line.
12051       (goto-char (+ forward (point)))
12052       ;; Replace the old mark with the new mark.
12053       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12054       ;; Optionally update the marks by some user rule.
12055       (when (eq type 'unread)
12056         (gnus-data-set-mark
12057          (gnus-data-find (gnus-summary-article-number)) mark)
12058         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12059
12060 (defun gnus-mark-article-as-read (article &optional mark)
12061   "Enter ARTICLE in the pertinent lists and remove it from others."
12062   ;; Make the article expirable.
12063   (let ((mark (or mark gnus-del-mark)))
12064     (if (= mark gnus-expirable-mark)
12065         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12066       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12067     ;; Remove from unread and marked lists.
12068     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12069     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12070     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12071     (push (cons article mark) gnus-newsgroup-reads)
12072     ;; Possibly remove from cache, if that is used.
12073     (when gnus-use-cache
12074       (gnus-cache-enter-remove-article article))))
12075
12076 (defun gnus-mark-article-as-unread (article &optional mark)
12077   "Enter ARTICLE in the pertinent lists and remove it from others."
12078   (let ((mark (or mark gnus-ticked-mark)))
12079     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12080     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12081     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12082     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12083     (cond ((= mark gnus-ticked-mark)
12084            (push article gnus-newsgroup-marked))
12085           ((= mark gnus-dormant-mark)
12086            (push article gnus-newsgroup-dormant))
12087           (t
12088            (push article gnus-newsgroup-unreads)))
12089     (setq gnus-newsgroup-reads
12090           (delq (assq article gnus-newsgroup-reads)
12091                 gnus-newsgroup-reads))))
12092
12093 (defalias 'gnus-summary-mark-as-unread-forward
12094   'gnus-summary-tick-article-forward)
12095 (make-obsolete 'gnus-summary-mark-as-unread-forward
12096                'gnus-summary-tick-article-forward)
12097 (defun gnus-summary-tick-article-forward (n)
12098   "Tick N articles forwards.
12099 If N is negative, tick backwards instead.
12100 The difference between N and the number of articles ticked is returned."
12101   (interactive "p")
12102   (gnus-summary-mark-forward n gnus-ticked-mark))
12103
12104 (defalias 'gnus-summary-mark-as-unread-backward
12105   'gnus-summary-tick-article-backward)
12106 (make-obsolete 'gnus-summary-mark-as-unread-backward
12107                'gnus-summary-tick-article-backward)
12108 (defun gnus-summary-tick-article-backward (n)
12109   "Tick N articles backwards.
12110 The difference between N and the number of articles ticked is returned."
12111   (interactive "p")
12112   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12113
12114 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12115 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12116 (defun gnus-summary-tick-article (&optional article clear-mark)
12117   "Mark current article as unread.
12118 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12119 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12120   (interactive)
12121   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12122                                        gnus-ticked-mark)))
12123
12124 (defun gnus-summary-mark-as-read-forward (n)
12125   "Mark N articles as read forwards.
12126 If N is negative, mark backwards instead.
12127 The difference between N and the actual number of articles marked is
12128 returned."
12129   (interactive "p")
12130   (gnus-summary-mark-forward n gnus-del-mark t))
12131
12132 (defun gnus-summary-mark-as-read-backward (n)
12133   "Mark the N articles as read backwards.
12134 The difference between N and the actual number of articles marked is
12135 returned."
12136   (interactive "p")
12137   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12138
12139 (defun gnus-summary-mark-as-read (&optional article mark)
12140   "Mark current article as read.
12141 ARTICLE specifies the article to be marked as read.
12142 MARK specifies a string to be inserted at the beginning of the line."
12143   (gnus-summary-mark-article article mark))
12144
12145 (defun gnus-summary-clear-mark-forward (n)
12146   "Clear marks from N articles forward.
12147 If N is negative, clear backward instead.
12148 The difference between N and the number of marks cleared is returned."
12149   (interactive "p")
12150   (gnus-summary-mark-forward n gnus-unread-mark))
12151
12152 (defun gnus-summary-clear-mark-backward (n)
12153   "Clear marks from N articles backward.
12154 The difference between N and the number of marks cleared is returned."
12155   (interactive "p")
12156   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12157
12158 (defun gnus-summary-mark-unread-as-read ()
12159   "Intended to be used by `gnus-summary-mark-article-hook'."
12160   (when (memq gnus-current-article gnus-newsgroup-unreads)
12161     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12162
12163 (defun gnus-summary-mark-read-and-unread-as-read ()
12164   "Intended to be used by `gnus-summary-mark-article-hook'."
12165   (let ((mark (gnus-summary-article-mark)))
12166     (when (or (gnus-unread-mark-p mark)
12167               (gnus-read-mark-p mark))
12168       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12169
12170 (defun gnus-summary-mark-region-as-read (point mark all)
12171   "Mark all unread articles between point and mark as read.
12172 If given a prefix, mark all articles between point and mark as read,
12173 even ticked and dormant ones."
12174   (interactive "r\nP")
12175   (save-excursion
12176     (let (article)
12177       (goto-char point)
12178       (beginning-of-line)
12179       (while (and
12180               (< (point) mark)
12181               (progn
12182                 (when (or all
12183                           (memq (setq article (gnus-summary-article-number))
12184                                 gnus-newsgroup-unreads))
12185                   (gnus-summary-mark-article article gnus-del-mark))
12186                 t)
12187               (gnus-summary-find-next))))))
12188
12189 (defun gnus-summary-mark-below (score mark)
12190   "Mark articles with score less than SCORE with MARK."
12191   (interactive "P\ncMark: ")
12192   (gnus-set-global-variables)
12193   (setq score (if score
12194                   (prefix-numeric-value score)
12195                 (or gnus-summary-default-score 0)))
12196   (save-excursion
12197     (set-buffer gnus-summary-buffer)
12198     (goto-char (point-min))
12199     (while 
12200         (progn
12201           (and (< (gnus-summary-article-score) score)
12202                (gnus-summary-mark-article nil mark))
12203           (gnus-summary-find-next)))))
12204
12205 (defun gnus-summary-kill-below (&optional score)
12206   "Mark articles with score below SCORE as read."
12207   (interactive "P")
12208   (gnus-set-global-variables)
12209   (gnus-summary-mark-below score gnus-killed-mark))
12210
12211 (defun gnus-summary-clear-above (&optional score)
12212   "Clear all marks from articles with score above SCORE."
12213   (interactive "P")
12214   (gnus-set-global-variables)
12215   (gnus-summary-mark-above score gnus-unread-mark))
12216
12217 (defun gnus-summary-tick-above (&optional score)
12218   "Tick all articles with score above SCORE."
12219   (interactive "P")
12220   (gnus-set-global-variables)
12221   (gnus-summary-mark-above score gnus-ticked-mark))
12222
12223 (defun gnus-summary-mark-above (score mark)
12224   "Mark articles with score over SCORE with MARK."
12225   (interactive "P\ncMark: ")
12226   (gnus-set-global-variables)
12227   (setq score (if score
12228                   (prefix-numeric-value score)
12229                 (or gnus-summary-default-score 0)))
12230   (save-excursion
12231     (set-buffer gnus-summary-buffer)
12232     (goto-char (point-min))
12233     (while (and (progn
12234                   (if (> (gnus-summary-article-score) score)
12235                       (gnus-summary-mark-article nil mark))
12236                   t)
12237                 (gnus-summary-find-next)))))
12238
12239 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12240 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12241 (defun gnus-summary-limit-include-expunged ()
12242   "Display all the hidden articles that were expunged for low scores."
12243   (interactive)
12244   (gnus-set-global-variables)
12245   (let ((buffer-read-only nil))
12246     (let ((scored gnus-newsgroup-scored)
12247           headers h)
12248       (while scored
12249         (or (gnus-summary-goto-subject (caar scored))
12250             (and (setq h (gnus-summary-article-header (caar scored)))
12251                  (< (cdar scored) gnus-summary-expunge-below)
12252                  (setq headers (cons h headers))))
12253         (setq scored (cdr scored)))
12254       (or headers (error "No expunged articles hidden."))
12255       (goto-char (point-min))
12256       (gnus-summary-prepare-unthreaded (nreverse headers)))
12257     (goto-char (point-min))
12258     (gnus-summary-position-point)))
12259
12260 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12261   "Mark all articles not marked as unread in this newsgroup as read.
12262 If prefix argument ALL is non-nil, all articles are marked as read.
12263 If QUIETLY is non-nil, no questions will be asked.
12264 If TO-HERE is non-nil, it should be a point in the buffer.  All
12265 articles before this point will be marked as read.
12266 The number of articles marked as read is returned."
12267   (interactive "P")
12268   (gnus-set-global-variables)
12269   (prog1
12270       (if (or quietly
12271               (not gnus-interactive-catchup) ;Without confirmation?
12272               gnus-expert-user
12273               (gnus-y-or-n-p
12274                (if all
12275                    "Mark absolutely all articles as read? "
12276                  "Mark all unread articles as read? ")))
12277           (if (and not-mark
12278                    (not gnus-newsgroup-adaptive)
12279                    (not gnus-newsgroup-auto-expire))
12280               (progn
12281                 (when all
12282                   (setq gnus-newsgroup-marked nil
12283                         gnus-newsgroup-dormant nil))
12284                 (setq gnus-newsgroup-unreads nil))
12285             ;; We actually mark all articles as canceled, which we
12286             ;; have to do when using auto-expiry or adaptive scoring.
12287             (gnus-summary-show-all-threads)
12288             (if (gnus-summary-first-subject (not all))
12289                 (while (and
12290                         (if to-here (< (point) to-here) t)
12291                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12292                         (gnus-summary-find-next (not all)))))
12293             (unless to-here
12294               (setq gnus-newsgroup-unreads nil))
12295             (gnus-set-mode-line 'summary)))
12296     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12297       (if (and (not to-here) (eq 'nnvirtual (car method)))
12298           (nnvirtual-catchup-group
12299            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12300     (gnus-summary-position-point)))
12301
12302 (defun gnus-summary-catchup-to-here (&optional all)
12303   "Mark all unticked articles before the current one as read.
12304 If ALL is non-nil, also mark ticked and dormant articles as read."
12305   (interactive "P")
12306   (gnus-set-global-variables)
12307   (save-excursion
12308     (let ((beg (point)))
12309       ;; We check that there are unread articles.
12310       (when (or all (gnus-summary-find-prev))
12311         (gnus-summary-catchup all t beg))))
12312   (gnus-summary-position-point))
12313
12314 (defun gnus-summary-catchup-all (&optional quietly)
12315   "Mark all articles in this newsgroup as read."
12316   (interactive "P")
12317   (gnus-set-global-variables)
12318   (gnus-summary-catchup t quietly))
12319
12320 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12321   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12322 If prefix argument ALL is non-nil, all articles are marked as read."
12323   (interactive "P")
12324   (gnus-set-global-variables)
12325   (gnus-summary-catchup all quietly nil 'fast)
12326   ;; Select next newsgroup or exit.
12327   (if (eq gnus-auto-select-next 'quietly)
12328       (gnus-summary-next-group nil)
12329     (gnus-summary-exit)))
12330
12331 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12332   "Mark all articles in this newsgroup as read, and then exit."
12333   (interactive "P")
12334   (gnus-set-global-variables)
12335   (gnus-summary-catchup-and-exit t quietly))
12336
12337 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12338 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12339   "Mark all articles in this group as read and select the next group.
12340 If given a prefix, mark all articles, unread as well as ticked, as
12341 read."
12342   (interactive "P")
12343   (gnus-set-global-variables)
12344   (save-excursion
12345     (gnus-summary-catchup all))
12346   (gnus-summary-next-article t nil nil t))
12347
12348 ;; Thread-based commands.
12349
12350 (defun gnus-summary-articles-in-thread (&optional article)
12351   "Return a list of all articles in the current thread.
12352 If ARTICLE is non-nil, return all articles in the thread that starts
12353 with that article."
12354   (let* ((article (or article (gnus-summary-article-number)))
12355          (data (gnus-data-find-list article))
12356          (top-level (gnus-data-level (car data)))
12357          (top-subject
12358           (cond ((null gnus-thread-operation-ignore-subject)
12359                  (gnus-simplify-subject-re
12360                   (mail-header-subject (gnus-data-header (car data)))))
12361                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12362                  (gnus-simplify-subject-fuzzy
12363                   (mail-header-subject (gnus-data-header (car data)))))
12364                 (t nil)))
12365          (end-point (save-excursion
12366                       (if (gnus-summary-go-to-next-thread) 
12367                           (point) (point-max))))
12368          articles)
12369     (while (and data
12370                 (< (gnus-data-pos (car data)) end-point))
12371       (when (or (not top-subject)
12372                 (string= top-subject
12373                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12374                              (gnus-simplify-subject-fuzzy
12375                               (mail-header-subject
12376                                (gnus-data-header (car data))))
12377                            (gnus-simplify-subject-re
12378                             (mail-header-subject
12379                              (gnus-data-header (car data)))))))
12380         (push (gnus-data-number (car data)) articles))
12381       (unless (and (setq data (cdr data))
12382                    (> (gnus-data-level (car data)) top-level))
12383         (setq data nil)))
12384     ;; Return the list of articles.
12385     (nreverse articles)))
12386
12387 (defun gnus-summary-rethread-current ()
12388   "Rethread the thread the current article is part of."
12389   (interactive)
12390   (gnus-set-global-variables)
12391   (let* ((gnus-show-threads t)
12392          (article (gnus-summary-article-number))
12393          (id (mail-header-id (gnus-summary-article-header)))
12394          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12395     (unless id
12396       (error "No article on the current line"))
12397     (gnus-rebuild-thread id)
12398     (gnus-summary-goto-subject article)))
12399
12400 (defun gnus-summary-reparent-thread ()
12401   "Make current article child of the marked (or previous) article.
12402
12403 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12404 is non-nil or the Subject: of both articles are the same."
12405   (interactive)
12406   (or (not (gnus-group-read-only-p))
12407       (error "The current newsgroup does not support article editing."))
12408   (or (<= (length gnus-newsgroup-processable) 1)
12409       (error "No more than one article may be marked."))
12410   (save-window-excursion
12411     (let ((gnus-article-buffer " *reparent*")
12412           (current-article (gnus-summary-article-number))
12413           ; first grab the marked article, otherwise one line up.
12414           (parent-article (if (not (null gnus-newsgroup-processable))
12415                               (car gnus-newsgroup-processable)
12416                             (save-excursion
12417                               (if (eq (forward-line -1) 0)
12418                                   (gnus-summary-article-number)
12419                                 (error "Beginning of summary buffer."))))))
12420       (or (not (eq current-article parent-article))
12421           (error "An article may not be self-referential."))
12422       (let ((message-id (mail-header-id 
12423                          (gnus-summary-article-header parent-article))))
12424         (or (and message-id (not (equal message-id "")))
12425             (error "No message-id in desired parent."))
12426         (gnus-summary-select-article t t nil current-article)
12427         (set-buffer gnus-article-buffer)
12428         (setq buffer-read-only nil)
12429         (let ((buf (format "%s" (buffer-string))))
12430           (erase-buffer)
12431           (insert buf))
12432         (goto-char (point-min))
12433         (if (search-forward-regexp "^References: " nil t)
12434             (insert message-id " " )
12435           (insert "References: " message-id "\n"))
12436         (or (gnus-request-replace-article current-article
12437                                           (car gnus-article-current)
12438                                           gnus-article-buffer)
12439             (error "Couldn't replace article."))
12440         (set-buffer gnus-summary-buffer)
12441         (gnus-summary-unmark-all-processable)
12442         (gnus-summary-rethread-current)
12443         (gnus-message 3 "Article %d is now the child of article %d."
12444                       current-article parent-article)))))
12445
12446 (defun gnus-summary-toggle-threads (&optional arg)
12447   "Toggle showing conversation threads.
12448 If ARG is positive number, turn showing conversation threads on."
12449   (interactive "P")
12450   (gnus-set-global-variables)
12451   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12452     (setq gnus-show-threads
12453           (if (null arg) (not gnus-show-threads)
12454             (> (prefix-numeric-value arg) 0)))
12455     (gnus-summary-prepare)
12456     (gnus-summary-goto-subject current)
12457     (gnus-summary-position-point)))
12458
12459 (defun gnus-summary-show-all-threads ()
12460   "Show all threads."
12461   (interactive)
12462   (gnus-set-global-variables)
12463   (save-excursion
12464     (let ((buffer-read-only nil))
12465       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12466   (gnus-summary-position-point))
12467
12468 (defun gnus-summary-show-thread ()
12469   "Show thread subtrees.
12470 Returns nil if no thread was there to be shown."
12471   (interactive)
12472   (gnus-set-global-variables)
12473   (let ((buffer-read-only nil)
12474         (orig (point))
12475         ;; first goto end then to beg, to have point at beg after let
12476         (end (progn (end-of-line) (point)))
12477         (beg (progn (beginning-of-line) (point))))
12478     (prog1
12479         ;; Any hidden lines here?
12480         (search-forward "\r" end t)
12481       (subst-char-in-region beg end ?\^M ?\n t)
12482       (goto-char orig)
12483       (gnus-summary-position-point))))
12484
12485 (defun gnus-summary-hide-all-threads ()
12486   "Hide all thread subtrees."
12487   (interactive)
12488   (gnus-set-global-variables)
12489   (save-excursion
12490     (goto-char (point-min))
12491     (gnus-summary-hide-thread)
12492     (while (zerop (gnus-summary-next-thread 1 t))
12493       (gnus-summary-hide-thread)))
12494   (gnus-summary-position-point))
12495
12496 (defun gnus-summary-hide-thread ()
12497   "Hide thread subtrees.
12498 Returns nil if no threads were there to be hidden."
12499   (interactive)
12500   (gnus-set-global-variables)
12501   (let ((buffer-read-only nil)
12502         (start (point))
12503         (article (gnus-summary-article-number)))
12504     (goto-char start)
12505     ;; Go forward until either the buffer ends or the subthread
12506     ;; ends.
12507     (when (and (not (eobp))
12508                (or (zerop (gnus-summary-next-thread 1 t))
12509                    (goto-char (point-max))))
12510       (prog1
12511           (if (and (> (point) start)
12512                    (search-backward "\n" start t))
12513               (progn
12514                 (subst-char-in-region start (point) ?\n ?\^M)
12515                 (gnus-summary-goto-subject article))
12516             (goto-char start)
12517             nil)
12518         ;;(gnus-summary-position-point)
12519         ))))
12520
12521 (defun gnus-summary-go-to-next-thread (&optional previous)
12522   "Go to the same level (or less) next thread.
12523 If PREVIOUS is non-nil, go to previous thread instead.
12524 Return the article number moved to, or nil if moving was impossible."
12525   (let ((level (gnus-summary-thread-level))
12526         (way (if previous -1 1))
12527         (beg (point)))
12528     (forward-line way)
12529     (while (and (not (eobp))
12530                 (< level (gnus-summary-thread-level)))
12531       (forward-line way))
12532     (if (eobp)
12533         (progn
12534           (goto-char beg)
12535           nil)
12536       (setq beg (point))
12537       (prog1
12538           (gnus-summary-article-number)
12539         (goto-char beg)))))
12540
12541 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12542   "Go to the same level (or less) next thread.
12543 If PREVIOUS is non-nil, go to previous thread instead.
12544 Return the article number moved to, or nil if moving was impossible."
12545   (if (and (eq gnus-summary-make-false-root 'dummy)
12546            (gnus-summary-article-intangible-p))
12547       (let ((beg (point)))
12548         (while (and (zerop (forward-line 1))
12549                     (not (gnus-summary-article-intangible-p))
12550                     (not (zerop (save-excursion 
12551                                   (gnus-summary-thread-level))))))
12552         (if (eobp)
12553             (progn
12554               (goto-char beg)
12555               nil)
12556           (point)))
12557     (let* ((level (gnus-summary-thread-level))
12558            (article (gnus-summary-article-number))
12559            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12560            oart)
12561       (while data
12562         (if (<= (gnus-data-level (car data)) level)
12563             (setq oart (gnus-data-number (car data))
12564                   data nil)
12565           (setq data (cdr data))))
12566       (and oart
12567            (gnus-summary-goto-subject oart)))))
12568
12569 (defun gnus-summary-next-thread (n &optional silent)
12570   "Go to the same level next N'th thread.
12571 If N is negative, search backward instead.
12572 Returns the difference between N and the number of skips actually
12573 done.
12574
12575 If SILENT, don't output messages."
12576   (interactive "p")
12577   (gnus-set-global-variables)
12578   (let ((backward (< n 0))
12579         (n (abs n))
12580         old dum int)
12581     (while (and (> n 0)
12582                 (gnus-summary-go-to-next-thread backward))
12583       (decf n))
12584     (unless silent 
12585       (gnus-summary-position-point))
12586     (when (and (not silent) (/= 0 n))
12587       (gnus-message 7 "No more threads"))
12588     n))
12589
12590 (defun gnus-summary-prev-thread (n)
12591   "Go to the same level previous N'th thread.
12592 Returns the difference between N and the number of skips actually
12593 done."
12594   (interactive "p")
12595   (gnus-set-global-variables)
12596   (gnus-summary-next-thread (- n)))
12597
12598 (defun gnus-summary-go-down-thread ()
12599   "Go down one level in the current thread."
12600   (let ((children (gnus-summary-article-children)))
12601     (and children
12602          (gnus-summary-goto-subject (car children)))))
12603
12604 (defun gnus-summary-go-up-thread ()
12605   "Go up one level in the current thread."
12606   (let ((parent (gnus-summary-article-parent)))
12607     (and parent
12608          (gnus-summary-goto-subject parent))))
12609
12610 (defun gnus-summary-down-thread (n)
12611   "Go down thread N steps.
12612 If N is negative, go up instead.
12613 Returns the difference between N and how many steps down that were
12614 taken."
12615   (interactive "p")
12616   (gnus-set-global-variables)
12617   (let ((up (< n 0))
12618         (n (abs n)))
12619     (while (and (> n 0)
12620                 (if up (gnus-summary-go-up-thread)
12621                   (gnus-summary-go-down-thread)))
12622       (setq n (1- n)))
12623     (gnus-summary-position-point)
12624     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12625     n))
12626
12627 (defun gnus-summary-up-thread (n)
12628   "Go up thread N steps.
12629 If N is negative, go up instead.
12630 Returns the difference between N and how many steps down that were
12631 taken."
12632   (interactive "p")
12633   (gnus-set-global-variables)
12634   (gnus-summary-down-thread (- n)))
12635
12636 (defun gnus-summary-top-thread ()
12637   "Go to the top of the thread."
12638   (interactive)
12639   (gnus-set-global-variables)
12640   (while (gnus-summary-go-up-thread))
12641   (gnus-summary-article-number))
12642
12643 (defun gnus-summary-kill-thread (&optional unmark)
12644   "Mark articles under current thread as read.
12645 If the prefix argument is positive, remove any kinds of marks.
12646 If the prefix argument is negative, tick articles instead."
12647   (interactive "P")
12648   (gnus-set-global-variables)
12649   (if unmark
12650       (setq unmark (prefix-numeric-value unmark)))
12651   (let ((articles (gnus-summary-articles-in-thread)))
12652     (save-excursion
12653       ;; Expand the thread.
12654       (gnus-summary-show-thread)
12655       ;; Mark all the articles.
12656       (while articles
12657         (gnus-summary-goto-subject (car articles))
12658         (cond ((null unmark)
12659                (gnus-summary-mark-article-as-read gnus-killed-mark))
12660               ((> unmark 0)
12661                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12662               (t
12663                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12664         (setq articles (cdr articles))))
12665     ;; Hide killed subtrees.
12666     (and (null unmark)
12667          gnus-thread-hide-killed
12668          (gnus-summary-hide-thread))
12669     ;; If marked as read, go to next unread subject.
12670     (if (null unmark)
12671         ;; Go to next unread subject.
12672         (gnus-summary-next-subject 1 t)))
12673   (gnus-set-mode-line 'summary))
12674
12675 ;; Summary sorting commands
12676
12677 (defun gnus-summary-sort-by-number (&optional reverse)
12678   "Sort summary buffer by article number.
12679 Argument REVERSE means reverse order."
12680   (interactive "P")
12681   (gnus-summary-sort 'number reverse))
12682
12683 (defun gnus-summary-sort-by-author (&optional reverse)
12684   "Sort summary buffer by author name alphabetically.
12685 If case-fold-search is non-nil, case of letters is ignored.
12686 Argument REVERSE means reverse order."
12687   (interactive "P")
12688   (gnus-summary-sort 'author reverse))
12689
12690 (defun gnus-summary-sort-by-subject (&optional reverse)
12691   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12692 If case-fold-search is non-nil, case of letters is ignored.
12693 Argument REVERSE means reverse order."
12694   (interactive "P")
12695   (gnus-summary-sort 'subject reverse))
12696
12697 (defun gnus-summary-sort-by-date (&optional reverse)
12698   "Sort summary buffer by date.
12699 Argument REVERSE means reverse order."
12700   (interactive "P")
12701   (gnus-summary-sort 'date reverse))
12702
12703 (defun gnus-summary-sort-by-score (&optional reverse)
12704   "Sort summary buffer by score.
12705 Argument REVERSE means reverse order."
12706   (interactive "P")
12707   (gnus-summary-sort 'score reverse))
12708
12709 (defun gnus-summary-sort (predicate reverse)
12710   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12711   (gnus-set-global-variables)
12712   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12713          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12714          (gnus-thread-sort-functions
12715           (list
12716            (if (not reverse)
12717                thread
12718              `(lambda (t1 t2)
12719                 (,thread t2 t1)))))
12720          (gnus-article-sort-functions
12721           (list
12722            (if (not reverse)
12723                article
12724              `(lambda (t1 t2)
12725                 (,article t2 t1)))))
12726          (buffer-read-only)
12727          (gnus-summary-prepare-hook nil))
12728     ;; We do the sorting by regenerating the threads.
12729     (gnus-summary-prepare)
12730     ;; Hide subthreads if needed.
12731     (when (and gnus-show-threads gnus-thread-hide-subtree)
12732       (gnus-summary-hide-all-threads)))
12733   ;; If in async mode, we send some info to the backend.
12734   (when gnus-newsgroup-async
12735     (gnus-request-asynchronous
12736      gnus-newsgroup-name gnus-newsgroup-data)))
12737
12738 (defun gnus-sortable-date (date)
12739   "Make sortable string by string-lessp from DATE.
12740 Timezone package is used."
12741   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12742          (year (aref date 0))
12743          (month (aref date 1))
12744          (day (aref date 2)))
12745     (timezone-make-sortable-date
12746      year month day
12747      (timezone-make-time-string
12748       (aref date 3) (aref date 4) (aref date 5)))))
12749
12750 ;; Summary saving commands.
12751
12752 (defun gnus-summary-save-article (&optional n not-saved)
12753   "Save the current article using the default saver function.
12754 If N is a positive number, save the N next articles.
12755 If N is a negative number, save the N previous articles.
12756 If N is nil and any articles have been marked with the process mark,
12757 save those articles instead.
12758 The variable `gnus-default-article-saver' specifies the saver function."
12759   (interactive "P")
12760   (gnus-set-global-variables)
12761   (let ((articles (gnus-summary-work-articles n))
12762         file header article)
12763     (while articles
12764       (setq header (gnus-summary-article-header
12765                     (setq article (pop articles))))
12766       (if (not (vectorp header))
12767           ;; This is a pseudo-article.
12768           (if (assq 'name header)
12769               (gnus-copy-file (cdr (assq 'name header)))
12770             (gnus-message 1 "Article %d is unsaveable" article))
12771         ;; This is a real article.
12772         (save-window-excursion
12773           (gnus-summary-select-article t nil nil article))
12774         (unless gnus-save-all-headers
12775           ;; Remove headers accoring to `gnus-saved-headers'.
12776           (let ((gnus-visible-headers
12777                  (or gnus-saved-headers gnus-visible-headers)))
12778             (gnus-article-hide-headers nil t)))
12779         ;; Remove any X-Gnus lines.
12780         (save-excursion
12781           (set-buffer gnus-article-buffer)
12782           (save-restriction
12783             (let ((buffer-read-only nil))
12784               (nnheader-narrow-to-headers)
12785               (while (re-search-forward "^X-Gnus" nil t)
12786                 (gnus-delete-line)))))
12787         (save-window-excursion
12788           (if (not gnus-default-article-saver)
12789               (error "No default saver is defined.")
12790             (setq file (funcall
12791                         gnus-default-article-saver
12792                         (cond
12793                          ((not gnus-prompt-before-saving)
12794                           'default)
12795                          ((eq gnus-prompt-before-saving 'always)
12796                           nil)
12797                          (t file))))))
12798         (gnus-summary-remove-process-mark article)
12799         (unless not-saved
12800           (gnus-summary-set-saved-mark article))))
12801     (gnus-summary-position-point)
12802     n))
12803
12804 (defun gnus-summary-pipe-output (&optional arg)
12805   "Pipe the current article to a subprocess.
12806 If N is a positive number, pipe the N next articles.
12807 If N is a negative number, pipe the N previous articles.
12808 If N is nil and any articles have been marked with the process mark,
12809 pipe those articles instead."
12810   (interactive "P")
12811   (gnus-set-global-variables)
12812   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12813     (gnus-summary-save-article arg t))
12814   (gnus-configure-windows 'pipe))
12815
12816 (defun gnus-summary-save-article-mail (&optional arg)
12817   "Append the current article to an mail file.
12818 If N is a positive number, save the N next articles.
12819 If N is a negative number, save the N previous articles.
12820 If N is nil and any articles have been marked with the process mark,
12821 save those articles instead."
12822   (interactive "P")
12823   (gnus-set-global-variables)
12824   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12825     (gnus-summary-save-article arg)))
12826
12827 (defun gnus-summary-save-article-rmail (&optional arg)
12828   "Append the current article to an rmail file.
12829 If N is a positive number, save the N next articles.
12830 If N is a negative number, save the N previous articles.
12831 If N is nil and any articles have been marked with the process mark,
12832 save those articles instead."
12833   (interactive "P")
12834   (gnus-set-global-variables)
12835   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12836     (gnus-summary-save-article arg)))
12837
12838 (defun gnus-summary-save-article-file (&optional arg)
12839   "Append the current article to a file.
12840 If N is a positive number, save the N next articles.
12841 If N is a negative number, save the N previous articles.
12842 If N is nil and any articles have been marked with the process mark,
12843 save those articles instead."
12844   (interactive "P")
12845   (gnus-set-global-variables)
12846   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12847     (gnus-summary-save-article arg)))
12848
12849 (defun gnus-summary-save-article-body-file (&optional arg)
12850   "Append the current article body to a file.
12851 If N is a positive number, save the N next articles.
12852 If N is a negative number, save the N previous articles.
12853 If N is nil and any articles have been marked with the process mark,
12854 save those articles instead."
12855   (interactive "P")
12856   (gnus-set-global-variables)
12857   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12858     (gnus-summary-save-article arg)))
12859
12860 (defun gnus-get-split-value (methods)
12861   "Return a value based on the split METHODS."
12862   (let (split-name method result match)
12863     (when methods
12864       (save-excursion
12865         (set-buffer gnus-original-article-buffer)
12866         (save-restriction
12867           (nnheader-narrow-to-headers)
12868           (while methods
12869             (goto-char (point-min))
12870             (setq method (pop methods))
12871             (setq match (car method))
12872             (when (cond
12873                    ((stringp match)
12874                     ;; Regular expression.
12875                     (condition-case ()
12876                         (re-search-forward match nil t)
12877                       (error nil)))
12878                    ((gnus-functionp match)
12879                     ;; Function.
12880                     (save-restriction
12881                       (widen)
12882                       (setq result (funcall match gnus-newsgroup-name))))
12883                    ((consp match)
12884                     ;; Form.
12885                     (save-restriction
12886                       (widen)
12887                       (setq result (eval match)))))
12888               (setq split-name (append (cdr method) split-name))
12889               (cond ((stringp result)
12890                      (push result split-name))
12891                     ((consp result)
12892                      (setq split-name (append result split-name)))))))))
12893     split-name))
12894
12895 (defun gnus-read-move-group-name (prompt default articles prefix)
12896   "Read a group name."
12897   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12898          group-map
12899          (dum (mapatoms
12900                (lambda (g) 
12901                  (and (boundp g)
12902                       (symbol-name g)
12903                       (memq 'respool
12904                             (assoc (symbol-name
12905                                     (car (gnus-find-method-for-group
12906                                           (symbol-name g))))
12907                                    gnus-valid-select-methods))
12908                       (push (list (symbol-name g)) group-map)))
12909                gnus-active-hashtb))
12910          (prom
12911           (format "%s %s to:"
12912                   prompt
12913                   (if (> (length articles) 1)
12914                       (format "these %d articles" (length articles))
12915                     "this article")))
12916          (to-newsgroup
12917           (cond
12918            ((null split-name)
12919             (gnus-completing-read default prom
12920                                   group-map nil nil prefix
12921                                   'gnus-group-history))
12922            ((= 1 (length split-name))
12923             (gnus-completing-read (car split-name) prom group-map
12924                                   nil nil nil
12925                                   'gnus-group-history))
12926            (t
12927             (gnus-completing-read nil prom 
12928                                   (mapcar (lambda (el) (list el))
12929                                           (nreverse split-name))
12930                                   nil nil nil
12931                                   'gnus-group-history)))))
12932     (when to-newsgroup
12933       (if (or (string= to-newsgroup "")
12934               (string= to-newsgroup prefix))
12935           (setq to-newsgroup (or default "")))
12936       (or (gnus-active to-newsgroup)
12937           (gnus-activate-group to-newsgroup)
12938           (error "No such group: %s" to-newsgroup)))
12939     to-newsgroup))
12940
12941 (defun gnus-read-save-file-name (prompt default-name)
12942   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12943          (file
12944           ;; Let the split methods have their say.
12945           (cond
12946            ;; No split name was found.
12947            ((null split-name)
12948             (read-file-name
12949              (concat prompt " (default "
12950                      (file-name-nondirectory default-name) ") ")
12951              (file-name-directory default-name)
12952              default-name))
12953            ;; A single split name was found
12954            ((= 1 (length split-name))
12955             (read-file-name
12956              (concat prompt " (default " (car split-name) ") ")
12957              gnus-article-save-directory
12958              (concat gnus-article-save-directory (car split-name))))
12959            ;; A list of splits was found.
12960            (t
12961             (setq split-name (nreverse split-name))
12962             (let (result)
12963               (let ((file-name-history (nconc split-name file-name-history)))
12964                 (setq result
12965                       (read-file-name
12966                        (concat prompt " (`M-p' for defaults) ")
12967                        gnus-article-save-directory
12968                        (car split-name))))
12969               (car (push result file-name-history)))))))
12970     ;; If we have read a directory, we append the default file name.
12971     (when (file-directory-p file)
12972       (setq file (concat (file-name-as-directory file)
12973                          (file-name-nondirectory default-name))))
12974     ;; Possibly translate some charaters.
12975     (nnheader-translate-file-chars file)))
12976
12977 (defun gnus-article-archive-name (group)
12978   "Return the first instance of an \"Archive-name\" in the current buffer."
12979   (let ((case-fold-search t))
12980     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12981       (match-string 1))))
12982
12983 (defun gnus-summary-save-in-rmail (&optional filename)
12984   "Append this article to Rmail file.
12985 Optional argument FILENAME specifies file name.
12986 Directory to save to is default to `gnus-article-save-directory' which
12987 is initialized from the SAVEDIR environment variable."
12988   (interactive)
12989   (gnus-set-global-variables)
12990   (let ((default-name
12991           (funcall gnus-rmail-save-name gnus-newsgroup-name
12992                    gnus-current-headers gnus-newsgroup-last-rmail)))
12993     (setq filename
12994           (cond ((eq filename 'default)
12995                  default-name)
12996                 (filename filename)
12997                 (t (gnus-read-save-file-name
12998                     "Save in rmail file:" default-name))))
12999     (gnus-make-directory (file-name-directory filename))
13000     (gnus-eval-in-buffer-window
13001      gnus-original-article-buffer
13002      (save-excursion
13003        (save-restriction
13004          (widen)
13005          (gnus-output-to-rmail filename))))
13006     ;; Remember the directory name to save articles
13007     (setq gnus-newsgroup-last-rmail filename)))
13008
13009 (defun gnus-summary-save-in-mail (&optional filename)
13010   "Append this article to Unix mail file.
13011 Optional argument FILENAME specifies file name.
13012 Directory to save to is default to `gnus-article-save-directory' which
13013 is initialized from the SAVEDIR environment variable."
13014   (interactive)
13015   (gnus-set-global-variables)
13016   (let ((default-name
13017           (funcall gnus-mail-save-name gnus-newsgroup-name
13018                    gnus-current-headers gnus-newsgroup-last-mail)))
13019     (setq filename
13020           (cond ((eq filename 'default)
13021                  default-name)
13022                 (filename filename)
13023                 (t (gnus-read-save-file-name
13024                     "Save in Unix mail file:" default-name))))
13025     (setq filename
13026           (expand-file-name filename
13027                             (and default-name
13028                                  (file-name-directory default-name))))
13029     (gnus-make-directory (file-name-directory filename))
13030     (gnus-eval-in-buffer-window
13031      gnus-original-article-buffer
13032      (save-excursion
13033        (save-restriction
13034          (widen)
13035          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13036              (gnus-output-to-rmail filename)
13037            (let ((mail-use-rfc822 t))
13038              (rmail-output filename 1 t t))))))
13039     ;; Remember the directory name to save articles.
13040     (setq gnus-newsgroup-last-mail filename)))
13041
13042 (defun gnus-summary-save-in-file (&optional filename)
13043   "Append this article to file.
13044 Optional argument FILENAME specifies file name.
13045 Directory to save to is default to `gnus-article-save-directory' which
13046 is initialized from the SAVEDIR environment variable."
13047   (interactive)
13048   (gnus-set-global-variables)
13049   (let ((default-name
13050           (funcall gnus-file-save-name gnus-newsgroup-name
13051                    gnus-current-headers gnus-newsgroup-last-file)))
13052     (setq filename
13053           (cond ((eq filename 'default)
13054                  default-name)
13055                 (filename filename)
13056                 (t (gnus-read-save-file-name
13057                     "Save in file:" default-name))))
13058     (gnus-make-directory (file-name-directory filename))
13059     (gnus-eval-in-buffer-window
13060      gnus-original-article-buffer
13061      (save-excursion
13062        (save-restriction
13063          (widen)
13064          (gnus-output-to-file filename))))
13065     ;; Remember the directory name to save articles.
13066     (setq gnus-newsgroup-last-file filename)))
13067
13068 (defun gnus-summary-save-body-in-file (&optional filename)
13069   "Append this article body to a file.
13070 Optional argument FILENAME specifies file name.
13071 The directory to save in defaults to `gnus-article-save-directory' which
13072 is initialized from the SAVEDIR environment variable."
13073   (interactive)
13074   (gnus-set-global-variables)
13075   (let ((default-name
13076           (funcall gnus-file-save-name gnus-newsgroup-name
13077                    gnus-current-headers gnus-newsgroup-last-file)))
13078     (setq filename
13079           (cond ((eq filename 'default)
13080                  default-name)
13081                 (filename filename)
13082                 (t (gnus-read-save-file-name
13083                     "Save body in file:" default-name))))
13084     (gnus-make-directory (file-name-directory filename))
13085     (gnus-eval-in-buffer-window
13086      gnus-article-buffer
13087      (save-excursion
13088        (save-restriction
13089          (widen)
13090          (goto-char (point-min))
13091          (and (search-forward "\n\n" nil t)
13092               (narrow-to-region (point) (point-max)))
13093          (gnus-output-to-file filename))))
13094     ;; Remember the directory name to save articles.
13095     (setq gnus-newsgroup-last-file filename)))
13096
13097 (defun gnus-summary-save-in-pipe (&optional command)
13098   "Pipe this article to subprocess."
13099   (interactive)
13100   (gnus-set-global-variables)
13101   (setq command
13102         (cond ((eq command 'default)
13103                gnus-last-shell-command)
13104               (command command)
13105               (t (read-string "Shell command on article: "
13106                               gnus-last-shell-command))))
13107   (if (string-equal command "")
13108       (setq command gnus-last-shell-command))
13109   (gnus-eval-in-buffer-window
13110    gnus-article-buffer
13111    (save-restriction
13112      (widen)
13113      (shell-command-on-region (point-min) (point-max) command nil)))
13114   (setq gnus-last-shell-command command))
13115
13116 ;; Summary extract commands
13117
13118 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13119   (let ((buffer-read-only nil)
13120         (article (gnus-summary-article-number))
13121         after-article b e)
13122     (or (gnus-summary-goto-subject article)
13123         (error (format "No such article: %d" article)))
13124     (gnus-summary-position-point)
13125     ;; If all commands are to be bunched up on one line, we collect
13126     ;; them here.
13127     (if gnus-view-pseudos-separately
13128         ()
13129       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13130             files action)
13131         (while ps
13132           (setq action (cdr (assq 'action (car ps))))
13133           (setq files (list (cdr (assq 'name (car ps)))))
13134           (while (and ps (cdr ps)
13135                       (string= (or action "1")
13136                                (or (cdr (assq 'action (cadr ps))) "2")))
13137             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13138             (setcdr ps (cddr ps)))
13139           (if (not files)
13140               ()
13141             (if (not (string-match "%s" action))
13142                 (setq files (cons " " files)))
13143             (setq files (cons " " files))
13144             (and (assq 'execute (car ps))
13145                  (setcdr (assq 'execute (car ps))
13146                          (funcall (if (string-match "%s" action)
13147                                       'format 'concat)
13148                                   action
13149                                   (mapconcat (lambda (f) f) files " ")))))
13150           (setq ps (cdr ps)))))
13151     (if (and gnus-view-pseudos (not not-view))
13152         (while pslist
13153           (and (assq 'execute (car pslist))
13154                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13155                                      (eq gnus-view-pseudos 'not-confirm)))
13156           (setq pslist (cdr pslist)))
13157       (save-excursion
13158         (while pslist
13159           (setq after-article (or (cdr (assq 'article (car pslist)))
13160                                   (gnus-summary-article-number)))
13161           (gnus-summary-goto-subject after-article)
13162           (forward-line 1)
13163           (setq b (point))
13164           (insert "    " (file-name-nondirectory
13165                                 (cdr (assq 'name (car pslist))))
13166                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13167           (setq e (point))
13168           (forward-line -1)             ; back to `b'
13169           (add-text-properties
13170            b e (list 'gnus-number gnus-reffed-article-number
13171                      gnus-mouse-face-prop gnus-mouse-face))
13172           (gnus-data-enter
13173            after-article gnus-reffed-article-number
13174            gnus-unread-mark b (car pslist) 0 (- e b))
13175           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13176           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13177           (setq pslist (cdr pslist)))))))
13178
13179 (defun gnus-pseudos< (p1 p2)
13180   (let ((c1 (cdr (assq 'action p1)))
13181         (c2 (cdr (assq 'action p2))))
13182     (and c1 c2 (string< c1 c2))))
13183
13184 (defun gnus-request-pseudo-article (props)
13185   (cond ((assq 'execute props)
13186          (gnus-execute-command (cdr (assq 'execute props)))))
13187   (let ((gnus-current-article (gnus-summary-article-number)))
13188     (run-hooks 'gnus-mark-article-hook)))
13189
13190 (defun gnus-execute-command (command &optional automatic)
13191   (save-excursion
13192     (gnus-article-setup-buffer)
13193     (set-buffer gnus-article-buffer)
13194     (let ((command (if automatic command (read-string "Command: " command)))
13195           (buffer-read-only nil))
13196       (erase-buffer)
13197       (insert "$ " command "\n\n")
13198       (if gnus-view-pseudo-asynchronously
13199           (start-process "gnus-execute" nil "sh" "-c" command)
13200         (call-process "sh" nil t nil "-c" command)))))
13201
13202 (defun gnus-copy-file (file &optional to)
13203   "Copy FILE to TO."
13204   (interactive
13205    (list (read-file-name "Copy file: " default-directory)
13206          (read-file-name "Copy file to: " default-directory)))
13207   (gnus-set-global-variables)
13208   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13209   (and (file-directory-p to)
13210        (setq to (concat (file-name-as-directory to)
13211                         (file-name-nondirectory file))))
13212   (copy-file file to))
13213
13214 ;; Summary kill commands.
13215
13216 (defun gnus-summary-edit-global-kill (article)
13217   "Edit the \"global\" kill file."
13218   (interactive (list (gnus-summary-article-number)))
13219   (gnus-set-global-variables)
13220   (gnus-group-edit-global-kill article))
13221
13222 (defun gnus-summary-edit-local-kill ()
13223   "Edit a local kill file applied to the current newsgroup."
13224   (interactive)
13225   (gnus-set-global-variables)
13226   (setq gnus-current-headers (gnus-summary-article-header))
13227   (gnus-set-global-variables)
13228   (gnus-group-edit-local-kill
13229    (gnus-summary-article-number) gnus-newsgroup-name))
13230
13231 \f
13232 ;;;
13233 ;;; Gnus article mode
13234 ;;;
13235
13236 (put 'gnus-article-mode 'mode-class 'special)
13237
13238 (if gnus-article-mode-map
13239     nil
13240   (setq gnus-article-mode-map (make-keymap))
13241   (suppress-keymap gnus-article-mode-map)
13242
13243   (gnus-define-keys gnus-article-mode-map
13244     " " gnus-article-goto-next-page
13245     "\177" gnus-article-goto-prev-page
13246     [delete] gnus-article-goto-prev-page
13247     "\C-c^" gnus-article-refer-article
13248     "h" gnus-article-show-summary
13249     "s" gnus-article-show-summary
13250     "\C-c\C-m" gnus-article-mail
13251     "?" gnus-article-describe-briefly
13252     gnus-mouse-2 gnus-article-push-button
13253     "\r" gnus-article-press-button
13254     "\t" gnus-article-next-button
13255     "\M-\t" gnus-article-prev-button
13256     "\C-c\C-b" gnus-bug)
13257
13258   (substitute-key-definition
13259    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13260
13261 (defun gnus-article-mode ()
13262   "Major mode for displaying an article.
13263
13264 All normal editing commands are switched off.
13265
13266 The following commands are available:
13267
13268 \\<gnus-article-mode-map>
13269 \\[gnus-article-next-page]\t Scroll the article one page forwards
13270 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13271 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13272 \\[gnus-article-show-summary]\t Display the summary buffer
13273 \\[gnus-article-mail]\t Send a reply to the address near point
13274 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13275 \\[gnus-info-find-node]\t Go to the Gnus info node"
13276   (interactive)
13277   (when (and menu-bar-mode
13278              (gnus-visual-p 'article-menu 'menu))
13279     (gnus-article-make-menu-bar))
13280   (kill-all-local-variables)
13281   (gnus-simplify-mode-line)
13282   (setq mode-name "Article")
13283   (setq major-mode 'gnus-article-mode)
13284   (make-local-variable 'minor-mode-alist)
13285   (or (assq 'gnus-show-mime minor-mode-alist)
13286       (setq minor-mode-alist
13287             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13288   (use-local-map gnus-article-mode-map)
13289   (make-local-variable 'page-delimiter)
13290   (setq page-delimiter gnus-page-delimiter)
13291   (buffer-disable-undo (current-buffer))
13292   (setq buffer-read-only t)             ;Disable modification
13293   (run-hooks 'gnus-article-mode-hook))
13294
13295 (defun gnus-article-setup-buffer ()
13296   "Initialize the article buffer."
13297   (let* ((name (if gnus-single-article-buffer "*Article*"
13298                  (concat "*Article " gnus-newsgroup-name "*")))
13299          (original
13300           (progn (string-match "\\*Article" name)
13301                  (concat " *Original Article"
13302                          (substring name (match-end 0))))))
13303     (setq gnus-article-buffer name)
13304     (setq gnus-original-article-buffer original)
13305     ;; This might be a variable local to the summary buffer.
13306     (unless gnus-single-article-buffer
13307       (save-excursion
13308         (set-buffer gnus-summary-buffer)
13309         (setq gnus-article-buffer name)
13310         (setq gnus-original-article-buffer original)
13311         (gnus-set-global-variables))
13312       (make-local-variable 'gnus-summary-buffer))
13313     ;; Init original article buffer.
13314     (save-excursion
13315       (set-buffer (get-buffer-create gnus-original-article-buffer))
13316       (buffer-disable-undo (current-buffer))
13317       (setq major-mode 'gnus-original-article-mode)
13318       (make-local-variable 'gnus-original-article))
13319     (if (get-buffer name)
13320         (save-excursion
13321           (set-buffer name)
13322           (buffer-disable-undo (current-buffer))
13323           (setq buffer-read-only t)
13324           (gnus-add-current-to-buffer-list)
13325           (or (eq major-mode 'gnus-article-mode)
13326               (gnus-article-mode))
13327           (current-buffer))
13328       (save-excursion
13329         (set-buffer (get-buffer-create name))
13330         (gnus-add-current-to-buffer-list)
13331         (gnus-article-mode)
13332         (current-buffer)))))
13333
13334 ;; Set article window start at LINE, where LINE is the number of lines
13335 ;; from the head of the article.
13336 (defun gnus-article-set-window-start (&optional line)
13337   (set-window-start
13338    (get-buffer-window gnus-article-buffer)
13339    (save-excursion
13340      (set-buffer gnus-article-buffer)
13341      (goto-char (point-min))
13342      (if (not line)
13343          (point-min)
13344        (gnus-message 6 "Moved to bookmark")
13345        (search-forward "\n\n" nil t)
13346        (forward-line line)
13347        (point)))))
13348
13349 (defun gnus-kill-all-overlays ()
13350   "Delete all overlays in the current buffer."
13351   (when (fboundp 'overlay-lists)
13352     (let* ((overlayss (overlay-lists))
13353            (buffer-read-only nil)
13354            (overlays (nconc (car overlayss) (cdr overlayss))))
13355       (while overlays
13356         (delete-overlay (pop overlays))))))
13357
13358 (defun gnus-request-article-this-buffer (article group)
13359   "Get an article and insert it into this buffer."
13360   (let (do-update-line)
13361     (prog1
13362         (save-excursion
13363           (erase-buffer)
13364           (gnus-kill-all-overlays)
13365           (setq group (or group gnus-newsgroup-name))
13366
13367           ;; Open server if it has closed.
13368           (gnus-check-server (gnus-find-method-for-group group))
13369
13370           ;; Using `gnus-request-article' directly will insert the article into
13371           ;; `nntp-server-buffer' - so we'll save some time by not having to
13372           ;; copy it from the server buffer into the article buffer.
13373
13374           ;; We only request an article by message-id when we do not have the
13375           ;; headers for it, so we'll have to get those.
13376           (when (stringp article)
13377             (let ((gnus-override-method gnus-refer-article-method))
13378               (gnus-read-header article)))
13379
13380           ;; If the article number is negative, that means that this article
13381           ;; doesn't belong in this newsgroup (possibly), so we find its
13382           ;; message-id and request it by id instead of number.
13383           (when (and (numberp article)
13384                      gnus-summary-buffer
13385                      (get-buffer gnus-summary-buffer)
13386                      (buffer-name (get-buffer gnus-summary-buffer)))
13387             (save-excursion
13388               (set-buffer gnus-summary-buffer)
13389               (let ((header (gnus-summary-article-header article)))
13390                 (if (< article 0)
13391                     (cond 
13392                      ((memq article gnus-newsgroup-sparse)
13393                       ;; This is a sparse gap article.
13394                       (setq do-update-line article)
13395                       (setq article (mail-header-id header))
13396                       (let ((gnus-override-method gnus-refer-article-method))
13397                         (gnus-read-header article)))
13398                      ((vectorp header)
13399                       ;; It's a real article.
13400                       (setq article (mail-header-id header)))
13401                      (t
13402                       ;; It is an extracted pseudo-article.
13403                       (setq article 'pseudo)
13404                       (gnus-request-pseudo-article header))))
13405                 
13406                 (let ((method (gnus-find-method-for-group 
13407                                gnus-newsgroup-name)))
13408                   (if (not (eq (car method) 'nneething))
13409                       ()
13410                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13411                                        (mail-header-subject header))))
13412                       (if (file-directory-p dir)
13413                           (progn
13414                             (setq article 'nneething)
13415                             (gnus-group-enter-directory dir)))))))))
13416
13417           (cond
13418            ;; We first check `gnus-original-article-buffer'.
13419            ((and (get-buffer gnus-original-article-buffer)
13420                  (save-excursion
13421                    (set-buffer gnus-original-article-buffer)
13422                    (and (equal (car gnus-original-article) group)
13423                         (eq (cdr gnus-original-article) article))))
13424             (insert-buffer-substring gnus-original-article-buffer)
13425             'article)
13426            ;; Check the backlog.
13427            ((and gnus-keep-backlog
13428                  (gnus-backlog-request-article group article (current-buffer)))
13429             'article)
13430            ;; Check the cache.
13431            ((and gnus-use-cache
13432                  (numberp article)
13433                  (gnus-cache-request-article article group))
13434             'article)
13435            ;; Get the article and put into the article buffer.
13436            ((or (stringp article) (numberp article))
13437             (let ((gnus-override-method
13438                    (and (stringp article) gnus-refer-article-method))
13439                   (buffer-read-only nil))
13440               (erase-buffer)
13441               (gnus-kill-all-overlays)
13442               (if (gnus-request-article article group (current-buffer))
13443                   (progn
13444                     (and gnus-keep-backlog
13445                          (numberp article)
13446                          (gnus-backlog-enter-article
13447                           group article (current-buffer)))
13448                     'article))))
13449            ;; It was a pseudo.
13450            (t article)))
13451
13452       ;; Take the article from the original article buffer
13453       ;; and place it in the buffer it's supposed to be in.
13454       (when (and (get-buffer gnus-article-buffer)
13455                  (equal (buffer-name (current-buffer))
13456                         (buffer-name (get-buffer gnus-article-buffer))))
13457         (save-excursion
13458           (if (get-buffer gnus-original-article-buffer)
13459               (set-buffer (get-buffer gnus-original-article-buffer))
13460             (set-buffer (get-buffer-create gnus-original-article-buffer))
13461             (buffer-disable-undo (current-buffer))
13462             (setq major-mode 'gnus-original-article-mode)
13463             (setq buffer-read-only t)
13464             (gnus-add-current-to-buffer-list))
13465           (let (buffer-read-only)
13466             (erase-buffer)
13467             (insert-buffer-substring gnus-article-buffer))
13468           (setq gnus-original-article (cons group article))))
13469     
13470       ;; Update sparse articles.
13471       (when do-update-line
13472         (save-excursion
13473           (set-buffer gnus-summary-buffer)
13474           (gnus-summary-update-article do-update-line)
13475           (gnus-summary-goto-subject do-update-line)
13476           (set-window-point (get-buffer-window (current-buffer) t)
13477                             (point)))))))
13478
13479 (defun gnus-read-header (id &optional header)
13480   "Read the headers of article ID and enter them into the Gnus system."
13481   (let ((group gnus-newsgroup-name)
13482         (gnus-override-method 
13483          (and (gnus-news-group-p gnus-newsgroup-name)
13484               gnus-refer-article-method))       
13485         where)
13486     ;; First we check to see whether the header in question is already
13487     ;; fetched.
13488     (if (stringp id)
13489         ;; This is a Message-ID.
13490         (setq header (or header (gnus-id-to-header id)))
13491       ;; This is an article number.
13492       (setq header (or header (gnus-summary-article-header id))))
13493     (if (and header
13494              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13495         ;; We have found the header.
13496         header
13497       ;; We have to really fetch the header to this article.
13498       (when (setq where
13499                   (if (gnus-check-backend-function 'request-head group)
13500                       (gnus-request-head id group)
13501                     (gnus-request-article id group)))
13502         (save-excursion
13503           (set-buffer nntp-server-buffer)
13504           (and (search-forward "\n\n" nil t)
13505                (delete-region (1- (point)) (point-max)))
13506           (goto-char (point-max))
13507           (insert ".\n")
13508           (goto-char (point-min))
13509           (insert "211 ")
13510           (princ (cond
13511                   ((numberp id) id)
13512                   ((cdr where) (cdr where))
13513                   (header (mail-header-number header))
13514                   (t gnus-reffed-article-number))
13515                  (current-buffer))
13516           (insert " Article retrieved.\n"))
13517         ;(when (and header
13518         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13519         ;  (setcar (gnus-id-to-thread id) nil))
13520         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13521             ()                          ; Malformed head.
13522           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13523             (if (and (stringp id)
13524                      (not (string= (gnus-group-real-name group)
13525                                    (car where))))
13526                 ;; If we fetched by Message-ID and the article came
13527                 ;; from a different group, we fudge some bogus article
13528                 ;; numbers for this article.
13529                 (mail-header-set-number header gnus-reffed-article-number))
13530             (decf gnus-reffed-article-number)
13531             (push header gnus-newsgroup-headers)
13532             (setq gnus-current-headers header)
13533             (push (mail-header-number header) gnus-newsgroup-limit))
13534           header)))))
13535
13536 (defun gnus-article-prepare (article &optional all-headers header)
13537   "Prepare ARTICLE in article mode buffer.
13538 ARTICLE should either be an article number or a Message-ID.
13539 If ARTICLE is an id, HEADER should be the article headers.
13540 If ALL-HEADERS is non-nil, no headers are hidden."
13541   (save-excursion
13542     ;; Make sure we start in a summary buffer.
13543     (unless (eq major-mode 'gnus-summary-mode)
13544       (set-buffer gnus-summary-buffer))
13545     (setq gnus-summary-buffer (current-buffer))
13546     ;; Make sure the connection to the server is alive.
13547     (unless (gnus-server-opened
13548              (gnus-find-method-for-group gnus-newsgroup-name))
13549       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13550       (gnus-request-group gnus-newsgroup-name t))
13551     (let* ((article (if header (mail-header-number header) article))
13552            (summary-buffer (current-buffer))
13553            (internal-hook gnus-article-internal-prepare-hook)
13554            (group gnus-newsgroup-name)
13555            result)
13556       (save-excursion
13557         (gnus-article-setup-buffer)
13558         (set-buffer gnus-article-buffer)
13559         ;; Deactivate active regions.
13560         (when (and (boundp 'transient-mark-mode)
13561                    transient-mark-mode)
13562           (setq mark-active nil))
13563         (if (not (setq result (let ((buffer-read-only nil))
13564                                 (gnus-request-article-this-buffer
13565                                  article group))))
13566             ;; There is no such article.
13567             (save-excursion
13568               (when (and (numberp article)
13569                          (not (memq article gnus-newsgroup-sparse)))
13570                 (setq gnus-article-current
13571                       (cons gnus-newsgroup-name article))
13572                 (set-buffer gnus-summary-buffer)
13573                 (setq gnus-current-article article)
13574                 (gnus-summary-mark-article article gnus-canceled-mark))
13575               (unless (memq article gnus-newsgroup-sparse)
13576                 (gnus-error
13577                  1 "No such article (may have expired or been canceled)")))
13578           (if (or (eq result 'pseudo) (eq result 'nneething))
13579               (progn
13580                 (save-excursion
13581                   (set-buffer summary-buffer)
13582                   (setq gnus-last-article gnus-current-article
13583                         gnus-newsgroup-history (cons gnus-current-article
13584                                                      gnus-newsgroup-history)
13585                         gnus-current-article 0
13586                         gnus-current-headers nil
13587                         gnus-article-current nil)
13588                   (if (eq result 'nneething)
13589                       (gnus-configure-windows 'summary)
13590                     (gnus-configure-windows 'article))
13591                   (gnus-set-global-variables))
13592                 (gnus-set-mode-line 'article))
13593             ;; The result from the `request' was an actual article -
13594             ;; or at least some text that is now displayed in the
13595             ;; article buffer.
13596             (if (and (numberp article)
13597                      (not (eq article gnus-current-article)))
13598                 ;; Seems like a new article has been selected.
13599                 ;; `gnus-current-article' must be an article number.
13600                 (save-excursion
13601                   (set-buffer summary-buffer)
13602                   (setq gnus-last-article gnus-current-article
13603                         gnus-newsgroup-history (cons gnus-current-article
13604                                                      gnus-newsgroup-history)
13605                         gnus-current-article article
13606                         gnus-current-headers
13607                         (gnus-summary-article-header gnus-current-article)
13608                         gnus-article-current
13609                         (cons gnus-newsgroup-name gnus-current-article))
13610                   (unless (vectorp gnus-current-headers)
13611                     (setq gnus-current-headers nil))
13612                   (gnus-summary-show-thread)
13613                   (run-hooks 'gnus-mark-article-hook)
13614                   (gnus-set-mode-line 'summary)
13615                   (and (gnus-visual-p 'article-highlight 'highlight)
13616                        (run-hooks 'gnus-visual-mark-article-hook))
13617                   ;; Set the global newsgroup variables here.
13618                   ;; Suggested by Jim Sisolak
13619                   ;; <sisolak@trans4.neep.wisc.edu>.
13620                   (gnus-set-global-variables)
13621                   (setq gnus-have-all-headers
13622                         (or all-headers gnus-show-all-headers))
13623                   (and gnus-use-cache
13624                        (vectorp (gnus-summary-article-header article))
13625                        (gnus-cache-possibly-enter-article
13626                         group article
13627                         (gnus-summary-article-header article)
13628                         (memq article gnus-newsgroup-marked)
13629                         (memq article gnus-newsgroup-dormant)
13630                         (memq article gnus-newsgroup-unreads)))))
13631             ;; Hooks for getting information from the article.
13632             ;; This hook must be called before being narrowed.
13633             (let (buffer-read-only)
13634               (run-hooks 'internal-hook)
13635               (run-hooks 'gnus-article-prepare-hook)
13636               ;; Decode MIME message.
13637               (if gnus-show-mime
13638                   (if (or (not gnus-strict-mime)
13639                           (gnus-fetch-field "Mime-Version"))
13640                       (funcall gnus-show-mime-method)
13641                     (funcall gnus-decode-encoded-word-method)))
13642               ;; Perform the article display hooks.
13643               (run-hooks 'gnus-article-display-hook))
13644             ;; Do page break.
13645             (goto-char (point-min))
13646             (and gnus-break-pages (gnus-narrow-to-page))
13647             (gnus-set-mode-line 'article)
13648             (gnus-configure-windows 'article)
13649             (goto-char (point-min))
13650             t))))))
13651
13652 (defun gnus-article-show-all-headers ()
13653   "Show all article headers in article mode buffer."
13654   (save-excursion
13655     (gnus-article-setup-buffer)
13656     (set-buffer gnus-article-buffer)
13657     (let ((buffer-read-only nil))
13658       (gnus-unhide-text (point-min) (point-max)))))
13659
13660 (defun gnus-article-hide-headers-if-wanted ()
13661   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13662 Provided for backwards compatibility."
13663   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13664       gnus-inhibit-hiding
13665       (gnus-article-hide-headers)))
13666
13667 (defsubst gnus-article-header-rank ()
13668   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13669   (let ((list gnus-sorted-header-list)
13670         (i 0))
13671     (while list
13672       (when (looking-at (car list))
13673         (setq list nil))
13674       (setq list (cdr list))
13675       (incf i))
13676     i))
13677
13678 (defun gnus-article-hide-headers (&optional arg delete)
13679   "Toggle whether to hide unwanted headers and possibly sort them as well.
13680 If given a negative prefix, always show; if given a positive prefix,
13681 always hide."
13682   (interactive "P")
13683   (unless (gnus-article-check-hidden-text 'headers arg)
13684     ;; This function might be inhibited.
13685     (unless gnus-inhibit-hiding
13686       (save-excursion
13687         (set-buffer gnus-article-buffer)
13688         (save-restriction
13689           (let ((buffer-read-only nil)
13690                 (props (nconc (list 'gnus-type 'headers)
13691                               gnus-hidden-properties))
13692                 (max (1+ (length gnus-sorted-header-list)))
13693                 (ignored (when (not (stringp gnus-visible-headers))
13694                            (cond ((stringp gnus-ignored-headers)
13695                                   gnus-ignored-headers)
13696                                  ((listp gnus-ignored-headers)
13697                                   (mapconcat 'identity gnus-ignored-headers
13698                                              "\\|")))))
13699                 (visible
13700                  (cond ((stringp gnus-visible-headers)
13701                         gnus-visible-headers)
13702                        ((and gnus-visible-headers
13703                              (listp gnus-visible-headers))
13704                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13705                 want-list beg)
13706             ;; First we narrow to just the headers.
13707             (widen)
13708             (goto-char (point-min))
13709             ;; Hide any "From " lines at the beginning of (mail) articles.
13710             (while (looking-at "From ")
13711               (forward-line 1))
13712             (unless (bobp)
13713               (gnus-hide-text (point-min) (point) props))
13714             ;; Then treat the rest of the header lines.
13715             (narrow-to-region
13716              (point)
13717              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13718             ;; Then we use the two regular expressions
13719             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13720             ;; select which header lines is to remain visible in the
13721             ;; article buffer.
13722             (goto-char (point-min))
13723             (while (re-search-forward "^[^ \t]*:" nil t)
13724               (beginning-of-line)
13725               ;; We add the headers we want to keep to a list and delete
13726               ;; them from the buffer.
13727               (put-text-property 
13728                (point) (1+ (point)) 'message-rank
13729                (if (or (and visible (looking-at visible))
13730                        (and ignored
13731                             (not (looking-at ignored))))
13732                    (gnus-article-header-rank) 
13733                  (+ 2 max)))
13734               (forward-line 1))
13735             (message-sort-headers-1)
13736             (when (setq beg (text-property-any 
13737                              (point-min) (point-max) 'message-rank (+ 2 max)))
13738               ;; We make the unwanted headers invisible.
13739               (if delete
13740                   (delete-region beg (point-max))
13741                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13742                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13743
13744 (defun gnus-article-hide-boring-headers (&optional arg)
13745   "Toggle hiding of headers that aren't very interesting.
13746 If given a negative prefix, always show; if given a positive prefix,
13747 always hide."
13748   (interactive "P")
13749   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13750     (save-excursion
13751       (set-buffer gnus-article-buffer)
13752       (save-restriction
13753         (let ((buffer-read-only nil)
13754               (list gnus-boring-article-headers)
13755               (inhibit-point-motion-hooks t)
13756               elem)
13757           (nnheader-narrow-to-headers)
13758           (while list
13759             (setq elem (pop list))
13760             (goto-char (point-min))
13761             (cond
13762              ;; Hide empty headers.
13763              ((eq elem 'empty)
13764               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13765                 (forward-line -1)
13766                 (gnus-hide-text-type
13767                  (progn (beginning-of-line) (point))
13768                  (progn 
13769                    (end-of-line)
13770                    (if (re-search-forward "^[^ \t]" nil t)
13771                        (match-beginning 0)
13772                      (point-max)))
13773                  'boring-headers)))
13774              ;; Hide boring Newsgroups header.
13775              ((eq elem 'newsgroups)
13776               (when (equal (mail-fetch-field "newsgroups")
13777                            (gnus-group-real-name gnus-newsgroup-name))
13778                 (gnus-article-hide-header "newsgroups")))
13779              ((eq elem 'followup-to)
13780               (when (equal (mail-fetch-field "followup-to")
13781                            (mail-fetch-field "newsgroups"))
13782                 (gnus-article-hide-header "followup-to")))
13783              ((eq elem 'reply-to)
13784               (let ((from (mail-fetch-field "from"))
13785                     (reply-to (mail-fetch-field "reply-to")))
13786                 (when (and
13787                        from reply-to
13788                        (equal 
13789                         (nth 1 (funcall gnus-extract-address-components from))
13790                         (nth 1 (funcall gnus-extract-address-components
13791                                         reply-to))))
13792                   (gnus-article-hide-header "reply-to"))))
13793              ((eq elem 'date)
13794               (let ((date (mail-fetch-field "date")))
13795                 (when (and date
13796                            (< (gnus-days-between date (current-time-string))
13797                               4))
13798                   (gnus-article-hide-header "date")))))))))))
13799
13800 (defun gnus-article-hide-header (header)
13801   (save-excursion
13802     (goto-char (point-min))
13803     (when (re-search-forward (concat "^" header ":") nil t)
13804       (gnus-hide-text-type
13805        (progn (beginning-of-line) (point))
13806        (progn 
13807          (end-of-line)
13808          (if (re-search-forward "^[^ \t]" nil t)
13809              (match-beginning 0)
13810            (point-max)))
13811        'boring-headers))))
13812
13813 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13814 (defun gnus-article-treat-overstrike ()
13815   "Translate overstrikes into bold text."
13816   (interactive)
13817   (save-excursion
13818     (set-buffer gnus-article-buffer)
13819     (let ((buffer-read-only nil))
13820       (while (search-forward "\b" nil t)
13821         (let ((next (following-char))
13822               (previous (char-after (- (point) 2))))
13823           (cond ((eq next previous)
13824                  (put-text-property (- (point) 2) (point) 'invisible t)
13825                  (put-text-property (point) (1+ (point)) 'face 'bold))
13826                 ((eq next ?_)
13827                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13828                  (put-text-property
13829                   (- (point) 2) (1- (point)) 'face 'underline))
13830                 ((eq previous ?_)
13831                  (put-text-property (- (point) 2) (point) 'invisible t)
13832                  (put-text-property
13833                   (point) (1+ (point))  'face 'underline))))))))
13834
13835 (defun gnus-article-word-wrap ()
13836   "Format too long lines."
13837   (interactive)
13838   (save-excursion
13839     (set-buffer gnus-article-buffer)
13840     (let ((buffer-read-only nil))
13841       (widen)
13842       (goto-char (point-min))
13843       (search-forward "\n\n" nil t)
13844       (end-of-line 1)
13845       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13846             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13847             (adaptive-fill-mode t))
13848         (while (not (eobp))
13849           (and (>= (current-column) (min fill-column (window-width)))
13850                (/= (preceding-char) ?:)
13851                (fill-paragraph nil))
13852           (end-of-line 2))))))
13853
13854 (defun gnus-article-remove-cr ()
13855   "Remove carriage returns from an article."
13856   (interactive)
13857   (save-excursion
13858     (set-buffer gnus-article-buffer)
13859     (let ((buffer-read-only nil))
13860       (goto-char (point-min))
13861       (while (search-forward "\r" nil t)
13862         (replace-match "" t t)))))
13863
13864 (defun gnus-article-remove-trailing-blank-lines ()
13865   "Remove all trailing blank lines from the article."
13866   (interactive)
13867   (save-excursion
13868     (set-buffer gnus-article-buffer)
13869     (let ((buffer-read-only nil))
13870       (goto-char (point-max))
13871       (delete-region
13872        (point)
13873        (progn
13874          (while (looking-at "^[ \t]*$")
13875            (forward-line -1))
13876          (forward-line 1)
13877          (point))))))
13878
13879 (defun gnus-article-display-x-face (&optional force)
13880   "Look for an X-Face header and display it if present."
13881   (interactive (list 'force))
13882   (save-excursion
13883     (set-buffer gnus-article-buffer)
13884     ;; Delete the old process, if any.
13885     (when (process-status "gnus-x-face")
13886       (delete-process "gnus-x-face"))
13887     (let ((inhibit-point-motion-hooks t)
13888           (case-fold-search nil)
13889           from)
13890       (save-restriction
13891         (nnheader-narrow-to-headers)
13892         (setq from (mail-fetch-field "from"))
13893         (goto-char (point-min))
13894         (when (and gnus-article-x-face-command
13895                    (or force
13896                        ;; Check whether this face is censored.
13897                        (not gnus-article-x-face-too-ugly)
13898                        (and gnus-article-x-face-too-ugly from
13899                             (not (string-match gnus-article-x-face-too-ugly
13900                                                from))))
13901                    ;; Has to be present.
13902                    (re-search-forward "^X-Face: " nil t))
13903           ;; We now have the area of the buffer where the X-Face is stored.
13904           (let ((beg (point))
13905                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13906             ;; We display the face.
13907             (if (symbolp gnus-article-x-face-command)
13908                 ;; The command is a lisp function, so we call it.
13909                 (if (gnus-functionp gnus-article-x-face-command)
13910                     (funcall gnus-article-x-face-command beg end)
13911                   (error "%s is not a function" gnus-article-x-face-command))
13912               ;; The command is a string, so we interpret the command
13913               ;; as a, well, command, and fork it off.
13914               (let ((process-connection-type nil))
13915                 (process-kill-without-query
13916                  (start-process
13917                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13918                 (process-send-region "gnus-x-face" beg end)
13919                 (process-send-eof "gnus-x-face")))))))))
13920
13921 (defun gnus-headers-decode-quoted-printable ()
13922   "Hack to remove QP encoding from headers."
13923   (let ((case-fold-search t)
13924         (inhibit-point-motion-hooks t)
13925         string)
13926     (goto-char (point-min))
13927     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13928       (setq string (match-string 1))
13929       (narrow-to-region (match-beginning 0) (match-end 0))
13930       (delete-region (point-min) (point-max))
13931       (insert string)
13932       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13933       (subst-char-in-region (point-min) (point-max) ?_ ? )
13934       (widen)
13935       (goto-char (point-min)))))
13936
13937 (defun gnus-article-de-quoted-unreadable (&optional force)
13938   "Do a naive translation of a quoted-printable-encoded article.
13939 This is in no way, shape or form meant as a replacement for real MIME
13940 processing, but is simply a stop-gap measure until MIME support is
13941 written.
13942 If FORCE, decode the article whether it is marked as quoted-printable
13943 or not."
13944   (interactive (list 'force))
13945   (save-excursion
13946     (set-buffer gnus-article-buffer)
13947     (let ((case-fold-search t)
13948           (buffer-read-only nil)
13949           (type (gnus-fetch-field "content-transfer-encoding")))
13950       (gnus-headers-decode-quoted-printable)
13951       (when (or force
13952                 (and type (string-match "quoted-printable" (downcase type))))
13953         (goto-char (point-min))
13954         (search-forward "\n\n" nil 'move)
13955         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13956
13957 (defun gnus-mime-decode-quoted-printable (from to)
13958   "Decode Quoted-Printable in the region between FROM and TO."
13959   (interactive "r")
13960   (goto-char from)
13961   (while (search-forward "=" to t)
13962     (cond ((eq (following-char) ?\n)
13963            (delete-char -1)
13964            (delete-char 1))
13965           ((looking-at "[0-9A-F][0-9A-F]")
13966            (delete-char -1)
13967            (insert (hexl-hex-string-to-integer
13968                     (buffer-substring (point) (+ 2 (point)))))
13969            (delete-char 2))
13970           ((looking-at "=")
13971            (delete-char 1))
13972           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13973
13974 (defun gnus-article-hide-pgp (&optional arg)
13975   "Toggle hiding of any PGP headers and signatures in the current article.
13976 If given a negative prefix, always show; if given a positive prefix,
13977 always hide."
13978   (interactive "P")
13979   (unless (gnus-article-check-hidden-text 'pgp arg)
13980     (save-excursion
13981       (set-buffer gnus-article-buffer)
13982       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13983             buffer-read-only beg end)
13984         (widen)
13985         (goto-char (point-min))
13986         ;; Hide the "header".
13987         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13988              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13989         (setq beg (point))
13990         ;; Hide the actual signature.
13991         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13992              (setq end (1+ (match-beginning 0)))
13993              (gnus-hide-text
13994               end
13995               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13996                   (match-end 0)
13997                 ;; Perhaps we shouldn't hide to the end of the buffer
13998                 ;; if there is no end to the signature?
13999                 (point-max))
14000               props))
14001         ;; Hide "- " PGP quotation markers.
14002         (when (and beg end)
14003           (narrow-to-region beg end)
14004           (goto-char (point-min))
14005           (while (re-search-forward "^- " nil t)
14006             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14007           (widen))))))
14008
14009 (defun gnus-article-hide-signature (&optional arg)
14010   "Hide the signature in the current article.
14011 If given a negative prefix, always show; if given a positive prefix,
14012 always hide."
14013   (interactive "P")
14014   (unless (gnus-article-check-hidden-text 'signature arg)
14015     (save-excursion
14016       (set-buffer gnus-article-buffer)
14017       (save-restriction
14018         (let ((buffer-read-only nil))
14019           (when (gnus-narrow-to-signature)
14020             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14021
14022 (defun gnus-article-strip-leading-blank-lines ()
14023   "Remove all blank lines from the beginning of the article."
14024   (interactive)
14025   (save-excursion
14026     (set-buffer gnus-article-buffer)
14027     (let (buffer-read-only)
14028       (goto-char (point-min))
14029       (when (search-forward "\n\n" nil t)
14030         (while (looking-at "[ \t]$")
14031           (gnus-delete-line))))))
14032
14033 (defun gnus-narrow-to-signature ()
14034   "Narrow to the signature."
14035   (widen)
14036   (goto-char (point-max))
14037   (when (re-search-backward gnus-signature-separator nil t)
14038     (forward-line 1)
14039     (when (or (null gnus-signature-limit)
14040               (and (numberp gnus-signature-limit)
14041                    (< (- (point-max) (point)) gnus-signature-limit))
14042               (and (gnus-functionp gnus-signature-limit)
14043                    (funcall gnus-signature-limit))
14044               (and (stringp gnus-signature-limit)
14045                    (not (re-search-forward gnus-signature-limit nil t))))
14046       (narrow-to-region (point) (point-max))
14047       t)))
14048
14049 (defun gnus-article-check-hidden-text (type arg)
14050   "Return nil if hiding is necessary."
14051   (save-excursion
14052     (set-buffer gnus-article-buffer)
14053     (let ((hide (gnus-article-hidden-text-p type)))
14054       (cond ((or (and (null arg) (eq hide 'hidden))
14055                  (and arg (< 0 (prefix-numeric-value arg))))
14056              (gnus-article-show-hidden-text type))
14057             ((eq hide 'shown)
14058              (gnus-article-show-hidden-text type t))
14059             (t nil)))))
14060
14061 (defun gnus-article-hidden-text-p (type)
14062   "Say whether the current buffer contains hidden text of type TYPE."
14063   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14064     (when pos
14065       (if (get-text-property pos 'invisible)
14066           'hidden
14067         'shown))))
14068
14069 (defun gnus-article-hide (&optional arg force)
14070   "Hide all the gruft in the current article.
14071 This means that PGP stuff, signatures, cited text and (some)
14072 headers will be hidden.
14073 If given a prefix, show the hidden text instead."
14074   (interactive (list current-prefix-arg 'force))
14075   (gnus-article-hide-headers arg)
14076   (gnus-article-hide-pgp arg)
14077   (gnus-article-hide-citation-maybe arg force)
14078   (gnus-article-hide-signature arg))
14079
14080 (defun gnus-article-show-hidden-text (type &optional hide)
14081   "Show all hidden text of type TYPE.
14082 If HIDE, hide the text instead."
14083   (save-excursion
14084     (set-buffer gnus-article-buffer)
14085     (let ((buffer-read-only nil)
14086           (inhibit-point-motion-hooks t)
14087           (beg (point-min)))
14088       (while (gnus-goto-char (text-property-any
14089                               beg (point-max) 'gnus-type type))
14090         (setq beg (point))
14091         (forward-char)
14092         (if hide
14093             (gnus-hide-text beg (point) gnus-hidden-properties)
14094           (gnus-unhide-text beg (point)))
14095         (setq beg (point)))
14096       t)))
14097
14098 (defvar gnus-article-time-units
14099   `((year . ,(* 365.25 24 60 60))
14100     (week . ,(* 7 24 60 60))
14101     (day . ,(* 24 60 60))
14102     (hour . ,(* 60 60))
14103     (minute . 60)
14104     (second . 1))
14105   "Mapping from time units to seconds.")
14106
14107 (defun gnus-article-date-ut (&optional type highlight)
14108   "Convert DATE date to universal time in the current article.
14109 If TYPE is `local', convert to local time; if it is `lapsed', output
14110 how much time has lapsed since DATE."
14111   (interactive (list 'ut t))
14112   (let* ((header (or gnus-current-headers
14113                      (gnus-summary-article-header) ""))
14114          (date (and (vectorp header) (mail-header-date header)))
14115          (date-regexp "^Date: \\|^X-Sent: ")
14116          (now (current-time))
14117          (inhibit-point-motion-hooks t)
14118          bface eface)
14119     (when (and date (not (string= date "")))
14120       (save-excursion
14121         (set-buffer gnus-article-buffer)
14122         (save-restriction
14123           (nnheader-narrow-to-headers)
14124           (let ((buffer-read-only nil))
14125             ;; Delete any old Date headers.
14126             (if (re-search-forward date-regexp nil t)
14127                 (progn
14128                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14129                         eface (get-text-property (gnus-point-at-eol) 'face))
14130                   (message-remove-header date-regexp t)
14131                   (beginning-of-line))
14132               (goto-char (point-max)))
14133             (insert
14134              (cond
14135               ;; Convert to the local timezone.  We have to slap a
14136               ;; `condition-case' round the calls to the timezone
14137               ;; functions since they aren't particularly resistant to
14138               ;; buggy dates.
14139               ((eq type 'local)
14140                (concat "Date: " (condition-case ()
14141                                     (timezone-make-date-arpa-standard date)
14142                                   (error date))
14143                        "\n"))
14144               ;; Convert to Universal Time.
14145               ((eq type 'ut)
14146                (concat "Date: "
14147                        (condition-case ()
14148                            (timezone-make-date-arpa-standard date nil "UT")
14149                          (error date))
14150                        "\n"))
14151               ;; Get the original date from the article.
14152               ((eq type 'original)
14153                (concat "Date: " date "\n"))
14154               ;; Do an X-Sent lapsed format.
14155               ((eq type 'lapsed)
14156                ;; If the date is seriously mangled, the timezone
14157                ;; functions are liable to bug out, so we condition-case
14158                ;; the entire thing.
14159                (let* ((real-time
14160                        (condition-case ()
14161                            (gnus-time-minus
14162                             (gnus-encode-date
14163                              (timezone-make-date-arpa-standard
14164                               (current-time-string now)
14165                               (current-time-zone now) "UT"))
14166                             (gnus-encode-date
14167                              (timezone-make-date-arpa-standard
14168                               date nil "UT")))
14169                          (error '(0 0))))
14170                       (real-sec (+ (* (float (car real-time)) 65536)
14171                                    (cadr real-time)))
14172                       (sec (abs real-sec))
14173                       num prev)
14174                  (if (zerop sec)
14175                      "X-Sent: Now\n"
14176                    (concat
14177                     "X-Sent: "
14178                     ;; This is a bit convoluted, but basically we go
14179                     ;; through the time units for years, weeks, etc,
14180                     ;; and divide things to see whether that results
14181                     ;; in positive answers.
14182                     (mapconcat
14183                      (lambda (unit)
14184                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14185                            ;; The (remaining) seconds are too few to
14186                            ;; be divided into this time unit.
14187                            ""
14188                          ;; It's big enough, so we output it.
14189                          (setq sec (- sec (* num (cdr unit))))
14190                          (prog1
14191                              (concat (if prev ", " "") (int-to-string
14192                                                         (floor num))
14193                                      " " (symbol-name (car unit))
14194                                      (if (> num 1) "s" ""))
14195                            (setq prev t))))
14196                      gnus-article-time-units "")
14197                     ;; If dates are odd, then it might appear like the
14198                     ;; article was sent in the future.
14199                     (if (> real-sec 0)
14200                         " ago\n"
14201                       " in the future\n")))))
14202               (t
14203                (error "Unknown conversion type: %s" type)))))
14204           ;; Do highlighting.
14205           (beginning-of-line)
14206           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14207                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14208             (put-text-property (match-beginning 1) (match-end 1)
14209                                'face bface)
14210             (put-text-property (match-beginning 2) (match-end 2)
14211                                'face eface)))))))
14212
14213 (defun gnus-article-date-local (&optional highlight)
14214   "Convert the current article date to the local timezone."
14215   (interactive (list t))
14216   (gnus-article-date-ut 'local highlight))
14217
14218 (defun gnus-article-date-original (&optional highlight)
14219   "Convert the current article date to what it was originally.
14220 This is only useful if you have used some other date conversion
14221 function and want to see what the date was before converting."
14222   (interactive (list t))
14223   (gnus-article-date-ut 'original highlight))
14224
14225 (defun gnus-article-date-lapsed (&optional highlight)
14226   "Convert the current article date to time lapsed since it was sent."
14227   (interactive (list t))
14228   (gnus-article-date-ut 'lapsed highlight))
14229
14230 (defun gnus-article-maybe-highlight ()
14231   "Do some article highlighting if `gnus-visual' is non-nil."
14232   (if (gnus-visual-p 'article-highlight 'highlight)
14233       (gnus-article-highlight-some)))
14234
14235 ;; Article savers.
14236
14237 (defun gnus-output-to-rmail (file-name)
14238   "Append the current article to an Rmail file named FILE-NAME."
14239   (require 'rmail)
14240   ;; Most of these codes are borrowed from rmailout.el.
14241   (setq file-name (expand-file-name file-name))
14242   (setq rmail-default-rmail-file file-name)
14243   (let ((artbuf (current-buffer))
14244         (tmpbuf (get-buffer-create " *Gnus-output*")))
14245     (save-excursion
14246       (or (get-file-buffer file-name)
14247           (file-exists-p file-name)
14248           (if (gnus-yes-or-no-p
14249                (concat "\"" file-name "\" does not exist, create it? "))
14250               (let ((file-buffer (create-file-buffer file-name)))
14251                 (save-excursion
14252                   (set-buffer file-buffer)
14253                   (rmail-insert-rmail-file-header)
14254                   (let ((require-final-newline nil))
14255                     (write-region (point-min) (point-max) file-name t 1)))
14256                 (kill-buffer file-buffer))
14257             (error "Output file does not exist")))
14258       (set-buffer tmpbuf)
14259       (buffer-disable-undo (current-buffer))
14260       (erase-buffer)
14261       (insert-buffer-substring artbuf)
14262       (gnus-convert-article-to-rmail)
14263       ;; Decide whether to append to a file or to an Emacs buffer.
14264       (let ((outbuf (get-file-buffer file-name)))
14265         (if (not outbuf)
14266             (append-to-file (point-min) (point-max) file-name)
14267           ;; File has been visited, in buffer OUTBUF.
14268           (set-buffer outbuf)
14269           (let ((buffer-read-only nil)
14270                 (msg (and (boundp 'rmail-current-message)
14271                           (symbol-value 'rmail-current-message))))
14272             ;; If MSG is non-nil, buffer is in RMAIL mode.
14273             (if msg
14274                 (progn (widen)
14275                        (narrow-to-region (point-max) (point-max))))
14276             (insert-buffer-substring tmpbuf)
14277             (if msg
14278                 (progn
14279                   (goto-char (point-min))
14280                   (widen)
14281                   (search-backward "\^_")
14282                   (narrow-to-region (point) (point-max))
14283                   (goto-char (1+ (point-min)))
14284                   (rmail-count-new-messages t)
14285                   (rmail-show-message msg)))))))
14286     (kill-buffer tmpbuf)))
14287
14288 (defun gnus-output-to-file (file-name)
14289   "Append the current article to a file named FILE-NAME."
14290   (setq file-name (expand-file-name file-name))
14291   (let ((artbuf (current-buffer))
14292         (tmpbuf (get-buffer-create " *Gnus-output*")))
14293     (save-excursion
14294       (set-buffer tmpbuf)
14295       (buffer-disable-undo (current-buffer))
14296       (erase-buffer)
14297       (insert-buffer-substring artbuf)
14298       ;; Append newline at end of the buffer as separator, and then
14299       ;; save it to file.
14300       (goto-char (point-max))
14301       (insert "\n")
14302       (append-to-file (point-min) (point-max) file-name))
14303     (kill-buffer tmpbuf)))
14304
14305 (defun gnus-convert-article-to-rmail ()
14306   "Convert article in current buffer to Rmail message format."
14307   (let ((buffer-read-only nil))
14308     ;; Convert article directly into Babyl format.
14309     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14310     (goto-char (point-min))
14311     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14312     (while (search-forward "\n\^_" nil t) ;single char
14313       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14314     (goto-char (point-max))
14315     (insert "\^_")))
14316
14317 (defun gnus-narrow-to-page (&optional arg)
14318   "Narrow the article buffer to a page.
14319 If given a numerical ARG, move forward ARG pages."
14320   (interactive "P")
14321   (setq arg (if arg (prefix-numeric-value arg) 0))
14322   (save-excursion
14323     (set-buffer gnus-article-buffer)
14324     (goto-char (point-min))
14325     (widen)
14326     (when (gnus-visual-p 'page-marker)
14327       (let ((buffer-read-only nil))
14328         (gnus-remove-text-with-property 'gnus-prev)
14329         (gnus-remove-text-with-property 'gnus-next)))
14330     (when
14331         (cond ((< arg 0)
14332                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14333               ((> arg 0)
14334                (re-search-forward page-delimiter nil 'move arg)))
14335       (goto-char (match-end 0)))
14336     (narrow-to-region
14337      (point)
14338      (if (re-search-forward page-delimiter nil 'move)
14339          (match-beginning 0)
14340        (point)))
14341     (when (and (gnus-visual-p 'page-marker)
14342                (not (= (point-min) 1)))
14343       (save-excursion
14344         (goto-char (point-min))
14345         (gnus-insert-prev-page-button)))
14346     (when (and (gnus-visual-p 'page-marker)
14347                (not (= (1- (point-max)) (buffer-size))))
14348       (save-excursion
14349         (goto-char (point-max))
14350         (gnus-insert-next-page-button)))))
14351
14352 ;; Article mode commands
14353
14354 (defun gnus-article-goto-next-page ()
14355   "Show the next page of the article."
14356   (interactive)
14357   (when (gnus-article-next-page)
14358     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14359
14360 (defun gnus-article-goto-prev-page ()
14361   "Show the next page of the article."
14362   (interactive)
14363   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14364     (gnus-article-prev-page nil)))
14365
14366 (defun gnus-article-next-page (&optional lines)
14367   "Show the next page of the current article.
14368 If end of article, return non-nil.  Otherwise return nil.
14369 Argument LINES specifies lines to be scrolled up."
14370   (interactive "p")
14371   (move-to-window-line -1)
14372   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14373   (if (save-excursion
14374         (end-of-line)
14375         (and (pos-visible-in-window-p)  ;Not continuation line.
14376              (eobp)))
14377       ;; Nothing in this page.
14378       (if (or (not gnus-break-pages)
14379               (save-excursion
14380                 (save-restriction
14381                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14382           t                             ;Nothing more.
14383         (gnus-narrow-to-page 1)         ;Go to next page.
14384         nil)
14385     ;; More in this page.
14386     (condition-case ()
14387         (scroll-up lines)
14388       (end-of-buffer
14389        ;; Long lines may cause an end-of-buffer error.
14390        (goto-char (point-max))))
14391     (move-to-window-line 0)
14392     nil))
14393
14394 (defun gnus-article-prev-page (&optional lines)
14395   "Show previous page of current article.
14396 Argument LINES specifies lines to be scrolled down."
14397   (interactive "p")
14398   (move-to-window-line 0)
14399   (if (and gnus-break-pages
14400            (bobp)
14401            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14402       (progn
14403         (gnus-narrow-to-page -1)        ;Go to previous page.
14404         (goto-char (point-max))
14405         (recenter -1))
14406     (prog1
14407         (condition-case ()
14408             (scroll-down lines)
14409           (error nil))
14410       (move-to-window-line 0))))
14411
14412 (defun gnus-article-refer-article ()
14413   "Read article specified by message-id around point."
14414   (interactive)
14415   (let ((point (point)))
14416     (search-forward ">" nil t)          ;Move point to end of "<....>".
14417     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14418         (let ((message-id (match-string 1)))
14419           (goto-char point)
14420           (set-buffer gnus-summary-buffer)
14421           (gnus-summary-refer-article message-id))
14422       (goto-char (point))
14423       (error "No references around point"))))
14424
14425 (defun gnus-article-show-summary ()
14426   "Reconfigure windows to show summary buffer."
14427   (interactive)
14428   (gnus-configure-windows 'article)
14429   (gnus-summary-goto-subject gnus-current-article))
14430
14431 (defun gnus-article-describe-briefly ()
14432   "Describe article mode commands briefly."
14433   (interactive)
14434   (gnus-message 6
14435                 (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")))
14436
14437 (defun gnus-article-summary-command ()
14438   "Execute the last keystroke in the summary buffer."
14439   (interactive)
14440   (let ((obuf (current-buffer))
14441         (owin (current-window-configuration))
14442         func)
14443     (switch-to-buffer gnus-summary-buffer 'norecord)
14444     (setq func (lookup-key (current-local-map) (this-command-keys)))
14445     (call-interactively func)
14446     (set-buffer obuf)
14447     (set-window-configuration owin)
14448     (set-window-point (get-buffer-window (current-buffer)) (point))))
14449
14450 (defun gnus-article-summary-command-nosave ()
14451   "Execute the last keystroke in the summary buffer."
14452   (interactive)
14453   (let (func)
14454     (pop-to-buffer gnus-summary-buffer 'norecord)
14455     (setq func (lookup-key (current-local-map) (this-command-keys)))
14456     (call-interactively func)))
14457
14458 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14459   "Read a summary buffer key sequence and execute it from the article buffer."
14460   (interactive "P")
14461   (let ((nosaves
14462          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14463            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14464            "=" "^" "\M-^"))
14465         keys)
14466     (save-excursion
14467       (set-buffer gnus-summary-buffer)
14468       (push (or key last-command-event) unread-command-events)
14469       (setq keys (read-key-sequence nil)))
14470     (message "")
14471
14472     (if (member keys nosaves)
14473         (let (func)
14474           (pop-to-buffer gnus-summary-buffer 'norecord)
14475           (if (setq func (lookup-key (current-local-map) keys))
14476               (call-interactively func)
14477             (ding)))
14478       (let ((obuf (current-buffer))
14479             (owin (current-window-configuration))
14480             (opoint (point))
14481             func in-buffer)
14482         (if not-restore-window
14483             (pop-to-buffer gnus-summary-buffer 'norecord)
14484           (switch-to-buffer gnus-summary-buffer 'norecord))
14485         (setq in-buffer (current-buffer))
14486         (if (setq func (lookup-key (current-local-map) keys))
14487             (call-interactively func)
14488           (ding))
14489         (when (eq in-buffer (current-buffer))
14490           (set-buffer obuf)
14491           (unless not-restore-window
14492             (set-window-configuration owin))
14493           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14494
14495 \f
14496 ;;;
14497 ;;; Kill file handling.
14498 ;;;
14499
14500 ;;;###autoload
14501 (defalias 'gnus-batch-kill 'gnus-batch-score)
14502 ;;;###autoload
14503 (defun gnus-batch-score ()
14504   "Run batched scoring.
14505 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14506 Newsgroups is a list of strings in Bnews format.  If you want to score
14507 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14508 score the alt hierarchy, you'd say \"!alt.all\"."
14509   (interactive)
14510   (let* ((yes-and-no
14511           (gnus-newsrc-parse-options
14512            (apply (function concat)
14513                   (mapcar (lambda (g) (concat g " "))
14514                           command-line-args-left))))
14515          (gnus-expert-user t)
14516          (nnmail-spool-file nil)
14517          (gnus-use-dribble-file nil)
14518          (yes (car yes-and-no))
14519          (no (cdr yes-and-no))
14520          group newsrc entry
14521          ;; Disable verbose message.
14522          gnus-novice-user gnus-large-newsgroup)
14523     ;; Eat all arguments.
14524     (setq command-line-args-left nil)
14525     ;; Start Gnus.
14526     (gnus)
14527     ;; Apply kills to specified newsgroups in command line arguments.
14528     (setq newsrc (cdr gnus-newsrc-alist))
14529     (while newsrc
14530       (setq group (caar newsrc))
14531       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14532       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14533                (and (car entry)
14534                     (or (eq (car entry) t)
14535                         (not (zerop (car entry)))))
14536                (if yes (string-match yes group) t)
14537                (or (null no) (not (string-match no group))))
14538           (progn
14539             (gnus-summary-read-group group nil t nil t)
14540             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14541                  (gnus-summary-exit))))
14542       (setq newsrc (cdr newsrc)))
14543     ;; Exit Emacs.
14544     (switch-to-buffer gnus-group-buffer)
14545     (gnus-group-save-newsrc)))
14546
14547 (defun gnus-apply-kill-file ()
14548   "Apply a kill file to the current newsgroup.
14549 Returns the number of articles marked as read."
14550   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14551           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14552       (gnus-apply-kill-file-internal)
14553     0))
14554
14555 (defun gnus-kill-save-kill-buffer ()
14556   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14557     (when (get-file-buffer file)
14558       (save-excursion
14559         (set-buffer (get-file-buffer file))
14560         (and (buffer-modified-p) (save-buffer))
14561         (kill-buffer (current-buffer))))))
14562
14563 (defvar gnus-kill-file-name "KILL"
14564   "Suffix of the kill files.")
14565
14566 (defun gnus-newsgroup-kill-file (newsgroup)
14567   "Return the name of a kill file name for NEWSGROUP.
14568 If NEWSGROUP is nil, return the global kill file name instead."
14569   (cond 
14570    ;; The global KILL file is placed at top of the directory.
14571    ((or (null newsgroup)
14572         (string-equal newsgroup ""))
14573     (expand-file-name gnus-kill-file-name
14574                       (or gnus-kill-files-directory "~/News")))
14575    ;; Append ".KILL" to newsgroup name.
14576    ((gnus-use-long-file-name 'not-kill)
14577     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14578                               "." gnus-kill-file-name)
14579                       (or gnus-kill-files-directory "~/News")))
14580    ;; Place "KILL" under the hierarchical directory.
14581    (t
14582     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14583                               "/" gnus-kill-file-name)
14584                       (or gnus-kill-files-directory "~/News")))))
14585
14586 \f
14587 ;;;
14588 ;;; Dribble file
14589 ;;;
14590
14591 (defvar gnus-dribble-ignore nil)
14592 (defvar gnus-dribble-eval-file nil)
14593
14594 (defun gnus-dribble-file-name ()
14595   "Return the dribble file for the current .newsrc."
14596   (concat
14597    (if gnus-dribble-directory
14598        (concat (file-name-as-directory gnus-dribble-directory)
14599                (file-name-nondirectory gnus-current-startup-file))
14600      gnus-current-startup-file)
14601    "-dribble"))
14602
14603 (defun gnus-dribble-enter (string)
14604   "Enter STRING into the dribble buffer."
14605   (if (and (not gnus-dribble-ignore)
14606            gnus-dribble-buffer
14607            (buffer-name gnus-dribble-buffer))
14608       (let ((obuf (current-buffer)))
14609         (set-buffer gnus-dribble-buffer)
14610         (insert string "\n")
14611         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14612         (set-buffer obuf))))
14613
14614 (defun gnus-dribble-read-file ()
14615   "Read the dribble file from disk."
14616   (let ((dribble-file (gnus-dribble-file-name)))
14617     (save-excursion
14618       (set-buffer (setq gnus-dribble-buffer
14619                         (get-buffer-create
14620                          (file-name-nondirectory dribble-file))))
14621       (gnus-add-current-to-buffer-list)
14622       (erase-buffer)
14623       (setq buffer-file-name dribble-file)
14624       (auto-save-mode t)
14625       (buffer-disable-undo (current-buffer))
14626       (bury-buffer (current-buffer))
14627       (set-buffer-modified-p nil)
14628       (let ((auto (make-auto-save-file-name))
14629             (gnus-dribble-ignore t))
14630         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14631           ;; Load whichever file is newest -- the auto save file
14632           ;; or the "real" file.
14633           (if (file-newer-than-file-p auto dribble-file)
14634               (insert-file-contents auto)
14635             (insert-file-contents dribble-file))
14636           (unless (zerop (buffer-size))
14637             (set-buffer-modified-p t))
14638           ;; Set the file modes to reflect the .newsrc file modes.
14639           (save-buffer)
14640           (when (file-exists-p gnus-current-startup-file)
14641             (set-file-modes dribble-file
14642                             (file-modes gnus-current-startup-file)))
14643           ;; Possibly eval the file later.
14644           (when (gnus-y-or-n-p
14645                  "Auto-save file exists.  Do you want to read it? ")
14646             (setq gnus-dribble-eval-file t)))))))
14647
14648 (defun gnus-dribble-eval-file ()
14649   (if (not gnus-dribble-eval-file)
14650       ()
14651     (setq gnus-dribble-eval-file nil)
14652     (save-excursion
14653       (let ((gnus-dribble-ignore t))
14654         (set-buffer gnus-dribble-buffer)
14655         (eval-buffer (current-buffer))))))
14656
14657 (defun gnus-dribble-delete-file ()
14658   (if (file-exists-p (gnus-dribble-file-name))
14659       (delete-file (gnus-dribble-file-name)))
14660   (if gnus-dribble-buffer
14661       (save-excursion
14662         (set-buffer gnus-dribble-buffer)
14663         (let ((auto (make-auto-save-file-name)))
14664           (if (file-exists-p auto)
14665               (delete-file auto))
14666           (erase-buffer)
14667           (set-buffer-modified-p nil)))))
14668
14669 (defun gnus-dribble-save ()
14670   (if (and gnus-dribble-buffer
14671            (buffer-name gnus-dribble-buffer))
14672       (save-excursion
14673         (set-buffer gnus-dribble-buffer)
14674         (save-buffer))))
14675
14676 (defun gnus-dribble-clear ()
14677   (save-excursion
14678     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14679         (progn
14680           (set-buffer gnus-dribble-buffer)
14681           (erase-buffer)
14682           (set-buffer-modified-p nil)
14683           (setq buffer-saved-size (buffer-size))))))
14684
14685 \f
14686 ;;;
14687 ;;; Server Communication
14688 ;;;
14689
14690 (defun gnus-start-news-server (&optional confirm)
14691   "Open a method for getting news.
14692 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14693   (let (how)
14694     (if gnus-current-select-method
14695         ;; Stream is already opened.
14696         nil
14697       ;; Open NNTP server.
14698       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14699       (if confirm
14700           (progn
14701             ;; Read server name with completion.
14702             (setq gnus-nntp-server
14703                   (completing-read "NNTP server: "
14704                                    (mapcar (lambda (server) (list server))
14705                                            (cons (list gnus-nntp-server)
14706                                                  gnus-secondary-servers))
14707                                    nil nil gnus-nntp-server))))
14708
14709       (if (and gnus-nntp-server
14710                (stringp gnus-nntp-server)
14711                (not (string= gnus-nntp-server "")))
14712           (setq gnus-select-method
14713                 (cond ((or (string= gnus-nntp-server "")
14714                            (string= gnus-nntp-server "::"))
14715                        (list 'nnspool (system-name)))
14716                       ((string-match "^:" gnus-nntp-server)
14717                        (list 'nnmh gnus-nntp-server
14718                              (list 'nnmh-directory
14719                                    (file-name-as-directory
14720                                     (expand-file-name
14721                                      (concat "~/" (substring
14722                                                    gnus-nntp-server 1)))))
14723                              (list 'nnmh-get-new-mail nil)))
14724                       (t
14725                        (list 'nntp gnus-nntp-server)))))
14726
14727       (setq how (car gnus-select-method))
14728       (cond ((eq how 'nnspool)
14729              (require 'nnspool)
14730              (gnus-message 5 "Looking up local news spool..."))
14731             ((eq how 'nnmh)
14732              (require 'nnmh)
14733              (gnus-message 5 "Looking up mh spool..."))
14734             (t
14735              (require 'nntp)))
14736       (setq gnus-current-select-method gnus-select-method)
14737       (run-hooks 'gnus-open-server-hook)
14738       (or
14739        ;; gnus-open-server-hook might have opened it
14740        (gnus-server-opened gnus-select-method)
14741        (gnus-open-server gnus-select-method)
14742        (gnus-y-or-n-p
14743         (format
14744          "%s (%s) open error: '%s'.     Continue? "
14745          (car gnus-select-method) (cadr gnus-select-method)
14746          (gnus-status-message gnus-select-method)))
14747        (gnus-error 1 "Couldn't open server on %s"
14748                    (nth 1 gnus-select-method))))))
14749
14750 (defun gnus-check-group (group)
14751   "Try to make sure that the server where GROUP exists is alive."
14752   (let ((method (gnus-find-method-for-group group)))
14753     (or (gnus-server-opened method)
14754         (gnus-open-server method))))
14755
14756 (defun gnus-check-server (&optional method silent)
14757   "Check whether the connection to METHOD is down.
14758 If METHOD is nil, use `gnus-select-method'.
14759 If it is down, start it up (again)."
14760   (let ((method (or method gnus-select-method)))
14761     ;; Transform virtual server names into select methods.
14762     (when (stringp method)
14763       (setq method (gnus-server-to-method method)))
14764     (if (gnus-server-opened method)
14765         ;; The stream is already opened.
14766         t
14767       ;; Open the server.
14768       (unless silent
14769         (gnus-message 5 "Opening %s server%s..." (car method)
14770                       (if (equal (nth 1 method) "") ""
14771                         (format " on %s" (nth 1 method)))))
14772       (run-hooks 'gnus-open-server-hook)
14773       (prog1
14774           (gnus-open-server method)
14775         (unless silent
14776           (message ""))))))
14777
14778 (defun gnus-get-function (method function)
14779   "Return a function symbol based on METHOD and FUNCTION."
14780   ;; Translate server names into methods.
14781   (unless method
14782     (error "Attempted use of a nil select method"))
14783   (when (stringp method)
14784     (setq method (gnus-server-to-method method)))
14785   (let ((func (intern (format "%s-%s" (car method) function))))
14786     ;; If the functions isn't bound, we require the backend in
14787     ;; question.
14788     (unless (fboundp func)
14789       (require (car method))
14790       (unless (fboundp func)
14791         ;; This backend doesn't implement this function.
14792         (error "No such function: %s" func)))
14793     func))
14794
14795 \f
14796 ;;;
14797 ;;; Interface functions to the backends.
14798 ;;;
14799
14800 (defun gnus-open-server (method)
14801   "Open a connection to METHOD."
14802   (when (stringp method)
14803     (setq method (gnus-server-to-method method)))
14804   (let ((elem (assoc method gnus-opened-servers)))
14805     ;; If this method was previously denied, we just return nil.
14806     (if (eq (nth 1 elem) 'denied)
14807         (progn
14808           (gnus-message 1 "Denied server")
14809           nil)
14810       ;; Open the server.
14811       (let ((result
14812              (funcall (gnus-get-function method 'open-server)
14813                       (nth 1 method) (nthcdr 2 method))))
14814         ;; If this hasn't been opened before, we add it to the list.
14815         (unless elem
14816           (setq elem (list method nil)
14817                 gnus-opened-servers (cons elem gnus-opened-servers)))
14818         ;; Set the status of this server.
14819         (setcar (cdr elem) (if result 'ok 'denied))
14820         ;; Return the result from the "open" call.
14821         result))))
14822
14823 (defun gnus-close-server (method)
14824   "Close the connection to METHOD."
14825   (when (stringp method)
14826     (setq method (gnus-server-to-method method)))
14827   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14828
14829 (defun gnus-request-list (method)
14830   "Request the active file from METHOD."
14831   (when (stringp method)
14832     (setq method (gnus-server-to-method method)))
14833   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14834
14835 (defun gnus-request-list-newsgroups (method)
14836   "Request the newsgroups file from METHOD."
14837   (when (stringp method)
14838     (setq method (gnus-server-to-method method)))
14839   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14840
14841 (defun gnus-request-newgroups (date method)
14842   "Request all new groups since DATE from METHOD."
14843   (when (stringp method)
14844     (setq method (gnus-server-to-method method)))
14845   (funcall (gnus-get-function method 'request-newgroups)
14846            date (nth 1 method)))
14847
14848 (defun gnus-server-opened (method)
14849   "Check whether a connection to METHOD has been opened."
14850   (when (stringp method)
14851     (setq method (gnus-server-to-method method)))
14852   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14853
14854 (defun gnus-status-message (method)
14855   "Return the status message from METHOD.
14856 If METHOD is a string, it is interpreted as a group name.   The method
14857 this group uses will be queried."
14858   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14859                   method)))
14860     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14861
14862 (defun gnus-request-group (group &optional dont-check method)
14863   "Request GROUP.  If DONT-CHECK, no information is required."
14864   (let ((method (or method (gnus-find-method-for-group group))))
14865     (when (stringp method)
14866       (setq method (gnus-server-to-method method)))
14867     (funcall (gnus-get-function method 'request-group)
14868              (gnus-group-real-name group) (nth 1 method) dont-check)))
14869
14870 (defun gnus-request-asynchronous (group &optional articles)
14871   "Request that GROUP behave asynchronously.
14872 ARTICLES is the `data' of the group."
14873   (let ((method (gnus-find-method-for-group group)))
14874     (funcall (gnus-get-function method 'request-asynchronous)
14875              (gnus-group-real-name group) (nth 1 method) articles)))
14876
14877 (defun gnus-list-active-group (group)
14878   "Request active information on GROUP."
14879   (let ((method (gnus-find-method-for-group group))
14880         (func 'list-active-group))
14881     (when (gnus-check-backend-function func group)
14882       (funcall (gnus-get-function method func)
14883                (gnus-group-real-name group) (nth 1 method)))))
14884
14885 (defun gnus-request-group-description (group)
14886   "Request a description of GROUP."
14887   (let ((method (gnus-find-method-for-group group))
14888         (func 'request-group-description))
14889     (when (gnus-check-backend-function func group)
14890       (funcall (gnus-get-function method func)
14891                (gnus-group-real-name group) (nth 1 method)))))
14892
14893 (defun gnus-close-group (group)
14894   "Request the GROUP be closed."
14895   (let ((method (gnus-find-method-for-group group)))
14896     (funcall (gnus-get-function method 'close-group)
14897              (gnus-group-real-name group) (nth 1 method))))
14898
14899 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14900   "Request headers for ARTICLES in GROUP.
14901 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14902   (let ((method (gnus-find-method-for-group group)))
14903     (if (and gnus-use-cache (numberp (car articles)))
14904         (gnus-cache-retrieve-headers articles group fetch-old)
14905       (funcall (gnus-get-function method 'retrieve-headers)
14906                articles (gnus-group-real-name group) (nth 1 method)
14907                fetch-old))))
14908
14909 (defun gnus-retrieve-groups (groups method)
14910   "Request active information on GROUPS from METHOD."
14911   (when (stringp method)
14912     (setq method (gnus-server-to-method method)))
14913   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14914
14915 (defun gnus-request-type (group &optional article)
14916   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14917   (let ((method (gnus-find-method-for-group group)))
14918     (if (not (gnus-check-backend-function 'request-type (car method)))
14919         'unknown
14920       (funcall (gnus-get-function method 'request-type)
14921                (gnus-group-real-name group) article))))
14922
14923 (defun gnus-request-update-mark (group article mark)
14924   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14925   (let ((method (gnus-find-method-for-group group)))
14926     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14927         mark
14928       (funcall (gnus-get-function method 'request-update-mark)
14929                (gnus-group-real-name group) article mark))))
14930
14931 (defun gnus-request-article (article group &optional buffer)
14932   "Request the ARTICLE in GROUP.
14933 ARTICLE can either be an article number or an article Message-ID.
14934 If BUFFER, insert the article in that group."
14935   (let ((method (gnus-find-method-for-group group)))
14936     (funcall (gnus-get-function method 'request-article)
14937              article (gnus-group-real-name group) (nth 1 method) buffer)))
14938
14939 (defun gnus-request-head (article group)
14940   "Request the head of ARTICLE in GROUP."
14941   (let ((method (gnus-find-method-for-group group)))
14942     (funcall (gnus-get-function method 'request-head)
14943              article (gnus-group-real-name group) (nth 1 method))))
14944
14945 (defun gnus-request-body (article group)
14946   "Request the body of ARTICLE in GROUP."
14947   (let ((method (gnus-find-method-for-group group)))
14948     (funcall (gnus-get-function method 'request-body)
14949              article (gnus-group-real-name group) (nth 1 method))))
14950
14951 (defun gnus-request-post (method)
14952   "Post the current buffer using METHOD."
14953   (when (stringp method)
14954     (setq method (gnus-server-to-method method)))
14955   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14956
14957 (defun gnus-request-scan (group method)
14958   "Request a SCAN being performed in GROUP from METHOD.
14959 If GROUP is nil, all groups on METHOD are scanned."
14960   (let ((method (if group (gnus-find-method-for-group group) method)))
14961     (funcall (gnus-get-function method 'request-scan)
14962              (and group (gnus-group-real-name group)) (nth 1 method))))
14963
14964 (defsubst gnus-request-update-info (info method)
14965   "Request that METHOD update INFO."
14966   (when (stringp method)
14967     (setq method (gnus-server-to-method method)))
14968   (when (gnus-check-backend-function 'request-update-info (car method))
14969     (funcall (gnus-get-function method 'request-update-info)
14970              (gnus-group-real-name (gnus-info-group info))
14971              info (nth 1 method))))
14972
14973 (defun gnus-request-expire-articles (articles group &optional force)
14974   (let ((method (gnus-find-method-for-group group)))
14975     (funcall (gnus-get-function method 'request-expire-articles)
14976              articles (gnus-group-real-name group) (nth 1 method)
14977              force)))
14978
14979 (defun gnus-request-move-article
14980   (article group server accept-function &optional last)
14981   (let ((method (gnus-find-method-for-group group)))
14982     (funcall (gnus-get-function method 'request-move-article)
14983              article (gnus-group-real-name group)
14984              (nth 1 method) accept-function last)))
14985
14986 (defun gnus-request-accept-article (group method &optional last)
14987   ;; Make sure there's a newline at the end of the article.
14988   (when (stringp method)
14989     (setq method (gnus-server-to-method method)))
14990   (when (and (not method)
14991              (stringp group))
14992     (setq method (gnus-find-method-for-group group)))
14993   (goto-char (point-max))
14994   (unless (bolp)
14995     (insert "\n"))
14996   (let ((func (car (or method (gnus-find-method-for-group group)))))
14997     (funcall (intern (format "%s-request-accept-article" func))
14998              (if (stringp group) (gnus-group-real-name group) group)
14999              (cadr method)
15000              last)))
15001
15002 (defun gnus-request-replace-article (article group buffer)
15003   (let ((func (car (gnus-find-method-for-group group))))
15004     (funcall (intern (format "%s-request-replace-article" func))
15005              article (gnus-group-real-name group) buffer)))
15006
15007 (defun gnus-request-associate-buffer (group)
15008   (let ((method (gnus-find-method-for-group group)))
15009     (funcall (gnus-get-function method 'request-associate-buffer)
15010              (gnus-group-real-name group))))
15011
15012 (defun gnus-request-restore-buffer (article group)
15013   "Request a new buffer restored to the state of ARTICLE."
15014   (let ((method (gnus-find-method-for-group group)))
15015     (funcall (gnus-get-function method 'request-restore-buffer)
15016              article (gnus-group-real-name group) (nth 1 method))))
15017
15018 (defun gnus-request-create-group (group &optional method)
15019   (when (stringp method)
15020     (setq method (gnus-server-to-method method)))
15021   (let ((method (or method (gnus-find-method-for-group group))))
15022     (funcall (gnus-get-function method 'request-create-group)
15023              (gnus-group-real-name group) (nth 1 method))))
15024
15025 (defun gnus-request-delete-group (group &optional force)
15026   (let ((method (gnus-find-method-for-group group)))
15027     (funcall (gnus-get-function method 'request-delete-group)
15028              (gnus-group-real-name group) force (nth 1 method))))
15029
15030 (defun gnus-request-rename-group (group new-name)
15031   (let ((method (gnus-find-method-for-group group)))
15032     (funcall (gnus-get-function method 'request-rename-group)
15033              (gnus-group-real-name group)
15034              (gnus-group-real-name new-name) (nth 1 method))))
15035
15036 (defun gnus-member-of-valid (symbol group)
15037   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15038   (memq symbol (assoc
15039                 (symbol-name (car (gnus-find-method-for-group group)))
15040                 gnus-valid-select-methods)))
15041
15042 (defun gnus-method-option-p (method option)
15043   "Return non-nil if select METHOD has OPTION as a parameter."
15044   (when (stringp method)
15045     (setq method (gnus-server-to-method method)))
15046   (memq option (assoc (format "%s" (car method))
15047                       gnus-valid-select-methods)))
15048
15049 (defun gnus-server-extend-method (group method)
15050   ;; This function "extends" a virtual server.  If the server is
15051   ;; "hello", and the select method is ("hello" (my-var "something"))
15052   ;; in the group "alt.alt", this will result in a new virtual server
15053   ;; called "hello+alt.alt".
15054   (let ((entry
15055          (gnus-copy-sequence
15056           (if (equal (car method) "native") gnus-select-method
15057             (cdr (assoc (car method) gnus-server-alist))))))
15058     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15059     (nconc entry (cdr method))))
15060
15061 (defun gnus-find-method-for-group (group &optional info)
15062   "Find the select method that GROUP uses."
15063   (or gnus-override-method
15064       (and (not group)
15065            gnus-select-method)
15066       (let ((info (or info (gnus-get-info group)))
15067             method)
15068         (if (or (not info)
15069                 (not (setq method (gnus-info-method info)))
15070                 (equal method "native"))
15071             gnus-select-method
15072           (setq method
15073                 (cond ((stringp method)
15074                        (gnus-server-to-method method))
15075                       ((stringp (car method))
15076                        (gnus-server-extend-method group method))
15077                       (t
15078                        method)))
15079           (cond ((equal (cadr method) "")
15080                  method)
15081                 ((null (cadr method))
15082                  (list (car method) ""))
15083                 (t
15084                  (gnus-server-add-address method)))))))
15085
15086 (defun gnus-check-backend-function (func group)
15087   "Check whether GROUP supports function FUNC."
15088   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15089                   group)))
15090     (fboundp (intern (format "%s-%s" method func)))))
15091
15092 (defun gnus-methods-using (feature)
15093   "Find all methods that have FEATURE."
15094   (let ((valids gnus-valid-select-methods)
15095         outs)
15096     (while valids
15097       (if (memq feature (car valids))
15098           (setq outs (cons (car valids) outs)))
15099       (setq valids (cdr valids)))
15100     outs))
15101
15102 \f
15103 ;;;
15104 ;;; Active & Newsrc File Handling
15105 ;;;
15106
15107 (defun gnus-setup-news (&optional rawfile level dont-connect)
15108   "Setup news information.
15109 If RAWFILE is non-nil, the .newsrc file will also be read.
15110 If LEVEL is non-nil, the news will be set up at level LEVEL."
15111   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15112
15113     (when init 
15114       ;; Clear some variables to re-initialize news information.
15115       (setq gnus-newsrc-alist nil
15116             gnus-active-hashtb nil)
15117       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15118       (gnus-read-newsrc-file rawfile))
15119
15120     (when (and (not (assoc "archive" gnus-server-alist))
15121                gnus-message-archive-method)
15122       (push (cons "archive" gnus-message-archive-method)
15123             gnus-server-alist))
15124
15125     ;; If we don't read the complete active file, we fill in the
15126     ;; hashtb here.
15127     (if (or (null gnus-read-active-file)
15128             (eq gnus-read-active-file 'some))
15129         (gnus-update-active-hashtb-from-killed))
15130
15131     ;; Read the active file and create `gnus-active-hashtb'.
15132     ;; If `gnus-read-active-file' is nil, then we just create an empty
15133     ;; hash table.  The partial filling out of the hash table will be
15134     ;; done in `gnus-get-unread-articles'.
15135     (and gnus-read-active-file
15136          (not level)
15137          (gnus-read-active-file))
15138
15139     (or gnus-active-hashtb
15140         (setq gnus-active-hashtb (make-vector 4095 0)))
15141
15142     ;; Initialize the cache.
15143     (when gnus-use-cache
15144       (gnus-cache-open))
15145
15146     ;; Possibly eval the dribble file.
15147     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15148
15149     (gnus-update-format-specifications)
15150
15151     ;; See whether we need to read the description file.
15152     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15153              (not gnus-description-hashtb)
15154              (not dont-connect)
15155              gnus-read-active-file)
15156         (gnus-read-all-descriptions-files))
15157
15158     ;; Find new newsgroups and treat them.
15159     (if (and init gnus-check-new-newsgroups (not level)
15160              (gnus-check-server gnus-select-method))
15161         (gnus-find-new-newsgroups))
15162
15163     ;; We might read in new NoCeM messages here.
15164     (when (and gnus-use-nocem 
15165                (not level)
15166                (not dont-connect))
15167       (gnus-nocem-scan-groups))
15168
15169     ;; Find the number of unread articles in each non-dead group.
15170     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15171       (gnus-get-unread-articles level))
15172
15173     (if (and init gnus-check-bogus-newsgroups
15174              gnus-read-active-file (not level)
15175              (gnus-server-opened gnus-select-method))
15176         (gnus-check-bogus-newsgroups))))
15177
15178 (defun gnus-find-new-newsgroups (&optional arg)
15179   "Search for new newsgroups and add them.
15180 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15181 The `-n' option line from .newsrc is respected.
15182 If ARG (the prefix), use the `ask-server' method to query
15183 the server for new groups."
15184   (interactive "P")
15185   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15186                        (null gnus-read-active-file)
15187                        (eq gnus-read-active-file 'some))
15188                    'ask-server gnus-check-new-newsgroups)))
15189     (unless (gnus-check-first-time-used)
15190       (if (or (consp check)
15191               (eq check 'ask-server))
15192           ;; Ask the server for new groups.
15193           (gnus-ask-server-for-new-groups)
15194         ;; Go through the active hashtb and look for new groups.
15195         (let ((groups 0)
15196               group new-newsgroups)
15197           (gnus-message 5 "Looking for new newsgroups...")
15198           (unless gnus-have-read-active-file
15199             (gnus-read-active-file))
15200           (setq gnus-newsrc-last-checked-date (current-time-string))
15201           (unless gnus-killed-hashtb
15202             (gnus-make-hashtable-from-killed))
15203           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15204           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15205           (mapatoms
15206            (lambda (sym)
15207              (if (or (null (setq group (symbol-name sym)))
15208                      (not (boundp sym))
15209                      (null (symbol-value sym))
15210                      (gnus-gethash group gnus-killed-hashtb)
15211                      (gnus-gethash group gnus-newsrc-hashtb))
15212                  ()
15213                (let ((do-sub (gnus-matches-options-n group)))
15214                  (cond
15215                   ((eq do-sub 'subscribe)
15216                    (setq groups (1+ groups))
15217                    (gnus-sethash group group gnus-killed-hashtb)
15218                    (funcall gnus-subscribe-options-newsgroup-method group))
15219                   ((eq do-sub 'ignore)
15220                    nil)
15221                   (t
15222                    (setq groups (1+ groups))
15223                    (gnus-sethash group group gnus-killed-hashtb)
15224                    (if gnus-subscribe-hierarchical-interactive
15225                        (setq new-newsgroups (cons group new-newsgroups))
15226                      (funcall gnus-subscribe-newsgroup-method group)))))))
15227            gnus-active-hashtb)
15228           (when new-newsgroups
15229             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15230           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15231           (if (> groups 0)
15232               (gnus-message 6 "%d new newsgroup%s arrived."
15233                             groups (if (> groups 1) "s have" " has"))
15234             (gnus-message 6 "No new newsgroups.")))))))
15235
15236 (defun gnus-matches-options-n (group)
15237   ;; Returns `subscribe' if the group is to be unconditionally
15238   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15239   ;; no match for the group.
15240
15241   ;; First we check the two user variables.
15242   (cond
15243    ((and gnus-options-subscribe
15244          (string-match gnus-options-subscribe group))
15245     'subscribe)
15246    ((and gnus-auto-subscribed-groups
15247          (string-match gnus-auto-subscribed-groups group))
15248     'subscribe)
15249    ((and gnus-options-not-subscribe
15250          (string-match gnus-options-not-subscribe group))
15251     'ignore)
15252    ;; Then we go through the list that was retrieved from the .newsrc
15253    ;; file.  This list has elements on the form
15254    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15255    ;; is in the reverse order of the options line) is returned.
15256    (t
15257     (let ((regs gnus-newsrc-options-n))
15258       (while (and regs
15259                   (not (string-match (caar regs) group)))
15260         (setq regs (cdr regs)))
15261       (and regs (cdar regs))))))
15262
15263 (defun gnus-ask-server-for-new-groups ()
15264   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15265          (methods (cons gnus-select-method
15266                         (nconc
15267                          (when gnus-message-archive-method
15268                            (list "archive"))
15269                          (append
15270                           (and (consp gnus-check-new-newsgroups)
15271                                gnus-check-new-newsgroups)
15272                           gnus-secondary-select-methods))))
15273          (groups 0)
15274          (new-date (current-time-string))
15275          group new-newsgroups got-new method hashtb
15276          gnus-override-subscribe-method)
15277     ;; Go through both primary and secondary select methods and
15278     ;; request new newsgroups.
15279     (while (setq method (gnus-server-get-method nil (pop methods)))
15280       (setq new-newsgroups nil)
15281       (setq gnus-override-subscribe-method method)
15282       (when (and (gnus-check-server method)
15283                  (gnus-request-newgroups date method))
15284         (save-excursion
15285           (setq got-new t)
15286           (setq hashtb (gnus-make-hashtable 100))
15287           (set-buffer nntp-server-buffer)
15288           ;; Enter all the new groups into a hashtable.
15289           (gnus-active-to-gnus-format method hashtb 'ignore))
15290         ;; Now all new groups from `method' are in `hashtb'.
15291         (mapatoms
15292          (lambda (group-sym)
15293            (if (or (null (setq group (symbol-name group-sym)))
15294                    (not (boundp group-sym))
15295                    (null (symbol-value group-sym))
15296                    (gnus-gethash group gnus-newsrc-hashtb)
15297                    (member group gnus-zombie-list)
15298                    (member group gnus-killed-list))
15299                ;; The group is already known.
15300                ()
15301              ;; Make this group active.
15302              (when (symbol-value group-sym)
15303                (gnus-set-active group (symbol-value group-sym)))
15304              ;; Check whether we want it or not.
15305              (let ((do-sub (gnus-matches-options-n group)))
15306                (cond
15307                 ((eq do-sub 'subscribe)
15308                  (incf groups)
15309                  (gnus-sethash group group gnus-killed-hashtb)
15310                  (funcall gnus-subscribe-options-newsgroup-method group))
15311                 ((eq do-sub 'ignore)
15312                  nil)
15313                 (t
15314                  (incf groups)
15315                  (gnus-sethash group group gnus-killed-hashtb)
15316                  (if gnus-subscribe-hierarchical-interactive
15317                      (push group new-newsgroups)
15318                    (funcall gnus-subscribe-newsgroup-method group)))))))
15319          hashtb))
15320       (when new-newsgroups
15321         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15322     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15323     (when (> groups 0)
15324       (gnus-message 6 "%d new newsgroup%s arrived."
15325                     groups (if (> groups 1) "s have" " has")))
15326     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15327     got-new))
15328
15329 (defun gnus-check-first-time-used ()
15330   (if (or (> (length gnus-newsrc-alist) 1)
15331           (file-exists-p gnus-startup-file)
15332           (file-exists-p (concat gnus-startup-file ".el"))
15333           (file-exists-p (concat gnus-startup-file ".eld")))
15334       nil
15335     (gnus-message 6 "First time user; subscribing you to default groups")
15336     (unless gnus-have-read-active-file
15337       (gnus-read-active-file))
15338     (setq gnus-newsrc-last-checked-date (current-time-string))
15339     (let ((groups gnus-default-subscribed-newsgroups)
15340           group)
15341       (if (eq groups t)
15342           nil
15343         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15344         (mapatoms
15345          (lambda (sym)
15346            (if (null (setq group (symbol-name sym)))
15347                ()
15348              (let ((do-sub (gnus-matches-options-n group)))
15349                (cond
15350                 ((eq do-sub 'subscribe)
15351                  (gnus-sethash group group gnus-killed-hashtb)
15352                  (funcall gnus-subscribe-options-newsgroup-method group))
15353                 ((eq do-sub 'ignore)
15354                  nil)
15355                 (t
15356                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15357          gnus-active-hashtb)
15358         (while groups
15359           (if (gnus-active (car groups))
15360               (gnus-group-change-level
15361                (car groups) gnus-level-default-subscribed gnus-level-killed))
15362           (setq groups (cdr groups)))
15363         (gnus-group-make-help-group)
15364         (and gnus-novice-user
15365              (gnus-message 7 "`A k' to list killed groups"))))))
15366
15367 (defun gnus-subscribe-group (group previous &optional method)
15368   (gnus-group-change-level
15369    (if method
15370        (list t group gnus-level-default-subscribed nil nil method)
15371      group)
15372    gnus-level-default-subscribed gnus-level-killed previous t))
15373
15374 ;; `gnus-group-change-level' is the fundamental function for changing
15375 ;; subscription levels of newsgroups.  This might mean just changing
15376 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15377 ;; again, which subscribes/unsubscribes a group, which is equally
15378 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15379 ;; from 8-9 to 1-7 means that you remove the group from the list of
15380 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15381 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15382 ;; which is trivial.
15383 ;; ENTRY can either be a string (newsgroup name) or a list (if
15384 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15385 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15386 ;; entries.
15387 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15388 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15389 ;; after.
15390 (defun gnus-group-change-level (entry level &optional oldlevel
15391                                       previous fromkilled)
15392   (let (group info active num)
15393     ;; Glean what info we can from the arguments
15394     (if (consp entry)
15395         (if fromkilled (setq group (nth 1 entry))
15396           (setq group (car (nth 2 entry))))
15397       (setq group entry))
15398     (if (and (stringp entry)
15399              oldlevel
15400              (< oldlevel gnus-level-zombie))
15401         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15402     (if (and (not oldlevel)
15403              (consp entry))
15404         (setq oldlevel (gnus-info-level (nth 2 entry)))
15405       (setq oldlevel (or oldlevel 9)))
15406     (if (stringp previous)
15407         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15408
15409     (if (and (>= oldlevel gnus-level-zombie)
15410              (gnus-gethash group gnus-newsrc-hashtb))
15411         ;; We are trying to subscribe a group that is already
15412         ;; subscribed.
15413         ()                              ; Do nothing.
15414
15415       (or (gnus-ephemeral-group-p group)
15416           (gnus-dribble-enter
15417            (format "(gnus-group-change-level %S %S %S %S %S)"
15418                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15419
15420       ;; Then we remove the newgroup from any old structures, if needed.
15421       ;; If the group was killed, we remove it from the killed or zombie
15422       ;; list.  If not, and it is in fact going to be killed, we remove
15423       ;; it from the newsrc hash table and assoc.
15424       (cond
15425        ((>= oldlevel gnus-level-zombie)
15426         (if (= oldlevel gnus-level-zombie)
15427             (setq gnus-zombie-list (delete group gnus-zombie-list))
15428           (setq gnus-killed-list (delete group gnus-killed-list))))
15429        (t
15430         (if (and (>= level gnus-level-zombie)
15431                  entry)
15432             (progn
15433               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15434               (if (nth 3 entry)
15435                   (setcdr (gnus-gethash (car (nth 3 entry))
15436                                         gnus-newsrc-hashtb)
15437                           (cdr entry)))
15438               (setcdr (cdr entry) (cdddr entry))))))
15439
15440       ;; Finally we enter (if needed) the list where it is supposed to
15441       ;; go, and change the subscription level.  If it is to be killed,
15442       ;; we enter it into the killed or zombie list.
15443       (cond 
15444        ((>= level gnus-level-zombie)
15445         ;; Remove from the hash table.
15446         (gnus-sethash group nil gnus-newsrc-hashtb)
15447         ;; We do not enter foreign groups into the list of dead
15448         ;; groups.
15449         (unless (gnus-group-foreign-p group)
15450           (if (= level gnus-level-zombie)
15451               (setq gnus-zombie-list (cons group gnus-zombie-list))
15452             (setq gnus-killed-list (cons group gnus-killed-list)))))
15453        (t
15454         ;; If the list is to be entered into the newsrc assoc, and
15455         ;; it was killed, we have to create an entry in the newsrc
15456         ;; hashtb format and fix the pointers in the newsrc assoc.
15457         (if (< oldlevel gnus-level-zombie)
15458             ;; It was alive, and it is going to stay alive, so we
15459             ;; just change the level and don't change any pointers or
15460             ;; hash table entries.
15461             (setcar (cdaddr entry) level)
15462           (if (listp entry)
15463               (setq info (cdr entry)
15464                     num (car entry))
15465             (setq active (gnus-active group))
15466             (setq num
15467                   (if active (- (1+ (cdr active)) (car active)) t))
15468             ;; Check whether the group is foreign.  If so, the
15469             ;; foreign select method has to be entered into the
15470             ;; info.
15471             (let ((method (or gnus-override-subscribe-method
15472                               (gnus-group-method group))))
15473               (if (eq method gnus-select-method)
15474                   (setq info (list group level nil))
15475                 (setq info (list group level nil nil method)))))
15476           (unless previous
15477             (setq previous
15478                   (let ((p gnus-newsrc-alist))
15479                     (while (cddr p)
15480                       (setq p (cdr p)))
15481                     p)))
15482           (setq entry (cons info (cddr previous)))
15483           (if (cdr previous)
15484               (progn
15485                 (setcdr (cdr previous) entry)
15486                 (gnus-sethash group (cons num (cdr previous))
15487                               gnus-newsrc-hashtb))
15488             (setcdr previous entry)
15489             (gnus-sethash group (cons num previous)
15490                           gnus-newsrc-hashtb))
15491           (when (cdr entry)
15492             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15493       (when gnus-group-change-level-function
15494         (funcall gnus-group-change-level-function group level oldlevel)))))
15495
15496 (defun gnus-kill-newsgroup (newsgroup)
15497   "Obsolete function.  Kills a newsgroup."
15498   (gnus-group-change-level
15499    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15500
15501 (defun gnus-check-bogus-newsgroups (&optional confirm)
15502   "Remove bogus newsgroups.
15503 If CONFIRM is non-nil, the user has to confirm the deletion of every
15504 newsgroup."
15505   (let ((newsrc (cdr gnus-newsrc-alist))
15506         bogus group entry info)
15507     (gnus-message 5 "Checking bogus newsgroups...")
15508     (unless gnus-have-read-active-file
15509       (gnus-read-active-file))
15510     (when (member gnus-select-method gnus-have-read-active-file)
15511       ;; Find all bogus newsgroup that are subscribed.
15512       (while newsrc
15513         (setq info (pop newsrc)
15514               group (gnus-info-group info))
15515         (unless (or (gnus-active group) ; Active
15516                     (gnus-info-method info) ; Foreign
15517                     (and confirm
15518                          (not (gnus-y-or-n-p
15519                                (format "Remove bogus newsgroup: %s " group)))))
15520           ;; Found a bogus newsgroup.
15521           (push group bogus)))
15522       ;; Remove all bogus subscribed groups by first killing them, and
15523       ;; then removing them from the list of killed groups.
15524       (while bogus
15525         (when (setq entry (gnus-gethash (setq group (pop bogus))
15526                                         gnus-newsrc-hashtb))
15527           (gnus-group-change-level entry gnus-level-killed)
15528           (setq gnus-killed-list (delete group gnus-killed-list))))
15529       ;; Then we remove all bogus groups from the list of killed and
15530       ;; zombie groups.  They are are removed without confirmation.
15531       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15532             killed)
15533         (while dead-lists
15534           (setq killed (symbol-value (car dead-lists)))
15535           (while killed
15536             (unless (gnus-active (setq group (pop killed)))
15537               ;; The group is bogus.
15538               ;; !!!Slow as hell.
15539               (set (car dead-lists)
15540                    (delete group (symbol-value (car dead-lists))))))
15541           (setq dead-lists (cdr dead-lists))))
15542       (gnus-message 5 "Checking bogus newsgroups...done"))))
15543
15544 (defun gnus-check-duplicate-killed-groups ()
15545   "Remove duplicates from the list of killed groups."
15546   (interactive)
15547   (let ((killed gnus-killed-list))
15548     (while killed
15549       (gnus-message 9 "%d" (length killed))
15550       (setcdr killed (delete (car killed) (cdr killed)))
15551       (setq killed (cdr killed)))))
15552
15553 ;; We want to inline a function from gnus-cache, so we cheat here:
15554 (eval-when-compile
15555   (provide 'gnus)
15556   (require 'gnus-cache))
15557
15558 (defun gnus-get-unread-articles-in-group (info active &optional update)
15559   (when active
15560     ;; Allow the backend to update the info in the group.
15561     (when (and update 
15562                (gnus-request-update-info
15563                 info (gnus-find-method-for-group (gnus-info-group info))))
15564       (gnus-activate-group (gnus-info-group info) nil t))
15565     (let* ((range (gnus-info-read info))
15566            (num 0))
15567       ;; If a cache is present, we may have to alter the active info.
15568       (when (and gnus-use-cache info)
15569         (inline (gnus-cache-possibly-alter-active 
15570                  (gnus-info-group info) active)))
15571       ;; Modify the list of read articles according to what articles
15572       ;; are available; then tally the unread articles and add the
15573       ;; number to the group hash table entry.
15574       (cond
15575        ((zerop (cdr active))
15576         (setq num 0))
15577        ((not range)
15578         (setq num (- (1+ (cdr active)) (car active))))
15579        ((not (listp (cdr range)))
15580         ;; Fix a single (num . num) range according to the
15581         ;; active hash table.
15582         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15583         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15584         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15585         ;; Compute number of unread articles.
15586         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15587        (t
15588         ;; The read list is a list of ranges.  Fix them according to
15589         ;; the active hash table.
15590         ;; First peel off any elements that are below the lower
15591         ;; active limit.
15592         (while (and (cdr range)
15593                     (>= (car active)
15594                         (or (and (atom (cadr range)) (cadr range))
15595                             (caadr range))))
15596           (if (numberp (car range))
15597               (setcar range
15598                       (cons (car range)
15599                             (or (and (numberp (cadr range))
15600                                      (cadr range))
15601                                 (cdadr range))))
15602             (setcdr (car range)
15603                     (or (and (numberp (nth 1 range)) (nth 1 range))
15604                         (cdadr range))))
15605           (setcdr range (cddr range)))
15606         ;; Adjust the first element to be the same as the lower limit.
15607         (if (and (not (atom (car range)))
15608                  (< (cdar range) (car active)))
15609             (setcdr (car range) (1- (car active))))
15610         ;; Then we want to peel off any elements that are higher
15611         ;; than the upper active limit.
15612         (let ((srange range))
15613           ;; Go past all legal elements.
15614           (while (and (cdr srange)
15615                       (<= (or (and (atom (cadr srange))
15616                                    (cadr srange))
15617                               (caadr srange)) (cdr active)))
15618             (setq srange (cdr srange)))
15619           (if (cdr srange)
15620               ;; Nuke all remaining illegal elements.
15621               (setcdr srange nil))
15622
15623           ;; Adjust the final element.
15624           (if (and (not (atom (car srange)))
15625                    (> (cdar srange) (cdr active)))
15626               (setcdr (car srange) (cdr active))))
15627         ;; Compute the number of unread articles.
15628         (while range
15629           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15630                                       (cdar range)))
15631                               (or (and (atom (car range)) (car range))
15632                                   (caar range)))))
15633           (setq range (cdr range)))
15634         (setq num (max 0 (- (cdr active) num)))))
15635       ;; Set the number of unread articles.
15636       (when info
15637         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15638       num)))
15639
15640 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15641 ;; and compute how many unread articles there are in each group.
15642 (defun gnus-get-unread-articles (&optional level)
15643   (let* ((newsrc (cdr gnus-newsrc-alist))
15644          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15645          (foreign-level
15646           (min
15647            (cond ((and gnus-activate-foreign-newsgroups
15648                        (not (numberp gnus-activate-foreign-newsgroups)))
15649                   (1+ gnus-level-subscribed))
15650                  ((numberp gnus-activate-foreign-newsgroups)
15651                   gnus-activate-foreign-newsgroups)
15652                  (t 0))
15653            level))
15654          info group active method)
15655     (gnus-message 5 "Checking new news...")
15656
15657     (while newsrc
15658       (setq active (gnus-active (setq group (gnus-info-group
15659                                              (setq info (pop newsrc))))))
15660
15661       ;; Check newsgroups.  If the user doesn't want to check them, or
15662       ;; they can't be checked (for instance, if the news server can't
15663       ;; be reached) we just set the number of unread articles in this
15664       ;; newsgroup to t.  This means that Gnus thinks that there are
15665       ;; unread articles, but it has no idea how many.
15666       (if (and (setq method (gnus-info-method info))
15667                (not (gnus-server-equal
15668                      gnus-select-method
15669                      (setq method (gnus-server-get-method nil method))))
15670                (not (gnus-secondary-method-p method)))
15671           ;; These groups are foreign.  Check the level.
15672           (when (<= (gnus-info-level info) foreign-level)
15673             (setq active (gnus-activate-group group 'scan))
15674             (unless (inline (gnus-virtual-group-p group))
15675               (inline (gnus-close-group group))))
15676
15677         ;; These groups are native or secondary.
15678         (when (and (<= (gnus-info-level info) level)
15679                    (not gnus-read-active-file))
15680           (setq active (gnus-activate-group group 'scan))
15681           (inline (gnus-close-group group))))
15682
15683       (if active
15684           (inline (gnus-get-unread-articles-in-group 
15685                    info active
15686                    (and method
15687                         (fboundp (intern (concat (symbol-name (car method))
15688                                                  "-request-update-info"))))))
15689         ;; The group couldn't be reached, so we nix out the number of
15690         ;; unread articles and stuff.
15691         (gnus-set-active group nil)
15692         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15693
15694     (gnus-message 5 "Checking new news...done")))
15695
15696 ;; Create a hash table out of the newsrc alist.  The `car's of the
15697 ;; alist elements are used as keys.
15698 (defun gnus-make-hashtable-from-newsrc-alist ()
15699   (let ((alist gnus-newsrc-alist)
15700         (ohashtb gnus-newsrc-hashtb)
15701         prev)
15702     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15703     (setq alist
15704           (setq prev (setq gnus-newsrc-alist
15705                            (if (equal (caar gnus-newsrc-alist)
15706                                       "dummy.group")
15707                                gnus-newsrc-alist
15708                              (cons (list "dummy.group" 0 nil) alist)))))
15709     (while alist
15710       (gnus-sethash
15711        (caar alist)
15712        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15713              prev)
15714        gnus-newsrc-hashtb)
15715       (setq prev alist
15716             alist (cdr alist)))))
15717
15718 (defun gnus-make-hashtable-from-killed ()
15719   "Create a hash table from the killed and zombie lists."
15720   (let ((lists '(gnus-killed-list gnus-zombie-list))
15721         list)
15722     (setq gnus-killed-hashtb
15723           (gnus-make-hashtable
15724            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15725     (while (setq list (pop lists))
15726       (setq list (symbol-value list))
15727       (while list
15728         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15729
15730 (defun gnus-activate-group (group &optional scan dont-check)
15731   ;; Check whether a group has been activated or not.
15732   ;; If SCAN, request a scan of that group as well.
15733   (let ((method (gnus-find-method-for-group group))
15734         active)
15735     (and (gnus-check-server method)
15736          ;; We escape all bugs and quit here to make it possible to
15737          ;; continue if a group is so out-there that it reports bugs
15738          ;; and stuff.
15739          (progn
15740            (and scan
15741                 (gnus-check-backend-function 'request-scan (car method))
15742                 (gnus-request-scan group method))
15743            t)
15744          (condition-case ()
15745              (gnus-request-group group dont-check)
15746         ;   (error nil)
15747            (quit nil))
15748          (save-excursion
15749            (set-buffer nntp-server-buffer)
15750            (goto-char (point-min))
15751            ;; Parse the result we got from `gnus-request-group'.
15752            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15753                 (progn
15754                   (goto-char (match-beginning 1))
15755                   (gnus-set-active
15756                    group (setq active (cons (read (current-buffer))
15757                                             (read (current-buffer)))))
15758                   ;; Return the new active info.
15759                   active))))))
15760
15761 (defun gnus-update-read-articles (group unread)
15762   "Update the list of read and ticked articles in GROUP using the
15763 UNREAD and TICKED lists.
15764 Note: UNSELECTED has to be sorted over `<'.
15765 Returns whether the updating was successful."
15766   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15767          (entry (gnus-gethash group gnus-newsrc-hashtb))
15768          (info (nth 2 entry))
15769          (prev 1)
15770          (unread (sort (copy-sequence unread) '<))
15771          read)
15772     (if (or (not info) (not active))
15773         ;; There is no info on this group if it was, in fact,
15774         ;; killed.  Gnus stores no information on killed groups, so
15775         ;; there's nothing to be done.
15776         ;; One could store the information somewhere temporarily,
15777         ;; perhaps...  Hmmm...
15778         ()
15779       ;; Remove any negative articles numbers.
15780       (while (and unread (< (car unread) 0))
15781         (setq unread (cdr unread)))
15782       ;; Remove any expired article numbers
15783       (while (and unread (< (car unread) (car active)))
15784         (setq unread (cdr unread)))
15785       ;; Compute the ranges of read articles by looking at the list of
15786       ;; unread articles.
15787       (while unread
15788         (if (/= (car unread) prev)
15789             (setq read (cons (if (= prev (1- (car unread))) prev
15790                                (cons prev (1- (car unread)))) read)))
15791         (setq prev (1+ (car unread)))
15792         (setq unread (cdr unread)))
15793       (when (<= prev (cdr active))
15794         (setq read (cons (cons prev (cdr active)) read)))
15795       ;; Enter this list into the group info.
15796       (gnus-info-set-read
15797        info (if (> (length read) 1) (nreverse read) read))
15798       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15799       (gnus-get-unread-articles-in-group info (gnus-active group))
15800       t)))
15801
15802 (defun gnus-make-articles-unread (group articles)
15803   "Mark ARTICLES in GROUP as unread."
15804   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15805                           (gnus-gethash (gnus-group-real-name group)
15806                                         gnus-newsrc-hashtb))))
15807          (ranges (gnus-info-read info))
15808          news article)
15809     (while articles
15810       (when (gnus-member-of-range
15811              (setq article (pop articles)) ranges)
15812         (setq news (cons article news))))
15813     (when news
15814       (gnus-info-set-read
15815        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15816       (gnus-group-update-group group t))))
15817
15818 ;; Enter all dead groups into the hashtb.
15819 (defun gnus-update-active-hashtb-from-killed ()
15820   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15821         (lists (list gnus-killed-list gnus-zombie-list))
15822         killed)
15823     (while lists
15824       (setq killed (car lists))
15825       (while killed
15826         (gnus-sethash (car killed) nil hashtb)
15827         (setq killed (cdr killed)))
15828       (setq lists (cdr lists)))))
15829
15830 ;; Get the active file(s) from the backend(s).
15831 (defun gnus-read-active-file ()
15832   (gnus-group-set-mode-line)
15833   (let ((methods 
15834          (append
15835           (if (gnus-check-server gnus-select-method)
15836               ;; The native server is available.
15837               (cons gnus-select-method gnus-secondary-select-methods)
15838             ;; The native server is down, so we just do the
15839             ;; secondary ones.
15840             gnus-secondary-select-methods)
15841           ;; Also read from the archive server.
15842           (when gnus-message-archive-method
15843             (list "archive"))))
15844         list-type)
15845     (setq gnus-have-read-active-file nil)
15846     (save-excursion
15847       (set-buffer nntp-server-buffer)
15848       (while methods
15849         (let* ((method (if (stringp (car methods))
15850                            (gnus-server-get-method nil (car methods))
15851                          (car methods)))
15852                (where (nth 1 method))
15853                (mesg (format "Reading active file%s via %s..."
15854                              (if (and where (not (zerop (length where))))
15855                                  (concat " from " where) "")
15856                              (car method))))
15857           (gnus-message 5 mesg)
15858           (when (gnus-check-server method)
15859             ;; Request that the backend scan its incoming messages.
15860             (and (gnus-check-backend-function 'request-scan (car method))
15861                  (gnus-request-scan nil method))
15862             (cond
15863              ((and (eq gnus-read-active-file 'some)
15864                    (gnus-check-backend-function 'retrieve-groups (car method)))
15865               (let ((newsrc (cdr gnus-newsrc-alist))
15866                     (gmethod (gnus-server-get-method nil method))
15867                     groups info)
15868                 (while (setq info (pop newsrc))
15869                   (when (gnus-server-equal
15870                          (gnus-find-method-for-group 
15871                           (gnus-info-group info) info)
15872                          gmethod)
15873                     (push (gnus-group-real-name (gnus-info-group info)) 
15874                           groups)))
15875                 (when groups
15876                   (gnus-check-server method)
15877                   (setq list-type (gnus-retrieve-groups groups method))
15878                   (cond
15879                    ((not list-type)
15880                     (gnus-error
15881                      1.2 "Cannot read partial active file from %s server."
15882                      (car method)))
15883                    ((eq list-type 'active)
15884                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15885                    (t
15886                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15887              (t
15888               (if (not (gnus-request-list method))
15889                   (unless (equal method gnus-message-archive-method)
15890                     (gnus-error 1 "Cannot read active file from %s server."
15891                                 (car method)))
15892                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15893                 ;; We mark this active file as read.
15894                 (push method gnus-have-read-active-file)
15895                 (gnus-message 5 "%sdone" mesg))))))
15896         (setq methods (cdr methods))))))
15897
15898 ;; Read an active file and place the results in `gnus-active-hashtb'.
15899 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15900   (unless method
15901     (setq method gnus-select-method))
15902   (let ((cur (current-buffer))
15903         (hashtb (or hashtb
15904                     (if (and gnus-active-hashtb
15905                              (not (equal method gnus-select-method)))
15906                         gnus-active-hashtb
15907                       (setq gnus-active-hashtb
15908                             (if (equal method gnus-select-method)
15909                                 (gnus-make-hashtable
15910                                  (count-lines (point-min) (point-max)))
15911                               (gnus-make-hashtable 4096)))))))
15912     ;; Delete unnecessary lines.
15913     (goto-char (point-min))
15914     (while (search-forward "\nto." nil t)
15915       (delete-region (1+ (match-beginning 0))
15916                      (progn (forward-line 1) (point))))
15917     (or (string= gnus-ignored-newsgroups "")
15918         (progn
15919           (goto-char (point-min))
15920           (delete-matching-lines gnus-ignored-newsgroups)))
15921     ;; Make the group names readable as a lisp expression even if they
15922     ;; contain special characters.
15923     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15924     (goto-char (point-max))
15925     (while (re-search-backward "[][';?()#]" nil t)
15926       (insert ?\\))
15927     ;; If these are groups from a foreign select method, we insert the
15928     ;; group prefix in front of the group names.
15929     (and method (not (gnus-server-equal
15930                       (gnus-server-get-method nil method)
15931                       (gnus-server-get-method nil gnus-select-method)))
15932          (let ((prefix (gnus-group-prefixed-name "" method)))
15933            (goto-char (point-min))
15934            (while (and (not (eobp))
15935                        (progn (insert prefix)
15936                               (zerop (forward-line 1)))))))
15937     ;; Store the active file in a hash table.
15938     (goto-char (point-min))
15939     (if (string-match "%[oO]" gnus-group-line-format)
15940         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15941         ;; If we want information on moderated groups, we use this
15942         ;; loop...
15943         (let* ((mod-hashtb (make-vector 7 0))
15944                (m (intern "m" mod-hashtb))
15945                group max min)
15946           (while (not (eobp))
15947             (condition-case nil
15948                 (progn
15949                   (narrow-to-region (point) (gnus-point-at-eol))
15950                   (setq group (let ((obarray hashtb)) (read cur)))
15951                   (if (and (numberp (setq max (read cur)))
15952                            (numberp (setq min (read cur)))
15953                            (progn
15954                              (skip-chars-forward " \t")
15955                              (not
15956                               (or (= (following-char) ?=)
15957                                   (= (following-char) ?x)
15958                                   (= (following-char) ?j)))))
15959                       (set group (cons min max))
15960                     (set group nil))
15961                   ;; Enter moderated groups into a list.
15962                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15963                       (setq gnus-moderated-list
15964                             (cons (symbol-name group) gnus-moderated-list))))
15965               (error
15966                (and group
15967                     (symbolp group)
15968                     (set group nil))))
15969             (widen)
15970             (forward-line 1)))
15971       ;; And if we do not care about moderation, we use this loop,
15972       ;; which is faster.
15973       (let (group max min)
15974         (while (not (eobp))
15975           (condition-case ()
15976               (progn
15977                 (narrow-to-region (point) (gnus-point-at-eol))
15978                 ;; group gets set to a symbol interned in the hash table
15979                 ;; (what a hack!!) - jwz
15980                 (setq group (let ((obarray hashtb)) (read cur)))
15981                 (if (and (numberp (setq max (read cur)))
15982                          (numberp (setq min (read cur)))
15983                          (progn
15984                            (skip-chars-forward " \t")
15985                            (not
15986                             (or (= (following-char) ?=)
15987                                 (= (following-char) ?x)
15988                                 (= (following-char) ?j)))))
15989                     (set group (cons min max))
15990                   (set group nil)))
15991             (error
15992              (progn
15993                (and group
15994                     (symbolp group)
15995                     (set group nil))
15996                (or ignore-errors
15997                    (gnus-message 3 "Warning - illegal active: %s"
15998                                  (buffer-substring
15999                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16000           (widen)
16001           (forward-line 1))))))
16002
16003 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16004   ;; Parse a "groups" active file.
16005   (let ((cur (current-buffer))
16006         (hashtb (or hashtb
16007                     (if (and method gnus-active-hashtb)
16008                         gnus-active-hashtb
16009                       (setq gnus-active-hashtb
16010                             (gnus-make-hashtable
16011                              (count-lines (point-min) (point-max)))))))
16012         (prefix (and method
16013                      (not (gnus-server-equal
16014                            (gnus-server-get-method nil method)
16015                            (gnus-server-get-method nil gnus-select-method)))
16016                      (gnus-group-prefixed-name "" method))))
16017
16018     (goto-char (point-min))
16019     ;; We split this into to separate loops, one with the prefix
16020     ;; and one without to speed the reading up somewhat.
16021     (if prefix
16022         (let (min max opoint group)
16023           (while (not (eobp))
16024             (condition-case ()
16025                 (progn
16026                   (read cur) (read cur)
16027                   (setq min (read cur)
16028                         max (read cur)
16029                         opoint (point))
16030                   (skip-chars-forward " \t")
16031                   (insert prefix)
16032                   (goto-char opoint)
16033                   (set (let ((obarray hashtb)) (read cur))
16034                        (cons min max)))
16035               (error (and group (symbolp group) (set group nil))))
16036             (forward-line 1)))
16037       (let (min max group)
16038         (while (not (eobp))
16039           (condition-case ()
16040               (if (= (following-char) ?2)
16041                   (progn
16042                     (read cur) (read cur)
16043                     (setq min (read cur)
16044                           max (read cur))
16045                     (set (setq group (let ((obarray hashtb)) (read cur)))
16046                          (cons min max))))
16047             (error (and group (symbolp group) (set group nil))))
16048           (forward-line 1))))))
16049
16050 (defun gnus-read-newsrc-file (&optional force)
16051   "Read startup file.
16052 If FORCE is non-nil, the .newsrc file is read."
16053   ;; Reset variables that might be defined in the .newsrc.eld file.
16054   (let ((variables gnus-variable-list))
16055     (while variables
16056       (set (car variables) nil)
16057       (setq variables (cdr variables))))
16058   (let* ((newsrc-file gnus-current-startup-file)
16059          (quick-file (concat newsrc-file ".el")))
16060     (save-excursion
16061       ;; We always load the .newsrc.eld file.  If always contains
16062       ;; much information that can not be gotten from the .newsrc
16063       ;; file (ticked articles, killed groups, foreign methods, etc.)
16064       (gnus-read-newsrc-el-file quick-file)
16065
16066       (if (and (file-exists-p gnus-current-startup-file)
16067                (or force
16068                    (and (file-newer-than-file-p newsrc-file quick-file)
16069                         (file-newer-than-file-p newsrc-file
16070                                                 (concat quick-file "d")))
16071                    (not gnus-newsrc-alist)))
16072           ;; We read the .newsrc file.  Note that if there if a
16073           ;; .newsrc.eld file exists, it has already been read, and
16074           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16075           ;; the .newsrc file, Gnus will only use the information it
16076           ;; can find there for changing the data already read -
16077           ;; ie. reading the .newsrc file will not trash the data
16078           ;; already read (except for read articles).
16079           (save-excursion
16080             (gnus-message 5 "Reading %s..." newsrc-file)
16081             (set-buffer (find-file-noselect newsrc-file))
16082             (buffer-disable-undo (current-buffer))
16083             (gnus-newsrc-to-gnus-format)
16084             (kill-buffer (current-buffer))
16085             (gnus-message 5 "Reading %s...done" newsrc-file)))
16086
16087       ;; Read any slave files.
16088       (unless gnus-slave
16089         (gnus-master-read-slave-newsrc))
16090       
16091       ;; Convert old to new.
16092       (gnus-convert-old-newsrc))))
16093
16094 (defun gnus-continuum-version (version)
16095   "Return VERSION as a floating point number."
16096   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16097     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16098            (number (match-string 2 version))
16099            major minor least)
16100       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16101       (setq major (string-to-number (match-string 1 number)))
16102       (setq minor (string-to-number (match-string 2 number)))
16103       (setq least (if (match-beginning 3)
16104                       (string-to-number (match-string 3 number))
16105                     0))
16106       (string-to-number
16107        (if (zerop major)
16108            (format "%s00%02d%02d"
16109                    (cond 
16110                     ((string= alpha "(ding)") "4.99")
16111                     ((string= alpha "September") "5.01")
16112                     ((string= alpha "Red") "5.03"))
16113                    minor least)
16114          (format "%d.%02d%20d" major minor least))))))
16115
16116 (defun gnus-convert-old-newsrc ()
16117   "Convert old newsrc into the new format, if needed."
16118   (let ((fcv (and gnus-newsrc-file-version
16119                   (gnus-continuum-version gnus-newsrc-file-version))))
16120     (cond
16121      ;; No .newsrc.eld file was loaded.
16122      ((null fcv) nil)
16123      ;; Gnus 5 .newsrc.eld was loaded.
16124      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16125       (gnus-convert-old-ticks)))))
16126
16127 (defun gnus-convert-old-ticks ()
16128   (let ((newsrc (cdr gnus-newsrc-alist))
16129         marks info dormant ticked)
16130     (while (setq info (pop newsrc))
16131       (when (setq marks (gnus-info-marks info))
16132         (setq dormant (cdr (assq 'dormant marks))
16133               ticked (cdr (assq 'tick marks)))
16134         (when (or dormant ticked)
16135           (gnus-info-set-read
16136            info
16137            (gnus-add-to-range
16138             (gnus-info-read info)
16139             (nconc (gnus-uncompress-range dormant)
16140                    (gnus-uncompress-range ticked)))))))))
16141
16142 (defun gnus-read-newsrc-el-file (file)
16143   (let ((ding-file (concat file "d")))
16144     ;; We always, always read the .eld file.
16145     (gnus-message 5 "Reading %s..." ding-file)
16146     (let (gnus-newsrc-assoc)
16147       (condition-case nil
16148           (load ding-file t t t)
16149         (error
16150          (gnus-error 1 "Error in %s" ding-file)))
16151       (when gnus-newsrc-assoc
16152         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16153     (gnus-make-hashtable-from-newsrc-alist)
16154     (when (file-newer-than-file-p file ding-file)
16155       ;; Old format quick file
16156       (gnus-message 5 "Reading %s..." file)
16157       ;; The .el file is newer than the .eld file, so we read that one
16158       ;; as well.
16159       (gnus-read-old-newsrc-el-file file))))
16160
16161 ;; Parse the old-style quick startup file
16162 (defun gnus-read-old-newsrc-el-file (file)
16163   (let (newsrc killed marked group m info)
16164     (prog1
16165         (let ((gnus-killed-assoc nil)
16166               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16167           (prog1
16168               (condition-case nil
16169                   (load file t t t)
16170                 (error nil))
16171             (setq newsrc gnus-newsrc-assoc
16172                   killed gnus-killed-assoc
16173                   marked gnus-marked-assoc)))
16174       (setq gnus-newsrc-alist nil)
16175       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16176         (if info
16177             (progn
16178               (gnus-info-set-read info (cddr group))
16179               (gnus-info-set-level
16180                info (if (nth 1 group) gnus-level-default-subscribed
16181                       gnus-level-default-unsubscribed))
16182               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16183           (push (setq info
16184                       (list (car group)
16185                             (if (nth 1 group) gnus-level-default-subscribed
16186                               gnus-level-default-unsubscribed)
16187                             (cddr group)))
16188                 gnus-newsrc-alist))
16189         ;; Copy marks into info.
16190         (when (setq m (assoc (car group) marked))
16191           (unless (nthcdr 3 info)
16192             (nconc info (list nil)))
16193           (gnus-info-set-marks
16194            info (list (cons 'tick (gnus-compress-sequence 
16195                                    (sort (cdr m) '<) t))))))
16196       (setq newsrc killed)
16197       (while newsrc
16198         (setcar newsrc (caar newsrc))
16199         (setq newsrc (cdr newsrc)))
16200       (setq gnus-killed-list killed))
16201     ;; The .el file version of this variable does not begin with
16202     ;; "options", while the .eld version does, so we just add it if it
16203     ;; isn't there.
16204     (and
16205      gnus-newsrc-options
16206      (progn
16207        (and (not (string-match "^ *options" gnus-newsrc-options))
16208             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16209        (and (not (string-match "\n$" gnus-newsrc-options))
16210             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16211        ;; Finally, if we read some options lines, we parse them.
16212        (or (string= gnus-newsrc-options "")
16213            (gnus-newsrc-parse-options gnus-newsrc-options))))
16214
16215     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16216     (gnus-make-hashtable-from-newsrc-alist)))
16217
16218 (defun gnus-make-newsrc-file (file)
16219   "Make server dependent file name by catenating FILE and server host name."
16220   (let* ((file (expand-file-name file nil))
16221          (real-file (concat file "-" (nth 1 gnus-select-method))))
16222     (if (or (file-exists-p real-file)
16223             (file-exists-p (concat real-file ".el"))
16224             (file-exists-p (concat real-file ".eld")))
16225         real-file file)))
16226
16227 (defun gnus-newsrc-to-gnus-format ()
16228   (setq gnus-newsrc-options "")
16229   (setq gnus-newsrc-options-n nil)
16230
16231   (or gnus-active-hashtb
16232       (setq gnus-active-hashtb (make-vector 4095 0)))
16233   (let ((buf (current-buffer))
16234         (already-read (> (length gnus-newsrc-alist) 1))
16235         group subscribed options-symbol newsrc Options-symbol
16236         symbol reads num1)
16237     (goto-char (point-min))
16238     ;; We intern the symbol `options' in the active hashtb so that we
16239     ;; can `eq' against it later.
16240     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16241     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16242
16243     (while (not (eobp))
16244       ;; We first read the first word on the line by narrowing and
16245       ;; then reading into `gnus-active-hashtb'.  Most groups will
16246       ;; already exist in that hashtb, so this will save some string
16247       ;; space.
16248       (narrow-to-region
16249        (point)
16250        (progn (skip-chars-forward "^ \t!:\n") (point)))
16251       (goto-char (point-min))
16252       (setq symbol
16253             (and (/= (point-min) (point-max))
16254                  (let ((obarray gnus-active-hashtb)) (read buf))))
16255       (widen)
16256       ;; Now, the symbol we have read is either `options' or a group
16257       ;; name.  If it is an options line, we just add it to a string.
16258       (cond
16259        ((or (eq symbol options-symbol)
16260             (eq symbol Options-symbol))
16261         (setq gnus-newsrc-options
16262               ;; This concating is quite inefficient, but since our
16263               ;; thorough studies show that approx 99.37% of all
16264               ;; .newsrc files only contain a single options line, we
16265               ;; don't give a damn, frankly, my dear.
16266               (concat gnus-newsrc-options
16267                       (buffer-substring
16268                        (gnus-point-at-bol)
16269                        ;; Options may continue on the next line.
16270                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16271                                 (progn (beginning-of-line) (point)))
16272                            (point)))))
16273         (forward-line -1))
16274        (symbol
16275         ;; Group names can be just numbers.  
16276         (when (numberp symbol) 
16277           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16278         (or (boundp symbol) (set symbol nil))
16279         ;; It was a group name.
16280         (setq subscribed (= (following-char) ?:)
16281               group (symbol-name symbol)
16282               reads nil)
16283         (if (eolp)
16284             ;; If the line ends here, this is clearly a buggy line, so
16285             ;; we put point a the beginning of line and let the cond
16286             ;; below do the error handling.
16287             (beginning-of-line)
16288           ;; We skip to the beginning of the ranges.
16289           (skip-chars-forward "!: \t"))
16290         ;; We are now at the beginning of the list of read articles.
16291         ;; We read them range by range.
16292         (while
16293             (cond
16294              ((looking-at "[0-9]+")
16295               ;; We narrow and read a number instead of buffer-substring/
16296               ;; string-to-int because it's faster.  narrow/widen is
16297               ;; faster than save-restriction/narrow, and save-restriction
16298               ;; produces a garbage object.
16299               (setq num1 (progn
16300                            (narrow-to-region (match-beginning 0) (match-end 0))
16301                            (read buf)))
16302               (widen)
16303               ;; If the next character is a dash, then this is a range.
16304               (if (= (following-char) ?-)
16305                   (progn
16306                     ;; We read the upper bound of the range.
16307                     (forward-char 1)
16308                     (if (not (looking-at "[0-9]+"))
16309                         ;; This is a buggy line, by we pretend that
16310                         ;; it's kinda OK.  Perhaps the user should be
16311                         ;; dinged?
16312                         (setq reads (cons num1 reads))
16313                       (setq reads
16314                             (cons
16315                              (cons num1
16316                                    (progn
16317                                      (narrow-to-region (match-beginning 0)
16318                                                        (match-end 0))
16319                                      (read buf)))
16320                              reads))
16321                       (widen)))
16322                 ;; It was just a simple number, so we add it to the
16323                 ;; list of ranges.
16324                 (setq reads (cons num1 reads)))
16325               ;; If the next char in ?\n, then we have reached the end
16326               ;; of the line and return nil.
16327               (/= (following-char) ?\n))
16328              ((= (following-char) ?\n)
16329               ;; End of line, so we end.
16330               nil)
16331              (t
16332               ;; Not numbers and not eol, so this might be a buggy
16333               ;; line...
16334               (or (eobp)
16335                   ;; If it was eob instead of ?\n, we allow it.
16336                   (progn
16337                     ;; The line was buggy.
16338                     (setq group nil)
16339                     (gnus-error 3.1 "Mangled line: %s"
16340                                 (buffer-substring (gnus-point-at-bol)
16341                                                   (gnus-point-at-eol)))))
16342               nil))
16343           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16344           ;; we allow them, because it's a common mistake to put a
16345           ;; space after the comma.
16346           (skip-chars-forward ", "))
16347
16348         ;; We have already read .newsrc.eld, so we gently update the
16349         ;; data in the hash table with the information we have just
16350         ;; read.
16351         (when group
16352           (let ((info (gnus-get-info group))
16353                 level)
16354             (if info
16355                 ;; There is an entry for this file in the alist.
16356                 (progn
16357                   (gnus-info-set-read info (nreverse reads))
16358                   ;; We update the level very gently.  In fact, we
16359                   ;; only change it if there's been a status change
16360                   ;; from subscribed to unsubscribed, or vice versa.
16361                   (setq level (gnus-info-level info))
16362                   (cond ((and (<= level gnus-level-subscribed)
16363                               (not subscribed))
16364                          (setq level (if reads
16365                                          gnus-level-default-unsubscribed
16366                                        (1+ gnus-level-default-unsubscribed))))
16367                         ((and (> level gnus-level-subscribed) subscribed)
16368                          (setq level gnus-level-default-subscribed)))
16369                   (gnus-info-set-level info level))
16370               ;; This is a new group.
16371               (setq info (list group
16372                                (if subscribed
16373                                    gnus-level-default-subscribed
16374                                  (if reads
16375                                      (1+ gnus-level-subscribed)
16376                                    gnus-level-default-unsubscribed))
16377                                (nreverse reads))))
16378             (setq newsrc (cons info newsrc))))))
16379       (forward-line 1))
16380
16381     (setq newsrc (nreverse newsrc))
16382
16383     (if (not already-read)
16384         ()
16385       ;; We now have two newsrc lists - `newsrc', which is what we
16386       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16387       ;; what we've read from .newsrc.eld.  We have to merge these
16388       ;; lists.  We do this by "attaching" any (foreign) groups in the
16389       ;; gnus-newsrc-alist to the (native) group that precedes them.
16390       (let ((rc (cdr gnus-newsrc-alist))
16391             (prev gnus-newsrc-alist)
16392             entry mentry)
16393         (while rc
16394           (or (null (nth 4 (car rc)))   ; It's a native group.
16395               (assoc (caar rc) newsrc) ; It's already in the alist.
16396               (if (setq entry (assoc (caar prev) newsrc))
16397                   (setcdr (setq mentry (memq entry newsrc))
16398                           (cons (car rc) (cdr mentry)))
16399                 (setq newsrc (cons (car rc) newsrc))))
16400           (setq prev rc
16401                 rc (cdr rc)))))
16402
16403     (setq gnus-newsrc-alist newsrc)
16404     ;; We make the newsrc hashtb.
16405     (gnus-make-hashtable-from-newsrc-alist)
16406
16407     ;; Finally, if we read some options lines, we parse them.
16408     (or (string= gnus-newsrc-options "")
16409         (gnus-newsrc-parse-options gnus-newsrc-options))))
16410
16411 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16412 ;; The return value will be a list on the form
16413 ;; ((regexp1 . ignore)
16414 ;;  (regexp2 . subscribe)...)
16415 ;; When handling new newsgroups, groups that match a `ignore' regexp
16416 ;; will be ignored, and groups that match a `subscribe' regexp will be
16417 ;; subscribed.  A line like
16418 ;; options -n !all rec.all
16419 ;; will lead to a list that looks like
16420 ;; (("^rec\\..+" . subscribe)
16421 ;;  ("^.+" . ignore))
16422 ;; So all "rec.*" groups will be subscribed, while all the other
16423 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16424 ;; different from "options -n rec.all !all".
16425 (defun gnus-newsrc-parse-options (options)
16426   (let (out eol)
16427     (save-excursion
16428       (gnus-set-work-buffer)
16429       (insert (regexp-quote options))
16430       ;; First we treat all continuation lines.
16431       (goto-char (point-min))
16432       (while (re-search-forward "\n[ \t]+" nil t)
16433         (replace-match " " t t))
16434       ;; Then we transform all "all"s into ".+"s.
16435       (goto-char (point-min))
16436       (while (re-search-forward "\\ball\\b" nil t)
16437         (replace-match ".+" t t))
16438       (goto-char (point-min))
16439       ;; We remove all other options than the "-n" ones.
16440       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16441         (replace-match " ")
16442         (forward-char -1))
16443       (goto-char (point-min))
16444
16445       ;; We are only interested in "options -n" lines - we
16446       ;; ignore the other option lines.
16447       (while (re-search-forward "[ \t]-n" nil t)
16448         (setq eol
16449               (or (save-excursion
16450                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16451                          (- (point) 2)))
16452                   (gnus-point-at-eol)))
16453         ;; Search for all "words"...
16454         (while (re-search-forward "[^ \t,\n]+" eol t)
16455           (if (= (char-after (match-beginning 0)) ?!)
16456               ;; If the word begins with a bang (!), this is a "not"
16457               ;; spec.  We put this spec (minus the bang) and the
16458               ;; symbol `ignore' into the list.
16459               (setq out (cons (cons (concat
16460                                      "^" (buffer-substring
16461                                           (1+ (match-beginning 0))
16462                                           (match-end 0)))
16463                                     'ignore) out))
16464             ;; There was no bang, so this is a "yes" spec.
16465             (setq out (cons (cons (concat "^" (match-string 0))
16466                                   'subscribe) out)))))
16467
16468       (setq gnus-newsrc-options-n out))))
16469
16470 (defun gnus-save-newsrc-file (&optional force)
16471   "Save .newsrc file."
16472   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16473   ;; from the variable gnus-newsrc-alist.
16474   (when (and (or gnus-newsrc-alist gnus-killed-list)
16475              gnus-current-startup-file)
16476     (save-excursion
16477       (if (and (or gnus-use-dribble-file gnus-slave)
16478                (not force)
16479                (or (not gnus-dribble-buffer)
16480                    (not (buffer-name gnus-dribble-buffer))
16481                    (zerop (save-excursion
16482                             (set-buffer gnus-dribble-buffer)
16483                             (buffer-size)))))
16484           (gnus-message 4 "(No changes need to be saved)")
16485         (run-hooks 'gnus-save-newsrc-hook)
16486         (if gnus-slave
16487             (gnus-slave-save-newsrc)
16488           ;; Save .newsrc.
16489           (when gnus-save-newsrc-file
16490             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16491             (gnus-gnus-to-newsrc-format)
16492             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16493           ;; Save .newsrc.eld.
16494           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16495           (make-local-variable 'version-control)
16496           (setq version-control 'never)
16497           (setq buffer-file-name
16498                 (concat gnus-current-startup-file ".eld"))
16499           (gnus-add-current-to-buffer-list)
16500           (buffer-disable-undo (current-buffer))
16501           (erase-buffer)
16502           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16503           (gnus-gnus-to-quick-newsrc-format)
16504           (run-hooks 'gnus-save-quick-newsrc-hook)
16505           (save-buffer)
16506           (kill-buffer (current-buffer))
16507           (gnus-message
16508            5 "Saving %s.eld...done" gnus-current-startup-file))
16509         (gnus-dribble-delete-file)))))
16510
16511 (defun gnus-gnus-to-quick-newsrc-format ()
16512   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16513   (insert ";; Gnus startup file.\n")
16514   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16515   (insert ";; to read .newsrc.\n")
16516   (insert "(setq gnus-newsrc-file-version "
16517           (prin1-to-string gnus-version) ")\n")
16518   (let ((variables
16519          (if gnus-save-killed-list gnus-variable-list
16520            ;; Remove the `gnus-killed-list' from the list of variables
16521            ;; to be saved, if required.
16522            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16523         ;; Peel off the "dummy" group.
16524         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16525         variable)
16526     ;; Insert the variables into the file.
16527     (while variables
16528       (when (and (boundp (setq variable (pop variables)))
16529                  (symbol-value variable))
16530         (insert "(setq " (symbol-name variable) " '")
16531         (prin1 (symbol-value variable) (current-buffer))
16532         (insert ")\n")))))
16533
16534 (defun gnus-gnus-to-newsrc-format ()
16535   ;; Generate and save the .newsrc file.
16536   (save-excursion
16537     (set-buffer (create-file-buffer gnus-current-startup-file))
16538     (let ((newsrc (cdr gnus-newsrc-alist))
16539           (standard-output (current-buffer))
16540           info ranges range method)
16541       (setq buffer-file-name gnus-current-startup-file)
16542       (buffer-disable-undo (current-buffer))
16543       (erase-buffer)
16544       ;; Write options.
16545       (if gnus-newsrc-options (insert gnus-newsrc-options))
16546       ;; Write subscribed and unsubscribed.
16547       (while (setq info (pop newsrc))
16548         ;; Don't write foreign groups to .newsrc.
16549         (when (or (null (setq method (gnus-info-method info)))
16550                   (equal method "native")
16551                   (gnus-server-equal method gnus-select-method))
16552           (insert (gnus-info-group info)
16553                   (if (> (gnus-info-level info) gnus-level-subscribed)
16554                       "!" ":"))
16555           (when (setq ranges (gnus-info-read info))
16556             (insert " ")
16557             (if (not (listp (cdr ranges)))
16558                 (if (= (car ranges) (cdr ranges))
16559                     (princ (car ranges))
16560                   (princ (car ranges))
16561                   (insert "-")
16562                   (princ (cdr ranges)))
16563               (while (setq range (pop ranges))
16564                 (if (or (atom range) (= (car range) (cdr range)))
16565                     (princ (or (and (atom range) range) (car range)))
16566                   (princ (car range))
16567                   (insert "-")
16568                   (princ (cdr range)))
16569                 (if ranges (insert ",")))))
16570           (insert "\n")))
16571       (make-local-variable 'version-control)
16572       (setq version-control 'never)
16573       ;; It has been reported that sometime the modtime on the .newsrc
16574       ;; file seems to be off.  We really do want to overwrite it, so
16575       ;; we clear the modtime here before saving.  It's a bit odd,
16576       ;; though...
16577       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16578       ;; delete the silly thing entirely first.  but this fails to provide
16579       ;; such niceties as .newsrc~ creation.
16580       (if gnus-modtime-botch
16581           (delete-file gnus-startup-file)
16582         (clear-visited-file-modtime))
16583       (run-hooks 'gnus-save-standard-newsrc-hook)
16584       (save-buffer)
16585       (kill-buffer (current-buffer)))))
16586
16587 \f
16588 ;;;
16589 ;;; Slave functions.
16590 ;;;
16591
16592 (defun gnus-slave-save-newsrc ()
16593   (save-excursion
16594     (set-buffer gnus-dribble-buffer)
16595     (let ((slave-name
16596            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16597       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16598
16599 (defun gnus-master-read-slave-newsrc ()
16600   (let ((slave-files
16601          (directory-files
16602           (file-name-directory gnus-current-startup-file)
16603           t (concat
16604              "^" (regexp-quote
16605                   (concat
16606                    (file-name-nondirectory gnus-current-startup-file)
16607                    "-slave-")))
16608           t))
16609         file)
16610     (if (not slave-files)
16611         ()                              ; There are no slave files to read.
16612       (gnus-message 7 "Reading slave newsrcs...")
16613       (save-excursion
16614         (set-buffer (get-buffer-create " *gnus slave*"))
16615         (buffer-disable-undo (current-buffer))
16616         (setq slave-files
16617               (sort (mapcar (lambda (file)
16618                               (list (nth 5 (file-attributes file)) file))
16619                             slave-files)
16620                     (lambda (f1 f2)
16621                       (or (< (caar f1) (caar f2))
16622                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16623         (while slave-files
16624           (erase-buffer)
16625           (setq file (nth 1 (car slave-files)))
16626           (insert-file-contents file)
16627           (if (condition-case ()
16628                   (progn
16629                     (eval-buffer (current-buffer))
16630                     t)
16631                 (error
16632                  (gnus-error 3.2 "Possible error in %s" file)
16633                  nil))
16634               (or gnus-slave ; Slaves shouldn't delete these files.
16635                   (condition-case ()
16636                       (delete-file file)
16637                     (error nil))))
16638           (setq slave-files (cdr slave-files))))
16639       (gnus-message 7 "Reading slave newsrcs...done"))))
16640
16641 \f
16642 ;;;
16643 ;;; Group description.
16644 ;;;
16645
16646 (defun gnus-read-all-descriptions-files ()
16647   (let ((methods (cons gnus-select-method 
16648                        (nconc
16649                         (when gnus-message-archive-method
16650                           (list "archive"))
16651                         gnus-secondary-select-methods))))
16652     (while methods
16653       (gnus-read-descriptions-file (car methods))
16654       (setq methods (cdr methods)))
16655     t))
16656
16657 (defun gnus-read-descriptions-file (&optional method)
16658   (let ((method (or method gnus-select-method)))
16659     (when (stringp method)
16660       (setq method (gnus-server-to-method method)))
16661     ;; We create the hashtable whether we manage to read the desc file
16662     ;; to avoid trying to re-read after a failed read.
16663     (or gnus-description-hashtb
16664         (setq gnus-description-hashtb
16665               (gnus-make-hashtable (length gnus-active-hashtb))))
16666     ;; Mark this method's desc file as read.
16667     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16668                   gnus-description-hashtb)
16669
16670     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16671     (cond
16672      ((not (gnus-check-server method))
16673       (gnus-message 1 "Couldn't open server")
16674       nil)
16675      ((not (gnus-request-list-newsgroups method))
16676       (gnus-message 1 "Couldn't read newsgroups descriptions")
16677       nil)
16678      (t
16679       (let (group)
16680         (save-excursion
16681           (save-restriction
16682             (set-buffer nntp-server-buffer)
16683             (goto-char (point-min))
16684             (if (or (search-forward "\n.\n" nil t)
16685                     (goto-char (point-max)))
16686                 (progn
16687                   (beginning-of-line)
16688                   (narrow-to-region (point-min) (point))))
16689             (goto-char (point-min))
16690             (while (not (eobp))
16691               ;; If we get an error, we set group to 0, which is not a
16692               ;; symbol...
16693               (setq group
16694                     (condition-case ()
16695                         (let ((obarray gnus-description-hashtb))
16696                           ;; Group is set to a symbol interned in this
16697                           ;; hash table.
16698                           (read nntp-server-buffer))
16699                       (error 0)))
16700               (skip-chars-forward " \t")
16701               ;; ...  which leads to this line being effectively ignored.
16702               (and (symbolp group)
16703                    (set group (buffer-substring
16704                                (point) (progn (end-of-line) (point)))))
16705               (forward-line 1))))
16706         (gnus-message 5 "Reading descriptions file...done")
16707         t)))))
16708
16709 (defun gnus-group-get-description (group)
16710   "Get the description of a group by sending XGTITLE to the server."
16711   (when (gnus-request-group-description group)
16712     (save-excursion
16713       (set-buffer nntp-server-buffer)
16714       (goto-char (point-min))
16715       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16716         (match-string 1)))))
16717
16718 \f
16719 ;;;
16720 ;;; Buffering of read articles.
16721 ;;;
16722
16723 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16724 (defvar gnus-backlog-articles nil)
16725 (defvar gnus-backlog-hashtb nil)
16726
16727 (defun gnus-backlog-buffer ()
16728   "Return the backlog buffer."
16729   (or (get-buffer gnus-backlog-buffer)
16730       (save-excursion
16731         (set-buffer (get-buffer-create gnus-backlog-buffer))
16732         (buffer-disable-undo (current-buffer))
16733         (setq buffer-read-only t)
16734         (gnus-add-current-to-buffer-list)
16735         (get-buffer gnus-backlog-buffer))))
16736
16737 (defun gnus-backlog-setup ()
16738   "Initialize backlog variables."
16739   (unless gnus-backlog-hashtb
16740     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16741
16742 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16743
16744 (defun gnus-backlog-shutdown ()
16745   "Clear all backlog variables and buffers."
16746   (when (get-buffer gnus-backlog-buffer)
16747     (kill-buffer gnus-backlog-buffer))
16748   (setq gnus-backlog-hashtb nil
16749         gnus-backlog-articles nil))
16750
16751 (defun gnus-backlog-enter-article (group number buffer)
16752   (gnus-backlog-setup)
16753   (let ((ident (intern (concat group ":" (int-to-string number))
16754                        gnus-backlog-hashtb))
16755         b)
16756     (if (memq ident gnus-backlog-articles)
16757         () ; It's already kept.
16758       ;; Remove the oldest article, if necessary.
16759       (and (numberp gnus-keep-backlog)
16760            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16761            (gnus-backlog-remove-oldest-article))
16762       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16763       ;; Insert the new article.
16764       (save-excursion
16765         (set-buffer (gnus-backlog-buffer))
16766         (let (buffer-read-only)
16767           (goto-char (point-max))
16768           (or (bolp) (insert "\n"))
16769           (setq b (point))
16770           (insert-buffer-substring buffer)
16771           ;; Tag the beginning of the article with the ident.
16772           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16773
16774 (defun gnus-backlog-remove-oldest-article ()
16775   (save-excursion
16776     (set-buffer (gnus-backlog-buffer))
16777     (goto-char (point-min))
16778     (if (zerop (buffer-size))
16779         () ; The buffer is empty.
16780       (let ((ident (get-text-property (point) 'gnus-backlog))
16781             buffer-read-only)
16782         ;; Remove the ident from the list of articles.
16783         (when ident
16784           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16785         ;; Delete the article itself.
16786         (delete-region
16787          (point) (next-single-property-change
16788                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16789
16790 (defun gnus-backlog-request-article (group number buffer)
16791   (when (numberp number)
16792     (gnus-backlog-setup)
16793     (let ((ident (intern (concat group ":" (int-to-string number))
16794                          gnus-backlog-hashtb))
16795           beg end)
16796       (when (memq ident gnus-backlog-articles)
16797         ;; It was in the backlog.
16798         (save-excursion
16799           (set-buffer (gnus-backlog-buffer))
16800           (if (not (setq beg (text-property-any
16801                               (point-min) (point-max) 'gnus-backlog
16802                               ident)))
16803               ;; It wasn't in the backlog after all.
16804               (ignore
16805                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16806             ;; Find the end (i. e., the beginning of the next article).
16807             (setq end
16808                   (next-single-property-change
16809                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16810         (let ((buffer-read-only nil))
16811           (erase-buffer)
16812           (insert-buffer-substring gnus-backlog-buffer beg end)
16813           t)))))
16814
16815 ;; Allow redefinition of Gnus functions.
16816
16817 (gnus-ems-redefine)
16818
16819 (provide 'gnus)
16820
16821 ;;; gnus.el ends here