*** 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.82"
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 "\\(<[^\n<>]+>\\)[ \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 (and thread
8024                (car thread))
8025       (mail-header-number (car thread)))))
8026
8027 (defun gnus-id-to-header (id)
8028   "Return the article headers of ID."
8029   (car (gnus-id-to-thread id)))
8030
8031 (defun gnus-article-displayed-root-p (article)
8032   "Say whether ARTICLE is a root(ish) article."
8033   (let ((level (gnus-summary-thread-level article))
8034         (refs (mail-header-references  (gnus-summary-article-header article)))
8035         particle)
8036     (cond 
8037      ((null level) nil)
8038      ((zerop level) t)
8039      ((null refs) t)
8040      ((null(gnus-parent-id refs)) t)
8041      ((and (= 1 level)
8042            (null (setq particle (gnus-id-to-article
8043                                  (gnus-parent-id refs))))
8044            (null (gnus-summary-thread-level particle)))))))
8045
8046 (defun gnus-root-id (id)
8047   "Return the id of the root of the thread where ID appears."
8048   (let (last-id prev)
8049     (while (and id (setq prev (car (gnus-gethash 
8050                                     id gnus-newsgroup-dependencies))))
8051       (setq last-id id
8052             id (gnus-parent-id (mail-header-references prev))))
8053     last-id))
8054
8055 (defun gnus-remove-thread (id &optional dont-remove)
8056   "Remove the thread that has ID in it."
8057   (let ((dep gnus-newsgroup-dependencies)
8058         headers thread last-id)
8059     ;; First go up in this thread until we find the root.
8060     (setq last-id (gnus-root-id id))
8061     (setq headers (list (car (gnus-id-to-thread last-id))
8062                         (caadr (gnus-id-to-thread last-id))))
8063     ;; We have now found the real root of this thread.  It might have
8064     ;; been gathered into some loose thread, so we have to search
8065     ;; through the threads to find the thread we wanted.
8066     (let ((threads gnus-newsgroup-threads)
8067           sub)
8068       (while threads
8069         (setq sub (car threads))
8070         (if (stringp (car sub))
8071             ;; This is a gathered threads, so we look at the roots
8072             ;; below it to find whether this article in in this
8073             ;; gathered root.
8074             (progn
8075               (setq sub (cdr sub))
8076               (while sub
8077                 (when (member (caar sub) headers)
8078                   (setq thread (car threads)
8079                         threads nil
8080                         sub nil))
8081                 (setq sub (cdr sub))))
8082           ;; It's an ordinary thread, so we check it.
8083           (when (eq (car sub) (car headers))
8084             (setq thread sub
8085                   threads nil)))
8086         (setq threads (cdr threads)))
8087       ;; If this article is in no thread, then it's a root.
8088       (if thread
8089           (unless dont-remove
8090             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8091         (setq thread (gnus-gethash last-id dep)))
8092       (when thread
8093         (prog1
8094             thread ; We return this thread.
8095           (unless dont-remove
8096             (if (stringp (car thread))
8097                 (progn
8098                   ;; If we use dummy roots, then we have to remove the
8099                   ;; dummy root as well.
8100                   (when (eq gnus-summary-make-false-root 'dummy)
8101                     ;; Uhm.
8102                     )
8103                   (setq thread (cdr thread))
8104                   (while thread
8105                     (gnus-remove-thread-1 (car thread))
8106                     (setq thread (cdr thread))))
8107               (gnus-remove-thread-1 thread))))))))
8108
8109 (defun gnus-remove-thread-1 (thread)
8110   "Remove the thread THREAD recursively."
8111   (let ((number (mail-header-number (car thread)))
8112         pos)
8113     (when (setq pos (text-property-any
8114                      (point-min) (point-max) 'gnus-number number))
8115       (goto-char pos)
8116       (gnus-delete-line)
8117       (gnus-data-remove number))
8118     (setq thread (cdr thread))
8119     (while thread
8120       (gnus-remove-thread-1 (pop thread)))))
8121
8122 (defun gnus-sort-threads (threads)
8123   "Sort THREADS."
8124   (if (not gnus-thread-sort-functions)
8125       threads
8126     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8127                     (car gnus-thread-sort-functions)
8128                   `(lambda (t1 t2)
8129                      ,(gnus-make-sort-function 
8130                        (reverse gnus-thread-sort-functions))))))
8131       (gnus-message 7 "Sorting threads...")
8132       (prog1
8133           (sort threads func)
8134         (gnus-message 7 "Sorting threads...done")))))
8135
8136 (defun gnus-sort-articles (articles)
8137   "Sort ARTICLES."
8138   (when gnus-article-sort-functions
8139     (let ((func (if (= 1 (length gnus-article-sort-functions))
8140                     (car gnus-article-sort-functions)
8141                   `(lambda (t1 t2)
8142                      ,(gnus-make-sort-function 
8143                        (reverse gnus-article-sort-functions))))))
8144       (gnus-message 7 "Sorting articles...")
8145       (prog1
8146           (setq gnus-newsgroup-headers (sort articles func))
8147         (gnus-message 7 "Sorting articles...done")))))
8148
8149 (defun gnus-make-sort-function (funs)
8150   "Return a composite sort condition based on the functions in FUNC."
8151   (if (cdr funs)
8152       `(or (,(car funs) t1 t2)
8153            (and (not (,(car funs) t2 t1))
8154                 ,(gnus-make-sort-function (cdr funs))))
8155     `(,(car funs) t1 t2)))
8156                  
8157 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8158 (defmacro gnus-thread-header (thread)
8159   ;; Return header of first article in THREAD.
8160   ;; Note that THREAD must never, ever be anything else than a variable -
8161   ;; using some other form will lead to serious barfage.
8162   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8163   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8164   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8165         (vector thread) 2))
8166
8167 (defsubst gnus-article-sort-by-number (h1 h2)
8168   "Sort articles by article number."
8169   (< (mail-header-number h1)
8170      (mail-header-number h2)))
8171
8172 (defun gnus-thread-sort-by-number (h1 h2)
8173   "Sort threads by root article number."
8174   (gnus-article-sort-by-number
8175    (gnus-thread-header h1) (gnus-thread-header h2)))
8176
8177 (defsubst gnus-article-sort-by-author (h1 h2)
8178   "Sort articles by root author."
8179   (string-lessp
8180    (let ((extract (funcall
8181                    gnus-extract-address-components
8182                    (mail-header-from h1))))
8183      (or (car extract) (cdr extract)))
8184    (let ((extract (funcall
8185                    gnus-extract-address-components
8186                    (mail-header-from h2))))
8187      (or (car extract) (cdr extract)))))
8188
8189 (defun gnus-thread-sort-by-author (h1 h2)
8190   "Sort threads by root author."
8191   (gnus-article-sort-by-author
8192    (gnus-thread-header h1)  (gnus-thread-header h2)))
8193
8194 (defsubst gnus-article-sort-by-subject (h1 h2)
8195   "Sort articles by root subject."
8196   (string-lessp
8197    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8198    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8199
8200 (defun gnus-thread-sort-by-subject (h1 h2)
8201   "Sort threads by root subject."
8202   (gnus-article-sort-by-subject
8203    (gnus-thread-header h1) (gnus-thread-header h2)))
8204
8205 (defsubst gnus-article-sort-by-date (h1 h2)
8206   "Sort articles by root article date."
8207   (string-lessp
8208    (gnus-sortable-date (mail-header-date h1))
8209    (gnus-sortable-date (mail-header-date h2))))
8210
8211 (defun gnus-thread-sort-by-date (h1 h2)
8212   "Sort threads by root article date."
8213   (gnus-article-sort-by-date
8214    (gnus-thread-header h1) (gnus-thread-header h2)))
8215
8216 (defsubst gnus-article-sort-by-score (h1 h2)
8217   "Sort articles by root article score.
8218 Unscored articles will be counted as having a score of zero."
8219   (> (or (cdr (assq (mail-header-number h1)
8220                     gnus-newsgroup-scored))
8221          gnus-summary-default-score 0)
8222      (or (cdr (assq (mail-header-number h2)
8223                     gnus-newsgroup-scored))
8224          gnus-summary-default-score 0)))
8225
8226 (defun gnus-thread-sort-by-score (h1 h2)
8227   "Sort threads by root article score."
8228   (gnus-article-sort-by-score
8229    (gnus-thread-header h1) (gnus-thread-header h2)))
8230
8231 (defun gnus-thread-sort-by-total-score (h1 h2)
8232   "Sort threads by the sum of all scores in the thread.
8233 Unscored articles will be counted as having a score of zero."
8234   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8235
8236 (defun gnus-thread-total-score (thread)
8237   ;;  This function find the total score of THREAD.
8238   (if (consp thread)
8239       (if (stringp (car thread))
8240           (apply gnus-thread-score-function 0
8241                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8242         (gnus-thread-total-score-1 thread))
8243     (gnus-thread-total-score-1 (list thread))))
8244
8245 (defun gnus-thread-total-score-1 (root)
8246   ;; This function find the total score of the thread below ROOT.
8247   (setq root (car root))
8248   (apply gnus-thread-score-function
8249          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8250              gnus-summary-default-score 0)
8251          (mapcar 'gnus-thread-total-score
8252                  (cdr (gnus-gethash (mail-header-id root)
8253                                     gnus-newsgroup-dependencies)))))
8254
8255 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8256 (defvar gnus-tmp-prev-subject nil)
8257 (defvar gnus-tmp-false-parent nil)
8258 (defvar gnus-tmp-root-expunged nil)
8259 (defvar gnus-tmp-dummy-line nil)
8260
8261 (defun gnus-summary-prepare-threads (threads)
8262   "Prepare summary buffer from THREADS and indentation LEVEL.
8263 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8264 or a straight list of headers."
8265   (gnus-message 7 "Generating summary...")
8266
8267   (setq gnus-newsgroup-threads threads)
8268   (beginning-of-line)
8269
8270   (let ((gnus-tmp-level 0)
8271         (default-score (or gnus-summary-default-score 0))
8272         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8273         thread number subject stack state gnus-tmp-gathered beg-match
8274         new-roots gnus-tmp-new-adopts thread-end
8275         gnus-tmp-header gnus-tmp-unread
8276         gnus-tmp-replied gnus-tmp-subject-or-nil
8277         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8278         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8279         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8280
8281     (setq gnus-tmp-prev-subject nil)
8282
8283     (if (vectorp (car threads))
8284         ;; If this is a straight (sic) list of headers, then a
8285         ;; threaded summary display isn't required, so we just create
8286         ;; an unthreaded one.
8287         (gnus-summary-prepare-unthreaded threads)
8288
8289       ;; Do the threaded display.
8290
8291       (while (or threads stack gnus-tmp-new-adopts new-roots)
8292
8293         (if (and (= gnus-tmp-level 0)
8294                  (not (setq gnus-tmp-dummy-line nil))
8295                  (or (not stack)
8296                      (= (caar stack) 0))
8297                  (not gnus-tmp-false-parent)
8298                  (or gnus-tmp-new-adopts new-roots))
8299             (if gnus-tmp-new-adopts
8300                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8301                       thread (list (car gnus-tmp-new-adopts))
8302                       gnus-tmp-header (caar thread)
8303                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8304               (if new-roots
8305                   (setq thread (list (car new-roots))
8306                         gnus-tmp-header (caar thread)
8307                         new-roots (cdr new-roots))))
8308
8309           (if threads
8310               ;; If there are some threads, we do them before the
8311               ;; threads on the stack.
8312               (setq thread threads
8313                     gnus-tmp-header (caar thread))
8314             ;; There were no current threads, so we pop something off
8315             ;; the stack.
8316             (setq state (car stack)
8317                   gnus-tmp-level (car state)
8318                   thread (cdr state)
8319                   stack (cdr stack)
8320                   gnus-tmp-header (caar thread))))
8321
8322         (setq gnus-tmp-false-parent nil)
8323         (setq gnus-tmp-root-expunged nil)
8324         (setq thread-end nil)
8325
8326         (if (stringp gnus-tmp-header)
8327             ;; The header is a dummy root.
8328             (cond
8329              ((eq gnus-summary-make-false-root 'adopt)
8330               ;; We let the first article adopt the rest.
8331               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8332                                                (cddar thread)))
8333               (setq gnus-tmp-gathered
8334                     (nconc (mapcar
8335                             (lambda (h) (mail-header-number (car h)))
8336                             (cddar thread))
8337                            gnus-tmp-gathered))
8338               (setq thread (cons (list (caar thread)
8339                                        (cadar thread))
8340                                  (cdr thread)))
8341               (setq gnus-tmp-level -1
8342                     gnus-tmp-false-parent t))
8343              ((eq gnus-summary-make-false-root 'empty)
8344               ;; We print adopted articles with empty subject fields.
8345               (setq gnus-tmp-gathered
8346                     (nconc (mapcar
8347                             (lambda (h) (mail-header-number (car h)))
8348                             (cddar thread))
8349                            gnus-tmp-gathered))
8350               (setq gnus-tmp-level -1))
8351              ((eq gnus-summary-make-false-root 'dummy)
8352               ;; We remember that we probably want to output a dummy
8353               ;; root.
8354               (setq gnus-tmp-dummy-line gnus-tmp-header)
8355               (setq gnus-tmp-prev-subject gnus-tmp-header))
8356              (t
8357               ;; We do not make a root for the gathered
8358               ;; sub-threads at all.
8359               (setq gnus-tmp-level -1)))
8360
8361           (setq number (mail-header-number gnus-tmp-header)
8362                 subject (mail-header-subject gnus-tmp-header))
8363
8364           (cond
8365            ;; If the thread has changed subject, we might want to make
8366            ;; this subthread into a root.
8367            ((and (null gnus-thread-ignore-subject)
8368                  (not (zerop gnus-tmp-level))
8369                  gnus-tmp-prev-subject
8370                  (not (inline
8371                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8372             (setq new-roots (nconc new-roots (list (car thread)))
8373                   thread-end t
8374                   gnus-tmp-header nil))
8375            ;; If the article lies outside the current limit,
8376            ;; then we do not display it.
8377            ((and (not (memq number gnus-newsgroup-limit))
8378                  (not gnus-tmp-dummy-line))
8379             (setq gnus-tmp-gathered
8380                   (nconc (mapcar
8381                           (lambda (h) (mail-header-number (car h)))
8382                           (cdar thread))
8383                          gnus-tmp-gathered))
8384             (setq gnus-tmp-new-adopts (if (cdar thread)
8385                                           (append gnus-tmp-new-adopts
8386                                                   (cdar thread))
8387                                         gnus-tmp-new-adopts)
8388                   thread-end t
8389                   gnus-tmp-header nil)
8390             (when (zerop gnus-tmp-level)
8391               (setq gnus-tmp-root-expunged t)))
8392            ;; Perhaps this article is to be marked as read?
8393            ((and gnus-summary-mark-below
8394                  (< (or (cdr (assq number gnus-newsgroup-scored))
8395                         default-score)
8396                     gnus-summary-mark-below)
8397                  ;; Don't touch sparse articles.
8398                  (not (memq number gnus-newsgroup-sparse)))
8399             (setq gnus-newsgroup-unreads
8400                   (delq number gnus-newsgroup-unreads))
8401             (if gnus-newsgroup-auto-expire
8402                 (push number gnus-newsgroup-expirable)
8403               (push (cons number gnus-low-score-mark)
8404                     gnus-newsgroup-reads))))
8405
8406           (when gnus-tmp-header
8407             ;; We may have an old dummy line to output before this
8408             ;; article.
8409             (when gnus-tmp-dummy-line
8410               (gnus-summary-insert-dummy-line
8411                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8412               (setq gnus-tmp-dummy-line nil))
8413
8414             ;; Compute the mark.
8415             (setq
8416              gnus-tmp-unread
8417              (cond
8418               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8419               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8420               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8421               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8422               (t (or (cdr (assq number gnus-newsgroup-reads))
8423                      gnus-ancient-mark))))
8424
8425             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8426                                   gnus-tmp-header gnus-tmp-level)
8427                   gnus-newsgroup-data)
8428
8429             ;; Actually insert the line.
8430             (setq
8431              gnus-tmp-subject-or-nil
8432              (cond
8433               ((and gnus-thread-ignore-subject
8434                     gnus-tmp-prev-subject
8435                     (not (inline (gnus-subject-equal
8436                                   gnus-tmp-prev-subject subject))))
8437                subject)
8438               ((zerop gnus-tmp-level)
8439                (if (and (eq gnus-summary-make-false-root 'empty)
8440                         (memq number gnus-tmp-gathered)
8441                         gnus-tmp-prev-subject
8442                         (inline (gnus-subject-equal
8443                                  gnus-tmp-prev-subject subject)))
8444                    gnus-summary-same-subject
8445                  subject))
8446               (t gnus-summary-same-subject)))
8447             (if (and (eq gnus-summary-make-false-root 'adopt)
8448                      (= gnus-tmp-level 1)
8449                      (memq number gnus-tmp-gathered))
8450                 (setq gnus-tmp-opening-bracket ?\<
8451                       gnus-tmp-closing-bracket ?\>)
8452               (setq gnus-tmp-opening-bracket ?\[
8453                     gnus-tmp-closing-bracket ?\]))
8454             (setq
8455              gnus-tmp-indentation
8456              (aref gnus-thread-indent-array gnus-tmp-level)
8457              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8458              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8459                                 gnus-summary-default-score 0)
8460              gnus-tmp-score-char
8461              (if (or (null gnus-summary-default-score)
8462                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8463                          gnus-summary-zcore-fuzz)) ? 
8464                (if (< gnus-tmp-score gnus-summary-default-score)
8465                    gnus-score-below-mark gnus-score-over-mark))
8466              gnus-tmp-replied
8467              (cond ((memq number gnus-newsgroup-processable)
8468                     gnus-process-mark)
8469                    ((memq number gnus-newsgroup-cached)
8470                     gnus-cached-mark)
8471                    ((memq number gnus-newsgroup-replied)
8472                     gnus-replied-mark)
8473                    (t gnus-unread-mark))
8474              gnus-tmp-from (mail-header-from gnus-tmp-header)
8475              gnus-tmp-name
8476              (cond
8477               ((string-match "(.+)" gnus-tmp-from)
8478                (substring gnus-tmp-from
8479                           (1+ (match-beginning 0)) (1- (match-end 0))))
8480               ((string-match "<[^>]+> *$" gnus-tmp-from)
8481                (setq beg-match (match-beginning 0))
8482                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8483                         (substring gnus-tmp-from (1+ (match-beginning 0))
8484                                    (1- (match-end 0))))
8485                    (substring gnus-tmp-from 0 beg-match)))
8486               (t gnus-tmp-from)))
8487             (when (string= gnus-tmp-name "")
8488               (setq gnus-tmp-name gnus-tmp-from))
8489             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8490             (put-text-property
8491              (point)
8492              (progn (eval gnus-summary-line-format-spec) (point))
8493              'gnus-number number)
8494             (when gnus-visual-p
8495               (forward-line -1)
8496               (run-hooks 'gnus-summary-update-hook)
8497               (forward-line 1))
8498
8499             (setq gnus-tmp-prev-subject subject)))
8500
8501         (when (nth 1 thread)
8502           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8503         (incf gnus-tmp-level)
8504         (setq threads (if thread-end nil (cdar thread)))
8505         (unless threads
8506           (setq gnus-tmp-level 0)))))
8507   (gnus-message 7 "Generating summary...done"))
8508
8509 (defun gnus-summary-prepare-unthreaded (headers)
8510   "Generate an unthreaded summary buffer based on HEADERS."
8511   (let (header number mark)
8512
8513     (while headers
8514       ;; We may have to root out some bad articles...
8515       (when (memq (setq number (mail-header-number
8516                                 (setq header (pop headers))))
8517                   gnus-newsgroup-limit)
8518         ;; Mark article as read when it has a low score.
8519         (when (and gnus-summary-mark-below
8520                    (< (or (cdr (assq number gnus-newsgroup-scored))
8521                           gnus-summary-default-score 0)
8522                       gnus-summary-mark-below))
8523           (setq gnus-newsgroup-unreads
8524                 (delq number gnus-newsgroup-unreads))
8525           (if gnus-newsgroup-auto-expire
8526               (push number gnus-newsgroup-expirable)
8527             (push (cons number gnus-low-score-mark)
8528                   gnus-newsgroup-reads)))
8529
8530         (setq mark
8531               (cond
8532                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8533                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8534                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8535                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8536                (t (or (cdr (assq number gnus-newsgroup-reads))
8537                       gnus-ancient-mark))))
8538         (setq gnus-newsgroup-data
8539               (cons (gnus-data-make number mark (1+ (point)) header 0)
8540                     gnus-newsgroup-data))
8541         (gnus-summary-insert-line
8542          header 0 nil mark (memq number gnus-newsgroup-replied)
8543          (memq number gnus-newsgroup-expirable)
8544          (mail-header-subject header) nil
8545          (cdr (assq number gnus-newsgroup-scored))
8546          (memq number gnus-newsgroup-processable))))))
8547
8548 (defun gnus-select-newsgroup (group &optional read-all)
8549   "Select newsgroup GROUP.
8550 If READ-ALL is non-nil, all articles in the group are selected."
8551   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8552          (info (nth 2 entry))
8553          articles fetched-articles cached)
8554
8555     (or (gnus-check-server
8556          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8557         (error "Couldn't open server"))
8558
8559     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8560         (gnus-activate-group group) ; Or we can activate it...
8561         (progn ; Or we bug out.
8562           (when (equal major-mode 'gnus-summary-mode)
8563             (kill-buffer (current-buffer)))
8564           (error "Couldn't request group %s: %s"
8565                  group (gnus-status-message group))))
8566
8567     (setq gnus-newsgroup-name group)
8568     (setq gnus-newsgroup-unselected nil)
8569     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8570
8571     (and gnus-asynchronous
8572          (gnus-check-backend-function
8573           'request-asynchronous gnus-newsgroup-name)
8574          (setq gnus-newsgroup-async
8575                (gnus-request-asynchronous gnus-newsgroup-name)))
8576
8577     ;; Adjust and set lists of article marks.
8578     (when info
8579       (gnus-adjust-marked-articles info))
8580
8581     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8582     (when (gnus-virtual-group-p group)
8583       (setq cached gnus-newsgroup-cached))
8584
8585     (setq gnus-newsgroup-unreads
8586           (gnus-set-difference
8587            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8588            gnus-newsgroup-dormant))
8589
8590     (setq gnus-newsgroup-processable nil)
8591
8592     (setq articles (gnus-articles-to-read group read-all))
8593
8594     (cond
8595      ((null articles)
8596       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8597       'quit)
8598      ((eq articles 0) nil)
8599      (t
8600       ;; Init the dependencies hash table.
8601       (setq gnus-newsgroup-dependencies
8602             (gnus-make-hashtable (length articles)))
8603       ;; Retrieve the headers and read them in.
8604       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8605       (setq gnus-newsgroup-headers
8606             (if (eq 'nov
8607                     (setq gnus-headers-retrieved-by
8608                           (gnus-retrieve-headers
8609                            articles gnus-newsgroup-name
8610                            ;; We might want to fetch old headers, but
8611                            ;; not if there is only 1 article.
8612                            (and gnus-fetch-old-headers
8613                                 (or (and
8614                                      (not (eq gnus-fetch-old-headers 'some))
8615                                      (not (numberp gnus-fetch-old-headers)))
8616                                     (> (length articles) 1))))))
8617                 (gnus-get-newsgroup-headers-xover articles)
8618               (gnus-get-newsgroup-headers)))
8619       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8620
8621       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8622       (when cached
8623         (setq gnus-newsgroup-cached cached))
8624
8625       ;; Set the initial limit.
8626       (setq gnus-newsgroup-limit (copy-sequence articles))
8627       ;; Remove canceled articles from the list of unread articles.
8628       (setq gnus-newsgroup-unreads
8629             (gnus-set-sorted-intersection
8630              gnus-newsgroup-unreads
8631              (setq fetched-articles
8632                    (mapcar (lambda (headers) (mail-header-number headers))
8633                            gnus-newsgroup-headers))))
8634       ;; Removed marked articles that do not exist.
8635       (gnus-update-missing-marks
8636        (gnus-sorted-complement fetched-articles articles))
8637       ;; We might want to build some more threads first.
8638       (and gnus-fetch-old-headers
8639            (eq gnus-headers-retrieved-by 'nov)
8640            (gnus-build-old-threads))
8641       ;; Check whether auto-expire is to be done in this group.
8642       (setq gnus-newsgroup-auto-expire
8643             (gnus-group-auto-expirable-p group))
8644       ;; Set up the article buffer now, if necessary.
8645       (unless gnus-single-article-buffer
8646         (gnus-article-setup-buffer))
8647       ;; First and last article in this newsgroup.
8648       (and gnus-newsgroup-headers
8649            (setq gnus-newsgroup-begin
8650                  (mail-header-number (car gnus-newsgroup-headers)))
8651            (setq gnus-newsgroup-end
8652                  (mail-header-number
8653                   (gnus-last-element gnus-newsgroup-headers))))
8654       (setq gnus-reffed-article-number -1)
8655       ;; GROUP is successfully selected.
8656       (or gnus-newsgroup-headers t)))))
8657
8658 (defun gnus-articles-to-read (group read-all)
8659   ;; Find out what articles the user wants to read.
8660   (let* ((articles
8661           ;; Select all articles if `read-all' is non-nil, or if there
8662           ;; are no unread articles.
8663           (if (or read-all
8664                   (and (zerop (length gnus-newsgroup-marked))
8665                        (zerop (length gnus-newsgroup-unreads))))
8666               (gnus-uncompress-range (gnus-active group))
8667             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8668                           (copy-sequence gnus-newsgroup-unreads))
8669                   '<)))
8670          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8671          (scored (length scored-list))
8672          (number (length articles))
8673          (marked (+ (length gnus-newsgroup-marked)
8674                     (length gnus-newsgroup-dormant)))
8675          (select
8676           (cond
8677            ((numberp read-all)
8678             read-all)
8679            (t
8680             (condition-case ()
8681                 (cond
8682                  ((and (or (<= scored marked) (= scored number))
8683                        (numberp gnus-large-newsgroup)
8684                        (> number gnus-large-newsgroup))
8685                   (let ((input
8686                          (read-string
8687                           (format
8688                            "How many articles from %s (default %d): "
8689                            gnus-newsgroup-name number))))
8690                     (if (string-match "^[ \t]*$" input) number input)))
8691                  ((and (> scored marked) (< scored number))
8692                   (let ((input
8693                          (read-string
8694                           (format "%s %s (%d scored, %d total): "
8695                                   "How many articles from"
8696                                   group scored number))))
8697                     (if (string-match "^[ \t]*$" input)
8698                         number input)))
8699                  (t number))
8700               (quit nil))))))
8701     (setq select (if (stringp select) (string-to-number select) select))
8702     (if (or (null select) (zerop select))
8703         select
8704       (if (and (not (zerop scored)) (<= (abs select) scored))
8705           (progn
8706             (setq articles (sort scored-list '<))
8707             (setq number (length articles)))
8708         (setq articles (copy-sequence articles)))
8709
8710       (if (< (abs select) number)
8711           (if (< select 0)
8712               ;; Select the N oldest articles.
8713               (setcdr (nthcdr (1- (abs select)) articles) nil)
8714             ;; Select the N most recent articles.
8715             (setq articles (nthcdr (- number select) articles))))
8716       (setq gnus-newsgroup-unselected
8717             (gnus-sorted-intersection
8718              gnus-newsgroup-unreads
8719              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8720       articles)))
8721
8722 (defun gnus-killed-articles (killed articles)
8723   (let (out)
8724     (while articles
8725       (if (inline (gnus-member-of-range (car articles) killed))
8726           (setq out (cons (car articles) out)))
8727       (setq articles (cdr articles)))
8728     out))
8729
8730 (defun gnus-uncompress-marks (marks)
8731   "Uncompress the mark ranges in MARKS."
8732   (let ((uncompressed '(score bookmark))
8733         out)
8734     (while marks
8735       (if (memq (caar marks) uncompressed)
8736           (push (car marks) out)
8737         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8738       (setq marks (cdr marks)))
8739     out))
8740
8741 (defun gnus-adjust-marked-articles (info)
8742   "Set all article lists and remove all marks that are no longer legal."
8743   (let* ((marked-lists (gnus-info-marks info))
8744          (active (gnus-active (gnus-info-group info)))
8745          (min (car active))
8746          (max (cdr active))
8747          (types gnus-article-mark-lists)
8748          (uncompressed '(score bookmark))
8749          marks var articles article mark)
8750
8751     (while marked-lists
8752       (setq marks (pop marked-lists))
8753       (set (setq var (intern (format "gnus-newsgroup-%s"
8754                                      (car (rassq (setq mark (car marks))
8755                                                  types)))))
8756            (if (memq (car marks) uncompressed) (cdr marks)
8757              (gnus-uncompress-range (cdr marks))))
8758
8759       (setq articles (symbol-value var))
8760
8761       ;; All articles have to be subsets of the active articles.
8762       (cond
8763        ;; Adjust "simple" lists.
8764        ((memq mark '(tick dormant expirable reply killed save))
8765         (while articles
8766           (when (or (< (setq article (pop articles)) min) (> article max))
8767             (set var (delq article (symbol-value var))))))
8768        ;; Adjust assocs.
8769        ((memq mark '(score bookmark))
8770         (while articles
8771           (when (or (< (car (setq article (pop articles))) min)
8772                     (> (car article) max))
8773             (set var (delq article (symbol-value var))))))))))
8774
8775 (defun gnus-update-missing-marks (missing)
8776   "Go through the list of MISSING articles and remove them mark lists."
8777   (when missing
8778     (let ((types gnus-article-mark-lists)
8779           var m)
8780       ;; Go through all types.
8781       (while types
8782         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8783         (when (symbol-value var)
8784           ;; This list has articles.  So we delete all missing articles
8785           ;; from it.
8786           (setq m missing)
8787           (while m
8788             (set var (delq (pop m) (symbol-value var)))))))))
8789
8790 (defun gnus-update-marks ()
8791   "Enter the various lists of marked articles into the newsgroup info list."
8792   (let ((types gnus-article-mark-lists)
8793         (info (gnus-get-info gnus-newsgroup-name))
8794         (uncompressed '(score bookmark killed))
8795         type list newmarked symbol)
8796     (when info
8797       ;; Add all marks lists that are non-nil to the list of marks lists.
8798       (while types
8799         (setq type (pop types))
8800         (when (setq list (symbol-value
8801                           (setq symbol
8802                                 (intern (format "gnus-newsgroup-%s"
8803                                                 (car type))))))
8804           (push (cons (cdr type)
8805                       (if (memq (cdr type) uncompressed) list
8806                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8807                 newmarked)))
8808
8809       ;; Enter these new marks into the info of the group.
8810       (if (nthcdr 3 info)
8811           (setcar (nthcdr 3 info) newmarked)
8812         ;; Add the marks lists to the end of the info.
8813         (when newmarked
8814           (setcdr (nthcdr 2 info) (list newmarked))))
8815
8816       ;; Cut off the end of the info if there's nothing else there.
8817       (let ((i 5))
8818         (while (and (> i 2)
8819                     (not (nth i info)))
8820           (when (nthcdr (decf i) info)
8821             (setcdr (nthcdr i info) nil)))))))
8822
8823 (defun gnus-add-marked-articles (group type articles &optional info force)
8824   ;; Add ARTICLES of TYPE to the info of GROUP.
8825   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8826   ;; add, but replace marked articles of TYPE with ARTICLES.
8827   (let ((info (or info (gnus-get-info group)))
8828         (uncompressed '(score bookmark killed))
8829         marked m)
8830     (or (not info)
8831         (and (not (setq marked (nthcdr 3 info)))
8832              (or (null articles)
8833                  (setcdr (nthcdr 2 info)
8834                          (list (list (cons type (gnus-compress-sequence
8835                                                  articles t)))))))
8836         (and (not (setq m (assq type (car marked))))
8837              (or (null articles)
8838                  (setcar marked
8839                          (cons (cons type (gnus-compress-sequence articles t) )
8840                                (car marked)))))
8841         (if force
8842             (if (null articles)
8843                 (setcar (nthcdr 3 info)
8844                         (delq (assq type (car marked)) (car marked)))
8845               (setcdr m (gnus-compress-sequence articles t)))
8846           (setcdr m (gnus-compress-sequence
8847                      (sort (nconc (gnus-uncompress-range (cdr m))
8848                                   (copy-sequence articles)) '<) t))))))
8849
8850 (defun gnus-set-mode-line (where)
8851   "This function sets the mode line of the article or summary buffers.
8852 If WHERE is `summary', the summary mode line format will be used."
8853   ;; Is this mode line one we keep updated?
8854   (when (memq where gnus-updated-mode-lines)
8855     (let (mode-string)
8856       (save-excursion
8857         ;; We evaluate this in the summary buffer since these
8858         ;; variables are buffer-local to that buffer.
8859         (set-buffer gnus-summary-buffer)
8860         ;; We bind all these variables that are used in the `eval' form
8861         ;; below.
8862         (let* ((mformat (symbol-value
8863                          (intern
8864                           (format "gnus-%s-mode-line-format-spec" where))))
8865                (gnus-tmp-group-name gnus-newsgroup-name)
8866                (gnus-tmp-article-number (or gnus-current-article 0))
8867                (gnus-tmp-unread gnus-newsgroup-unreads)
8868                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8869                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8870                (gnus-tmp-unread-and-unselected
8871                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8872                             (zerop gnus-tmp-unselected)) "")
8873                       ((zerop gnus-tmp-unselected)
8874                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8875                       (t (format "{%d(+%d) more}"
8876                                  gnus-tmp-unread-and-unticked
8877                                  gnus-tmp-unselected))))
8878                (gnus-tmp-subject
8879                 (if (and gnus-current-headers
8880                          (vectorp gnus-current-headers))
8881                     (mail-header-subject gnus-current-headers) ""))
8882                max-len
8883                gnus-tmp-header);; passed as argument to any user-format-funcs
8884           (setq mode-string (eval mformat))
8885           (setq max-len (max 4 (if gnus-mode-non-string-length
8886                                    (- (window-width)
8887                                       gnus-mode-non-string-length)
8888                                  (length mode-string))))
8889           ;; We might have to chop a bit of the string off...
8890           (when (> (length mode-string) max-len)
8891             (setq mode-string
8892                   (concat (gnus-truncate-string mode-string (- max-len 3))
8893                           "...")))
8894           ;; Pad the mode string a bit.
8895           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8896       ;; Update the mode line.
8897       (setq mode-line-buffer-identification (list mode-string))
8898       (set-buffer-modified-p t))))
8899
8900 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8901   "Go through the HEADERS list and add all Xrefs to a hash table.
8902 The resulting hash table is returned, or nil if no Xrefs were found."
8903   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8904          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8905          (xref-hashtb (make-vector 63 0))
8906          start group entry number xrefs header)
8907     (while headers
8908       (setq header (pop headers))
8909       (when (and (setq xrefs (mail-header-xref header))
8910                  (not (memq (setq number (mail-header-number header))
8911                             unreads)))
8912         (setq start 0)
8913         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8914           (setq start (match-end 0))
8915           (setq group (if prefix
8916                           (concat prefix (substring xrefs (match-beginning 1)
8917                                                     (match-end 1)))
8918                         (substring xrefs (match-beginning 1) (match-end 1))))
8919           (setq number
8920                 (string-to-int (substring xrefs (match-beginning 2)
8921                                           (match-end 2))))
8922           (if (setq entry (gnus-gethash group xref-hashtb))
8923               (setcdr entry (cons number (cdr entry)))
8924             (gnus-sethash group (cons number nil) xref-hashtb)))))
8925     (and start xref-hashtb)))
8926
8927 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8928   "Look through all the headers and mark the Xrefs as read."
8929   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8930         name entry info xref-hashtb idlist method nth4)
8931     (save-excursion
8932       (set-buffer gnus-group-buffer)
8933       (when (setq xref-hashtb
8934                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8935         (mapatoms
8936          (lambda (group)
8937            (unless (string= from-newsgroup (setq name (symbol-name group)))
8938              (setq idlist (symbol-value group))
8939              ;; Dead groups are not updated.
8940              (and (prog1
8941                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8942                             info (nth 2 entry))
8943                     (if (stringp (setq nth4 (gnus-info-method info)))
8944                         (setq nth4 (gnus-server-to-method nth4))))
8945                   ;; Only do the xrefs if the group has the same
8946                   ;; select method as the group we have just read.
8947                   (or (gnus-methods-equal-p
8948                        nth4 (gnus-find-method-for-group from-newsgroup))
8949                       virtual
8950                       (equal nth4 (setq method (gnus-find-method-for-group
8951                                                 from-newsgroup)))
8952                       (and (equal (car nth4) (car method))
8953                            (equal (nth 1 nth4) (nth 1 method))))
8954                   gnus-use-cross-reference
8955                   (or (not (eq gnus-use-cross-reference t))
8956                       virtual
8957                       ;; Only do cross-references on subscribed
8958                       ;; groups, if that is what is wanted.
8959                       (<= (gnus-info-level info) gnus-level-subscribed))
8960                   (gnus-group-make-articles-read name idlist))))
8961          xref-hashtb)))))
8962
8963 (defun gnus-group-make-articles-read (group articles)
8964   (let* ((num 0)
8965          (entry (gnus-gethash group gnus-newsrc-hashtb))
8966          (info (nth 2 entry))
8967          (active (gnus-active group))
8968          range)
8969     ;; First peel off all illegal article numbers.
8970     (if active
8971         (let ((ids articles)
8972               id first)
8973           (while ids
8974             (setq id (car ids))
8975             (if (and first (> id (cdr active)))
8976                 (progn
8977                   ;; We'll end up in this situation in one particular
8978                   ;; obscure situation.  If you re-scan a group and get
8979                   ;; a new article that is cross-posted to a different
8980                   ;; group that has not been re-scanned, you might get
8981                   ;; crossposted article that has a higher number than
8982                   ;; Gnus believes possible.  So we re-activate this
8983                   ;; group as well.  This might mean doing the
8984                   ;; crossposting thingy will *increase* the number
8985                   ;; of articles in some groups.  Tsk, tsk.
8986                   (setq active (or (gnus-activate-group group) active))))
8987             (if (or (> id (cdr active))
8988                     (< id (car active)))
8989                 (setq articles (delq id articles)))
8990             (setq ids (cdr ids)))))
8991     ;; If the read list is nil, we init it.
8992     (and active
8993          (null (gnus-info-read info))
8994          (> (car active) 1)
8995          (gnus-info-set-read info (cons 1 (1- (car active)))))
8996     ;; Then we add the read articles to the range.
8997     (gnus-info-set-read
8998      info
8999      (setq range
9000            (gnus-add-to-range
9001             (gnus-info-read info) (setq articles (sort articles '<)))))
9002     ;; Then we have to re-compute how many unread
9003     ;; articles there are in this group.
9004     (if active
9005         (progn
9006           (cond
9007            ((not range)
9008             (setq num (- (1+ (cdr active)) (car active))))
9009            ((not (listp (cdr range)))
9010             (setq num (- (cdr active) (- (1+ (cdr range))
9011                                          (car range)))))
9012            (t
9013             (while range
9014               (if (numberp (car range))
9015                   (setq num (1+ num))
9016                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9017               (setq range (cdr range)))
9018             (setq num (- (cdr active) num))))
9019           ;; Update the number of unread articles.
9020           (setcar entry num)
9021           ;; Update the group buffer.
9022           (gnus-group-update-group group t)))))
9023
9024 (defun gnus-methods-equal-p (m1 m2)
9025   (let ((m1 (or m1 gnus-select-method))
9026         (m2 (or m2 gnus-select-method)))
9027     (or (equal m1 m2)
9028         (and (eq (car m1) (car m2))
9029              (or (not (memq 'address (assoc (symbol-name (car m1))
9030                                             gnus-valid-select-methods)))
9031                  (equal (nth 1 m1) (nth 1 m2)))))))
9032
9033 (defsubst gnus-header-value ()
9034   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9035
9036 (defvar gnus-newsgroup-none-id 0)
9037
9038 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9039   (let ((cur nntp-server-buffer)
9040         (dependencies
9041          (or dependencies
9042              (save-excursion (set-buffer gnus-summary-buffer)
9043                              gnus-newsgroup-dependencies)))
9044         headers id id-dep ref-dep end ref)
9045     (save-excursion
9046       (set-buffer nntp-server-buffer)
9047       (run-hooks 'gnus-parse-headers-hook)
9048       (let ((case-fold-search t)
9049             in-reply-to header p lines)
9050         (goto-char (point-min))
9051         ;; Search to the beginning of the next header.  Error messages
9052         ;; do not begin with 2 or 3.
9053         (while (re-search-forward "^[23][0-9]+ " nil t)
9054           (setq id nil
9055                 ref nil)
9056           ;; This implementation of this function, with nine
9057           ;; search-forwards instead of the one re-search-forward and
9058           ;; a case (which basically was the old function) is actually
9059           ;; about twice as fast, even though it looks messier.  You
9060           ;; can't have everything, I guess.  Speed and elegance
9061           ;; doesn't always go hand in hand.
9062           (setq
9063            header
9064            (vector
9065             ;; Number.
9066             (prog1
9067                 (read cur)
9068               (end-of-line)
9069               (setq p (point))
9070               (narrow-to-region (point)
9071                                 (or (and (search-forward "\n.\n" nil t)
9072                                          (- (point) 2))
9073                                     (point))))
9074             ;; Subject.
9075             (progn
9076               (goto-char p)
9077               (if (search-forward "\nsubject: " nil t)
9078                   (gnus-header-value) "(none)"))
9079             ;; From.
9080             (progn
9081               (goto-char p)
9082               (if (search-forward "\nfrom: " nil t)
9083                   (gnus-header-value) "(nobody)"))
9084             ;; Date.
9085             (progn
9086               (goto-char p)
9087               (if (search-forward "\ndate: " nil t)
9088                   (gnus-header-value) ""))
9089             ;; Message-ID.
9090             (progn
9091               (goto-char p)
9092               (if (search-forward "\nmessage-id: " nil t)
9093                   (setq id (gnus-header-value))
9094                 ;; If there was no message-id, we just fake one to make
9095                 ;; subsequent routines simpler.
9096                 (setq id (concat "none+"
9097                                  (int-to-string
9098                                   (setq gnus-newsgroup-none-id
9099                                         (1+ gnus-newsgroup-none-id)))))))
9100             ;; References.
9101             (progn
9102               (goto-char p)
9103               (if (search-forward "\nreferences: " nil t)
9104                   (prog1
9105                       (gnus-header-value)
9106                     (setq end (match-end 0))
9107                     (save-excursion
9108                       (setq ref
9109                             (buffer-substring
9110                              (progn
9111                                (end-of-line)
9112                                (search-backward ">" end t)
9113                                (1+ (point)))
9114                              (progn
9115                                (search-backward "<" end t)
9116                                (point))))))
9117                 ;; Get the references from the in-reply-to header if there
9118                 ;; were no references and the in-reply-to header looks
9119                 ;; promising.
9120                 (if (and (search-forward "\nin-reply-to: " nil t)
9121                          (setq in-reply-to (gnus-header-value))
9122                          (string-match "<[^>]+>" in-reply-to))
9123                     (setq ref (substring in-reply-to (match-beginning 0)
9124                                          (match-end 0)))
9125                   (setq ref ""))))
9126             ;; Chars.
9127             0
9128             ;; Lines.
9129             (progn
9130               (goto-char p)
9131               (if (search-forward "\nlines: " nil t)
9132                   (if (numberp (setq lines (read cur)))
9133                       lines 0)
9134                 0))
9135             ;; Xref.
9136             (progn
9137               (goto-char p)
9138               (and (search-forward "\nxref: " nil t)
9139                    (gnus-header-value)))))
9140           ;; We do the threading while we read the headers.  The
9141           ;; message-id and the last reference are both entered into
9142           ;; the same hash table.  Some tippy-toeing around has to be
9143           ;; done in case an article has arrived before the article
9144           ;; which it refers to.
9145           (if (boundp (setq id-dep (intern id dependencies)))
9146               (if (and (car (symbol-value id-dep))
9147                        (not force-new))
9148                   ;; An article with this Message-ID has already
9149                   ;; been seen, so we ignore this one, except we add
9150                   ;; any additional Xrefs (in case the two articles
9151                   ;; came from different servers).
9152                   (progn
9153                     (mail-header-set-xref
9154                      (car (symbol-value id-dep))
9155                      (concat (or (mail-header-xref
9156                                   (car (symbol-value id-dep))) "")
9157                              (or (mail-header-xref header) "")))
9158                     (setq header nil))
9159                 (setcar (symbol-value id-dep) header))
9160             (set id-dep (list header)))
9161           (when header
9162             (if (boundp (setq ref-dep (intern ref dependencies)))
9163                 (setcdr (symbol-value ref-dep)
9164                         (nconc (cdr (symbol-value ref-dep))
9165                                (list (symbol-value id-dep))))
9166               (set ref-dep (list nil (symbol-value id-dep))))
9167             (setq headers (cons header headers)))
9168           (goto-char (point-max))
9169           (widen))
9170         (nreverse headers)))))
9171
9172 ;; The following macros and functions were written by Felix Lee
9173 ;; <flee@cse.psu.edu>.
9174
9175 (defmacro gnus-nov-read-integer ()
9176   '(prog1
9177        (if (= (following-char) ?\t)
9178            0
9179          (let ((num (condition-case nil (read buffer) (error nil))))
9180            (if (numberp num) num 0)))
9181      (or (eobp) (forward-char 1))))
9182
9183 (defmacro gnus-nov-skip-field ()
9184   '(search-forward "\t" eol 'move))
9185
9186 (defmacro gnus-nov-field ()
9187   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9188
9189 ;; Goes through the xover lines and returns a list of vectors
9190 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9191                                                   force-new dependencies)
9192   "Parse the news overview data in the server buffer, and return a
9193 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9194   ;; Get the Xref when the users reads the articles since most/some
9195   ;; NNTP servers do not include Xrefs when using XOVER.
9196   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9197   (let ((cur nntp-server-buffer)
9198         (dependencies (or dependencies gnus-newsgroup-dependencies))
9199         number headers header)
9200     (save-excursion
9201       (set-buffer nntp-server-buffer)
9202       ;; Allow the user to mangle the headers before parsing them.
9203       (run-hooks 'gnus-parse-headers-hook)
9204       (goto-char (point-min))
9205       (while (and sequence (not (eobp)))
9206         (setq number (read cur))
9207         (while (and sequence (< (car sequence) number))
9208           (setq sequence (cdr sequence)))
9209         (and sequence
9210              (eq number (car sequence))
9211              (progn
9212                (setq sequence (cdr sequence))
9213                (if (setq header
9214                          (inline (gnus-nov-parse-line
9215                                   number dependencies force-new)))
9216                    (setq headers (cons header headers)))))
9217         (forward-line 1))
9218       (setq headers (nreverse headers)))
9219     headers))
9220
9221 ;; This function has to be called with point after the article number
9222 ;; on the beginning of the line.
9223 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9224   (let ((none 0)
9225         (eol (gnus-point-at-eol))
9226         (buffer (current-buffer))
9227         header ref id id-dep ref-dep)
9228
9229     ;; overview: [num subject from date id refs chars lines misc]
9230     (narrow-to-region (point) eol)
9231     (or (eobp) (forward-char))
9232
9233     (condition-case nil
9234         (setq header
9235               (vector
9236                number                   ; number
9237                (gnus-nov-field)         ; subject
9238                (gnus-nov-field)         ; from
9239                (gnus-nov-field)         ; date
9240                (setq id (or (gnus-nov-field)
9241                             (concat "none+"
9242                                     (int-to-string
9243                                      (setq none (1+ none)))))) ; id
9244                (progn
9245                  (save-excursion
9246                    (let ((beg (point)))
9247                      (search-forward "\t" eol)
9248                      (if (search-backward ">" beg t)
9249                          (setq ref
9250                                (buffer-substring
9251                                 (1+ (point))
9252                                 (search-backward "<" beg t)))
9253                        (setq ref nil))))
9254                  (gnus-nov-field))      ; refs
9255                (gnus-nov-read-integer)  ; chars
9256                (gnus-nov-read-integer)  ; lines
9257                (if (= (following-char) ?\n)
9258                    nil
9259                  (gnus-nov-field))      ; misc
9260                ))
9261       (error (progn
9262                (gnus-error 4 "Strange nov line")
9263                (setq header nil)
9264                (goto-char eol))))
9265
9266     (widen)
9267
9268     ;; We build the thread tree.
9269     (when header
9270       (if (boundp (setq id-dep (intern id dependencies)))
9271           (if (and (car (symbol-value id-dep))
9272                    (not force-new))
9273               ;; An article with this Message-ID has already been seen,
9274               ;; so we ignore this one, except we add any additional
9275               ;; Xrefs (in case the two articles came from different
9276               ;; servers.
9277               (progn
9278                 (mail-header-set-xref
9279                  (car (symbol-value id-dep))
9280                  (concat (or (mail-header-xref
9281                               (car (symbol-value id-dep))) "")
9282                          (or (mail-header-xref header) "")))
9283                 (setq header nil))
9284             (setcar (symbol-value id-dep) header))
9285         (set id-dep (list header))))
9286     (if header
9287         (progn
9288           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9289               (setcdr (symbol-value ref-dep)
9290                       (nconc (cdr (symbol-value ref-dep))
9291                              (list (symbol-value id-dep))))
9292             (set ref-dep (list nil (symbol-value id-dep))))))
9293     header))
9294
9295 (defun gnus-article-get-xrefs ()
9296   "Fill in the Xref value in `gnus-current-headers', if necessary.
9297 This is meant to be called in `gnus-article-internal-prepare-hook'."
9298   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9299                                  gnus-current-headers)))
9300     (or (not gnus-use-cross-reference)
9301         (not headers)
9302         (and (mail-header-xref headers)
9303              (not (string= (mail-header-xref headers) "")))
9304         (let ((case-fold-search t)
9305               xref)
9306           (save-restriction
9307             (nnheader-narrow-to-headers)
9308             (goto-char (point-min))
9309             (if (or (and (eq (downcase (following-char)) ?x)
9310                          (looking-at "Xref:"))
9311                     (search-forward "\nXref:" nil t))
9312                 (progn
9313                   (goto-char (1+ (match-end 0)))
9314                   (setq xref (buffer-substring (point)
9315                                                (progn (end-of-line) (point))))
9316                   (mail-header-set-xref headers xref))))))))
9317
9318 (defun gnus-summary-insert-subject (id &optional old-header)
9319   "Find article ID and insert the summary line for that article."
9320   (let ((header (gnus-read-header id))
9321         (number (and (numberp id) id))
9322         pos)
9323     (when header
9324       ;; Rebuild the thread that this article is part of and go to the
9325       ;; article we have fetched.
9326       (when old-header
9327         (when (setq pos (text-property-any
9328                          (point-min) (point-max) 'gnus-number 
9329                          (mail-header-number old-header)))
9330           (goto-char pos)
9331           (gnus-delete-line)
9332           (gnus-data-remove (mail-header-number old-header))))
9333       (gnus-rebuild-thread (mail-header-id header))
9334       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9335     (when (and (numberp number)
9336                (> number 0))
9337       ;; We have to update the boundaries even if we can't fetch the
9338       ;; article if ID is a number -- so that the next `P' or `N'
9339       ;; command will fetch the previous (or next) article even
9340       ;; if the one we tried to fetch this time has been canceled.
9341       (and (> number gnus-newsgroup-end)
9342            (setq gnus-newsgroup-end number))
9343       (and (< number gnus-newsgroup-begin)
9344            (setq gnus-newsgroup-begin number))
9345       (setq gnus-newsgroup-unselected
9346             (delq number gnus-newsgroup-unselected)))
9347     ;; Report back a success?
9348     (and header (mail-header-number header))))
9349
9350 (defun gnus-summary-work-articles (n)
9351   "Return a list of articles to be worked upon.  The prefix argument,
9352 the list of process marked articles, and the current article will be
9353 taken into consideration."
9354   (cond
9355    ((and n (numberp n))
9356     ;; A numerical prefix has been given.
9357     (let ((backward (< n 0))
9358           (n (abs n))
9359           articles article)
9360       (save-excursion
9361         (while
9362             (and (> n 0)
9363                  (push (setq article (gnus-summary-article-number))
9364                        articles)
9365                  (if backward
9366                      (gnus-summary-find-prev nil article)
9367                    (gnus-summary-find-next nil article)))
9368           (decf n)))
9369       (nreverse articles)))
9370    ((and (boundp 'transient-mark-mode)
9371          transient-mark-mode
9372          mark-active)
9373     ;; Work on the region between point and mark.
9374     (let ((max (max (point) (mark)))
9375           articles article)
9376       (save-excursion
9377         (goto-char (min (point) (mark)))
9378         (while
9379             (and
9380              (push (setq article (gnus-summary-article-number)) articles)
9381              (gnus-summary-find-next nil article)
9382              (< (point) max)))
9383         (nreverse articles))))
9384    (gnus-newsgroup-processable
9385     ;; There are process-marked articles present.
9386     (reverse gnus-newsgroup-processable))
9387    (t
9388     ;; Just return the current article.
9389     (list (gnus-summary-article-number)))))
9390
9391 (defun gnus-summary-search-group (&optional backward use-level)
9392   "Search for next unread newsgroup.
9393 If optional argument BACKWARD is non-nil, search backward instead."
9394   (save-excursion
9395     (set-buffer gnus-group-buffer)
9396     (if (gnus-group-search-forward
9397          backward nil (if use-level (gnus-group-group-level) nil))
9398         (gnus-group-group-name))))
9399
9400 (defun gnus-summary-best-group (&optional exclude-group)
9401   "Find the name of the best unread group.
9402 If EXCLUDE-GROUP, do not go to this group."
9403   (save-excursion
9404     (set-buffer gnus-group-buffer)
9405     (save-excursion
9406       (gnus-group-best-unread-group exclude-group))))
9407
9408 (defun gnus-summary-find-next (&optional unread article backward)
9409   (if backward (gnus-summary-find-prev)
9410     (let* ((dummy (gnus-summary-article-intangible-p))
9411            (article (or article (gnus-summary-article-number)))
9412            (arts (gnus-data-find-list article))
9413            result)
9414       (when (and (not dummy)
9415                  (or (not gnus-summary-check-current)
9416                      (not unread)
9417                      (not (gnus-data-unread-p (car arts)))))
9418         (setq arts (cdr arts)))
9419       (when (setq result
9420                   (if unread
9421                       (progn
9422                         (while arts
9423                           (when (gnus-data-unread-p (car arts))
9424                             (setq result (car arts)
9425                                   arts nil))
9426                           (setq arts (cdr arts)))
9427                         result)
9428                     (car arts)))
9429         (goto-char (gnus-data-pos result))
9430         (gnus-data-number result)))))
9431
9432 (defun gnus-summary-find-prev (&optional unread article)
9433   (let* ((article (or article (gnus-summary-article-number)))
9434          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9435          result)
9436     (when (or (not gnus-summary-check-current)
9437               (not unread)
9438               (not (gnus-data-unread-p (car arts))))
9439       (setq arts (cdr arts)))
9440     (if (setq result
9441               (if unread
9442                   (progn
9443                     (while arts
9444                       (and (gnus-data-unread-p (car arts))
9445                            (setq result (car arts)
9446                                  arts nil))
9447                       (setq arts (cdr arts)))
9448                     result)
9449                 (car arts)))
9450         (progn
9451           (goto-char (gnus-data-pos result))
9452           (gnus-data-number result)))))
9453
9454 (defun gnus-summary-find-subject (subject &optional unread backward article)
9455   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9456          (article (or article (gnus-summary-article-number)))
9457          (articles (gnus-data-list backward))
9458          (arts (gnus-data-find-list article articles))
9459          result)
9460     (when (or (not gnus-summary-check-current)
9461               (not unread)
9462               (not (gnus-data-unread-p (car arts))))
9463       (setq arts (cdr arts)))
9464     (while arts
9465       (and (or (not unread)
9466                (gnus-data-unread-p (car arts)))
9467            (vectorp (gnus-data-header (car arts)))
9468            (gnus-subject-equal
9469             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9470            (setq result (car arts)
9471                  arts nil))
9472       (setq arts (cdr arts)))
9473     (and result
9474          (goto-char (gnus-data-pos result))
9475          (gnus-data-number result))))
9476
9477 (defun gnus-summary-search-forward (&optional unread subject backward)
9478   "Search forward for an article.
9479 If UNREAD, look for unread articles.  If SUBJECT, look for
9480 articles with that subject.  If BACKWARD, search backward instead."
9481   (cond (subject (gnus-summary-find-subject subject unread backward))
9482         (backward (gnus-summary-find-prev unread))
9483         (t (gnus-summary-find-next unread))))
9484
9485 (defun gnus-recenter (&optional n)
9486   "Center point in window and redisplay frame.
9487 Also do horizontal recentering."
9488   (interactive "P")
9489   (when (and gnus-auto-center-summary
9490              (not (eq gnus-auto-center-summary 'vertical)))
9491     (gnus-horizontal-recenter))
9492   (recenter n))
9493
9494 (defun gnus-summary-recenter ()
9495   "Center point in the summary window.
9496 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9497 displayed, no centering will be performed."
9498   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9499   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9500   (let* ((top (cond ((< (window-height) 4) 0)
9501                     ((< (window-height) 7) 1)
9502                     (t 2)))
9503          (height (1- (window-height)))
9504          (bottom (save-excursion (goto-char (point-max))
9505                                  (forward-line (- height))
9506                                  (point)))
9507          (window (get-buffer-window (current-buffer))))
9508     ;; The user has to want it.
9509     (when gnus-auto-center-summary
9510       (when (get-buffer-window gnus-article-buffer)
9511        ;; Only do recentering when the article buffer is displayed,
9512        ;; Set the window start to either `bottom', which is the biggest
9513        ;; possible valid number, or the second line from the top,
9514        ;; whichever is the least.
9515        (set-window-start
9516         window (min bottom (save-excursion 
9517                              (forward-line (- top)) (point)))))
9518       ;; Do horizontal recentering while we're at it.
9519       (when (and (get-buffer-window (current-buffer) t)
9520                  (not (eq gnus-auto-center-summary 'vertical)))
9521         (let ((selected (selected-window)))
9522           (select-window (get-buffer-window (current-buffer) t))
9523           (gnus-summary-position-point)
9524           (gnus-horizontal-recenter)
9525           (select-window selected))))))
9526
9527 (defun gnus-horizontal-recenter ()
9528   "Recenter the current buffer horizontally."
9529   (if (< (current-column) (/ (window-width) 2))
9530       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9531     (let* ((orig (point))
9532            (end (window-end (get-buffer-window (current-buffer) t)))
9533            (max 0))
9534       ;; Find the longest line currently displayed in the window.
9535       (goto-char (window-start))
9536       (while (and (not (eobp)) 
9537                   (< (point) end))
9538         (end-of-line)
9539         (setq max (max max (current-column)))
9540         (forward-line 1))
9541       (goto-char orig)
9542       ;; Scroll horizontally to center (sort of) the point.
9543       (if (> max (window-width))
9544           (set-window-hscroll 
9545            (get-buffer-window (current-buffer) t)
9546            (min (- (current-column) (/ (window-width) 3))
9547                 (+ 2 (- max (window-width)))))
9548         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9549       max)))
9550
9551 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9552 (defun gnus-short-group-name (group &optional levels)
9553   "Collapse GROUP name LEVELS."
9554   (let* ((name "") 
9555          (foreign "")
9556          (depth 0) 
9557          (skip 1)
9558          (levels (or levels
9559                      (progn
9560                        (while (string-match "\\." group skip)
9561                          (setq skip (match-end 0)
9562                                depth (+ depth 1)))
9563                        depth))))
9564     (if (string-match ":" group)
9565         (setq foreign (substring group 0 (match-end 0))
9566               group (substring group (match-end 0))))
9567     (while group
9568       (if (and (string-match "\\." group)
9569                (> levels (- gnus-group-uncollapsed-levels 1)))
9570           (setq name (concat name (substring group 0 1))
9571                 group (substring group (match-end 0))
9572                 levels (- levels 1)
9573                 name (concat name "."))
9574         (setq name (concat foreign name group)
9575               group nil)))
9576     name))
9577
9578 (defun gnus-summary-jump-to-group (newsgroup)
9579   "Move point to NEWSGROUP in group mode buffer."
9580   ;; Keep update point of group mode buffer if visible.
9581   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9582       (save-window-excursion
9583         ;; Take care of tree window mode.
9584         (if (get-buffer-window gnus-group-buffer)
9585             (pop-to-buffer gnus-group-buffer))
9586         (gnus-group-jump-to-group newsgroup))
9587     (save-excursion
9588       ;; Take care of tree window mode.
9589       (if (get-buffer-window gnus-group-buffer)
9590           (pop-to-buffer gnus-group-buffer)
9591         (set-buffer gnus-group-buffer))
9592       (gnus-group-jump-to-group newsgroup))))
9593
9594 ;; This function returns a list of article numbers based on the
9595 ;; difference between the ranges of read articles in this group and
9596 ;; the range of active articles.
9597 (defun gnus-list-of-unread-articles (group)
9598   (let* ((read (gnus-info-read (gnus-get-info group)))
9599          (active (gnus-active group))
9600          (last (cdr active))
9601          first nlast unread)
9602     ;; If none are read, then all are unread.
9603     (if (not read)
9604         (setq first (car active))
9605       ;; If the range of read articles is a single range, then the
9606       ;; first unread article is the article after the last read
9607       ;; article.  Sounds logical, doesn't it?
9608       (if (not (listp (cdr read)))
9609           (setq first (1+ (cdr read)))
9610         ;; `read' is a list of ranges.
9611         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9612                                 (caar read))) 1)
9613             (setq first 1))
9614         (while read
9615           (if first
9616               (while (< first nlast)
9617                 (setq unread (cons first unread))
9618                 (setq first (1+ first))))
9619           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9620           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9621           (setq read (cdr read)))))
9622     ;; And add the last unread articles.
9623     (while (<= first last)
9624       (setq unread (cons first unread))
9625       (setq first (1+ first)))
9626     ;; Return the list of unread articles.
9627     (nreverse unread)))
9628
9629 (defun gnus-list-of-read-articles (group)
9630   "Return a list of unread, unticked and non-dormant articles."
9631   (let* ((info (gnus-get-info group))
9632          (marked (gnus-info-marks info))
9633          (active (gnus-active group)))
9634     (and info active
9635          (gnus-set-difference
9636           (gnus-sorted-complement
9637            (gnus-uncompress-range active)
9638            (gnus-list-of-unread-articles group))
9639           (append
9640            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9641            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9642
9643 ;; Various summary commands
9644
9645 (defun gnus-summary-universal-argument (arg)
9646   "Perform any operation on all articles that are process/prefixed."
9647   (interactive "P")
9648   (gnus-set-global-variables)
9649   (let ((articles (gnus-summary-work-articles arg))
9650         func article)
9651     (if (eq
9652          (setq
9653           func
9654           (key-binding
9655            (read-key-sequence
9656             (substitute-command-keys
9657              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9658              ))))
9659          'undefined)
9660         (gnus-error 1 "Undefined key")
9661       (save-excursion
9662         (while articles
9663           (gnus-summary-goto-subject (setq article (pop articles)))
9664           (command-execute func)
9665           (gnus-summary-remove-process-mark article)))))
9666   (gnus-summary-position-point))
9667
9668 (defun gnus-summary-toggle-truncation (&optional arg)
9669   "Toggle truncation of summary lines.
9670 With arg, turn line truncation on iff arg is positive."
9671   (interactive "P")
9672   (setq truncate-lines
9673         (if (null arg) (not truncate-lines)
9674           (> (prefix-numeric-value arg) 0)))
9675   (redraw-display))
9676
9677 (defun gnus-summary-reselect-current-group (&optional all rescan)
9678   "Exit and then reselect the current newsgroup.
9679 The prefix argument ALL means to select all articles."
9680   (interactive "P")
9681   (gnus-set-global-variables)
9682   (let ((current-subject (gnus-summary-article-number))
9683         (group gnus-newsgroup-name))
9684     (setq gnus-newsgroup-begin nil)
9685     (gnus-summary-exit)
9686     ;; We have to adjust the point of group mode buffer because the
9687     ;; current point was moved to the next unread newsgroup by
9688     ;; exiting.
9689     (gnus-summary-jump-to-group group)
9690     (when rescan
9691       (save-excursion
9692         (gnus-group-get-new-news-this-group 1)))
9693     (gnus-group-read-group all t)
9694     (gnus-summary-goto-subject current-subject)))
9695
9696 (defun gnus-summary-rescan-group (&optional all)
9697   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9698   (interactive "P")
9699   (gnus-summary-reselect-current-group all t))
9700
9701 (defun gnus-summary-update-info ()
9702   (let* ((group gnus-newsgroup-name))
9703     (when gnus-newsgroup-kill-headers
9704       (setq gnus-newsgroup-killed
9705             (gnus-compress-sequence
9706              (nconc
9707               (gnus-set-sorted-intersection
9708                (gnus-uncompress-range gnus-newsgroup-killed)
9709                (setq gnus-newsgroup-unselected
9710                      (sort gnus-newsgroup-unselected '<)))
9711               (setq gnus-newsgroup-unreads
9712                     (sort gnus-newsgroup-unreads '<))) t)))
9713     (unless (listp (cdr gnus-newsgroup-killed))
9714       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9715     (let ((headers gnus-newsgroup-headers))
9716       (run-hooks 'gnus-exit-group-hook)
9717       (unless gnus-save-score
9718         (setq gnus-newsgroup-scored nil))
9719       ;; Set the new ranges of read articles.
9720       (gnus-update-read-articles
9721        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9722       ;; Set the current article marks.
9723       (gnus-update-marks)
9724       ;; Do the cross-ref thing.
9725       (when gnus-use-cross-reference
9726         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9727       ;; Do adaptive scoring, and possibly save score files.
9728       (when gnus-newsgroup-adaptive
9729         (gnus-score-adaptive))
9730       (when gnus-use-scoring
9731         (gnus-score-save))
9732       ;; Do not switch windows but change the buffer to work.
9733       (set-buffer gnus-group-buffer)
9734       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9735           (gnus-group-update-group group)))))
9736
9737 (defun gnus-summary-exit (&optional temporary)
9738   "Exit reading current newsgroup, and then return to group selection mode.
9739 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9740   (interactive)
9741   (gnus-set-global-variables)
9742   (gnus-kill-save-kill-buffer)
9743   (let* ((group gnus-newsgroup-name)
9744          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9745          (mode major-mode)
9746          (buf (current-buffer)))
9747     (run-hooks 'gnus-summary-prepare-exit-hook)
9748     ;; If we have several article buffers, we kill them at exit.
9749     (unless gnus-single-article-buffer
9750       (gnus-kill-buffer gnus-article-buffer)
9751       (gnus-kill-buffer gnus-original-article-buffer)
9752       (setq gnus-article-current nil))
9753     (when gnus-use-cache
9754       (gnus-cache-possibly-remove-articles)
9755       (gnus-cache-save-buffers))
9756     (when gnus-use-trees
9757       (gnus-tree-close group))
9758     ;; Make all changes in this group permanent.
9759     (unless quit-config
9760       (gnus-summary-update-info))
9761     (gnus-close-group group)
9762     ;; Make sure where I was, and go to next newsgroup.
9763     (set-buffer gnus-group-buffer)
9764     (unless quit-config
9765       (gnus-group-jump-to-group group)
9766       (gnus-group-next-unread-group 1))
9767     (run-hooks 'gnus-summary-exit-hook)
9768     (unless gnus-single-article-buffer
9769       (setq gnus-article-current nil))
9770     (if temporary
9771         nil                             ;Nothing to do.
9772       ;; If we have several article buffers, we kill them at exit.
9773       (unless gnus-single-article-buffer
9774         (gnus-kill-buffer gnus-article-buffer)
9775         (gnus-kill-buffer gnus-original-article-buffer)
9776         (setq gnus-article-current nil))
9777       (set-buffer buf)
9778       (if (not gnus-kill-summary-on-exit)
9779           (gnus-deaden-summary)
9780         ;; We set all buffer-local variables to nil.  It is unclear why
9781         ;; this is needed, but if we don't, buffer-local variables are
9782         ;; not garbage-collected, it seems.  This would the lead to en
9783         ;; ever-growing Emacs.
9784         (gnus-summary-clear-local-variables)
9785         (when (get-buffer gnus-article-buffer)
9786           (bury-buffer gnus-article-buffer))
9787         ;; We clear the global counterparts of the buffer-local
9788         ;; variables as well, just to be on the safe side.
9789         (gnus-configure-windows 'group 'force)
9790         (gnus-summary-clear-local-variables)
9791         ;; Return to group mode buffer.
9792         (if (eq mode 'gnus-summary-mode)
9793             (gnus-kill-buffer buf)))
9794       (setq gnus-current-select-method gnus-select-method)
9795       (pop-to-buffer gnus-group-buffer)
9796       ;; Clear the current group name.
9797       (if (not quit-config)
9798           (progn
9799             (gnus-group-jump-to-group group)
9800             (gnus-group-next-unread-group 1)
9801             (gnus-configure-windows 'group 'force))
9802         (if (not (buffer-name (car quit-config)))
9803             (gnus-configure-windows 'group 'force)
9804           (set-buffer (car quit-config))
9805           (and (eq major-mode 'gnus-summary-mode)
9806                (gnus-set-global-variables))
9807           (gnus-configure-windows (cdr quit-config))))
9808       (unless quit-config
9809         (setq gnus-newsgroup-name nil)))))
9810
9811 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9812 (defun gnus-summary-exit-no-update (&optional no-questions)
9813   "Quit reading current newsgroup without updating read article info."
9814   (interactive)
9815   (gnus-set-global-variables)
9816   (let* ((group gnus-newsgroup-name)
9817          (quit-config (gnus-group-quit-config group)))
9818     (when (or no-questions
9819               gnus-expert-user
9820               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9821       ;; If we have several article buffers, we kill them at exit.
9822       (unless gnus-single-article-buffer
9823         (gnus-kill-buffer gnus-article-buffer)
9824         (gnus-kill-buffer gnus-original-article-buffer)
9825         (setq gnus-article-current nil))
9826       (if (not gnus-kill-summary-on-exit)
9827           (gnus-deaden-summary)
9828         (gnus-close-group group)
9829         (gnus-summary-clear-local-variables)
9830         (set-buffer gnus-group-buffer)
9831         (gnus-summary-clear-local-variables)
9832         (when (get-buffer gnus-summary-buffer)
9833           (kill-buffer gnus-summary-buffer)))
9834       (unless gnus-single-article-buffer
9835         (setq gnus-article-current nil))
9836       (when gnus-use-trees
9837         (gnus-tree-close group))
9838       (when (get-buffer gnus-article-buffer)
9839         (bury-buffer gnus-article-buffer))
9840       ;; Return to the group buffer.
9841       (gnus-configure-windows 'group 'force)
9842       ;; Clear the current group name.
9843       (setq gnus-newsgroup-name nil)
9844       (when (equal (gnus-group-group-name) group)
9845         (gnus-group-next-unread-group 1))
9846       (when quit-config
9847         (if (not (buffer-name (car quit-config)))
9848             (gnus-configure-windows 'group 'force)
9849           (set-buffer (car quit-config))
9850           (when (eq major-mode 'gnus-summary-mode)
9851             (gnus-set-global-variables))
9852           (gnus-configure-windows (cdr quit-config)))))))
9853
9854 ;;; Dead summaries.
9855
9856 (defvar gnus-dead-summary-mode-map nil)
9857
9858 (if gnus-dead-summary-mode-map
9859     nil
9860   (setq gnus-dead-summary-mode-map (make-keymap))
9861   (suppress-keymap gnus-dead-summary-mode-map)
9862   (substitute-key-definition
9863    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9864   (let ((keys '("\C-d" "\r" "\177")))
9865     (while keys
9866       (define-key gnus-dead-summary-mode-map
9867         (pop keys) 'gnus-summary-wake-up-the-dead))))
9868
9869 (defvar gnus-dead-summary-mode nil
9870   "Minor mode for Gnus summary buffers.")
9871
9872 (defun gnus-dead-summary-mode (&optional arg)
9873   "Minor mode for Gnus summary buffers."
9874   (interactive "P")
9875   (when (eq major-mode 'gnus-summary-mode)
9876     (make-local-variable 'gnus-dead-summary-mode)
9877     (setq gnus-dead-summary-mode
9878           (if (null arg) (not gnus-dead-summary-mode)
9879             (> (prefix-numeric-value arg) 0)))
9880     (when gnus-dead-summary-mode
9881       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9882         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9883       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9884         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9885               minor-mode-map-alist)))))
9886
9887 (defun gnus-deaden-summary ()
9888   "Make the current summary buffer into a dead summary buffer."
9889   ;; Kill any previous dead summary buffer.
9890   (when (and gnus-dead-summary
9891              (buffer-name gnus-dead-summary))
9892     (save-excursion
9893       (set-buffer gnus-dead-summary)
9894       (when gnus-dead-summary-mode
9895         (kill-buffer (current-buffer)))))
9896   ;; Make this the current dead summary.
9897   (setq gnus-dead-summary (current-buffer))
9898   (gnus-dead-summary-mode 1)
9899   (let ((name (buffer-name)))
9900     (when (string-match "Summary" name)
9901       (rename-buffer
9902        (concat (substring name 0 (match-beginning 0)) "Dead "
9903                (substring name (match-beginning 0))) t))))
9904
9905 (defun gnus-kill-or-deaden-summary (buffer)
9906   "Kill or deaden the summary BUFFER."
9907   (cond (gnus-kill-summary-on-exit
9908          (when (and gnus-use-trees
9909                     (and (get-buffer buffer)
9910                          (buffer-name (get-buffer buffer))))
9911            (save-excursion
9912              (set-buffer (get-buffer buffer))
9913              (gnus-tree-close gnus-newsgroup-name)))
9914          (gnus-kill-buffer buffer))
9915         ((and (get-buffer buffer)
9916               (buffer-name (get-buffer buffer)))
9917          (save-excursion
9918            (set-buffer buffer)
9919            (gnus-deaden-summary)))))
9920
9921 (defun gnus-summary-wake-up-the-dead (&rest args)
9922   "Wake up the dead summary buffer."
9923   (interactive)
9924   (gnus-dead-summary-mode -1)
9925   (let ((name (buffer-name)))
9926     (when (string-match "Dead " name)
9927       (rename-buffer
9928        (concat (substring name 0 (match-beginning 0))
9929                (substring name (match-end 0))) t)))
9930   (gnus-message 3 "This dead summary is now alive again"))
9931
9932 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9933 (defun gnus-summary-fetch-faq (&optional faq-dir)
9934   "Fetch the FAQ for the current group.
9935 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9936 in."
9937   (interactive
9938    (list
9939     (if current-prefix-arg
9940         (completing-read
9941          "Faq dir: " (and (listp gnus-group-faq-directory)
9942                           gnus-group-faq-directory)))))
9943   (let (gnus-faq-buffer)
9944     (and (setq gnus-faq-buffer
9945                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9946          (gnus-configure-windows 'summary-faq))))
9947
9948 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9949 (defun gnus-summary-describe-group (&optional force)
9950   "Describe the current newsgroup."
9951   (interactive "P")
9952   (gnus-group-describe-group force gnus-newsgroup-name))
9953
9954 (defun gnus-summary-describe-briefly ()
9955   "Describe summary mode commands briefly."
9956   (interactive)
9957   (gnus-message 6
9958                 (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")))
9959
9960 ;; Walking around group mode buffer from summary mode.
9961
9962 (defun gnus-summary-next-group (&optional no-article target-group backward)
9963   "Exit current newsgroup and then select next unread newsgroup.
9964 If prefix argument NO-ARTICLE is non-nil, no article is selected
9965 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9966 previous group instead."
9967   (interactive "P")
9968   (gnus-set-global-variables)
9969   (let ((current-group gnus-newsgroup-name)
9970         (current-buffer (current-buffer))
9971         entered)
9972     ;; First we semi-exit this group to update Xrefs and all variables.
9973     ;; We can't do a real exit, because the window conf must remain
9974     ;; the same in case the user is prompted for info, and we don't
9975     ;; want the window conf to change before that...
9976     (gnus-summary-exit t)
9977     (while (not entered)
9978       ;; Then we find what group we are supposed to enter.
9979       (set-buffer gnus-group-buffer)
9980       (gnus-group-jump-to-group current-group)
9981       (setq target-group
9982             (or target-group
9983                 (if (eq gnus-keep-same-level 'best)
9984                     (gnus-summary-best-group gnus-newsgroup-name)
9985                   (gnus-summary-search-group backward gnus-keep-same-level))))
9986       (if (not target-group)
9987           ;; There are no further groups, so we return to the group
9988           ;; buffer.
9989           (progn
9990             (gnus-message 5 "Returning to the group buffer")
9991             (setq entered t)
9992             (set-buffer current-buffer)
9993             (gnus-summary-exit))
9994         ;; We try to enter the target group.
9995         (gnus-group-jump-to-group target-group)
9996         (let ((unreads (gnus-group-group-unread)))
9997           (if (and (or (eq t unreads)
9998                        (and unreads (not (zerop unreads))))
9999                    (gnus-summary-read-group
10000                     target-group nil no-article current-buffer))
10001               (setq entered t)
10002             (setq current-group target-group
10003                   target-group nil)))))))
10004
10005 (defun gnus-summary-prev-group (&optional no-article)
10006   "Exit current newsgroup and then select previous unread newsgroup.
10007 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10008   (interactive "P")
10009   (gnus-summary-next-group no-article nil t))
10010
10011 ;; Walking around summary lines.
10012
10013 (defun gnus-summary-first-subject (&optional unread)
10014   "Go to the first unread subject.
10015 If UNREAD is non-nil, go to the first unread article.
10016 Returns the article selected or nil if there are no unread articles."
10017   (interactive "P")
10018   (prog1
10019       (cond
10020        ;; Empty summary.
10021        ((null gnus-newsgroup-data)
10022         (gnus-message 3 "No articles in the group")
10023         nil)
10024        ;; Pick the first article.
10025        ((not unread)
10026         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10027         (gnus-data-number (car gnus-newsgroup-data)))
10028        ;; No unread articles.
10029        ((null gnus-newsgroup-unreads)
10030         (gnus-message 3 "No more unread articles")
10031         nil)
10032        ;; Find the first unread article.
10033        (t
10034         (let ((data gnus-newsgroup-data))
10035           (while (and data
10036                       (not (gnus-data-unread-p (car data))))
10037             (setq data (cdr data)))
10038           (if data
10039               (progn
10040                 (goto-char (gnus-data-pos (car data)))
10041                 (gnus-data-number (car data)))))))
10042     (gnus-summary-position-point)))
10043
10044 (defun gnus-summary-next-subject (n &optional unread dont-display)
10045   "Go to next N'th summary line.
10046 If N is negative, go to the previous N'th subject line.
10047 If UNREAD is non-nil, only unread articles are selected.
10048 The difference between N and the actual number of steps taken is
10049 returned."
10050   (interactive "p")
10051   (let ((backward (< n 0))
10052         (n (abs n)))
10053     (while (and (> n 0)
10054                 (if backward
10055                     (gnus-summary-find-prev unread)
10056                   (gnus-summary-find-next unread)))
10057       (setq n (1- n)))
10058     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10059                                (if unread " unread" "")))
10060     (unless dont-display
10061       (gnus-summary-recenter)
10062       (gnus-summary-position-point))
10063     n))
10064
10065 (defun gnus-summary-next-unread-subject (n)
10066   "Go to next N'th unread summary line."
10067   (interactive "p")
10068   (gnus-summary-next-subject n t))
10069
10070 (defun gnus-summary-prev-subject (n &optional unread)
10071   "Go to previous N'th summary line.
10072 If optional argument UNREAD is non-nil, only unread article is selected."
10073   (interactive "p")
10074   (gnus-summary-next-subject (- n) unread))
10075
10076 (defun gnus-summary-prev-unread-subject (n)
10077   "Go to previous N'th unread summary line."
10078   (interactive "p")
10079   (gnus-summary-next-subject (- n) t))
10080
10081 (defun gnus-summary-goto-subject (article &optional force silent)
10082   "Go the subject line of ARTICLE.
10083 If FORCE, also allow jumping to articles not currently shown."
10084   (let ((b (point))
10085         (data (gnus-data-find article)))
10086     ;; We read in the article if we have to.
10087     (and (not data)
10088          force
10089          (gnus-summary-insert-subject article)
10090          (setq data (gnus-data-find article)))
10091     (goto-char b)
10092     (if (not data)
10093         (progn
10094           (unless silent
10095             (gnus-message 3 "Can't find article %d" article))
10096           nil)
10097       (goto-char (gnus-data-pos data))
10098       article)))
10099
10100 ;; Walking around summary lines with displaying articles.
10101
10102 (defun gnus-summary-expand-window (&optional arg)
10103   "Make the summary buffer take up the entire Emacs frame.
10104 Given a prefix, will force an `article' buffer configuration."
10105   (interactive "P")
10106   (gnus-set-global-variables)
10107   (if arg
10108       (gnus-configure-windows 'article 'force)
10109     (gnus-configure-windows 'summary 'force)))
10110
10111 (defun gnus-summary-display-article (article &optional all-header)
10112   "Display ARTICLE in article buffer."
10113   (gnus-set-global-variables)
10114   (if (null article)
10115       nil
10116     (prog1
10117         (if gnus-summary-display-article-function
10118             (funcall gnus-summary-display-article-function article all-header)
10119           (gnus-article-prepare article all-header))
10120       (run-hooks 'gnus-select-article-hook)
10121       (gnus-summary-recenter)
10122       (gnus-summary-goto-subject article)
10123       (when gnus-use-trees
10124         (gnus-possibly-generate-tree article)
10125         (gnus-highlight-selected-tree article))
10126       ;; Successfully display article.
10127       (gnus-article-set-window-start
10128        (cdr (assq article gnus-newsgroup-bookmarks)))
10129       t)))
10130
10131 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10132   "Select the current article.
10133 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10134 non-nil, the article will be re-fetched even if it already present in
10135 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10136 be displayed."
10137   (let ((article (or article (gnus-summary-article-number)))
10138         (all-headers (not (not all-headers))) ;Must be T or NIL.
10139         gnus-summary-display-article-function
10140         did)
10141     (and (not pseudo)
10142          (gnus-summary-article-pseudo-p article)
10143          (error "This is a pseudo-article."))
10144     (prog1
10145         (save-excursion
10146           (set-buffer gnus-summary-buffer)
10147           (if (or (and gnus-single-article-buffer
10148                        (or (null gnus-current-article)
10149                            (null gnus-article-current)
10150                            (null (get-buffer gnus-article-buffer))
10151                            (not (eq article (cdr gnus-article-current)))
10152                            (not (equal (car gnus-article-current)
10153                                        gnus-newsgroup-name))))
10154                   (and (not gnus-single-article-buffer)
10155                        (or (null gnus-current-article)
10156                            (not (eq gnus-current-article article))))
10157                   force)
10158               ;; The requested article is different from the current article.
10159               (prog1
10160                   (gnus-summary-display-article article all-headers)
10161                 (setq did article))
10162             (if (or all-headers gnus-show-all-headers)
10163                 (gnus-article-show-all-headers))
10164             'old))
10165       (if did
10166           (gnus-article-set-window-start
10167            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10168
10169 (defun gnus-summary-set-current-mark (&optional current-mark)
10170   "Obsolete function."
10171   nil)
10172
10173 (defun gnus-summary-next-article (&optional unread subject backward push)
10174   "Select the next article.
10175 If UNREAD, only unread articles are selected.
10176 If SUBJECT, only articles with SUBJECT are selected.
10177 If BACKWARD, the previous article is selected instead of the next."
10178   (interactive "P")
10179   (gnus-set-global-variables)
10180   (cond
10181    ;; Is there such an article?
10182    ((and (gnus-summary-search-forward unread subject backward)
10183          (or (gnus-summary-display-article (gnus-summary-article-number))
10184              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10185     (gnus-summary-position-point))
10186    ;; If not, we try the first unread, if that is wanted.
10187    ((and subject
10188          gnus-auto-select-same
10189          (or (gnus-summary-first-unread-article)
10190              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10191     (gnus-summary-position-point)
10192     (gnus-message 6 "Wrapped"))
10193    ;; Try to get next/previous article not displayed in this group.
10194    ((and gnus-auto-extend-newsgroup
10195          (not unread) (not subject))
10196     (gnus-summary-goto-article
10197      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10198      nil t))
10199    ;; Go to next/previous group.
10200    (t
10201     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10202         (gnus-summary-jump-to-group gnus-newsgroup-name))
10203     (let ((cmd last-command-char)
10204           (group
10205            (if (eq gnus-keep-same-level 'best)
10206                (gnus-summary-best-group gnus-newsgroup-name)
10207              (gnus-summary-search-group backward gnus-keep-same-level))))
10208       ;; For some reason, the group window gets selected.  We change
10209       ;; it back.
10210       (select-window (get-buffer-window (current-buffer)))
10211       ;; Select next unread newsgroup automagically.
10212       (cond
10213        ((not gnus-auto-select-next)
10214         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10215        ((or (eq gnus-auto-select-next 'quietly)
10216             (and (eq gnus-auto-select-next 'slightly-quietly)
10217                  push)
10218             (and (eq gnus-auto-select-next 'almost-quietly)
10219                  (gnus-summary-last-article-p)))
10220         ;; Select quietly.
10221         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10222             (gnus-summary-exit)
10223           (gnus-message 7 "No more%s articles (%s)..."
10224                         (if unread " unread" "")
10225                         (if group (concat "selecting " group)
10226                           "exiting"))
10227           (gnus-summary-next-group nil group backward)))
10228        (t
10229         (gnus-summary-walk-group-buffer
10230          gnus-newsgroup-name cmd unread backward)))))))
10231
10232 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10233   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10234                       (?\C-p (gnus-group-prev-unread-group 1))))
10235         keve key group ended)
10236     (save-excursion
10237       (set-buffer gnus-group-buffer)
10238       (gnus-summary-jump-to-group from-group)
10239       (setq group
10240             (if (eq gnus-keep-same-level 'best)
10241                 (gnus-summary-best-group gnus-newsgroup-name)
10242               (gnus-summary-search-group backward gnus-keep-same-level))))
10243     (while (not ended)
10244       (gnus-message
10245        5 "No more%s articles%s" (if unread " unread" "")
10246        (if (and group
10247                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10248            (format " (Type %s for %s [%s])"
10249                    (single-key-description cmd) group
10250                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10251          (format " (Type %s to exit %s)"
10252                  (single-key-description cmd)
10253                  gnus-newsgroup-name)))
10254       ;; Confirm auto selection.
10255       (setq key (car (setq keve (gnus-read-event-char))))
10256       (setq ended t)
10257       (cond
10258        ((assq key keystrokes)
10259         (let ((obuf (current-buffer)))
10260           (switch-to-buffer gnus-group-buffer)
10261           (and group
10262                (gnus-group-jump-to-group group))
10263           (eval (cadr (assq key keystrokes)))
10264           (setq group (gnus-group-group-name))
10265           (switch-to-buffer obuf))
10266         (setq ended nil))
10267        ((equal key cmd)
10268         (if (or (not group)
10269                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10270             (gnus-summary-exit)
10271           (gnus-summary-next-group nil group backward)))
10272        (t
10273         (push (cdr keve) unread-command-events))))))
10274
10275 (defun gnus-read-event-char ()
10276   "Get the next event."
10277   (let ((event (read-event)))
10278     (cons (and (numberp event) event) event)))
10279
10280 (defun gnus-summary-next-unread-article ()
10281   "Select unread article after current one."
10282   (interactive)
10283   (gnus-summary-next-article t (and gnus-auto-select-same
10284                                     (gnus-summary-article-subject))))
10285
10286 (defun gnus-summary-prev-article (&optional unread subject)
10287   "Select the article after the current one.
10288 If UNREAD is non-nil, only unread articles are selected."
10289   (interactive "P")
10290   (gnus-summary-next-article unread subject t))
10291
10292 (defun gnus-summary-prev-unread-article ()
10293   "Select unred article before current one."
10294   (interactive)
10295   (gnus-summary-prev-article t (and gnus-auto-select-same
10296                                     (gnus-summary-article-subject))))
10297
10298 (defun gnus-summary-next-page (&optional lines circular)
10299   "Show next page of the selected article.
10300 If at the end of the current article, select the next article.
10301 LINES says how many lines should be scrolled up.
10302
10303 If CIRCULAR is non-nil, go to the start of the article instead of
10304 selecting the next article when reaching the end of the current
10305 article."
10306   (interactive "P")
10307   (setq gnus-summary-buffer (current-buffer))
10308   (gnus-set-global-variables)
10309   (let ((article (gnus-summary-article-number))
10310         (endp nil))
10311     (gnus-configure-windows 'article)
10312     (if (or (null gnus-current-article)
10313             (null gnus-article-current)
10314             (/= article (cdr gnus-article-current))
10315             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10316         ;; Selected subject is different from current article's.
10317         (gnus-summary-display-article article)
10318       (gnus-eval-in-buffer-window
10319        gnus-article-buffer
10320        (setq endp (gnus-article-next-page lines)))
10321       (if endp
10322           (cond (circular
10323                  (gnus-summary-beginning-of-article))
10324                 (lines
10325                  (gnus-message 3 "End of message"))
10326                 ((null lines)
10327                  (if (and (eq gnus-summary-goto-unread 'never)
10328                           (not (gnus-summary-last-article-p article)))
10329                      (gnus-summary-next-article)
10330                    (gnus-summary-next-unread-article))))))
10331     (gnus-summary-recenter)
10332     (gnus-summary-position-point)))
10333
10334 (defun gnus-summary-prev-page (&optional lines)
10335   "Show previous page of selected article.
10336 Argument LINES specifies lines to be scrolled down."
10337   (interactive "P")
10338   (gnus-set-global-variables)
10339   (let ((article (gnus-summary-article-number)))
10340     (gnus-configure-windows 'article)
10341     (if (or (null gnus-current-article)
10342             (null gnus-article-current)
10343             (/= article (cdr gnus-article-current))
10344             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10345         ;; Selected subject is different from current article's.
10346         (gnus-summary-display-article article)
10347       (gnus-summary-recenter)
10348       (gnus-eval-in-buffer-window gnus-article-buffer
10349                                   (gnus-article-prev-page lines))))
10350   (gnus-summary-position-point))
10351
10352 (defun gnus-summary-scroll-up (lines)
10353   "Scroll up (or down) one line current article.
10354 Argument LINES specifies lines to be scrolled up (or down if negative)."
10355   (interactive "p")
10356   (gnus-set-global-variables)
10357   (gnus-configure-windows 'article)
10358   (gnus-summary-show-thread)
10359   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10360     (gnus-eval-in-buffer-window
10361      gnus-article-buffer
10362      (cond ((> lines 0)
10363             (if (gnus-article-next-page lines)
10364                 (gnus-message 3 "End of message")))
10365            ((< lines 0)
10366             (gnus-article-prev-page (- lines))))))
10367   (gnus-summary-recenter)
10368   (gnus-summary-position-point))
10369
10370 (defun gnus-summary-next-same-subject ()
10371   "Select next article which has the same subject as current one."
10372   (interactive)
10373   (gnus-set-global-variables)
10374   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10375
10376 (defun gnus-summary-prev-same-subject ()
10377   "Select previous article which has the same subject as current one."
10378   (interactive)
10379   (gnus-set-global-variables)
10380   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10381
10382 (defun gnus-summary-next-unread-same-subject ()
10383   "Select next unread article which has the same subject as current one."
10384   (interactive)
10385   (gnus-set-global-variables)
10386   (gnus-summary-next-article t (gnus-summary-article-subject)))
10387
10388 (defun gnus-summary-prev-unread-same-subject ()
10389   "Select previous unread article which has the same subject as current one."
10390   (interactive)
10391   (gnus-set-global-variables)
10392   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10393
10394 (defun gnus-summary-first-unread-article ()
10395   "Select the first unread article.
10396 Return nil if there are no unread articles."
10397   (interactive)
10398   (gnus-set-global-variables)
10399   (prog1
10400       (if (gnus-summary-first-subject t)
10401           (progn
10402             (gnus-summary-show-thread)
10403             (gnus-summary-first-subject t)
10404             (gnus-summary-display-article (gnus-summary-article-number))))
10405     (gnus-summary-position-point)))
10406
10407 (defun gnus-summary-best-unread-article ()
10408   "Select the unread article with the highest score."
10409   (interactive)
10410   (gnus-set-global-variables)
10411   (let ((best -1000000)
10412         (data gnus-newsgroup-data)
10413         article score)
10414     (while data
10415       (and (gnus-data-unread-p (car data))
10416            (> (setq score
10417                     (gnus-summary-article-score (gnus-data-number (car data))))
10418               best)
10419            (setq best score
10420                  article (gnus-data-number (car data))))
10421       (setq data (cdr data)))
10422     (prog1
10423         (if article
10424             (gnus-summary-goto-article article)
10425           (error "No unread articles"))
10426       (gnus-summary-position-point))))
10427
10428 (defun gnus-summary-last-subject ()
10429   "Go to the last displayed subject line in the group."
10430   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10431     (when article
10432       (gnus-summary-goto-subject article))))
10433
10434 (defun gnus-summary-goto-article (article &optional all-headers force)
10435   "Fetch ARTICLE and display it if it exists.
10436 If ALL-HEADERS is non-nil, no header lines are hidden."
10437   (interactive
10438    (list
10439     (string-to-int
10440      (completing-read
10441       "Article number: "
10442       (mapcar (lambda (number) (list (int-to-string number)))
10443               gnus-newsgroup-limit)))
10444     current-prefix-arg
10445     t))
10446   (prog1
10447       (if (gnus-summary-goto-subject article force)
10448           (gnus-summary-display-article article all-headers)
10449         (gnus-message 4 "Couldn't go to article %s" article) nil)
10450     (gnus-summary-position-point)))
10451
10452 (defun gnus-summary-goto-last-article ()
10453   "Go to the previously read article."
10454   (interactive)
10455   (prog1
10456       (and gnus-last-article
10457            (gnus-summary-goto-article gnus-last-article))
10458     (gnus-summary-position-point)))
10459
10460 (defun gnus-summary-pop-article (number)
10461   "Pop one article off the history and go to the previous.
10462 NUMBER articles will be popped off."
10463   (interactive "p")
10464   (let (to)
10465     (setq gnus-newsgroup-history
10466           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10467     (if to
10468         (gnus-summary-goto-article (car to))
10469       (error "Article history empty")))
10470   (gnus-summary-position-point))
10471
10472 ;; Summary commands and functions for limiting the summary buffer.
10473
10474 (defun gnus-summary-limit-to-articles (n)
10475   "Limit the summary buffer to the next N articles.
10476 If not given a prefix, use the process marked articles instead."
10477   (interactive "P")
10478   (gnus-set-global-variables)
10479   (prog1
10480       (let ((articles (gnus-summary-work-articles n)))
10481         (setq gnus-newsgroup-processable nil)
10482         (gnus-summary-limit articles))
10483     (gnus-summary-position-point)))
10484
10485 (defun gnus-summary-pop-limit (&optional total)
10486   "Restore the previous limit.
10487 If given a prefix, remove all limits."
10488   (interactive "P")
10489   (gnus-set-global-variables)
10490   (when total 
10491     (setq gnus-newsgroup-limits
10492           (list (mapcar (lambda (h) (mail-header-number h))
10493                         gnus-newsgroup-headers))))
10494   (unless gnus-newsgroup-limits
10495     (error "No limit to pop"))
10496   (prog1
10497       (gnus-summary-limit nil 'pop)
10498     (gnus-summary-position-point)))
10499
10500 (defun gnus-summary-limit-to-subject (subject &optional header)
10501   "Limit the summary buffer to articles that have subjects that match a regexp."
10502   (interactive "sRegexp: ")
10503   (unless header
10504     (setq header "subject"))
10505   (when (not (equal "" subject))
10506     (prog1
10507         (let ((articles (gnus-summary-find-matching
10508                          (or header "subject") subject 'all)))
10509           (or articles (error "Found no matches for \"%s\"" subject))
10510           (gnus-summary-limit articles))
10511       (gnus-summary-position-point))))
10512
10513 (defun gnus-summary-limit-to-author (from)
10514   "Limit the summary buffer to articles that have authors that match a regexp."
10515   (interactive "sRegexp: ")
10516   (gnus-summary-limit-to-subject from "from"))
10517
10518 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10519 (make-obsolete
10520  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10521
10522 (defun gnus-summary-limit-to-unread (&optional all)
10523   "Limit the summary buffer to articles that are not marked as read.
10524 If ALL is non-nil, limit strictly to unread articles."
10525   (interactive "P")
10526   (if all
10527       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10528     (gnus-summary-limit-to-marks
10529      ;; Concat all the marks that say that an article is read and have
10530      ;; those removed.
10531      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10532            gnus-killed-mark gnus-kill-file-mark
10533            gnus-low-score-mark gnus-expirable-mark
10534            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10535      'reverse)))
10536
10537 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10538 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10539
10540 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10541   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10542 If REVERSE, limit the summary buffer to articles that are not marked
10543 with MARKS.  MARKS can either be a string of marks or a list of marks.
10544 Returns how many articles were removed."
10545   (interactive "sMarks: ")
10546   (gnus-set-global-variables)
10547   (prog1
10548       (let ((data gnus-newsgroup-data)
10549             (marks (if (listp marks) marks
10550                      (append marks nil))) ; Transform to list.
10551             articles)
10552         (while data
10553           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10554                  (memq (gnus-data-mark (car data)) marks))
10555                (setq articles (cons (gnus-data-number (car data)) articles)))
10556           (setq data (cdr data)))
10557         (gnus-summary-limit articles))
10558     (gnus-summary-position-point)))
10559
10560 (defun gnus-summary-limit-to-score (&optional score)
10561   "Limit to articles with score at or above SCORE."
10562   (interactive "P")
10563   (gnus-set-global-variables)
10564   (setq score (if score
10565                   (prefix-numeric-value score)
10566                 (or gnus-summary-default-score 0)))
10567   (let ((data gnus-newsgroup-data)
10568         articles)
10569     (while data
10570       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10571                 score)
10572         (push (gnus-data-number (car data)) articles))
10573       (setq data (cdr data)))
10574     (prog1
10575         (gnus-summary-limit articles)
10576       (gnus-summary-position-point))))
10577
10578 (defun gnus-summary-limit-include-dormant ()
10579   "Display all the hidden articles that are marked as dormant."
10580   (interactive)
10581   (gnus-set-global-variables)
10582   (or gnus-newsgroup-dormant
10583       (error "There are no dormant articles in this group"))
10584   (prog1
10585       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10586     (gnus-summary-position-point)))
10587
10588 (defun gnus-summary-limit-exclude-dormant ()
10589   "Hide all dormant articles."
10590   (interactive)
10591   (gnus-set-global-variables)
10592   (prog1
10593       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10594     (gnus-summary-position-point)))
10595
10596 (defun gnus-summary-limit-exclude-childless-dormant ()
10597   "Hide all dormant articles that have no children."
10598   (interactive)
10599   (gnus-set-global-variables)
10600   (let ((data (gnus-data-list t))
10601         articles d children)
10602     ;; Find all articles that are either not dormant or have
10603     ;; children.
10604     (while (setq d (pop data))
10605       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10606                 (and (setq children 
10607                            (gnus-article-children (gnus-data-number d)))
10608                      (let (found)
10609                        (while children
10610                          (when (memq (car children) articles)
10611                            (setq children nil
10612                                  found t))
10613                          (pop children))
10614                        found)))
10615         (push (gnus-data-number d) articles)))
10616     ;; Do the limiting.
10617     (prog1
10618         (gnus-summary-limit articles)
10619       (gnus-summary-position-point))))
10620
10621 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10622   "Mark all unread excluded articles as read.
10623 If ALL, mark even excluded ticked and dormants as read."
10624   (interactive "P")
10625   (let ((articles (gnus-sorted-complement
10626                    (sort
10627                     (mapcar (lambda (h) (mail-header-number h))
10628                             gnus-newsgroup-headers)
10629                     '<)
10630                    (sort gnus-newsgroup-limit '<)))
10631         article)
10632     (setq gnus-newsgroup-unreads nil)
10633     (if all
10634         (setq gnus-newsgroup-dormant nil
10635               gnus-newsgroup-marked nil
10636               gnus-newsgroup-reads
10637               (nconc
10638                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10639                gnus-newsgroup-reads))
10640       (while (setq article (pop articles))
10641         (unless (or (memq article gnus-newsgroup-dormant)
10642                     (memq article gnus-newsgroup-marked))
10643           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10644
10645 (defun gnus-summary-limit (articles &optional pop)
10646   (if pop
10647       ;; We pop the previous limit off the stack and use that.
10648       (setq articles (car gnus-newsgroup-limits)
10649             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10650     ;; We use the new limit, so we push the old limit on the stack.
10651     (setq gnus-newsgroup-limits
10652           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10653   ;; Set the limit.
10654   (setq gnus-newsgroup-limit articles)
10655   (let ((total (length gnus-newsgroup-data))
10656         (data (gnus-data-find-list (gnus-summary-article-number)))
10657         found)
10658     ;; This will do all the work of generating the new summary buffer
10659     ;; according to the new limit.
10660     (gnus-summary-prepare)
10661     ;; Hide any threads, possibly.
10662     (and gnus-show-threads
10663          gnus-thread-hide-subtree
10664          (gnus-summary-hide-all-threads))
10665     ;; Try to return to the article you were at, or one in the
10666     ;; neighborhood.
10667     (if data
10668         ;; We try to find some article after the current one.
10669         (while data
10670           (and (gnus-summary-goto-subject
10671                 (gnus-data-number (car data)) nil t)
10672                (setq data nil
10673                      found t))
10674           (setq data (cdr data))))
10675     (or found
10676         ;; If there is no data, that means that we were after the last
10677         ;; article.  The same goes when we can't find any articles
10678         ;; after the current one.
10679         (progn
10680           (goto-char (point-max))
10681           (gnus-summary-find-prev)))
10682     ;; We return how many articles were removed from the summary
10683     ;; buffer as a result of the new limit.
10684     (- total (length gnus-newsgroup-data))))
10685
10686 (defsubst gnus-cut-thread (thread)
10687   "Go forwards in the thread until we find an article that we want to display."
10688   (when (eq gnus-fetch-old-headers 'some)
10689     ;; Deal with old-fetched headers.
10690     (while (and thread
10691                 (memq (mail-header-number (car thread)) 
10692                       gnus-newsgroup-ancient)
10693                 (<= (length (cdr thread)) 1))
10694       (setq thread (cadr thread))))
10695   ;; Deal with sparse threads.
10696   (when (or (eq gnus-build-sparse-threads 'some)
10697             (eq gnus-build-sparse-threads 'more))
10698     (while (and thread
10699                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10700                 (= (length (cdr thread)) 1))
10701       (setq thread (cadr thread))))
10702   thread)
10703
10704 (defun gnus-cut-threads (threads)
10705   "Cut off all uninteresting articles from the beginning of threads."
10706   (when (or (eq gnus-fetch-old-headers 'some)
10707             (eq gnus-build-sparse-threads 'some)
10708             (eq gnus-build-sparse-threads 'more))
10709     (let ((th threads))
10710       (while th
10711         (setcar th (gnus-cut-thread (car th)))
10712         (setq th (cdr th)))))
10713   ;; Remove nixed out threads.
10714   (delq nil threads))
10715
10716 (defun gnus-summary-initial-limit (&optional show-if-empty)
10717   "Figure out what the initial limit is supposed to be on group entry.
10718 This entails weeding out unwanted dormants, low-scored articles,
10719 fetch-old-headers verbiage, and so on."
10720   ;; Most groups have nothing to remove.
10721   (if (or gnus-inhibit-limiting
10722           (and (null gnus-newsgroup-dormant)
10723                (not (eq gnus-fetch-old-headers 'some))
10724                (null gnus-summary-expunge-below)
10725                (not (eq gnus-build-sparse-threads 'some))
10726                (not (eq gnus-build-sparse-threads 'more))
10727                (null gnus-thread-expunge-below)
10728                (not gnus-use-nocem)))
10729       () ; Do nothing.
10730     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10731     (setq gnus-newsgroup-limit nil)
10732     (mapatoms
10733      (lambda (node)
10734        (unless (car (symbol-value node))
10735          ;; These threads have no parents -- they are roots.
10736          (let ((nodes (cdr (symbol-value node)))
10737                thread)
10738            (while nodes
10739              (if (and gnus-thread-expunge-below
10740                       (< (gnus-thread-total-score (car nodes))
10741                          gnus-thread-expunge-below))
10742                  (gnus-expunge-thread (pop nodes))
10743                (setq thread (pop nodes))
10744                (gnus-summary-limit-children thread))))))
10745      gnus-newsgroup-dependencies)
10746     ;; If this limitation resulted in an empty group, we might
10747     ;; pop the previous limit and use it instead.
10748     (when (and (not gnus-newsgroup-limit)
10749                show-if-empty)
10750       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10751     gnus-newsgroup-limit))
10752
10753 (defun gnus-summary-limit-children (thread)
10754   "Return 1 if this subthread is visible and 0 if it is not."
10755   ;; First we get the number of visible children to this thread.  This
10756   ;; is done by recursing down the thread using this function, so this
10757   ;; will really go down to a leaf article first, before slowly
10758   ;; working its way up towards the root.
10759   (when thread
10760     (let ((children
10761            (if (cdr thread)
10762                (apply '+ (mapcar 'gnus-summary-limit-children
10763                                  (cdr thread)))
10764              0))
10765           (number (mail-header-number (car thread)))
10766           score)
10767       (if (or
10768            ;; If this article is dormant and has absolutely no visible
10769            ;; children, then this article isn't visible.
10770            (and (memq number gnus-newsgroup-dormant)
10771                 (= children 0))
10772            ;; If this is a "fetch-old-headered" and there is only one
10773            ;; visible child (or less), then we don't want this article.
10774            (and (eq gnus-fetch-old-headers 'some)
10775                 (memq number gnus-newsgroup-ancient)
10776                 (zerop children))
10777            ;; If this is a sparsely inserted article with no children,
10778            ;; we don't want it.
10779            (and (eq gnus-build-sparse-threads 'some)
10780                 (memq number gnus-newsgroup-sparse)
10781                 (zerop children))
10782            ;; If we use expunging, and this article is really
10783            ;; low-scored, then we don't want this article.
10784            (when (and gnus-summary-expunge-below
10785                       (< (setq score
10786                                (or (cdr (assq number gnus-newsgroup-scored))
10787                                    gnus-summary-default-score))
10788                          gnus-summary-expunge-below))
10789              ;; We increase the expunge-tally here, but that has
10790              ;; nothing to do with the limits, really.
10791              (incf gnus-newsgroup-expunged-tally)
10792              ;; We also mark as read here, if that's wanted.
10793              (when (and gnus-summary-mark-below
10794                         (< score gnus-summary-mark-below))
10795                (setq gnus-newsgroup-unreads
10796                      (delq number gnus-newsgroup-unreads))
10797                (if gnus-newsgroup-auto-expire
10798                    (push number gnus-newsgroup-expirable)
10799                  (push (cons number gnus-low-score-mark)
10800                        gnus-newsgroup-reads)))
10801              t)
10802            (and gnus-use-nocem
10803                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10804           ;; Nope, invisible article.
10805           0
10806         ;; Ok, this article is to be visible, so we add it to the limit
10807         ;; and return 1.
10808         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10809         1))))
10810
10811 (defun gnus-expunge-thread (thread)
10812   "Mark all articles in THREAD as read."
10813   (let* ((number (mail-header-number (car thread))))
10814     (incf gnus-newsgroup-expunged-tally)
10815     ;; We also mark as read here, if that's wanted.
10816     (setq gnus-newsgroup-unreads
10817           (delq number gnus-newsgroup-unreads))
10818     (if gnus-newsgroup-auto-expire
10819         (push number gnus-newsgroup-expirable)
10820       (push (cons number gnus-low-score-mark)
10821             gnus-newsgroup-reads)))
10822   ;; Go recursively through all subthreads.
10823   (mapcar 'gnus-expunge-thread (cdr thread)))
10824
10825 ;; Summary article oriented commands
10826
10827 (defun gnus-summary-refer-parent-article (n)
10828   "Refer parent article N times.
10829 The difference between N and the number of articles fetched is returned."
10830   (interactive "p")
10831   (gnus-set-global-variables)
10832   (while
10833       (and
10834        (> n 0)
10835        (let* ((header (gnus-summary-article-header))
10836               (ref
10837                ;; If we try to find the parent of the currently
10838                ;; displayed article, then we take a look at the actual
10839                ;; References header, since this is slightly more
10840                ;; reliable than the References field we got from the
10841                ;; server.
10842                (if (and (eq (mail-header-number header)
10843                             (cdr gnus-article-current))
10844                         (equal gnus-newsgroup-name
10845                                (car gnus-article-current)))
10846                    (save-excursion
10847                      (set-buffer gnus-original-article-buffer)
10848                      (nnheader-narrow-to-headers)
10849                      (prog1
10850                          (mail-fetch-field "references")
10851                        (widen)))
10852                  ;; It's not the current article, so we take a bet on
10853                  ;; the value we got from the server.
10854                  (mail-header-references header))))
10855          (if (setq ref (or ref (mail-header-references header)))
10856              (or (gnus-summary-refer-article (gnus-parent-id ref))
10857                  (gnus-message 1 "Couldn't find parent"))
10858            (gnus-message 1 "No references in article %d"
10859                          (gnus-summary-article-number))
10860            nil)))
10861     (setq n (1- n)))
10862   (gnus-summary-position-point)
10863   n)
10864
10865 (defun gnus-summary-refer-references ()
10866   "Fetch all articles mentioned in the References header.
10867 Return how many articles were fetched."
10868   (interactive)
10869   (gnus-set-global-variables)
10870   (let ((ref (mail-header-references (gnus-summary-article-header)))
10871         (current (gnus-summary-article-number))
10872         (n 0))
10873     ;; For each Message-ID in the References header...
10874     (while (string-match "<[^>]*>" ref)
10875       (incf n)
10876       ;; ... fetch that article.
10877       (gnus-summary-refer-article
10878        (prog1 (match-string 0 ref)
10879          (setq ref (substring ref (match-end 0))))))
10880     (gnus-summary-goto-subject current)
10881     (gnus-summary-position-point)
10882     n))
10883
10884 (defun gnus-summary-refer-article (message-id)
10885   "Fetch an article specified by MESSAGE-ID."
10886   (interactive "sMessage-ID: ")
10887   (when (and (stringp message-id)
10888              (not (zerop (length message-id))))
10889     ;; Construct the correct Message-ID if necessary.
10890     ;; Suggested by tale@pawl.rpi.edu.
10891     (unless (string-match "^<" message-id)
10892       (setq message-id (concat "<" message-id)))
10893     (unless (string-match ">$" message-id)
10894       (setq message-id (concat message-id ">")))
10895     (let ((header (car (gnus-gethash message-id
10896                                      gnus-newsgroup-dependencies))))
10897       (if header
10898           ;; The article is present in the buffer, to we just go to it.
10899           (gnus-summary-goto-article (mail-header-number header) nil t)
10900         ;; We fetch the article
10901         (let ((gnus-override-method 
10902                (and (gnus-news-group-p gnus-newsgroup-name)
10903                     gnus-refer-article-method))
10904               number)
10905           ;; Start the special refer-article method, if necessary.
10906           (when gnus-refer-article-method
10907             (gnus-check-server gnus-refer-article-method))
10908           ;; Fetch the header, and display the article.
10909           (if (setq number (gnus-summary-insert-subject message-id))
10910               (gnus-summary-select-article nil nil nil number)
10911             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10912
10913 (defun gnus-summary-enter-digest-group (&optional force)
10914   "Enter a digest group based on the current article."
10915   (interactive "P")
10916   (gnus-set-global-variables)
10917   (gnus-summary-select-article)
10918   (let ((name (format "%s-%d"
10919                       (gnus-group-prefixed-name
10920                        gnus-newsgroup-name (list 'nndoc ""))
10921                       gnus-current-article))
10922         (ogroup gnus-newsgroup-name)
10923         (case-fold-search t)
10924         (buf (current-buffer))
10925         dig)
10926     (save-excursion
10927       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10928       (insert-buffer-substring gnus-original-article-buffer)
10929       (narrow-to-region
10930        (goto-char (point-min))
10931        (or (search-forward "\n\n" nil t) (point)))
10932       (goto-char (point-min))
10933       (delete-matching-lines "^\\(Path\\):\\|^From ")
10934       (widen))
10935     (unwind-protect
10936         (if (gnus-group-read-ephemeral-group
10937              name `(nndoc ,name (nndoc-address
10938                                  ,(get-buffer dig))
10939                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10940             ;; Make all postings to this group go to the parent group.
10941             (nconc (gnus-info-params (gnus-get-info name))
10942                    (list (cons 'to-group ogroup)))
10943           ;; Couldn't select this doc group.
10944           (switch-to-buffer buf)
10945           (gnus-set-global-variables)
10946           (gnus-configure-windows 'summary)
10947           (gnus-message 3 "Article couldn't be entered?"))
10948       (kill-buffer dig))))
10949
10950 (defun gnus-summary-isearch-article (&optional regexp-p)
10951   "Do incremental search forward on the current article.
10952 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10953   (interactive "P")
10954   (gnus-set-global-variables)
10955   (gnus-summary-select-article)
10956   (gnus-configure-windows 'article)
10957   (gnus-eval-in-buffer-window
10958    gnus-article-buffer
10959    (goto-char (point-min))
10960    (isearch-forward regexp-p)))
10961
10962 (defun gnus-summary-search-article-forward (regexp &optional backward)
10963   "Search for an article containing REGEXP forward.
10964 If BACKWARD, search backward instead."
10965   (interactive
10966    (list (read-string
10967           (format "Search article %s (regexp%s): "
10968                   (if current-prefix-arg "backward" "forward")
10969                   (if gnus-last-search-regexp
10970                       (concat ", default " gnus-last-search-regexp)
10971                     "")))
10972          current-prefix-arg))
10973   (gnus-set-global-variables)
10974   (if (string-equal regexp "")
10975       (setq regexp (or gnus-last-search-regexp ""))
10976     (setq gnus-last-search-regexp regexp))
10977   (if (gnus-summary-search-article regexp backward)
10978       (gnus-article-set-window-start
10979        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10980     (error "Search failed: \"%s\"" regexp)))
10981
10982 (defun gnus-summary-search-article-backward (regexp)
10983   "Search for an article containing REGEXP backward."
10984   (interactive
10985    (list (read-string
10986           (format "Search article backward (regexp%s): "
10987                   (if gnus-last-search-regexp
10988                       (concat ", default " gnus-last-search-regexp)
10989                     "")))))
10990   (gnus-summary-search-article-forward regexp 'backward))
10991
10992 (defun gnus-summary-search-article (regexp &optional backward)
10993   "Search for an article containing REGEXP.
10994 Optional argument BACKWARD means do search for backward.
10995 gnus-select-article-hook is not called during the search."
10996   (let ((gnus-select-article-hook nil)  ;Disable hook.
10997         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10998         (re-search
10999          (if backward
11000              (function re-search-backward) (function re-search-forward)))
11001         (found nil)
11002         (last nil))
11003     ;; Hidden thread subtrees must be searched for ,too.
11004     (gnus-summary-show-all-threads)
11005     ;; First of all, search current article.
11006     ;; We don't want to read article again from NNTP server nor reset
11007     ;; current point.
11008     (gnus-summary-select-article)
11009     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11010     (setq last gnus-current-article)
11011     (gnus-eval-in-buffer-window
11012      gnus-article-buffer
11013      (save-restriction
11014        (widen)
11015        ;; Begin search from current point.
11016        (setq found (funcall re-search regexp nil t))))
11017     ;; Then search next articles.
11018     (while (and (not found)
11019                 (gnus-summary-display-article
11020                  (if backward (gnus-summary-find-prev)
11021                    (gnus-summary-find-next))))
11022       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11023       (gnus-eval-in-buffer-window
11024        gnus-article-buffer
11025        (save-restriction
11026          (widen)
11027          (goto-char (if backward (point-max) (point-min)))
11028          (setq found (funcall re-search regexp nil t)))))
11029     (message "")
11030     ;; Adjust article pointer.
11031     (or (eq last gnus-current-article)
11032         (setq gnus-last-article last))
11033     ;; Return T if found such article.
11034     found))
11035
11036 (defun gnus-summary-find-matching (header regexp &optional backward unread
11037                                           not-case-fold)
11038   "Return a list of all articles that match REGEXP on HEADER.
11039 The search stars on the current article and goes forwards unless
11040 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11041 If UNREAD is non-nil, only unread articles will
11042 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11043 in the comparisons."
11044   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11045                 (gnus-data-find-list
11046                  (gnus-summary-article-number) (gnus-data-list backward))))
11047         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11048         (case-fold-search (not not-case-fold))
11049         articles d)
11050     (or (fboundp (intern (concat "mail-header-" header)))
11051         (error "%s is not a valid header" header))
11052     (while data
11053       (setq d (car data))
11054       (and (or (not unread)             ; We want all articles...
11055                (gnus-data-unread-p d))  ; Or just unreads.
11056            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11057            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11058            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11059       (setq data (cdr data)))
11060     (nreverse articles)))
11061
11062 (defun gnus-summary-execute-command (header regexp command &optional backward)
11063   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11064 If HEADER is an empty string (or nil), the match is done on the entire
11065 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11066   (interactive
11067    (list (let ((completion-ignore-case t))
11068            (completing-read
11069             "Header name: "
11070             (mapcar (lambda (string) (list string))
11071                     '("Number" "Subject" "From" "Lines" "Date"
11072                       "Message-ID" "Xref" "References" "Body"))
11073             nil 'require-match))
11074          (read-string "Regexp: ")
11075          (read-key-sequence "Command: ")
11076          current-prefix-arg))
11077   (when (equal header "Body")
11078     (setq header ""))
11079   (gnus-set-global-variables)
11080   ;; Hidden thread subtrees must be searched as well.
11081   (gnus-summary-show-all-threads)
11082   ;; We don't want to change current point nor window configuration.
11083   (save-excursion
11084     (save-window-excursion
11085       (gnus-message 6 "Executing %s..." (key-description command))
11086       ;; We'd like to execute COMMAND interactively so as to give arguments.
11087       (gnus-execute header regexp
11088                     `(lambda () (call-interactively ',(key-binding command)))
11089                     backward)
11090       (gnus-message 6 "Executing %s...done" (key-description command)))))
11091
11092 (defun gnus-summary-beginning-of-article ()
11093   "Scroll the article back to the beginning."
11094   (interactive)
11095   (gnus-set-global-variables)
11096   (gnus-summary-select-article)
11097   (gnus-configure-windows 'article)
11098   (gnus-eval-in-buffer-window
11099    gnus-article-buffer
11100    (widen)
11101    (goto-char (point-min))
11102    (and gnus-break-pages (gnus-narrow-to-page))))
11103
11104 (defun gnus-summary-end-of-article ()
11105   "Scroll to the end of the article."
11106   (interactive)
11107   (gnus-set-global-variables)
11108   (gnus-summary-select-article)
11109   (gnus-configure-windows 'article)
11110   (gnus-eval-in-buffer-window
11111    gnus-article-buffer
11112    (widen)
11113    (goto-char (point-max))
11114    (recenter -3)
11115    (and gnus-break-pages (gnus-narrow-to-page))))
11116
11117 (defun gnus-summary-show-article (&optional arg)
11118   "Force re-fetching of the current article.
11119 If ARG (the prefix) is non-nil, show the raw article without any
11120 article massaging functions being run."
11121   (interactive "P")
11122   (gnus-set-global-variables)
11123   (if (not arg)
11124       ;; Select the article the normal way.
11125       (gnus-summary-select-article nil 'force)
11126     ;; Bind the article treatment functions to nil.
11127     (let ((gnus-have-all-headers t)
11128           gnus-article-display-hook
11129           gnus-article-prepare-hook
11130           gnus-break-pages
11131           gnus-visual)
11132       (gnus-summary-select-article nil 'force)))
11133 ;  (gnus-configure-windows 'article)
11134   (gnus-summary-position-point))
11135
11136 (defun gnus-summary-verbose-headers (&optional arg)
11137   "Toggle permanent full header display.
11138 If ARG is a positive number, turn header display on.
11139 If ARG is a negative number, turn header display off."
11140   (interactive "P")
11141   (gnus-set-global-variables)
11142   (gnus-summary-toggle-header arg)
11143   (setq gnus-show-all-headers
11144         (cond ((or (not (numberp arg))
11145                    (zerop arg))
11146                (not gnus-show-all-headers))
11147               ((natnump arg)
11148                t))))
11149
11150 (defun gnus-summary-toggle-header (&optional arg)
11151   "Show the headers if they are hidden, or hide them if they are shown.
11152 If ARG is a positive number, show the entire header.
11153 If ARG is a negative number, hide the unwanted header lines."
11154   (interactive "P")
11155   (gnus-set-global-variables)
11156   (save-excursion
11157     (set-buffer gnus-article-buffer)
11158     (let* ((buffer-read-only nil)
11159            (inhibit-point-motion-hooks t)
11160            (hidden (text-property-any
11161                     (goto-char (point-min)) (search-forward "\n\n")
11162                     'invisible t))
11163            e)
11164       (goto-char (point-min))
11165       (when (search-forward "\n\n" nil t)
11166         (delete-region (point-min) (1- (point))))
11167       (goto-char (point-min))
11168       (save-excursion
11169         (set-buffer gnus-original-article-buffer)
11170         (goto-char (point-min))
11171         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11172       (insert-buffer-substring gnus-original-article-buffer 1 e)
11173       (let ((gnus-inhibit-hiding t))
11174         (run-hooks 'gnus-article-display-hook))
11175       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11176           (gnus-article-hide-headers)))))
11177
11178 (defun gnus-summary-show-all-headers ()
11179   "Make all header lines visible."
11180   (interactive)
11181   (gnus-set-global-variables)
11182   (gnus-article-show-all-headers))
11183
11184 (defun gnus-summary-toggle-mime (&optional arg)
11185   "Toggle MIME processing.
11186 If ARG is a positive number, turn MIME processing on."
11187   (interactive "P")
11188   (gnus-set-global-variables)
11189   (setq gnus-show-mime
11190         (if (null arg) (not gnus-show-mime)
11191           (> (prefix-numeric-value arg) 0)))
11192   (gnus-summary-select-article t 'force))
11193
11194 (defun gnus-summary-caesar-message (&optional arg)
11195   "Caesar rotate the current article by 13.
11196 The numerical prefix specifies how manu places to rotate each letter
11197 forward."
11198   (interactive "P")
11199   (gnus-set-global-variables)
11200   (gnus-summary-select-article)
11201   (let ((mail-header-separator ""))
11202     (gnus-eval-in-buffer-window
11203      gnus-article-buffer
11204      (save-restriction
11205        (widen)
11206        (let ((start (window-start)))
11207          (news-caesar-buffer-body arg)
11208          (set-window-start (get-buffer-window (current-buffer)) start))))))
11209
11210 (defun gnus-summary-stop-page-breaking ()
11211   "Stop page breaking in the current article."
11212   (interactive)
11213   (gnus-set-global-variables)
11214   (gnus-summary-select-article)
11215   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11216
11217 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11218   "Move the current article to a different newsgroup.
11219 If N is a positive number, move the N next articles.
11220 If N is a negative number, move the N previous articles.
11221 If N is nil and any articles have been marked with the process mark,
11222 move those articles instead.
11223 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11224 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11225 re-spool using this method.
11226
11227 For this function to work, both the current newsgroup and the
11228 newsgroup that you want to move to have to support the `request-move'
11229 and `request-accept' functions."
11230   (interactive "P")
11231   (unless action (setq action 'move))
11232   (gnus-set-global-variables)
11233   ;; Check whether the source group supports the required functions.
11234   (cond ((and (eq action 'move)
11235               (not (gnus-check-backend-function
11236                     'request-move-article gnus-newsgroup-name)))
11237          (error "The current group does not support article moving"))
11238         ((and (eq action 'crosspost)
11239               (not (gnus-check-backend-function
11240                     'request-replace-article gnus-newsgroup-name)))
11241          (error "The current group does not support article editing")))
11242   (let ((articles (gnus-summary-work-articles n))
11243         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11244         (names '((move "Move" "Moving")
11245                  (copy "Copy" "Copying")
11246                  (crosspost "Crosspost" "Crossposting")))
11247         (copy-buf (save-excursion
11248                     (nnheader-set-temp-buffer " *copy article*")))
11249         art-group to-method new-xref article to-groups)
11250     (unless (assq action names)
11251       (error "Unknown action %s" action))
11252     ;; Read the newsgroup name.
11253     (when (and (not to-newsgroup)
11254                (not select-method))
11255       (setq to-newsgroup
11256             (gnus-read-move-group-name
11257              (cadr (assq action names))
11258              (symbol-value (intern (format "gnus-current-%s-group" action)))
11259              articles prefix))
11260       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11261     (setq to-method (or select-method 
11262                         (gnus-find-method-for-group to-newsgroup)))
11263     ;; Check the method we are to move this article to...
11264     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11265         (error "%s does not support article copying" (car to-method)))
11266     (or (gnus-check-server to-method)
11267         (error "Can't open server %s" (car to-method)))
11268     (gnus-message 6 "%s to %s: %s..."
11269                   (caddr (assq action names))
11270                   (or (car select-method) to-newsgroup) articles)
11271     (while articles
11272       (setq article (pop articles))
11273       (setq
11274        art-group
11275        (cond
11276         ;; Move the article.
11277         ((eq action 'move)
11278          (gnus-request-move-article
11279           article                       ; Article to move
11280           gnus-newsgroup-name           ; From newsgrouo
11281           (nth 1 (gnus-find-method-for-group
11282                   gnus-newsgroup-name)) ; Server
11283           (list 'gnus-request-accept-article
11284                 to-newsgroup (list 'quote select-method)
11285                 (not articles))         ; Accept form
11286           (not articles)))              ; Only save nov last time
11287         ;; Copy the article.
11288         ((eq action 'copy)
11289          (save-excursion
11290            (set-buffer copy-buf)
11291            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11292            (gnus-request-accept-article
11293             to-newsgroup select-method (not articles))))
11294         ;; Crosspost the article.
11295         ((eq action 'crosspost)
11296          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11297            (setq new-xref (concat gnus-newsgroup-name ":" article))
11298            (if (and xref (not (string= xref "")))
11299                (progn
11300                  (when (string-match "^Xref: " xref)
11301                    (setq xref (substring xref (match-end 0))))
11302                  (setq new-xref (concat xref " " new-xref)))
11303              (setq new-xref (concat (system-name) " " new-xref)))
11304            (save-excursion
11305              (set-buffer copy-buf)
11306              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11307              (nnheader-replace-header "xref" new-xref)
11308              (gnus-request-accept-article
11309               to-newsgroup select-method (not articles)))))))
11310       (if (not art-group)
11311           (gnus-message 1 "Couldn't %s article %s"
11312                         (cadr (assq action names)) article)
11313         (let* ((entry
11314                 (or
11315                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11316                  (gnus-gethash
11317                   (gnus-group-prefixed-name
11318                    (car art-group)
11319                    (or select-method 
11320                        (gnus-find-method-for-group to-newsgroup)))
11321                   gnus-newsrc-hashtb)))
11322                (info (nth 2 entry))
11323                (to-group (gnus-info-group info)))
11324           ;; Update the group that has been moved to.
11325           (when (and info
11326                      (memq action '(move copy)))
11327             (unless (member to-group to-groups)
11328               (push to-group to-groups))
11329
11330             (unless (memq article gnus-newsgroup-unreads)
11331               (gnus-info-set-read
11332                info (gnus-add-to-range (gnus-info-read info)
11333                                        (list (cdr art-group)))))
11334
11335             ;; Copy any marks over to the new group.
11336             (let ((marks gnus-article-mark-lists)
11337                   (to-article (cdr art-group)))
11338
11339               ;; See whether the article is to be put in the cache.
11340               (when gnus-use-cache
11341                 (gnus-cache-possibly-enter-article
11342                  to-group to-article
11343                  (let ((header (copy-sequence
11344                                 (gnus-summary-article-header article))))
11345                    (mail-header-set-number header to-article)
11346                    header)
11347                  (memq article gnus-newsgroup-marked)
11348                  (memq article gnus-newsgroup-dormant)
11349                  (memq article gnus-newsgroup-unreads)))
11350
11351               (while marks
11352                 (when (memq article (symbol-value
11353                                      (intern (format "gnus-newsgroup-%s"
11354                                                      (caar marks)))))
11355                   ;; If the other group is the same as this group,
11356                   ;; then we have to add the mark to the list.
11357                   (when (equal to-group gnus-newsgroup-name)
11358                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11359                          (cons to-article
11360                                (symbol-value
11361                                 (intern (format "gnus-newsgroup-%s"
11362                                                 (caar marks)))))))
11363                   ;; Copy mark to other group.
11364                   (gnus-add-marked-articles
11365                    to-group (cdar marks) (list to-article) info))
11366                 (setq marks (cdr marks)))))
11367
11368           ;; Update the Xref header in this article to point to
11369           ;; the new crossposted article we have just created.
11370           (when (eq action 'crosspost)
11371             (save-excursion
11372               (set-buffer copy-buf)
11373               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11374               (nnheader-replace-header
11375                "xref" (concat new-xref " " (gnus-group-prefixed-name
11376                                             (car art-group) to-method)
11377                               ":" (cdr art-group)))
11378               (gnus-request-replace-article
11379                article gnus-newsgroup-name (current-buffer)))))
11380
11381         (gnus-summary-goto-subject article)
11382         (when (eq action 'move)
11383           (gnus-summary-mark-article article gnus-canceled-mark)))
11384       (gnus-summary-remove-process-mark article))
11385     ;; Re-activate all groups that have been moved to.
11386     (while to-groups
11387       (gnus-activate-group (pop to-groups)))
11388     
11389     (gnus-kill-buffer copy-buf)
11390     (gnus-summary-position-point)
11391     (gnus-set-mode-line 'summary)))
11392
11393 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11394   "Move the current article to a different newsgroup.
11395 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11396 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11397 re-spool using this method."
11398   (interactive "P")
11399   (gnus-summary-move-article n nil select-method 'copy))
11400
11401 (defun gnus-summary-crosspost-article (&optional n)
11402   "Crosspost the current article to some other group."
11403   (interactive "P")
11404   (gnus-summary-move-article n nil nil 'crosspost))
11405
11406 (defvar gnus-summary-respool-default-method nil
11407   "Default method for respooling an article.  
11408 If nil, use to the current newsgroup method.")
11409
11410 (defun gnus-summary-respool-article (&optional n method)
11411   "Respool the current article.
11412 The article will be squeezed through the mail spooling process again,
11413 which means that it will be put in some mail newsgroup or other
11414 depending on `nnmail-split-methods'.
11415 If N is a positive number, respool the N next articles.
11416 If N is a negative number, respool the N previous articles.
11417 If N is nil and any articles have been marked with the process mark,
11418 respool those articles instead.
11419
11420 Respooling can be done both from mail groups and \"real\" newsgroups.
11421 In the former case, the articles in question will be moved from the
11422 current group into whatever groups they are destined to.  In the
11423 latter case, they will be copied into the relevant groups."
11424   (interactive 
11425    (list current-prefix-arg
11426          (let* ((methods (gnus-methods-using 'respool))
11427                 (methname
11428                  (symbol-name (or gnus-summary-respool-default-method
11429                                   (car (gnus-find-method-for-group
11430                                         gnus-newsgroup-name)))))
11431                 (method
11432                  (gnus-completing-read 
11433                   methname "What backend do you want to use when? "
11434                   methods nil t nil 'gnus-method-history))
11435                 ms)
11436            (cond
11437             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11438              (list (intern method) ""))
11439             ((= 1 (length ms))
11440              (car ms))
11441             (t
11442              (cdr (completing-read 
11443                    "Server name: "
11444                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11445   (gnus-set-global-variables)
11446   (unless method
11447     (error "No method given for respooling"))
11448   (if (assoc (symbol-name
11449               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11450              (gnus-methods-using 'respool))
11451       (gnus-summary-move-article n nil method)
11452     (gnus-summary-copy-article n nil method)))
11453
11454 (defun gnus-summary-import-article (file)
11455   "Import a random file into a mail newsgroup."
11456   (interactive "fImport file: ")
11457   (gnus-set-global-variables)
11458   (let ((group gnus-newsgroup-name)
11459         (now (current-time))
11460         atts lines)
11461     (or (gnus-check-backend-function 'request-accept-article group)
11462         (error "%s does not support article importing" group))
11463     (or (file-readable-p file)
11464         (not (file-regular-p file))
11465         (error "Can't read %s" file))
11466     (save-excursion
11467       (set-buffer (get-buffer-create " *import file*"))
11468       (buffer-disable-undo (current-buffer))
11469       (erase-buffer)
11470       (insert-file-contents file)
11471       (goto-char (point-min))
11472       (unless (nnheader-article-p)
11473         ;; This doesn't look like an article, so we fudge some headers.
11474         (setq atts (file-attributes file)
11475               lines (count-lines (point-min) (point-max)))
11476         (insert "From: " (read-string "From: ") "\n"
11477                 "Subject: " (read-string "Subject: ") "\n"
11478                 "Date: " (timezone-make-date-arpa-standard
11479                           (current-time-string (nth 5 atts))
11480                           (current-time-zone now)
11481                           (current-time-zone now)) "\n"
11482                 "Message-ID: " (gnus-inews-message-id) "\n"
11483                 "Lines: " (int-to-string lines) "\n"
11484                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11485       (gnus-request-accept-article group nil t)
11486       (kill-buffer (current-buffer)))))
11487
11488 (defun gnus-summary-expire-articles ()
11489   "Expire all articles that are marked as expirable in the current group."
11490   (interactive)
11491   (gnus-set-global-variables)
11492   (when (gnus-check-backend-function
11493          'request-expire-articles gnus-newsgroup-name)
11494     ;; This backend supports expiry.
11495     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11496            (expirable (if total
11497                           (gnus-list-of-read-articles gnus-newsgroup-name)
11498                         (setq gnus-newsgroup-expirable
11499                               (sort gnus-newsgroup-expirable '<))))
11500            (expiry-wait (gnus-group-get-parameter
11501                          gnus-newsgroup-name 'expiry-wait))
11502            es)
11503       (when expirable
11504         ;; There are expirable articles in this group, so we run them
11505         ;; through the expiry process.
11506         (gnus-message 6 "Expiring articles...")
11507         ;; The list of articles that weren't expired is returned.
11508         (if expiry-wait
11509             (let ((nnmail-expiry-wait-function nil)
11510                   (nnmail-expiry-wait expiry-wait))
11511               (setq es (gnus-request-expire-articles
11512                         expirable gnus-newsgroup-name)))
11513           (setq es (gnus-request-expire-articles
11514                     expirable gnus-newsgroup-name)))
11515         (or total (setq gnus-newsgroup-expirable es))
11516         ;; We go through the old list of expirable, and mark all
11517         ;; really expired articles as nonexistent.
11518         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11519           (let ((gnus-use-cache nil))
11520             (while expirable
11521               (unless (memq (car expirable) es)
11522                 (when (gnus-data-find (car expirable))
11523                   (gnus-summary-mark-article
11524                    (car expirable) gnus-canceled-mark)))
11525               (setq expirable (cdr expirable)))))
11526         (gnus-message 6 "Expiring articles...done")))))
11527
11528 (defun gnus-summary-expire-articles-now ()
11529   "Expunge all expirable articles in the current group.
11530 This means that *all* articles that are marked as expirable will be
11531 deleted forever, right now."
11532   (interactive)
11533   (gnus-set-global-variables)
11534   (or gnus-expert-user
11535       (gnus-y-or-n-p
11536        "Are you really, really, really sure you want to expunge? ")
11537       (error "Phew!"))
11538   (let ((nnmail-expiry-wait 'immediate)
11539         (nnmail-expiry-wait-function nil))
11540     (gnus-summary-expire-articles)))
11541
11542 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11543 (defun gnus-summary-delete-article (&optional n)
11544   "Delete the N next (mail) articles.
11545 This command actually deletes articles.  This is not a marking
11546 command.  The article will disappear forever from your life, never to
11547 return.
11548 If N is negative, delete backwards.
11549 If N is nil and articles have been marked with the process mark,
11550 delete these instead."
11551   (interactive "P")
11552   (gnus-set-global-variables)
11553   (or (gnus-check-backend-function 'request-expire-articles
11554                                    gnus-newsgroup-name)
11555       (error "The current newsgroup does not support article deletion."))
11556   ;; Compute the list of articles to delete.
11557   (let ((articles (gnus-summary-work-articles n))
11558         not-deleted)
11559     (if (and gnus-novice-user
11560              (not (gnus-y-or-n-p
11561                    (format "Do you really want to delete %s forever? "
11562                            (if (> (length articles) 1) "these articles"
11563                              "this article")))))
11564         ()
11565       ;; Delete the articles.
11566       (setq not-deleted (gnus-request-expire-articles
11567                          articles gnus-newsgroup-name 'force))
11568       (while articles
11569         (gnus-summary-remove-process-mark (car articles))
11570         ;; The backend might not have been able to delete the article
11571         ;; after all.
11572         (or (memq (car articles) not-deleted)
11573             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11574         (setq articles (cdr articles))))
11575     (gnus-summary-position-point)
11576     (gnus-set-mode-line 'summary)
11577     not-deleted))
11578
11579 (defun gnus-summary-edit-article (&optional force)
11580   "Enter into a buffer and edit the current article.
11581 This will have permanent effect only in mail groups.
11582 If FORCE is non-nil, allow editing of articles even in read-only
11583 groups."
11584   (interactive "P")
11585   (save-excursion
11586     (set-buffer gnus-summary-buffer)
11587     (gnus-set-global-variables)
11588     (when (and (not force)
11589                (gnus-group-read-only-p))
11590       (error "The current newsgroup does not support article editing."))
11591     (gnus-summary-select-article t nil t)
11592     (gnus-configure-windows 'article)
11593     (select-window (get-buffer-window gnus-article-buffer))
11594     (gnus-message 6 "C-c C-c to end edits")
11595     (setq buffer-read-only nil)
11596     (text-mode)
11597     (use-local-map (copy-keymap (current-local-map)))
11598     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11599     (buffer-enable-undo)
11600     (widen)
11601     (goto-char (point-min))
11602     (search-forward "\n\n" nil t)))
11603
11604 (defun gnus-summary-edit-article-done ()
11605   "Make edits to the current article permanent."
11606   (interactive)
11607   (if (gnus-group-read-only-p)
11608       (progn
11609         (gnus-summary-edit-article-postpone)
11610         (gnus-error
11611          1 "The current newsgroup does not support article editing."))
11612     (let ((buf (format "%s" (buffer-string))))
11613       (erase-buffer)
11614       (insert buf)
11615       (if (not (gnus-request-replace-article
11616                 (cdr gnus-article-current) (car gnus-article-current)
11617                 (current-buffer)))
11618           (error "Couldn't replace article.")
11619         (gnus-article-mode)
11620         (use-local-map gnus-article-mode-map)
11621         (setq buffer-read-only t)
11622         (buffer-disable-undo (current-buffer))
11623         (gnus-configure-windows 'summary)
11624         (gnus-summary-update-article (cdr gnus-article-current))
11625         (when gnus-use-cache
11626           (gnus-cache-update-article 
11627            (cdr gnus-article-current) (car gnus-article-current))))
11628       (save-excursion
11629         (when (get-buffer gnus-original-article-buffer)
11630           (set-buffer gnus-original-article-buffer)
11631           (setq gnus-original-article nil)))
11632       (setq gnus-article-current nil
11633             gnus-current-article nil)
11634       (run-hooks 'gnus-article-display-hook)
11635       (and (gnus-visual-p 'summary-highlight 'highlight)
11636            (run-hooks 'gnus-visual-mark-article-hook)))))
11637
11638 (defun gnus-summary-edit-article-postpone ()
11639   "Postpone changes to the current article."
11640   (interactive)
11641   (gnus-article-mode)
11642   (use-local-map gnus-article-mode-map)
11643   (setq buffer-read-only t)
11644   (buffer-disable-undo (current-buffer))
11645   (gnus-configure-windows 'summary)
11646   (and (gnus-visual-p 'summary-highlight 'highlight)
11647        (run-hooks 'gnus-visual-mark-article-hook)))
11648
11649 (defun gnus-summary-respool-query ()
11650   "Query where the respool algorithm would put this article."
11651   (interactive)
11652   (gnus-set-global-variables)
11653   (gnus-summary-select-article)
11654   (save-excursion
11655     (set-buffer gnus-article-buffer)
11656     (save-restriction
11657       (goto-char (point-min))
11658       (search-forward "\n\n")
11659       (narrow-to-region (point-min) (point))
11660       (pp-eval-expression
11661        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11662
11663 ;; Summary score commands.
11664
11665 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11666
11667 (defun gnus-summary-raise-score (n)
11668   "Raise the score of the current article by N."
11669   (interactive "p")
11670   (gnus-set-global-variables)
11671   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11672
11673 (defun gnus-summary-set-score (n)
11674   "Set the score of the current article to N."
11675   (interactive "p")
11676   (gnus-set-global-variables)
11677   (save-excursion
11678     (gnus-summary-show-thread)
11679     (let ((buffer-read-only nil))
11680       ;; Set score.
11681       (gnus-summary-update-mark
11682        (if (= n (or gnus-summary-default-score 0)) ? 
11683          (if (< n (or gnus-summary-default-score 0))
11684              gnus-score-below-mark gnus-score-over-mark)) 'score))
11685     (let* ((article (gnus-summary-article-number))
11686            (score (assq article gnus-newsgroup-scored)))
11687       (if score (setcdr score n)
11688         (setq gnus-newsgroup-scored
11689               (cons (cons article n) gnus-newsgroup-scored))))
11690     (gnus-summary-update-line)))
11691
11692 (defun gnus-summary-current-score ()
11693   "Return the score of the current article."
11694   (interactive)
11695   (gnus-set-global-variables)
11696   (gnus-message 1 "%s" (gnus-summary-article-score)))
11697
11698 ;; Summary marking commands.
11699
11700 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11701   "Mark articles which has the same subject as read, and then select the next.
11702 If UNMARK is positive, remove any kind of mark.
11703 If UNMARK is negative, tick articles."
11704   (interactive "P")
11705   (gnus-set-global-variables)
11706   (if unmark
11707       (setq unmark (prefix-numeric-value unmark)))
11708   (let ((count
11709          (gnus-summary-mark-same-subject
11710           (gnus-summary-article-subject) unmark)))
11711     ;; Select next unread article.  If auto-select-same mode, should
11712     ;; select the first unread article.
11713     (gnus-summary-next-article t (and gnus-auto-select-same
11714                                       (gnus-summary-article-subject)))
11715     (gnus-message 7 "%d article%s marked as %s"
11716                   count (if (= count 1) " is" "s are")
11717                   (if unmark "unread" "read"))))
11718
11719 (defun gnus-summary-kill-same-subject (&optional unmark)
11720   "Mark articles which has the same subject as read.
11721 If UNMARK is positive, remove any kind of mark.
11722 If UNMARK is negative, tick articles."
11723   (interactive "P")
11724   (gnus-set-global-variables)
11725   (if unmark
11726       (setq unmark (prefix-numeric-value unmark)))
11727   (let ((count
11728          (gnus-summary-mark-same-subject
11729           (gnus-summary-article-subject) unmark)))
11730     ;; If marked as read, go to next unread subject.
11731     (if (null unmark)
11732         ;; Go to next unread subject.
11733         (gnus-summary-next-subject 1 t))
11734     (gnus-message 7 "%d articles are marked as %s"
11735                   count (if unmark "unread" "read"))))
11736
11737 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11738   "Mark articles with same SUBJECT as read, and return marked number.
11739 If optional argument UNMARK is positive, remove any kinds of marks.
11740 If optional argument UNMARK is negative, mark articles as unread instead."
11741   (let ((count 1))
11742     (save-excursion
11743       (cond
11744        ((null unmark)                   ; Mark as read.
11745         (while (and
11746                 (progn
11747                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11748                   (gnus-summary-show-thread) t)
11749                 (gnus-summary-find-subject subject))
11750           (setq count (1+ count))))
11751        ((> unmark 0)                    ; Tick.
11752         (while (and
11753                 (progn
11754                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11755                   (gnus-summary-show-thread) t)
11756                 (gnus-summary-find-subject subject))
11757           (setq count (1+ count))))
11758        (t                               ; Mark as unread.
11759         (while (and
11760                 (progn
11761                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11762                   (gnus-summary-show-thread) t)
11763                 (gnus-summary-find-subject subject))
11764           (setq count (1+ count)))))
11765       (gnus-set-mode-line 'summary)
11766       ;; Return the number of marked articles.
11767       count)))
11768
11769 (defun gnus-summary-mark-as-processable (n &optional unmark)
11770   "Set the process mark on the next N articles.
11771 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11772 the process mark instead.  The difference between N and the actual
11773 number of articles marked is returned."
11774   (interactive "p")
11775   (gnus-set-global-variables)
11776   (let ((backward (< n 0))
11777         (n (abs n)))
11778     (while (and
11779             (> n 0)
11780             (if unmark
11781                 (gnus-summary-remove-process-mark
11782                  (gnus-summary-article-number))
11783               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11784             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11785       (setq n (1- n)))
11786     (if (/= 0 n) (gnus-message 7 "No more articles"))
11787     (gnus-summary-recenter)
11788     (gnus-summary-position-point)
11789     n))
11790
11791 (defun gnus-summary-unmark-as-processable (n)
11792   "Remove the process mark from the next N articles.
11793 If N is negative, mark backward instead.  The difference between N and
11794 the actual number of articles marked is returned."
11795   (interactive "p")
11796   (gnus-set-global-variables)
11797   (gnus-summary-mark-as-processable n t))
11798
11799 (defun gnus-summary-unmark-all-processable ()
11800   "Remove the process mark from all articles."
11801   (interactive)
11802   (gnus-set-global-variables)
11803   (save-excursion
11804     (while gnus-newsgroup-processable
11805       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11806   (gnus-summary-position-point))
11807
11808 (defun gnus-summary-mark-as-expirable (n)
11809   "Mark N articles forward as expirable.
11810 If N is negative, mark backward instead.  The difference between N and
11811 the actual number of articles marked is returned."
11812   (interactive "p")
11813   (gnus-set-global-variables)
11814   (gnus-summary-mark-forward n gnus-expirable-mark))
11815
11816 (defun gnus-summary-mark-article-as-replied (article)
11817   "Mark ARTICLE replied and update the summary line."
11818   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11819   (let ((buffer-read-only nil))
11820     (when (gnus-summary-goto-subject article)
11821       (gnus-summary-update-secondary-mark article))))
11822
11823 (defun gnus-summary-set-bookmark (article)
11824   "Set a bookmark in current article."
11825   (interactive (list (gnus-summary-article-number)))
11826   (gnus-set-global-variables)
11827   (if (or (not (get-buffer gnus-article-buffer))
11828           (not gnus-current-article)
11829           (not gnus-article-current)
11830           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11831       (error "No current article selected"))
11832   ;; Remove old bookmark, if one exists.
11833   (let ((old (assq article gnus-newsgroup-bookmarks)))
11834     (if old (setq gnus-newsgroup-bookmarks
11835                   (delq old gnus-newsgroup-bookmarks))))
11836   ;; Set the new bookmark, which is on the form
11837   ;; (article-number . line-number-in-body).
11838   (setq gnus-newsgroup-bookmarks
11839         (cons
11840          (cons article
11841                (save-excursion
11842                  (set-buffer gnus-article-buffer)
11843                  (count-lines
11844                   (min (point)
11845                        (save-excursion
11846                          (goto-char (point-min))
11847                          (search-forward "\n\n" nil t)
11848                          (point)))
11849                   (point))))
11850          gnus-newsgroup-bookmarks))
11851   (gnus-message 6 "A bookmark has been added to the current article."))
11852
11853 (defun gnus-summary-remove-bookmark (article)
11854   "Remove the bookmark from the current article."
11855   (interactive (list (gnus-summary-article-number)))
11856   (gnus-set-global-variables)
11857   ;; Remove old bookmark, if one exists.
11858   (let ((old (assq article gnus-newsgroup-bookmarks)))
11859     (if old
11860         (progn
11861           (setq gnus-newsgroup-bookmarks
11862                 (delq old gnus-newsgroup-bookmarks))
11863           (gnus-message 6 "Removed bookmark."))
11864       (gnus-message 6 "No bookmark in current article."))))
11865
11866 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11867 (defun gnus-summary-mark-as-dormant (n)
11868   "Mark N articles forward as dormant.
11869 If N is negative, mark backward instead.  The difference between N and
11870 the actual number of articles marked is returned."
11871   (interactive "p")
11872   (gnus-set-global-variables)
11873   (gnus-summary-mark-forward n gnus-dormant-mark))
11874
11875 (defun gnus-summary-set-process-mark (article)
11876   "Set the process mark on ARTICLE and update the summary line."
11877   (setq gnus-newsgroup-processable
11878         (cons article
11879               (delq article gnus-newsgroup-processable)))
11880   (when (gnus-summary-goto-subject article)
11881     (gnus-summary-show-thread)
11882     (gnus-summary-update-secondary-mark article)))
11883
11884 (defun gnus-summary-remove-process-mark (article)
11885   "Remove the process mark from ARTICLE and update the summary line."
11886   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11887   (when (gnus-summary-goto-subject article)
11888     (gnus-summary-show-thread)
11889     (gnus-summary-update-secondary-mark article)))
11890
11891 (defun gnus-summary-set-saved-mark (article)
11892   "Set the process mark on ARTICLE and update the summary line."
11893   (push article gnus-newsgroup-saved)
11894   (when (gnus-summary-goto-subject article)
11895     (gnus-summary-update-secondary-mark article)))
11896
11897 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11898   "Mark N articles as read forwards.
11899 If N is negative, mark backwards instead.
11900 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11901 marked as unread.
11902 The difference between N and the actual number of articles marked is
11903 returned."
11904   (interactive "p")
11905   (gnus-set-global-variables)
11906   (let ((backward (< n 0))
11907         (gnus-summary-goto-unread
11908          (and gnus-summary-goto-unread
11909               (not (eq gnus-summary-goto-unread 'never))
11910               (not (memq mark (list gnus-unread-mark
11911                                     gnus-ticked-mark gnus-dormant-mark)))))
11912         (n (abs n))
11913         (mark (or mark gnus-del-mark)))
11914     (while (and (> n 0)
11915                 (gnus-summary-mark-article nil mark no-expire)
11916                 (zerop (gnus-summary-next-subject
11917                         (if backward -1 1)
11918                         (and gnus-summary-goto-unread
11919                              (not (eq gnus-summary-goto-unread 'never)))
11920                         t)))
11921       (setq n (1- n)))
11922     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11923     (gnus-summary-recenter)
11924     (gnus-summary-position-point)
11925     (gnus-set-mode-line 'summary)
11926     n))
11927
11928 (defun gnus-summary-mark-article-as-read (mark)
11929   "Mark the current article quickly as read with MARK."
11930   (let ((article (gnus-summary-article-number)))
11931     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11932     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11933     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11934     (setq gnus-newsgroup-reads
11935           (cons (cons article mark) gnus-newsgroup-reads))
11936     ;; Possibly remove from cache, if that is used.
11937     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11938     ;; Allow the backend to change the mark.
11939     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11940     ;; Check for auto-expiry.
11941     (when (and gnus-newsgroup-auto-expire
11942                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11943                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11944                    (= mark gnus-ancient-mark)
11945                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11946       (setq mark gnus-expirable-mark)
11947       (push article gnus-newsgroup-expirable))
11948     ;; Set the mark in the buffer.
11949     (gnus-summary-update-mark mark 'unread)
11950     t))
11951
11952 (defun gnus-summary-mark-article-as-unread (mark)
11953   "Mark the current article quickly as unread with MARK."
11954   (let ((article (gnus-summary-article-number)))
11955     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11956     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11957     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11958     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11959     (cond ((= mark gnus-ticked-mark)
11960            (push article gnus-newsgroup-marked))
11961           ((= mark gnus-dormant-mark)
11962            (push article gnus-newsgroup-dormant))
11963           (t
11964            (push article gnus-newsgroup-unreads)))
11965     (setq gnus-newsgroup-reads
11966           (delq (assq article gnus-newsgroup-reads)
11967                 gnus-newsgroup-reads))
11968
11969     ;; See whether the article is to be put in the cache.
11970     (and gnus-use-cache
11971          (vectorp (gnus-summary-article-header article))
11972          (save-excursion
11973            (gnus-cache-possibly-enter-article
11974             gnus-newsgroup-name article
11975             (gnus-summary-article-header article)
11976             (= mark gnus-ticked-mark)
11977             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11978
11979     ;; Fix the mark.
11980     (gnus-summary-update-mark mark 'unread)
11981     t))
11982
11983 (defun gnus-summary-mark-article (&optional article mark no-expire)
11984   "Mark ARTICLE with MARK.  MARK can be any character.
11985 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11986 `??' (dormant) and `?E' (expirable).
11987 If MARK is nil, then the default character `?D' is used.
11988 If ARTICLE is nil, then the article on the current line will be
11989 marked."
11990   ;; The mark might be a string.
11991   (and (stringp mark)
11992        (setq mark (aref mark 0)))
11993   ;; If no mark is given, then we check auto-expiring.
11994   (and (not no-expire)
11995        gnus-newsgroup-auto-expire
11996        (or (not mark)
11997            (and (numberp mark)
11998                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11999                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12000                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12001        (setq mark gnus-expirable-mark))
12002   (let* ((mark (or mark gnus-del-mark))
12003          (article (or article (gnus-summary-article-number))))
12004     (or article (error "No article on current line"))
12005     (if (or (= mark gnus-unread-mark)
12006             (= mark gnus-ticked-mark)
12007             (= mark gnus-dormant-mark))
12008         (gnus-mark-article-as-unread article mark)
12009       (gnus-mark-article-as-read article mark))
12010
12011     ;; See whether the article is to be put in the cache.
12012     (and gnus-use-cache
12013          (not (= mark gnus-canceled-mark))
12014          (vectorp (gnus-summary-article-header article))
12015          (save-excursion
12016            (gnus-cache-possibly-enter-article
12017             gnus-newsgroup-name article
12018             (gnus-summary-article-header article)
12019             (= mark gnus-ticked-mark)
12020             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12021
12022     (if (gnus-summary-goto-subject article nil t)
12023         (let ((buffer-read-only nil))
12024           (gnus-summary-show-thread)
12025           ;; Fix the mark.
12026           (gnus-summary-update-mark mark 'unread)
12027           t))))
12028
12029 (defun gnus-summary-update-secondary-mark (article)
12030   "Update the secondary (read, process, cache) mark."
12031   (gnus-summary-update-mark
12032    (cond ((memq article gnus-newsgroup-processable)
12033           gnus-process-mark)
12034          ((memq article gnus-newsgroup-cached)
12035           gnus-cached-mark)
12036          ((memq article gnus-newsgroup-replied)
12037           gnus-replied-mark)
12038          ((memq article gnus-newsgroup-saved)
12039           gnus-saved-mark)
12040          (t gnus-unread-mark))
12041    'replied)
12042   (when (gnus-visual-p 'summary-highlight 'highlight)
12043     (run-hooks 'gnus-summary-update-hook))
12044   t)
12045
12046 (defun gnus-summary-update-mark (mark type)
12047   (beginning-of-line)
12048   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12049         (buffer-read-only nil))
12050     (when (and forward
12051                (<= (+ forward (point)) (point-max)))
12052       ;; Go to the right position on the line.
12053       (goto-char (+ forward (point)))
12054       ;; Replace the old mark with the new mark.
12055       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12056       ;; Optionally update the marks by some user rule.
12057       (when (eq type 'unread)
12058         (gnus-data-set-mark
12059          (gnus-data-find (gnus-summary-article-number)) mark)
12060         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12061
12062 (defun gnus-mark-article-as-read (article &optional mark)
12063   "Enter ARTICLE in the pertinent lists and remove it from others."
12064   ;; Make the article expirable.
12065   (let ((mark (or mark gnus-del-mark)))
12066     (if (= mark gnus-expirable-mark)
12067         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12068       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12069     ;; Remove from unread and marked lists.
12070     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12071     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12072     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12073     (push (cons article mark) gnus-newsgroup-reads)
12074     ;; Possibly remove from cache, if that is used.
12075     (when gnus-use-cache
12076       (gnus-cache-enter-remove-article article))))
12077
12078 (defun gnus-mark-article-as-unread (article &optional mark)
12079   "Enter ARTICLE in the pertinent lists and remove it from others."
12080   (let ((mark (or mark gnus-ticked-mark)))
12081     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12082     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12083     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12084     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12085     (cond ((= mark gnus-ticked-mark)
12086            (push article gnus-newsgroup-marked))
12087           ((= mark gnus-dormant-mark)
12088            (push article gnus-newsgroup-dormant))
12089           (t
12090            (push article gnus-newsgroup-unreads)))
12091     (setq gnus-newsgroup-reads
12092           (delq (assq article gnus-newsgroup-reads)
12093                 gnus-newsgroup-reads))))
12094
12095 (defalias 'gnus-summary-mark-as-unread-forward
12096   'gnus-summary-tick-article-forward)
12097 (make-obsolete 'gnus-summary-mark-as-unread-forward
12098                'gnus-summary-tick-article-forward)
12099 (defun gnus-summary-tick-article-forward (n)
12100   "Tick N articles forwards.
12101 If N is negative, tick backwards instead.
12102 The difference between N and the number of articles ticked is returned."
12103   (interactive "p")
12104   (gnus-summary-mark-forward n gnus-ticked-mark))
12105
12106 (defalias 'gnus-summary-mark-as-unread-backward
12107   'gnus-summary-tick-article-backward)
12108 (make-obsolete 'gnus-summary-mark-as-unread-backward
12109                'gnus-summary-tick-article-backward)
12110 (defun gnus-summary-tick-article-backward (n)
12111   "Tick N articles backwards.
12112 The difference between N and the number of articles ticked is returned."
12113   (interactive "p")
12114   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12115
12116 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12117 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12118 (defun gnus-summary-tick-article (&optional article clear-mark)
12119   "Mark current article as unread.
12120 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12121 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12122   (interactive)
12123   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12124                                        gnus-ticked-mark)))
12125
12126 (defun gnus-summary-mark-as-read-forward (n)
12127   "Mark N articles as read forwards.
12128 If N is negative, mark backwards instead.
12129 The difference between N and the actual number of articles marked is
12130 returned."
12131   (interactive "p")
12132   (gnus-summary-mark-forward n gnus-del-mark t))
12133
12134 (defun gnus-summary-mark-as-read-backward (n)
12135   "Mark the N articles as read backwards.
12136 The difference between N and the actual number of articles marked is
12137 returned."
12138   (interactive "p")
12139   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12140
12141 (defun gnus-summary-mark-as-read (&optional article mark)
12142   "Mark current article as read.
12143 ARTICLE specifies the article to be marked as read.
12144 MARK specifies a string to be inserted at the beginning of the line."
12145   (gnus-summary-mark-article article mark))
12146
12147 (defun gnus-summary-clear-mark-forward (n)
12148   "Clear marks from N articles forward.
12149 If N is negative, clear backward instead.
12150 The difference between N and the number of marks cleared is returned."
12151   (interactive "p")
12152   (gnus-summary-mark-forward n gnus-unread-mark))
12153
12154 (defun gnus-summary-clear-mark-backward (n)
12155   "Clear marks from N articles backward.
12156 The difference between N and the number of marks cleared is returned."
12157   (interactive "p")
12158   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12159
12160 (defun gnus-summary-mark-unread-as-read ()
12161   "Intended to be used by `gnus-summary-mark-article-hook'."
12162   (when (memq gnus-current-article gnus-newsgroup-unreads)
12163     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12164
12165 (defun gnus-summary-mark-read-and-unread-as-read ()
12166   "Intended to be used by `gnus-summary-mark-article-hook'."
12167   (let ((mark (gnus-summary-article-mark)))
12168     (when (or (gnus-unread-mark-p mark)
12169               (gnus-read-mark-p mark))
12170       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12171
12172 (defun gnus-summary-mark-region-as-read (point mark all)
12173   "Mark all unread articles between point and mark as read.
12174 If given a prefix, mark all articles between point and mark as read,
12175 even ticked and dormant ones."
12176   (interactive "r\nP")
12177   (save-excursion
12178     (let (article)
12179       (goto-char point)
12180       (beginning-of-line)
12181       (while (and
12182               (< (point) mark)
12183               (progn
12184                 (when (or all
12185                           (memq (setq article (gnus-summary-article-number))
12186                                 gnus-newsgroup-unreads))
12187                   (gnus-summary-mark-article article gnus-del-mark))
12188                 t)
12189               (gnus-summary-find-next))))))
12190
12191 (defun gnus-summary-mark-below (score mark)
12192   "Mark articles with score less than SCORE with MARK."
12193   (interactive "P\ncMark: ")
12194   (gnus-set-global-variables)
12195   (setq score (if score
12196                   (prefix-numeric-value score)
12197                 (or gnus-summary-default-score 0)))
12198   (save-excursion
12199     (set-buffer gnus-summary-buffer)
12200     (goto-char (point-min))
12201     (while 
12202         (progn
12203           (and (< (gnus-summary-article-score) score)
12204                (gnus-summary-mark-article nil mark))
12205           (gnus-summary-find-next)))))
12206
12207 (defun gnus-summary-kill-below (&optional score)
12208   "Mark articles with score below SCORE as read."
12209   (interactive "P")
12210   (gnus-set-global-variables)
12211   (gnus-summary-mark-below score gnus-killed-mark))
12212
12213 (defun gnus-summary-clear-above (&optional score)
12214   "Clear all marks from articles with score above SCORE."
12215   (interactive "P")
12216   (gnus-set-global-variables)
12217   (gnus-summary-mark-above score gnus-unread-mark))
12218
12219 (defun gnus-summary-tick-above (&optional score)
12220   "Tick all articles with score above SCORE."
12221   (interactive "P")
12222   (gnus-set-global-variables)
12223   (gnus-summary-mark-above score gnus-ticked-mark))
12224
12225 (defun gnus-summary-mark-above (score mark)
12226   "Mark articles with score over SCORE with MARK."
12227   (interactive "P\ncMark: ")
12228   (gnus-set-global-variables)
12229   (setq score (if score
12230                   (prefix-numeric-value score)
12231                 (or gnus-summary-default-score 0)))
12232   (save-excursion
12233     (set-buffer gnus-summary-buffer)
12234     (goto-char (point-min))
12235     (while (and (progn
12236                   (if (> (gnus-summary-article-score) score)
12237                       (gnus-summary-mark-article nil mark))
12238                   t)
12239                 (gnus-summary-find-next)))))
12240
12241 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12242 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12243 (defun gnus-summary-limit-include-expunged ()
12244   "Display all the hidden articles that were expunged for low scores."
12245   (interactive)
12246   (gnus-set-global-variables)
12247   (let ((buffer-read-only nil))
12248     (let ((scored gnus-newsgroup-scored)
12249           headers h)
12250       (while scored
12251         (or (gnus-summary-goto-subject (caar scored))
12252             (and (setq h (gnus-summary-article-header (caar scored)))
12253                  (< (cdar scored) gnus-summary-expunge-below)
12254                  (setq headers (cons h headers))))
12255         (setq scored (cdr scored)))
12256       (or headers (error "No expunged articles hidden."))
12257       (goto-char (point-min))
12258       (gnus-summary-prepare-unthreaded (nreverse headers)))
12259     (goto-char (point-min))
12260     (gnus-summary-position-point)))
12261
12262 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12263   "Mark all articles not marked as unread in this newsgroup as read.
12264 If prefix argument ALL is non-nil, all articles are marked as read.
12265 If QUIETLY is non-nil, no questions will be asked.
12266 If TO-HERE is non-nil, it should be a point in the buffer.  All
12267 articles before this point will be marked as read.
12268 The number of articles marked as read is returned."
12269   (interactive "P")
12270   (gnus-set-global-variables)
12271   (prog1
12272       (if (or quietly
12273               (not gnus-interactive-catchup) ;Without confirmation?
12274               gnus-expert-user
12275               (gnus-y-or-n-p
12276                (if all
12277                    "Mark absolutely all articles as read? "
12278                  "Mark all unread articles as read? ")))
12279           (if (and not-mark
12280                    (not gnus-newsgroup-adaptive)
12281                    (not gnus-newsgroup-auto-expire))
12282               (progn
12283                 (when all
12284                   (setq gnus-newsgroup-marked nil
12285                         gnus-newsgroup-dormant nil))
12286                 (setq gnus-newsgroup-unreads nil))
12287             ;; We actually mark all articles as canceled, which we
12288             ;; have to do when using auto-expiry or adaptive scoring.
12289             (gnus-summary-show-all-threads)
12290             (if (gnus-summary-first-subject (not all))
12291                 (while (and
12292                         (if to-here (< (point) to-here) t)
12293                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12294                         (gnus-summary-find-next (not all)))))
12295             (unless to-here
12296               (setq gnus-newsgroup-unreads nil))
12297             (gnus-set-mode-line 'summary)))
12298     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12299       (if (and (not to-here) (eq 'nnvirtual (car method)))
12300           (nnvirtual-catchup-group
12301            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12302     (gnus-summary-position-point)))
12303
12304 (defun gnus-summary-catchup-to-here (&optional all)
12305   "Mark all unticked articles before the current one as read.
12306 If ALL is non-nil, also mark ticked and dormant articles as read."
12307   (interactive "P")
12308   (gnus-set-global-variables)
12309   (save-excursion
12310     (let ((beg (point)))
12311       ;; We check that there are unread articles.
12312       (when (or all (gnus-summary-find-prev))
12313         (gnus-summary-catchup all t beg))))
12314   (gnus-summary-position-point))
12315
12316 (defun gnus-summary-catchup-all (&optional quietly)
12317   "Mark all articles in this newsgroup as read."
12318   (interactive "P")
12319   (gnus-set-global-variables)
12320   (gnus-summary-catchup t quietly))
12321
12322 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12323   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12324 If prefix argument ALL is non-nil, all articles are marked as read."
12325   (interactive "P")
12326   (gnus-set-global-variables)
12327   (gnus-summary-catchup all quietly nil 'fast)
12328   ;; Select next newsgroup or exit.
12329   (if (eq gnus-auto-select-next 'quietly)
12330       (gnus-summary-next-group nil)
12331     (gnus-summary-exit)))
12332
12333 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12334   "Mark all articles in this newsgroup as read, and then exit."
12335   (interactive "P")
12336   (gnus-set-global-variables)
12337   (gnus-summary-catchup-and-exit t quietly))
12338
12339 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12340 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12341   "Mark all articles in this group as read and select the next group.
12342 If given a prefix, mark all articles, unread as well as ticked, as
12343 read."
12344   (interactive "P")
12345   (gnus-set-global-variables)
12346   (save-excursion
12347     (gnus-summary-catchup all))
12348   (gnus-summary-next-article t nil nil t))
12349
12350 ;; Thread-based commands.
12351
12352 (defun gnus-summary-articles-in-thread (&optional article)
12353   "Return a list of all articles in the current thread.
12354 If ARTICLE is non-nil, return all articles in the thread that starts
12355 with that article."
12356   (let* ((article (or article (gnus-summary-article-number)))
12357          (data (gnus-data-find-list article))
12358          (top-level (gnus-data-level (car data)))
12359          (top-subject
12360           (cond ((null gnus-thread-operation-ignore-subject)
12361                  (gnus-simplify-subject-re
12362                   (mail-header-subject (gnus-data-header (car data)))))
12363                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12364                  (gnus-simplify-subject-fuzzy
12365                   (mail-header-subject (gnus-data-header (car data)))))
12366                 (t nil)))
12367          (end-point (save-excursion
12368                       (if (gnus-summary-go-to-next-thread) 
12369                           (point) (point-max))))
12370          articles)
12371     (while (and data
12372                 (< (gnus-data-pos (car data)) end-point))
12373       (when (or (not top-subject)
12374                 (string= top-subject
12375                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12376                              (gnus-simplify-subject-fuzzy
12377                               (mail-header-subject
12378                                (gnus-data-header (car data))))
12379                            (gnus-simplify-subject-re
12380                             (mail-header-subject
12381                              (gnus-data-header (car data)))))))
12382         (push (gnus-data-number (car data)) articles))
12383       (unless (and (setq data (cdr data))
12384                    (> (gnus-data-level (car data)) top-level))
12385         (setq data nil)))
12386     ;; Return the list of articles.
12387     (nreverse articles)))
12388
12389 (defun gnus-summary-rethread-current ()
12390   "Rethread the thread the current article is part of."
12391   (interactive)
12392   (gnus-set-global-variables)
12393   (let* ((gnus-show-threads t)
12394          (article (gnus-summary-article-number))
12395          (id (mail-header-id (gnus-summary-article-header)))
12396          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12397     (unless id
12398       (error "No article on the current line"))
12399     (gnus-rebuild-thread id)
12400     (gnus-summary-goto-subject article)))
12401
12402 (defun gnus-summary-reparent-thread ()
12403   "Make current article child of the marked (or previous) article.
12404
12405 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12406 is non-nil or the Subject: of both articles are the same."
12407   (interactive)
12408   (or (not (gnus-group-read-only-p))
12409       (error "The current newsgroup does not support article editing."))
12410   (or (<= (length gnus-newsgroup-processable) 1)
12411       (error "No more than one article may be marked."))
12412   (save-window-excursion
12413     (let ((gnus-article-buffer " *reparent*")
12414           (current-article (gnus-summary-article-number))
12415           ; first grab the marked article, otherwise one line up.
12416           (parent-article (if (not (null gnus-newsgroup-processable))
12417                               (car gnus-newsgroup-processable)
12418                             (save-excursion
12419                               (if (eq (forward-line -1) 0)
12420                                   (gnus-summary-article-number)
12421                                 (error "Beginning of summary buffer."))))))
12422       (or (not (eq current-article parent-article))
12423           (error "An article may not be self-referential."))
12424       (let ((message-id (mail-header-id 
12425                          (gnus-summary-article-header parent-article))))
12426         (or (and message-id (not (equal message-id "")))
12427             (error "No message-id in desired parent."))
12428         (gnus-summary-select-article t t nil current-article)
12429         (set-buffer gnus-article-buffer)
12430         (setq buffer-read-only nil)
12431         (let ((buf (format "%s" (buffer-string))))
12432           (erase-buffer)
12433           (insert buf))
12434         (goto-char (point-min))
12435         (if (search-forward-regexp "^References: " nil t)
12436             (insert message-id " " )
12437           (insert "References: " message-id "\n"))
12438         (or (gnus-request-replace-article current-article
12439                                           (car gnus-article-current)
12440                                           gnus-article-buffer)
12441             (error "Couldn't replace article."))
12442         (set-buffer gnus-summary-buffer)
12443         (gnus-summary-unmark-all-processable)
12444         (gnus-summary-rethread-current)
12445         (gnus-message 3 "Article %d is now the child of article %d."
12446                       current-article parent-article)))))
12447
12448 (defun gnus-summary-toggle-threads (&optional arg)
12449   "Toggle showing conversation threads.
12450 If ARG is positive number, turn showing conversation threads on."
12451   (interactive "P")
12452   (gnus-set-global-variables)
12453   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12454     (setq gnus-show-threads
12455           (if (null arg) (not gnus-show-threads)
12456             (> (prefix-numeric-value arg) 0)))
12457     (gnus-summary-prepare)
12458     (gnus-summary-goto-subject current)
12459     (gnus-summary-position-point)))
12460
12461 (defun gnus-summary-show-all-threads ()
12462   "Show all threads."
12463   (interactive)
12464   (gnus-set-global-variables)
12465   (save-excursion
12466     (let ((buffer-read-only nil))
12467       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12468   (gnus-summary-position-point))
12469
12470 (defun gnus-summary-show-thread ()
12471   "Show thread subtrees.
12472 Returns nil if no thread was there to be shown."
12473   (interactive)
12474   (gnus-set-global-variables)
12475   (let ((buffer-read-only nil)
12476         (orig (point))
12477         ;; first goto end then to beg, to have point at beg after let
12478         (end (progn (end-of-line) (point)))
12479         (beg (progn (beginning-of-line) (point))))
12480     (prog1
12481         ;; Any hidden lines here?
12482         (search-forward "\r" end t)
12483       (subst-char-in-region beg end ?\^M ?\n t)
12484       (goto-char orig)
12485       (gnus-summary-position-point))))
12486
12487 (defun gnus-summary-hide-all-threads ()
12488   "Hide all thread subtrees."
12489   (interactive)
12490   (gnus-set-global-variables)
12491   (save-excursion
12492     (goto-char (point-min))
12493     (gnus-summary-hide-thread)
12494     (while (zerop (gnus-summary-next-thread 1 t))
12495       (gnus-summary-hide-thread)))
12496   (gnus-summary-position-point))
12497
12498 (defun gnus-summary-hide-thread ()
12499   "Hide thread subtrees.
12500 Returns nil if no threads were there to be hidden."
12501   (interactive)
12502   (gnus-set-global-variables)
12503   (let ((buffer-read-only nil)
12504         (start (point))
12505         (article (gnus-summary-article-number)))
12506     (goto-char start)
12507     ;; Go forward until either the buffer ends or the subthread
12508     ;; ends.
12509     (when (and (not (eobp))
12510                (or (zerop (gnus-summary-next-thread 1 t))
12511                    (goto-char (point-max))))
12512       (prog1
12513           (if (and (> (point) start)
12514                    (search-backward "\n" start t))
12515               (progn
12516                 (subst-char-in-region start (point) ?\n ?\^M)
12517                 (gnus-summary-goto-subject article))
12518             (goto-char start)
12519             nil)
12520         ;;(gnus-summary-position-point)
12521         ))))
12522
12523 (defun gnus-summary-go-to-next-thread (&optional previous)
12524   "Go to the same level (or less) next thread.
12525 If PREVIOUS is non-nil, go to previous thread instead.
12526 Return the article number moved to, or nil if moving was impossible."
12527   (let ((level (gnus-summary-thread-level))
12528         (way (if previous -1 1))
12529         (beg (point)))
12530     (forward-line way)
12531     (while (and (not (eobp))
12532                 (< level (gnus-summary-thread-level)))
12533       (forward-line way))
12534     (if (eobp)
12535         (progn
12536           (goto-char beg)
12537           nil)
12538       (setq beg (point))
12539       (prog1
12540           (gnus-summary-article-number)
12541         (goto-char beg)))))
12542
12543 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12544   "Go to the same level (or less) next thread.
12545 If PREVIOUS is non-nil, go to previous thread instead.
12546 Return the article number moved to, or nil if moving was impossible."
12547   (if (and (eq gnus-summary-make-false-root 'dummy)
12548            (gnus-summary-article-intangible-p))
12549       (let ((beg (point)))
12550         (while (and (zerop (forward-line 1))
12551                     (not (gnus-summary-article-intangible-p))
12552                     (not (zerop (save-excursion 
12553                                   (gnus-summary-thread-level))))))
12554         (if (eobp)
12555             (progn
12556               (goto-char beg)
12557               nil)
12558           (point)))
12559     (let* ((level (gnus-summary-thread-level))
12560            (article (gnus-summary-article-number))
12561            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12562            oart)
12563       (while data
12564         (if (<= (gnus-data-level (car data)) level)
12565             (setq oart (gnus-data-number (car data))
12566                   data nil)
12567           (setq data (cdr data))))
12568       (and oart
12569            (gnus-summary-goto-subject oart)))))
12570
12571 (defun gnus-summary-next-thread (n &optional silent)
12572   "Go to the same level next N'th thread.
12573 If N is negative, search backward instead.
12574 Returns the difference between N and the number of skips actually
12575 done.
12576
12577 If SILENT, don't output messages."
12578   (interactive "p")
12579   (gnus-set-global-variables)
12580   (let ((backward (< n 0))
12581         (n (abs n))
12582         old dum int)
12583     (while (and (> n 0)
12584                 (gnus-summary-go-to-next-thread backward))
12585       (decf n))
12586     (unless silent 
12587       (gnus-summary-position-point))
12588     (when (and (not silent) (/= 0 n))
12589       (gnus-message 7 "No more threads"))
12590     n))
12591
12592 (defun gnus-summary-prev-thread (n)
12593   "Go to the same level previous N'th thread.
12594 Returns the difference between N and the number of skips actually
12595 done."
12596   (interactive "p")
12597   (gnus-set-global-variables)
12598   (gnus-summary-next-thread (- n)))
12599
12600 (defun gnus-summary-go-down-thread ()
12601   "Go down one level in the current thread."
12602   (let ((children (gnus-summary-article-children)))
12603     (and children
12604          (gnus-summary-goto-subject (car children)))))
12605
12606 (defun gnus-summary-go-up-thread ()
12607   "Go up one level in the current thread."
12608   (let ((parent (gnus-summary-article-parent)))
12609     (and parent
12610          (gnus-summary-goto-subject parent))))
12611
12612 (defun gnus-summary-down-thread (n)
12613   "Go down thread N steps.
12614 If N is negative, go up instead.
12615 Returns the difference between N and how many steps down that were
12616 taken."
12617   (interactive "p")
12618   (gnus-set-global-variables)
12619   (let ((up (< n 0))
12620         (n (abs n)))
12621     (while (and (> n 0)
12622                 (if up (gnus-summary-go-up-thread)
12623                   (gnus-summary-go-down-thread)))
12624       (setq n (1- n)))
12625     (gnus-summary-position-point)
12626     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12627     n))
12628
12629 (defun gnus-summary-up-thread (n)
12630   "Go up thread N steps.
12631 If N is negative, go up instead.
12632 Returns the difference between N and how many steps down that were
12633 taken."
12634   (interactive "p")
12635   (gnus-set-global-variables)
12636   (gnus-summary-down-thread (- n)))
12637
12638 (defun gnus-summary-top-thread ()
12639   "Go to the top of the thread."
12640   (interactive)
12641   (gnus-set-global-variables)
12642   (while (gnus-summary-go-up-thread))
12643   (gnus-summary-article-number))
12644
12645 (defun gnus-summary-kill-thread (&optional unmark)
12646   "Mark articles under current thread as read.
12647 If the prefix argument is positive, remove any kinds of marks.
12648 If the prefix argument is negative, tick articles instead."
12649   (interactive "P")
12650   (gnus-set-global-variables)
12651   (if unmark
12652       (setq unmark (prefix-numeric-value unmark)))
12653   (let ((articles (gnus-summary-articles-in-thread)))
12654     (save-excursion
12655       ;; Expand the thread.
12656       (gnus-summary-show-thread)
12657       ;; Mark all the articles.
12658       (while articles
12659         (gnus-summary-goto-subject (car articles))
12660         (cond ((null unmark)
12661                (gnus-summary-mark-article-as-read gnus-killed-mark))
12662               ((> unmark 0)
12663                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12664               (t
12665                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12666         (setq articles (cdr articles))))
12667     ;; Hide killed subtrees.
12668     (and (null unmark)
12669          gnus-thread-hide-killed
12670          (gnus-summary-hide-thread))
12671     ;; If marked as read, go to next unread subject.
12672     (if (null unmark)
12673         ;; Go to next unread subject.
12674         (gnus-summary-next-subject 1 t)))
12675   (gnus-set-mode-line 'summary))
12676
12677 ;; Summary sorting commands
12678
12679 (defun gnus-summary-sort-by-number (&optional reverse)
12680   "Sort summary buffer by article number.
12681 Argument REVERSE means reverse order."
12682   (interactive "P")
12683   (gnus-summary-sort 'number reverse))
12684
12685 (defun gnus-summary-sort-by-author (&optional reverse)
12686   "Sort summary buffer by author name alphabetically.
12687 If case-fold-search is non-nil, case of letters is ignored.
12688 Argument REVERSE means reverse order."
12689   (interactive "P")
12690   (gnus-summary-sort 'author reverse))
12691
12692 (defun gnus-summary-sort-by-subject (&optional reverse)
12693   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12694 If case-fold-search is non-nil, case of letters is ignored.
12695 Argument REVERSE means reverse order."
12696   (interactive "P")
12697   (gnus-summary-sort 'subject reverse))
12698
12699 (defun gnus-summary-sort-by-date (&optional reverse)
12700   "Sort summary buffer by date.
12701 Argument REVERSE means reverse order."
12702   (interactive "P")
12703   (gnus-summary-sort 'date reverse))
12704
12705 (defun gnus-summary-sort-by-score (&optional reverse)
12706   "Sort summary buffer by score.
12707 Argument REVERSE means reverse order."
12708   (interactive "P")
12709   (gnus-summary-sort 'score reverse))
12710
12711 (defun gnus-summary-sort (predicate reverse)
12712   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12713   (gnus-set-global-variables)
12714   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12715          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12716          (gnus-thread-sort-functions
12717           (list
12718            (if (not reverse)
12719                thread
12720              `(lambda (t1 t2)
12721                 (,thread t2 t1)))))
12722          (gnus-article-sort-functions
12723           (list
12724            (if (not reverse)
12725                article
12726              `(lambda (t1 t2)
12727                 (,article t2 t1)))))
12728          (buffer-read-only)
12729          (gnus-summary-prepare-hook nil))
12730     ;; We do the sorting by regenerating the threads.
12731     (gnus-summary-prepare)
12732     ;; Hide subthreads if needed.
12733     (when (and gnus-show-threads gnus-thread-hide-subtree)
12734       (gnus-summary-hide-all-threads)))
12735   ;; If in async mode, we send some info to the backend.
12736   (when gnus-newsgroup-async
12737     (gnus-request-asynchronous
12738      gnus-newsgroup-name gnus-newsgroup-data)))
12739
12740 (defun gnus-sortable-date (date)
12741   "Make sortable string by string-lessp from DATE.
12742 Timezone package is used."
12743   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12744          (year (aref date 0))
12745          (month (aref date 1))
12746          (day (aref date 2)))
12747     (timezone-make-sortable-date
12748      year month day
12749      (timezone-make-time-string
12750       (aref date 3) (aref date 4) (aref date 5)))))
12751
12752 ;; Summary saving commands.
12753
12754 (defun gnus-summary-save-article (&optional n not-saved)
12755   "Save the current article using the default saver function.
12756 If N is a positive number, save the N next articles.
12757 If N is a negative number, save the N previous articles.
12758 If N is nil and any articles have been marked with the process mark,
12759 save those articles instead.
12760 The variable `gnus-default-article-saver' specifies the saver function."
12761   (interactive "P")
12762   (gnus-set-global-variables)
12763   (let ((articles (gnus-summary-work-articles n))
12764         file header article)
12765     (while articles
12766       (setq header (gnus-summary-article-header
12767                     (setq article (pop articles))))
12768       (if (not (vectorp header))
12769           ;; This is a pseudo-article.
12770           (if (assq 'name header)
12771               (gnus-copy-file (cdr (assq 'name header)))
12772             (gnus-message 1 "Article %d is unsaveable" article))
12773         ;; This is a real article.
12774         (save-window-excursion
12775           (gnus-summary-select-article t nil nil article))
12776         (unless gnus-save-all-headers
12777           ;; Remove headers accoring to `gnus-saved-headers'.
12778           (let ((gnus-visible-headers
12779                  (or gnus-saved-headers gnus-visible-headers)))
12780             (gnus-article-hide-headers nil t)))
12781         ;; Remove any X-Gnus lines.
12782         (save-excursion
12783           (set-buffer gnus-article-buffer)
12784           (save-restriction
12785             (let ((buffer-read-only nil))
12786               (nnheader-narrow-to-headers)
12787               (while (re-search-forward "^X-Gnus" nil t)
12788                 (gnus-delete-line)))))
12789         (save-window-excursion
12790           (if (not gnus-default-article-saver)
12791               (error "No default saver is defined.")
12792             (setq file (funcall
12793                         gnus-default-article-saver
12794                         (cond
12795                          ((not gnus-prompt-before-saving)
12796                           'default)
12797                          ((eq gnus-prompt-before-saving 'always)
12798                           nil)
12799                          (t file))))))
12800         (gnus-summary-remove-process-mark article)
12801         (unless not-saved
12802           (gnus-summary-set-saved-mark article))))
12803     (gnus-summary-position-point)
12804     n))
12805
12806 (defun gnus-summary-pipe-output (&optional arg)
12807   "Pipe the current article to a subprocess.
12808 If N is a positive number, pipe the N next articles.
12809 If N is a negative number, pipe the N previous articles.
12810 If N is nil and any articles have been marked with the process mark,
12811 pipe those articles instead."
12812   (interactive "P")
12813   (gnus-set-global-variables)
12814   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12815     (gnus-summary-save-article arg t))
12816   (gnus-configure-windows 'pipe))
12817
12818 (defun gnus-summary-save-article-mail (&optional arg)
12819   "Append the current article to an mail file.
12820 If N is a positive number, save the N next articles.
12821 If N is a negative number, save the N previous articles.
12822 If N is nil and any articles have been marked with the process mark,
12823 save those articles instead."
12824   (interactive "P")
12825   (gnus-set-global-variables)
12826   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12827     (gnus-summary-save-article arg)))
12828
12829 (defun gnus-summary-save-article-rmail (&optional arg)
12830   "Append the current article to an rmail file.
12831 If N is a positive number, save the N next articles.
12832 If N is a negative number, save the N previous articles.
12833 If N is nil and any articles have been marked with the process mark,
12834 save those articles instead."
12835   (interactive "P")
12836   (gnus-set-global-variables)
12837   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12838     (gnus-summary-save-article arg)))
12839
12840 (defun gnus-summary-save-article-file (&optional arg)
12841   "Append the current article to a file.
12842 If N is a positive number, save the N next articles.
12843 If N is a negative number, save the N previous articles.
12844 If N is nil and any articles have been marked with the process mark,
12845 save those articles instead."
12846   (interactive "P")
12847   (gnus-set-global-variables)
12848   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12849     (gnus-summary-save-article arg)))
12850
12851 (defun gnus-summary-save-article-body-file (&optional arg)
12852   "Append the current article body to a file.
12853 If N is a positive number, save the N next articles.
12854 If N is a negative number, save the N previous articles.
12855 If N is nil and any articles have been marked with the process mark,
12856 save those articles instead."
12857   (interactive "P")
12858   (gnus-set-global-variables)
12859   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12860     (gnus-summary-save-article arg)))
12861
12862 (defun gnus-get-split-value (methods)
12863   "Return a value based on the split METHODS."
12864   (let (split-name method result match)
12865     (when methods
12866       (save-excursion
12867         (set-buffer gnus-original-article-buffer)
12868         (save-restriction
12869           (nnheader-narrow-to-headers)
12870           (while methods
12871             (goto-char (point-min))
12872             (setq method (pop methods))
12873             (setq match (car method))
12874             (when (cond
12875                    ((stringp match)
12876                     ;; Regular expression.
12877                     (condition-case ()
12878                         (re-search-forward match nil t)
12879                       (error nil)))
12880                    ((gnus-functionp match)
12881                     ;; Function.
12882                     (save-restriction
12883                       (widen)
12884                       (setq result (funcall match gnus-newsgroup-name))))
12885                    ((consp match)
12886                     ;; Form.
12887                     (save-restriction
12888                       (widen)
12889                       (setq result (eval match)))))
12890               (setq split-name (append (cdr method) split-name))
12891               (cond ((stringp result)
12892                      (push result split-name))
12893                     ((consp result)
12894                      (setq split-name (append result split-name)))))))))
12895     split-name))
12896
12897 (defun gnus-read-move-group-name (prompt default articles prefix)
12898   "Read a group name."
12899   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12900          group-map
12901          (dum (mapatoms
12902                (lambda (g) 
12903                  (and (boundp g)
12904                       (symbol-name g)
12905                       (memq 'respool
12906                             (assoc (symbol-name
12907                                     (car (gnus-find-method-for-group
12908                                           (symbol-name g))))
12909                                    gnus-valid-select-methods))
12910                       (push (list (symbol-name g)) group-map)))
12911                gnus-active-hashtb))
12912          (prom
12913           (format "%s %s to:"
12914                   prompt
12915                   (if (> (length articles) 1)
12916                       (format "these %d articles" (length articles))
12917                     "this article")))
12918          (to-newsgroup
12919           (cond
12920            ((null split-name)
12921             (gnus-completing-read default prom
12922                                   group-map nil nil prefix
12923                                   'gnus-group-history))
12924            ((= 1 (length split-name))
12925             (gnus-completing-read (car split-name) prom group-map
12926                                   nil nil nil
12927                                   'gnus-group-history))
12928            (t
12929             (gnus-completing-read nil prom 
12930                                   (mapcar (lambda (el) (list el))
12931                                           (nreverse split-name))
12932                                   nil nil nil
12933                                   'gnus-group-history)))))
12934     (when to-newsgroup
12935       (if (or (string= to-newsgroup "")
12936               (string= to-newsgroup prefix))
12937           (setq to-newsgroup (or default "")))
12938       (or (gnus-active to-newsgroup)
12939           (gnus-activate-group to-newsgroup)
12940           (error "No such group: %s" to-newsgroup)))
12941     to-newsgroup))
12942
12943 (defun gnus-read-save-file-name (prompt default-name)
12944   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12945          (file
12946           ;; Let the split methods have their say.
12947           (cond
12948            ;; No split name was found.
12949            ((null split-name)
12950             (read-file-name
12951              (concat prompt " (default "
12952                      (file-name-nondirectory default-name) ") ")
12953              (file-name-directory default-name)
12954              default-name))
12955            ;; A single split name was found
12956            ((= 1 (length split-name))
12957             (read-file-name
12958              (concat prompt " (default " (car split-name) ") ")
12959              gnus-article-save-directory
12960              (concat gnus-article-save-directory (car split-name))))
12961            ;; A list of splits was found.
12962            (t
12963             (setq split-name (nreverse split-name))
12964             (let (result)
12965               (let ((file-name-history (nconc split-name file-name-history)))
12966                 (setq result
12967                       (read-file-name
12968                        (concat prompt " (`M-p' for defaults) ")
12969                        gnus-article-save-directory
12970                        (car split-name))))
12971               (car (push result file-name-history)))))))
12972     ;; If we have read a directory, we append the default file name.
12973     (when (file-directory-p file)
12974       (setq file (concat (file-name-as-directory file)
12975                          (file-name-nondirectory default-name))))
12976     ;; Possibly translate some charaters.
12977     (nnheader-translate-file-chars file)))
12978
12979 (defun gnus-article-archive-name (group)
12980   "Return the first instance of an \"Archive-name\" in the current buffer."
12981   (let ((case-fold-search t))
12982     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12983       (match-string 1))))
12984
12985 (defun gnus-summary-save-in-rmail (&optional filename)
12986   "Append this article to Rmail file.
12987 Optional argument FILENAME specifies file name.
12988 Directory to save to is default to `gnus-article-save-directory' which
12989 is initialized from the SAVEDIR environment variable."
12990   (interactive)
12991   (gnus-set-global-variables)
12992   (let ((default-name
12993           (funcall gnus-rmail-save-name gnus-newsgroup-name
12994                    gnus-current-headers gnus-newsgroup-last-rmail)))
12995     (setq filename
12996           (cond ((eq filename 'default)
12997                  default-name)
12998                 (filename filename)
12999                 (t (gnus-read-save-file-name
13000                     "Save in rmail file:" default-name))))
13001     (gnus-make-directory (file-name-directory filename))
13002     (gnus-eval-in-buffer-window
13003      gnus-original-article-buffer
13004      (save-excursion
13005        (save-restriction
13006          (widen)
13007          (gnus-output-to-rmail filename))))
13008     ;; Remember the directory name to save articles
13009     (setq gnus-newsgroup-last-rmail filename)))
13010
13011 (defun gnus-summary-save-in-mail (&optional filename)
13012   "Append this article to Unix mail file.
13013 Optional argument FILENAME specifies file name.
13014 Directory to save to is default to `gnus-article-save-directory' which
13015 is initialized from the SAVEDIR environment variable."
13016   (interactive)
13017   (gnus-set-global-variables)
13018   (let ((default-name
13019           (funcall gnus-mail-save-name gnus-newsgroup-name
13020                    gnus-current-headers gnus-newsgroup-last-mail)))
13021     (setq filename
13022           (cond ((eq filename 'default)
13023                  default-name)
13024                 (filename filename)
13025                 (t (gnus-read-save-file-name
13026                     "Save in Unix mail file:" default-name))))
13027     (setq filename
13028           (expand-file-name filename
13029                             (and default-name
13030                                  (file-name-directory default-name))))
13031     (gnus-make-directory (file-name-directory filename))
13032     (gnus-eval-in-buffer-window
13033      gnus-original-article-buffer
13034      (save-excursion
13035        (save-restriction
13036          (widen)
13037          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13038              (gnus-output-to-rmail filename)
13039            (let ((mail-use-rfc822 t))
13040              (rmail-output filename 1 t t))))))
13041     ;; Remember the directory name to save articles.
13042     (setq gnus-newsgroup-last-mail filename)))
13043
13044 (defun gnus-summary-save-in-file (&optional filename)
13045   "Append this article to file.
13046 Optional argument FILENAME specifies file name.
13047 Directory to save to is default to `gnus-article-save-directory' which
13048 is initialized from the SAVEDIR environment variable."
13049   (interactive)
13050   (gnus-set-global-variables)
13051   (let ((default-name
13052           (funcall gnus-file-save-name gnus-newsgroup-name
13053                    gnus-current-headers gnus-newsgroup-last-file)))
13054     (setq filename
13055           (cond ((eq filename 'default)
13056                  default-name)
13057                 (filename filename)
13058                 (t (gnus-read-save-file-name
13059                     "Save in file:" default-name))))
13060     (gnus-make-directory (file-name-directory filename))
13061     (gnus-eval-in-buffer-window
13062      gnus-original-article-buffer
13063      (save-excursion
13064        (save-restriction
13065          (widen)
13066          (gnus-output-to-file filename))))
13067     ;; Remember the directory name to save articles.
13068     (setq gnus-newsgroup-last-file filename)))
13069
13070 (defun gnus-summary-save-body-in-file (&optional filename)
13071   "Append this article body to a file.
13072 Optional argument FILENAME specifies file name.
13073 The directory to save in defaults to `gnus-article-save-directory' which
13074 is initialized from the SAVEDIR environment variable."
13075   (interactive)
13076   (gnus-set-global-variables)
13077   (let ((default-name
13078           (funcall gnus-file-save-name gnus-newsgroup-name
13079                    gnus-current-headers gnus-newsgroup-last-file)))
13080     (setq filename
13081           (cond ((eq filename 'default)
13082                  default-name)
13083                 (filename filename)
13084                 (t (gnus-read-save-file-name
13085                     "Save body in file:" default-name))))
13086     (gnus-make-directory (file-name-directory filename))
13087     (gnus-eval-in-buffer-window
13088      gnus-article-buffer
13089      (save-excursion
13090        (save-restriction
13091          (widen)
13092          (goto-char (point-min))
13093          (and (search-forward "\n\n" nil t)
13094               (narrow-to-region (point) (point-max)))
13095          (gnus-output-to-file filename))))
13096     ;; Remember the directory name to save articles.
13097     (setq gnus-newsgroup-last-file filename)))
13098
13099 (defun gnus-summary-save-in-pipe (&optional command)
13100   "Pipe this article to subprocess."
13101   (interactive)
13102   (gnus-set-global-variables)
13103   (setq command
13104         (cond ((eq command 'default)
13105                gnus-last-shell-command)
13106               (command command)
13107               (t (read-string "Shell command on article: "
13108                               gnus-last-shell-command))))
13109   (if (string-equal command "")
13110       (setq command gnus-last-shell-command))
13111   (gnus-eval-in-buffer-window
13112    gnus-article-buffer
13113    (save-restriction
13114      (widen)
13115      (shell-command-on-region (point-min) (point-max) command nil)))
13116   (setq gnus-last-shell-command command))
13117
13118 ;; Summary extract commands
13119
13120 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13121   (let ((buffer-read-only nil)
13122         (article (gnus-summary-article-number))
13123         after-article b e)
13124     (or (gnus-summary-goto-subject article)
13125         (error (format "No such article: %d" article)))
13126     (gnus-summary-position-point)
13127     ;; If all commands are to be bunched up on one line, we collect
13128     ;; them here.
13129     (if gnus-view-pseudos-separately
13130         ()
13131       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13132             files action)
13133         (while ps
13134           (setq action (cdr (assq 'action (car ps))))
13135           (setq files (list (cdr (assq 'name (car ps)))))
13136           (while (and ps (cdr ps)
13137                       (string= (or action "1")
13138                                (or (cdr (assq 'action (cadr ps))) "2")))
13139             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13140             (setcdr ps (cddr ps)))
13141           (if (not files)
13142               ()
13143             (if (not (string-match "%s" action))
13144                 (setq files (cons " " files)))
13145             (setq files (cons " " files))
13146             (and (assq 'execute (car ps))
13147                  (setcdr (assq 'execute (car ps))
13148                          (funcall (if (string-match "%s" action)
13149                                       'format 'concat)
13150                                   action
13151                                   (mapconcat (lambda (f) f) files " ")))))
13152           (setq ps (cdr ps)))))
13153     (if (and gnus-view-pseudos (not not-view))
13154         (while pslist
13155           (and (assq 'execute (car pslist))
13156                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13157                                      (eq gnus-view-pseudos 'not-confirm)))
13158           (setq pslist (cdr pslist)))
13159       (save-excursion
13160         (while pslist
13161           (setq after-article (or (cdr (assq 'article (car pslist)))
13162                                   (gnus-summary-article-number)))
13163           (gnus-summary-goto-subject after-article)
13164           (forward-line 1)
13165           (setq b (point))
13166           (insert "    " (file-name-nondirectory
13167                                 (cdr (assq 'name (car pslist))))
13168                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13169           (setq e (point))
13170           (forward-line -1)             ; back to `b'
13171           (add-text-properties
13172            b e (list 'gnus-number gnus-reffed-article-number
13173                      gnus-mouse-face-prop gnus-mouse-face))
13174           (gnus-data-enter
13175            after-article gnus-reffed-article-number
13176            gnus-unread-mark b (car pslist) 0 (- e b))
13177           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13178           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13179           (setq pslist (cdr pslist)))))))
13180
13181 (defun gnus-pseudos< (p1 p2)
13182   (let ((c1 (cdr (assq 'action p1)))
13183         (c2 (cdr (assq 'action p2))))
13184     (and c1 c2 (string< c1 c2))))
13185
13186 (defun gnus-request-pseudo-article (props)
13187   (cond ((assq 'execute props)
13188          (gnus-execute-command (cdr (assq 'execute props)))))
13189   (let ((gnus-current-article (gnus-summary-article-number)))
13190     (run-hooks 'gnus-mark-article-hook)))
13191
13192 (defun gnus-execute-command (command &optional automatic)
13193   (save-excursion
13194     (gnus-article-setup-buffer)
13195     (set-buffer gnus-article-buffer)
13196     (let ((command (if automatic command (read-string "Command: " command)))
13197           (buffer-read-only nil))
13198       (erase-buffer)
13199       (insert "$ " command "\n\n")
13200       (if gnus-view-pseudo-asynchronously
13201           (start-process "gnus-execute" nil "sh" "-c" command)
13202         (call-process "sh" nil t nil "-c" command)))))
13203
13204 (defun gnus-copy-file (file &optional to)
13205   "Copy FILE to TO."
13206   (interactive
13207    (list (read-file-name "Copy file: " default-directory)
13208          (read-file-name "Copy file to: " default-directory)))
13209   (gnus-set-global-variables)
13210   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13211   (and (file-directory-p to)
13212        (setq to (concat (file-name-as-directory to)
13213                         (file-name-nondirectory file))))
13214   (copy-file file to))
13215
13216 ;; Summary kill commands.
13217
13218 (defun gnus-summary-edit-global-kill (article)
13219   "Edit the \"global\" kill file."
13220   (interactive (list (gnus-summary-article-number)))
13221   (gnus-set-global-variables)
13222   (gnus-group-edit-global-kill article))
13223
13224 (defun gnus-summary-edit-local-kill ()
13225   "Edit a local kill file applied to the current newsgroup."
13226   (interactive)
13227   (gnus-set-global-variables)
13228   (setq gnus-current-headers (gnus-summary-article-header))
13229   (gnus-set-global-variables)
13230   (gnus-group-edit-local-kill
13231    (gnus-summary-article-number) gnus-newsgroup-name))
13232
13233 \f
13234 ;;;
13235 ;;; Gnus article mode
13236 ;;;
13237
13238 (put 'gnus-article-mode 'mode-class 'special)
13239
13240 (if gnus-article-mode-map
13241     nil
13242   (setq gnus-article-mode-map (make-keymap))
13243   (suppress-keymap gnus-article-mode-map)
13244
13245   (gnus-define-keys gnus-article-mode-map
13246     " " gnus-article-goto-next-page
13247     "\177" gnus-article-goto-prev-page
13248     [delete] gnus-article-goto-prev-page
13249     "\C-c^" gnus-article-refer-article
13250     "h" gnus-article-show-summary
13251     "s" gnus-article-show-summary
13252     "\C-c\C-m" gnus-article-mail
13253     "?" gnus-article-describe-briefly
13254     gnus-mouse-2 gnus-article-push-button
13255     "\r" gnus-article-press-button
13256     "\t" gnus-article-next-button
13257     "\M-\t" gnus-article-prev-button
13258     "\C-c\C-b" gnus-bug)
13259
13260   (substitute-key-definition
13261    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13262
13263 (defun gnus-article-mode ()
13264   "Major mode for displaying an article.
13265
13266 All normal editing commands are switched off.
13267
13268 The following commands are available:
13269
13270 \\<gnus-article-mode-map>
13271 \\[gnus-article-next-page]\t Scroll the article one page forwards
13272 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13273 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13274 \\[gnus-article-show-summary]\t Display the summary buffer
13275 \\[gnus-article-mail]\t Send a reply to the address near point
13276 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13277 \\[gnus-info-find-node]\t Go to the Gnus info node"
13278   (interactive)
13279   (when (and menu-bar-mode
13280              (gnus-visual-p 'article-menu 'menu))
13281     (gnus-article-make-menu-bar))
13282   (kill-all-local-variables)
13283   (gnus-simplify-mode-line)
13284   (setq mode-name "Article")
13285   (setq major-mode 'gnus-article-mode)
13286   (make-local-variable 'minor-mode-alist)
13287   (or (assq 'gnus-show-mime minor-mode-alist)
13288       (setq minor-mode-alist
13289             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13290   (use-local-map gnus-article-mode-map)
13291   (make-local-variable 'page-delimiter)
13292   (setq page-delimiter gnus-page-delimiter)
13293   (buffer-disable-undo (current-buffer))
13294   (setq buffer-read-only t)             ;Disable modification
13295   (run-hooks 'gnus-article-mode-hook))
13296
13297 (defun gnus-article-setup-buffer ()
13298   "Initialize the article buffer."
13299   (let* ((name (if gnus-single-article-buffer "*Article*"
13300                  (concat "*Article " gnus-newsgroup-name "*")))
13301          (original
13302           (progn (string-match "\\*Article" name)
13303                  (concat " *Original Article"
13304                          (substring name (match-end 0))))))
13305     (setq gnus-article-buffer name)
13306     (setq gnus-original-article-buffer original)
13307     ;; This might be a variable local to the summary buffer.
13308     (unless gnus-single-article-buffer
13309       (save-excursion
13310         (set-buffer gnus-summary-buffer)
13311         (setq gnus-article-buffer name)
13312         (setq gnus-original-article-buffer original)
13313         (gnus-set-global-variables))
13314       (make-local-variable 'gnus-summary-buffer))
13315     ;; Init original article buffer.
13316     (save-excursion
13317       (set-buffer (get-buffer-create gnus-original-article-buffer))
13318       (buffer-disable-undo (current-buffer))
13319       (setq major-mode 'gnus-original-article-mode)
13320       (make-local-variable 'gnus-original-article))
13321     (if (get-buffer name)
13322         (save-excursion
13323           (set-buffer name)
13324           (buffer-disable-undo (current-buffer))
13325           (setq buffer-read-only t)
13326           (gnus-add-current-to-buffer-list)
13327           (or (eq major-mode 'gnus-article-mode)
13328               (gnus-article-mode))
13329           (current-buffer))
13330       (save-excursion
13331         (set-buffer (get-buffer-create name))
13332         (gnus-add-current-to-buffer-list)
13333         (gnus-article-mode)
13334         (current-buffer)))))
13335
13336 ;; Set article window start at LINE, where LINE is the number of lines
13337 ;; from the head of the article.
13338 (defun gnus-article-set-window-start (&optional line)
13339   (set-window-start
13340    (get-buffer-window gnus-article-buffer)
13341    (save-excursion
13342      (set-buffer gnus-article-buffer)
13343      (goto-char (point-min))
13344      (if (not line)
13345          (point-min)
13346        (gnus-message 6 "Moved to bookmark")
13347        (search-forward "\n\n" nil t)
13348        (forward-line line)
13349        (point)))))
13350
13351 (defun gnus-kill-all-overlays ()
13352   "Delete all overlays in the current buffer."
13353   (when (fboundp 'overlay-lists)
13354     (let* ((overlayss (overlay-lists))
13355            (buffer-read-only nil)
13356            (overlays (nconc (car overlayss) (cdr overlayss))))
13357       (while overlays
13358         (delete-overlay (pop overlays))))))
13359
13360 (defun gnus-request-article-this-buffer (article group)
13361   "Get an article and insert it into this buffer."
13362   (let (do-update-line)
13363     (prog1
13364         (save-excursion
13365           (erase-buffer)
13366           (gnus-kill-all-overlays)
13367           (setq group (or group gnus-newsgroup-name))
13368
13369           ;; Open server if it has closed.
13370           (gnus-check-server (gnus-find-method-for-group group))
13371
13372           ;; Using `gnus-request-article' directly will insert the article into
13373           ;; `nntp-server-buffer' - so we'll save some time by not having to
13374           ;; copy it from the server buffer into the article buffer.
13375
13376           ;; We only request an article by message-id when we do not have the
13377           ;; headers for it, so we'll have to get those.
13378           (when (stringp article)
13379             (let ((gnus-override-method gnus-refer-article-method))
13380               (gnus-read-header article)))
13381
13382           ;; If the article number is negative, that means that this article
13383           ;; doesn't belong in this newsgroup (possibly), so we find its
13384           ;; message-id and request it by id instead of number.
13385           (when (and (numberp article)
13386                      gnus-summary-buffer
13387                      (get-buffer gnus-summary-buffer)
13388                      (buffer-name (get-buffer gnus-summary-buffer)))
13389             (save-excursion
13390               (set-buffer gnus-summary-buffer)
13391               (let ((header (gnus-summary-article-header article)))
13392                 (if (< article 0)
13393                     (cond 
13394                      ((memq article gnus-newsgroup-sparse)
13395                       ;; This is a sparse gap article.
13396                       (setq do-update-line article)
13397                       (setq article (mail-header-id header))
13398                       (let ((gnus-override-method gnus-refer-article-method))
13399                         (gnus-read-header article)))
13400                      ((vectorp header)
13401                       ;; It's a real article.
13402                       (setq article (mail-header-id header)))
13403                      (t
13404                       ;; It is an extracted pseudo-article.
13405                       (setq article 'pseudo)
13406                       (gnus-request-pseudo-article header))))
13407                 
13408                 (let ((method (gnus-find-method-for-group 
13409                                gnus-newsgroup-name)))
13410                   (if (not (eq (car method) 'nneething))
13411                       ()
13412                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13413                                        (mail-header-subject header))))
13414                       (if (file-directory-p dir)
13415                           (progn
13416                             (setq article 'nneething)
13417                             (gnus-group-enter-directory dir)))))))))
13418
13419           (cond
13420            ;; We first check `gnus-original-article-buffer'.
13421            ((and (get-buffer gnus-original-article-buffer)
13422                  (save-excursion
13423                    (set-buffer gnus-original-article-buffer)
13424                    (and (equal (car gnus-original-article) group)
13425                         (eq (cdr gnus-original-article) article))))
13426             (insert-buffer-substring gnus-original-article-buffer)
13427             'article)
13428            ;; Check the backlog.
13429            ((and gnus-keep-backlog
13430                  (gnus-backlog-request-article group article (current-buffer)))
13431             'article)
13432            ;; Check the cache.
13433            ((and gnus-use-cache
13434                  (numberp article)
13435                  (gnus-cache-request-article article group))
13436             'article)
13437            ;; Get the article and put into the article buffer.
13438            ((or (stringp article) (numberp article))
13439             (let ((gnus-override-method
13440                    (and (stringp article) gnus-refer-article-method))
13441                   (buffer-read-only nil))
13442               (erase-buffer)
13443               (gnus-kill-all-overlays)
13444               (if (gnus-request-article article group (current-buffer))
13445                   (progn
13446                     (and gnus-keep-backlog
13447                          (numberp article)
13448                          (gnus-backlog-enter-article
13449                           group article (current-buffer)))
13450                     'article))))
13451            ;; It was a pseudo.
13452            (t article)))
13453
13454       ;; Take the article from the original article buffer
13455       ;; and place it in the buffer it's supposed to be in.
13456       (when (and (get-buffer gnus-article-buffer)
13457                  (equal (buffer-name (current-buffer))
13458                         (buffer-name (get-buffer gnus-article-buffer))))
13459         (save-excursion
13460           (if (get-buffer gnus-original-article-buffer)
13461               (set-buffer (get-buffer gnus-original-article-buffer))
13462             (set-buffer (get-buffer-create gnus-original-article-buffer))
13463             (buffer-disable-undo (current-buffer))
13464             (setq major-mode 'gnus-original-article-mode)
13465             (setq buffer-read-only t)
13466             (gnus-add-current-to-buffer-list))
13467           (let (buffer-read-only)
13468             (erase-buffer)
13469             (insert-buffer-substring gnus-article-buffer))
13470           (setq gnus-original-article (cons group article))))
13471     
13472       ;; Update sparse articles.
13473       (when do-update-line
13474         (save-excursion
13475           (set-buffer gnus-summary-buffer)
13476           (gnus-summary-update-article do-update-line)
13477           (gnus-summary-goto-subject do-update-line)
13478           (set-window-point (get-buffer-window (current-buffer) t)
13479                             (point)))))))
13480
13481 (defun gnus-read-header (id &optional header)
13482   "Read the headers of article ID and enter them into the Gnus system."
13483   (let ((group gnus-newsgroup-name)
13484         (gnus-override-method 
13485          (and (gnus-news-group-p gnus-newsgroup-name)
13486               gnus-refer-article-method))       
13487         where)
13488     ;; First we check to see whether the header in question is already
13489     ;; fetched.
13490     (if (stringp id)
13491         ;; This is a Message-ID.
13492         (setq header (or header (gnus-id-to-header id)))
13493       ;; This is an article number.
13494       (setq header (or header (gnus-summary-article-header id))))
13495     (if (and header
13496              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13497         ;; We have found the header.
13498         header
13499       ;; We have to really fetch the header to this article.
13500       (when (setq where
13501                   (if (gnus-check-backend-function 'request-head group)
13502                       (gnus-request-head id group)
13503                     (gnus-request-article id group)))
13504         (save-excursion
13505           (set-buffer nntp-server-buffer)
13506           (and (search-forward "\n\n" nil t)
13507                (delete-region (1- (point)) (point-max)))
13508           (goto-char (point-max))
13509           (insert ".\n")
13510           (goto-char (point-min))
13511           (insert "211 ")
13512           (princ (cond
13513                   ((numberp id) id)
13514                   ((cdr where) (cdr where))
13515                   (header (mail-header-number header))
13516                   (t gnus-reffed-article-number))
13517                  (current-buffer))
13518           (insert " Article retrieved.\n"))
13519         ;(when (and header
13520         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13521         ;  (setcar (gnus-id-to-thread id) nil))
13522         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13523             ()                          ; Malformed head.
13524           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13525             (if (and (stringp id)
13526                      (not (string= (gnus-group-real-name group)
13527                                    (car where))))
13528                 ;; If we fetched by Message-ID and the article came
13529                 ;; from a different group, we fudge some bogus article
13530                 ;; numbers for this article.
13531                 (mail-header-set-number header gnus-reffed-article-number))
13532             (decf gnus-reffed-article-number)
13533             (push header gnus-newsgroup-headers)
13534             (setq gnus-current-headers header)
13535             (push (mail-header-number header) gnus-newsgroup-limit))
13536           header)))))
13537
13538 (defun gnus-article-prepare (article &optional all-headers header)
13539   "Prepare ARTICLE in article mode buffer.
13540 ARTICLE should either be an article number or a Message-ID.
13541 If ARTICLE is an id, HEADER should be the article headers.
13542 If ALL-HEADERS is non-nil, no headers are hidden."
13543   (save-excursion
13544     ;; Make sure we start in a summary buffer.
13545     (unless (eq major-mode 'gnus-summary-mode)
13546       (set-buffer gnus-summary-buffer))
13547     (setq gnus-summary-buffer (current-buffer))
13548     ;; Make sure the connection to the server is alive.
13549     (unless (gnus-server-opened
13550              (gnus-find-method-for-group gnus-newsgroup-name))
13551       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13552       (gnus-request-group gnus-newsgroup-name t))
13553     (let* ((article (if header (mail-header-number header) article))
13554            (summary-buffer (current-buffer))
13555            (internal-hook gnus-article-internal-prepare-hook)
13556            (group gnus-newsgroup-name)
13557            result)
13558       (save-excursion
13559         (gnus-article-setup-buffer)
13560         (set-buffer gnus-article-buffer)
13561         ;; Deactivate active regions.
13562         (when (and (boundp 'transient-mark-mode)
13563                    transient-mark-mode)
13564           (setq mark-active nil))
13565         (if (not (setq result (let ((buffer-read-only nil))
13566                                 (gnus-request-article-this-buffer
13567                                  article group))))
13568             ;; There is no such article.
13569             (save-excursion
13570               (when (and (numberp article)
13571                          (not (memq article gnus-newsgroup-sparse)))
13572                 (setq gnus-article-current
13573                       (cons gnus-newsgroup-name article))
13574                 (set-buffer gnus-summary-buffer)
13575                 (setq gnus-current-article article)
13576                 (gnus-summary-mark-article article gnus-canceled-mark))
13577               (unless (memq article gnus-newsgroup-sparse)
13578                 (gnus-error
13579                  1 "No such article (may have expired or been canceled)")))
13580           (if (or (eq result 'pseudo) (eq result 'nneething))
13581               (progn
13582                 (save-excursion
13583                   (set-buffer summary-buffer)
13584                   (setq gnus-last-article gnus-current-article
13585                         gnus-newsgroup-history (cons gnus-current-article
13586                                                      gnus-newsgroup-history)
13587                         gnus-current-article 0
13588                         gnus-current-headers nil
13589                         gnus-article-current nil)
13590                   (if (eq result 'nneething)
13591                       (gnus-configure-windows 'summary)
13592                     (gnus-configure-windows 'article))
13593                   (gnus-set-global-variables))
13594                 (gnus-set-mode-line 'article))
13595             ;; The result from the `request' was an actual article -
13596             ;; or at least some text that is now displayed in the
13597             ;; article buffer.
13598             (if (and (numberp article)
13599                      (not (eq article gnus-current-article)))
13600                 ;; Seems like a new article has been selected.
13601                 ;; `gnus-current-article' must be an article number.
13602                 (save-excursion
13603                   (set-buffer summary-buffer)
13604                   (setq gnus-last-article gnus-current-article
13605                         gnus-newsgroup-history (cons gnus-current-article
13606                                                      gnus-newsgroup-history)
13607                         gnus-current-article article
13608                         gnus-current-headers
13609                         (gnus-summary-article-header gnus-current-article)
13610                         gnus-article-current
13611                         (cons gnus-newsgroup-name gnus-current-article))
13612                   (unless (vectorp gnus-current-headers)
13613                     (setq gnus-current-headers nil))
13614                   (gnus-summary-show-thread)
13615                   (run-hooks 'gnus-mark-article-hook)
13616                   (gnus-set-mode-line 'summary)
13617                   (and (gnus-visual-p 'article-highlight 'highlight)
13618                        (run-hooks 'gnus-visual-mark-article-hook))
13619                   ;; Set the global newsgroup variables here.
13620                   ;; Suggested by Jim Sisolak
13621                   ;; <sisolak@trans4.neep.wisc.edu>.
13622                   (gnus-set-global-variables)
13623                   (setq gnus-have-all-headers
13624                         (or all-headers gnus-show-all-headers))
13625                   (and gnus-use-cache
13626                        (vectorp (gnus-summary-article-header article))
13627                        (gnus-cache-possibly-enter-article
13628                         group article
13629                         (gnus-summary-article-header article)
13630                         (memq article gnus-newsgroup-marked)
13631                         (memq article gnus-newsgroup-dormant)
13632                         (memq article gnus-newsgroup-unreads)))))
13633             ;; Hooks for getting information from the article.
13634             ;; This hook must be called before being narrowed.
13635             (let (buffer-read-only)
13636               (run-hooks 'internal-hook)
13637               (run-hooks 'gnus-article-prepare-hook)
13638               ;; Decode MIME message.
13639               (if gnus-show-mime
13640                   (if (or (not gnus-strict-mime)
13641                           (gnus-fetch-field "Mime-Version"))
13642                       (funcall gnus-show-mime-method)
13643                     (funcall gnus-decode-encoded-word-method)))
13644               ;; Perform the article display hooks.
13645               (run-hooks 'gnus-article-display-hook))
13646             ;; Do page break.
13647             (goto-char (point-min))
13648             (and gnus-break-pages (gnus-narrow-to-page))
13649             (gnus-set-mode-line 'article)
13650             (gnus-configure-windows 'article)
13651             (goto-char (point-min))
13652             t))))))
13653
13654 (defun gnus-article-show-all-headers ()
13655   "Show all article headers in article mode buffer."
13656   (save-excursion
13657     (gnus-article-setup-buffer)
13658     (set-buffer gnus-article-buffer)
13659     (let ((buffer-read-only nil))
13660       (gnus-unhide-text (point-min) (point-max)))))
13661
13662 (defun gnus-article-hide-headers-if-wanted ()
13663   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13664 Provided for backwards compatibility."
13665   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13666       gnus-inhibit-hiding
13667       (gnus-article-hide-headers)))
13668
13669 (defsubst gnus-article-header-rank ()
13670   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13671   (let ((list gnus-sorted-header-list)
13672         (i 0))
13673     (while list
13674       (when (looking-at (car list))
13675         (setq list nil))
13676       (setq list (cdr list))
13677       (incf i))
13678     i))
13679
13680 (defun gnus-article-hide-headers (&optional arg delete)
13681   "Toggle whether to hide unwanted headers and possibly sort them as well.
13682 If given a negative prefix, always show; if given a positive prefix,
13683 always hide."
13684   (interactive "P")
13685   (unless (gnus-article-check-hidden-text 'headers arg)
13686     ;; This function might be inhibited.
13687     (unless gnus-inhibit-hiding
13688       (save-excursion
13689         (set-buffer gnus-article-buffer)
13690         (save-restriction
13691           (let ((buffer-read-only nil)
13692                 (props (nconc (list 'gnus-type 'headers)
13693                               gnus-hidden-properties))
13694                 (max (1+ (length gnus-sorted-header-list)))
13695                 (ignored (when (not (stringp gnus-visible-headers))
13696                            (cond ((stringp gnus-ignored-headers)
13697                                   gnus-ignored-headers)
13698                                  ((listp gnus-ignored-headers)
13699                                   (mapconcat 'identity gnus-ignored-headers
13700                                              "\\|")))))
13701                 (visible
13702                  (cond ((stringp gnus-visible-headers)
13703                         gnus-visible-headers)
13704                        ((and gnus-visible-headers
13705                              (listp gnus-visible-headers))
13706                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13707                 want-list beg)
13708             ;; First we narrow to just the headers.
13709             (widen)
13710             (goto-char (point-min))
13711             ;; Hide any "From " lines at the beginning of (mail) articles.
13712             (while (looking-at "From ")
13713               (forward-line 1))
13714             (unless (bobp)
13715               (gnus-hide-text (point-min) (point) props))
13716             ;; Then treat the rest of the header lines.
13717             (narrow-to-region
13718              (point)
13719              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13720             ;; Then we use the two regular expressions
13721             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13722             ;; select which header lines is to remain visible in the
13723             ;; article buffer.
13724             (goto-char (point-min))
13725             (while (re-search-forward "^[^ \t]*:" nil t)
13726               (beginning-of-line)
13727               ;; We add the headers we want to keep to a list and delete
13728               ;; them from the buffer.
13729               (put-text-property 
13730                (point) (1+ (point)) 'message-rank
13731                (if (or (and visible (looking-at visible))
13732                        (and ignored
13733                             (not (looking-at ignored))))
13734                    (gnus-article-header-rank) 
13735                  (+ 2 max)))
13736               (forward-line 1))
13737             (message-sort-headers-1)
13738             (when (setq beg (text-property-any 
13739                              (point-min) (point-max) 'message-rank (+ 2 max)))
13740               ;; We make the unwanted headers invisible.
13741               (if delete
13742                   (delete-region beg (point-max))
13743                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13744                 (gnus-hide-text-type beg (point-max) 'headers)))))))))
13745
13746 (defun gnus-article-hide-boring-headers (&optional arg)
13747   "Toggle hiding of headers that aren't very interesting.
13748 If given a negative prefix, always show; if given a positive prefix,
13749 always hide."
13750   (interactive "P")
13751   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13752     (save-excursion
13753       (set-buffer gnus-article-buffer)
13754       (save-restriction
13755         (let ((buffer-read-only nil)
13756               (list gnus-boring-article-headers)
13757               (inhibit-point-motion-hooks t)
13758               elem)
13759           (nnheader-narrow-to-headers)
13760           (while list
13761             (setq elem (pop list))
13762             (goto-char (point-min))
13763             (cond
13764              ;; Hide empty headers.
13765              ((eq elem 'empty)
13766               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13767                 (forward-line -1)
13768                 (gnus-hide-text-type
13769                  (progn (beginning-of-line) (point))
13770                  (progn 
13771                    (end-of-line)
13772                    (if (re-search-forward "^[^ \t]" nil t)
13773                        (match-beginning 0)
13774                      (point-max)))
13775                  'boring-headers)))
13776              ;; Hide boring Newsgroups header.
13777              ((eq elem 'newsgroups)
13778               (when (equal (mail-fetch-field "newsgroups")
13779                            (gnus-group-real-name gnus-newsgroup-name))
13780                 (gnus-article-hide-header "newsgroups")))
13781              ((eq elem 'followup-to)
13782               (when (equal (mail-fetch-field "followup-to")
13783                            (mail-fetch-field "newsgroups"))
13784                 (gnus-article-hide-header "followup-to")))
13785              ((eq elem 'reply-to)
13786               (let ((from (mail-fetch-field "from"))
13787                     (reply-to (mail-fetch-field "reply-to")))
13788                 (when (and
13789                        from reply-to
13790                        (equal 
13791                         (nth 1 (funcall gnus-extract-address-components from))
13792                         (nth 1 (funcall gnus-extract-address-components
13793                                         reply-to))))
13794                   (gnus-article-hide-header "reply-to"))))
13795              ((eq elem 'date)
13796               (let ((date (mail-fetch-field "date")))
13797                 (when (and date
13798                            (< (gnus-days-between date (current-time-string))
13799                               4))
13800                   (gnus-article-hide-header "date")))))))))))
13801
13802 (defun gnus-article-hide-header (header)
13803   (save-excursion
13804     (goto-char (point-min))
13805     (when (re-search-forward (concat "^" header ":") nil t)
13806       (gnus-hide-text-type
13807        (progn (beginning-of-line) (point))
13808        (progn 
13809          (end-of-line)
13810          (if (re-search-forward "^[^ \t]" nil t)
13811              (match-beginning 0)
13812            (point-max)))
13813        'boring-headers))))
13814
13815 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13816 (defun gnus-article-treat-overstrike ()
13817   "Translate overstrikes into bold text."
13818   (interactive)
13819   (save-excursion
13820     (set-buffer gnus-article-buffer)
13821     (let ((buffer-read-only nil))
13822       (while (search-forward "\b" nil t)
13823         (let ((next (following-char))
13824               (previous (char-after (- (point) 2))))
13825           (cond ((eq next previous)
13826                  (put-text-property (- (point) 2) (point) 'invisible t)
13827                  (put-text-property (point) (1+ (point)) 'face 'bold))
13828                 ((eq next ?_)
13829                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13830                  (put-text-property
13831                   (- (point) 2) (1- (point)) 'face 'underline))
13832                 ((eq previous ?_)
13833                  (put-text-property (- (point) 2) (point) 'invisible t)
13834                  (put-text-property
13835                   (point) (1+ (point))  'face 'underline))))))))
13836
13837 (defun gnus-article-word-wrap ()
13838   "Format too long lines."
13839   (interactive)
13840   (save-excursion
13841     (set-buffer gnus-article-buffer)
13842     (let ((buffer-read-only nil))
13843       (widen)
13844       (goto-char (point-min))
13845       (search-forward "\n\n" nil t)
13846       (end-of-line 1)
13847       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13848             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13849             (adaptive-fill-mode t))
13850         (while (not (eobp))
13851           (and (>= (current-column) (min fill-column (window-width)))
13852                (/= (preceding-char) ?:)
13853                (fill-paragraph nil))
13854           (end-of-line 2))))))
13855
13856 (defun gnus-article-remove-cr ()
13857   "Remove carriage returns from an article."
13858   (interactive)
13859   (save-excursion
13860     (set-buffer gnus-article-buffer)
13861     (let ((buffer-read-only nil))
13862       (goto-char (point-min))
13863       (while (search-forward "\r" nil t)
13864         (replace-match "" t t)))))
13865
13866 (defun gnus-article-remove-trailing-blank-lines ()
13867   "Remove all trailing blank lines from the article."
13868   (interactive)
13869   (save-excursion
13870     (set-buffer gnus-article-buffer)
13871     (let ((buffer-read-only nil))
13872       (goto-char (point-max))
13873       (delete-region
13874        (point)
13875        (progn
13876          (while (looking-at "^[ \t]*$")
13877            (forward-line -1))
13878          (forward-line 1)
13879          (point))))))
13880
13881 (defun gnus-article-display-x-face (&optional force)
13882   "Look for an X-Face header and display it if present."
13883   (interactive (list 'force))
13884   (save-excursion
13885     (set-buffer gnus-article-buffer)
13886     ;; Delete the old process, if any.
13887     (when (process-status "gnus-x-face")
13888       (delete-process "gnus-x-face"))
13889     (let ((inhibit-point-motion-hooks t)
13890           (case-fold-search nil)
13891           from)
13892       (save-restriction
13893         (nnheader-narrow-to-headers)
13894         (setq from (mail-fetch-field "from"))
13895         (goto-char (point-min))
13896         (when (and gnus-article-x-face-command
13897                    (or force
13898                        ;; Check whether this face is censored.
13899                        (not gnus-article-x-face-too-ugly)
13900                        (and gnus-article-x-face-too-ugly from
13901                             (not (string-match gnus-article-x-face-too-ugly
13902                                                from))))
13903                    ;; Has to be present.
13904                    (re-search-forward "^X-Face: " nil t))
13905           ;; We now have the area of the buffer where the X-Face is stored.
13906           (let ((beg (point))
13907                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13908             ;; We display the face.
13909             (if (symbolp gnus-article-x-face-command)
13910                 ;; The command is a lisp function, so we call it.
13911                 (if (gnus-functionp gnus-article-x-face-command)
13912                     (funcall gnus-article-x-face-command beg end)
13913                   (error "%s is not a function" gnus-article-x-face-command))
13914               ;; The command is a string, so we interpret the command
13915               ;; as a, well, command, and fork it off.
13916               (let ((process-connection-type nil))
13917                 (process-kill-without-query
13918                  (start-process
13919                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13920                 (process-send-region "gnus-x-face" beg end)
13921                 (process-send-eof "gnus-x-face")))))))))
13922
13923 (defun gnus-headers-decode-quoted-printable ()
13924   "Hack to remove QP encoding from headers."
13925   (let ((case-fold-search t)
13926         (inhibit-point-motion-hooks t)
13927         string)
13928     (goto-char (point-min))
13929     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13930       (setq string (match-string 1))
13931       (narrow-to-region (match-beginning 0) (match-end 0))
13932       (delete-region (point-min) (point-max))
13933       (insert string)
13934       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13935       (subst-char-in-region (point-min) (point-max) ?_ ? )
13936       (widen)
13937       (goto-char (point-min)))))
13938
13939 (defun gnus-article-de-quoted-unreadable (&optional force)
13940   "Do a naive translation of a quoted-printable-encoded article.
13941 This is in no way, shape or form meant as a replacement for real MIME
13942 processing, but is simply a stop-gap measure until MIME support is
13943 written.
13944 If FORCE, decode the article whether it is marked as quoted-printable
13945 or not."
13946   (interactive (list 'force))
13947   (save-excursion
13948     (set-buffer gnus-article-buffer)
13949     (let ((case-fold-search t)
13950           (buffer-read-only nil)
13951           (type (gnus-fetch-field "content-transfer-encoding")))
13952       (gnus-headers-decode-quoted-printable)
13953       (when (or force
13954                 (and type (string-match "quoted-printable" (downcase type))))
13955         (goto-char (point-min))
13956         (search-forward "\n\n" nil 'move)
13957         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13958
13959 (defun gnus-mime-decode-quoted-printable (from to)
13960   "Decode Quoted-Printable in the region between FROM and TO."
13961   (interactive "r")
13962   (goto-char from)
13963   (while (search-forward "=" to t)
13964     (cond ((eq (following-char) ?\n)
13965            (delete-char -1)
13966            (delete-char 1))
13967           ((looking-at "[0-9A-F][0-9A-F]")
13968            (delete-char -1)
13969            (insert (hexl-hex-string-to-integer
13970                     (buffer-substring (point) (+ 2 (point)))))
13971            (delete-char 2))
13972           ((looking-at "=")
13973            (delete-char 1))
13974           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13975
13976 (defun gnus-article-hide-pgp (&optional arg)
13977   "Toggle hiding of any PGP headers and signatures in the current article.
13978 If given a negative prefix, always show; if given a positive prefix,
13979 always hide."
13980   (interactive "P")
13981   (unless (gnus-article-check-hidden-text 'pgp arg)
13982     (save-excursion
13983       (set-buffer gnus-article-buffer)
13984       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13985             buffer-read-only beg end)
13986         (widen)
13987         (goto-char (point-min))
13988         ;; Hide the "header".
13989         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13990              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13991         (setq beg (point))
13992         ;; Hide the actual signature.
13993         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13994              (setq end (1+ (match-beginning 0)))
13995              (gnus-hide-text
13996               end
13997               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13998                   (match-end 0)
13999                 ;; Perhaps we shouldn't hide to the end of the buffer
14000                 ;; if there is no end to the signature?
14001                 (point-max))
14002               props))
14003         ;; Hide "- " PGP quotation markers.
14004         (when (and beg end)
14005           (narrow-to-region beg end)
14006           (goto-char (point-min))
14007           (while (re-search-forward "^- " nil t)
14008             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14009           (widen))))))
14010
14011 (defun gnus-article-hide-signature (&optional arg)
14012   "Hide the signature in the current article.
14013 If given a negative prefix, always show; if given a positive prefix,
14014 always hide."
14015   (interactive "P")
14016   (unless (gnus-article-check-hidden-text 'signature arg)
14017     (save-excursion
14018       (set-buffer gnus-article-buffer)
14019       (save-restriction
14020         (let ((buffer-read-only nil))
14021           (when (gnus-narrow-to-signature)
14022             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14023
14024 (defun gnus-article-strip-leading-blank-lines ()
14025   "Remove all blank lines from the beginning of the article."
14026   (interactive)
14027   (save-excursion
14028     (set-buffer gnus-article-buffer)
14029     (let (buffer-read-only)
14030       (goto-char (point-min))
14031       (when (search-forward "\n\n" nil t)
14032         (while (looking-at "[ \t]$")
14033           (gnus-delete-line))))))
14034
14035 (defun gnus-narrow-to-signature ()
14036   "Narrow to the signature."
14037   (widen)
14038   (goto-char (point-max))
14039   (when (re-search-backward gnus-signature-separator nil t)
14040     (forward-line 1)
14041     (when (or (null gnus-signature-limit)
14042               (and (numberp gnus-signature-limit)
14043                    (< (- (point-max) (point)) gnus-signature-limit))
14044               (and (gnus-functionp gnus-signature-limit)
14045                    (funcall gnus-signature-limit))
14046               (and (stringp gnus-signature-limit)
14047                    (not (re-search-forward gnus-signature-limit nil t))))
14048       (narrow-to-region (point) (point-max))
14049       t)))
14050
14051 (defun gnus-article-check-hidden-text (type arg)
14052   "Return nil if hiding is necessary."
14053   (save-excursion
14054     (set-buffer gnus-article-buffer)
14055     (let ((hide (gnus-article-hidden-text-p type)))
14056       (cond ((or (and (null arg) (eq hide 'hidden))
14057                  (and arg (< 0 (prefix-numeric-value arg))))
14058              (gnus-article-show-hidden-text type))
14059             ((eq hide 'shown)
14060              (gnus-article-show-hidden-text type t))
14061             (t nil)))))
14062
14063 (defun gnus-article-hidden-text-p (type)
14064   "Say whether the current buffer contains hidden text of type TYPE."
14065   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14066     (when pos
14067       (if (get-text-property pos 'invisible)
14068           'hidden
14069         'shown))))
14070
14071 (defun gnus-article-hide (&optional arg force)
14072   "Hide all the gruft in the current article.
14073 This means that PGP stuff, signatures, cited text and (some)
14074 headers will be hidden.
14075 If given a prefix, show the hidden text instead."
14076   (interactive (list current-prefix-arg 'force))
14077   (gnus-article-hide-headers arg)
14078   (gnus-article-hide-pgp arg)
14079   (gnus-article-hide-citation-maybe arg force)
14080   (gnus-article-hide-signature arg))
14081
14082 (defun gnus-article-show-hidden-text (type &optional hide)
14083   "Show all hidden text of type TYPE.
14084 If HIDE, hide the text instead."
14085   (save-excursion
14086     (set-buffer gnus-article-buffer)
14087     (let ((buffer-read-only nil)
14088           (inhibit-point-motion-hooks t)
14089           (beg (point-min)))
14090       (while (gnus-goto-char (text-property-any
14091                               beg (point-max) 'gnus-type type))
14092         (setq beg (point))
14093         (forward-char)
14094         (if hide
14095             (gnus-hide-text beg (point) gnus-hidden-properties)
14096           (gnus-unhide-text beg (point)))
14097         (setq beg (point)))
14098       t)))
14099
14100 (defvar gnus-article-time-units
14101   `((year . ,(* 365.25 24 60 60))
14102     (week . ,(* 7 24 60 60))
14103     (day . ,(* 24 60 60))
14104     (hour . ,(* 60 60))
14105     (minute . 60)
14106     (second . 1))
14107   "Mapping from time units to seconds.")
14108
14109 (defun gnus-article-date-ut (&optional type highlight)
14110   "Convert DATE date to universal time in the current article.
14111 If TYPE is `local', convert to local time; if it is `lapsed', output
14112 how much time has lapsed since DATE."
14113   (interactive (list 'ut t))
14114   (let* ((header (or gnus-current-headers
14115                      (gnus-summary-article-header) ""))
14116          (date (and (vectorp header) (mail-header-date header)))
14117          (date-regexp "^Date: \\|^X-Sent: ")
14118          (now (current-time))
14119          (inhibit-point-motion-hooks t)
14120          bface eface)
14121     (when (and date (not (string= date "")))
14122       (save-excursion
14123         (set-buffer gnus-article-buffer)
14124         (save-restriction
14125           (nnheader-narrow-to-headers)
14126           (let ((buffer-read-only nil))
14127             ;; Delete any old Date headers.
14128             (if (re-search-forward date-regexp nil t)
14129                 (progn
14130                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14131                         eface (get-text-property (1- (gnus-point-at-eol))
14132                                                  'face))
14133                   (message-remove-header date-regexp t)
14134                   (beginning-of-line))
14135               (goto-char (point-max)))
14136             (insert
14137              (cond
14138               ;; Convert to the local timezone.  We have to slap a
14139               ;; `condition-case' round the calls to the timezone
14140               ;; functions since they aren't particularly resistant to
14141               ;; buggy dates.
14142               ((eq type 'local)
14143                (concat "Date: " (condition-case ()
14144                                     (timezone-make-date-arpa-standard date)
14145                                   (error date))
14146                        "\n"))
14147               ;; Convert to Universal Time.
14148               ((eq type 'ut)
14149                (concat "Date: "
14150                        (condition-case ()
14151                            (timezone-make-date-arpa-standard date nil "UT")
14152                          (error date))
14153                        "\n"))
14154               ;; Get the original date from the article.
14155               ((eq type 'original)
14156                (concat "Date: " date "\n"))
14157               ;; Do an X-Sent lapsed format.
14158               ((eq type 'lapsed)
14159                ;; If the date is seriously mangled, the timezone
14160                ;; functions are liable to bug out, so we condition-case
14161                ;; the entire thing.
14162                (let* ((real-time
14163                        (condition-case ()
14164                            (gnus-time-minus
14165                             (gnus-encode-date
14166                              (timezone-make-date-arpa-standard
14167                               (current-time-string now)
14168                               (current-time-zone now) "UT"))
14169                             (gnus-encode-date
14170                              (timezone-make-date-arpa-standard
14171                               date nil "UT")))
14172                          (error '(0 0))))
14173                       (real-sec (+ (* (float (car real-time)) 65536)
14174                                    (cadr real-time)))
14175                       (sec (abs real-sec))
14176                       num prev)
14177                  (if (zerop sec)
14178                      "X-Sent: Now\n"
14179                    (concat
14180                     "X-Sent: "
14181                     ;; This is a bit convoluted, but basically we go
14182                     ;; through the time units for years, weeks, etc,
14183                     ;; and divide things to see whether that results
14184                     ;; in positive answers.
14185                     (mapconcat
14186                      (lambda (unit)
14187                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14188                            ;; The (remaining) seconds are too few to
14189                            ;; be divided into this time unit.
14190                            ""
14191                          ;; It's big enough, so we output it.
14192                          (setq sec (- sec (* num (cdr unit))))
14193                          (prog1
14194                              (concat (if prev ", " "") (int-to-string
14195                                                         (floor num))
14196                                      " " (symbol-name (car unit))
14197                                      (if (> num 1) "s" ""))
14198                            (setq prev t))))
14199                      gnus-article-time-units "")
14200                     ;; If dates are odd, then it might appear like the
14201                     ;; article was sent in the future.
14202                     (if (> real-sec 0)
14203                         " ago\n"
14204                       " in the future\n")))))
14205               (t
14206                (error "Unknown conversion type: %s" type)))))
14207           ;; Do highlighting.
14208           (forward-line -1)
14209           (when (and (gnus-visual-p 'article-highlight 'highlight)
14210                      (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14211             (put-text-property (match-beginning 1) (match-end 1)
14212                                'face bface)
14213             (put-text-property (match-beginning 2) (match-end 2)
14214                                'face eface)))))))
14215
14216 (defun gnus-article-date-local (&optional highlight)
14217   "Convert the current article date to the local timezone."
14218   (interactive (list t))
14219   (gnus-article-date-ut 'local highlight))
14220
14221 (defun gnus-article-date-original (&optional highlight)
14222   "Convert the current article date to what it was originally.
14223 This is only useful if you have used some other date conversion
14224 function and want to see what the date was before converting."
14225   (interactive (list t))
14226   (gnus-article-date-ut 'original highlight))
14227
14228 (defun gnus-article-date-lapsed (&optional highlight)
14229   "Convert the current article date to time lapsed since it was sent."
14230   (interactive (list t))
14231   (gnus-article-date-ut 'lapsed highlight))
14232
14233 (defun gnus-article-maybe-highlight ()
14234   "Do some article highlighting if `gnus-visual' is non-nil."
14235   (if (gnus-visual-p 'article-highlight 'highlight)
14236       (gnus-article-highlight-some)))
14237
14238 ;; Article savers.
14239
14240 (defun gnus-output-to-rmail (file-name)
14241   "Append the current article to an Rmail file named FILE-NAME."
14242   (require 'rmail)
14243   ;; Most of these codes are borrowed from rmailout.el.
14244   (setq file-name (expand-file-name file-name))
14245   (setq rmail-default-rmail-file file-name)
14246   (let ((artbuf (current-buffer))
14247         (tmpbuf (get-buffer-create " *Gnus-output*")))
14248     (save-excursion
14249       (or (get-file-buffer file-name)
14250           (file-exists-p file-name)
14251           (if (gnus-yes-or-no-p
14252                (concat "\"" file-name "\" does not exist, create it? "))
14253               (let ((file-buffer (create-file-buffer file-name)))
14254                 (save-excursion
14255                   (set-buffer file-buffer)
14256                   (rmail-insert-rmail-file-header)
14257                   (let ((require-final-newline nil))
14258                     (write-region (point-min) (point-max) file-name t 1)))
14259                 (kill-buffer file-buffer))
14260             (error "Output file does not exist")))
14261       (set-buffer tmpbuf)
14262       (buffer-disable-undo (current-buffer))
14263       (erase-buffer)
14264       (insert-buffer-substring artbuf)
14265       (gnus-convert-article-to-rmail)
14266       ;; Decide whether to append to a file or to an Emacs buffer.
14267       (let ((outbuf (get-file-buffer file-name)))
14268         (if (not outbuf)
14269             (append-to-file (point-min) (point-max) file-name)
14270           ;; File has been visited, in buffer OUTBUF.
14271           (set-buffer outbuf)
14272           (let ((buffer-read-only nil)
14273                 (msg (and (boundp 'rmail-current-message)
14274                           (symbol-value 'rmail-current-message))))
14275             ;; If MSG is non-nil, buffer is in RMAIL mode.
14276             (if msg
14277                 (progn (widen)
14278                        (narrow-to-region (point-max) (point-max))))
14279             (insert-buffer-substring tmpbuf)
14280             (if msg
14281                 (progn
14282                   (goto-char (point-min))
14283                   (widen)
14284                   (search-backward "\^_")
14285                   (narrow-to-region (point) (point-max))
14286                   (goto-char (1+ (point-min)))
14287                   (rmail-count-new-messages t)
14288                   (rmail-show-message msg)))))))
14289     (kill-buffer tmpbuf)))
14290
14291 (defun gnus-output-to-file (file-name)
14292   "Append the current article to a file named FILE-NAME."
14293   (setq file-name (expand-file-name file-name))
14294   (let ((artbuf (current-buffer))
14295         (tmpbuf (get-buffer-create " *Gnus-output*")))
14296     (save-excursion
14297       (set-buffer tmpbuf)
14298       (buffer-disable-undo (current-buffer))
14299       (erase-buffer)
14300       (insert-buffer-substring artbuf)
14301       ;; Append newline at end of the buffer as separator, and then
14302       ;; save it to file.
14303       (goto-char (point-max))
14304       (insert "\n")
14305       (append-to-file (point-min) (point-max) file-name))
14306     (kill-buffer tmpbuf)))
14307
14308 (defun gnus-convert-article-to-rmail ()
14309   "Convert article in current buffer to Rmail message format."
14310   (let ((buffer-read-only nil))
14311     ;; Convert article directly into Babyl format.
14312     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14313     (goto-char (point-min))
14314     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14315     (while (search-forward "\n\^_" nil t) ;single char
14316       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14317     (goto-char (point-max))
14318     (insert "\^_")))
14319
14320 (defun gnus-narrow-to-page (&optional arg)
14321   "Narrow the article buffer to a page.
14322 If given a numerical ARG, move forward ARG pages."
14323   (interactive "P")
14324   (setq arg (if arg (prefix-numeric-value arg) 0))
14325   (save-excursion
14326     (set-buffer gnus-article-buffer)
14327     (goto-char (point-min))
14328     (widen)
14329     (when (gnus-visual-p 'page-marker)
14330       (let ((buffer-read-only nil))
14331         (gnus-remove-text-with-property 'gnus-prev)
14332         (gnus-remove-text-with-property 'gnus-next)))
14333     (when
14334         (cond ((< arg 0)
14335                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14336               ((> arg 0)
14337                (re-search-forward page-delimiter nil 'move arg)))
14338       (goto-char (match-end 0)))
14339     (narrow-to-region
14340      (point)
14341      (if (re-search-forward page-delimiter nil 'move)
14342          (match-beginning 0)
14343        (point)))
14344     (when (and (gnus-visual-p 'page-marker)
14345                (not (= (point-min) 1)))
14346       (save-excursion
14347         (goto-char (point-min))
14348         (gnus-insert-prev-page-button)))
14349     (when (and (gnus-visual-p 'page-marker)
14350                (not (= (1- (point-max)) (buffer-size))))
14351       (save-excursion
14352         (goto-char (point-max))
14353         (gnus-insert-next-page-button)))))
14354
14355 ;; Article mode commands
14356
14357 (defun gnus-article-goto-next-page ()
14358   "Show the next page of the article."
14359   (interactive)
14360   (when (gnus-article-next-page)
14361     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14362
14363 (defun gnus-article-goto-prev-page ()
14364   "Show the next page of the article."
14365   (interactive)
14366   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14367     (gnus-article-prev-page nil)))
14368
14369 (defun gnus-article-next-page (&optional lines)
14370   "Show the next page of the current article.
14371 If end of article, return non-nil.  Otherwise return nil.
14372 Argument LINES specifies lines to be scrolled up."
14373   (interactive "p")
14374   (move-to-window-line -1)
14375   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14376   (if (save-excursion
14377         (end-of-line)
14378         (and (pos-visible-in-window-p)  ;Not continuation line.
14379              (eobp)))
14380       ;; Nothing in this page.
14381       (if (or (not gnus-break-pages)
14382               (save-excursion
14383                 (save-restriction
14384                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14385           t                             ;Nothing more.
14386         (gnus-narrow-to-page 1)         ;Go to next page.
14387         nil)
14388     ;; More in this page.
14389     (condition-case ()
14390         (scroll-up lines)
14391       (end-of-buffer
14392        ;; Long lines may cause an end-of-buffer error.
14393        (goto-char (point-max))))
14394     (move-to-window-line 0)
14395     nil))
14396
14397 (defun gnus-article-prev-page (&optional lines)
14398   "Show previous page of current article.
14399 Argument LINES specifies lines to be scrolled down."
14400   (interactive "p")
14401   (move-to-window-line 0)
14402   (if (and gnus-break-pages
14403            (bobp)
14404            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14405       (progn
14406         (gnus-narrow-to-page -1)        ;Go to previous page.
14407         (goto-char (point-max))
14408         (recenter -1))
14409     (prog1
14410         (condition-case ()
14411             (scroll-down lines)
14412           (error nil))
14413       (move-to-window-line 0))))
14414
14415 (defun gnus-article-refer-article ()
14416   "Read article specified by message-id around point."
14417   (interactive)
14418   (let ((point (point)))
14419     (search-forward ">" nil t)          ;Move point to end of "<....>".
14420     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14421         (let ((message-id (match-string 1)))
14422           (goto-char point)
14423           (set-buffer gnus-summary-buffer)
14424           (gnus-summary-refer-article message-id))
14425       (goto-char (point))
14426       (error "No references around point"))))
14427
14428 (defun gnus-article-show-summary ()
14429   "Reconfigure windows to show summary buffer."
14430   (interactive)
14431   (gnus-configure-windows 'article)
14432   (gnus-summary-goto-subject gnus-current-article))
14433
14434 (defun gnus-article-describe-briefly ()
14435   "Describe article mode commands briefly."
14436   (interactive)
14437   (gnus-message 6
14438                 (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")))
14439
14440 (defun gnus-article-summary-command ()
14441   "Execute the last keystroke in the summary buffer."
14442   (interactive)
14443   (let ((obuf (current-buffer))
14444         (owin (current-window-configuration))
14445         func)
14446     (switch-to-buffer gnus-summary-buffer 'norecord)
14447     (setq func (lookup-key (current-local-map) (this-command-keys)))
14448     (call-interactively func)
14449     (set-buffer obuf)
14450     (set-window-configuration owin)
14451     (set-window-point (get-buffer-window (current-buffer)) (point))))
14452
14453 (defun gnus-article-summary-command-nosave ()
14454   "Execute the last keystroke in the summary buffer."
14455   (interactive)
14456   (let (func)
14457     (pop-to-buffer gnus-summary-buffer 'norecord)
14458     (setq func (lookup-key (current-local-map) (this-command-keys)))
14459     (call-interactively func)))
14460
14461 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14462   "Read a summary buffer key sequence and execute it from the article buffer."
14463   (interactive "P")
14464   (let ((nosaves
14465          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14466            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14467            "=" "^" "\M-^"))
14468         keys)
14469     (save-excursion
14470       (set-buffer gnus-summary-buffer)
14471       (push (or key last-command-event) unread-command-events)
14472       (setq keys (read-key-sequence nil)))
14473     (message "")
14474
14475     (if (member keys nosaves)
14476         (let (func)
14477           (pop-to-buffer gnus-summary-buffer 'norecord)
14478           (if (setq func (lookup-key (current-local-map) keys))
14479               (call-interactively func)
14480             (ding)))
14481       (let ((obuf (current-buffer))
14482             (owin (current-window-configuration))
14483             (opoint (point))
14484             func in-buffer)
14485         (if not-restore-window
14486             (pop-to-buffer gnus-summary-buffer 'norecord)
14487           (switch-to-buffer gnus-summary-buffer 'norecord))
14488         (setq in-buffer (current-buffer))
14489         (if (setq func (lookup-key (current-local-map) keys))
14490             (call-interactively func)
14491           (ding))
14492         (when (eq in-buffer (current-buffer))
14493           (set-buffer obuf)
14494           (unless not-restore-window
14495             (set-window-configuration owin))
14496           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14497
14498 \f
14499 ;;;
14500 ;;; Kill file handling.
14501 ;;;
14502
14503 ;;;###autoload
14504 (defalias 'gnus-batch-kill 'gnus-batch-score)
14505 ;;;###autoload
14506 (defun gnus-batch-score ()
14507   "Run batched scoring.
14508 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14509 Newsgroups is a list of strings in Bnews format.  If you want to score
14510 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14511 score the alt hierarchy, you'd say \"!alt.all\"."
14512   (interactive)
14513   (let* ((yes-and-no
14514           (gnus-newsrc-parse-options
14515            (apply (function concat)
14516                   (mapcar (lambda (g) (concat g " "))
14517                           command-line-args-left))))
14518          (gnus-expert-user t)
14519          (nnmail-spool-file nil)
14520          (gnus-use-dribble-file nil)
14521          (yes (car yes-and-no))
14522          (no (cdr yes-and-no))
14523          group newsrc entry
14524          ;; Disable verbose message.
14525          gnus-novice-user gnus-large-newsgroup)
14526     ;; Eat all arguments.
14527     (setq command-line-args-left nil)
14528     ;; Start Gnus.
14529     (gnus)
14530     ;; Apply kills to specified newsgroups in command line arguments.
14531     (setq newsrc (cdr gnus-newsrc-alist))
14532     (while newsrc
14533       (setq group (caar newsrc))
14534       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14535       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14536                (and (car entry)
14537                     (or (eq (car entry) t)
14538                         (not (zerop (car entry)))))
14539                (if yes (string-match yes group) t)
14540                (or (null no) (not (string-match no group))))
14541           (progn
14542             (gnus-summary-read-group group nil t nil t)
14543             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14544                  (gnus-summary-exit))))
14545       (setq newsrc (cdr newsrc)))
14546     ;; Exit Emacs.
14547     (switch-to-buffer gnus-group-buffer)
14548     (gnus-group-save-newsrc)))
14549
14550 (defun gnus-apply-kill-file ()
14551   "Apply a kill file to the current newsgroup.
14552 Returns the number of articles marked as read."
14553   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14554           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14555       (gnus-apply-kill-file-internal)
14556     0))
14557
14558 (defun gnus-kill-save-kill-buffer ()
14559   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14560     (when (get-file-buffer file)
14561       (save-excursion
14562         (set-buffer (get-file-buffer file))
14563         (and (buffer-modified-p) (save-buffer))
14564         (kill-buffer (current-buffer))))))
14565
14566 (defvar gnus-kill-file-name "KILL"
14567   "Suffix of the kill files.")
14568
14569 (defun gnus-newsgroup-kill-file (newsgroup)
14570   "Return the name of a kill file name for NEWSGROUP.
14571 If NEWSGROUP is nil, return the global kill file name instead."
14572   (cond 
14573    ;; The global KILL file is placed at top of the directory.
14574    ((or (null newsgroup)
14575         (string-equal newsgroup ""))
14576     (expand-file-name gnus-kill-file-name
14577                       (or gnus-kill-files-directory "~/News")))
14578    ;; Append ".KILL" to newsgroup name.
14579    ((gnus-use-long-file-name 'not-kill)
14580     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14581                               "." gnus-kill-file-name)
14582                       (or gnus-kill-files-directory "~/News")))
14583    ;; Place "KILL" under the hierarchical directory.
14584    (t
14585     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14586                               "/" gnus-kill-file-name)
14587                       (or gnus-kill-files-directory "~/News")))))
14588
14589 \f
14590 ;;;
14591 ;;; Dribble file
14592 ;;;
14593
14594 (defvar gnus-dribble-ignore nil)
14595 (defvar gnus-dribble-eval-file nil)
14596
14597 (defun gnus-dribble-file-name ()
14598   "Return the dribble file for the current .newsrc."
14599   (concat
14600    (if gnus-dribble-directory
14601        (concat (file-name-as-directory gnus-dribble-directory)
14602                (file-name-nondirectory gnus-current-startup-file))
14603      gnus-current-startup-file)
14604    "-dribble"))
14605
14606 (defun gnus-dribble-enter (string)
14607   "Enter STRING into the dribble buffer."
14608   (if (and (not gnus-dribble-ignore)
14609            gnus-dribble-buffer
14610            (buffer-name gnus-dribble-buffer))
14611       (let ((obuf (current-buffer)))
14612         (set-buffer gnus-dribble-buffer)
14613         (insert string "\n")
14614         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14615         (set-buffer obuf))))
14616
14617 (defun gnus-dribble-read-file ()
14618   "Read the dribble file from disk."
14619   (let ((dribble-file (gnus-dribble-file-name)))
14620     (save-excursion
14621       (set-buffer (setq gnus-dribble-buffer
14622                         (get-buffer-create
14623                          (file-name-nondirectory dribble-file))))
14624       (gnus-add-current-to-buffer-list)
14625       (erase-buffer)
14626       (setq buffer-file-name dribble-file)
14627       (auto-save-mode t)
14628       (buffer-disable-undo (current-buffer))
14629       (bury-buffer (current-buffer))
14630       (set-buffer-modified-p nil)
14631       (let ((auto (make-auto-save-file-name))
14632             (gnus-dribble-ignore t))
14633         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14634           ;; Load whichever file is newest -- the auto save file
14635           ;; or the "real" file.
14636           (if (file-newer-than-file-p auto dribble-file)
14637               (insert-file-contents auto)
14638             (insert-file-contents dribble-file))
14639           (unless (zerop (buffer-size))
14640             (set-buffer-modified-p t))
14641           ;; Set the file modes to reflect the .newsrc file modes.
14642           (save-buffer)
14643           (when (file-exists-p gnus-current-startup-file)
14644             (set-file-modes dribble-file
14645                             (file-modes gnus-current-startup-file)))
14646           ;; Possibly eval the file later.
14647           (when (gnus-y-or-n-p
14648                  "Auto-save file exists.  Do you want to read it? ")
14649             (setq gnus-dribble-eval-file t)))))))
14650
14651 (defun gnus-dribble-eval-file ()
14652   (if (not gnus-dribble-eval-file)
14653       ()
14654     (setq gnus-dribble-eval-file nil)
14655     (save-excursion
14656       (let ((gnus-dribble-ignore t))
14657         (set-buffer gnus-dribble-buffer)
14658         (eval-buffer (current-buffer))))))
14659
14660 (defun gnus-dribble-delete-file ()
14661   (if (file-exists-p (gnus-dribble-file-name))
14662       (delete-file (gnus-dribble-file-name)))
14663   (if gnus-dribble-buffer
14664       (save-excursion
14665         (set-buffer gnus-dribble-buffer)
14666         (let ((auto (make-auto-save-file-name)))
14667           (if (file-exists-p auto)
14668               (delete-file auto))
14669           (erase-buffer)
14670           (set-buffer-modified-p nil)))))
14671
14672 (defun gnus-dribble-save ()
14673   (if (and gnus-dribble-buffer
14674            (buffer-name gnus-dribble-buffer))
14675       (save-excursion
14676         (set-buffer gnus-dribble-buffer)
14677         (save-buffer))))
14678
14679 (defun gnus-dribble-clear ()
14680   (save-excursion
14681     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14682         (progn
14683           (set-buffer gnus-dribble-buffer)
14684           (erase-buffer)
14685           (set-buffer-modified-p nil)
14686           (setq buffer-saved-size (buffer-size))))))
14687
14688 \f
14689 ;;;
14690 ;;; Server Communication
14691 ;;;
14692
14693 (defun gnus-start-news-server (&optional confirm)
14694   "Open a method for getting news.
14695 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14696   (let (how)
14697     (if gnus-current-select-method
14698         ;; Stream is already opened.
14699         nil
14700       ;; Open NNTP server.
14701       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14702       (if confirm
14703           (progn
14704             ;; Read server name with completion.
14705             (setq gnus-nntp-server
14706                   (completing-read "NNTP server: "
14707                                    (mapcar (lambda (server) (list server))
14708                                            (cons (list gnus-nntp-server)
14709                                                  gnus-secondary-servers))
14710                                    nil nil gnus-nntp-server))))
14711
14712       (if (and gnus-nntp-server
14713                (stringp gnus-nntp-server)
14714                (not (string= gnus-nntp-server "")))
14715           (setq gnus-select-method
14716                 (cond ((or (string= gnus-nntp-server "")
14717                            (string= gnus-nntp-server "::"))
14718                        (list 'nnspool (system-name)))
14719                       ((string-match "^:" gnus-nntp-server)
14720                        (list 'nnmh gnus-nntp-server
14721                              (list 'nnmh-directory
14722                                    (file-name-as-directory
14723                                     (expand-file-name
14724                                      (concat "~/" (substring
14725                                                    gnus-nntp-server 1)))))
14726                              (list 'nnmh-get-new-mail nil)))
14727                       (t
14728                        (list 'nntp gnus-nntp-server)))))
14729
14730       (setq how (car gnus-select-method))
14731       (cond ((eq how 'nnspool)
14732              (require 'nnspool)
14733              (gnus-message 5 "Looking up local news spool..."))
14734             ((eq how 'nnmh)
14735              (require 'nnmh)
14736              (gnus-message 5 "Looking up mh spool..."))
14737             (t
14738              (require 'nntp)))
14739       (setq gnus-current-select-method gnus-select-method)
14740       (run-hooks 'gnus-open-server-hook)
14741       (or
14742        ;; gnus-open-server-hook might have opened it
14743        (gnus-server-opened gnus-select-method)
14744        (gnus-open-server gnus-select-method)
14745        (gnus-y-or-n-p
14746         (format
14747          "%s (%s) open error: '%s'.     Continue? "
14748          (car gnus-select-method) (cadr gnus-select-method)
14749          (gnus-status-message gnus-select-method)))
14750        (gnus-error 1 "Couldn't open server on %s"
14751                    (nth 1 gnus-select-method))))))
14752
14753 (defun gnus-check-group (group)
14754   "Try to make sure that the server where GROUP exists is alive."
14755   (let ((method (gnus-find-method-for-group group)))
14756     (or (gnus-server-opened method)
14757         (gnus-open-server method))))
14758
14759 (defun gnus-check-server (&optional method silent)
14760   "Check whether the connection to METHOD is down.
14761 If METHOD is nil, use `gnus-select-method'.
14762 If it is down, start it up (again)."
14763   (let ((method (or method gnus-select-method)))
14764     ;; Transform virtual server names into select methods.
14765     (when (stringp method)
14766       (setq method (gnus-server-to-method method)))
14767     (if (gnus-server-opened method)
14768         ;; The stream is already opened.
14769         t
14770       ;; Open the server.
14771       (unless silent
14772         (gnus-message 5 "Opening %s server%s..." (car method)
14773                       (if (equal (nth 1 method) "") ""
14774                         (format " on %s" (nth 1 method)))))
14775       (run-hooks 'gnus-open-server-hook)
14776       (prog1
14777           (gnus-open-server method)
14778         (unless silent
14779           (message ""))))))
14780
14781 (defun gnus-get-function (method function)
14782   "Return a function symbol based on METHOD and FUNCTION."
14783   ;; Translate server names into methods.
14784   (unless method
14785     (error "Attempted use of a nil select method"))
14786   (when (stringp method)
14787     (setq method (gnus-server-to-method method)))
14788   (let ((func (intern (format "%s-%s" (car method) function))))
14789     ;; If the functions isn't bound, we require the backend in
14790     ;; question.
14791     (unless (fboundp func)
14792       (require (car method))
14793       (unless (fboundp func)
14794         ;; This backend doesn't implement this function.
14795         (error "No such function: %s" func)))
14796     func))
14797
14798 \f
14799 ;;;
14800 ;;; Interface functions to the backends.
14801 ;;;
14802
14803 (defun gnus-open-server (method)
14804   "Open a connection to METHOD."
14805   (when (stringp method)
14806     (setq method (gnus-server-to-method method)))
14807   (let ((elem (assoc method gnus-opened-servers)))
14808     ;; If this method was previously denied, we just return nil.
14809     (if (eq (nth 1 elem) 'denied)
14810         (progn
14811           (gnus-message 1 "Denied server")
14812           nil)
14813       ;; Open the server.
14814       (let ((result
14815              (funcall (gnus-get-function method 'open-server)
14816                       (nth 1 method) (nthcdr 2 method))))
14817         ;; If this hasn't been opened before, we add it to the list.
14818         (unless elem
14819           (setq elem (list method nil)
14820                 gnus-opened-servers (cons elem gnus-opened-servers)))
14821         ;; Set the status of this server.
14822         (setcar (cdr elem) (if result 'ok 'denied))
14823         ;; Return the result from the "open" call.
14824         result))))
14825
14826 (defun gnus-close-server (method)
14827   "Close the connection to METHOD."
14828   (when (stringp method)
14829     (setq method (gnus-server-to-method method)))
14830   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14831
14832 (defun gnus-request-list (method)
14833   "Request the active file from METHOD."
14834   (when (stringp method)
14835     (setq method (gnus-server-to-method method)))
14836   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14837
14838 (defun gnus-request-list-newsgroups (method)
14839   "Request the newsgroups file from METHOD."
14840   (when (stringp method)
14841     (setq method (gnus-server-to-method method)))
14842   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14843
14844 (defun gnus-request-newgroups (date method)
14845   "Request all new groups since DATE from METHOD."
14846   (when (stringp method)
14847     (setq method (gnus-server-to-method method)))
14848   (funcall (gnus-get-function method 'request-newgroups)
14849            date (nth 1 method)))
14850
14851 (defun gnus-server-opened (method)
14852   "Check whether a connection to METHOD has been opened."
14853   (when (stringp method)
14854     (setq method (gnus-server-to-method method)))
14855   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14856
14857 (defun gnus-status-message (method)
14858   "Return the status message from METHOD.
14859 If METHOD is a string, it is interpreted as a group name.   The method
14860 this group uses will be queried."
14861   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14862                   method)))
14863     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14864
14865 (defun gnus-request-group (group &optional dont-check method)
14866   "Request GROUP.  If DONT-CHECK, no information is required."
14867   (let ((method (or method (gnus-find-method-for-group group))))
14868     (when (stringp method)
14869       (setq method (gnus-server-to-method method)))
14870     (funcall (gnus-get-function method 'request-group)
14871              (gnus-group-real-name group) (nth 1 method) dont-check)))
14872
14873 (defun gnus-request-asynchronous (group &optional articles)
14874   "Request that GROUP behave asynchronously.
14875 ARTICLES is the `data' of the group."
14876   (let ((method (gnus-find-method-for-group group)))
14877     (funcall (gnus-get-function method 'request-asynchronous)
14878              (gnus-group-real-name group) (nth 1 method) articles)))
14879
14880 (defun gnus-list-active-group (group)
14881   "Request active information on GROUP."
14882   (let ((method (gnus-find-method-for-group group))
14883         (func 'list-active-group))
14884     (when (gnus-check-backend-function func group)
14885       (funcall (gnus-get-function method func)
14886                (gnus-group-real-name group) (nth 1 method)))))
14887
14888 (defun gnus-request-group-description (group)
14889   "Request a description of GROUP."
14890   (let ((method (gnus-find-method-for-group group))
14891         (func 'request-group-description))
14892     (when (gnus-check-backend-function func group)
14893       (funcall (gnus-get-function method func)
14894                (gnus-group-real-name group) (nth 1 method)))))
14895
14896 (defun gnus-close-group (group)
14897   "Request the GROUP be closed."
14898   (let ((method (gnus-find-method-for-group group)))
14899     (funcall (gnus-get-function method 'close-group)
14900              (gnus-group-real-name group) (nth 1 method))))
14901
14902 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14903   "Request headers for ARTICLES in GROUP.
14904 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14905   (let ((method (gnus-find-method-for-group group)))
14906     (if (and gnus-use-cache (numberp (car articles)))
14907         (gnus-cache-retrieve-headers articles group fetch-old)
14908       (funcall (gnus-get-function method 'retrieve-headers)
14909                articles (gnus-group-real-name group) (nth 1 method)
14910                fetch-old))))
14911
14912 (defun gnus-retrieve-groups (groups method)
14913   "Request active information on GROUPS from METHOD."
14914   (when (stringp method)
14915     (setq method (gnus-server-to-method method)))
14916   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14917
14918 (defun gnus-request-type (group &optional article)
14919   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14920   (let ((method (gnus-find-method-for-group group)))
14921     (if (not (gnus-check-backend-function 'request-type (car method)))
14922         'unknown
14923       (funcall (gnus-get-function method 'request-type)
14924                (gnus-group-real-name group) article))))
14925
14926 (defun gnus-request-update-mark (group article mark)
14927   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14928   (let ((method (gnus-find-method-for-group group)))
14929     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14930         mark
14931       (funcall (gnus-get-function method 'request-update-mark)
14932                (gnus-group-real-name group) article mark))))
14933
14934 (defun gnus-request-article (article group &optional buffer)
14935   "Request the ARTICLE in GROUP.
14936 ARTICLE can either be an article number or an article Message-ID.
14937 If BUFFER, insert the article in that group."
14938   (let ((method (gnus-find-method-for-group group)))
14939     (funcall (gnus-get-function method 'request-article)
14940              article (gnus-group-real-name group) (nth 1 method) buffer)))
14941
14942 (defun gnus-request-head (article group)
14943   "Request the head of ARTICLE in GROUP."
14944   (let ((method (gnus-find-method-for-group group)))
14945     (funcall (gnus-get-function method 'request-head)
14946              article (gnus-group-real-name group) (nth 1 method))))
14947
14948 (defun gnus-request-body (article group)
14949   "Request the body of ARTICLE in GROUP."
14950   (let ((method (gnus-find-method-for-group group)))
14951     (funcall (gnus-get-function method 'request-body)
14952              article (gnus-group-real-name group) (nth 1 method))))
14953
14954 (defun gnus-request-post (method)
14955   "Post the current buffer using METHOD."
14956   (when (stringp method)
14957     (setq method (gnus-server-to-method method)))
14958   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14959
14960 (defun gnus-request-scan (group method)
14961   "Request a SCAN being performed in GROUP from METHOD.
14962 If GROUP is nil, all groups on METHOD are scanned."
14963   (let ((method (if group (gnus-find-method-for-group group) method)))
14964     (funcall (gnus-get-function method 'request-scan)
14965              (and group (gnus-group-real-name group)) (nth 1 method))))
14966
14967 (defsubst gnus-request-update-info (info method)
14968   "Request that METHOD update INFO."
14969   (when (stringp method)
14970     (setq method (gnus-server-to-method method)))
14971   (when (gnus-check-backend-function 'request-update-info (car method))
14972     (funcall (gnus-get-function method 'request-update-info)
14973              (gnus-group-real-name (gnus-info-group info))
14974              info (nth 1 method))))
14975
14976 (defun gnus-request-expire-articles (articles group &optional force)
14977   (let ((method (gnus-find-method-for-group group)))
14978     (funcall (gnus-get-function method 'request-expire-articles)
14979              articles (gnus-group-real-name group) (nth 1 method)
14980              force)))
14981
14982 (defun gnus-request-move-article
14983   (article group server accept-function &optional last)
14984   (let ((method (gnus-find-method-for-group group)))
14985     (funcall (gnus-get-function method 'request-move-article)
14986              article (gnus-group-real-name group)
14987              (nth 1 method) accept-function last)))
14988
14989 (defun gnus-request-accept-article (group method &optional last)
14990   ;; Make sure there's a newline at the end of the article.
14991   (when (stringp method)
14992     (setq method (gnus-server-to-method method)))
14993   (when (and (not method)
14994              (stringp group))
14995     (setq method (gnus-find-method-for-group group)))
14996   (goto-char (point-max))
14997   (unless (bolp)
14998     (insert "\n"))
14999   (let ((func (car (or method (gnus-find-method-for-group group)))))
15000     (funcall (intern (format "%s-request-accept-article" func))
15001              (if (stringp group) (gnus-group-real-name group) group)
15002              (cadr method)
15003              last)))
15004
15005 (defun gnus-request-replace-article (article group buffer)
15006   (let ((func (car (gnus-find-method-for-group group))))
15007     (funcall (intern (format "%s-request-replace-article" func))
15008              article (gnus-group-real-name group) buffer)))
15009
15010 (defun gnus-request-associate-buffer (group)
15011   (let ((method (gnus-find-method-for-group group)))
15012     (funcall (gnus-get-function method 'request-associate-buffer)
15013              (gnus-group-real-name group))))
15014
15015 (defun gnus-request-restore-buffer (article group)
15016   "Request a new buffer restored to the state of ARTICLE."
15017   (let ((method (gnus-find-method-for-group group)))
15018     (funcall (gnus-get-function method 'request-restore-buffer)
15019              article (gnus-group-real-name group) (nth 1 method))))
15020
15021 (defun gnus-request-create-group (group &optional method)
15022   (when (stringp method)
15023     (setq method (gnus-server-to-method method)))
15024   (let ((method (or method (gnus-find-method-for-group group))))
15025     (funcall (gnus-get-function method 'request-create-group)
15026              (gnus-group-real-name group) (nth 1 method))))
15027
15028 (defun gnus-request-delete-group (group &optional force)
15029   (let ((method (gnus-find-method-for-group group)))
15030     (funcall (gnus-get-function method 'request-delete-group)
15031              (gnus-group-real-name group) force (nth 1 method))))
15032
15033 (defun gnus-request-rename-group (group new-name)
15034   (let ((method (gnus-find-method-for-group group)))
15035     (funcall (gnus-get-function method 'request-rename-group)
15036              (gnus-group-real-name group)
15037              (gnus-group-real-name new-name) (nth 1 method))))
15038
15039 (defun gnus-member-of-valid (symbol group)
15040   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15041   (memq symbol (assoc
15042                 (symbol-name (car (gnus-find-method-for-group group)))
15043                 gnus-valid-select-methods)))
15044
15045 (defun gnus-method-option-p (method option)
15046   "Return non-nil if select METHOD has OPTION as a parameter."
15047   (when (stringp method)
15048     (setq method (gnus-server-to-method method)))
15049   (memq option (assoc (format "%s" (car method))
15050                       gnus-valid-select-methods)))
15051
15052 (defun gnus-server-extend-method (group method)
15053   ;; This function "extends" a virtual server.  If the server is
15054   ;; "hello", and the select method is ("hello" (my-var "something"))
15055   ;; in the group "alt.alt", this will result in a new virtual server
15056   ;; called "hello+alt.alt".
15057   (let ((entry
15058          (gnus-copy-sequence
15059           (if (equal (car method) "native") gnus-select-method
15060             (cdr (assoc (car method) gnus-server-alist))))))
15061     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15062     (nconc entry (cdr method))))
15063
15064 (defun gnus-find-method-for-group (group &optional info)
15065   "Find the select method that GROUP uses."
15066   (or gnus-override-method
15067       (and (not group)
15068            gnus-select-method)
15069       (let ((info (or info (gnus-get-info group)))
15070             method)
15071         (if (or (not info)
15072                 (not (setq method (gnus-info-method info)))
15073                 (equal method "native"))
15074             gnus-select-method
15075           (setq method
15076                 (cond ((stringp method)
15077                        (gnus-server-to-method method))
15078                       ((stringp (car method))
15079                        (gnus-server-extend-method group method))
15080                       (t
15081                        method)))
15082           (cond ((equal (cadr method) "")
15083                  method)
15084                 ((null (cadr method))
15085                  (list (car method) ""))
15086                 (t
15087                  (gnus-server-add-address method)))))))
15088
15089 (defun gnus-check-backend-function (func group)
15090   "Check whether GROUP supports function FUNC."
15091   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15092                   group)))
15093     (fboundp (intern (format "%s-%s" method func)))))
15094
15095 (defun gnus-methods-using (feature)
15096   "Find all methods that have FEATURE."
15097   (let ((valids gnus-valid-select-methods)
15098         outs)
15099     (while valids
15100       (if (memq feature (car valids))
15101           (setq outs (cons (car valids) outs)))
15102       (setq valids (cdr valids)))
15103     outs))
15104
15105 \f
15106 ;;;
15107 ;;; Active & Newsrc File Handling
15108 ;;;
15109
15110 (defun gnus-setup-news (&optional rawfile level dont-connect)
15111   "Setup news information.
15112 If RAWFILE is non-nil, the .newsrc file will also be read.
15113 If LEVEL is non-nil, the news will be set up at level LEVEL."
15114   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15115
15116     (when init 
15117       ;; Clear some variables to re-initialize news information.
15118       (setq gnus-newsrc-alist nil
15119             gnus-active-hashtb nil)
15120       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15121       (gnus-read-newsrc-file rawfile))
15122
15123     (when (and (not (assoc "archive" gnus-server-alist))
15124                gnus-message-archive-method)
15125       (push (cons "archive" gnus-message-archive-method)
15126             gnus-server-alist))
15127
15128     ;; If we don't read the complete active file, we fill in the
15129     ;; hashtb here.
15130     (if (or (null gnus-read-active-file)
15131             (eq gnus-read-active-file 'some))
15132         (gnus-update-active-hashtb-from-killed))
15133
15134     ;; Read the active file and create `gnus-active-hashtb'.
15135     ;; If `gnus-read-active-file' is nil, then we just create an empty
15136     ;; hash table.  The partial filling out of the hash table will be
15137     ;; done in `gnus-get-unread-articles'.
15138     (and gnus-read-active-file
15139          (not level)
15140          (gnus-read-active-file))
15141
15142     (or gnus-active-hashtb
15143         (setq gnus-active-hashtb (make-vector 4095 0)))
15144
15145     ;; Initialize the cache.
15146     (when gnus-use-cache
15147       (gnus-cache-open))
15148
15149     ;; Possibly eval the dribble file.
15150     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15151
15152     (gnus-update-format-specifications)
15153
15154     ;; See whether we need to read the description file.
15155     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15156              (not gnus-description-hashtb)
15157              (not dont-connect)
15158              gnus-read-active-file)
15159         (gnus-read-all-descriptions-files))
15160
15161     ;; Find new newsgroups and treat them.
15162     (if (and init gnus-check-new-newsgroups (not level)
15163              (gnus-check-server gnus-select-method))
15164         (gnus-find-new-newsgroups))
15165
15166     ;; We might read in new NoCeM messages here.
15167     (when (and gnus-use-nocem 
15168                (not level)
15169                (not dont-connect))
15170       (gnus-nocem-scan-groups))
15171
15172     ;; Find the number of unread articles in each non-dead group.
15173     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15174       (gnus-get-unread-articles level))
15175
15176     (if (and init gnus-check-bogus-newsgroups
15177              gnus-read-active-file (not level)
15178              (gnus-server-opened gnus-select-method))
15179         (gnus-check-bogus-newsgroups))))
15180
15181 (defun gnus-find-new-newsgroups (&optional arg)
15182   "Search for new newsgroups and add them.
15183 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15184 The `-n' option line from .newsrc is respected.
15185 If ARG (the prefix), use the `ask-server' method to query
15186 the server for new groups."
15187   (interactive "P")
15188   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15189                        (null gnus-read-active-file)
15190                        (eq gnus-read-active-file 'some))
15191                    'ask-server gnus-check-new-newsgroups)))
15192     (unless (gnus-check-first-time-used)
15193       (if (or (consp check)
15194               (eq check 'ask-server))
15195           ;; Ask the server for new groups.
15196           (gnus-ask-server-for-new-groups)
15197         ;; Go through the active hashtb and look for new groups.
15198         (let ((groups 0)
15199               group new-newsgroups)
15200           (gnus-message 5 "Looking for new newsgroups...")
15201           (unless gnus-have-read-active-file
15202             (gnus-read-active-file))
15203           (setq gnus-newsrc-last-checked-date (current-time-string))
15204           (unless gnus-killed-hashtb
15205             (gnus-make-hashtable-from-killed))
15206           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15207           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15208           (mapatoms
15209            (lambda (sym)
15210              (if (or (null (setq group (symbol-name sym)))
15211                      (not (boundp sym))
15212                      (null (symbol-value sym))
15213                      (gnus-gethash group gnus-killed-hashtb)
15214                      (gnus-gethash group gnus-newsrc-hashtb))
15215                  ()
15216                (let ((do-sub (gnus-matches-options-n group)))
15217                  (cond
15218                   ((eq do-sub 'subscribe)
15219                    (setq groups (1+ groups))
15220                    (gnus-sethash group group gnus-killed-hashtb)
15221                    (funcall gnus-subscribe-options-newsgroup-method group))
15222                   ((eq do-sub 'ignore)
15223                    nil)
15224                   (t
15225                    (setq groups (1+ groups))
15226                    (gnus-sethash group group gnus-killed-hashtb)
15227                    (if gnus-subscribe-hierarchical-interactive
15228                        (setq new-newsgroups (cons group new-newsgroups))
15229                      (funcall gnus-subscribe-newsgroup-method group)))))))
15230            gnus-active-hashtb)
15231           (when new-newsgroups
15232             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15233           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15234           (if (> groups 0)
15235               (gnus-message 6 "%d new newsgroup%s arrived."
15236                             groups (if (> groups 1) "s have" " has"))
15237             (gnus-message 6 "No new newsgroups.")))))))
15238
15239 (defun gnus-matches-options-n (group)
15240   ;; Returns `subscribe' if the group is to be unconditionally
15241   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15242   ;; no match for the group.
15243
15244   ;; First we check the two user variables.
15245   (cond
15246    ((and gnus-options-subscribe
15247          (string-match gnus-options-subscribe group))
15248     'subscribe)
15249    ((and gnus-auto-subscribed-groups
15250          (string-match gnus-auto-subscribed-groups group))
15251     'subscribe)
15252    ((and gnus-options-not-subscribe
15253          (string-match gnus-options-not-subscribe group))
15254     'ignore)
15255    ;; Then we go through the list that was retrieved from the .newsrc
15256    ;; file.  This list has elements on the form
15257    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15258    ;; is in the reverse order of the options line) is returned.
15259    (t
15260     (let ((regs gnus-newsrc-options-n))
15261       (while (and regs
15262                   (not (string-match (caar regs) group)))
15263         (setq regs (cdr regs)))
15264       (and regs (cdar regs))))))
15265
15266 (defun gnus-ask-server-for-new-groups ()
15267   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15268          (methods (cons gnus-select-method
15269                         (nconc
15270                          (when gnus-message-archive-method
15271                            (list "archive"))
15272                          (append
15273                           (and (consp gnus-check-new-newsgroups)
15274                                gnus-check-new-newsgroups)
15275                           gnus-secondary-select-methods))))
15276          (groups 0)
15277          (new-date (current-time-string))
15278          group new-newsgroups got-new method hashtb
15279          gnus-override-subscribe-method)
15280     ;; Go through both primary and secondary select methods and
15281     ;; request new newsgroups.
15282     (while (setq method (gnus-server-get-method nil (pop methods)))
15283       (setq new-newsgroups nil)
15284       (setq gnus-override-subscribe-method method)
15285       (when (and (gnus-check-server method)
15286                  (gnus-request-newgroups date method))
15287         (save-excursion
15288           (setq got-new t)
15289           (setq hashtb (gnus-make-hashtable 100))
15290           (set-buffer nntp-server-buffer)
15291           ;; Enter all the new groups into a hashtable.
15292           (gnus-active-to-gnus-format method hashtb 'ignore))
15293         ;; Now all new groups from `method' are in `hashtb'.
15294         (mapatoms
15295          (lambda (group-sym)
15296            (if (or (null (setq group (symbol-name group-sym)))
15297                    (not (boundp group-sym))
15298                    (null (symbol-value group-sym))
15299                    (gnus-gethash group gnus-newsrc-hashtb)
15300                    (member group gnus-zombie-list)
15301                    (member group gnus-killed-list))
15302                ;; The group is already known.
15303                ()
15304              ;; Make this group active.
15305              (when (symbol-value group-sym)
15306                (gnus-set-active group (symbol-value group-sym)))
15307              ;; Check whether we want it or not.
15308              (let ((do-sub (gnus-matches-options-n group)))
15309                (cond
15310                 ((eq do-sub 'subscribe)
15311                  (incf groups)
15312                  (gnus-sethash group group gnus-killed-hashtb)
15313                  (funcall gnus-subscribe-options-newsgroup-method group))
15314                 ((eq do-sub 'ignore)
15315                  nil)
15316                 (t
15317                  (incf groups)
15318                  (gnus-sethash group group gnus-killed-hashtb)
15319                  (if gnus-subscribe-hierarchical-interactive
15320                      (push group new-newsgroups)
15321                    (funcall gnus-subscribe-newsgroup-method group)))))))
15322          hashtb))
15323       (when new-newsgroups
15324         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15325     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15326     (when (> groups 0)
15327       (gnus-message 6 "%d new newsgroup%s arrived."
15328                     groups (if (> groups 1) "s have" " has")))
15329     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15330     got-new))
15331
15332 (defun gnus-check-first-time-used ()
15333   (if (or (> (length gnus-newsrc-alist) 1)
15334           (file-exists-p gnus-startup-file)
15335           (file-exists-p (concat gnus-startup-file ".el"))
15336           (file-exists-p (concat gnus-startup-file ".eld")))
15337       nil
15338     (gnus-message 6 "First time user; subscribing you to default groups")
15339     (unless gnus-have-read-active-file
15340       (gnus-read-active-file))
15341     (setq gnus-newsrc-last-checked-date (current-time-string))
15342     (let ((groups gnus-default-subscribed-newsgroups)
15343           group)
15344       (if (eq groups t)
15345           nil
15346         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15347         (mapatoms
15348          (lambda (sym)
15349            (if (null (setq group (symbol-name sym)))
15350                ()
15351              (let ((do-sub (gnus-matches-options-n group)))
15352                (cond
15353                 ((eq do-sub 'subscribe)
15354                  (gnus-sethash group group gnus-killed-hashtb)
15355                  (funcall gnus-subscribe-options-newsgroup-method group))
15356                 ((eq do-sub 'ignore)
15357                  nil)
15358                 (t
15359                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15360          gnus-active-hashtb)
15361         (while groups
15362           (if (gnus-active (car groups))
15363               (gnus-group-change-level
15364                (car groups) gnus-level-default-subscribed gnus-level-killed))
15365           (setq groups (cdr groups)))
15366         (gnus-group-make-help-group)
15367         (and gnus-novice-user
15368              (gnus-message 7 "`A k' to list killed groups"))))))
15369
15370 (defun gnus-subscribe-group (group previous &optional method)
15371   (gnus-group-change-level
15372    (if method
15373        (list t group gnus-level-default-subscribed nil nil method)
15374      group)
15375    gnus-level-default-subscribed gnus-level-killed previous t))
15376
15377 ;; `gnus-group-change-level' is the fundamental function for changing
15378 ;; subscription levels of newsgroups.  This might mean just changing
15379 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15380 ;; again, which subscribes/unsubscribes a group, which is equally
15381 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15382 ;; from 8-9 to 1-7 means that you remove the group from the list of
15383 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15384 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15385 ;; which is trivial.
15386 ;; ENTRY can either be a string (newsgroup name) or a list (if
15387 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15388 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15389 ;; entries.
15390 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15391 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15392 ;; after.
15393 (defun gnus-group-change-level (entry level &optional oldlevel
15394                                       previous fromkilled)
15395   (let (group info active num)
15396     ;; Glean what info we can from the arguments
15397     (if (consp entry)
15398         (if fromkilled (setq group (nth 1 entry))
15399           (setq group (car (nth 2 entry))))
15400       (setq group entry))
15401     (if (and (stringp entry)
15402              oldlevel
15403              (< oldlevel gnus-level-zombie))
15404         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15405     (if (and (not oldlevel)
15406              (consp entry))
15407         (setq oldlevel (gnus-info-level (nth 2 entry)))
15408       (setq oldlevel (or oldlevel 9)))
15409     (if (stringp previous)
15410         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15411
15412     (if (and (>= oldlevel gnus-level-zombie)
15413              (gnus-gethash group gnus-newsrc-hashtb))
15414         ;; We are trying to subscribe a group that is already
15415         ;; subscribed.
15416         ()                              ; Do nothing.
15417
15418       (or (gnus-ephemeral-group-p group)
15419           (gnus-dribble-enter
15420            (format "(gnus-group-change-level %S %S %S %S %S)"
15421                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15422
15423       ;; Then we remove the newgroup from any old structures, if needed.
15424       ;; If the group was killed, we remove it from the killed or zombie
15425       ;; list.  If not, and it is in fact going to be killed, we remove
15426       ;; it from the newsrc hash table and assoc.
15427       (cond
15428        ((>= oldlevel gnus-level-zombie)
15429         (if (= oldlevel gnus-level-zombie)
15430             (setq gnus-zombie-list (delete group gnus-zombie-list))
15431           (setq gnus-killed-list (delete group gnus-killed-list))))
15432        (t
15433         (if (and (>= level gnus-level-zombie)
15434                  entry)
15435             (progn
15436               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15437               (if (nth 3 entry)
15438                   (setcdr (gnus-gethash (car (nth 3 entry))
15439                                         gnus-newsrc-hashtb)
15440                           (cdr entry)))
15441               (setcdr (cdr entry) (cdddr entry))))))
15442
15443       ;; Finally we enter (if needed) the list where it is supposed to
15444       ;; go, and change the subscription level.  If it is to be killed,
15445       ;; we enter it into the killed or zombie list.
15446       (cond 
15447        ((>= level gnus-level-zombie)
15448         ;; Remove from the hash table.
15449         (gnus-sethash group nil gnus-newsrc-hashtb)
15450         ;; We do not enter foreign groups into the list of dead
15451         ;; groups.
15452         (unless (gnus-group-foreign-p group)
15453           (if (= level gnus-level-zombie)
15454               (setq gnus-zombie-list (cons group gnus-zombie-list))
15455             (setq gnus-killed-list (cons group gnus-killed-list)))))
15456        (t
15457         ;; If the list is to be entered into the newsrc assoc, and
15458         ;; it was killed, we have to create an entry in the newsrc
15459         ;; hashtb format and fix the pointers in the newsrc assoc.
15460         (if (< oldlevel gnus-level-zombie)
15461             ;; It was alive, and it is going to stay alive, so we
15462             ;; just change the level and don't change any pointers or
15463             ;; hash table entries.
15464             (setcar (cdaddr entry) level)
15465           (if (listp entry)
15466               (setq info (cdr entry)
15467                     num (car entry))
15468             (setq active (gnus-active group))
15469             (setq num
15470                   (if active (- (1+ (cdr active)) (car active)) t))
15471             ;; Check whether the group is foreign.  If so, the
15472             ;; foreign select method has to be entered into the
15473             ;; info.
15474             (let ((method (or gnus-override-subscribe-method
15475                               (gnus-group-method group))))
15476               (if (eq method gnus-select-method)
15477                   (setq info (list group level nil))
15478                 (setq info (list group level nil nil method)))))
15479           (unless previous
15480             (setq previous
15481                   (let ((p gnus-newsrc-alist))
15482                     (while (cddr p)
15483                       (setq p (cdr p)))
15484                     p)))
15485           (setq entry (cons info (cddr previous)))
15486           (if (cdr previous)
15487               (progn
15488                 (setcdr (cdr previous) entry)
15489                 (gnus-sethash group (cons num (cdr previous))
15490                               gnus-newsrc-hashtb))
15491             (setcdr previous entry)
15492             (gnus-sethash group (cons num previous)
15493                           gnus-newsrc-hashtb))
15494           (when (cdr entry)
15495             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15496       (when gnus-group-change-level-function
15497         (funcall gnus-group-change-level-function group level oldlevel)))))
15498
15499 (defun gnus-kill-newsgroup (newsgroup)
15500   "Obsolete function.  Kills a newsgroup."
15501   (gnus-group-change-level
15502    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15503
15504 (defun gnus-check-bogus-newsgroups (&optional confirm)
15505   "Remove bogus newsgroups.
15506 If CONFIRM is non-nil, the user has to confirm the deletion of every
15507 newsgroup."
15508   (let ((newsrc (cdr gnus-newsrc-alist))
15509         bogus group entry info)
15510     (gnus-message 5 "Checking bogus newsgroups...")
15511     (unless gnus-have-read-active-file
15512       (gnus-read-active-file))
15513     (when (member gnus-select-method gnus-have-read-active-file)
15514       ;; Find all bogus newsgroup that are subscribed.
15515       (while newsrc
15516         (setq info (pop newsrc)
15517               group (gnus-info-group info))
15518         (unless (or (gnus-active group) ; Active
15519                     (gnus-info-method info) ; Foreign
15520                     (and confirm
15521                          (not (gnus-y-or-n-p
15522                                (format "Remove bogus newsgroup: %s " group)))))
15523           ;; Found a bogus newsgroup.
15524           (push group bogus)))
15525       ;; Remove all bogus subscribed groups by first killing them, and
15526       ;; then removing them from the list of killed groups.
15527       (while bogus
15528         (when (setq entry (gnus-gethash (setq group (pop bogus))
15529                                         gnus-newsrc-hashtb))
15530           (gnus-group-change-level entry gnus-level-killed)
15531           (setq gnus-killed-list (delete group gnus-killed-list))))
15532       ;; Then we remove all bogus groups from the list of killed and
15533       ;; zombie groups.  They are are removed without confirmation.
15534       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15535             killed)
15536         (while dead-lists
15537           (setq killed (symbol-value (car dead-lists)))
15538           (while killed
15539             (unless (gnus-active (setq group (pop killed)))
15540               ;; The group is bogus.
15541               ;; !!!Slow as hell.
15542               (set (car dead-lists)
15543                    (delete group (symbol-value (car dead-lists))))))
15544           (setq dead-lists (cdr dead-lists))))
15545       (gnus-message 5 "Checking bogus newsgroups...done"))))
15546
15547 (defun gnus-check-duplicate-killed-groups ()
15548   "Remove duplicates from the list of killed groups."
15549   (interactive)
15550   (let ((killed gnus-killed-list))
15551     (while killed
15552       (gnus-message 9 "%d" (length killed))
15553       (setcdr killed (delete (car killed) (cdr killed)))
15554       (setq killed (cdr killed)))))
15555
15556 ;; We want to inline a function from gnus-cache, so we cheat here:
15557 (eval-when-compile
15558   (provide 'gnus)
15559   (require 'gnus-cache))
15560
15561 (defun gnus-get-unread-articles-in-group (info active &optional update)
15562   (when active
15563     ;; Allow the backend to update the info in the group.
15564     (when (and update 
15565                (gnus-request-update-info
15566                 info (gnus-find-method-for-group (gnus-info-group info))))
15567       (gnus-activate-group (gnus-info-group info) nil t))
15568     (let* ((range (gnus-info-read info))
15569            (num 0))
15570       ;; If a cache is present, we may have to alter the active info.
15571       (when (and gnus-use-cache info)
15572         (inline (gnus-cache-possibly-alter-active 
15573                  (gnus-info-group info) active)))
15574       ;; Modify the list of read articles according to what articles
15575       ;; are available; then tally the unread articles and add the
15576       ;; number to the group hash table entry.
15577       (cond
15578        ((zerop (cdr active))
15579         (setq num 0))
15580        ((not range)
15581         (setq num (- (1+ (cdr active)) (car active))))
15582        ((not (listp (cdr range)))
15583         ;; Fix a single (num . num) range according to the
15584         ;; active hash table.
15585         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15586         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15587         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15588         ;; Compute number of unread articles.
15589         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15590        (t
15591         ;; The read list is a list of ranges.  Fix them according to
15592         ;; the active hash table.
15593         ;; First peel off any elements that are below the lower
15594         ;; active limit.
15595         (while (and (cdr range)
15596                     (>= (car active)
15597                         (or (and (atom (cadr range)) (cadr range))
15598                             (caadr range))))
15599           (if (numberp (car range))
15600               (setcar range
15601                       (cons (car range)
15602                             (or (and (numberp (cadr range))
15603                                      (cadr range))
15604                                 (cdadr range))))
15605             (setcdr (car range)
15606                     (or (and (numberp (nth 1 range)) (nth 1 range))
15607                         (cdadr range))))
15608           (setcdr range (cddr range)))
15609         ;; Adjust the first element to be the same as the lower limit.
15610         (if (and (not (atom (car range)))
15611                  (< (cdar range) (car active)))
15612             (setcdr (car range) (1- (car active))))
15613         ;; Then we want to peel off any elements that are higher
15614         ;; than the upper active limit.
15615         (let ((srange range))
15616           ;; Go past all legal elements.
15617           (while (and (cdr srange)
15618                       (<= (or (and (atom (cadr srange))
15619                                    (cadr srange))
15620                               (caadr srange)) (cdr active)))
15621             (setq srange (cdr srange)))
15622           (if (cdr srange)
15623               ;; Nuke all remaining illegal elements.
15624               (setcdr srange nil))
15625
15626           ;; Adjust the final element.
15627           (if (and (not (atom (car srange)))
15628                    (> (cdar srange) (cdr active)))
15629               (setcdr (car srange) (cdr active))))
15630         ;; Compute the number of unread articles.
15631         (while range
15632           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15633                                       (cdar range)))
15634                               (or (and (atom (car range)) (car range))
15635                                   (caar range)))))
15636           (setq range (cdr range)))
15637         (setq num (max 0 (- (cdr active) num)))))
15638       ;; Set the number of unread articles.
15639       (when info
15640         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15641       num)))
15642
15643 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15644 ;; and compute how many unread articles there are in each group.
15645 (defun gnus-get-unread-articles (&optional level)
15646   (let* ((newsrc (cdr gnus-newsrc-alist))
15647          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15648          (foreign-level
15649           (min
15650            (cond ((and gnus-activate-foreign-newsgroups
15651                        (not (numberp gnus-activate-foreign-newsgroups)))
15652                   (1+ gnus-level-subscribed))
15653                  ((numberp gnus-activate-foreign-newsgroups)
15654                   gnus-activate-foreign-newsgroups)
15655                  (t 0))
15656            level))
15657          info group active method)
15658     (gnus-message 5 "Checking new news...")
15659
15660     (while newsrc
15661       (setq active (gnus-active (setq group (gnus-info-group
15662                                              (setq info (pop newsrc))))))
15663
15664       ;; Check newsgroups.  If the user doesn't want to check them, or
15665       ;; they can't be checked (for instance, if the news server can't
15666       ;; be reached) we just set the number of unread articles in this
15667       ;; newsgroup to t.  This means that Gnus thinks that there are
15668       ;; unread articles, but it has no idea how many.
15669       (if (and (setq method (gnus-info-method info))
15670                (not (gnus-server-equal
15671                      gnus-select-method
15672                      (setq method (gnus-server-get-method nil method))))
15673                (not (gnus-secondary-method-p method)))
15674           ;; These groups are foreign.  Check the level.
15675           (when (<= (gnus-info-level info) foreign-level)
15676             (setq active (gnus-activate-group group 'scan))
15677             (unless (inline (gnus-virtual-group-p group))
15678               (inline (gnus-close-group group))))
15679
15680         ;; These groups are native or secondary.
15681         (when (and (<= (gnus-info-level info) level)
15682                    (not gnus-read-active-file))
15683           (setq active (gnus-activate-group group 'scan))
15684           (inline (gnus-close-group group))))
15685
15686       (if active
15687           (inline (gnus-get-unread-articles-in-group 
15688                    info active
15689                    (and method
15690                         (fboundp (intern (concat (symbol-name (car method))
15691                                                  "-request-update-info"))))))
15692         ;; The group couldn't be reached, so we nix out the number of
15693         ;; unread articles and stuff.
15694         (gnus-set-active group nil)
15695         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15696
15697     (gnus-message 5 "Checking new news...done")))
15698
15699 ;; Create a hash table out of the newsrc alist.  The `car's of the
15700 ;; alist elements are used as keys.
15701 (defun gnus-make-hashtable-from-newsrc-alist ()
15702   (let ((alist gnus-newsrc-alist)
15703         (ohashtb gnus-newsrc-hashtb)
15704         prev)
15705     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15706     (setq alist
15707           (setq prev (setq gnus-newsrc-alist
15708                            (if (equal (caar gnus-newsrc-alist)
15709                                       "dummy.group")
15710                                gnus-newsrc-alist
15711                              (cons (list "dummy.group" 0 nil) alist)))))
15712     (while alist
15713       (gnus-sethash
15714        (caar alist)
15715        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15716              prev)
15717        gnus-newsrc-hashtb)
15718       (setq prev alist
15719             alist (cdr alist)))))
15720
15721 (defun gnus-make-hashtable-from-killed ()
15722   "Create a hash table from the killed and zombie lists."
15723   (let ((lists '(gnus-killed-list gnus-zombie-list))
15724         list)
15725     (setq gnus-killed-hashtb
15726           (gnus-make-hashtable
15727            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15728     (while (setq list (pop lists))
15729       (setq list (symbol-value list))
15730       (while list
15731         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15732
15733 (defun gnus-activate-group (group &optional scan dont-check)
15734   ;; Check whether a group has been activated or not.
15735   ;; If SCAN, request a scan of that group as well.
15736   (let ((method (gnus-find-method-for-group group))
15737         active)
15738     (and (gnus-check-server method)
15739          ;; We escape all bugs and quit here to make it possible to
15740          ;; continue if a group is so out-there that it reports bugs
15741          ;; and stuff.
15742          (progn
15743            (and scan
15744                 (gnus-check-backend-function 'request-scan (car method))
15745                 (gnus-request-scan group method))
15746            t)
15747          (condition-case ()
15748              (gnus-request-group group dont-check)
15749         ;   (error nil)
15750            (quit nil))
15751          (save-excursion
15752            (set-buffer nntp-server-buffer)
15753            (goto-char (point-min))
15754            ;; Parse the result we got from `gnus-request-group'.
15755            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15756                 (progn
15757                   (goto-char (match-beginning 1))
15758                   (gnus-set-active
15759                    group (setq active (cons (read (current-buffer))
15760                                             (read (current-buffer)))))
15761                   ;; Return the new active info.
15762                   active))))))
15763
15764 (defun gnus-update-read-articles (group unread)
15765   "Update the list of read and ticked articles in GROUP using the
15766 UNREAD and TICKED lists.
15767 Note: UNSELECTED has to be sorted over `<'.
15768 Returns whether the updating was successful."
15769   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15770          (entry (gnus-gethash group gnus-newsrc-hashtb))
15771          (info (nth 2 entry))
15772          (prev 1)
15773          (unread (sort (copy-sequence unread) '<))
15774          read)
15775     (if (or (not info) (not active))
15776         ;; There is no info on this group if it was, in fact,
15777         ;; killed.  Gnus stores no information on killed groups, so
15778         ;; there's nothing to be done.
15779         ;; One could store the information somewhere temporarily,
15780         ;; perhaps...  Hmmm...
15781         ()
15782       ;; Remove any negative articles numbers.
15783       (while (and unread (< (car unread) 0))
15784         (setq unread (cdr unread)))
15785       ;; Remove any expired article numbers
15786       (while (and unread (< (car unread) (car active)))
15787         (setq unread (cdr unread)))
15788       ;; Compute the ranges of read articles by looking at the list of
15789       ;; unread articles.
15790       (while unread
15791         (if (/= (car unread) prev)
15792             (setq read (cons (if (= prev (1- (car unread))) prev
15793                                (cons prev (1- (car unread)))) read)))
15794         (setq prev (1+ (car unread)))
15795         (setq unread (cdr unread)))
15796       (when (<= prev (cdr active))
15797         (setq read (cons (cons prev (cdr active)) read)))
15798       ;; Enter this list into the group info.
15799       (gnus-info-set-read
15800        info (if (> (length read) 1) (nreverse read) read))
15801       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15802       (gnus-get-unread-articles-in-group info (gnus-active group))
15803       t)))
15804
15805 (defun gnus-make-articles-unread (group articles)
15806   "Mark ARTICLES in GROUP as unread."
15807   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15808                           (gnus-gethash (gnus-group-real-name group)
15809                                         gnus-newsrc-hashtb))))
15810          (ranges (gnus-info-read info))
15811          news article)
15812     (while articles
15813       (when (gnus-member-of-range
15814              (setq article (pop articles)) ranges)
15815         (setq news (cons article news))))
15816     (when news
15817       (gnus-info-set-read
15818        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15819       (gnus-group-update-group group t))))
15820
15821 ;; Enter all dead groups into the hashtb.
15822 (defun gnus-update-active-hashtb-from-killed ()
15823   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15824         (lists (list gnus-killed-list gnus-zombie-list))
15825         killed)
15826     (while lists
15827       (setq killed (car lists))
15828       (while killed
15829         (gnus-sethash (car killed) nil hashtb)
15830         (setq killed (cdr killed)))
15831       (setq lists (cdr lists)))))
15832
15833 ;; Get the active file(s) from the backend(s).
15834 (defun gnus-read-active-file ()
15835   (gnus-group-set-mode-line)
15836   (let ((methods 
15837          (append
15838           (if (gnus-check-server gnus-select-method)
15839               ;; The native server is available.
15840               (cons gnus-select-method gnus-secondary-select-methods)
15841             ;; The native server is down, so we just do the
15842             ;; secondary ones.
15843             gnus-secondary-select-methods)
15844           ;; Also read from the archive server.
15845           (when gnus-message-archive-method
15846             (list "archive"))))
15847         list-type)
15848     (setq gnus-have-read-active-file nil)
15849     (save-excursion
15850       (set-buffer nntp-server-buffer)
15851       (while methods
15852         (let* ((method (if (stringp (car methods))
15853                            (gnus-server-get-method nil (car methods))
15854                          (car methods)))
15855                (where (nth 1 method))
15856                (mesg (format "Reading active file%s via %s..."
15857                              (if (and where (not (zerop (length where))))
15858                                  (concat " from " where) "")
15859                              (car method))))
15860           (gnus-message 5 mesg)
15861           (when (gnus-check-server method)
15862             ;; Request that the backend scan its incoming messages.
15863             (and (gnus-check-backend-function 'request-scan (car method))
15864                  (gnus-request-scan nil method))
15865             (cond
15866              ((and (eq gnus-read-active-file 'some)
15867                    (gnus-check-backend-function 'retrieve-groups (car method)))
15868               (let ((newsrc (cdr gnus-newsrc-alist))
15869                     (gmethod (gnus-server-get-method nil method))
15870                     groups info)
15871                 (while (setq info (pop newsrc))
15872                   (when (gnus-server-equal
15873                          (gnus-find-method-for-group 
15874                           (gnus-info-group info) info)
15875                          gmethod)
15876                     (push (gnus-group-real-name (gnus-info-group info)) 
15877                           groups)))
15878                 (when groups
15879                   (gnus-check-server method)
15880                   (setq list-type (gnus-retrieve-groups groups method))
15881                   (cond
15882                    ((not list-type)
15883                     (gnus-error
15884                      1.2 "Cannot read partial active file from %s server."
15885                      (car method)))
15886                    ((eq list-type 'active)
15887                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15888                    (t
15889                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15890              (t
15891               (if (not (gnus-request-list method))
15892                   (unless (equal method gnus-message-archive-method)
15893                     (gnus-error 1 "Cannot read active file from %s server."
15894                                 (car method)))
15895                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15896                 ;; We mark this active file as read.
15897                 (push method gnus-have-read-active-file)
15898                 (gnus-message 5 "%sdone" mesg))))))
15899         (setq methods (cdr methods))))))
15900
15901 ;; Read an active file and place the results in `gnus-active-hashtb'.
15902 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15903   (unless method
15904     (setq method gnus-select-method))
15905   (let ((cur (current-buffer))
15906         (hashtb (or hashtb
15907                     (if (and gnus-active-hashtb
15908                              (not (equal method gnus-select-method)))
15909                         gnus-active-hashtb
15910                       (setq gnus-active-hashtb
15911                             (if (equal method gnus-select-method)
15912                                 (gnus-make-hashtable
15913                                  (count-lines (point-min) (point-max)))
15914                               (gnus-make-hashtable 4096)))))))
15915     ;; Delete unnecessary lines.
15916     (goto-char (point-min))
15917     (while (search-forward "\nto." nil t)
15918       (delete-region (1+ (match-beginning 0))
15919                      (progn (forward-line 1) (point))))
15920     (or (string= gnus-ignored-newsgroups "")
15921         (progn
15922           (goto-char (point-min))
15923           (delete-matching-lines gnus-ignored-newsgroups)))
15924     ;; Make the group names readable as a lisp expression even if they
15925     ;; contain special characters.
15926     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15927     (goto-char (point-max))
15928     (while (re-search-backward "[][';?()#]" nil t)
15929       (insert ?\\))
15930     ;; If these are groups from a foreign select method, we insert the
15931     ;; group prefix in front of the group names.
15932     (and method (not (gnus-server-equal
15933                       (gnus-server-get-method nil method)
15934                       (gnus-server-get-method nil gnus-select-method)))
15935          (let ((prefix (gnus-group-prefixed-name "" method)))
15936            (goto-char (point-min))
15937            (while (and (not (eobp))
15938                        (progn (insert prefix)
15939                               (zerop (forward-line 1)))))))
15940     ;; Store the active file in a hash table.
15941     (goto-char (point-min))
15942     (if (string-match "%[oO]" gnus-group-line-format)
15943         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15944         ;; If we want information on moderated groups, we use this
15945         ;; loop...
15946         (let* ((mod-hashtb (make-vector 7 0))
15947                (m (intern "m" mod-hashtb))
15948                group max min)
15949           (while (not (eobp))
15950             (condition-case nil
15951                 (progn
15952                   (narrow-to-region (point) (gnus-point-at-eol))
15953                   (setq group (let ((obarray hashtb)) (read cur)))
15954                   (if (and (numberp (setq max (read cur)))
15955                            (numberp (setq min (read cur)))
15956                            (progn
15957                              (skip-chars-forward " \t")
15958                              (not
15959                               (or (= (following-char) ?=)
15960                                   (= (following-char) ?x)
15961                                   (= (following-char) ?j)))))
15962                       (set group (cons min max))
15963                     (set group nil))
15964                   ;; Enter moderated groups into a list.
15965                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15966                       (setq gnus-moderated-list
15967                             (cons (symbol-name group) gnus-moderated-list))))
15968               (error
15969                (and group
15970                     (symbolp group)
15971                     (set group nil))))
15972             (widen)
15973             (forward-line 1)))
15974       ;; And if we do not care about moderation, we use this loop,
15975       ;; which is faster.
15976       (let (group max min)
15977         (while (not (eobp))
15978           (condition-case ()
15979               (progn
15980                 (narrow-to-region (point) (gnus-point-at-eol))
15981                 ;; group gets set to a symbol interned in the hash table
15982                 ;; (what a hack!!) - jwz
15983                 (setq group (let ((obarray hashtb)) (read cur)))
15984                 (if (and (numberp (setq max (read cur)))
15985                          (numberp (setq min (read cur)))
15986                          (progn
15987                            (skip-chars-forward " \t")
15988                            (not
15989                             (or (= (following-char) ?=)
15990                                 (= (following-char) ?x)
15991                                 (= (following-char) ?j)))))
15992                     (set group (cons min max))
15993                   (set group nil)))
15994             (error
15995              (progn
15996                (and group
15997                     (symbolp group)
15998                     (set group nil))
15999                (or ignore-errors
16000                    (gnus-message 3 "Warning - illegal active: %s"
16001                                  (buffer-substring
16002                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16003           (widen)
16004           (forward-line 1))))))
16005
16006 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16007   ;; Parse a "groups" active file.
16008   (let ((cur (current-buffer))
16009         (hashtb (or hashtb
16010                     (if (and method gnus-active-hashtb)
16011                         gnus-active-hashtb
16012                       (setq gnus-active-hashtb
16013                             (gnus-make-hashtable
16014                              (count-lines (point-min) (point-max)))))))
16015         (prefix (and method
16016                      (not (gnus-server-equal
16017                            (gnus-server-get-method nil method)
16018                            (gnus-server-get-method nil gnus-select-method)))
16019                      (gnus-group-prefixed-name "" method))))
16020
16021     (goto-char (point-min))
16022     ;; We split this into to separate loops, one with the prefix
16023     ;; and one without to speed the reading up somewhat.
16024     (if prefix
16025         (let (min max opoint group)
16026           (while (not (eobp))
16027             (condition-case ()
16028                 (progn
16029                   (read cur) (read cur)
16030                   (setq min (read cur)
16031                         max (read cur)
16032                         opoint (point))
16033                   (skip-chars-forward " \t")
16034                   (insert prefix)
16035                   (goto-char opoint)
16036                   (set (let ((obarray hashtb)) (read cur))
16037                        (cons min max)))
16038               (error (and group (symbolp group) (set group nil))))
16039             (forward-line 1)))
16040       (let (min max group)
16041         (while (not (eobp))
16042           (condition-case ()
16043               (if (= (following-char) ?2)
16044                   (progn
16045                     (read cur) (read cur)
16046                     (setq min (read cur)
16047                           max (read cur))
16048                     (set (setq group (let ((obarray hashtb)) (read cur)))
16049                          (cons min max))))
16050             (error (and group (symbolp group) (set group nil))))
16051           (forward-line 1))))))
16052
16053 (defun gnus-read-newsrc-file (&optional force)
16054   "Read startup file.
16055 If FORCE is non-nil, the .newsrc file is read."
16056   ;; Reset variables that might be defined in the .newsrc.eld file.
16057   (let ((variables gnus-variable-list))
16058     (while variables
16059       (set (car variables) nil)
16060       (setq variables (cdr variables))))
16061   (let* ((newsrc-file gnus-current-startup-file)
16062          (quick-file (concat newsrc-file ".el")))
16063     (save-excursion
16064       ;; We always load the .newsrc.eld file.  If always contains
16065       ;; much information that can not be gotten from the .newsrc
16066       ;; file (ticked articles, killed groups, foreign methods, etc.)
16067       (gnus-read-newsrc-el-file quick-file)
16068
16069       (if (and (file-exists-p gnus-current-startup-file)
16070                (or force
16071                    (and (file-newer-than-file-p newsrc-file quick-file)
16072                         (file-newer-than-file-p newsrc-file
16073                                                 (concat quick-file "d")))
16074                    (not gnus-newsrc-alist)))
16075           ;; We read the .newsrc file.  Note that if there if a
16076           ;; .newsrc.eld file exists, it has already been read, and
16077           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16078           ;; the .newsrc file, Gnus will only use the information it
16079           ;; can find there for changing the data already read -
16080           ;; ie. reading the .newsrc file will not trash the data
16081           ;; already read (except for read articles).
16082           (save-excursion
16083             (gnus-message 5 "Reading %s..." newsrc-file)
16084             (set-buffer (find-file-noselect newsrc-file))
16085             (buffer-disable-undo (current-buffer))
16086             (gnus-newsrc-to-gnus-format)
16087             (kill-buffer (current-buffer))
16088             (gnus-message 5 "Reading %s...done" newsrc-file)))
16089
16090       ;; Read any slave files.
16091       (unless gnus-slave
16092         (gnus-master-read-slave-newsrc))
16093       
16094       ;; Convert old to new.
16095       (gnus-convert-old-newsrc))))
16096
16097 (defun gnus-continuum-version (version)
16098   "Return VERSION as a floating point number."
16099   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16100     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16101            (number (match-string 2 version))
16102            major minor least)
16103       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16104       (setq major (string-to-number (match-string 1 number)))
16105       (setq minor (string-to-number (match-string 2 number)))
16106       (setq least (if (match-beginning 3)
16107                       (string-to-number (match-string 3 number))
16108                     0))
16109       (string-to-number
16110        (if (zerop major)
16111            (format "%s00%02d%02d"
16112                    (cond 
16113                     ((string= alpha "(ding)") "4.99")
16114                     ((string= alpha "September") "5.01")
16115                     ((string= alpha "Red") "5.03"))
16116                    minor least)
16117          (format "%d.%02d%20d" major minor least))))))
16118
16119 (defun gnus-convert-old-newsrc ()
16120   "Convert old newsrc into the new format, if needed."
16121   (let ((fcv (and gnus-newsrc-file-version
16122                   (gnus-continuum-version gnus-newsrc-file-version))))
16123     (cond
16124      ;; No .newsrc.eld file was loaded.
16125      ((null fcv) nil)
16126      ;; Gnus 5 .newsrc.eld was loaded.
16127      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16128       (gnus-convert-old-ticks)))))
16129
16130 (defun gnus-convert-old-ticks ()
16131   (let ((newsrc (cdr gnus-newsrc-alist))
16132         marks info dormant ticked)
16133     (while (setq info (pop newsrc))
16134       (when (setq marks (gnus-info-marks info))
16135         (setq dormant (cdr (assq 'dormant marks))
16136               ticked (cdr (assq 'tick marks)))
16137         (when (or dormant ticked)
16138           (gnus-info-set-read
16139            info
16140            (gnus-add-to-range
16141             (gnus-info-read info)
16142             (nconc (gnus-uncompress-range dormant)
16143                    (gnus-uncompress-range ticked)))))))))
16144
16145 (defun gnus-read-newsrc-el-file (file)
16146   (let ((ding-file (concat file "d")))
16147     ;; We always, always read the .eld file.
16148     (gnus-message 5 "Reading %s..." ding-file)
16149     (let (gnus-newsrc-assoc)
16150       (condition-case nil
16151           (load ding-file t t t)
16152         (error
16153          (gnus-error 1 "Error in %s" ding-file)))
16154       (when gnus-newsrc-assoc
16155         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16156     (gnus-make-hashtable-from-newsrc-alist)
16157     (when (file-newer-than-file-p file ding-file)
16158       ;; Old format quick file
16159       (gnus-message 5 "Reading %s..." file)
16160       ;; The .el file is newer than the .eld file, so we read that one
16161       ;; as well.
16162       (gnus-read-old-newsrc-el-file file))))
16163
16164 ;; Parse the old-style quick startup file
16165 (defun gnus-read-old-newsrc-el-file (file)
16166   (let (newsrc killed marked group m info)
16167     (prog1
16168         (let ((gnus-killed-assoc nil)
16169               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16170           (prog1
16171               (condition-case nil
16172                   (load file t t t)
16173                 (error nil))
16174             (setq newsrc gnus-newsrc-assoc
16175                   killed gnus-killed-assoc
16176                   marked gnus-marked-assoc)))
16177       (setq gnus-newsrc-alist nil)
16178       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16179         (if info
16180             (progn
16181               (gnus-info-set-read info (cddr group))
16182               (gnus-info-set-level
16183                info (if (nth 1 group) gnus-level-default-subscribed
16184                       gnus-level-default-unsubscribed))
16185               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16186           (push (setq info
16187                       (list (car group)
16188                             (if (nth 1 group) gnus-level-default-subscribed
16189                               gnus-level-default-unsubscribed)
16190                             (cddr group)))
16191                 gnus-newsrc-alist))
16192         ;; Copy marks into info.
16193         (when (setq m (assoc (car group) marked))
16194           (unless (nthcdr 3 info)
16195             (nconc info (list nil)))
16196           (gnus-info-set-marks
16197            info (list (cons 'tick (gnus-compress-sequence 
16198                                    (sort (cdr m) '<) t))))))
16199       (setq newsrc killed)
16200       (while newsrc
16201         (setcar newsrc (caar newsrc))
16202         (setq newsrc (cdr newsrc)))
16203       (setq gnus-killed-list killed))
16204     ;; The .el file version of this variable does not begin with
16205     ;; "options", while the .eld version does, so we just add it if it
16206     ;; isn't there.
16207     (and
16208      gnus-newsrc-options
16209      (progn
16210        (and (not (string-match "^ *options" gnus-newsrc-options))
16211             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16212        (and (not (string-match "\n$" gnus-newsrc-options))
16213             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16214        ;; Finally, if we read some options lines, we parse them.
16215        (or (string= gnus-newsrc-options "")
16216            (gnus-newsrc-parse-options gnus-newsrc-options))))
16217
16218     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16219     (gnus-make-hashtable-from-newsrc-alist)))
16220
16221 (defun gnus-make-newsrc-file (file)
16222   "Make server dependent file name by catenating FILE and server host name."
16223   (let* ((file (expand-file-name file nil))
16224          (real-file (concat file "-" (nth 1 gnus-select-method))))
16225     (if (or (file-exists-p real-file)
16226             (file-exists-p (concat real-file ".el"))
16227             (file-exists-p (concat real-file ".eld")))
16228         real-file file)))
16229
16230 (defun gnus-newsrc-to-gnus-format ()
16231   (setq gnus-newsrc-options "")
16232   (setq gnus-newsrc-options-n nil)
16233
16234   (or gnus-active-hashtb
16235       (setq gnus-active-hashtb (make-vector 4095 0)))
16236   (let ((buf (current-buffer))
16237         (already-read (> (length gnus-newsrc-alist) 1))
16238         group subscribed options-symbol newsrc Options-symbol
16239         symbol reads num1)
16240     (goto-char (point-min))
16241     ;; We intern the symbol `options' in the active hashtb so that we
16242     ;; can `eq' against it later.
16243     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16244     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16245
16246     (while (not (eobp))
16247       ;; We first read the first word on the line by narrowing and
16248       ;; then reading into `gnus-active-hashtb'.  Most groups will
16249       ;; already exist in that hashtb, so this will save some string
16250       ;; space.
16251       (narrow-to-region
16252        (point)
16253        (progn (skip-chars-forward "^ \t!:\n") (point)))
16254       (goto-char (point-min))
16255       (setq symbol
16256             (and (/= (point-min) (point-max))
16257                  (let ((obarray gnus-active-hashtb)) (read buf))))
16258       (widen)
16259       ;; Now, the symbol we have read is either `options' or a group
16260       ;; name.  If it is an options line, we just add it to a string.
16261       (cond
16262        ((or (eq symbol options-symbol)
16263             (eq symbol Options-symbol))
16264         (setq gnus-newsrc-options
16265               ;; This concating is quite inefficient, but since our
16266               ;; thorough studies show that approx 99.37% of all
16267               ;; .newsrc files only contain a single options line, we
16268               ;; don't give a damn, frankly, my dear.
16269               (concat gnus-newsrc-options
16270                       (buffer-substring
16271                        (gnus-point-at-bol)
16272                        ;; Options may continue on the next line.
16273                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16274                                 (progn (beginning-of-line) (point)))
16275                            (point)))))
16276         (forward-line -1))
16277        (symbol
16278         ;; Group names can be just numbers.  
16279         (when (numberp symbol) 
16280           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16281         (or (boundp symbol) (set symbol nil))
16282         ;; It was a group name.
16283         (setq subscribed (= (following-char) ?:)
16284               group (symbol-name symbol)
16285               reads nil)
16286         (if (eolp)
16287             ;; If the line ends here, this is clearly a buggy line, so
16288             ;; we put point a the beginning of line and let the cond
16289             ;; below do the error handling.
16290             (beginning-of-line)
16291           ;; We skip to the beginning of the ranges.
16292           (skip-chars-forward "!: \t"))
16293         ;; We are now at the beginning of the list of read articles.
16294         ;; We read them range by range.
16295         (while
16296             (cond
16297              ((looking-at "[0-9]+")
16298               ;; We narrow and read a number instead of buffer-substring/
16299               ;; string-to-int because it's faster.  narrow/widen is
16300               ;; faster than save-restriction/narrow, and save-restriction
16301               ;; produces a garbage object.
16302               (setq num1 (progn
16303                            (narrow-to-region (match-beginning 0) (match-end 0))
16304                            (read buf)))
16305               (widen)
16306               ;; If the next character is a dash, then this is a range.
16307               (if (= (following-char) ?-)
16308                   (progn
16309                     ;; We read the upper bound of the range.
16310                     (forward-char 1)
16311                     (if (not (looking-at "[0-9]+"))
16312                         ;; This is a buggy line, by we pretend that
16313                         ;; it's kinda OK.  Perhaps the user should be
16314                         ;; dinged?
16315                         (setq reads (cons num1 reads))
16316                       (setq reads
16317                             (cons
16318                              (cons num1
16319                                    (progn
16320                                      (narrow-to-region (match-beginning 0)
16321                                                        (match-end 0))
16322                                      (read buf)))
16323                              reads))
16324                       (widen)))
16325                 ;; It was just a simple number, so we add it to the
16326                 ;; list of ranges.
16327                 (setq reads (cons num1 reads)))
16328               ;; If the next char in ?\n, then we have reached the end
16329               ;; of the line and return nil.
16330               (/= (following-char) ?\n))
16331              ((= (following-char) ?\n)
16332               ;; End of line, so we end.
16333               nil)
16334              (t
16335               ;; Not numbers and not eol, so this might be a buggy
16336               ;; line...
16337               (or (eobp)
16338                   ;; If it was eob instead of ?\n, we allow it.
16339                   (progn
16340                     ;; The line was buggy.
16341                     (setq group nil)
16342                     (gnus-error 3.1 "Mangled line: %s"
16343                                 (buffer-substring (gnus-point-at-bol)
16344                                                   (gnus-point-at-eol)))))
16345               nil))
16346           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16347           ;; we allow them, because it's a common mistake to put a
16348           ;; space after the comma.
16349           (skip-chars-forward ", "))
16350
16351         ;; We have already read .newsrc.eld, so we gently update the
16352         ;; data in the hash table with the information we have just
16353         ;; read.
16354         (when group
16355           (let ((info (gnus-get-info group))
16356                 level)
16357             (if info
16358                 ;; There is an entry for this file in the alist.
16359                 (progn
16360                   (gnus-info-set-read info (nreverse reads))
16361                   ;; We update the level very gently.  In fact, we
16362                   ;; only change it if there's been a status change
16363                   ;; from subscribed to unsubscribed, or vice versa.
16364                   (setq level (gnus-info-level info))
16365                   (cond ((and (<= level gnus-level-subscribed)
16366                               (not subscribed))
16367                          (setq level (if reads
16368                                          gnus-level-default-unsubscribed
16369                                        (1+ gnus-level-default-unsubscribed))))
16370                         ((and (> level gnus-level-subscribed) subscribed)
16371                          (setq level gnus-level-default-subscribed)))
16372                   (gnus-info-set-level info level))
16373               ;; This is a new group.
16374               (setq info (list group
16375                                (if subscribed
16376                                    gnus-level-default-subscribed
16377                                  (if reads
16378                                      (1+ gnus-level-subscribed)
16379                                    gnus-level-default-unsubscribed))
16380                                (nreverse reads))))
16381             (setq newsrc (cons info newsrc))))))
16382       (forward-line 1))
16383
16384     (setq newsrc (nreverse newsrc))
16385
16386     (if (not already-read)
16387         ()
16388       ;; We now have two newsrc lists - `newsrc', which is what we
16389       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16390       ;; what we've read from .newsrc.eld.  We have to merge these
16391       ;; lists.  We do this by "attaching" any (foreign) groups in the
16392       ;; gnus-newsrc-alist to the (native) group that precedes them.
16393       (let ((rc (cdr gnus-newsrc-alist))
16394             (prev gnus-newsrc-alist)
16395             entry mentry)
16396         (while rc
16397           (or (null (nth 4 (car rc)))   ; It's a native group.
16398               (assoc (caar rc) newsrc) ; It's already in the alist.
16399               (if (setq entry (assoc (caar prev) newsrc))
16400                   (setcdr (setq mentry (memq entry newsrc))
16401                           (cons (car rc) (cdr mentry)))
16402                 (setq newsrc (cons (car rc) newsrc))))
16403           (setq prev rc
16404                 rc (cdr rc)))))
16405
16406     (setq gnus-newsrc-alist newsrc)
16407     ;; We make the newsrc hashtb.
16408     (gnus-make-hashtable-from-newsrc-alist)
16409
16410     ;; Finally, if we read some options lines, we parse them.
16411     (or (string= gnus-newsrc-options "")
16412         (gnus-newsrc-parse-options gnus-newsrc-options))))
16413
16414 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16415 ;; The return value will be a list on the form
16416 ;; ((regexp1 . ignore)
16417 ;;  (regexp2 . subscribe)...)
16418 ;; When handling new newsgroups, groups that match a `ignore' regexp
16419 ;; will be ignored, and groups that match a `subscribe' regexp will be
16420 ;; subscribed.  A line like
16421 ;; options -n !all rec.all
16422 ;; will lead to a list that looks like
16423 ;; (("^rec\\..+" . subscribe)
16424 ;;  ("^.+" . ignore))
16425 ;; So all "rec.*" groups will be subscribed, while all the other
16426 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16427 ;; different from "options -n rec.all !all".
16428 (defun gnus-newsrc-parse-options (options)
16429   (let (out eol)
16430     (save-excursion
16431       (gnus-set-work-buffer)
16432       (insert (regexp-quote options))
16433       ;; First we treat all continuation lines.
16434       (goto-char (point-min))
16435       (while (re-search-forward "\n[ \t]+" nil t)
16436         (replace-match " " t t))
16437       ;; Then we transform all "all"s into ".+"s.
16438       (goto-char (point-min))
16439       (while (re-search-forward "\\ball\\b" nil t)
16440         (replace-match ".+" t t))
16441       (goto-char (point-min))
16442       ;; We remove all other options than the "-n" ones.
16443       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16444         (replace-match " ")
16445         (forward-char -1))
16446       (goto-char (point-min))
16447
16448       ;; We are only interested in "options -n" lines - we
16449       ;; ignore the other option lines.
16450       (while (re-search-forward "[ \t]-n" nil t)
16451         (setq eol
16452               (or (save-excursion
16453                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16454                          (- (point) 2)))
16455                   (gnus-point-at-eol)))
16456         ;; Search for all "words"...
16457         (while (re-search-forward "[^ \t,\n]+" eol t)
16458           (if (= (char-after (match-beginning 0)) ?!)
16459               ;; If the word begins with a bang (!), this is a "not"
16460               ;; spec.  We put this spec (minus the bang) and the
16461               ;; symbol `ignore' into the list.
16462               (setq out (cons (cons (concat
16463                                      "^" (buffer-substring
16464                                           (1+ (match-beginning 0))
16465                                           (match-end 0)))
16466                                     'ignore) out))
16467             ;; There was no bang, so this is a "yes" spec.
16468             (setq out (cons (cons (concat "^" (match-string 0))
16469                                   'subscribe) out)))))
16470
16471       (setq gnus-newsrc-options-n out))))
16472
16473 (defun gnus-save-newsrc-file (&optional force)
16474   "Save .newsrc file."
16475   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16476   ;; from the variable gnus-newsrc-alist.
16477   (when (and (or gnus-newsrc-alist gnus-killed-list)
16478              gnus-current-startup-file)
16479     (save-excursion
16480       (if (and (or gnus-use-dribble-file gnus-slave)
16481                (not force)
16482                (or (not gnus-dribble-buffer)
16483                    (not (buffer-name gnus-dribble-buffer))
16484                    (zerop (save-excursion
16485                             (set-buffer gnus-dribble-buffer)
16486                             (buffer-size)))))
16487           (gnus-message 4 "(No changes need to be saved)")
16488         (run-hooks 'gnus-save-newsrc-hook)
16489         (if gnus-slave
16490             (gnus-slave-save-newsrc)
16491           ;; Save .newsrc.
16492           (when gnus-save-newsrc-file
16493             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16494             (gnus-gnus-to-newsrc-format)
16495             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16496           ;; Save .newsrc.eld.
16497           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16498           (make-local-variable 'version-control)
16499           (setq version-control 'never)
16500           (setq buffer-file-name
16501                 (concat gnus-current-startup-file ".eld"))
16502           (gnus-add-current-to-buffer-list)
16503           (buffer-disable-undo (current-buffer))
16504           (erase-buffer)
16505           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16506           (gnus-gnus-to-quick-newsrc-format)
16507           (run-hooks 'gnus-save-quick-newsrc-hook)
16508           (save-buffer)
16509           (kill-buffer (current-buffer))
16510           (gnus-message
16511            5 "Saving %s.eld...done" gnus-current-startup-file))
16512         (gnus-dribble-delete-file)))))
16513
16514 (defun gnus-gnus-to-quick-newsrc-format ()
16515   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16516   (insert ";; Gnus startup file.\n")
16517   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16518   (insert ";; to read .newsrc.\n")
16519   (insert "(setq gnus-newsrc-file-version "
16520           (prin1-to-string gnus-version) ")\n")
16521   (let ((variables
16522          (if gnus-save-killed-list gnus-variable-list
16523            ;; Remove the `gnus-killed-list' from the list of variables
16524            ;; to be saved, if required.
16525            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16526         ;; Peel off the "dummy" group.
16527         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16528         variable)
16529     ;; Insert the variables into the file.
16530     (while variables
16531       (when (and (boundp (setq variable (pop variables)))
16532                  (symbol-value variable))
16533         (insert "(setq " (symbol-name variable) " '")
16534         (prin1 (symbol-value variable) (current-buffer))
16535         (insert ")\n")))))
16536
16537 (defun gnus-gnus-to-newsrc-format ()
16538   ;; Generate and save the .newsrc file.
16539   (save-excursion
16540     (set-buffer (create-file-buffer gnus-current-startup-file))
16541     (let ((newsrc (cdr gnus-newsrc-alist))
16542           (standard-output (current-buffer))
16543           info ranges range method)
16544       (setq buffer-file-name gnus-current-startup-file)
16545       (buffer-disable-undo (current-buffer))
16546       (erase-buffer)
16547       ;; Write options.
16548       (if gnus-newsrc-options (insert gnus-newsrc-options))
16549       ;; Write subscribed and unsubscribed.
16550       (while (setq info (pop newsrc))
16551         ;; Don't write foreign groups to .newsrc.
16552         (when (or (null (setq method (gnus-info-method info)))
16553                   (equal method "native")
16554                   (gnus-server-equal method gnus-select-method))
16555           (insert (gnus-info-group info)
16556                   (if (> (gnus-info-level info) gnus-level-subscribed)
16557                       "!" ":"))
16558           (when (setq ranges (gnus-info-read info))
16559             (insert " ")
16560             (if (not (listp (cdr ranges)))
16561                 (if (= (car ranges) (cdr ranges))
16562                     (princ (car ranges))
16563                   (princ (car ranges))
16564                   (insert "-")
16565                   (princ (cdr ranges)))
16566               (while (setq range (pop ranges))
16567                 (if (or (atom range) (= (car range) (cdr range)))
16568                     (princ (or (and (atom range) range) (car range)))
16569                   (princ (car range))
16570                   (insert "-")
16571                   (princ (cdr range)))
16572                 (if ranges (insert ",")))))
16573           (insert "\n")))
16574       (make-local-variable 'version-control)
16575       (setq version-control 'never)
16576       ;; It has been reported that sometime the modtime on the .newsrc
16577       ;; file seems to be off.  We really do want to overwrite it, so
16578       ;; we clear the modtime here before saving.  It's a bit odd,
16579       ;; though...
16580       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16581       ;; delete the silly thing entirely first.  but this fails to provide
16582       ;; such niceties as .newsrc~ creation.
16583       (if gnus-modtime-botch
16584           (delete-file gnus-startup-file)
16585         (clear-visited-file-modtime))
16586       (run-hooks 'gnus-save-standard-newsrc-hook)
16587       (save-buffer)
16588       (kill-buffer (current-buffer)))))
16589
16590 \f
16591 ;;;
16592 ;;; Slave functions.
16593 ;;;
16594
16595 (defun gnus-slave-save-newsrc ()
16596   (save-excursion
16597     (set-buffer gnus-dribble-buffer)
16598     (let ((slave-name
16599            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16600       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16601
16602 (defun gnus-master-read-slave-newsrc ()
16603   (let ((slave-files
16604          (directory-files
16605           (file-name-directory gnus-current-startup-file)
16606           t (concat
16607              "^" (regexp-quote
16608                   (concat
16609                    (file-name-nondirectory gnus-current-startup-file)
16610                    "-slave-")))
16611           t))
16612         file)
16613     (if (not slave-files)
16614         ()                              ; There are no slave files to read.
16615       (gnus-message 7 "Reading slave newsrcs...")
16616       (save-excursion
16617         (set-buffer (get-buffer-create " *gnus slave*"))
16618         (buffer-disable-undo (current-buffer))
16619         (setq slave-files
16620               (sort (mapcar (lambda (file)
16621                               (list (nth 5 (file-attributes file)) file))
16622                             slave-files)
16623                     (lambda (f1 f2)
16624                       (or (< (caar f1) (caar f2))
16625                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16626         (while slave-files
16627           (erase-buffer)
16628           (setq file (nth 1 (car slave-files)))
16629           (insert-file-contents file)
16630           (if (condition-case ()
16631                   (progn
16632                     (eval-buffer (current-buffer))
16633                     t)
16634                 (error
16635                  (gnus-error 3.2 "Possible error in %s" file)
16636                  nil))
16637               (or gnus-slave ; Slaves shouldn't delete these files.
16638                   (condition-case ()
16639                       (delete-file file)
16640                     (error nil))))
16641           (setq slave-files (cdr slave-files))))
16642       (gnus-message 7 "Reading slave newsrcs...done"))))
16643
16644 \f
16645 ;;;
16646 ;;; Group description.
16647 ;;;
16648
16649 (defun gnus-read-all-descriptions-files ()
16650   (let ((methods (cons gnus-select-method 
16651                        (nconc
16652                         (when gnus-message-archive-method
16653                           (list "archive"))
16654                         gnus-secondary-select-methods))))
16655     (while methods
16656       (gnus-read-descriptions-file (car methods))
16657       (setq methods (cdr methods)))
16658     t))
16659
16660 (defun gnus-read-descriptions-file (&optional method)
16661   (let ((method (or method gnus-select-method)))
16662     (when (stringp method)
16663       (setq method (gnus-server-to-method method)))
16664     ;; We create the hashtable whether we manage to read the desc file
16665     ;; to avoid trying to re-read after a failed read.
16666     (or gnus-description-hashtb
16667         (setq gnus-description-hashtb
16668               (gnus-make-hashtable (length gnus-active-hashtb))))
16669     ;; Mark this method's desc file as read.
16670     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16671                   gnus-description-hashtb)
16672
16673     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16674     (cond
16675      ((not (gnus-check-server method))
16676       (gnus-message 1 "Couldn't open server")
16677       nil)
16678      ((not (gnus-request-list-newsgroups method))
16679       (gnus-message 1 "Couldn't read newsgroups descriptions")
16680       nil)
16681      (t
16682       (let (group)
16683         (save-excursion
16684           (save-restriction
16685             (set-buffer nntp-server-buffer)
16686             (goto-char (point-min))
16687             (if (or (search-forward "\n.\n" nil t)
16688                     (goto-char (point-max)))
16689                 (progn
16690                   (beginning-of-line)
16691                   (narrow-to-region (point-min) (point))))
16692             (goto-char (point-min))
16693             (while (not (eobp))
16694               ;; If we get an error, we set group to 0, which is not a
16695               ;; symbol...
16696               (setq group
16697                     (condition-case ()
16698                         (let ((obarray gnus-description-hashtb))
16699                           ;; Group is set to a symbol interned in this
16700                           ;; hash table.
16701                           (read nntp-server-buffer))
16702                       (error 0)))
16703               (skip-chars-forward " \t")
16704               ;; ...  which leads to this line being effectively ignored.
16705               (and (symbolp group)
16706                    (set group (buffer-substring
16707                                (point) (progn (end-of-line) (point)))))
16708               (forward-line 1))))
16709         (gnus-message 5 "Reading descriptions file...done")
16710         t)))))
16711
16712 (defun gnus-group-get-description (group)
16713   "Get the description of a group by sending XGTITLE to the server."
16714   (when (gnus-request-group-description group)
16715     (save-excursion
16716       (set-buffer nntp-server-buffer)
16717       (goto-char (point-min))
16718       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16719         (match-string 1)))))
16720
16721 \f
16722 ;;;
16723 ;;; Buffering of read articles.
16724 ;;;
16725
16726 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16727 (defvar gnus-backlog-articles nil)
16728 (defvar gnus-backlog-hashtb nil)
16729
16730 (defun gnus-backlog-buffer ()
16731   "Return the backlog buffer."
16732   (or (get-buffer gnus-backlog-buffer)
16733       (save-excursion
16734         (set-buffer (get-buffer-create gnus-backlog-buffer))
16735         (buffer-disable-undo (current-buffer))
16736         (setq buffer-read-only t)
16737         (gnus-add-current-to-buffer-list)
16738         (get-buffer gnus-backlog-buffer))))
16739
16740 (defun gnus-backlog-setup ()
16741   "Initialize backlog variables."
16742   (unless gnus-backlog-hashtb
16743     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16744
16745 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16746
16747 (defun gnus-backlog-shutdown ()
16748   "Clear all backlog variables and buffers."
16749   (when (get-buffer gnus-backlog-buffer)
16750     (kill-buffer gnus-backlog-buffer))
16751   (setq gnus-backlog-hashtb nil
16752         gnus-backlog-articles nil))
16753
16754 (defun gnus-backlog-enter-article (group number buffer)
16755   (gnus-backlog-setup)
16756   (let ((ident (intern (concat group ":" (int-to-string number))
16757                        gnus-backlog-hashtb))
16758         b)
16759     (if (memq ident gnus-backlog-articles)
16760         () ; It's already kept.
16761       ;; Remove the oldest article, if necessary.
16762       (and (numberp gnus-keep-backlog)
16763            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16764            (gnus-backlog-remove-oldest-article))
16765       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16766       ;; Insert the new article.
16767       (save-excursion
16768         (set-buffer (gnus-backlog-buffer))
16769         (let (buffer-read-only)
16770           (goto-char (point-max))
16771           (or (bolp) (insert "\n"))
16772           (setq b (point))
16773           (insert-buffer-substring buffer)
16774           ;; Tag the beginning of the article with the ident.
16775           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16776
16777 (defun gnus-backlog-remove-oldest-article ()
16778   (save-excursion
16779     (set-buffer (gnus-backlog-buffer))
16780     (goto-char (point-min))
16781     (if (zerop (buffer-size))
16782         () ; The buffer is empty.
16783       (let ((ident (get-text-property (point) 'gnus-backlog))
16784             buffer-read-only)
16785         ;; Remove the ident from the list of articles.
16786         (when ident
16787           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16788         ;; Delete the article itself.
16789         (delete-region
16790          (point) (next-single-property-change
16791                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16792
16793 (defun gnus-backlog-request-article (group number buffer)
16794   (when (numberp number)
16795     (gnus-backlog-setup)
16796     (let ((ident (intern (concat group ":" (int-to-string number))
16797                          gnus-backlog-hashtb))
16798           beg end)
16799       (when (memq ident gnus-backlog-articles)
16800         ;; It was in the backlog.
16801         (save-excursion
16802           (set-buffer (gnus-backlog-buffer))
16803           (if (not (setq beg (text-property-any
16804                               (point-min) (point-max) 'gnus-backlog
16805                               ident)))
16806               ;; It wasn't in the backlog after all.
16807               (ignore
16808                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16809             ;; Find the end (i. e., the beginning of the next article).
16810             (setq end
16811                   (next-single-property-change
16812                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16813         (let ((buffer-read-only nil))
16814           (erase-buffer)
16815           (insert-buffer-substring gnus-backlog-buffer beg end)
16816           t)))))
16817
16818 ;; Allow redefinition of Gnus functions.
16819
16820 (gnus-ems-redefine)
16821
16822 (provide 'gnus)
16823
16824 ;;; gnus.el ends here