*** 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 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
39   "*Directory variable from which all other Gnus file variables are derived.")
40
41 ;; Site dependent variables.  These variables should be defined in
42 ;; paths.el.
43
44 (defvar gnus-default-nntp-server nil
45   "Specify a default NNTP server.
46 This variable should be defined in paths.el, and should never be set
47 by the user.
48 If you want to change servers, you should use `gnus-select-method'.
49 See the documentation to that variable.")
50
51 (defvar gnus-backup-default-subscribed-newsgroups
52   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
53   "Default default new newsgroups the first time Gnus is run.
54 Should be set in paths.el, and shouldn't be touched by the user.")
55
56 (defvar gnus-local-domain nil
57   "Local domain name without a host name.
58 The DOMAINNAME environment variable is used instead if it is defined.
59 If the `system-name' function returns the full Internet name, there is
60 no need to set this variable.")
61
62 (defvar gnus-local-organization nil
63   "String with a description of what organization (if any) the user belongs to.
64 The ORGANIZATION environment variable is used instead if it is defined.
65 If this variable contains a function, this function will be called
66 with the current newsgroup name as the argument.  The function should
67 return a string.
68
69 In any case, if the string (either in the variable, in the environment
70 variable, or returned by the function) is a file name, the contents of
71 this file will be used as the organization.")
72
73 ;; Customization variables
74
75 ;; Don't touch this variable.
76 (defvar gnus-nntp-service "nntp"
77   "*NNTP service name (\"nntp\" or 119).
78 This is an obsolete variable, which is scarcely used.  If you use an
79 nntp server for your newsgroup and want to change the port number
80 used to 899, you would say something along these lines:
81
82  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
83
84 (defvar gnus-nntpserver-file "/etc/nntpserver"
85   "*A file with only the name of the nntp server in it.")
86
87 ;; This function is used to check both the environment variable
88 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
89 ;; an nntp server name default.
90 (defun gnus-getenv-nntpserver ()
91   (or (getenv "NNTPSERVER")
92       (and (file-readable-p gnus-nntpserver-file)
93            (save-excursion
94              (set-buffer (get-buffer-create " *gnus nntp*"))
95              (buffer-disable-undo (current-buffer))
96              (insert-file-contents gnus-nntpserver-file)
97              (let ((name (buffer-string)))
98                (prog1
99                    (if (string-match "^[ \t\n]*$" name)
100                        nil
101                      name)
102                  (kill-buffer (current-buffer))))))))
103
104 (defvar gnus-select-method
105   (nconc
106    (list 'nntp (or (condition-case ()
107                        (gnus-getenv-nntpserver)
108                      (error nil))
109                    (if (and gnus-default-nntp-server
110                             (not (string= gnus-default-nntp-server "")))
111                        gnus-default-nntp-server)
112                    (system-name)))
113    (if (or (null gnus-nntp-service)
114            (equal gnus-nntp-service "nntp"))
115        nil
116      (list gnus-nntp-service)))
117   "*Default method for selecting a newsgroup.
118 This variable should be a list, where the first element is how the
119 news is to be fetched, the second is the address.
120
121 For instance, if you want to get your news via NNTP from
122 \"flab.flab.edu\", you could say:
123
124 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
125
126 If you want to use your local spool, say:
127
128 (setq gnus-select-method (list 'nnspool (system-name)))
129
130 If you use this variable, you must set `gnus-nntp-server' to nil.
131
132 There is a lot more to know about select methods and virtual servers -
133 see the manual for details.")
134
135 (defvar gnus-message-archive-method 
136   '(nnfolder
137     "archive"
138     (nnfolder-directory (nnheader-concat message-directory "archive"))
139     (nnfolder-active-file (nnheader-concat message-directory "archive/active"))
140     (nnfolder-get-new-mail nil)
141     (nnfolder-inhibit-expiry t))
142   "*Method used for archiving messages you've sent.
143 This should be a mail method.")
144
145 (defvar gnus-refer-article-method nil
146   "*Preferred method for fetching an article by Message-ID.
147 If you are reading news from the local spool (with nnspool), fetching
148 articles by Message-ID is painfully slow.  By setting this method to an
149 nntp method, you might get acceptable results.
150
151 The value of this variable must be a valid select method as discussed
152 in the documentation of `gnus-select-method'.")
153
154 (defvar gnus-secondary-select-methods nil
155   "*A list of secondary methods that will be used for reading news.
156 This is a list where each element is a complete select method (see
157 `gnus-select-method').
158
159 If, for instance, you want to read your mail with the nnml backend,
160 you could set this variable:
161
162 (setq gnus-secondary-select-methods '((nnml \"\")))")
163
164 (defvar gnus-secondary-servers nil
165   "*List of NNTP servers that the user can choose between interactively.
166 To make Gnus query you for a server, you have to give `gnus' a
167 non-numeric prefix - `C-u M-x gnus', in short.")
168
169 (defvar gnus-nntp-server nil
170   "*The name of the host running the NNTP server.
171 This variable is semi-obsolete.  Use the `gnus-select-method'
172 variable instead.")
173
174 (defvar gnus-startup-file "~/.newsrc"
175   "*Your `.newsrc' file.
176 `.newsrc-SERVER' will be used instead if that exists.")
177
178 (defvar gnus-init-file "~/.gnus"
179   "*Your Gnus elisp startup file.
180 If a file with the .el or .elc suffixes exist, it will be read
181 instead.")
182
183 (defvar gnus-group-faq-directory
184   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
185     "/ftp@sunsite.auc.dk:/pub/usenet/"
186     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
187     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
188     "/ftp@rtfm.mit.edu:/pub/usenet/"
189     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
190     "/ftp@ftp.sunet.se:/pub/usenet/"
191     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
192     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
193     "/ftp@ftp.hk.super.net:/mirror/faqs/")
194   "*Directory where the group FAQs are stored.
195 This will most commonly be on a remote machine, and the file will be
196 fetched by ange-ftp.
197
198 This variable can also be a list of directories.  In that case, the
199 first element in the list will be used by default, and the others will
200 be used as backup sites.
201
202 Note that Gnus uses an aol machine as the default directory.  If this
203 feels fundamentally unclean, just think of it as a way to finally get
204 something of value back from them.
205
206 If the default site is too slow, try one of these:
207
208    North America: mirrors.aol.com                /pub/rtfm/usenet
209                   ftp.seas.gwu.edu               /pub/rtfm
210                   rtfm.mit.edu                   /pub/usenet
211    Europe:        ftp.uni-paderborn.de           /pub/FAQ
212                   src.doc.ic.ac.uk               /usenet/news-FAQS
213                   ftp.sunet.se                   /pub/usenet
214                   sunsite.auc.dk                 /pub/usenet
215    Asia:          nctuccca.edu.tw                /USENET/FAQ
216                   hwarang.postech.ac.kr          /pub/usenet
217                   ftp.hk.super.net               /mirror/faqs")
218
219 (defvar gnus-group-archive-directory
220   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
221   "*The address of the (ding) archives.")
222
223 (defvar gnus-group-recent-archive-directory
224   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
225   "*The address of the most recent (ding) articles.")
226
227 (defvar gnus-default-subscribed-newsgroups nil
228   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
229 It should be a list of strings.
230 If it is `t', Gnus will not do anything special the first time it is
231 started; it'll just use the normal newsgroups subscription methods.")
232
233 (defvar gnus-use-cross-reference t
234   "*Non-nil means that cross referenced articles will be marked as read.
235 If nil, ignore cross references.  If t, mark articles as read in
236 subscribed newsgroups.  If neither t nor nil, mark as read in all
237 newsgroups.")
238
239 (defvar gnus-single-article-buffer t
240   "*If non-nil, display all articles in the same buffer.
241 If nil, each group will get its own article buffer.")
242
243 (defvar gnus-use-dribble-file t
244   "*Non-nil means that Gnus will use a dribble file to store user updates.
245 If Emacs should crash without saving the .newsrc files, complete
246 information can be restored from the dribble file.")
247
248 (defvar gnus-dribble-directory nil
249   "*The directory where dribble files will be saved.
250 If this variable is nil, the directory where the .newsrc files are
251 saved will be used.")
252
253 (defvar gnus-asynchronous nil
254   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
255
256 (defvar gnus-kill-summary-on-exit t
257   "*If non-nil, kill the summary buffer when you exit from it.
258 If nil, the summary will become a \"*Dead Summary*\" buffer, and
259 it will be killed sometime later.")
260
261 (defvar gnus-large-newsgroup 200
262   "*The number of articles which indicates a large newsgroup.
263 If the number of articles in a newsgroup is greater than this value,
264 confirmation is required for selecting the newsgroup.")
265
266 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
267 (defvar gnus-no-groups-message "No news is horrible news"
268   "*Message displayed by Gnus when no groups are available.")
269
270 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
271   "*Non-nil means that the default name of a file to save articles in is the group name.
272 If it's nil, the directory form of the group name is used instead.
273
274 If this variable is a list, and the list contains the element
275 `not-score', long file names will not be used for score files; if it
276 contains the element `not-save', long file names will not be used for
277 saving; and if it contains the element `not-kill', long file names
278 will not be used for kill files.")
279
280 (defvar gnus-article-save-directory gnus-directory
281   "*Name of the directory articles will be saved in (default \"~/News\").")
282
283 (defvar gnus-kill-files-directory gnus-directory
284   "*Name of the directory where kill files will be stored (default \"~/News\").")
285
286 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
287   "*A function to save articles in your favorite format.
288 The function must be interactively callable (in other words, it must
289 be an Emacs command).
290
291 Gnus provides the following functions:
292
293 * gnus-summary-save-in-rmail (Rmail format)
294 * gnus-summary-save-in-mail (Unix mail format)
295 * gnus-summary-save-in-folder (MH folder)
296 * gnus-summary-save-in-file (article format).
297 * gnus-summary-save-in-vm (use VM's folder format).")
298
299 (defvar gnus-prompt-before-saving 'always
300   "*This variable says how much prompting is to be done when saving articles.
301 If it is nil, no prompting will be done, and the articles will be
302 saved to the default files.  If this variable is `always', each and
303 every article that is saved will be preceded by a prompt, even when
304 saving large batches of articles.  If this variable is neither nil not
305 `always', there the user will be prompted once for a file name for
306 each invocation of the saving commands.")
307
308 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
309   "*A function generating a file name to save articles in Rmail format.
310 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
311
312 (defvar gnus-mail-save-name (function gnus-plain-save-name)
313   "*A function generating a file name to save articles in Unix mail format.
314 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
315
316 (defvar gnus-folder-save-name (function gnus-folder-save-name)
317   "*A function generating a file name to save articles in MH folder.
318 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
319
320 (defvar gnus-file-save-name (function gnus-numeric-save-name)
321   "*A function generating a file name to save articles in article format.
322 The function is called with NEWSGROUP, HEADERS, and optional
323 LAST-FILE.")
324
325 (defvar gnus-split-methods
326   '((gnus-article-archive-name))
327   "*Variable used to suggest where articles are to be saved.
328 For instance, if you would like to save articles related to Gnus in
329 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
330 you could set this variable to something like:
331
332  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
333    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
334
335 This variable is an alist where the where the key is the match and the
336 value is a list of possible files to save in if the match is non-nil.
337
338 If the match is a string, it is used as a regexp match on the
339 article.  If the match is a symbol, that symbol will be funcalled
340 from the buffer of the article to be saved with the newsgroup as the
341 parameter.  If it is a list, it will be evaled in the same buffer.
342
343 If this form or function returns a string, this string will be used as
344 a possible file name; and if it returns a non-nil list, that list will
345 be used as possible file names.")
346
347 (defvar gnus-move-split-methods nil
348   "*Variable used to suggest where articles are to be moved to.
349 It uses the same syntax as the `gnus-split-methods' variable.")
350
351 (defvar gnus-save-score nil
352   "*If non-nil, save group scoring info.")
353
354 (defvar gnus-use-adaptive-scoring nil
355   "*If non-nil, use some adaptive scoring scheme.")
356
357 (defvar gnus-use-cache nil
358   "*If nil, Gnus will ignore the article cache.
359 If `passive', it will allow entering (and reading) articles
360 explicitly entered into the cache.  If anything else, use the
361 cache to the full extent of the law.")
362
363 (defvar gnus-use-trees nil
364   "*If non-nil, display a thread tree buffer.")
365
366 (defvar gnus-use-grouplens nil
367   "*If non-nil, use GroupLens ratings.")
368
369 (defvar gnus-keep-backlog nil
370   "*If non-nil, Gnus will keep read articles for later re-retrieval.
371 If it is a number N, then Gnus will only keep the last N articles
372 read.  If it is neither nil nor a number, Gnus will keep all read
373 articles.  This is not a good idea.")
374
375 (defvar gnus-use-nocem nil
376   "*If non-nil, Gnus will read NoCeM cancel messages.")
377
378 (defvar gnus-use-demon nil
379   "If non-nil, Gnus might use some demons.")
380
381 (defvar gnus-use-scoring t
382   "*If non-nil, enable scoring.")
383
384 (defvar gnus-use-picons nil
385   "*If non-nil, display picons.")
386
387 (defvar gnus-fetch-old-headers nil
388   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
389 If an unread article in the group refers to an older, already read (or
390 just marked as read) article, the old article will not normally be
391 displayed in the Summary buffer.  If this variable is non-nil, Gnus
392 will attempt to grab the headers to the old articles, and thereby
393 build complete threads.  If it has the value `some', only enough
394 headers to connect otherwise loose threads will be displayed.
395 This variable can also be a number.  In that case, no more than that
396 number of old headers will be fetched.
397
398 The server has to support NOV for any of this to work.")
399
400 ;see gnus-cus.el
401 ;(defvar gnus-visual t
402 ;  "*If non-nil, will do various highlighting.
403 ;If nil, no mouse highlights (or any other highlights) will be
404 ;performed.  This might speed up Gnus some when generating large group
405 ;and summary buffers.")
406
407 (defvar gnus-novice-user t
408   "*Non-nil means that you are a usenet novice.
409 If non-nil, verbose messages may be displayed and confirmations may be
410 required.")
411
412 (defvar gnus-expert-user nil
413   "*Non-nil means that you will never be asked for confirmation about anything.
414 And that means *anything*.")
415
416 (defvar gnus-verbose 7
417   "*Integer that says how verbose Gnus should be.
418 The higher the number, the more messages Gnus will flash to say what
419 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
420 display most important messages; and at ten, Gnus will keep on
421 jabbering all the time.")
422
423 (defvar gnus-keep-same-level nil
424   "*Non-nil means that the next newsgroup after the current will be on the same level.
425 When you type, for instance, `n' after reading the last article in the
426 current newsgroup, you will go to the next newsgroup.  If this variable
427 is nil, the next newsgroup will be the next from the group
428 buffer.
429 If this variable is non-nil, Gnus will either put you in the
430 next newsgroup with the same level, or, if no such newsgroup is
431 available, the next newsgroup with the lowest possible level higher
432 than the current level.
433 If this variable is `best', Gnus will make the next newsgroup the one
434 with the best level.")
435
436 (defvar gnus-summary-make-false-root 'adopt
437   "*nil means that Gnus won't gather loose threads.
438 If the root of a thread has expired or been read in a previous
439 session, the information necessary to build a complete thread has been
440 lost.  Instead of having many small sub-threads from this original thread
441 scattered all over the summary buffer, Gnus can gather them.
442
443 If non-nil, Gnus will try to gather all loose sub-threads from an
444 original thread into one large thread.
445
446 If this variable is non-nil, it should be one of `none', `adopt',
447 `dummy' or `empty'.
448
449 If this variable is `none', Gnus will not make a false root, but just
450 present the sub-threads after another.
451 If this variable is `dummy', Gnus will create a dummy root that will
452 have all the sub-threads as children.
453 If this variable is `adopt', Gnus will make one of the \"children\"
454 the parent and mark all the step-children as such.
455 If this variable is `empty', the \"children\" are printed with empty
456 subject fields.  (Or rather, they will be printed with a string
457 given by the `gnus-summary-same-subject' variable.)")
458
459 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
460   "*A regexp to match subjects to be excluded from loose thread gathering.
461 As loose thread gathering is done on subjects only, that means that
462 there can be many false gatherings performed.  By rooting out certain
463 common subjects, gathering might become saner.")
464
465 (defvar gnus-summary-gather-subject-limit nil
466   "*Maximum length of subject comparisons when gathering loose threads.
467 Use nil to compare full subjects.  Setting this variable to a low
468 number will help gather threads that have been corrupted by
469 newsreaders chopping off subject lines, but it might also mean that
470 unrelated articles that have subject that happen to begin with the
471 same few characters will be incorrectly gathered.
472
473 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
474 comparing subjects.")
475
476 (defvar gnus-simplify-ignored-prefixes nil
477   "*Regexp, matches for which are removed from subject lines when simplifying.")
478
479 (defvar gnus-build-sparse-threads nil
480   "*If non-nil, fill in the gaps in threads.
481 If `some', only fill in the gaps that are needed to tie loose threads
482 together.  If `more', fill in all leaf nodes that Gnus can find.  If
483 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
484
485 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
486   "Function used for gathering loose threads.
487 There are two pre-defined functions: `gnus-gather-threads-by-subject',
488 which only takes Subjects into consideration; and
489 `gnus-gather-threads-by-references', which compared the References
490 headers of the articles to find matches.")
491
492 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
493 (defvar gnus-summary-same-subject ""
494   "*String indicating that the current article has the same subject as the previous.
495 This variable will only be used if the value of
496 `gnus-summary-make-false-root' is `empty'.")
497
498 (defvar gnus-summary-goto-unread t
499   "*If non-nil, marking commands will go to the next unread article.
500 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
501 whether it is read or not.")
502
503 (defvar gnus-group-goto-unread t
504   "*If non-nil, movement commands will go to the next unread and subscribed group.")
505
506 (defvar gnus-goto-next-group-when-activating t
507   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
508
509 (defvar gnus-check-new-newsgroups t
510   "*Non-nil means that Gnus will add new newsgroups at startup.
511 If this variable is `ask-server', Gnus will ask the server for new
512 groups since the last time it checked.  This means that the killed list
513 is no longer necessary, so you could set `gnus-save-killed-list' to
514 nil.
515
516 A variant is to have this variable be a list of select methods.  Gnus
517 will then use the `ask-server' method on all these select methods to
518 query for new groups from all those servers.
519
520 Eg.
521   (setq gnus-check-new-newsgroups
522         '((nntp \"some.server\") (nntp \"other.server\")))
523
524 If this variable is nil, then you have to tell Gnus explicitly to
525 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
526
527 (defvar gnus-check-bogus-newsgroups nil
528   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
529 If this variable is nil, then you have to tell Gnus explicitly to
530 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
531
532 (defvar gnus-read-active-file t
533   "*Non-nil means that Gnus will read the entire active file at startup.
534 If this variable is nil, Gnus will only know about the groups in your
535 `.newsrc' file.
536
537 If this variable is `some', Gnus will try to only read the relevant
538 parts of the active file from the server.  Not all servers support
539 this, and it might be quite slow with other servers, but this should
540 generally be faster than both the t and nil value.
541
542 If you set this variable to nil or `some', you probably still want to
543 be told about new newsgroups that arrive.  To do that, set
544 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
545 properly with all servers.")
546
547 (defvar gnus-level-subscribed 5
548   "*Groups with levels less than or equal to this variable are subscribed.")
549
550 (defvar gnus-level-unsubscribed 7
551   "*Groups with levels less than or equal to this variable are unsubscribed.
552 Groups with levels less than `gnus-level-subscribed', which should be
553 less than this variable, are subscribed.")
554
555 (defvar gnus-level-zombie 8
556   "*Groups with this level are zombie groups.")
557
558 (defvar gnus-level-killed 9
559   "*Groups with this level are killed.")
560
561 (defvar gnus-level-default-subscribed 3
562   "*New subscribed groups will be subscribed at this level.")
563
564 (defvar gnus-level-default-unsubscribed 6
565   "*New unsubscribed groups will be unsubscribed at this level.")
566
567 (defvar gnus-activate-level (1+ gnus-level-subscribed)
568   "*Groups higher than this level won't be activated on startup.
569 Setting this variable to something log might save lots of time when
570 you have many groups that you aren't interested in.")
571
572 (defvar gnus-activate-foreign-newsgroups 4
573   "*If nil, Gnus will not check foreign newsgroups at startup.
574 If it is non-nil, it should be a number between one and nine.  Foreign
575 newsgroups that have a level lower or equal to this number will be
576 activated on startup.  For instance, if you want to active all
577 subscribed newsgroups, but not the rest, you'd set this variable to
578 `gnus-level-subscribed'.
579
580 If you subscribe to lots of newsgroups from different servers, startup
581 might take a while.  By setting this variable to nil, you'll save time,
582 but you won't be told how many unread articles there are in the
583 groups.")
584
585 (defvar gnus-save-newsrc-file t
586   "*Non-nil means that Gnus will save the `.newsrc' file.
587 Gnus always saves its own startup file, which is called
588 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
589 be readily understood by other newsreaders.  If you don't plan on
590 using other newsreaders, set this variable to nil to save some time on
591 exit.")
592
593 (defvar gnus-save-killed-list t
594   "*If non-nil, save the list of killed groups to the startup file.
595 If you set this variable to nil, you'll save both time (when starting
596 and quitting) and space (both memory and disk), but it will also mean
597 that Gnus has no record of which groups are new and which are old, so
598 the automatic new newsgroups subscription methods become meaningless.
599
600 You should always set `gnus-check-new-newsgroups' to `ask-server' or
601 nil if you set this variable to nil.")
602
603 (defvar gnus-interactive-catchup t
604   "*If non-nil, require your confirmation when catching up a group.")
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 `gnus-subscribe-zombies' will make all new groups into zombies.")
974
975 ;; Suggested by a bug report by Hallvard B Furuseth.
976 ;; <h.b.furuseth@usit.uio.no>.
977 (defvar gnus-subscribe-options-newsgroup-method
978   (function gnus-subscribe-alphabetically)
979   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
980 If, for instance, you want to subscribe to all newsgroups in the
981 \"no\" and \"alt\" hierarchies, you'd put the following in your
982 .newsrc file:
983
984 options -n no.all alt.all
985
986 Gnus will the subscribe all new newsgroups in these hierarchies with
987 the subscription method in this variable.")
988
989 (defvar gnus-subscribe-hierarchical-interactive nil
990   "*If non-nil, Gnus will offer to subscribe hierarchically.
991 When a new hierarchy appears, Gnus will ask the user:
992
993 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
994
995 If the user pressed `d', Gnus will descend the hierarchy, `y' will
996 subscribe to all newsgroups in the hierarchy and `s' will skip this
997 hierarchy in its entirety.")
998
999 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1000   "*Function used for sorting the group buffer.
1001 This function will be called with group info entries as the arguments
1002 for the groups to be sorted.  Pre-made functions include
1003 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1004 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1005 `gnus-group-sort-by-rank'.
1006
1007 This variable can also be a list of sorting functions.  In that case,
1008 the most significant sort function should be the last function in the
1009 list.")
1010
1011 ;; Mark variables suggested by Thomas Michanek
1012 ;; <Thomas.Michanek@telelogic.se>.
1013 (defvar gnus-unread-mark ? 
1014   "*Mark used for unread articles.")
1015 (defvar gnus-ticked-mark ?!
1016   "*Mark used for ticked articles.")
1017 (defvar gnus-dormant-mark ??
1018   "*Mark used for dormant articles.")
1019 (defvar gnus-del-mark ?r
1020   "*Mark used for del'd articles.")
1021 (defvar gnus-read-mark ?R
1022   "*Mark used for read articles.")
1023 (defvar gnus-expirable-mark ?E
1024   "*Mark used for expirable articles.")
1025 (defvar gnus-killed-mark ?K
1026   "*Mark used for killed articles.")
1027 (defvar gnus-souped-mark ?F
1028   "*Mark used for killed articles.")
1029 (defvar gnus-kill-file-mark ?X
1030   "*Mark used for articles killed by kill files.")
1031 (defvar gnus-low-score-mark ?Y
1032   "*Mark used for articles with a low score.")
1033 (defvar gnus-catchup-mark ?C
1034   "*Mark used for articles that are caught up.")
1035 (defvar gnus-replied-mark ?A
1036   "*Mark used for articles that have been replied to.")
1037 (defvar gnus-cached-mark ?*
1038   "*Mark used for articles that are in the cache.")
1039 (defvar gnus-saved-mark ?S
1040   "*Mark used for articles that have been saved to.")
1041 (defvar gnus-process-mark ?#
1042   "*Process mark.")
1043 (defvar gnus-ancient-mark ?O
1044   "*Mark used for ancient articles.")
1045 (defvar gnus-sparse-mark ?Q
1046   "*Mark used for sparsely reffed articles.")
1047 (defvar gnus-canceled-mark ?G
1048   "*Mark used for canceled articles.")
1049 (defvar gnus-score-over-mark ?+
1050   "*Score mark used for articles with high scores.")
1051 (defvar gnus-score-below-mark ?-
1052   "*Score mark used for articles with low scores.")
1053 (defvar gnus-empty-thread-mark ? 
1054   "*There is no thread under the article.")
1055 (defvar gnus-not-empty-thread-mark ?=
1056   "*There is a thread under the article.")
1057
1058 (defvar gnus-view-pseudo-asynchronously nil
1059   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1060
1061 (defvar gnus-view-pseudos nil
1062   "*If `automatic', pseudo-articles will be viewed automatically.
1063 If `not-confirm', pseudos will be viewed automatically, and the user
1064 will not be asked to confirm the command.")
1065
1066 (defvar gnus-view-pseudos-separately t
1067   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1068 If nil, all files that use the same viewing command will be given as a
1069 list of parameters to that command.")
1070
1071 (defvar gnus-insert-pseudo-articles t
1072   "*If non-nil, insert pseudo-articles when decoding articles.")
1073
1074 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1075   "*Format of group lines.
1076 It works along the same lines as a normal formatting string,
1077 with some simple extensions.
1078
1079 %M    Only marked articles (character, \"*\" or \" \")
1080 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1081 %L    Level of subscribedness (integer)
1082 %N    Number of unread articles (integer)
1083 %I    Number of dormant articles (integer)
1084 %i    Number of ticked and dormant (integer)
1085 %T    Number of ticked articles (integer)
1086 %R    Number of read articles (integer)
1087 %t    Total number of articles (integer)
1088 %y    Number of unread, unticked articles (integer)
1089 %G    Group name (string)
1090 %g    Qualified group name (string)
1091 %D    Group description (string)
1092 %s    Select method (string)
1093 %o    Moderated group (char, \"m\")
1094 %p    Process mark (char)
1095 %O    Moderated group (string, \"(m)\" or \"\")
1096 %P    Topic indentation (string)
1097 %l    Whether there are GroupLens predictions for this group (string)
1098 %n    Select from where (string)
1099 %z    A string that look like `<%s:%n>' if a foreign select method is used
1100 %u    User defined specifier.  The next character in the format string should
1101       be a letter.  Gnus will call the function gnus-user-format-function-X,
1102       where X is the letter following %u.  The function will be passed the
1103       current header as argument.  The function should return a string, which
1104       will be inserted into the buffer just like information from any other
1105       group specifier.
1106
1107 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1108 the mouse point move inside the area.  There can only be one such area.
1109
1110 Note that this format specification is not always respected.  For
1111 reasons of efficiency, when listing killed groups, this specification
1112 is ignored altogether.  If the spec is changed considerably, your
1113 output may end up looking strange when listing both alive and killed
1114 groups.
1115
1116 If you use %o or %O, reading the active file will be slower and quite
1117 a bit of extra memory will be used. %D will also worsen performance.
1118 Also note that if you change the format specification to include any
1119 of these specs, you must probably re-start Gnus to see them go into
1120 effect.")
1121
1122 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1123   "*The format specification of the lines in the summary buffer.
1124
1125 It works along the same lines as a normal formatting string,
1126 with some simple extensions.
1127
1128 %N   Article number, left padded with spaces (string)
1129 %S   Subject (string)
1130 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1131 %n   Name of the poster (string)
1132 %a   Extracted name of the poster (string)
1133 %A   Extracted address of the poster (string)
1134 %F   Contents of the From: header (string)
1135 %x   Contents of the Xref: header (string)
1136 %D   Date of the article (string)
1137 %d   Date of the article (string) in DD-MMM format
1138 %M   Message-id of the article (string)
1139 %r   References of the article (string)
1140 %c   Number of characters in the article (integer)
1141 %L   Number of lines in the article (integer)
1142 %I   Indentation based on thread level (a string of spaces)
1143 %T   A string with two possible values: 80 spaces if the article
1144      is on thread level two or larger and 0 spaces on level one
1145 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1146 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1147 %[   Opening bracket (character, \"[\" or \"<\")
1148 %]   Closing bracket (character, \"]\" or \">\")
1149 %>   Spaces of length thread-level (string)
1150 %<   Spaces of length (- 20 thread-level) (string)
1151 %i   Article score (number)
1152 %z   Article zcore (character)
1153 %t   Number of articles under the current thread (number).
1154 %e   Whether the thread is empty or not (character).
1155 %l   GroupLens score (string).
1156 %u   User defined specifier.  The next character in the format string should
1157      be a letter.  Gnus will call the function gnus-user-format-function-X,
1158      where X is the letter following %u.  The function will be passed the
1159      current header as argument.  The function should return a string, which
1160      will be inserted into the summary just like information from any other
1161      summary specifier.
1162
1163 Text between %( and %) will be highlighted with `gnus-mouse-face'
1164 when the mouse point is placed inside the area.  There can only be one
1165 such area.
1166
1167 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1168 with care.  For reasons of efficiency, Gnus will compute what column
1169 these characters will end up in, and \"hard-code\" that.  This means that
1170 it is illegal to have these specs after a variable-length spec.  Well,
1171 you might not be arrested, but your summary buffer will look strange,
1172 which is bad enough.
1173
1174 The smart choice is to have these specs as for to the left as
1175 possible.
1176
1177 This restriction may disappear in later versions of Gnus.")
1178
1179 (defvar gnus-summary-dummy-line-format
1180   "*  %(:                          :%) %S\n"
1181   "*The format specification for the dummy roots in the summary buffer.
1182 It works along the same lines as a normal formatting string,
1183 with some simple extensions.
1184
1185 %S  The subject")
1186
1187 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1188   "*The format specification for the summary mode line.
1189 It works along the same lines as a normal formatting string,
1190 with some simple extensions:
1191
1192 %G  Group name
1193 %p  Unprefixed group name
1194 %A  Current article number
1195 %V  Gnus version
1196 %U  Number of unread articles in the group
1197 %e  Number of unselected articles in the group
1198 %Z  A string with unread/unselected article counts
1199 %g  Shortish group name
1200 %S  Subject of the current article
1201 %u  User-defined spec
1202 %s  Current score file name
1203 %d  Number of dormant articles
1204 %r  Number of articles that have been marked as read in this session
1205 %E  Number of articles expunged by the score files")
1206
1207 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1208   "*The format specification for the article mode line.
1209 See `gnus-summary-mode-line-format' for a closer description.")
1210
1211 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1212   "*The format specification for the group mode line.
1213 It works along the same lines as a normal formatting string,
1214 with some simple extensions:
1215
1216 %S   The native news server.
1217 %M   The native select method.
1218 %:   \":\" if %S isn't \"\".")
1219
1220 (defvar gnus-valid-select-methods
1221   '(("nntp" post address prompt-address)
1222     ("nnspool" post address)
1223     ("nnvirtual" post-mail virtual prompt-address)
1224     ("nnmbox" mail respool address)
1225     ("nnml" mail respool address)
1226     ("nnmh" mail respool address)
1227     ("nndir" post-mail prompt-address address)
1228     ("nneething" none address prompt-address)
1229     ("nndoc" none address prompt-address)
1230     ("nnbabyl" mail address respool)
1231     ("nnkiboze" post address virtual)
1232     ("nnsoup" post-mail address)
1233     ("nndraft" post-mail)
1234     ("nnfolder" mail respool address))
1235   "An alist of valid select methods.
1236 The first element of each list lists should be a string with the name
1237 of the select method.  The other elements may be be the category of
1238 this method (ie. `post', `mail', `none' or whatever) or other
1239 properties that this method has (like being respoolable).
1240 If you implement a new select method, all you should have to change is
1241 this variable.  I think.")
1242
1243 (defvar gnus-updated-mode-lines '(group article summary tree)
1244   "*List of buffers that should update their mode lines.
1245 The list may contain the symbols `group', `article' and `summary'.  If
1246 the corresponding symbol is present, Gnus will keep that mode line
1247 updated with information that may be pertinent.
1248 If this variable is nil, screen refresh may be quicker.")
1249
1250 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1251 (defvar gnus-mode-non-string-length nil
1252   "*Max length of mode-line non-string contents.
1253 If this is nil, Gnus will take space as is needed, leaving the rest
1254 of the modeline intact.")
1255
1256 ;see gnus-cus.el
1257 ;(defvar gnus-mouse-face 'highlight
1258 ;  "*Face used for mouse highlighting in Gnus.
1259 ;No mouse highlights will be done if `gnus-visual' is nil.")
1260
1261 (defvar gnus-summary-mark-below nil
1262   "*Mark all articles with a score below this variable as read.
1263 This variable is local to each summary buffer and usually set by the
1264 score file.")
1265
1266 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1267   "*List of functions used for sorting articles in the summary buffer.
1268 This variable is only used when not using a threaded display.")
1269
1270 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1271   "*List of functions used for sorting threads in the summary buffer.
1272 By default, threads are sorted by article number.
1273
1274 Each function takes two threads and return non-nil if the first thread
1275 should be sorted before the other.  If you use more than one function,
1276 the primary sort function should be the last.  You should probably
1277 always include `gnus-thread-sort-by-number' in the list of sorting
1278 functions -- preferably first.
1279
1280 Ready-mady functions include `gnus-thread-sort-by-number',
1281 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1282 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1283 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1284
1285 (defvar gnus-thread-score-function '+
1286   "*Function used for calculating the total score of a thread.
1287
1288 The function is called with the scores of the article and each
1289 subthread and should then return the score of the thread.
1290
1291 Some functions you can use are `+', `max', or `min'.")
1292
1293 (defvar gnus-summary-expunge-below nil
1294   "All articles that have a score less than this variable will be expunged.")
1295
1296 (defvar gnus-thread-expunge-below nil
1297   "All threads that have a total score less than this variable will be expunged.
1298 See `gnus-thread-score-function' for en explanation of what a
1299 \"thread score\" is.")
1300
1301 (defvar gnus-auto-subscribed-groups
1302   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1303   "*All new groups that match this regexp will be subscribed automatically.
1304 Note that this variable only deals with new groups.  It has no effect
1305 whatsoever on old groups.")
1306
1307 (defvar gnus-options-subscribe nil
1308   "*All new groups matching this regexp will be subscribed unconditionally.
1309 Note that this variable deals only with new newsgroups.  This variable
1310 does not affect old newsgroups.")
1311
1312 (defvar gnus-options-not-subscribe nil
1313   "*All new groups matching this regexp will be ignored.
1314 Note that this variable deals only with new newsgroups.  This variable
1315 does not affect old (already subscribed) newsgroups.")
1316
1317 (defvar gnus-auto-expirable-newsgroups nil
1318   "*Groups in which to automatically mark read articles as expirable.
1319 If non-nil, this should be a regexp that should match all groups in
1320 which to perform auto-expiry.  This only makes sense for mail groups.")
1321
1322 (defvar gnus-total-expirable-newsgroups nil
1323   "*Groups in which to perform expiry of all read articles.
1324 Use with extreme caution.  All groups that match this regexp will be
1325 expiring - which means that all read articles will be deleted after
1326 (say) one week.  (This only goes for mail groups and the like, of
1327 course.)")
1328
1329 (defvar gnus-group-uncollapsed-levels 1
1330   "Number of group name elements to leave alone when making a short group name.")
1331
1332 (defvar gnus-hidden-properties '(invisible t intangible t)
1333   "Property list to use for hiding text.")
1334
1335 (defvar gnus-modtime-botch nil
1336   "*Non-nil means .newsrc should be deleted prior to save.  
1337 Its use is due to the bogus appearance that .newsrc was modified on
1338 disc.")
1339
1340 ;; Hooks.
1341
1342 (defvar gnus-group-mode-hook nil
1343   "*A hook for Gnus group mode.")
1344
1345 (defvar gnus-summary-mode-hook nil
1346   "*A hook for Gnus summary mode.
1347 This hook is run before any variables are set in the summary buffer.")
1348
1349 (defvar gnus-article-mode-hook nil
1350   "*A hook for Gnus article mode.")
1351
1352 (defvar gnus-summary-prepare-exit-hook nil
1353   "*A hook called when preparing to exit from the summary buffer.
1354 It calls `gnus-summary-expire-articles' by default.")
1355 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1356
1357 (defvar gnus-summary-exit-hook nil
1358   "*A hook called on exit from the summary buffer.")
1359
1360 (defvar gnus-group-catchup-group-hook nil
1361   "*A hook run when catching up a group from the group buffer.")
1362
1363 (defvar gnus-open-server-hook nil
1364   "*A hook called just before opening connection to the news server.")
1365
1366 (defvar gnus-load-hook nil
1367   "*A hook run while Gnus is loaded.")
1368
1369 (defvar gnus-startup-hook nil
1370   "*A hook called at startup.
1371 This hook is called after Gnus is connected to the NNTP server.")
1372
1373 (defvar gnus-get-new-news-hook nil
1374   "*A hook run just before Gnus checks for new news.")
1375
1376 (defvar gnus-after-getting-new-news-hook nil
1377   "*A hook run after Gnus checks for new news.")
1378
1379 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1380   "*A function that is called to generate the group buffer.
1381 The function is called with three arguments: The first is a number;
1382 all group with a level less or equal to that number should be listed,
1383 if the second is non-nil, empty groups should also be displayed.  If
1384 the third is non-nil, it is a number.  No groups with a level lower
1385 than this number should be displayed.
1386
1387 The only current function implemented is `gnus-group-prepare-flat'.")
1388
1389 (defvar gnus-group-prepare-hook nil
1390   "*A hook called after the group buffer has been generated.
1391 If you want to modify the group buffer, you can use this hook.")
1392
1393 (defvar gnus-summary-prepare-hook nil
1394   "*A hook called after the summary buffer has been generated.
1395 If you want to modify the summary buffer, you can use this hook.")
1396
1397 (defvar gnus-summary-generate-hook nil
1398   "*A hook run just before generating the summary buffer.
1399 This hook is commonly used to customize threading variables and the
1400 like.")
1401
1402 (defvar gnus-article-prepare-hook nil
1403   "*A hook called after an article has been prepared in the article buffer.
1404 If you want to run a special decoding program like nkf, use this hook.")
1405
1406 ;(defvar gnus-article-display-hook nil
1407 ;  "*A hook called after the article is displayed in the article buffer.
1408 ;The hook is designed to change the contents of the article
1409 ;buffer.  Typical functions that this hook may contain are
1410 ;`gnus-article-hide-headers' (hide selected headers),
1411 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1412 ;`gnus-article-hide-signature' (hide signature) and
1413 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1414 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1415 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1416 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1417
1418 (defvar gnus-article-x-face-command
1419   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1420   "String or function to be executed to display an X-Face header.
1421 If it is a string, the command will be executed in a sub-shell
1422 asynchronously.  The compressed face will be piped to this command.")
1423
1424 (defvar gnus-article-x-face-too-ugly nil
1425   "Regexp matching posters whose face shouldn't be shown automatically.")
1426
1427 (defvar gnus-select-group-hook nil
1428   "*A hook called when a newsgroup is selected.
1429
1430 If you'd like to simplify subjects like the
1431 `gnus-summary-next-same-subject' command does, you can use the
1432 following hook:
1433
1434  (setq gnus-select-group-hook
1435       (list
1436         (lambda ()
1437           (mapcar (lambda (header)
1438                      (mail-header-set-subject
1439                       header
1440                       (gnus-simplify-subject
1441                        (mail-header-subject header) 're-only)))
1442                   gnus-newsgroup-headers))))")
1443
1444 (defvar gnus-select-article-hook nil
1445   "*A hook called when an article is selected.")
1446
1447 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1448   "*A hook called to apply kill files to a group.
1449 This hook is intended to apply a kill file to the selected newsgroup.
1450 The function `gnus-apply-kill-file' is called by default.
1451
1452 Since a general kill file is too heavy to use only for a few
1453 newsgroups, I recommend you to use a lighter hook function.  For
1454 example, if you'd like to apply a kill file to articles which contains
1455 a string `rmgroup' in subject in newsgroup `control', you can use the
1456 following hook:
1457
1458  (setq gnus-apply-kill-hook
1459       (list
1460         (lambda ()
1461           (cond ((string-match \"control\" gnus-newsgroup-name)
1462                  (gnus-kill \"Subject\" \"rmgroup\")
1463                  (gnus-expunge \"X\"))))))")
1464
1465 (defvar gnus-visual-mark-article-hook
1466   (list 'gnus-highlight-selected-summary)
1467   "*Hook run after selecting an article in the summary buffer.
1468 It is meant to be used for highlighting the article in some way.  It
1469 is not run if `gnus-visual' is nil.")
1470
1471 (defvar gnus-parse-headers-hook nil
1472   "*A hook called before parsing the headers.")
1473 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1474
1475 (defvar gnus-exit-group-hook nil
1476   "*A hook called when exiting (not quitting) summary mode.")
1477
1478 (defvar gnus-suspend-gnus-hook nil
1479   "*A hook called when suspending (not exiting) Gnus.")
1480
1481 (defvar gnus-exit-gnus-hook nil
1482   "*A hook called when exiting Gnus.")
1483
1484 (defvar gnus-after-exiting-gnus-hook nil
1485   "*A hook called after exiting Gnus.")
1486
1487 (defvar gnus-save-newsrc-hook nil
1488   "*A hook called before saving any of the newsrc files.")
1489
1490 (defvar gnus-save-quick-newsrc-hook nil
1491   "*A hook called just before saving the quick newsrc file.
1492 Can be used to turn version control on or off.")
1493
1494 (defvar gnus-save-standard-newsrc-hook nil
1495   "*A hook called just before saving the standard newsrc file.
1496 Can be used to turn version control on or off.")
1497
1498 (defvar gnus-summary-update-hook
1499   (list 'gnus-summary-highlight-line)
1500   "*A hook called when a summary line is changed.
1501 The hook will not be called if `gnus-visual' is nil.
1502
1503 The default function `gnus-summary-highlight-line' will
1504 highlight the line according to the `gnus-summary-highlight'
1505 variable.")
1506
1507 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1508   "*A hook called when a group line is changed.
1509 The hook will not be called if `gnus-visual' is nil.
1510
1511 The default function `gnus-group-highlight-line' will
1512 highlight the line according to the `gnus-group-highlight'
1513 variable.")
1514
1515 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1516   "*A hook called when an article is selected for the first time.
1517 The hook is intended to mark an article as read (or unread)
1518 automatically when it is selected.")
1519
1520 (defvar gnus-group-change-level-function nil
1521   "Function run when a group level is changed.
1522 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1523
1524 ;; Remove any hilit infestation.
1525 (add-hook 'gnus-startup-hook
1526           (lambda ()
1527             (remove-hook 'gnus-summary-prepare-hook
1528                          'hilit-rehighlight-buffer-quietly)
1529             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1530             (setq gnus-mark-article-hook
1531                   '(gnus-summary-mark-read-and-unread-as-read))
1532             (remove-hook 'gnus-article-prepare-hook
1533                          'hilit-rehighlight-buffer-quietly)))
1534
1535 \f
1536 ;; Internal variables
1537
1538 (defvar gnus-tree-buffer "*Tree*"
1539   "Buffer where Gnus thread trees are displayed.")
1540
1541 ;; Dummy variable.
1542 (defvar gnus-use-generic-from nil)
1543
1544 (defvar gnus-thread-indent-array nil)
1545 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1546
1547 (defvar gnus-newsrc-file-version nil)
1548
1549 (defvar gnus-method-history nil)
1550 ;; Variable holding the user answers to all method prompts.
1551
1552 (defvar gnus-group-history nil)
1553 ;; Variable holding the user answers to all group prompts.
1554
1555 (defvar gnus-server-alist nil
1556   "List of available servers.")
1557
1558 (defvar gnus-group-indentation-function nil)
1559
1560 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1561
1562 (defvar gnus-goto-missing-group-function nil)
1563
1564 (defvar gnus-override-subscribe-method nil)
1565
1566 (defvar gnus-group-goto-next-group-function nil
1567   "Function to override finding the next group after listing groups.")
1568
1569 (defconst gnus-article-mark-lists
1570   '((marked . tick) (replied . reply)
1571     (expirable . expire) (killed . killed)
1572     (bookmarks . bookmark) (dormant . dormant)
1573     (scored . score) (saved . save)
1574     (cached . cache)
1575     ))
1576
1577 ;; Avoid highlighting in kill files.
1578 (defvar gnus-summary-inhibit-highlight nil)
1579 (defvar gnus-newsgroup-selected-overlay nil)
1580
1581 (defvar gnus-inhibit-hiding nil)
1582 (defvar gnus-group-indentation "")
1583 (defvar gnus-inhibit-limiting nil)
1584 (defvar gnus-created-frames nil)
1585
1586 (defvar gnus-article-mode-map nil)
1587 (defvar gnus-dribble-buffer nil)
1588 (defvar gnus-headers-retrieved-by nil)
1589 (defvar gnus-article-reply nil)
1590 (defvar gnus-override-method nil)
1591 (defvar gnus-article-check-size nil)
1592
1593 (defvar gnus-current-score-file nil)
1594 (defvar gnus-newsgroup-adaptive-score-file nil)
1595 (defvar gnus-scores-exclude-files nil)
1596
1597 (defvar gnus-opened-servers nil)
1598
1599 (defvar gnus-current-move-group nil)
1600 (defvar gnus-current-copy-group nil)
1601 (defvar gnus-current-crosspost-group nil)
1602
1603 (defvar gnus-newsgroup-dependencies nil)
1604 (defvar gnus-newsgroup-async nil)
1605 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1606
1607 (defvar gnus-newsgroup-adaptive nil)
1608
1609 (defvar gnus-summary-display-table nil)
1610 (defvar gnus-summary-display-article-function nil)
1611
1612 (defvar gnus-summary-highlight-line-function nil
1613   "Function called after highlighting a summary line.")
1614
1615 (defvar gnus-group-line-format-alist
1616   `((?M gnus-tmp-marked-mark ?c)
1617     (?S gnus-tmp-subscribed ?c)
1618     (?L gnus-tmp-level ?d)
1619     (?N (cond ((eq number t) "*" )
1620               ((numberp number) 
1621                (int-to-string
1622                 (+ number
1623                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1624                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1625               (t number)) ?s)
1626     (?R gnus-tmp-number-of-read ?s)
1627     (?t gnus-tmp-number-total ?d)
1628     (?y gnus-tmp-number-of-unread ?s)
1629     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1630     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1631     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1632            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1633     (?g gnus-tmp-group ?s)
1634     (?G gnus-tmp-qualified-group ?s)
1635     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1636     (?D gnus-tmp-newsgroup-description ?s)
1637     (?o gnus-tmp-moderated ?c)
1638     (?O gnus-tmp-moderated-string ?s)
1639     (?p gnus-tmp-process-marked ?c)
1640     (?s gnus-tmp-news-server ?s)
1641     (?n gnus-tmp-news-method ?s)
1642     (?P gnus-group-indentation ?s)
1643     (?l gnus-tmp-grouplens ?s)
1644     (?z gnus-tmp-news-method-string ?s)
1645     (?u gnus-tmp-user-defined ?s)))
1646
1647 (defvar gnus-summary-line-format-alist
1648   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1649     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1650     (?s gnus-tmp-subject-or-nil ?s)
1651     (?n gnus-tmp-name ?s)
1652     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1653         ?s)
1654     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1655             gnus-tmp-from) ?s)
1656     (?F gnus-tmp-from ?s)
1657     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1658     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1659     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1660     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1661     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1662     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1663     (?L gnus-tmp-lines ?d)
1664     (?I gnus-tmp-indentation ?s)
1665     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1666     (?R gnus-tmp-replied ?c)
1667     (?\[ gnus-tmp-opening-bracket ?c)
1668     (?\] gnus-tmp-closing-bracket ?c)
1669     (?\> (make-string gnus-tmp-level ? ) ?s)
1670     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1671     (?i gnus-tmp-score ?d)
1672     (?z gnus-tmp-score-char ?c)
1673     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1674     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1675     (?U gnus-tmp-unread ?c)
1676     (?t (gnus-summary-number-of-articles-in-thread
1677          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1678         ?d)
1679     (?e (gnus-summary-number-of-articles-in-thread
1680          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1681         ?c)
1682     (?u gnus-tmp-user-defined ?s))
1683   "An alist of format specifications that can appear in summary lines,
1684 and what variables they correspond with, along with the type of the
1685 variable (string, integer, character, etc).")
1686
1687 (defvar gnus-summary-dummy-line-format-alist
1688   `((?S gnus-tmp-subject ?s)
1689     (?N gnus-tmp-number ?d)
1690     (?u gnus-tmp-user-defined ?s)))
1691
1692 (defvar gnus-summary-mode-line-format-alist
1693   `((?G gnus-tmp-group-name ?s)
1694     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1695     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1696     (?A gnus-tmp-article-number ?d)
1697     (?Z gnus-tmp-unread-and-unselected ?s)
1698     (?V gnus-version ?s)
1699     (?U gnus-tmp-unread ?d)
1700     (?S gnus-tmp-subject ?s)
1701     (?e gnus-tmp-unselected ?d)
1702     (?u gnus-tmp-user-defined ?s)
1703     (?d (length gnus-newsgroup-dormant) ?d)
1704     (?t (length gnus-newsgroup-marked) ?d)
1705     (?r (length gnus-newsgroup-reads) ?d)
1706     (?E gnus-newsgroup-expunged-tally ?d)
1707     (?s (gnus-current-score-file-nondirectory) ?s)))
1708
1709 (defvar gnus-article-mode-line-format-alist
1710   gnus-summary-mode-line-format-alist)
1711
1712 (defvar gnus-group-mode-line-format-alist
1713   `((?S gnus-tmp-news-server ?s)
1714     (?M gnus-tmp-news-method ?s)
1715     (?u gnus-tmp-user-defined ?s)
1716     (?: gnus-tmp-colon ?s)))
1717
1718 (defvar gnus-have-read-active-file nil)
1719
1720 (defconst gnus-maintainer
1721   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1722   "The mail address of the Gnus maintainers.")
1723
1724 (defconst gnus-version "September Gnus v0.90"
1725   "Version number for this version of Gnus.")
1726
1727 (defvar gnus-info-nodes
1728   '((gnus-group-mode "(gnus)The Group Buffer")
1729     (gnus-summary-mode "(gnus)The Summary Buffer")
1730     (gnus-article-mode "(gnus)The Article Buffer"))
1731   "Assoc list of major modes and related Info nodes.")
1732
1733 (defvar gnus-group-buffer "*Group*")
1734 (defvar gnus-summary-buffer "*Summary*")
1735 (defvar gnus-article-buffer "*Article*")
1736 (defvar gnus-server-buffer "*Server*")
1737
1738 (defvar gnus-work-buffer " *gnus work*")
1739
1740 (defvar gnus-original-article-buffer " *Original Article*")
1741 (defvar gnus-original-article nil)
1742
1743 (defvar gnus-buffer-list nil
1744   "Gnus buffers that should be killed on exit.")
1745
1746 (defvar gnus-slave nil
1747   "Whether this Gnus is a slave or not.")
1748
1749 (defvar gnus-variable-list
1750   '(gnus-newsrc-options gnus-newsrc-options-n
1751     gnus-newsrc-last-checked-date
1752     gnus-newsrc-alist gnus-server-alist
1753     gnus-killed-list gnus-zombie-list
1754     gnus-topic-topology gnus-topic-alist
1755     gnus-format-specs)
1756   "Gnus variables saved in the quick startup file.")
1757
1758 (defvar gnus-newsrc-options nil
1759   "Options line in the .newsrc file.")
1760
1761 (defvar gnus-newsrc-options-n nil
1762   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1763
1764 (defvar gnus-newsrc-last-checked-date nil
1765   "Date Gnus last asked server for new newsgroups.")
1766
1767 (defvar gnus-topic-topology nil
1768   "The complete topic hierarchy.")
1769
1770 (defvar gnus-topic-alist nil
1771   "The complete topic-group alist.")
1772
1773 (defvar gnus-newsrc-alist nil
1774   "Assoc list of read articles.
1775 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1776
1777 (defvar gnus-newsrc-hashtb nil
1778   "Hashtable of gnus-newsrc-alist.")
1779
1780 (defvar gnus-killed-list nil
1781   "List of killed newsgroups.")
1782
1783 (defvar gnus-killed-hashtb nil
1784   "Hash table equivalent of gnus-killed-list.")
1785
1786 (defvar gnus-zombie-list nil
1787   "List of almost dead newsgroups.")
1788
1789 (defvar gnus-description-hashtb nil
1790   "Descriptions of newsgroups.")
1791
1792 (defvar gnus-list-of-killed-groups nil
1793   "List of newsgroups that have recently been killed by the user.")
1794
1795 (defvar gnus-active-hashtb nil
1796   "Hashtable of active articles.")
1797
1798 (defvar gnus-moderated-list nil
1799   "List of moderated newsgroups.")
1800
1801 (defvar gnus-group-marked nil)
1802
1803 (defvar gnus-current-startup-file nil
1804   "Startup file for the current host.")
1805
1806 (defvar gnus-last-search-regexp nil
1807   "Default regexp for article search command.")
1808
1809 (defvar gnus-last-shell-command nil
1810   "Default shell command on article.")
1811
1812 (defvar gnus-current-select-method nil
1813   "The current method for selecting a newsgroup.")
1814
1815 (defvar gnus-group-list-mode nil)
1816
1817 (defvar gnus-article-internal-prepare-hook nil)
1818
1819 (defvar gnus-newsgroup-name nil)
1820 (defvar gnus-newsgroup-begin nil)
1821 (defvar gnus-newsgroup-end nil)
1822 (defvar gnus-newsgroup-last-rmail nil)
1823 (defvar gnus-newsgroup-last-mail nil)
1824 (defvar gnus-newsgroup-last-folder nil)
1825 (defvar gnus-newsgroup-last-file nil)
1826 (defvar gnus-newsgroup-auto-expire nil)
1827 (defvar gnus-newsgroup-active nil)
1828
1829 (defvar gnus-newsgroup-data nil)
1830 (defvar gnus-newsgroup-data-reverse nil)
1831 (defvar gnus-newsgroup-limit nil)
1832 (defvar gnus-newsgroup-limits nil)
1833
1834 (defvar gnus-newsgroup-unreads nil
1835   "List of unread articles in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-unselected nil
1838   "List of unselected unread articles in the current newsgroup.")
1839
1840 (defvar gnus-newsgroup-reads nil
1841   "Alist of read articles and article marks in the current newsgroup.")
1842
1843 (defvar gnus-newsgroup-expunged-tally nil)
1844
1845 (defvar gnus-newsgroup-marked nil
1846   "List of ticked articles in the current newsgroup (a subset of unread art).")
1847
1848 (defvar gnus-newsgroup-killed nil
1849   "List of ranges of articles that have been through the scoring process.")
1850
1851 (defvar gnus-newsgroup-cached nil
1852   "List of articles that come from the article cache.")
1853
1854 (defvar gnus-newsgroup-saved nil
1855   "List of articles that have been saved.")
1856
1857 (defvar gnus-newsgroup-kill-headers nil)
1858
1859 (defvar gnus-newsgroup-replied nil
1860   "List of articles that have been replied to in the current newsgroup.")
1861
1862 (defvar gnus-newsgroup-expirable nil
1863   "List of articles in the current newsgroup that can be expired.")
1864
1865 (defvar gnus-newsgroup-processable nil
1866   "List of articles in the current newsgroup that can be processed.")
1867
1868 (defvar gnus-newsgroup-bookmarks nil
1869   "List of articles in the current newsgroup that have bookmarks.")
1870
1871 (defvar gnus-newsgroup-dormant nil
1872   "List of dormant articles in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-scored nil
1875   "List of scored articles in the current newsgroup.")
1876
1877 (defvar gnus-newsgroup-headers nil
1878   "List of article headers in the current newsgroup.")
1879
1880 (defvar gnus-newsgroup-threads nil)
1881
1882 (defvar gnus-newsgroup-prepared nil
1883   "Whether the current group has been prepared properly.")
1884
1885 (defvar gnus-newsgroup-ancient nil
1886   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1887
1888 (defvar gnus-newsgroup-sparse nil)
1889
1890 (defvar gnus-current-article nil)
1891 (defvar gnus-article-current nil)
1892 (defvar gnus-current-headers nil)
1893 (defvar gnus-have-all-headers nil)
1894 (defvar gnus-last-article nil)
1895 (defvar gnus-newsgroup-history nil)
1896 (defvar gnus-current-kill-article nil)
1897
1898 ;; Save window configuration.
1899 (defvar gnus-prev-winconf nil)
1900
1901 (defvar gnus-summary-mark-positions nil)
1902 (defvar gnus-group-mark-positions nil)
1903
1904 (defvar gnus-reffed-article-number nil)
1905
1906 ;;; Let the byte-compiler know that we know about this variable.
1907 (defvar rmail-default-rmail-file)
1908
1909 (defvar gnus-cache-removable-articles nil)
1910
1911 (defvar gnus-dead-summary nil)
1912
1913 (defconst gnus-summary-local-variables
1914   '(gnus-newsgroup-name
1915     gnus-newsgroup-begin gnus-newsgroup-end
1916     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1917     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1918     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1919     gnus-newsgroup-unselected gnus-newsgroup-marked
1920     gnus-newsgroup-reads gnus-newsgroup-saved
1921     gnus-newsgroup-replied gnus-newsgroup-expirable
1922     gnus-newsgroup-processable gnus-newsgroup-killed
1923     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1924     gnus-newsgroup-headers gnus-newsgroup-threads
1925     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1926     gnus-current-article gnus-current-headers gnus-have-all-headers
1927     gnus-last-article gnus-article-internal-prepare-hook
1928     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1929     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1930     gnus-newsgroup-async gnus-thread-expunge-below
1931     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1932     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1933     gnus-newsgroup-history gnus-newsgroup-ancient
1934     gnus-newsgroup-sparse
1935     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1936     gnus-newsgroup-adaptive-score-file
1937     (gnus-newsgroup-expunged-tally . 0)
1938     gnus-cache-removable-articles gnus-newsgroup-cached
1939     gnus-newsgroup-data gnus-newsgroup-data-reverse
1940     gnus-newsgroup-limit gnus-newsgroup-limits)
1941   "Variables that are buffer-local to the summary buffers.")
1942
1943 (defconst gnus-bug-message
1944   "Sending a bug report to the Gnus Towers.
1945 ========================================
1946
1947 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1948 be sent to the Gnus Bug Exterminators.
1949
1950 At the bottom of the buffer you'll see lots of variable settings.
1951 Please do not delete those.  They will tell the Bug People what your
1952 environment is, so that it will be easier to locate the bugs.
1953
1954 If you have found a bug that makes Emacs go \"beep\", set
1955 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1956 and include the backtrace in your bug report.
1957
1958 Please describe the bug in annoying, painstaking detail.
1959
1960 Thank you for your help in stamping out bugs.
1961 ")
1962
1963 ;;; End of variables.
1964
1965 ;; Define some autoload functions Gnus might use.
1966 (eval-and-compile
1967
1968   ;; This little mapcar goes through the list below and marks the
1969   ;; symbols in question as autoloaded functions.
1970   (mapcar
1971    (lambda (package)
1972      (let ((interactive (nth 1 (memq ':interactive package))))
1973        (mapcar
1974         (lambda (function)
1975           (let (keymap)
1976             (when (consp function)
1977               (setq keymap (car (memq 'keymap function)))
1978               (setq function (car function)))
1979             (autoload function (car package) nil interactive keymap)))
1980         (if (eq (nth 1 package) ':interactive)
1981             (cdddr package)
1982           (cdr package)))))
1983    '(("metamail" metamail-buffer)
1984      ("info" Info-goto-node)
1985      ("hexl" hexl-hex-string-to-integer)
1986      ("pp" pp pp-to-string pp-eval-expression)
1987      ("mail-extr" mail-extract-address-components)
1988      ("nnmail" nnmail-split-fancy nnmail-article-group)
1989      ("nnvirtual" nnvirtual-catchup-group)
1990      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1991       timezone-make-sortable-date timezone-make-time-string)
1992      ("sendmail" mail-position-on-field mail-setup)
1993      ("rmailout" rmail-output)
1994      ("rnewspost" news-mail-other-window news-reply-yank-original
1995       news-caesar-buffer-body)
1996      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1997       rmail-show-message)
1998      ("gnus-soup" :interactive t
1999       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2000       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2001      ("nnsoup" nnsoup-pack-replies)
2002      ("gnus-scomo" :interactive t gnus-score-mode)
2003      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2004       gnus-Folder-save-name gnus-folder-save-name)
2005      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2006      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2007       gnus-server-make-menu-bar gnus-article-make-menu-bar
2008       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2009       gnus-summary-highlight-line gnus-carpal-setup-buffer
2010       gnus-group-highlight-line
2011       gnus-article-add-button gnus-insert-next-page-button
2012       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2013      ("gnus-vis" :interactive t
2014       gnus-article-push-button gnus-article-press-button
2015       gnus-article-highlight gnus-article-highlight-some
2016       gnus-article-highlight-headers gnus-article-highlight-signature
2017       gnus-article-add-buttons gnus-article-add-buttons-to-head
2018       gnus-article-next-button gnus-article-prev-button)
2019      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2020       gnus-demon-add-disconnection gnus-demon-add-handler
2021       gnus-demon-remove-handler)
2022      ("gnus-demon" :interactive t
2023       gnus-demon-init gnus-demon-cancel)
2024      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2025       gnus-tree-open gnus-tree-close)
2026      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2027       gnus-nocem-unwanted-article-p)
2028      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2029      ("gnus-srvr" gnus-browse-foreign-server)
2030      ("gnus-cite" :interactive t
2031       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2032       gnus-article-hide-citation gnus-article-fill-cited-article
2033       gnus-article-hide-citation-in-followups)
2034      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2035       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2036       gnus-execute gnus-expunge)
2037      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2038       gnus-cache-possibly-remove-articles gnus-cache-request-article
2039       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2040       gnus-cache-enter-remove-article gnus-cached-article-p
2041       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2042      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2043       gnus-cache-remove-article)
2044      ("gnus-score" :interactive t
2045       gnus-summary-increase-score gnus-summary-lower-score
2046       gnus-score-flush-cache gnus-score-close
2047       gnus-score-raise-same-subject-and-select
2048       gnus-score-raise-same-subject gnus-score-default
2049       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2050       gnus-score-lower-same-subject gnus-score-lower-thread
2051       gnus-possibly-score-headers)
2052      ("gnus-score"
2053       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2054       gnus-current-score-file-nondirectory gnus-score-adaptive
2055       gnus-score-find-trace gnus-score-file-name)
2056      ("gnus-edit" :interactive t gnus-score-customize)
2057      ("gnus-topic" :interactive t gnus-topic-mode)
2058      ("gnus-topic" gnus-topic-remove-group)
2059      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2060      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2061      ("gnus-uu" :interactive t
2062       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2063       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2064       gnus-uu-mark-by-regexp gnus-uu-mark-all
2065       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2066       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2067       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2068       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2069       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2070       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2071       gnus-uu-decode-binhex-view)
2072      ("gnus-msg" (gnus-summary-send-map keymap)
2073       gnus-mail-yank-original gnus-mail-send-and-exit
2074       gnus-sendmail-setup-mail gnus-article-mail
2075       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2076      ("gnus-msg" :interactive t
2077       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2078       gnus-summary-followup gnus-summary-followup-with-original
2079       gnus-summary-followup-and-reply
2080       gnus-summary-followup-and-reply-with-original
2081       gnus-summary-cancel-article gnus-summary-supersede-article
2082       gnus-post-news gnus-inews-news gnus-cancel-news
2083       gnus-summary-reply gnus-summary-reply-with-original
2084       gnus-summary-mail-forward gnus-summary-mail-other-window
2085       gnus-bug)
2086      ("gnus-picon" :interactive t gnus-article-display-picons
2087       gnus-group-display-picons gnus-picons-article-display-x-face)
2088      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2089       gnus-grouplens-mode)
2090      ("gnus-vm" gnus-vm-mail-setup)
2091      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2092       gnus-summary-save-article-vm gnus-yank-article))))
2093
2094 \f
2095
2096 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2097 ;; If you want the cursor to go somewhere else, set these two
2098 ;; functions in some startup hook to whatever you want.
2099 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2100 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2101
2102 ;;; Various macros and substs.
2103
2104 (defun gnus-header-from (header)
2105   (mail-header-from header))
2106
2107 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2108   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2109   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2110         (w (make-symbol "w"))
2111         (buf (make-symbol "buf")))
2112     `(let* ((,tempvar (selected-window))
2113             (,buf ,buffer)
2114             (,w (get-buffer-window ,buf 'visible)))
2115        (unwind-protect
2116            (progn
2117              (if ,w
2118                  (select-window ,w)
2119                (pop-to-buffer ,buf))
2120              ,@forms)
2121          (select-window ,tempvar)))))
2122
2123 (defmacro gnus-gethash (string hashtable)
2124   "Get hash value of STRING in HASHTABLE."
2125   `(symbol-value (intern-soft ,string ,hashtable)))
2126
2127 (defmacro gnus-sethash (string value hashtable)
2128   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2129   `(set (intern ,string ,hashtable) ,value))
2130
2131 (defmacro gnus-intern-safe (string hashtable)
2132   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2133   `(let ((symbol (intern ,string ,hashtable)))
2134      (or (boundp symbol)
2135          (set symbol nil))
2136      symbol))
2137
2138 (defmacro gnus-group-unread (group)
2139   "Get the currently computed number of unread articles in GROUP."
2140   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2141
2142 (defmacro gnus-group-entry (group)
2143   "Get the newsrc entry for GROUP."
2144   `(gnus-gethash ,group gnus-newsrc-hashtb))
2145
2146 (defmacro gnus-active (group)
2147   "Get active info on GROUP."
2148   `(gnus-gethash ,group gnus-active-hashtb))
2149
2150 (defmacro gnus-set-active (group active)
2151   "Set GROUP's active info."
2152   `(gnus-sethash ,group ,active gnus-active-hashtb))
2153
2154 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2155 ;;   function `substring' might cut on a middle of multi-octet
2156 ;;   character.
2157 (defun gnus-truncate-string (str width)
2158   (substring str 0 width))
2159
2160 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2161 ;; to limit the length of a string.  This function is necessary since
2162 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2163 (defsubst gnus-limit-string (str width)
2164   (if (> (length str) width)
2165       (substring str 0 width)
2166     str))
2167
2168 (defsubst gnus-simplify-subject-re (subject)
2169   "Remove \"Re:\" from subject lines."
2170   (if (string-match "^[Rr][Ee]: *" subject)
2171       (substring subject (match-end 0))
2172     subject))
2173
2174 (defsubst gnus-functionp (form)
2175   "Return non-nil if FORM is funcallable."
2176   (or (and (symbolp form) (fboundp form))
2177       (and (listp form) (eq (car form) 'lambda))))
2178
2179 (defsubst gnus-goto-char (point)
2180   (and point (goto-char point)))
2181
2182 (defmacro gnus-buffer-exists-p (buffer)
2183   `(let ((buffer ,buffer))
2184      (and buffer
2185           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2186                    buffer))))
2187
2188 (defmacro gnus-kill-buffer (buffer)
2189   `(let ((buf ,buffer))
2190      (if (gnus-buffer-exists-p buf)
2191          (kill-buffer buf))))
2192
2193 (defsubst gnus-point-at-bol ()
2194   "Return point at the beginning of the line."
2195   (let ((p (point)))
2196     (beginning-of-line)
2197     (prog1
2198         (point)
2199       (goto-char p))))
2200
2201 (defsubst gnus-point-at-eol ()
2202   "Return point at the end of the line."
2203   (let ((p (point)))
2204     (end-of-line)
2205     (prog1
2206         (point)
2207       (goto-char p))))
2208
2209 (defun gnus-alive-p ()
2210   "Say whether Gnus is running or not."
2211   (and gnus-group-buffer
2212        (get-buffer gnus-group-buffer)))
2213
2214 ;; Delete the current line (and the next N lines.);
2215 (defmacro gnus-delete-line (&optional n)
2216   `(delete-region (progn (beginning-of-line) (point))
2217                   (progn (forward-line ,(or n 1)) (point))))
2218
2219 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2220 (defvar gnus-init-inhibit nil)
2221 (defun gnus-read-init-file (&optional inhibit-next)
2222   (if gnus-init-inhibit
2223       (setq gnus-init-inhibit nil)
2224     (setq gnus-init-inhibit inhibit-next)
2225     (and gnus-init-file
2226          (or (and (file-exists-p gnus-init-file)
2227                   ;; Don't try to load a directory.
2228                   (not (file-directory-p gnus-init-file)))
2229              (file-exists-p (concat gnus-init-file ".el"))
2230              (file-exists-p (concat gnus-init-file ".elc")))
2231          (condition-case var
2232              (load gnus-init-file nil t)
2233            (error
2234             (error "Error in %s: %s" gnus-init-file var))))))
2235
2236 ;; Info access macros.
2237
2238 (defmacro gnus-info-group (info)
2239   `(nth 0 ,info))
2240 (defmacro gnus-info-rank (info)
2241   `(nth 1 ,info))
2242 (defmacro gnus-info-read (info)
2243   `(nth 2 ,info))
2244 (defmacro gnus-info-marks (info)
2245   `(nth 3 ,info))
2246 (defmacro gnus-info-method (info)
2247   `(nth 4 ,info))
2248 (defmacro gnus-info-params (info)
2249   `(nth 5 ,info))
2250
2251 (defmacro gnus-info-level (info)
2252   `(let ((rank (gnus-info-rank ,info)))
2253      (if (consp rank)
2254          (car rank)
2255        rank)))
2256 (defmacro gnus-info-score (info)
2257   `(let ((rank (gnus-info-rank ,info)))
2258      (or (and (consp rank) (cdr rank)) 0)))
2259
2260 (defmacro gnus-info-set-group (info group)
2261   `(setcar ,info ,group))
2262 (defmacro gnus-info-set-rank (info rank)
2263   `(setcar (nthcdr 1 ,info) ,rank))
2264 (defmacro gnus-info-set-read (info read)
2265   `(setcar (nthcdr 2 ,info) ,read))
2266 (defmacro gnus-info-set-marks (info marks)
2267   `(setcar (nthcdr 3 ,info) ,marks))
2268 (defmacro gnus-info-set-method (info method)
2269   `(setcar (nthcdr 4 ,info) ,method))
2270 (defmacro gnus-info-set-params (info params)
2271   `(setcar (nthcdr 5 ,info) ,params))
2272
2273 (defmacro gnus-info-set-level (info level)
2274   `(let ((rank (cdr ,info)))
2275      (if (consp (car rank))
2276          (setcar (car rank) ,level)
2277        (setcar rank ,level))))
2278 (defmacro gnus-info-set-score (info score)
2279   `(let ((rank (cdr ,info)))
2280      (if (consp (car rank))
2281          (setcdr (car rank) ,score)
2282        (setcar rank (cons (car rank) ,score)))))
2283
2284 (defmacro gnus-get-info (group)
2285   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2286
2287 (defun gnus-byte-code (func)
2288   "Return a form that can be `eval'ed based on FUNC."
2289   (let ((fval (symbol-function func)))
2290     (if (byte-code-function-p fval)
2291         (let ((flist (append fval nil)))
2292           (setcar flist 'byte-code)
2293           flist)
2294       (cons 'progn (cddr fval)))))
2295
2296 ;;; Load the compatability functions.
2297
2298 (require 'gnus-cus)
2299 (require 'gnus-ems)
2300
2301 \f
2302 ;;;
2303 ;;; Shutdown
2304 ;;;
2305
2306 (defvar gnus-shutdown-alist nil)
2307
2308 (defun gnus-add-shutdown (function &rest symbols)
2309   "Run FUNCTION whenever one of SYMBOLS is shut down."
2310   (push (cons function symbols) gnus-shutdown-alist))
2311
2312 (defun gnus-shutdown (symbol)
2313   "Shut down everything that waits for SYMBOL."
2314   (let ((alist gnus-shutdown-alist)
2315         entry)
2316     (while (setq entry (pop alist))
2317       (when (memq symbol (cdr entry))
2318         (funcall (car entry))))))
2319
2320 \f
2321
2322 ;; Format specs.  The chunks below are the machine-generated forms
2323 ;; that are to be evaled as the result of the default format strings.
2324 ;; We write them in here to get them byte-compiled.  That way the
2325 ;; default actions will be quite fast, while still retaining the full
2326 ;; flexibility of the user-defined format specs.
2327
2328 ;; First we have lots of dummy defvars to let the compiler know these
2329 ;; are really dynamic variables.
2330
2331 (defvar gnus-tmp-unread)
2332 (defvar gnus-tmp-replied)
2333 (defvar gnus-tmp-score-char)
2334 (defvar gnus-tmp-indentation)
2335 (defvar gnus-tmp-opening-bracket)
2336 (defvar gnus-tmp-lines)
2337 (defvar gnus-tmp-name)
2338 (defvar gnus-tmp-closing-bracket)
2339 (defvar gnus-tmp-subject-or-nil)
2340 (defvar gnus-tmp-subject)
2341 (defvar gnus-tmp-marked)
2342 (defvar gnus-tmp-marked-mark)
2343 (defvar gnus-tmp-subscribed)
2344 (defvar gnus-tmp-process-marked)
2345 (defvar gnus-tmp-number-of-unread)
2346 (defvar gnus-tmp-group-name)
2347 (defvar gnus-tmp-group)
2348 (defvar gnus-tmp-article-number)
2349 (defvar gnus-tmp-unread-and-unselected)
2350 (defvar gnus-tmp-news-method)
2351 (defvar gnus-tmp-news-server)
2352 (defvar gnus-tmp-article-number)
2353 (defvar gnus-mouse-face)
2354 (defvar gnus-mouse-face-prop)
2355
2356 (defun gnus-summary-line-format-spec ()
2357   (insert gnus-tmp-unread gnus-tmp-replied
2358           gnus-tmp-score-char gnus-tmp-indentation)
2359   (gnus-put-text-property
2360    (point)
2361    (progn
2362      (insert
2363       gnus-tmp-opening-bracket
2364       (format "%4d: %-20s"
2365               gnus-tmp-lines
2366               (if (> (length gnus-tmp-name) 20)
2367                   (substring gnus-tmp-name 0 20)
2368                 gnus-tmp-name))
2369       gnus-tmp-closing-bracket)
2370      (point))
2371    gnus-mouse-face-prop gnus-mouse-face)
2372   (insert " " gnus-tmp-subject-or-nil "\n"))
2373
2374 (defvar gnus-summary-line-format-spec
2375   (gnus-byte-code 'gnus-summary-line-format-spec))
2376
2377 (defun gnus-summary-dummy-line-format-spec ()
2378   (insert "*  ")
2379   (gnus-put-text-property
2380    (point)
2381    (progn
2382      (insert ":                          :")
2383      (point))
2384    gnus-mouse-face-prop gnus-mouse-face)
2385   (insert " " gnus-tmp-subject "\n"))
2386
2387 (defvar gnus-summary-dummy-line-format-spec
2388   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2389
2390 (defun gnus-group-line-format-spec ()
2391   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2392           gnus-tmp-process-marked
2393           gnus-group-indentation
2394           (format "%5s: " gnus-tmp-number-of-unread))
2395   (gnus-put-text-property
2396    (point)
2397    (progn
2398      (insert gnus-tmp-group "\n")
2399      (1- (point)))
2400    gnus-mouse-face-prop gnus-mouse-face))
2401 (defvar gnus-group-line-format-spec
2402   (gnus-byte-code 'gnus-group-line-format-spec))
2403
2404 (defvar gnus-format-specs
2405   `((version . ,emacs-version)
2406     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2407     (summary-dummy ,gnus-summary-dummy-line-format
2408                    ,gnus-summary-dummy-line-format-spec)
2409     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2410
2411 (defvar gnus-article-mode-line-format-spec nil)
2412 (defvar gnus-summary-mode-line-format-spec nil)
2413 (defvar gnus-group-mode-line-format-spec nil)
2414
2415 ;;; Phew.  All that gruft is over, fortunately.
2416
2417 \f
2418 ;;;
2419 ;;; Gnus Utility Functions
2420 ;;;
2421
2422 (defun gnus-extract-address-components (from)
2423   (let (name address)
2424     ;; First find the address - the thing with the @ in it.  This may
2425     ;; not be accurate in mail addresses, but does the trick most of
2426     ;; the time in news messages.
2427     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2428         (setq address (substring from (match-beginning 0) (match-end 0))))
2429     ;; Then we check whether the "name <address>" format is used.
2430     (and address
2431          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2432          ;; Linear white space is not required.
2433          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2434          (and (setq name (substring from 0 (match-beginning 0)))
2435               ;; Strip any quotes from the name.
2436               (string-match "\".*\"" name)
2437               (setq name (substring name 1 (1- (match-end 0))))))
2438     ;; If not, then "address (name)" is used.
2439     (or name
2440         (and (string-match "(.+)" from)
2441              (setq name (substring from (1+ (match-beginning 0))
2442                                    (1- (match-end 0)))))
2443         (and (string-match "()" from)
2444              (setq name address))
2445         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2446         ;; XOVER might not support folded From headers.
2447         (and (string-match "(.*" from)
2448              (setq name (substring from (1+ (match-beginning 0))
2449                                    (match-end 0)))))
2450     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2451     (list (or name from) (or address from))))
2452
2453 (defun gnus-fetch-field (field)
2454   "Return the value of the header FIELD of current article."
2455   (save-excursion
2456     (save-restriction
2457       (let ((case-fold-search t)
2458             (inhibit-point-motion-hooks t))
2459         (nnheader-narrow-to-headers)
2460         (message-fetch-field field)))))
2461
2462 (defun gnus-goto-colon ()
2463   (beginning-of-line)
2464   (search-forward ":" (gnus-point-at-eol) t))
2465
2466 ;;;###autoload
2467 (defun gnus-update-format (var)
2468   "Update the format specification near point."
2469   (interactive
2470    (list
2471     (save-excursion
2472       (eval-defun nil)
2473       ;; Find the end of the current word.
2474       (re-search-forward "[ \t\n]" nil t)
2475       ;; Search backward.
2476       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2477         (match-string 1)))))
2478   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2479                               (match-string 1 var))))
2480          (entry (assq type gnus-format-specs))
2481          value spec)
2482     (when entry
2483       (setq gnus-format-specs (delq entry gnus-format-specs)))
2484     (set
2485      (intern (format "%s-spec" var))
2486      (gnus-parse-format (setq value (symbol-value (intern var)))
2487                         (symbol-value (intern (format "%s-alist" var)))
2488                         (not (string-match "mode" var))))
2489     (setq spec (symbol-value (intern (format "%s-spec" var))))
2490     (push (list type value spec) gnus-format-specs)
2491
2492     (pop-to-buffer "*Gnus Format*")
2493     (erase-buffer)
2494     (lisp-interaction-mode)
2495     (insert (pp-to-string spec))))
2496
2497 (defun gnus-update-format-specifications (&optional force)
2498   "Update all (necessary) format specifications."
2499   ;; Make the indentation array.
2500   (gnus-make-thread-indent-array)
2501
2502   ;; See whether all the stored info needs to be flushed.
2503   (when (or force
2504             (not (equal emacs-version
2505                         (cdr (assq 'version gnus-format-specs)))))
2506     (setq gnus-format-specs nil))
2507
2508   ;; Go through all the formats and see whether they need updating.
2509   (let ((types '(summary summary-dummy group
2510                          summary-mode group-mode article-mode))
2511         new-format entry type val)
2512     (while (setq type (pop types))
2513       ;; Jump to the proper buffer to find out the value of
2514       ;; the variable, if possible.  (It may be buffer-local.)
2515       (save-excursion
2516         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2517               val)
2518           (when (and (boundp buffer)
2519                      (setq val (symbol-value buffer))
2520                      (get-buffer val)
2521                      (buffer-name (get-buffer val)))
2522             (set-buffer (get-buffer val)))
2523           (setq new-format (symbol-value
2524                             (intern (format "gnus-%s-line-format" type))))))
2525       (setq entry (cdr (assq type gnus-format-specs)))
2526       (if (and entry
2527                (equal (car entry) new-format))
2528           ;; Use the old format.
2529           (set (intern (format "gnus-%s-line-format-spec" type))
2530                (cadr entry))
2531         ;; This is a new format.
2532         (setq val
2533               (if (not (stringp new-format))
2534                   ;; This is a function call or something.
2535                   new-format
2536                 ;; This is a "real" format.
2537                 (gnus-parse-format
2538                  new-format
2539                  (symbol-value
2540                   (intern (format "gnus-%s-line-format-alist"
2541                                   (if (eq type 'article-mode)
2542                                       'summary-mode type))))
2543                  (not (string-match "mode$" (symbol-name type))))))
2544         ;; Enter the new format spec into the list.
2545         (if entry
2546             (progn
2547               (setcar (cdr entry) val)
2548               (setcar entry new-format))
2549           (push (list type new-format val) gnus-format-specs))
2550         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2551
2552   (unless (assq 'version gnus-format-specs)
2553     (push (cons 'version emacs-version) gnus-format-specs))
2554
2555   (gnus-update-group-mark-positions)
2556   (gnus-update-summary-mark-positions))
2557
2558 (defun gnus-update-summary-mark-positions ()
2559   "Compute where the summary marks are to go."
2560   (save-excursion
2561     (when (and gnus-summary-buffer
2562                (get-buffer gnus-summary-buffer)
2563                (buffer-name (get-buffer gnus-summary-buffer)))
2564       (set-buffer gnus-summary-buffer))
2565     (let ((gnus-replied-mark 129)
2566           (gnus-score-below-mark 130)
2567           (gnus-score-over-mark 130)
2568           (thread nil)
2569           (gnus-visual nil)
2570           (spec gnus-summary-line-format-spec)
2571           pos)
2572       (save-excursion
2573         (gnus-set-work-buffer)
2574         (let ((gnus-summary-line-format-spec spec))
2575           (gnus-summary-insert-line
2576            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2577           (goto-char (point-min))
2578           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2579                                              (- (point) 2)))))
2580           (goto-char (point-min))
2581           (push (cons 'replied (and (search-forward "\201" nil t) 
2582                                     (- (point) 2)))
2583                 pos)
2584           (goto-char (point-min))
2585           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2586                 pos)))
2587       (setq gnus-summary-mark-positions pos))))
2588
2589 (defun gnus-update-group-mark-positions ()
2590   (save-excursion
2591     (let ((gnus-process-mark 128)
2592           (gnus-group-marked '("dummy.group"))
2593           (gnus-active-hashtb (make-vector 10 0)))
2594       (gnus-set-active "dummy.group" '(0 . 0))
2595       (gnus-set-work-buffer)
2596       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2597       (goto-char (point-min))
2598       (setq gnus-group-mark-positions
2599             (list (cons 'process (and (search-forward "\200" nil t)
2600                                       (- (point) 2))))))))
2601
2602 (defvar gnus-mouse-face-0 'highlight)
2603 (defvar gnus-mouse-face-1 'highlight)
2604 (defvar gnus-mouse-face-2 'highlight)
2605 (defvar gnus-mouse-face-3 'highlight)
2606 (defvar gnus-mouse-face-4 'highlight)
2607
2608 (defun gnus-mouse-face-function (form type)
2609   `(gnus-put-text-property
2610     (point) (progn ,@form (point))
2611     gnus-mouse-face-prop
2612     ,(if (equal type 0)
2613          'gnus-mouse-face
2614        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2615
2616 (defvar gnus-face-0 'bold)
2617 (defvar gnus-face-1 'italic)
2618 (defvar gnus-face-2 'bold-italic)
2619 (defvar gnus-face-3 'bold)
2620 (defvar gnus-face-4 'bold)
2621
2622 (defun gnus-face-face-function (form type)
2623   `(gnus-put-text-property
2624     (point) (progn ,@form (point))
2625     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2626
2627 (defun gnus-max-width-function (el max-width)
2628   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2629   (if (symbolp el)
2630       `(if (> (length ,el) ,max-width)
2631            (substring ,el 0 ,max-width)
2632          ,el)
2633     `(let ((val (eval ,el)))
2634        (if (numberp val)
2635            (setq val (int-to-string val)))
2636        (if (> (length val) ,max-width)
2637            (substring val 0 ,max-width)
2638          val))))
2639
2640 (defun gnus-parse-format (format spec-alist &optional insert)
2641   ;; This function parses the FORMAT string with the help of the
2642   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2643   ;; string.  If the FORMAT string contains the specifiers %( and %)
2644   ;; the text between them will have the mouse-face text property.
2645   (if (string-match
2646        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2647        format)
2648       (gnus-parse-complex-format format spec-alist)
2649     ;; This is a simple format.
2650     (gnus-parse-simple-format format spec-alist insert)))
2651
2652 (defun gnus-parse-complex-format (format spec-alist)
2653   (save-excursion
2654     (gnus-set-work-buffer)
2655     (insert format)
2656     (goto-char (point-min))
2657     (while (re-search-forward "\"" nil t)
2658       (replace-match "\\\"" nil t))
2659     (goto-char (point-min))
2660     (insert "(\"")
2661     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2662       (let ((number (if (match-beginning 1)
2663                         (match-string 1) "0"))
2664             (delim (aref (match-string 2) 0)))
2665         (if (or (= delim ?\() (= delim ?\{))
2666             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2667                                    " " number " \""))
2668           (replace-match "\")\""))))
2669     (goto-char (point-max))
2670     (insert "\")")
2671     (goto-char (point-min))
2672     (let ((form (read (current-buffer))))
2673       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2674
2675 (defun gnus-complex-form-to-spec (form spec-alist)
2676   (delq nil
2677         (mapcar
2678          (lambda (sform)
2679            (if (stringp sform)
2680                (gnus-parse-simple-format sform spec-alist t)
2681              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2682                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2683                       (nth 1 sform))))
2684          form)))
2685
2686 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2687   ;; This function parses the FORMAT string with the help of the
2688   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2689   ;; string.
2690   (let ((max-width 0)
2691         spec flist fstring newspec elem beg result dontinsert)
2692     (save-excursion
2693       (gnus-set-work-buffer)
2694       (insert format)
2695       (goto-char (point-min))
2696       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2697                                 nil t)
2698         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2699               (setq newspec "%"
2700                     beg (1+ (match-beginning 0)))
2701           ;; First check if there are any specs that look anything like
2702           ;; "%12,12A", ie. with a "max width specification".  These have
2703           ;; to be treated specially.
2704           (if (setq beg (match-beginning 1))
2705               (setq max-width
2706                     (string-to-int
2707                      (buffer-substring
2708                       (1+ (match-beginning 1)) (match-end 1))))
2709             (setq max-width 0)
2710             (setq beg (match-beginning 2)))
2711           ;; Find the specification from `spec-alist'.
2712           (unless (setq elem (cdr (assq spec spec-alist)))
2713             (setq elem '("*" ?s)))
2714           ;; Treat user defined format specifiers specially.
2715           (when (eq (car elem) 'gnus-tmp-user-defined)
2716             (setq elem
2717                   (list
2718                    (list (intern (concat "gnus-user-format-function-"
2719                                          (match-string 3)))
2720                          'gnus-tmp-header) ?s))
2721             (delete-region (match-beginning 3) (match-end 3)))
2722           (if (not (zerop max-width))
2723               (let ((el (car elem)))
2724                 (cond ((= (cadr elem) ?c)
2725                        (setq el (list 'char-to-string el)))
2726                       ((= (cadr elem) ?d)
2727                        (setq el (list 'int-to-string el))))
2728                 (setq flist (cons (gnus-max-width-function el max-width)
2729                                   flist))
2730                 (setq newspec ?s))
2731             (progn
2732               (setq flist (cons (car elem) flist))
2733               (setq newspec (cadr elem)))))
2734         ;; Remove the old specification (and possibly a ",12" string).
2735         (delete-region beg (match-end 2))
2736         ;; Insert the new specification.
2737         (goto-char beg)
2738         (insert newspec))
2739       (setq fstring (buffer-substring 1 (point-max))))
2740     ;; Do some postprocessing to increase efficiency.
2741     (setq
2742      result
2743      (cond
2744       ;; Emptyness.
2745       ((string= fstring "")
2746        nil)
2747       ;; Not a format string.
2748       ((not (string-match "%" fstring))
2749        (list fstring))
2750       ;; A format string with just a single string spec.
2751       ((string= fstring "%s")
2752        (list (car flist)))
2753       ;; A single character.
2754       ((string= fstring "%c")
2755        (list (car flist)))
2756       ;; A single number.
2757       ((string= fstring "%d")
2758        (setq dontinsert)
2759        (if insert
2760            (list `(princ ,(car flist)))
2761          (list `(int-to-string ,(car flist)))))
2762       ;; Just lots of chars and strings.
2763       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2764        (nreverse flist))
2765       ;; A single string spec at the beginning of the spec.
2766       ((string-match "\\`%[sc][^%]+\\'" fstring)
2767        (list (car flist) (substring fstring 2)))
2768       ;; A single string spec in the middle of the spec.
2769       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2770        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2771       ;; A single string spec in the end of the spec.
2772       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2773        (list (match-string 1 fstring) (car flist)))
2774       ;; A more complex spec.
2775       (t
2776        (list (cons 'format (cons fstring (nreverse flist)))))))
2777
2778     (if insert
2779         (when result
2780           (if dontinsert
2781               result
2782             (cons 'insert result)))
2783       (cond ((stringp result)
2784              result)
2785             ((consp result)
2786              (cons 'concat result))
2787             (t "")))))
2788
2789 (defun gnus-eval-format (format &optional alist props)
2790   "Eval the format variable FORMAT, using ALIST.
2791 If PROPS, insert the result."
2792   (let ((form (gnus-parse-format format alist props)))
2793     (if props
2794         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2795       (eval form))))
2796
2797 (defun gnus-remove-text-with-property (prop)
2798   "Delete all text in the current buffer with text property PROP."
2799   (save-excursion
2800     (goto-char (point-min))
2801     (while (not (eobp))
2802       (while (get-text-property (point) prop)
2803         (delete-char 1))
2804       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2805
2806 (defun gnus-set-work-buffer ()
2807   (if (get-buffer gnus-work-buffer)
2808       (progn
2809         (set-buffer gnus-work-buffer)
2810         (erase-buffer))
2811     (set-buffer (get-buffer-create gnus-work-buffer))
2812     (kill-all-local-variables)
2813     (buffer-disable-undo (current-buffer))
2814     (gnus-add-current-to-buffer-list)))
2815
2816 ;; Article file names when saving.
2817
2818 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2819   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2820 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2821 Otherwise, it is like ~/News/news/group/num."
2822   (let ((default
2823           (expand-file-name
2824            (concat (if (gnus-use-long-file-name 'not-save)
2825                        (gnus-capitalize-newsgroup newsgroup)
2826                      (gnus-newsgroup-directory-form newsgroup))
2827                    "/" (int-to-string (mail-header-number headers)))
2828            gnus-article-save-directory)))
2829     (if (and last-file
2830              (string-equal (file-name-directory default)
2831                            (file-name-directory last-file))
2832              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2833         default
2834       (or last-file default))))
2835
2836 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2837   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2838 If variable `gnus-use-long-file-name' is non-nil, it is
2839 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2840   (let ((default
2841           (expand-file-name
2842            (concat (if (gnus-use-long-file-name 'not-save)
2843                        newsgroup
2844                      (gnus-newsgroup-directory-form newsgroup))
2845                    "/" (int-to-string (mail-header-number headers)))
2846            gnus-article-save-directory)))
2847     (if (and last-file
2848              (string-equal (file-name-directory default)
2849                            (file-name-directory last-file))
2850              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2851         default
2852       (or last-file default))))
2853
2854 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2855   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2856 If variable `gnus-use-long-file-name' is non-nil, it is
2857 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2858   (or last-file
2859       (expand-file-name
2860        (if (gnus-use-long-file-name 'not-save)
2861            (gnus-capitalize-newsgroup newsgroup)
2862          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2863        gnus-article-save-directory)))
2864
2865 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2866   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2867 If variable `gnus-use-long-file-name' is non-nil, it is
2868 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2869   (or last-file
2870       (expand-file-name
2871        (if (gnus-use-long-file-name 'not-save)
2872            newsgroup
2873          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2874        gnus-article-save-directory)))
2875
2876 ;; For subscribing new newsgroup
2877
2878 (defun gnus-subscribe-hierarchical-interactive (groups)
2879   (let ((groups (sort groups 'string<))
2880         prefixes prefix start ans group starts)
2881     (while groups
2882       (setq prefixes (list "^"))
2883       (while (and groups prefixes)
2884         (while (not (string-match (car prefixes) (car groups)))
2885           (setq prefixes (cdr prefixes)))
2886         (setq prefix (car prefixes))
2887         (setq start (1- (length prefix)))
2888         (if (and (string-match "[^\\.]\\." (car groups) start)
2889                  (cdr groups)
2890                  (setq prefix
2891                        (concat "^" (substring (car groups) 0 (match-end 0))))
2892                  (string-match prefix (cadr groups)))
2893             (progn
2894               (setq prefixes (cons prefix prefixes))
2895               (message "Descend hierarchy %s? ([y]nsq): "
2896                        (substring prefix 1 (1- (length prefix))))
2897               (setq ans (read-char))
2898               (cond ((= ans ?n)
2899                      (while (and groups
2900                                  (string-match prefix
2901                                                (setq group (car groups))))
2902                        (setq gnus-killed-list
2903                              (cons group gnus-killed-list))
2904                        (gnus-sethash group group gnus-killed-hashtb)
2905                        (setq groups (cdr groups)))
2906                      (setq starts (cdr starts)))
2907                     ((= ans ?s)
2908                      (while (and groups
2909                                  (string-match prefix
2910                                                (setq group (car groups))))
2911                        (gnus-sethash group group gnus-killed-hashtb)
2912                        (gnus-subscribe-alphabetically (car groups))
2913                        (setq groups (cdr groups)))
2914                      (setq starts (cdr starts)))
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 nil)))
2922           (message "Subscribe %s? ([n]yq)" (car groups))
2923           (setq ans (read-char))
2924           (setq group (car groups))
2925           (cond ((= ans ?y)
2926                  (gnus-subscribe-alphabetically (car groups))
2927                  (gnus-sethash group group gnus-killed-hashtb))
2928                 ((= ans ?q)
2929                  (while groups
2930                    (setq group (car groups))
2931                    (setq gnus-killed-list (cons group gnus-killed-list))
2932                    (gnus-sethash group group gnus-killed-hashtb)
2933                    (setq groups (cdr groups))))
2934                 (t
2935                  (setq gnus-killed-list (cons group gnus-killed-list))
2936                  (gnus-sethash group group gnus-killed-hashtb)))
2937           (setq groups (cdr groups)))))))
2938
2939 (defun gnus-subscribe-randomly (newsgroup)
2940   "Subscribe new NEWSGROUP by making it the first newsgroup."
2941   (gnus-subscribe-newsgroup newsgroup))
2942
2943 (defun gnus-subscribe-alphabetically (newgroup)
2944   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2945   (let ((groups (cdr gnus-newsrc-alist))
2946         before)
2947     (while (and (not before) groups)
2948       (if (string< newgroup (caar groups))
2949           (setq before (caar groups))
2950         (setq groups (cdr groups))))
2951     (gnus-subscribe-newsgroup newgroup before)))
2952
2953 (defun gnus-subscribe-hierarchically (newgroup)
2954   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2955   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2956   (save-excursion
2957     (set-buffer (find-file-noselect gnus-current-startup-file))
2958     (let ((groupkey newgroup)
2959           before)
2960       (while (and (not before) groupkey)
2961         (goto-char (point-min))
2962         (let ((groupkey-re
2963                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2964           (while (and (re-search-forward groupkey-re nil t)
2965                       (progn
2966                         (setq before (match-string 1))
2967                         (string< before newgroup)))))
2968         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2969         (setq groupkey
2970               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2971                   (substring groupkey (match-beginning 1) (match-end 1)))))
2972       (gnus-subscribe-newsgroup newgroup before))))
2973
2974 (defun gnus-subscribe-interactively (group)
2975   "Subscribe the new GROUP interactively.
2976 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2977 it is killed."
2978   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2979       (gnus-subscribe-hierarchically group)
2980     (push group gnus-killed-list)))
2981
2982 (defun gnus-subscribe-zombies (group)
2983   "Make the new GROUP into a zombie group."
2984   (push group gnus-zombie-list))
2985
2986 (defun gnus-subscribe-killed (group)
2987   "Make the new GROUP a killed group."
2988   (push group gnus-killed-list))
2989
2990 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2991   "Subscribe new NEWSGROUP.
2992 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2993 the first newsgroup."
2994   ;; We subscribe the group by changing its level to `subscribed'.
2995   (gnus-group-change-level
2996    newsgroup gnus-level-default-subscribed
2997    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2998   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2999
3000 ;; For directories
3001
3002 (defun gnus-newsgroup-directory-form (newsgroup)
3003   "Make hierarchical directory name from NEWSGROUP name."
3004   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3005         (len (length newsgroup))
3006         idx)
3007     ;; If this is a foreign group, we don't want to translate the
3008     ;; entire name.
3009     (if (setq idx (string-match ":" newsgroup))
3010         (aset newsgroup idx ?/)
3011       (setq idx 0))
3012     ;; Replace all occurrences of `.' with `/'.
3013     (while (< idx len)
3014       (if (= (aref newsgroup idx) ?.)
3015           (aset newsgroup idx ?/))
3016       (setq idx (1+ idx)))
3017     newsgroup))
3018
3019 (defun gnus-newsgroup-savable-name (group)
3020   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3021   ;; with dots.
3022   (nnheader-replace-chars-in-string group ?/ ?.))
3023
3024 (defun gnus-make-directory (dir)
3025   "Make DIRECTORY recursively."
3026   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3027   ;; of the many mysteries of the universe.
3028   (let* ((dir (expand-file-name dir default-directory))
3029          dirs err)
3030     (if (string-match "/$" dir)
3031         (setq dir (substring dir 0 (match-beginning 0))))
3032     ;; First go down the path until we find a directory that exists.
3033     (while (not (file-exists-p dir))
3034       (setq dirs (cons dir dirs))
3035       (string-match "/[^/]+$" dir)
3036       (setq dir (substring dir 0 (match-beginning 0))))
3037     ;; Then create all the subdirs.
3038     (while (and dirs (not err))
3039       (condition-case ()
3040           (make-directory (car dirs))
3041         (error (setq err t)))
3042       (setq dirs (cdr dirs)))
3043     ;; We return whether we were successful or not.
3044     (not dirs)))
3045
3046 (defun gnus-capitalize-newsgroup (newsgroup)
3047   "Capitalize NEWSGROUP name."
3048   (and (not (zerop (length newsgroup)))
3049        (concat (char-to-string (upcase (aref newsgroup 0)))
3050                (substring newsgroup 1))))
3051
3052 ;; Various... things.
3053
3054 (defun gnus-simplify-subject (subject &optional re-only)
3055   "Remove `Re:' and words in parentheses.
3056 If RE-ONLY is non-nil, strip leading `Re:'s only."
3057   (let ((case-fold-search t))           ;Ignore case.
3058     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3059     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3060       (setq subject (substring subject (match-end 0))))
3061     ;; Remove uninteresting prefixes.
3062     (if (and (not re-only)
3063              gnus-simplify-ignored-prefixes
3064              (string-match gnus-simplify-ignored-prefixes subject))
3065         (setq subject (substring subject (match-end 0))))
3066     ;; Remove words in parentheses from end.
3067     (unless re-only
3068       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3069         (setq subject (substring subject 0 (match-beginning 0)))))
3070     ;; Return subject string.
3071     subject))
3072
3073 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3074 ;; all whitespace.
3075 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3076 (defun gnus-simplify-buffer-fuzzy ()
3077   (goto-char (point-min))
3078   (while (search-forward "\t" nil t)
3079     (replace-match " " t t))
3080   (goto-char (point-min))
3081   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3082   (goto-char (match-beginning 0))
3083   (while (or
3084           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3085           (looking-at "^[[].*: .*[]]$"))
3086     (goto-char (point-min))
3087     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3088                               nil t)
3089       (replace-match "" t t))
3090     (goto-char (point-min))
3091     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3092       (goto-char (match-end 0))
3093       (delete-char -1)
3094       (delete-region
3095        (progn (goto-char (match-beginning 0)))
3096        (re-search-forward ":"))))
3097   (goto-char (point-min))
3098   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3099     (replace-match "" t t))
3100   (goto-char (point-min))
3101   (while (re-search-forward "  +" nil t)
3102     (replace-match " " t t))
3103   (goto-char (point-min))
3104   (while (re-search-forward " $" nil t)
3105     (replace-match "" t t))
3106   (goto-char (point-min))
3107   (while (re-search-forward "^ +" nil t)
3108     (replace-match "" t t))
3109   (goto-char (point-min))
3110   (when gnus-simplify-subject-fuzzy-regexp
3111     (if (listp gnus-simplify-subject-fuzzy-regexp)
3112         (let ((list gnus-simplify-subject-fuzzy-regexp))
3113           (while list
3114             (goto-char (point-min))
3115             (while (re-search-forward (car list) nil t)
3116               (replace-match "" t t))
3117             (setq list (cdr list))))
3118       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3119         (replace-match "" t t)))))
3120
3121 (defun gnus-simplify-subject-fuzzy (subject)
3122   "Siplify a subject string fuzzily."
3123   (save-excursion
3124     (gnus-set-work-buffer)
3125     (let ((case-fold-search t))
3126       (insert subject)
3127       (inline (gnus-simplify-buffer-fuzzy))
3128       (buffer-string))))
3129
3130 ;; Add the current buffer to the list of buffers to be killed on exit.
3131 (defun gnus-add-current-to-buffer-list ()
3132   (or (memq (current-buffer) gnus-buffer-list)
3133       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3134
3135 (defun gnus-string> (s1 s2)
3136   (not (or (string< s1 s2)
3137            (string= s1 s2))))
3138
3139 (defun gnus-read-active-file-p ()
3140   "Say whether the active file has been read from `gnus-select-method'."
3141   (memq gnus-select-method gnus-have-read-active-file))
3142
3143 ;;; General various misc type functions.
3144
3145 (defun gnus-clear-system ()
3146   "Clear all variables and buffers."
3147   ;; Clear Gnus variables.
3148   (let ((variables gnus-variable-list))
3149     (while variables
3150       (set (car variables) nil)
3151       (setq variables (cdr variables))))
3152   ;; Clear other internal variables.
3153   (setq gnus-list-of-killed-groups nil
3154         gnus-have-read-active-file nil
3155         gnus-newsrc-alist nil
3156         gnus-newsrc-hashtb nil
3157         gnus-killed-list nil
3158         gnus-zombie-list nil
3159         gnus-killed-hashtb nil
3160         gnus-active-hashtb nil
3161         gnus-moderated-list nil
3162         gnus-description-hashtb nil
3163         gnus-current-headers nil
3164         gnus-thread-indent-array nil
3165         gnus-newsgroup-headers nil
3166         gnus-newsgroup-name nil
3167         gnus-server-alist nil
3168         gnus-group-list-mode nil
3169         gnus-opened-servers nil
3170         gnus-current-select-method nil)
3171   (gnus-shutdown 'gnus)
3172   ;; Kill the startup file.
3173   (and gnus-current-startup-file
3174        (get-file-buffer gnus-current-startup-file)
3175        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3176   ;; Clear the dribble buffer.
3177   (gnus-dribble-clear)
3178   ;; Kill global KILL file buffer.
3179   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3180     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3181   (gnus-kill-buffer nntp-server-buffer)
3182   ;; Kill Gnus buffers.
3183   (while gnus-buffer-list
3184     (gnus-kill-buffer (pop gnus-buffer-list)))
3185   ;; Remove Gnus frames.
3186   (while gnus-created-frames
3187     (when (frame-live-p (car gnus-created-frames))
3188       ;; We slap a condition-case around this `delete-frame' to ensure 
3189       ;; agains errors if we try do delete the single frame that's left.
3190       (condition-case ()
3191           (delete-frame (car gnus-created-frames))
3192         (error nil)))
3193     (pop gnus-created-frames)))
3194
3195 (defun gnus-windows-old-to-new (setting)
3196   ;; First we take care of the really, really old Gnus 3 actions.
3197   (when (symbolp setting)
3198     (setq setting
3199           ;; Take care of ooold GNUS 3.x values.
3200           (cond ((eq setting 'SelectArticle) 'article)
3201                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3202                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3203                 (t setting))))
3204   (if (or (listp setting)
3205           (not (and gnus-window-configuration
3206                     (memq setting '(group summary article)))))
3207       setting
3208     (let* ((setting (if (eq setting 'group)
3209                         (if (assq 'newsgroup gnus-window-configuration)
3210                             'newsgroup
3211                           'newsgroups) setting))
3212            (elem (cadr (assq setting gnus-window-configuration)))
3213            (total (apply '+ elem))
3214            (types '(group summary article))
3215            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3216            (i 0)
3217            perc
3218            out)
3219       (while (< i 3)
3220         (or (not (numberp (nth i elem)))
3221             (zerop (nth i elem))
3222             (progn
3223               (setq perc  (/ (float (nth 0 elem)) total))
3224               (setq out (cons (if (eq pbuf (nth i types))
3225                                   (vector (nth i types) perc 'point)
3226                                 (vector (nth i types) perc))
3227                               out))))
3228         (setq i (1+ i)))
3229       (list (nreverse out)))))
3230
3231 ;;;###autoload
3232 (defun gnus-add-configuration (conf)
3233   "Add the window configuration CONF to `gnus-buffer-configuration'."
3234   (setq gnus-buffer-configuration
3235         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3236                          gnus-buffer-configuration))))
3237
3238 (defvar gnus-frame-list nil)
3239
3240 (defun gnus-configure-frame (split &optional window)
3241   "Split WINDOW according to SPLIT."
3242   (unless window
3243     (setq window (get-buffer-window (current-buffer))))
3244   (select-window window)
3245   ;; This might be an old-stylee buffer config.
3246   (when (vectorp split)
3247     (setq split (append split nil)))
3248   (when (or (consp (car split))
3249             (vectorp (car split)))
3250     (push 1.0 split)
3251     (push 'vertical split))
3252   ;; The SPLIT might be something that is to be evaled to
3253   ;; return a new SPLIT.
3254   (while (and (not (assq (car split) gnus-window-to-buffer))
3255               (gnus-functionp (car split)))
3256     (setq split (eval split)))
3257   (let* ((type (car split))
3258          (subs (cddr split))
3259          (len (if (eq type 'horizontal) (window-width) (window-height)))
3260          (total 0)
3261          (window-min-width (or gnus-window-min-width window-min-width))
3262          (window-min-height (or gnus-window-min-height window-min-height))
3263          s result new-win rest comp-subs size sub)
3264     (cond
3265      ;; Nothing to do here.
3266      ((null split))
3267      ;; Don't switch buffers.
3268      ((null type)
3269       (and (memq 'point split) window))
3270      ;; This is a buffer to be selected.
3271      ((not (memq type '(frame horizontal vertical)))
3272       (let ((buffer (cond ((stringp type) type)
3273                           (t (cdr (assq type gnus-window-to-buffer)))))
3274             buf)
3275         (unless buffer
3276           (error "Illegal buffer type: %s" type))
3277         (unless (setq buf (get-buffer (if (symbolp buffer)
3278                                           (symbol-value buffer) buffer)))
3279           (setq buf (get-buffer-create (if (symbolp buffer)
3280                                            (symbol-value buffer) buffer))))
3281         (switch-to-buffer buf)
3282         ;; We return the window if it has the `point' spec.
3283         (and (memq 'point split) window)))
3284      ;; This is a frame split.
3285      ((eq type 'frame)
3286       (unless gnus-frame-list
3287         (setq gnus-frame-list (list (window-frame
3288                                      (get-buffer-window (current-buffer))))))
3289       (let ((i 0)
3290             params frame fresult)
3291         (while (< i (length subs))
3292           ;; Frame parameter is gotten from the sub-split.
3293           (setq params (cadr (elt subs i)))
3294           ;; It should be a list.
3295           (unless (listp params)
3296             (setq params nil))
3297           ;; Create a new frame?
3298           (unless (setq frame (elt gnus-frame-list i))
3299             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3300             (push frame gnus-created-frames))
3301           ;; Is the old frame still alive?
3302           (unless (frame-live-p frame)
3303             (setcar (nthcdr i gnus-frame-list)
3304                     (setq frame (make-frame params))))
3305           ;; Select the frame in question and do more splits there.
3306           (select-frame frame)
3307           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3308           (incf i))
3309         ;; Select the frame that has the selected buffer.
3310         (when fresult
3311           (select-frame (window-frame fresult)))))
3312      ;; This is a normal split.
3313      (t
3314       (when (> (length subs) 0)
3315         ;; First we have to compute the sizes of all new windows.
3316         (while subs
3317           (setq sub (append (pop subs) nil))
3318           (while (and (not (assq (car sub) gnus-window-to-buffer))
3319                       (gnus-functionp (car sub)))
3320             (setq sub (eval sub)))
3321           (when sub
3322             (push sub comp-subs)
3323             (setq size (cadar comp-subs))
3324             (cond ((equal size 1.0)
3325                    (setq rest (car comp-subs))
3326                    (setq s 0))
3327                   ((floatp size)
3328                    (setq s (floor (* size len))))
3329                   ((integerp size)
3330                    (setq s size))
3331                   (t
3332                    (error "Illegal size: %s" size)))
3333             ;; Try to make sure that we are inside the safe limits.
3334             (cond ((zerop s))
3335                   ((eq type 'horizontal)
3336                    (setq s (max s window-min-width)))
3337                   ((eq type 'vertical)
3338                    (setq s (max s window-min-height))))
3339             (setcar (cdar comp-subs) s)
3340             (incf total s)))
3341         ;; Take care of the "1.0" spec.
3342         (if rest
3343             (setcar (cdr rest) (- len total))
3344           (error "No 1.0 specs in %s" split))
3345         ;; The we do the actual splitting in a nice recursive
3346         ;; fashion.
3347         (setq comp-subs (nreverse comp-subs))
3348         (while comp-subs
3349           (if (null (cdr comp-subs))
3350               (setq new-win window)
3351             (setq new-win
3352                   (split-window window (cadar comp-subs)
3353                                 (eq type 'horizontal))))
3354           (setq result (or (gnus-configure-frame
3355                             (car comp-subs) window) result))
3356           (select-window new-win)
3357           (setq window new-win)
3358           (setq comp-subs (cdr comp-subs))))
3359       ;; Return the proper window, if any.
3360       (when result
3361         (select-window result))))))
3362
3363 (defvar gnus-frame-split-p nil)
3364
3365 (defun gnus-configure-windows (setting &optional force)
3366   (setq setting (gnus-windows-old-to-new setting))
3367   (let ((split (if (symbolp setting)
3368                    (cadr (assq setting gnus-buffer-configuration))
3369                  setting))
3370         all-visible)
3371
3372     (setq gnus-frame-split-p nil)
3373
3374     (unless split
3375       (error "No such setting: %s" setting))
3376
3377     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3378              (not force))
3379         ;; All the windows mentioned are already visible, so we just
3380         ;; put point in the assigned buffer, and do not touch the
3381         ;; winconf.
3382         (select-window all-visible)
3383
3384       ;; Either remove all windows or just remove all Gnus windows.
3385       (let ((frame (selected-frame)))
3386         (unwind-protect
3387             (if gnus-use-full-window
3388                 ;; We want to remove all other windows.
3389                 (if (not gnus-frame-split-p)
3390                     ;; This is not a `frame' split, so we ignore the
3391                     ;; other frames.  
3392                     (delete-other-windows)
3393                   ;; This is a `frame' split, so we delete all windows
3394                   ;; on all frames.
3395                   (mapcar 
3396                    (lambda (frame)
3397                      (unless (eq (cdr (assq 'minibuffer
3398                                             (frame-parameters frame)))
3399                                  'only)
3400                        (select-frame frame)
3401                        (delete-other-windows)))
3402                    (frame-list)))
3403               ;; Just remove some windows.
3404               (gnus-remove-some-windows)
3405               (switch-to-buffer nntp-server-buffer))
3406           (select-frame frame)))
3407
3408       (switch-to-buffer nntp-server-buffer)
3409       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3410
3411 (defun gnus-all-windows-visible-p (split)
3412   "Say whether all buffers in SPLIT are currently visible.
3413 In particular, the value returned will be the window that
3414 should have point."
3415   (let ((stack (list split))
3416         (all-visible t)
3417         type buffer win buf)
3418     (while (and (setq split (pop stack))
3419                 all-visible)
3420       ;; Be backwards compatible.
3421       (when (vectorp split)
3422         (setq split (append split nil)))
3423       (when (or (consp (car split))
3424                 (vectorp (car split)))
3425         (push 1.0 split)
3426         (push 'vertical split))
3427       ;; The SPLIT might be something that is to be evaled to
3428       ;; return a new SPLIT.
3429       (while (and (not (assq (car split) gnus-window-to-buffer))
3430                   (gnus-functionp (car split)))
3431         (setq split (eval split)))
3432
3433       (setq type (elt split 0))
3434       (cond
3435        ;; Nothing here.
3436        ((null split) t)
3437        ;; A buffer.
3438        ((not (memq type '(horizontal vertical frame)))
3439         (setq buffer (cond ((stringp type) type)
3440                            (t (cdr (assq type gnus-window-to-buffer)))))
3441         (unless buffer
3442           (error "Illegal buffer type: %s" type))
3443         (when (setq buf (get-buffer (if (symbolp buffer)
3444                                         (symbol-value buffer)
3445                                       buffer)))
3446           (setq win (get-buffer-window buf t)))
3447         (if win
3448             (when (memq 'point split)
3449                 (setq all-visible win))
3450           (setq all-visible nil)))
3451        (t
3452         (when (eq type 'frame)
3453           (setq gnus-frame-split-p t))
3454         (setq stack (append (cddr split) stack)))))
3455     (unless (eq all-visible t)
3456       all-visible)))
3457
3458 (defun gnus-window-top-edge (&optional window)
3459   (nth 1 (window-edges window)))
3460
3461 (defun gnus-remove-some-windows ()
3462   (let ((buffers gnus-window-to-buffer)
3463         buf bufs lowest-buf lowest)
3464     (save-excursion
3465       ;; Remove windows on all known Gnus buffers.
3466       (while buffers
3467         (setq buf (cdar buffers))
3468         (if (symbolp buf)
3469             (setq buf (and (boundp buf) (symbol-value buf))))
3470         (and buf
3471              (get-buffer-window buf)
3472              (progn
3473                (setq bufs (cons buf bufs))
3474                (pop-to-buffer buf)
3475                (if (or (not lowest)
3476                        (< (gnus-window-top-edge) lowest))
3477                    (progn
3478                      (setq lowest (gnus-window-top-edge))
3479                      (setq lowest-buf buf)))))
3480         (setq buffers (cdr buffers)))
3481       ;; Remove windows on *all* summary buffers.
3482       (walk-windows
3483        (lambda (win)
3484          (let ((buf (window-buffer win)))
3485            (if (string-match    "^\\*Summary" (buffer-name buf))
3486                (progn
3487                  (setq bufs (cons buf bufs))
3488                  (pop-to-buffer buf)
3489                  (if (or (not lowest)
3490                          (< (gnus-window-top-edge) lowest))
3491                      (progn
3492                        (setq lowest-buf buf)
3493                        (setq lowest (gnus-window-top-edge)))))))))
3494       (and lowest-buf
3495            (progn
3496              (pop-to-buffer lowest-buf)
3497              (switch-to-buffer nntp-server-buffer)))
3498       (while bufs
3499         (and (not (eq (car bufs) lowest-buf))
3500              (delete-windows-on (car bufs)))
3501         (setq bufs (cdr bufs))))))
3502
3503 (defun gnus-version ()
3504   "Version numbers of this version of Gnus."
3505   (interactive)
3506   (let ((methods gnus-valid-select-methods)
3507         (mess gnus-version)
3508         meth)
3509     ;; Go through all the legal select methods and add their version
3510     ;; numbers to the total version string.  Only the backends that are
3511     ;; currently in use will have their message numbers taken into
3512     ;; consideration.
3513     (while methods
3514       (setq meth (intern (concat (caar methods) "-version")))
3515       (and (boundp meth)
3516            (stringp (symbol-value meth))
3517            (setq mess (concat mess "; " (symbol-value meth))))
3518       (setq methods (cdr methods)))
3519     (gnus-message 2 mess)))
3520
3521 (defun gnus-info-find-node ()
3522   "Find Info documentation of Gnus."
3523   (interactive)
3524   ;; Enlarge info window if needed.
3525   (let ((mode major-mode)
3526         gnus-info-buffer)
3527     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3528     (setq gnus-info-buffer (current-buffer))
3529     (gnus-configure-windows 'info)))
3530
3531 (defun gnus-days-between (date1 date2)
3532   ;; Return the number of days between date1 and date2.
3533   (- (gnus-day-number date1) (gnus-day-number date2)))
3534
3535 (defun gnus-day-number (date)
3536   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3537                      (timezone-parse-date date))))
3538     (timezone-absolute-from-gregorian
3539      (nth 1 dat) (nth 2 dat) (car dat))))
3540
3541 (defun gnus-encode-date (date)
3542   "Convert DATE to internal time."
3543   (let* ((parse (timezone-parse-date date))
3544          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3545          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3546     (encode-time (caddr time) (cadr time) (car time)
3547                  (caddr date) (cadr date) (car date) (nth 4 date))))
3548
3549 (defun gnus-time-minus (t1 t2)
3550   "Subtract two internal times."
3551   (let ((borrow (< (cadr t1) (cadr t2))))
3552     (list (- (car t1) (car t2) (if borrow 1 0))
3553           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3554
3555 (defun gnus-file-newer-than (file date)
3556   (let ((fdate (nth 5 (file-attributes file))))
3557     (or (> (car fdate) (car date))
3558         (and (= (car fdate) (car date))
3559              (> (nth 1 fdate) (nth 1 date))))))
3560
3561 (defmacro gnus-local-set-keys (&rest plist)
3562   "Set the keys in PLIST in the current keymap."
3563   `(gnus-define-keys-1 (current-local-map) ',plist))
3564
3565 (defmacro gnus-define-keys (keymap &rest plist)
3566   "Define all keys in PLIST in KEYMAP."
3567   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3568
3569 (put 'gnus-define-keys 'lisp-indent-function 1)
3570 (put 'gnus-define-keys 'lisp-indent-hook 1)
3571 (put 'gnus-define-keymap 'lisp-indent-function 1)
3572 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3573
3574 (defmacro gnus-define-keymap (keymap &rest plist)
3575   "Define all keys in PLIST in KEYMAP."
3576   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3577
3578 (defun gnus-define-keys-1 (keymap plist)
3579   (when (null keymap)
3580     (error "Can't set keys in a null keymap"))
3581   (cond ((symbolp keymap)
3582          (setq keymap (symbol-value keymap)))
3583         ((keymapp keymap))
3584         ((listp keymap)
3585          (set (car keymap) nil)
3586          (define-prefix-command (car keymap))
3587          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3588          (setq keymap (symbol-value (car keymap)))))
3589   (let (key)
3590     (while plist
3591       (when (symbolp (setq key (pop plist)))
3592         (setq key (symbol-value key)))
3593       (define-key keymap key (pop plist)))))
3594
3595 (defun gnus-group-read-only-p (&optional group)
3596   "Check whether GROUP supports editing or not.
3597 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3598 that that variable is buffer-local to the summary buffers."
3599   (let ((group (or group gnus-newsgroup-name)))
3600     (not (gnus-check-backend-function 'request-replace-article group))))
3601
3602 (defun gnus-group-total-expirable-p (group)
3603   "Check whether GROUP is total-expirable or not."
3604   (let ((params (gnus-info-params (gnus-get-info group))))
3605     (or (memq 'total-expire params)
3606         (cdr (assq 'total-expire params)) ; (total-expire . t)
3607         (and gnus-total-expirable-newsgroups ; Check var.
3608              (string-match gnus-total-expirable-newsgroups group)))))
3609
3610 (defun gnus-group-auto-expirable-p (group)
3611   "Check whether GROUP is total-expirable or not."
3612   (let ((params (gnus-info-params (gnus-get-info group))))
3613     (or (memq 'auto-expire params)
3614         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3615         (and gnus-auto-expirable-newsgroups ; Check var.
3616              (string-match gnus-auto-expirable-newsgroups group)))))
3617
3618 (defun gnus-virtual-group-p (group)
3619   "Say whether GROUP is virtual or not."
3620   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3621                         gnus-valid-select-methods)))
3622
3623 (defun gnus-news-group-p (group &optional article)
3624   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3625   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3626       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3627            (eq (gnus-request-type group article) 'news))))
3628
3629 (defsubst gnus-simplify-subject-fully (subject)
3630   "Simplify a subject string according to the user's wishes."
3631   (cond
3632    ((null gnus-summary-gather-subject-limit)
3633     (gnus-simplify-subject-re subject))
3634    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3635     (gnus-simplify-subject-fuzzy subject))
3636    ((numberp gnus-summary-gather-subject-limit)
3637     (gnus-limit-string (gnus-simplify-subject-re subject)
3638                        gnus-summary-gather-subject-limit))
3639    (t
3640     subject)))
3641
3642 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3643   "Check whether two subjects are equal.  If optional argument
3644 simple-first is t, first argument is already simplified."
3645   (cond
3646    ((null simple-first)
3647     (equal (gnus-simplify-subject-fully s1)
3648            (gnus-simplify-subject-fully s2)))
3649    (t
3650     (equal s1
3651            (gnus-simplify-subject-fully s2)))))
3652
3653 ;; Returns a list of writable groups.
3654 (defun gnus-writable-groups ()
3655   (let ((alist gnus-newsrc-alist)
3656         groups group)
3657     (while (setq group (car (pop alist)))
3658       (unless (gnus-group-read-only-p group)
3659         (push group groups)))
3660     (nreverse groups)))
3661
3662 (defun gnus-completing-read (default prompt &rest args)
3663   ;; Like `completing-read', except that DEFAULT is the default argument.
3664   (let* ((prompt (if default 
3665                      (concat prompt " (default " default ") ")
3666                    (concat prompt " ")))
3667          (answer (apply 'completing-read prompt args)))
3668     (if (or (null answer) (zerop (length answer)))
3669         default
3670       answer)))
3671
3672 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3673 ;; the echo area.
3674 (defun gnus-y-or-n-p (prompt)
3675   (prog1
3676       (y-or-n-p prompt)
3677     (message "")))
3678
3679 (defun gnus-yes-or-no-p (prompt)
3680   (prog1
3681       (yes-or-no-p prompt)
3682     (message "")))
3683
3684 ;; Check whether to use long file names.
3685 (defun gnus-use-long-file-name (symbol)
3686   ;; The variable has to be set...
3687   (and gnus-use-long-file-name
3688        ;; If it isn't a list, then we return t.
3689        (or (not (listp gnus-use-long-file-name))
3690            ;; If it is a list, and the list contains `symbol', we
3691            ;; return nil.
3692            (not (memq symbol gnus-use-long-file-name)))))
3693
3694 ;; I suspect there's a better way, but I haven't taken the time to do
3695 ;; it yet. -erik selberg@cs.washington.edu
3696 (defun gnus-dd-mmm (messy-date)
3697   "Return a string like DD-MMM from a big messy string"
3698   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3699                    (error nil))))
3700     (if (not datevec)
3701         "??-???"
3702       (format "%2s-%s"
3703               (condition-case ()
3704                   ;; Make sure leading zeroes are stripped.
3705                   (number-to-string (string-to-number (aref datevec 2)))
3706                 (error "??"))
3707               (capitalize
3708                (or (car
3709                     (nth (1- (string-to-number (aref datevec 1)))
3710                          timezone-months-assoc))
3711                    "???"))))))
3712
3713 (defun gnus-mode-string-quote (string)
3714   "Quote all \"%\" in STRING."
3715   (save-excursion
3716     (gnus-set-work-buffer)
3717     (insert string)
3718     (goto-char (point-min))
3719     (while (search-forward "%" nil t)
3720       (insert "%"))
3721     (buffer-string)))
3722
3723 ;; Make a hash table (default and minimum size is 255).
3724 ;; Optional argument HASHSIZE specifies the table size.
3725 (defun gnus-make-hashtable (&optional hashsize)
3726   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3727
3728 ;; Make a number that is suitable for hashing; bigger than MIN and one
3729 ;; less than 2^x.
3730 (defun gnus-create-hash-size (min)
3731   (let ((i 1))
3732     (while (< i min)
3733       (setq i (* 2 i)))
3734     (1- i)))
3735
3736 ;; Show message if message has a lower level than `gnus-verbose'.
3737 ;; Guideline for numbers:
3738 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3739 ;; for things that take a long time, 7 - not very important messages
3740 ;; on stuff, 9 - messages inside loops.
3741 (defun gnus-message (level &rest args)
3742   (if (<= level gnus-verbose)
3743       (apply 'message args)
3744     ;; We have to do this format thingy here even if the result isn't
3745     ;; shown - the return value has to be the same as the return value
3746     ;; from `message'.
3747     (apply 'format args)))
3748
3749 (defun gnus-error (level &rest args)
3750   "Beep an error if `gnus-verbose' is on LEVEL or less."
3751   (when (<= (floor level) gnus-verbose)
3752     (apply 'message args)
3753     (ding)
3754     (let (duration)
3755       (when (and (floatp level)
3756                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3757         (sit-for duration))))
3758   nil)
3759
3760 ;; Generate a unique new group name.
3761 (defun gnus-generate-new-group-name (leaf)
3762   (let ((name leaf)
3763         (num 0))
3764     (while (gnus-gethash name gnus-newsrc-hashtb)
3765       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3766     name))
3767
3768 (defsubst gnus-hide-text (b e props)
3769   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3770   (gnus-add-text-properties b e props)
3771   (when (memq 'intangible props)
3772     (gnus-put-text-property (max (1- b) (point-min))
3773                        b 'intangible (cddr (memq 'intangible props)))))
3774
3775 (defsubst gnus-unhide-text (b e)
3776   "Remove hidden text properties from region between B and E."
3777   (remove-text-properties b e gnus-hidden-properties)
3778   (when (memq 'intangible gnus-hidden-properties)
3779     (gnus-put-text-property (max (1- b) (point-min))
3780                        b 'intangible nil)))
3781
3782 (defun gnus-hide-text-type (b e type)
3783   "Hide text of TYPE between B and E."
3784   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3785
3786 ;; Find out whether the gnus-visual TYPE is wanted.
3787 (defun gnus-visual-p (&optional type class)
3788   (and gnus-visual                      ; Has to be non-nil, at least.
3789        (if (not type)                   ; We don't care about type.
3790            gnus-visual
3791          (if (listp gnus-visual)        ; It's a list, so we check it.
3792              (or (memq type gnus-visual)
3793                  (memq class gnus-visual))
3794            t))))
3795
3796 (defun gnus-parent-headers (headers &optional generation)
3797   "Return the headers of the GENERATIONeth parent of HEADERS."
3798   (unless generation 
3799     (setq generation 1))
3800   (let (references parent)
3801     (while (and headers (not (zerop generation)))
3802       (setq references (mail-header-references headers))
3803       (when (and references
3804                  (setq parent (gnus-parent-id references))
3805                  (setq headers (car (gnus-id-to-thread parent))))
3806         (decf generation)))
3807     headers))
3808
3809 (defun gnus-parent-id (references)
3810   "Return the last Message-ID in REFERENCES."
3811   (when (and references
3812              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3813     (substring references (match-beginning 1) (match-end 1))))
3814
3815 (defun gnus-split-references (references)
3816   "Return a list of Message-IDs in REFERENCES."
3817   (let ((beg 0)
3818         ids)
3819     (while (string-match "<[^>]+>" references beg)
3820       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3821             ids))
3822     (nreverse ids)))
3823
3824 (defun gnus-buffer-live-p (buffer)
3825   "Say whether BUFFER is alive or not."
3826   (and buffer
3827        (get-buffer buffer)
3828        (buffer-name (get-buffer buffer))))
3829
3830 (defun gnus-ephemeral-group-p (group)
3831   "Say whether GROUP is ephemeral or not."
3832   (gnus-group-get-parameter group 'quit-config))
3833
3834 (defun gnus-group-quit-config (group)
3835   "Return the quit-config of GROUP."
3836   (gnus-group-get-parameter group 'quit-config))
3837
3838 (defun gnus-simplify-mode-line ()
3839   "Make mode lines a bit simpler."
3840   (setq mode-line-modified "-- ")
3841   (when (listp mode-line-format)
3842     (make-local-variable 'mode-line-format)
3843     (setq mode-line-format (copy-sequence mode-line-format))
3844     (when (equal (nth 3 mode-line-format) "   ")
3845       (setcar (nthcdr 3 mode-line-format) " "))))
3846
3847 ;;; List and range functions
3848
3849 (defun gnus-last-element (list)
3850   "Return last element of LIST."
3851   (while (cdr list)
3852     (setq list (cdr list)))
3853   (car list))
3854
3855 (defun gnus-copy-sequence (list)
3856   "Do a complete, total copy of a list."
3857   (if (and (consp list) (not (consp (cdr list))))
3858       (cons (car list) (cdr list))
3859     (mapcar (lambda (elem) (if (consp elem)
3860                                (if (consp (cdr elem))
3861                                    (gnus-copy-sequence elem)
3862                                  (cons (car elem) (cdr elem)))
3863                              elem))
3864             list)))
3865
3866 (defun gnus-set-difference (list1 list2)
3867   "Return a list of elements of LIST1 that do not appear in LIST2."
3868   (let ((list1 (copy-sequence list1)))
3869     (while list2
3870       (setq list1 (delq (car list2) list1))
3871       (setq list2 (cdr list2)))
3872     list1))
3873
3874 (defun gnus-sorted-complement (list1 list2)
3875   "Return a list of elements of LIST1 that do not appear in LIST2.
3876 Both lists have to be sorted over <."
3877   (let (out)
3878     (if (or (null list1) (null list2))
3879         (or list1 list2)
3880       (while (and list1 list2)
3881         (cond ((= (car list1) (car list2))
3882                (setq list1 (cdr list1)
3883                      list2 (cdr list2)))
3884               ((< (car list1) (car list2))
3885                (setq out (cons (car list1) out))
3886                (setq list1 (cdr list1)))
3887               (t
3888                (setq out (cons (car list2) out))
3889                (setq list2 (cdr list2)))))
3890       (nconc (nreverse out) (or list1 list2)))))
3891
3892 (defun gnus-intersection (list1 list2)
3893   (let ((result nil))
3894     (while list2
3895       (if (memq (car list2) list1)
3896           (setq result (cons (car list2) result)))
3897       (setq list2 (cdr list2)))
3898     result))
3899
3900 (defun gnus-sorted-intersection (list1 list2)
3901   ;; LIST1 and LIST2 have to be sorted over <.
3902   (let (out)
3903     (while (and list1 list2)
3904       (cond ((= (car list1) (car list2))
3905              (setq out (cons (car list1) out)
3906                    list1 (cdr list1)
3907                    list2 (cdr list2)))
3908             ((< (car list1) (car list2))
3909              (setq list1 (cdr list1)))
3910             (t
3911              (setq list2 (cdr list2)))))
3912     (nreverse out)))
3913
3914 (defun gnus-set-sorted-intersection (list1 list2)
3915   ;; LIST1 and LIST2 have to be sorted over <.
3916   ;; This function modifies LIST1.
3917   (let* ((top (cons nil list1))
3918          (prev top))
3919     (while (and list1 list2)
3920       (cond ((= (car list1) (car list2))
3921              (setq prev list1
3922                    list1 (cdr list1)
3923                    list2 (cdr list2)))
3924             ((< (car list1) (car list2))
3925              (setcdr prev (cdr list1))
3926              (setq list1 (cdr list1)))
3927             (t
3928              (setq list2 (cdr list2)))))
3929     (setcdr prev nil)
3930     (cdr top)))
3931
3932 (defun gnus-compress-sequence (numbers &optional always-list)
3933   "Convert list of numbers to a list of ranges or a single range.
3934 If ALWAYS-LIST is non-nil, this function will always release a list of
3935 ranges."
3936   (let* ((first (car numbers))
3937          (last (car numbers))
3938          result)
3939     (if (null numbers)
3940         nil
3941       (if (not (listp (cdr numbers)))
3942           numbers
3943         (while numbers
3944           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3945                 ((= (1+ last) (car numbers)) ;Still in sequence
3946                  (setq last (car numbers)))
3947                 (t                      ;End of one sequence
3948                  (setq result
3949                        (cons (if (= first last) first
3950                                (cons first last)) result))
3951                  (setq first (car numbers))
3952                  (setq last  (car numbers))))
3953           (setq numbers (cdr numbers)))
3954         (if (and (not always-list) (null result))
3955             (if (= first last) (list first) (cons first last))
3956           (nreverse (cons (if (= first last) first (cons first last))
3957                           result)))))))
3958
3959 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3960 (defun gnus-uncompress-range (ranges)
3961   "Expand a list of ranges into a list of numbers.
3962 RANGES is either a single range on the form `(num . num)' or a list of
3963 these ranges."
3964   (let (first last result)
3965     (cond
3966      ((null ranges)
3967       nil)
3968      ((not (listp (cdr ranges)))
3969       (setq first (car ranges))
3970       (setq last (cdr ranges))
3971       (while (<= first last)
3972         (setq result (cons first result))
3973         (setq first (1+ first)))
3974       (nreverse result))
3975      (t
3976       (while ranges
3977         (if (atom (car ranges))
3978             (if (numberp (car ranges))
3979                 (setq result (cons (car ranges) result)))
3980           (setq first (caar ranges))
3981           (setq last  (cdar ranges))
3982           (while (<= first last)
3983             (setq result (cons first result))
3984             (setq first (1+ first))))
3985         (setq ranges (cdr ranges)))
3986       (nreverse result)))))
3987
3988 (defun gnus-add-to-range (ranges list)
3989   "Return a list of ranges that has all articles from both RANGES and LIST.
3990 Note: LIST has to be sorted over `<'."
3991   (if (not ranges)
3992       (gnus-compress-sequence list t)
3993     (setq list (copy-sequence list))
3994     (or (listp (cdr ranges))
3995         (setq ranges (list ranges)))
3996     (let ((out ranges)
3997           ilist lowest highest temp)
3998       (while (and ranges list)
3999         (setq ilist list)
4000         (setq lowest (or (and (atom (car ranges)) (car ranges))
4001                          (caar ranges)))
4002         (while (and list (cdr list) (< (cadr list) lowest))
4003           (setq list (cdr list)))
4004         (if (< (car ilist) lowest)
4005             (progn
4006               (setq temp list)
4007               (setq list (cdr list))
4008               (setcdr temp nil)
4009               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4010         (setq highest (or (and (atom (car ranges)) (car ranges))
4011                           (cdar ranges)))
4012         (while (and list (<= (car list) highest))
4013           (setq list (cdr list)))
4014         (setq ranges (cdr ranges)))
4015       (if list
4016           (setq out (nconc (gnus-compress-sequence list t) out)))
4017       (setq out (sort out (lambda (r1 r2)
4018                             (< (or (and (atom r1) r1) (car r1))
4019                                (or (and (atom r2) r2) (car r2))))))
4020       (setq ranges out)
4021       (while ranges
4022         (if (atom (car ranges))
4023             (if (cdr ranges)
4024                 (if (atom (cadr ranges))
4025                     (if (= (1+ (car ranges)) (cadr ranges))
4026                         (progn
4027                           (setcar ranges (cons (car ranges)
4028                                                (cadr ranges)))
4029                           (setcdr ranges (cddr ranges))))
4030                   (if (= (1+ (car ranges)) (caadr ranges))
4031                       (progn
4032                         (setcar (cadr ranges) (car ranges))
4033                         (setcar ranges (cadr ranges))
4034                         (setcdr ranges (cddr ranges))))))
4035           (if (cdr ranges)
4036               (if (atom (cadr ranges))
4037                   (if (= (1+ (cdar ranges)) (cadr ranges))
4038                       (progn
4039                         (setcdr (car ranges) (cadr ranges))
4040                         (setcdr ranges (cddr ranges))))
4041                 (if (= (1+ (cdar ranges)) (caadr ranges))
4042                     (progn
4043                       (setcdr (car ranges) (cdadr ranges))
4044                       (setcdr ranges (cddr ranges)))))))
4045         (setq ranges (cdr ranges)))
4046       out)))
4047
4048 (defun gnus-remove-from-range (ranges list)
4049   "Return a list of ranges that has all articles from LIST removed from RANGES.
4050 Note: LIST has to be sorted over `<'."
4051   ;; !!! This function shouldn't look like this, but I've got a headache.
4052   (gnus-compress-sequence
4053    (gnus-sorted-complement
4054     (gnus-uncompress-range ranges) list)))
4055
4056 (defun gnus-member-of-range (number ranges)
4057   (if (not (listp (cdr ranges)))
4058       (and (>= number (car ranges))
4059            (<= number (cdr ranges)))
4060     (let ((not-stop t))
4061       (while (and ranges
4062                   (if (numberp (car ranges))
4063                       (>= number (car ranges))
4064                     (>= number (caar ranges)))
4065                   not-stop)
4066         (if (if (numberp (car ranges))
4067                 (= number (car ranges))
4068               (and (>= number (caar ranges))
4069                    (<= number (cdar ranges))))
4070             (setq not-stop nil))
4071         (setq ranges (cdr ranges)))
4072       (not not-stop))))
4073
4074 (defun gnus-range-length (range)
4075   "Return the length RANGE would have if uncompressed."
4076   (length (gnus-uncompress-range range)))
4077
4078 (defun gnus-sublist-p (list sublist)
4079   "Test whether all elements in SUBLIST are members of LIST."
4080   (let ((sublistp t))
4081     (while sublist
4082       (unless (memq (pop sublist) list)
4083         (setq sublistp nil
4084               sublist nil)))
4085     sublistp))
4086
4087 \f
4088 ;;;
4089 ;;; Gnus group mode
4090 ;;;
4091
4092 (defvar gnus-group-mode-map nil)
4093 (put 'gnus-group-mode 'mode-class 'special)
4094
4095 (unless gnus-group-mode-map
4096   (setq gnus-group-mode-map (make-keymap))
4097   (suppress-keymap gnus-group-mode-map)
4098
4099   (gnus-define-keys gnus-group-mode-map
4100     " " gnus-group-read-group
4101     "=" gnus-group-select-group
4102     "\r" gnus-group-select-group
4103     "\M-\r" gnus-group-quick-select-group
4104     "j" gnus-group-jump-to-group
4105     "n" gnus-group-next-unread-group
4106     "p" gnus-group-prev-unread-group
4107     "\177" gnus-group-prev-unread-group
4108     [delete] gnus-group-prev-unread-group
4109     "N" gnus-group-next-group
4110     "P" gnus-group-prev-group
4111     "\M-n" gnus-group-next-unread-group-same-level
4112     "\M-p" gnus-group-prev-unread-group-same-level
4113     "," gnus-group-best-unread-group
4114     "." gnus-group-first-unread-group
4115     "u" gnus-group-unsubscribe-current-group
4116     "U" gnus-group-unsubscribe-group
4117     "c" gnus-group-catchup-current
4118     "C" gnus-group-catchup-current-all
4119     "l" gnus-group-list-groups
4120     "L" gnus-group-list-all-groups
4121     "m" gnus-group-mail
4122     "g" gnus-group-get-new-news
4123     "\M-g" gnus-group-get-new-news-this-group
4124     "R" gnus-group-restart
4125     "r" gnus-group-read-init-file
4126     "B" gnus-group-browse-foreign-server
4127     "b" gnus-group-check-bogus-groups
4128     "F" gnus-find-new-newsgroups
4129     "\C-c\C-d" gnus-group-describe-group
4130     "\M-d" gnus-group-describe-all-groups
4131     "\C-c\C-a" gnus-group-apropos
4132     "\C-c\M-\C-a" gnus-group-description-apropos
4133     "a" gnus-group-post-news
4134     "\ek" gnus-group-edit-local-kill
4135     "\eK" gnus-group-edit-global-kill
4136     "\C-k" gnus-group-kill-group
4137     "\C-y" gnus-group-yank-group
4138     "\C-w" gnus-group-kill-region
4139     "\C-x\C-t" gnus-group-transpose-groups
4140     "\C-c\C-l" gnus-group-list-killed
4141     "\C-c\C-x" gnus-group-expire-articles
4142     "\C-c\M-\C-x" gnus-group-expire-all-groups
4143     "V" gnus-version
4144     "s" gnus-group-save-newsrc
4145     "z" gnus-group-suspend
4146     "Z" gnus-group-clear-dribble
4147     "q" gnus-group-exit
4148     "Q" gnus-group-quit
4149     "?" gnus-group-describe-briefly
4150     "\C-c\C-i" gnus-info-find-node
4151     "\M-e" gnus-group-edit-group-method
4152     "^" gnus-group-enter-server-mode
4153     gnus-mouse-2 gnus-mouse-pick-group
4154     "<" beginning-of-buffer
4155     ">" end-of-buffer
4156     "\C-c\C-b" gnus-bug
4157     "\C-c\C-s" gnus-group-sort-groups
4158     "t" gnus-topic-mode
4159     "\C-c\M-g" gnus-activate-all-groups
4160     "\M-&" gnus-group-universal-argument
4161     "#" gnus-group-mark-group
4162     "\M-#" gnus-group-unmark-group)
4163
4164   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4165     "m" gnus-group-mark-group
4166     "u" gnus-group-unmark-group
4167     "w" gnus-group-mark-region
4168     "m" gnus-group-mark-buffer
4169     "r" gnus-group-mark-regexp
4170     "U" gnus-group-unmark-all-groups)
4171
4172   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4173     "d" gnus-group-make-directory-group
4174     "h" gnus-group-make-help-group
4175     "a" gnus-group-make-archive-group
4176     "k" gnus-group-make-kiboze-group
4177     "m" gnus-group-make-group
4178     "E" gnus-group-edit-group
4179     "e" gnus-group-edit-group-method
4180     "p" gnus-group-edit-group-parameters
4181     "v" gnus-group-add-to-virtual
4182     "V" gnus-group-make-empty-virtual
4183     "D" gnus-group-enter-directory
4184     "f" gnus-group-make-doc-group
4185     "r" gnus-group-rename-group
4186     "\177" gnus-group-delete-group
4187     [delete] gnus-group-delete-group)
4188
4189    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4190      "b" gnus-group-brew-soup
4191      "w" gnus-soup-save-areas
4192      "s" gnus-soup-send-replies
4193      "p" gnus-soup-pack-packet
4194      "r" nnsoup-pack-replies)
4195
4196    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4197      "s" gnus-group-sort-groups
4198      "a" gnus-group-sort-groups-by-alphabet
4199      "u" gnus-group-sort-groups-by-unread
4200      "l" gnus-group-sort-groups-by-level
4201      "v" gnus-group-sort-groups-by-score
4202      "r" gnus-group-sort-groups-by-rank
4203      "m" gnus-group-sort-groups-by-method)
4204
4205    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4206      "k" gnus-group-list-killed
4207      "z" gnus-group-list-zombies
4208      "s" gnus-group-list-groups
4209      "u" gnus-group-list-all-groups
4210      "A" gnus-group-list-active
4211      "a" gnus-group-apropos
4212      "d" gnus-group-description-apropos
4213      "m" gnus-group-list-matching
4214      "M" gnus-group-list-all-matching
4215      "l" gnus-group-list-level)
4216
4217    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4218      "f" gnus-score-flush-cache)
4219
4220    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4221      "f" gnus-group-fetch-faq)
4222
4223    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4224      "l" gnus-group-set-current-level
4225      "t" gnus-group-unsubscribe-current-group
4226      "s" gnus-group-unsubscribe-group
4227      "k" gnus-group-kill-group
4228      "y" gnus-group-yank-group
4229      "w" gnus-group-kill-region
4230      "\C-k" gnus-group-kill-level
4231      "z" gnus-group-kill-all-zombies))
4232
4233 (defun gnus-group-mode ()
4234   "Major mode for reading news.
4235
4236 All normal editing commands are switched off.
4237 \\<gnus-group-mode-map>
4238 The group buffer lists (some of) the groups available.  For instance,
4239 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4240 lists all zombie groups.
4241
4242 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4243 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4244
4245 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4246
4247 The following commands are available:
4248
4249 \\{gnus-group-mode-map}"
4250   (interactive)
4251   (when (and menu-bar-mode
4252              (gnus-visual-p 'group-menu 'menu))
4253     (gnus-group-make-menu-bar))
4254   (kill-all-local-variables)
4255   (gnus-simplify-mode-line)
4256   (setq major-mode 'gnus-group-mode)
4257   (setq mode-name "Group")
4258   (gnus-group-set-mode-line)
4259   (setq mode-line-process nil)
4260   (use-local-map gnus-group-mode-map)
4261   (buffer-disable-undo (current-buffer))
4262   (setq truncate-lines t)
4263   (setq buffer-read-only t)
4264   (run-hooks 'gnus-group-mode-hook))
4265
4266 (defun gnus-mouse-pick-group (e)
4267   "Enter the group under the mouse pointer."
4268   (interactive "e")
4269   (mouse-set-point e)
4270   (gnus-group-read-group nil))
4271
4272 ;; Look at LEVEL and find out what the level is really supposed to be.
4273 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4274 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4275 (defun gnus-group-default-level (&optional level number-or-nil)
4276   (cond
4277    (gnus-group-use-permanent-levels
4278     (or (setq gnus-group-use-permanent-levels
4279               (or level (if (numberp gnus-group-use-permanent-levels)
4280                             gnus-group-use-permanent-levels
4281                           (or gnus-group-default-list-level
4282                               gnus-level-subscribed))))
4283         gnus-group-default-list-level gnus-level-subscribed))
4284    (number-or-nil
4285     level)
4286    (t
4287     (or level gnus-group-default-list-level gnus-level-subscribed))))
4288
4289 ;;;###autoload
4290 (defun gnus-slave-no-server (&optional arg)
4291   "Read network news as a slave, without connecting to local server"
4292   (interactive "P")
4293   (gnus-no-server arg t))
4294
4295 ;;;###autoload
4296 (defun gnus-no-server (&optional arg slave)
4297   "Read network news.
4298 If ARG is a positive number, Gnus will use that as the
4299 startup level.  If ARG is nil, Gnus will be started at level 2.
4300 If ARG is non-nil and not a positive number, Gnus will
4301 prompt the user for the name of an NNTP server to use.
4302 As opposed to `gnus', this command will not connect to the local server."
4303   (interactive "P")
4304   (let ((val (or arg (1- gnus-level-default-subscribed))))
4305     (gnus val t slave)
4306     (make-local-variable 'gnus-group-use-permanent-levels)
4307     (setq gnus-group-use-permanent-levels val)))
4308
4309 ;;;###autoload
4310 (defun gnus-slave (&optional arg)
4311   "Read news as a slave."
4312   (interactive "P")
4313   (gnus arg nil 'slave))
4314
4315 ;;;###autoload
4316 (defun gnus-other-frame (&optional arg)
4317   "Pop up a frame to read news."
4318   (interactive "P")
4319   (if (get-buffer gnus-group-buffer)
4320       (let ((pop-up-frames t))
4321         (gnus arg))
4322     (select-frame (make-frame))
4323     (gnus arg)))
4324
4325 ;;;###autoload
4326 (defun gnus (&optional arg dont-connect slave)
4327   "Read network news.
4328 If ARG is non-nil and a positive number, Gnus will use that as the
4329 startup level.  If ARG is non-nil and not a positive number, Gnus will
4330 prompt the user for the name of an NNTP server to use."
4331   (interactive "P")
4332
4333   (if (get-buffer gnus-group-buffer)
4334       (progn
4335         (switch-to-buffer gnus-group-buffer)
4336         (gnus-group-get-new-news))
4337
4338     (gnus-clear-system)
4339     (nnheader-init-server-buffer)
4340     (gnus-read-init-file)
4341     (setq gnus-slave slave)
4342
4343     (gnus-group-setup-buffer)
4344     (let ((buffer-read-only nil))
4345       (erase-buffer)
4346       (if (not gnus-inhibit-startup-message)
4347           (progn
4348             (gnus-group-startup-message)
4349             (sit-for 0))))
4350
4351     (let ((level (and (numberp arg) (> arg 0) arg))
4352           did-connect)
4353       (unwind-protect
4354           (progn
4355             (or dont-connect
4356                 (setq did-connect
4357                       (gnus-start-news-server (and arg (not level))))))
4358         (if (and (not dont-connect)
4359                  (not did-connect))
4360             (gnus-group-quit)
4361           (run-hooks 'gnus-startup-hook)
4362           ;; NNTP server is successfully open.
4363
4364           ;; Find the current startup file name.
4365           (setq gnus-current-startup-file
4366                 (gnus-make-newsrc-file gnus-startup-file))
4367
4368           ;; Read the dribble file.
4369           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4370
4371           ;; Allow using GroupLens predictions.
4372           (when gnus-use-grouplens
4373             (bbb-login)
4374             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4375
4376           (gnus-summary-make-display-table)
4377           ;; Do the actual startup.
4378           (gnus-setup-news nil level dont-connect)
4379           ;; Generate the group buffer.
4380           (gnus-group-list-groups level)
4381           (gnus-group-first-unread-group)
4382           (gnus-configure-windows 'group)
4383           (gnus-group-set-mode-line))))))
4384
4385 (defun gnus-unload ()
4386   "Unload all Gnus features."
4387   (interactive)
4388   (or (boundp 'load-history)
4389       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4390   (let ((history load-history)
4391         feature)
4392     (while history
4393       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4394            (setq feature (cdr (assq 'provide (car history))))
4395            (unload-feature feature 'force))
4396       (setq history (cdr history)))))
4397
4398 (defun gnus-compile ()
4399   "Byte-compile the user-defined format specs."
4400   (interactive)
4401   (let ((entries gnus-format-specs)
4402         entry gnus-tmp-func)
4403     (save-excursion
4404       (gnus-message 7 "Compiling format specs...")
4405
4406       (while entries
4407         (setq entry (pop entries))
4408         (if (eq (car entry) 'version)
4409             (setq gnus-format-specs (delq entry gnus-format-specs))
4410           (when (and (listp (caddr entry))
4411                      (not (eq 'byte-code (caaddr entry))))
4412             (fset 'gnus-tmp-func
4413                   `(lambda () ,(caddr entry)))
4414             (byte-compile 'gnus-tmp-func)
4415             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4416
4417       (push (cons 'version emacs-version) gnus-format-specs)
4418
4419       (gnus-message 7 "Compiling user specs...done"))))
4420
4421 (defun gnus-indent-rigidly (start end arg)
4422   "Indent rigidly using only spaces and no tabs."
4423   (save-excursion
4424     (save-restriction
4425       (narrow-to-region start end)
4426       (indent-rigidly start end arg)
4427       (goto-char (point-min))
4428       (while (search-forward "\t" nil t)
4429         (replace-match "        " t t)))))
4430
4431 (defun gnus-group-startup-message (&optional x y)
4432   "Insert startup message in current buffer."
4433   ;; Insert the message.
4434   (erase-buffer)
4435   (insert
4436    (format "              %s
4437           _    ___ _             _
4438           _ ___ __ ___  __    _ ___
4439           __   _     ___    __  ___
4440               _           ___     _
4441              _  _ __             _
4442              ___   __            _
4443                    __           _
4444                     _      _   _
4445                    _      _    _
4446                       _  _    _
4447                   __  ___
4448                  _   _ _     _
4449                 _   _
4450               _    _
4451              _    _
4452             _
4453           __
4454
4455 "
4456            ""))
4457   ;; And then hack it.
4458   (gnus-indent-rigidly (point-min) (point-max)
4459                        (/ (max (- (window-width) (or x 46)) 0) 2))
4460   (goto-char (point-min))
4461   (forward-line 1)
4462   (let* ((pheight (count-lines (point-min) (point-max)))
4463          (wheight (window-height))
4464          (rest (- wheight pheight)))
4465     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4466   ;; Fontify some.
4467   (goto-char (point-min))
4468   (and (search-forward "Praxis" nil t)
4469        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4470   (goto-char (point-min))
4471   (let* ((mode-string (gnus-group-set-mode-line)))
4472     (setq mode-line-buffer-identification
4473           (list (concat gnus-version (substring (car mode-string) 4))))
4474     (set-buffer-modified-p t)))
4475
4476 (defun gnus-group-setup-buffer ()
4477   (or (get-buffer gnus-group-buffer)
4478       (progn
4479         (switch-to-buffer gnus-group-buffer)
4480         (gnus-add-current-to-buffer-list)
4481         (gnus-group-mode)
4482         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4483
4484 (defun gnus-group-list-groups (&optional level unread lowest)
4485   "List newsgroups with level LEVEL or lower that have unread articles.
4486 Default is all subscribed groups.
4487 If argument UNREAD is non-nil, groups with no unread articles are also
4488 listed."
4489   (interactive (list (if current-prefix-arg
4490                          (prefix-numeric-value current-prefix-arg)
4491                        (or
4492                         (gnus-group-default-level nil t)
4493                         gnus-group-default-list-level
4494                         gnus-level-subscribed))))
4495   (or level
4496       (setq level (car gnus-group-list-mode)
4497             unread (cdr gnus-group-list-mode)))
4498   (setq level (gnus-group-default-level level))
4499   (gnus-group-setup-buffer)             ;May call from out of group buffer
4500   (gnus-update-format-specifications)
4501   (let ((case-fold-search nil)
4502         (props (text-properties-at (gnus-point-at-bol)))
4503         (group (gnus-group-group-name)))
4504     (set-buffer gnus-group-buffer)
4505     (funcall gnus-group-prepare-function level unread lowest)
4506     (if (zerop (buffer-size))
4507         (gnus-message 5 gnus-no-groups-message)
4508       (goto-char (point-max))
4509       (when (or (not gnus-group-goto-next-group-function)
4510                 (not (funcall gnus-group-goto-next-group-function 
4511                               group props)))
4512         (if (not group)
4513             ;; Go to the first group with unread articles.
4514             (gnus-group-search-forward t)
4515           ;; Find the right group to put point on.  If the current group
4516           ;; has disappeared in the new listing, try to find the next
4517           ;; one.        If no next one can be found, just leave point at the
4518           ;; first newsgroup in the buffer.
4519           (if (not (gnus-goto-char
4520                     (text-property-any
4521                      (point-min) (point-max)
4522                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4523               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4524                 (while (and newsrc
4525                             (not (gnus-goto-char
4526                                   (text-property-any
4527                                    (point-min) (point-max) 'gnus-group
4528                                    (gnus-intern-safe
4529                                     (caar newsrc) gnus-active-hashtb)))))
4530                   (setq newsrc (cdr newsrc)))
4531                 (or newsrc (progn (goto-char (point-max))
4532                                   (forward-line -1)))))))
4533       ;; Adjust cursor point.
4534       (gnus-group-position-point))))
4535
4536 (defun gnus-group-list-level (level &optional all)
4537   "List groups on LEVEL.
4538 If ALL (the prefix), also list groups that have no unread articles."
4539   (interactive "nList groups on level: \nP")
4540   (gnus-group-list-groups level all level))
4541
4542 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4543   "List all newsgroups with unread articles of level LEVEL or lower.
4544 If ALL is non-nil, list groups that have no unread articles.
4545 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4546 If REGEXP, only list groups matching REGEXP."
4547   (set-buffer gnus-group-buffer)
4548   (let ((buffer-read-only nil)
4549         (newsrc (cdr gnus-newsrc-alist))
4550         (lowest (or lowest 1))
4551         info clevel unread group params)
4552     (erase-buffer)
4553     (if (< lowest gnus-level-zombie)
4554         ;; List living groups.
4555         (while newsrc
4556           (setq info (car newsrc)
4557                 group (gnus-info-group info)
4558                 params (gnus-info-params info)
4559                 newsrc (cdr newsrc)
4560                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4561           (and unread                   ; This group might be bogus
4562                (or (not regexp)
4563                    (string-match regexp group))
4564                (<= (setq clevel (gnus-info-level info)) level)
4565                (>= clevel lowest)
4566                (or all                  ; We list all groups?
4567                    (if (eq unread t)    ; Unactivated?
4568                        gnus-group-list-inactive-groups ; We list unactivated 
4569                      (> unread 0))      ; We list groups with unread articles
4570                    (and gnus-list-groups-with-ticked-articles
4571                         (cdr (assq 'tick (gnus-info-marks info))))
4572                                         ; And groups with tickeds
4573                    ;; Check for permanent visibility.
4574                    (and gnus-permanently-visible-groups
4575                         (string-match gnus-permanently-visible-groups
4576                                       group))
4577                    (memq 'visible params)
4578                    (cdr (assq 'visible params)))
4579                (gnus-group-insert-group-line
4580                 group (gnus-info-level info)
4581                 (gnus-info-marks info) unread (gnus-info-method info)))))
4582
4583     ;; List dead groups.
4584     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4585          (gnus-group-prepare-flat-list-dead
4586           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4587           gnus-level-zombie ?Z
4588           regexp))
4589     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4590          (gnus-group-prepare-flat-list-dead
4591           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4592           gnus-level-killed ?K regexp))
4593
4594     (gnus-group-set-mode-line)
4595     (setq gnus-group-list-mode (cons level all))
4596     (run-hooks 'gnus-group-prepare-hook)))
4597
4598 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4599   ;; List zombies and killed lists somewhat faster, which was
4600   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4601   ;; this by ignoring the group format specification altogether.
4602   (let (group)
4603     (if regexp
4604         ;; This loop is used when listing groups that match some
4605         ;; regexp.
4606         (while groups
4607           (setq group (pop groups))
4608           (when (string-match regexp group)
4609             (gnus-add-text-properties
4610              (point) (prog1 (1+ (point))
4611                        (insert " " mark "     *: " group "\n"))
4612              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4613                    'gnus-unread t
4614                    'gnus-level level))))
4615       ;; This loop is used when listing all groups.
4616       (while groups
4617         (gnus-add-text-properties
4618          (point) (prog1 (1+ (point))
4619                    (insert " " mark "     *: "
4620                            (setq group (pop groups)) "\n"))
4621          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4622                'gnus-unread t
4623                'gnus-level level))))))
4624
4625 (defmacro gnus-group-real-name (group)
4626   "Find the real name of a foreign newsgroup."
4627   `(let ((gname ,group))
4628      (if (string-match ":[^:]+$" gname)
4629          (substring gname (1+ (match-beginning 0)))
4630        gname)))
4631
4632 (defsubst gnus-server-add-address (method)
4633   (let ((method-name (symbol-name (car method))))
4634     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4635              (not (assq (intern (concat method-name "-address")) method)))
4636         (append method (list (list (intern (concat method-name "-address"))
4637                                    (nth 1 method))))
4638       method)))
4639
4640 (defsubst gnus-server-get-method (group method)
4641   ;; Input either a server name, and extended server name, or a
4642   ;; select method, and return a select method.
4643   (cond ((stringp method)
4644          (gnus-server-to-method method))
4645         ((equal method gnus-select-method)
4646          gnus-select-method)
4647         ((and (stringp (car method)) group)
4648          (gnus-server-extend-method group method))
4649         ((and method (not group)
4650               (equal (cadr method) ""))
4651          method)
4652         (t
4653          (gnus-server-add-address method))))
4654
4655 (defun gnus-server-to-method (server)
4656   "Map virtual server names to select methods."
4657   (or 
4658    ;; Is this a method, perhaps?
4659    (and server (listp server) server)
4660    ;; Perhaps this is the native server?
4661    (and (equal server "native") gnus-select-method)
4662    ;; It should be in the server alist.
4663    (cdr (assoc server gnus-server-alist))
4664    ;; If not, we look through all the opened server
4665    ;; to see whether we can find it there.
4666    (let ((opened gnus-opened-servers))
4667      (while (and opened
4668                  (not (equal server (format "%s:%s" (caaar opened)
4669                                             (cadaar opened)))))
4670        (pop opened))
4671      (caar opened))))
4672
4673 (defmacro gnus-method-equal (ss1 ss2)
4674   "Say whether two servers are equal."
4675   `(let ((s1 ,ss1)
4676          (s2 ,ss2))
4677      (or (equal s1 s2)
4678          (and (= (length s1) (length s2))
4679               (progn
4680                 (while (and s1 (member (car s1) s2))
4681                   (setq s1 (cdr s1)))
4682                 (null s1))))))
4683
4684 (defun gnus-server-equal (m1 m2)
4685   "Say whether two methods are equal."
4686   (let ((m1 (cond ((null m1) gnus-select-method)
4687                   ((stringp m1) (gnus-server-to-method m1))
4688                   (t m1)))
4689         (m2 (cond ((null m2) gnus-select-method)
4690                   ((stringp m2) (gnus-server-to-method m2))
4691                   (t m2))))
4692     (gnus-method-equal m1 m2)))
4693
4694 (defun gnus-servers-using-backend (backend)
4695   "Return a list of known servers using BACKEND."
4696   (let ((opened gnus-opened-servers)
4697         out)
4698     (while opened
4699       (when (eq backend (caaar opened))
4700         (push (caar opened) out))
4701       (pop opened))
4702     out))
4703
4704 (defun gnus-group-prefixed-name (group method)
4705   "Return the whole name from GROUP and METHOD."
4706   (and (stringp method) (setq method (gnus-server-to-method method)))
4707   (concat (format "%s" (car method))
4708           (if (and
4709                (or (assoc (format "%s" (car method)) 
4710                           (gnus-methods-using 'address))
4711                    (gnus-server-equal method gnus-message-archive-method))
4712                (nth 1 method)
4713                (not (string= (nth 1 method) "")))
4714               (concat "+" (nth 1 method)))
4715           ":" group))
4716
4717 (defun gnus-group-real-prefix (group)
4718   "Return the prefix of the current group name."
4719   (if (string-match "^[^:]+:" group)
4720       (substring group 0 (match-end 0))
4721     ""))
4722
4723 (defun gnus-group-method (group)
4724   "Return the server or method used for selecting GROUP."
4725   (let ((prefix (gnus-group-real-prefix group)))
4726     (if (equal prefix "")
4727         gnus-select-method
4728       (let ((servers gnus-opened-servers)
4729             (server "")
4730             backend possible found)
4731         (if (string-match "^[^\\+]+\\+" prefix)
4732             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4733                   server (substring prefix (match-end 0) (1- (length prefix))))
4734           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4735         (while servers
4736           (when (eq (caaar servers) backend)
4737             (setq possible (caar servers))
4738             (when (equal (cadaar servers) server)
4739               (setq found (caar servers))))
4740           (pop servers))
4741         (or (car (rassoc found gnus-server-alist))
4742             found
4743             (car (rassoc possible gnus-server-alist))
4744             possible
4745             (list backend server))))))
4746
4747 (defsubst gnus-secondary-method-p (method)
4748   "Return whether METHOD is a secondary select method."
4749   (let ((methods gnus-secondary-select-methods)
4750         (gmethod (gnus-server-get-method nil method)))
4751     (while (and methods
4752                 (not (equal (gnus-server-get-method nil (car methods))
4753                             gmethod)))
4754       (setq methods (cdr methods)))
4755     methods))
4756
4757 (defun gnus-group-foreign-p (group)
4758   "Say whether a group is foreign or not."
4759   (and (not (gnus-group-native-p group))
4760        (not (gnus-group-secondary-p group))))
4761
4762 (defun gnus-group-native-p (group)
4763   "Say whether the group is native or not."
4764   (not (string-match ":" group)))
4765
4766 (defun gnus-group-secondary-p (group)
4767   "Say whether the group is secondary or not."
4768   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4769
4770 (defun gnus-group-get-parameter (group &optional symbol)
4771   "Returns the group parameters for GROUP.
4772 If SYMBOL, return the value of that symbol in the group parameters."
4773   (let ((params (gnus-info-params (gnus-get-info group))))
4774     (if symbol
4775         (gnus-group-parameter-value params symbol)
4776       params)))
4777
4778 (defun gnus-group-parameter-value (params symbol)
4779   "Return the value of SYMBOL in group PARAMS."
4780   (or (car (memq symbol params))        ; It's either a simple symbol
4781       (cdr (assq symbol params))))      ; or a cons.
4782
4783 (defun gnus-group-add-parameter (group param)
4784   "Add parameter PARAM to GROUP."
4785   (let ((info (gnus-get-info group)))
4786     (if (not info)
4787         () ; This is a dead group.  We just ignore it.
4788       ;; Cons the new param to the old one and update.
4789       (gnus-group-set-info (cons param (gnus-info-params info))
4790                            group 'params))))
4791
4792 (defun gnus-group-set-parameter (group name value)
4793   "Set parameter NAME to VALUE in GROUP."
4794   (let ((info (gnus-get-info group)))
4795     (if (not info)
4796         () ; This is a dead group.  We just ignore it.
4797       (let ((old-params (gnus-info-params info))
4798             (new-params (list (cons name value))))
4799         (while old-params
4800           (if (or (not (listp (car old-params)))
4801                   (not (eq (caar old-params) name)))
4802               (setq new-params (append new-params (list (car old-params)))))
4803           (setq old-params (cdr old-params)))
4804         (gnus-group-set-info new-params group 'params)))))
4805
4806 (defun gnus-group-add-score (group &optional score)
4807   "Add SCORE to the GROUP score.
4808 If SCORE is nil, add 1 to the score of GROUP."
4809   (let ((info (gnus-get-info group)))
4810     (when info
4811       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4812
4813 (defun gnus-summary-bubble-group ()
4814   "Increase the score of the current group.
4815 This is a handy function to add to `gnus-summary-exit-hook' to
4816 increase the score of each group you read."
4817   (gnus-group-add-score gnus-newsgroup-name))
4818
4819 (defun gnus-group-set-info (info &optional method-only-group part)
4820   (let* ((entry (gnus-gethash
4821                  (or method-only-group (gnus-info-group info))
4822                  gnus-newsrc-hashtb))
4823          (part-info info)
4824          (info (if method-only-group (nth 2 entry) info))
4825          method)
4826     (when method-only-group
4827       (unless entry
4828         (error "Trying to change non-existent group %s" method-only-group))
4829       ;; We have received parts of the actual group info - either the
4830       ;; select method or the group parameters.  We first check
4831       ;; whether we have to extend the info, and if so, do that.
4832       (let ((len (length info))
4833             (total (if (eq part 'method) 5 6)))
4834         (when (< len total)
4835           (setcdr (nthcdr (1- len) info)
4836                   (make-list (- total len) nil)))
4837         ;; Then we enter the new info.
4838         (setcar (nthcdr (1- total) info) part-info)))
4839     (unless entry
4840       ;; This is a new group, so we just create it.
4841       (save-excursion
4842         (set-buffer gnus-group-buffer)
4843         (setq method (gnus-info-method info))
4844         (when (gnus-server-equal method "native")
4845           (setq method nil))
4846         (save-excursion
4847           (set-buffer gnus-group-buffer)
4848           (if method
4849               ;; It's a foreign group...
4850               (gnus-group-make-group
4851                (gnus-group-real-name (gnus-info-group info))
4852                (if (stringp method) method
4853                  (prin1-to-string (car method)))
4854                (and (consp method)
4855                     (nth 1 (gnus-info-method info))))
4856             ;; It's a native group.
4857             (gnus-group-make-group (gnus-info-group info))))
4858         (gnus-message 6 "Note: New group created")
4859         (setq entry
4860               (gnus-gethash (gnus-group-prefixed-name
4861                              (gnus-group-real-name (gnus-info-group info))
4862                              (or (gnus-info-method info) gnus-select-method))
4863                             gnus-newsrc-hashtb))))
4864     ;; Whether it was a new group or not, we now have the entry, so we
4865     ;; can do the update.
4866     (if entry
4867         (progn
4868           (setcar (nthcdr 2 entry) info)
4869           (when (and (not (eq (car entry) t))
4870                      (gnus-active (gnus-info-group info)))
4871             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4872       (error "No such group: %s" (gnus-info-group info)))))
4873
4874 (defun gnus-group-set-method-info (group select-method)
4875   (gnus-group-set-info select-method group 'method))
4876
4877 (defun gnus-group-set-params-info (group params)
4878   (gnus-group-set-info params group 'params))
4879
4880 (defun gnus-group-update-group-line ()
4881   "Update the current line in the group buffer."
4882   (let* ((buffer-read-only nil)
4883          (group (gnus-group-group-name))
4884          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4885          gnus-group-indentation)
4886     (and entry
4887          (not (gnus-ephemeral-group-p group))
4888          (gnus-dribble-enter
4889           (concat "(gnus-group-set-info '"
4890                   (prin1-to-string (nth 2 entry)) ")")))
4891     (setq gnus-group-indentation (gnus-group-group-indentation))
4892     (gnus-delete-line)
4893     (gnus-group-insert-group-line-info group)
4894     (forward-line -1)
4895     (gnus-group-position-point)))
4896
4897 (defun gnus-group-insert-group-line-info (group)
4898   "Insert GROUP on the current line."
4899   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4900         active info)
4901     (if entry
4902         (progn
4903           ;; (Un)subscribed group.
4904           (setq info (nth 2 entry))
4905           (gnus-group-insert-group-line
4906            group (gnus-info-level info) (gnus-info-marks info)
4907            (or (car entry) t) (gnus-info-method info)))
4908       ;; This group is dead.
4909       (gnus-group-insert-group-line
4910        group
4911        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4912        nil
4913        (if (setq active (gnus-active group))
4914            (- (1+ (cdr active)) (car active)) 0)
4915        nil))))
4916
4917 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4918                                                     gnus-tmp-marked number
4919                                                     gnus-tmp-method)
4920   "Insert a group line in the group buffer."
4921   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4922          (gnus-tmp-number-total
4923           (if gnus-tmp-active
4924               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4925             0))
4926          (gnus-tmp-number-of-unread
4927           (if (numberp number) (int-to-string (max 0 number))
4928             "*"))
4929          (gnus-tmp-number-of-read
4930           (if (numberp number)
4931               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4932             "*"))
4933          (gnus-tmp-subscribed
4934           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4935                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4936                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4937                 (t ?K)))
4938          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4939          (gnus-tmp-newsgroup-description
4940           (if gnus-description-hashtb
4941               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4942             ""))
4943          (gnus-tmp-moderated
4944           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4945          (gnus-tmp-moderated-string
4946           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4947          (gnus-tmp-method
4948           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4949          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4950          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4951          (gnus-tmp-news-method-string
4952           (if gnus-tmp-method
4953               (format "(%s:%s)" (car gnus-tmp-method)
4954                       (cadr gnus-tmp-method)) ""))
4955          (gnus-tmp-marked-mark
4956           (if (and (numberp number)
4957                    (zerop number)
4958                    (cdr (assq 'tick gnus-tmp-marked)))
4959               ?* ? ))
4960          (gnus-tmp-process-marked
4961           (if (member gnus-tmp-group gnus-group-marked)
4962               gnus-process-mark ? ))
4963          (gnus-tmp-grouplens
4964           (or (and gnus-use-grouplens
4965                    (bbb-grouplens-group-p gnus-tmp-group))
4966               ""))
4967          (buffer-read-only nil)
4968          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4969     (beginning-of-line)
4970     (gnus-add-text-properties
4971      (point)
4972      (prog1 (1+ (point))
4973        ;; Insert the text.
4974        (eval gnus-group-line-format-spec))
4975      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4976        gnus-unread ,(if (numberp number)
4977                         (string-to-int gnus-tmp-number-of-unread)
4978                       t)
4979        gnus-marked ,gnus-tmp-marked-mark
4980        gnus-indentation ,gnus-group-indentation
4981        gnus-level ,gnus-tmp-level))
4982     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4983       (forward-line -1)
4984       (run-hooks 'gnus-group-update-hook)
4985       (forward-line))
4986     ;; Allow XEmacs to remove front-sticky text properties.
4987     (gnus-group-remove-excess-properties)))
4988
4989 (defun gnus-group-update-group (group &optional visible-only)
4990   "Update all lines where GROUP appear.
4991 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4992 already."
4993   (save-excursion
4994     (set-buffer gnus-group-buffer)
4995     ;; The buffer may be narrowed.
4996     (save-restriction
4997       (widen)
4998       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4999             (loc (point-min))
5000             found buffer-read-only)
5001         ;; Enter the current status into the dribble buffer.
5002         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5003           (if (and entry (not (gnus-ephemeral-group-p group)))
5004               (gnus-dribble-enter
5005                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5006                        ")"))))
5007         ;; Find all group instances.  If topics are in use, each group
5008         ;; may be listed in more than once.
5009         (while (setq loc (text-property-any
5010                           loc (point-max) 'gnus-group ident))
5011           (setq found t)
5012           (goto-char loc)
5013           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5014             (gnus-delete-line)
5015             (gnus-group-insert-group-line-info group))
5016           (setq loc (1+ loc)))
5017         (unless (or found visible-only)
5018           ;; No such line in the buffer, find out where it's supposed to
5019           ;; go, and insert it there (or at the end of the buffer).
5020           (if gnus-goto-missing-group-function
5021               (funcall gnus-goto-missing-group-function group)
5022             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5023               (while (and entry (car entry)
5024                           (not
5025                            (gnus-goto-char
5026                             (text-property-any
5027                              (point-min) (point-max)
5028                              'gnus-group (gnus-intern-safe
5029                                           (caar entry) gnus-active-hashtb)))))
5030                 (setq entry (cdr entry)))
5031               (or entry (goto-char (point-max)))))
5032           ;; Finally insert the line.
5033           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5034             (gnus-group-insert-group-line-info group)))
5035         (gnus-group-set-mode-line)))))
5036
5037 (defun gnus-group-set-mode-line ()
5038   "Update the mode line in the group buffer."
5039   (when (memq 'group gnus-updated-mode-lines)
5040     ;; Yes, we want to keep this mode line updated.
5041     (save-excursion
5042       (set-buffer gnus-group-buffer)
5043       (let* ((gformat (or gnus-group-mode-line-format-spec
5044                           (setq gnus-group-mode-line-format-spec
5045                                 (gnus-parse-format
5046                                  gnus-group-mode-line-format
5047                                  gnus-group-mode-line-format-alist))))
5048              (gnus-tmp-news-server (cadr gnus-select-method))
5049              (gnus-tmp-news-method (car gnus-select-method))
5050              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5051              (max-len 60)
5052              gnus-tmp-header            ;Dummy binding for user-defined formats
5053              ;; Get the resulting string.
5054              (mode-string (eval gformat)))
5055         ;; Say whether the dribble buffer has been modified.
5056         (setq mode-line-modified
5057               (if (and gnus-dribble-buffer
5058                        (buffer-name gnus-dribble-buffer)
5059                        (buffer-modified-p gnus-dribble-buffer)
5060                        (save-excursion
5061                          (set-buffer gnus-dribble-buffer)
5062                          (not (zerop (buffer-size)))))
5063                   "---*- " "----- "))
5064         ;; If the line is too long, we chop it off.
5065         (when (> (length mode-string) max-len)
5066           (setq mode-string (substring mode-string 0 (- max-len 4))))
5067         (prog1
5068             (setq mode-line-buffer-identification 
5069                   (list mode-string))
5070           (set-buffer-modified-p t))))))
5071
5072 (defun gnus-group-group-name ()
5073   "Get the name of the newsgroup on the current line."
5074   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5075     (and group (symbol-name group))))
5076
5077 (defun gnus-group-group-level ()
5078   "Get the level of the newsgroup on the current line."
5079   (get-text-property (gnus-point-at-bol) 'gnus-level))
5080
5081 (defun gnus-group-group-indentation ()
5082   "Get the indentation of the newsgroup on the current line."
5083   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5084       (and gnus-group-indentation-function
5085            (funcall gnus-group-indentation-function))
5086       ""))
5087
5088 (defun gnus-group-group-unread ()
5089   "Get the number of unread articles of the newsgroup on the current line."
5090   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5091
5092 (defun gnus-group-search-forward (&optional backward all level first-too)
5093   "Find the next newsgroup with unread articles.
5094 If BACKWARD is non-nil, find the previous newsgroup instead.
5095 If ALL is non-nil, just find any newsgroup.
5096 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5097 group exists.
5098 If FIRST-TOO, the current line is also eligible as a target."
5099   (let ((way (if backward -1 1))
5100         (low gnus-level-killed)
5101         (beg (point))
5102         pos found lev)
5103     (if (and backward (progn (beginning-of-line)) (bobp))
5104         nil
5105       (or first-too (forward-line way))
5106       (while (and
5107               (not (eobp))
5108               (not (setq
5109                     found
5110                     (and (or all
5111                              (and
5112                               (let ((unread
5113                                      (get-text-property (point) 'gnus-unread)))
5114                                 (and (numberp unread) (> unread 0)))
5115                               (setq lev (get-text-property (point)
5116                                                            'gnus-level))
5117                               (<= lev gnus-level-subscribed)))
5118                          (or (not level)
5119                              (and (setq lev (get-text-property (point)
5120                                                                'gnus-level))
5121                                   (or (= lev level)
5122                                       (and (< lev low)
5123                                            (< level lev)
5124                                            (progn
5125                                              (setq low lev)
5126                                              (setq pos (point))
5127                                              nil))))))))
5128               (zerop (forward-line way)))))
5129     (if found
5130         (progn (gnus-group-position-point) t)
5131       (goto-char (or pos beg))
5132       (and pos t))))
5133
5134 ;;; Gnus group mode commands
5135
5136 ;; Group marking.
5137
5138 (defun gnus-group-mark-group (n &optional unmark no-advance)
5139   "Mark the current group."
5140   (interactive "p")
5141   (let ((buffer-read-only nil)
5142         group)
5143     (while (and (> n 0)
5144                 (not (eobp)))
5145       (when (setq group (gnus-group-group-name))
5146         ;; Update the mark.
5147         (beginning-of-line)
5148         (forward-char
5149          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5150         (delete-char 1)
5151         (if unmark
5152             (progn
5153               (insert " ")
5154               (setq gnus-group-marked (delete group gnus-group-marked)))
5155           (insert "#")
5156           (setq gnus-group-marked
5157                 (cons group (delete group gnus-group-marked)))))
5158       (or no-advance (gnus-group-next-group 1))
5159       (decf n))
5160     (gnus-summary-position-point)
5161     n))
5162
5163 (defun gnus-group-unmark-group (n)
5164   "Remove the mark from the current group."
5165   (interactive "p")
5166   (gnus-group-mark-group n 'unmark)
5167   (gnus-group-position-point))
5168
5169 (defun gnus-group-unmark-all-groups ()
5170   "Unmark all groups."
5171   (interactive)
5172   (let ((groups gnus-group-marked))
5173     (save-excursion
5174       (while groups
5175         (gnus-group-remove-mark (pop groups)))))
5176   (gnus-group-position-point))
5177
5178 (defun gnus-group-mark-region (unmark beg end)
5179   "Mark all groups between point and mark.
5180 If UNMARK, remove the mark instead."
5181   (interactive "P\nr")
5182   (let ((num (count-lines beg end)))
5183     (save-excursion
5184       (goto-char beg)
5185       (- num (gnus-group-mark-group num unmark)))))
5186
5187 (defun gnus-group-mark-buffer (&optional unmark)
5188   "Mark all groups in the buffer.
5189 If UNMARK, remove the mark instead."
5190   (interactive "P")
5191   (gnus-group-mark-region unmark (point-min) (point-max)))
5192
5193 (defun gnus-group-mark-regexp (regexp)
5194   "Mark all groups that match some regexp."
5195   (interactive "sMark (regexp): ")
5196   (let ((alist (cdr gnus-newsrc-alist))
5197         group)
5198     (while alist
5199       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5200         (gnus-group-set-mark group))))
5201   (gnus-group-position-point))
5202
5203 (defun gnus-group-remove-mark (group)
5204   "Remove the process mark from GROUP and move point there.
5205 Return nil if the group isn't displayed."
5206   (if (gnus-group-goto-group group)
5207       (save-excursion
5208         (gnus-group-mark-group 1 'unmark t)
5209         t)
5210     (setq gnus-group-marked
5211           (delete group gnus-group-marked))
5212     nil))
5213
5214 (defun gnus-group-set-mark (group)
5215   "Set the process mark on GROUP."
5216   (if (gnus-group-goto-group group) 
5217       (save-excursion
5218         (gnus-group-mark-group 1 nil t))
5219     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5220
5221 (defun gnus-group-universal-argument (arg &optional groups func)
5222   "Perform any command on all groups accoring to the process/prefix convention."
5223   (interactive "P")
5224   (let ((groups (or groups (gnus-group-process-prefix arg)))
5225         group func)
5226     (if (eq (setq func (or func
5227                            (key-binding
5228                             (read-key-sequence
5229                              (substitute-command-keys
5230                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5231             'undefined)
5232         (gnus-error 1 "Undefined key")
5233       (while groups
5234         (gnus-group-remove-mark (setq group (pop groups)))
5235         (command-execute func))))
5236   (gnus-group-position-point))
5237
5238 (defun gnus-group-process-prefix (n)
5239   "Return a list of groups to work on.
5240 Take into consideration N (the prefix) and the list of marked groups."
5241   (cond
5242    (n
5243     (setq n (prefix-numeric-value n))
5244     ;; There is a prefix, so we return a list of the N next
5245     ;; groups.
5246     (let ((way (if (< n 0) -1 1))
5247           (n (abs n))
5248           group groups)
5249       (save-excursion
5250         (while (and (> n 0)
5251                     (setq group (gnus-group-group-name)))
5252           (setq groups (cons group groups))
5253           (setq n (1- n))
5254           (gnus-group-next-group way)))
5255       (nreverse groups)))
5256    ((and (boundp 'transient-mark-mode)
5257          transient-mark-mode
5258          (boundp 'mark-active)
5259          mark-active)
5260     ;; Work on the region between point and mark.
5261     (let ((max (max (point) (mark)))
5262           groups)
5263       (save-excursion
5264         (goto-char (min (point) (mark)))
5265         (while
5266             (and
5267              (push (gnus-group-group-name) groups)
5268              (zerop (gnus-group-next-group 1))
5269              (< (point) max)))
5270         (nreverse groups))))
5271    (gnus-group-marked
5272     ;; No prefix, but a list of marked articles.
5273     (reverse gnus-group-marked))
5274    (t
5275     ;; Neither marked articles or a prefix, so we return the
5276     ;; current group.
5277     (let ((group (gnus-group-group-name)))
5278       (and group (list group))))))
5279
5280 ;; Selecting groups.
5281
5282 (defun gnus-group-read-group (&optional all no-article group)
5283   "Read news in this newsgroup.
5284 If the prefix argument ALL is non-nil, already read articles become
5285 readable.  IF ALL is a number, fetch this number of articles.  If the
5286 optional argument NO-ARTICLE is non-nil, no article will be
5287 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5288 group."
5289   (interactive "P")
5290   (let ((group (or group (gnus-group-group-name)))
5291         number active marked entry)
5292     (or group (error "No group on current line"))
5293     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5294                                             group gnus-newsrc-hashtb)))))
5295     ;; This group might be a dead group.  In that case we have to get
5296     ;; the number of unread articles from `gnus-active-hashtb'.
5297     (setq number
5298           (cond ((numberp all) all)
5299                 (entry (car entry))
5300                 ((setq active (gnus-active group))
5301                  (- (1+ (cdr active)) (car active)))))
5302     (gnus-summary-read-group
5303      group (or all (and (numberp number)
5304                         (zerop (+ number (length (cdr (assq 'tick marked)))
5305                                   (length (cdr (assq 'dormant marked)))))))
5306      no-article)))
5307
5308 (defun gnus-group-select-group (&optional all)
5309   "Select this newsgroup.
5310 No article is selected automatically.
5311 If ALL is non-nil, already read articles become readable.
5312 If ALL is a number, fetch this number of articles."
5313   (interactive "P")
5314   (gnus-group-read-group all t))
5315
5316 (defun gnus-group-quick-select-group (&optional all)
5317   "Select the current group \"quickly\".
5318 This means that no highlighting or scoring will be performed."
5319   (interactive "P")
5320   (let (gnus-visual
5321         gnus-score-find-score-files-function
5322         gnus-apply-kill-hook
5323         gnus-summary-expunge-below)
5324     (gnus-group-read-group all t)))
5325
5326 (defun gnus-group-visible-select-group (&optional all)
5327   "Select the current group without hiding any articles."
5328   (interactive "P")
5329   (let ((gnus-inhibit-limiting t))
5330     (gnus-group-read-group all t)))
5331
5332 ;;;###autoload
5333 (defun gnus-fetch-group (group)
5334   "Start Gnus if necessary and enter GROUP.
5335 Returns whether the fetching was successful or not."
5336   (interactive "sGroup name: ")
5337   (or (get-buffer gnus-group-buffer)
5338       (gnus))
5339   (gnus-group-read-group nil nil group))
5340
5341 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5342 ;; if selection was successful.
5343 (defun gnus-group-read-ephemeral-group
5344   (group method &optional activate quit-config)
5345   (let ((group (if (gnus-group-foreign-p group) group
5346                  (gnus-group-prefixed-name group method))))
5347     (gnus-sethash
5348      group
5349      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5350                      ((quit-config . ,(if quit-config quit-config
5351                                         (cons (current-buffer) 'summary))))))
5352      gnus-newsrc-hashtb)
5353     (set-buffer gnus-group-buffer)
5354     (or (gnus-check-server method)
5355         (error "Unable to contact server: %s" (gnus-status-message method)))
5356     (if activate (or (gnus-request-group group)
5357                      (error "Couldn't request group")))
5358     (condition-case ()
5359         (gnus-group-read-group t t group)
5360       (error nil)
5361       (quit nil))))
5362
5363 (defun gnus-group-jump-to-group (group)
5364   "Jump to newsgroup GROUP."
5365   (interactive
5366    (list (completing-read
5367           "Group: " gnus-active-hashtb nil
5368           (gnus-read-active-file-p)
5369           nil
5370           'gnus-group-history)))
5371
5372   (when (equal group "")
5373     (error "Empty group name"))
5374
5375   (when (string-match "[\000-\032]" group)
5376     (error "Control characters in group: %s" group))
5377
5378   (let ((b (text-property-any
5379             (point-min) (point-max)
5380             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5381     (unless (gnus-ephemeral-group-p group)
5382       (if b
5383           ;; Either go to the line in the group buffer...
5384           (goto-char b)
5385         ;; ... or insert the line.
5386         (or
5387          (gnus-active group)
5388          (gnus-activate-group group)
5389          (error "%s error: %s" group (gnus-status-message group)))
5390
5391         (gnus-group-update-group group)
5392         (goto-char (text-property-any
5393                     (point-min) (point-max)
5394                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5395     ;; Adjust cursor point.
5396     (gnus-group-position-point)))
5397
5398 (defun gnus-group-goto-group (group)
5399   "Goto to newsgroup GROUP."
5400   (when group
5401     (let ((b (text-property-any (point-min) (point-max)
5402                                 'gnus-group (gnus-intern-safe
5403                                              group gnus-active-hashtb))))
5404       (and b (goto-char b)))))
5405
5406 (defun gnus-group-next-group (n)
5407   "Go to next N'th newsgroup.
5408 If N is negative, search backward instead.
5409 Returns the difference between N and the number of skips actually
5410 done."
5411   (interactive "p")
5412   (gnus-group-next-unread-group n t))
5413
5414 (defun gnus-group-next-unread-group (n &optional all level)
5415   "Go to next N'th unread newsgroup.
5416 If N is negative, search backward instead.
5417 If ALL is non-nil, choose any newsgroup, unread or not.
5418 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5419 such group can be found, the next group with a level higher than
5420 LEVEL.
5421 Returns the difference between N and the number of skips actually
5422 made."
5423   (interactive "p")
5424   (let ((backward (< n 0))
5425         (n (abs n)))
5426     (while (and (> n 0)
5427                 (gnus-group-search-forward
5428                  backward (or (not gnus-group-goto-unread) all) level))
5429       (setq n (1- n)))
5430     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5431                                (if level " on this level or higher" "")))
5432     n))
5433
5434 (defun gnus-group-prev-group (n)
5435   "Go to previous N'th newsgroup.
5436 Returns the difference between N and the number of skips actually
5437 done."
5438   (interactive "p")
5439   (gnus-group-next-unread-group (- n) t))
5440
5441 (defun gnus-group-prev-unread-group (n)
5442   "Go to previous N'th unread newsgroup.
5443 Returns the difference between N and the number of skips actually
5444 done."
5445   (interactive "p")
5446   (gnus-group-next-unread-group (- n)))
5447
5448 (defun gnus-group-next-unread-group-same-level (n)
5449   "Go to next N'th unread newsgroup on the same level.
5450 If N is negative, search backward instead.
5451 Returns the difference between N and the number of skips actually
5452 done."
5453   (interactive "p")
5454   (gnus-group-next-unread-group n t (gnus-group-group-level))
5455   (gnus-group-position-point))
5456
5457 (defun gnus-group-prev-unread-group-same-level (n)
5458   "Go to next N'th unread newsgroup on the same level.
5459 Returns the difference between N and the number of skips actually
5460 done."
5461   (interactive "p")
5462   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5463   (gnus-group-position-point))
5464
5465 (defun gnus-group-best-unread-group (&optional exclude-group)
5466   "Go to the group with the highest level.
5467 If EXCLUDE-GROUP, do not go to that group."
5468   (interactive)
5469   (goto-char (point-min))
5470   (let ((best 100000)
5471         unread best-point)
5472     (while (not (eobp))
5473       (setq unread (get-text-property (point) 'gnus-unread))
5474       (if (and (numberp unread) (> unread 0))
5475           (progn
5476             (if (and (get-text-property (point) 'gnus-level)
5477                      (< (get-text-property (point) 'gnus-level) best)
5478                      (or (not exclude-group)
5479                          (not (equal exclude-group (gnus-group-group-name)))))
5480                 (progn
5481                   (setq best (get-text-property (point) 'gnus-level))
5482                   (setq best-point (point))))))
5483       (forward-line 1))
5484     (if best-point (goto-char best-point))
5485     (gnus-summary-position-point)
5486     (and best-point (gnus-group-group-name))))
5487
5488 (defun gnus-group-first-unread-group ()
5489   "Go to the first group with unread articles."
5490   (interactive)
5491   (prog1
5492       (let ((opoint (point))
5493             unread)
5494         (goto-char (point-min))
5495         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5496                 (and (numberp unread)   ; Not a topic.
5497                      (not (zerop unread))) ; Has unread articles.
5498                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5499             (point)                     ; Success.
5500           (goto-char opoint)
5501           nil))                         ; Not success.
5502     (gnus-group-position-point)))
5503
5504 (defun gnus-group-enter-server-mode ()
5505   "Jump to the server buffer."
5506   (interactive)
5507   (gnus-enter-server-buffer))
5508
5509 (defun gnus-group-make-group (name &optional method address)
5510   "Add a new newsgroup.
5511 The user will be prompted for a NAME, for a select METHOD, and an
5512 ADDRESS."
5513   (interactive
5514    (cons
5515     (read-string "Group name: ")
5516     (let ((method
5517            (completing-read
5518             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5519             nil t nil 'gnus-method-history)))
5520       (cond ((assoc method gnus-valid-select-methods)
5521              (list method
5522                    (if (memq 'prompt-address
5523                              (assoc method gnus-valid-select-methods))
5524                        (read-string "Address: ")
5525                      "")))
5526             ((assoc method gnus-server-alist)
5527              (list method))
5528             (t
5529              (list method ""))))))
5530
5531   (let* ((meth (and method (if address (list (intern method) address)
5532                              method)))
5533          (nname (if method (gnus-group-prefixed-name name meth) name))
5534          backend info)
5535     (when (gnus-gethash nname gnus-newsrc-hashtb)
5536       (error "Group %s already exists" nname))
5537     ;; Subscribe to the new group.
5538     (gnus-group-change-level
5539      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5540      gnus-level-default-subscribed gnus-level-killed
5541      (and (gnus-group-group-name)
5542           (gnus-gethash (gnus-group-group-name)
5543                         gnus-newsrc-hashtb))
5544      t)
5545     ;; Make it active.
5546     (gnus-set-active nname (cons 1 0))
5547     (or (gnus-ephemeral-group-p name)
5548         (gnus-dribble-enter
5549          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5550     ;; Insert the line.
5551     (gnus-group-insert-group-line-info nname)
5552     (forward-line -1)
5553     (gnus-group-position-point)
5554
5555     ;; Load the backend and try to make the backend create
5556     ;; the group as well.
5557     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5558                                                   nil meth))))
5559                  gnus-valid-select-methods)
5560       (require backend))
5561     (gnus-check-server meth)
5562     (and (gnus-check-backend-function 'request-create-group nname)
5563          (gnus-request-create-group nname))
5564     t))
5565
5566 (defun gnus-group-delete-group (group &optional force)
5567   "Delete the current group.
5568 If FORCE (the prefix) is non-nil, all the articles in the group will
5569 be deleted.  This is \"deleted\" as in \"removed forever from the face
5570 of the Earth\".  There is no undo."
5571   (interactive
5572    (list (gnus-group-group-name)
5573          current-prefix-arg))
5574   (or group (error "No group to rename"))
5575   (or (gnus-check-backend-function 'request-delete-group group)
5576       (error "This backend does not support group deletion"))
5577   (prog1
5578       (if (not (gnus-yes-or-no-p
5579                 (format
5580                  "Do you really want to delete %s%s? "
5581                  group (if force " and all its contents" ""))))
5582           () ; Whew!
5583         (gnus-message 6 "Deleting group %s..." group)
5584         (if (not (gnus-request-delete-group group force))
5585             (gnus-error 3 "Couldn't delete group %s" group)
5586           (gnus-message 6 "Deleting group %s...done" group)
5587           (gnus-group-goto-group group)
5588           (gnus-group-kill-group 1 t)
5589           (gnus-sethash group nil gnus-active-hashtb)
5590           t))
5591     (gnus-group-position-point)))
5592
5593 (defun gnus-group-rename-group (group new-name)
5594   (interactive
5595    (list
5596     (gnus-group-group-name)
5597     (progn
5598       (or (gnus-check-backend-function
5599            'request-rename-group (gnus-group-group-name))
5600           (error "This backend does not support renaming groups"))
5601       (read-string "New group name: "))))
5602
5603   (or (gnus-check-backend-function 'request-rename-group group)
5604       (error "This backend does not support renaming groups"))
5605
5606   (or group (error "No group to rename"))
5607   (and (string-match "^[ \t]*$" new-name)
5608        (error "Not a valid group name"))
5609
5610   ;; We find the proper prefixed name.
5611   (setq new-name
5612         (gnus-group-prefixed-name
5613          (gnus-group-real-name new-name)
5614          (gnus-info-method (gnus-get-info group))))
5615
5616   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5617   (prog1
5618       (if (not (gnus-request-rename-group group new-name))
5619           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5620         ;; We rename the group internally by killing it...
5621         (gnus-group-goto-group group)
5622         (gnus-group-kill-group)
5623         ;; ... changing its name ...
5624         (setcar (cdar gnus-list-of-killed-groups) new-name)
5625         ;; ... and then yanking it.  Magic!
5626         (gnus-group-yank-group)
5627         (gnus-set-active new-name (gnus-active group))
5628         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5629         new-name)
5630     (gnus-group-position-point)))
5631
5632 (defun gnus-group-edit-group (group &optional part)
5633   "Edit the group on the current line."
5634   (interactive (list (gnus-group-group-name)))
5635   (let* ((part (or part 'info))
5636          (done-func `(lambda ()
5637                        "Exit editing mode and update the information."
5638                        (interactive)
5639                        (gnus-group-edit-group-done ',part ,group)))
5640          (winconf (current-window-configuration))
5641          info)
5642     (or group (error "No group on current line"))
5643     (or (setq info (gnus-get-info group))
5644         (error "Killed group; can't be edited"))
5645     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5646     (gnus-configure-windows 'edit-group)
5647     (gnus-add-current-to-buffer-list)
5648     (emacs-lisp-mode)
5649     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5650     (use-local-map (copy-keymap emacs-lisp-mode-map))
5651     (local-set-key "\C-c\C-c" done-func)
5652     (make-local-variable 'gnus-prev-winconf)
5653     (setq gnus-prev-winconf winconf)
5654     (erase-buffer)
5655     (insert
5656      (cond
5657       ((eq part 'method)
5658        ";; Type `C-c C-c' after editing the select method.\n\n")
5659       ((eq part 'params)
5660        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5661       ((eq part 'info)
5662        ";; Type `C-c C-c' after editing the group info.\n\n")))
5663     (insert
5664      (pp-to-string
5665       (cond ((eq part 'method)
5666              (or (gnus-info-method info) "native"))
5667             ((eq part 'params)
5668              (gnus-info-params info))
5669             (t info)))
5670      "\n")))
5671
5672 (defun gnus-group-edit-group-method (group)
5673   "Edit the select method of GROUP."
5674   (interactive (list (gnus-group-group-name)))
5675   (gnus-group-edit-group group 'method))
5676
5677 (defun gnus-group-edit-group-parameters (group)
5678   "Edit the group parameters of GROUP."
5679   (interactive (list (gnus-group-group-name)))
5680   (gnus-group-edit-group group 'params))
5681
5682 (defun gnus-group-edit-group-done (part group)
5683   "Get info from buffer, update variables and jump to the group buffer."
5684   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5685   (goto-char (point-min))
5686   (let* ((form (read (current-buffer)))
5687          (winconf gnus-prev-winconf)
5688          (method (cond ((eq part 'info) (nth 4 form))
5689                        ((eq part 'method) form)
5690                        (t nil)))
5691          (info (cond ((eq part 'info) form)
5692                      ((eq part 'method) (gnus-get-info group))
5693                      (t nil)))
5694          (new-group (if info
5695                       (if (or (not method)
5696                               (gnus-server-equal
5697                                gnus-select-method method))
5698                           (gnus-group-real-name (car info))
5699                         (gnus-group-prefixed-name
5700                          (gnus-group-real-name (car info)) method))
5701                       nil)))
5702     (when (and new-group
5703                (not (equal new-group group)))
5704       (when (gnus-group-goto-group group)
5705         (gnus-group-kill-group 1))
5706       (gnus-activate-group new-group))
5707     ;; Set the info.
5708     (if (and info new-group)
5709         (progn
5710           (setq info (gnus-copy-sequence info))
5711           (setcar info new-group)
5712           (unless (gnus-server-equal method "native")
5713             (unless (nthcdr 3 info)
5714               (nconc info (list nil nil)))
5715             (unless (nthcdr 4 info)
5716               (nconc info (list nil)))
5717             (gnus-info-set-method info method))
5718           (gnus-group-set-info info))
5719       (gnus-group-set-info form (or new-group group) part))
5720     (kill-buffer (current-buffer))
5721     (and winconf (set-window-configuration winconf))
5722     (set-buffer gnus-group-buffer)
5723     (gnus-group-update-group (or new-group group))
5724     (gnus-group-position-point)))
5725
5726 (defun gnus-group-make-help-group ()
5727   "Create the Gnus documentation group."
5728   (interactive)
5729   (let ((path load-path)
5730         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5731         file dir)
5732     (and (gnus-gethash name gnus-newsrc-hashtb)
5733          (error "Documentation group already exists"))
5734     (while path
5735       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5736             file nil)
5737       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5738                 (file-exists-p
5739                  (setq file (concat (file-name-directory
5740                                      (directory-file-name dir))
5741                                     "etc/gnus-tut.txt"))))
5742         (setq path nil)))
5743     (if (not file)
5744         (gnus-message 1 "Couldn't find doc group")
5745       (gnus-group-make-group
5746        (gnus-group-real-name name)
5747        (list 'nndoc "gnus-help"
5748              (list 'nndoc-address file)
5749              (list 'nndoc-article-type 'mbox)))))
5750   (gnus-group-position-point))
5751
5752 (defun gnus-group-make-doc-group (file type)
5753   "Create a group that uses a single file as the source."
5754   (interactive
5755    (list (read-file-name "File name: ")
5756          (and current-prefix-arg 'ask)))
5757   (when (eq type 'ask)
5758     (let ((err "")
5759           char found)
5760       (while (not found)
5761         (message
5762          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5763          err)
5764         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5765                           ((= char ?b) 'babyl)
5766                           ((= char ?d) 'digest)
5767                           ((= char ?f) 'forward)
5768                           ((= char ?a) 'mmfd)
5769                           (t (setq err (format "%c unknown. " char))
5770                              nil))))
5771       (setq type found)))
5772   (let* ((file (expand-file-name file))
5773          (name (gnus-generate-new-group-name
5774                 (gnus-group-prefixed-name
5775                  (file-name-nondirectory file) '(nndoc "")))))
5776     (gnus-group-make-group
5777      (gnus-group-real-name name)
5778      (list 'nndoc (file-name-nondirectory file)
5779            (list 'nndoc-address file)
5780            (list 'nndoc-article-type (or type 'guess))))))
5781
5782 (defun gnus-group-make-archive-group (&optional all)
5783   "Create the (ding) Gnus archive group of the most recent articles.
5784 Given a prefix, create a full group."
5785   (interactive "P")
5786   (let ((group (gnus-group-prefixed-name
5787                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5788     (and (gnus-gethash group gnus-newsrc-hashtb)
5789          (error "Archive group already exists"))
5790     (gnus-group-make-group
5791      (gnus-group-real-name group)
5792      (list 'nndir (if all "hpc" "edu")
5793            (list 'nndir-directory
5794                  (if all gnus-group-archive-directory
5795                    gnus-group-recent-archive-directory))))))
5796
5797 (defun gnus-group-make-directory-group (dir)
5798   "Create an nndir group.
5799 The user will be prompted for a directory.  The contents of this
5800 directory will be used as a newsgroup.  The directory should contain
5801 mail messages or news articles in files that have numeric names."
5802   (interactive
5803    (list (read-file-name "Create group from directory: ")))
5804   (or (file-exists-p dir) (error "No such directory"))
5805   (or (file-directory-p dir) (error "Not a directory"))
5806   (let ((ext "")
5807         (i 0)
5808         group)
5809     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5810       (setq group
5811             (gnus-group-prefixed-name
5812              (concat (file-name-as-directory (directory-file-name dir))
5813                      ext)
5814              '(nndir "")))
5815       (setq ext (format "<%d>" (setq i (1+ i)))))
5816     (gnus-group-make-group
5817      (gnus-group-real-name group)
5818      (list 'nndir group (list 'nndir-directory dir)))))
5819
5820 (defun gnus-group-make-kiboze-group (group address scores)
5821   "Create an nnkiboze group.
5822 The user will be prompted for a name, a regexp to match groups, and
5823 score file entries for articles to include in the group."
5824   (interactive
5825    (list
5826     (read-string "nnkiboze group name: ")
5827     (read-string "Source groups (regexp): ")
5828     (let ((headers (mapcar (lambda (group) (list group))
5829                            '("subject" "from" "number" "date" "message-id"
5830                              "references" "chars" "lines" "xref"
5831                              "followup" "all" "body" "head")))
5832           scores header regexp regexps)
5833       (while (not (equal "" (setq header (completing-read
5834                                           "Match on header: " headers nil t))))
5835         (setq regexps nil)
5836         (while (not (equal "" (setq regexp (read-string
5837                                             (format "Match on %s (string): "
5838                                                     header)))))
5839           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5840         (setq scores (cons (cons header regexps) scores)))
5841       scores)))
5842   (gnus-group-make-group group "nnkiboze" address)
5843   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5844     (let (emacs-lisp-mode-hook)
5845       (pp scores (current-buffer)))))
5846
5847 (defun gnus-group-add-to-virtual (n vgroup)
5848   "Add the current group to a virtual group."
5849   (interactive
5850    (list current-prefix-arg
5851          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5852                           "nnvirtual:")))
5853   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5854       (error "%s is not an nnvirtual group" vgroup))
5855   (let* ((groups (gnus-group-process-prefix n))
5856          (method (gnus-info-method (gnus-get-info vgroup))))
5857     (setcar (cdr method)
5858             (concat
5859              (nth 1 method) "\\|"
5860              (mapconcat
5861               (lambda (s)
5862                 (gnus-group-remove-mark s)
5863                 (concat "\\(^" (regexp-quote s) "$\\)"))
5864               groups "\\|"))))
5865   (gnus-group-position-point))
5866
5867 (defun gnus-group-make-empty-virtual (group)
5868   "Create a new, fresh, empty virtual group."
5869   (interactive "sCreate new, empty virtual group: ")
5870   (let* ((method (list 'nnvirtual "^$"))
5871          (pgroup (gnus-group-prefixed-name group method)))
5872     ;; Check whether it exists already.
5873     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5874          (error "Group %s already exists." pgroup))
5875     ;; Subscribe the new group after the group on the current line.
5876     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5877     (gnus-group-update-group pgroup)
5878     (forward-line -1)
5879     (gnus-group-position-point)))
5880
5881 (defun gnus-group-enter-directory (dir)
5882   "Enter an ephemeral nneething group."
5883   (interactive "DDirectory to read: ")
5884   (let* ((method (list 'nneething dir))
5885          (leaf (gnus-group-prefixed-name
5886                 (file-name-nondirectory (directory-file-name dir))
5887                 method))
5888          (name (gnus-generate-new-group-name leaf)))
5889     (let ((nneething-read-only t))
5890       (or (gnus-group-read-ephemeral-group
5891            name method t
5892            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5893                                       'summary 'group)))
5894           (error "Couldn't enter %s" dir)))))
5895
5896 ;; Group sorting commands
5897 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5898
5899 (defun gnus-group-sort-groups (func &optional reverse)
5900   "Sort the group buffer according to FUNC.
5901 If REVERSE, reverse the sorting order."
5902   (interactive (list gnus-group-sort-function
5903                      current-prefix-arg))
5904   (let ((func (cond 
5905                ((not (listp func)) func)
5906                ((null func) func)
5907                ((= 1 (length func)) (car func))
5908                (t `(lambda (t1 t2)
5909                      ,(gnus-make-sort-function 
5910                        (reverse func)))))))
5911     ;; We peel off the dummy group from the alist.
5912     (when func
5913       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5914         (pop gnus-newsrc-alist))
5915       ;; Do the sorting.
5916       (setq gnus-newsrc-alist
5917             (sort gnus-newsrc-alist func))
5918       (when reverse
5919         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5920       ;; Regenerate the hash table.
5921       (gnus-make-hashtable-from-newsrc-alist)
5922       (gnus-group-list-groups))))
5923
5924 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5925   "Sort the group buffer alphabetically by group name.
5926 If REVERSE, sort in reverse order."
5927   (interactive "P")
5928   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5929
5930 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5931   "Sort the group buffer by number of unread articles.
5932 If REVERSE, sort in reverse order."
5933   (interactive "P")
5934   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5935
5936 (defun gnus-group-sort-groups-by-level (&optional reverse)
5937   "Sort the group buffer by group level.
5938 If REVERSE, sort in reverse order."
5939   (interactive "P")
5940   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5941
5942 (defun gnus-group-sort-groups-by-score (&optional reverse)
5943   "Sort the group buffer by group score.
5944 If REVERSE, sort in reverse order."
5945   (interactive "P")
5946   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5947
5948 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5949   "Sort the group buffer by group rank.
5950 If REVERSE, sort in reverse order."
5951   (interactive "P")
5952   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5953
5954 (defun gnus-group-sort-groups-by-method (&optional reverse)
5955   "Sort the group buffer alphabetically by backend name.
5956 If REVERSE, sort in reverse order."
5957   (interactive "P")
5958   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5959
5960 (defun gnus-group-sort-by-alphabet (info1 info2)
5961   "Sort alphabetically."
5962   (string< (gnus-info-group info1) (gnus-info-group info2)))
5963
5964 (defun gnus-group-sort-by-unread (info1 info2)
5965   "Sort by number of unread articles."
5966   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5967         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5968     (< (or (and (numberp n1) n1) 0)
5969        (or (and (numberp n2) n2) 0))))
5970
5971 (defun gnus-group-sort-by-level (info1 info2)
5972   "Sort by level."
5973   (< (gnus-info-level info1) (gnus-info-level info2)))
5974
5975 (defun gnus-group-sort-by-method (info1 info2)
5976   "Sort alphabetically by backend name."
5977   (string< (symbol-name (car (gnus-find-method-for-group
5978                               (gnus-info-group info1) info1)))
5979            (symbol-name (car (gnus-find-method-for-group
5980                               (gnus-info-group info2) info2)))))
5981
5982 (defun gnus-group-sort-by-score (info1 info2)
5983   "Sort by group score."
5984   (< (gnus-info-score info1) (gnus-info-score info2)))
5985
5986 (defun gnus-group-sort-by-rank (info1 info2)
5987   "Sort by level and score."
5988   (let ((level1 (gnus-info-level info1))
5989         (level2 (gnus-info-level info2)))
5990     (or (< level1 level2)
5991         (and (= level1 level2)
5992              (> (gnus-info-score info1) (gnus-info-score info2))))))
5993
5994 ;; Group catching up.
5995
5996 (defun gnus-group-clear-data (n)
5997   "Clear all marks and read ranges from the current group."
5998   (interactive "P")
5999   (let ((groups (gnus-group-process-prefix n))
6000         group info)
6001     (while (setq group (pop groups))
6002       (setq info (gnus-get-info group))
6003       (gnus-info-set-read info nil)
6004       (when (gnus-info-marks info)
6005         (gnus-info-set-marks info nil))
6006       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6007       (when (gnus-group-goto-group group)
6008         (gnus-group-remove-mark group)
6009         (gnus-group-update-group-line)))))
6010
6011 (defun gnus-group-catchup-current (&optional n all)
6012   "Mark all articles not marked as unread in current newsgroup as read.
6013 If prefix argument N is numeric, the ARG next newsgroups will be
6014 caught up.  If ALL is non-nil, marked articles will also be marked as
6015 read.  Cross references (Xref: header) of articles are ignored.
6016 The difference between N and actual number of newsgroups that were
6017 caught up is returned."
6018   (interactive "P")
6019   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6020                gnus-expert-user
6021                (gnus-y-or-n-p
6022                 (if all
6023                     "Do you really want to mark all articles as read? "
6024                   "Mark all unread articles as read? "))))
6025       n
6026     (let ((groups (gnus-group-process-prefix n))
6027           (ret 0))
6028       (while groups
6029         ;; Virtual groups have to be given special treatment.
6030         (let ((method (gnus-find-method-for-group (car groups))))
6031           (if (eq 'nnvirtual (car method))
6032               (nnvirtual-catchup-group
6033                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6034         (gnus-group-remove-mark (car groups))
6035         (if (>= (gnus-group-group-level) gnus-level-zombie)
6036             (gnus-message 2 "Dead groups can't be caught up")
6037           (if (prog1
6038                   (gnus-group-goto-group (car groups))
6039                 (gnus-group-catchup (car groups) all))
6040               (gnus-group-update-group-line)
6041             (setq ret (1+ ret))))
6042         (setq groups (cdr groups)))
6043       (gnus-group-next-unread-group 1)
6044       ret)))
6045
6046 (defun gnus-group-catchup-current-all (&optional n)
6047   "Mark all articles in current newsgroup as read.
6048 Cross references (Xref: header) of articles are ignored."
6049   (interactive "P")
6050   (gnus-group-catchup-current n 'all))
6051
6052 (defun gnus-group-catchup (group &optional all)
6053   "Mark all articles in GROUP as read.
6054 If ALL is non-nil, all articles are marked as read.
6055 The return value is the number of articles that were marked as read,
6056 or nil if no action could be taken."
6057   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6058          (num (car entry)))
6059     ;; Do the updating only if the newsgroup isn't killed.
6060     (if (not (numberp (car entry)))
6061         (gnus-message 1 "Can't catch up; non-active group")
6062       ;; Do auto-expirable marks if that's required.
6063       (when (gnus-group-auto-expirable-p group)
6064         (gnus-add-marked-articles
6065          group 'expire (gnus-list-of-unread-articles group))
6066         (when all
6067           (let ((marks (nth 3 (nth 2 entry))))
6068             (gnus-add-marked-articles
6069              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6070             (gnus-add-marked-articles
6071              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6072       (when entry
6073         (gnus-update-read-articles group nil)
6074         ;; Also nix out the lists of marks and dormants.
6075         (when all
6076           (gnus-add-marked-articles group 'tick nil nil 'force)
6077           (gnus-add-marked-articles group 'dormant nil nil 'force))
6078         (run-hooks 'gnus-group-catchup-group-hook)
6079         num))))
6080
6081 (defun gnus-group-expire-articles (&optional n)
6082   "Expire all expirable articles in the current newsgroup."
6083   (interactive "P")
6084   (let ((groups (gnus-group-process-prefix n))
6085         group)
6086     (unless groups
6087       (error "No groups to expire"))
6088     (while (setq group (pop groups))
6089       (gnus-group-remove-mark group)
6090       (when (gnus-check-backend-function 'request-expire-articles group)
6091         (gnus-message 6 "Expiring articles in %s..." group)
6092         (let* ((info (gnus-get-info group))
6093                (expirable (if (gnus-group-total-expirable-p group)
6094                               (cons nil (gnus-list-of-read-articles group))
6095                             (assq 'expire (gnus-info-marks info))))
6096                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6097           (when expirable
6098             (setcdr
6099              expirable
6100              (gnus-compress-sequence
6101               (if expiry-wait
6102                   ;; We set the expiry variables to the groupp
6103                   ;; parameter. 
6104                   (let ((nnmail-expiry-wait-function nil)
6105                         (nnmail-expiry-wait expiry-wait))
6106                     (gnus-request-expire-articles
6107                      (gnus-uncompress-sequence (cdr expirable)) group))
6108                 ;; Just expire using the normal expiry values.
6109                 (gnus-request-expire-articles
6110                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6111           (gnus-message 6 "Expiring articles in %s...done" group)))
6112       (gnus-group-position-point))))
6113
6114 (defun gnus-group-expire-all-groups ()
6115   "Expire all expirable articles in all newsgroups."
6116   (interactive)
6117   (save-excursion
6118     (gnus-message 5 "Expiring...")
6119     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6120                                      (cdr gnus-newsrc-alist))))
6121       (gnus-group-expire-articles nil)))
6122   (gnus-group-position-point)
6123   (gnus-message 5 "Expiring...done"))
6124
6125 (defun gnus-group-set-current-level (n level)
6126   "Set the level of the next N groups to LEVEL."
6127   (interactive
6128    (list
6129     current-prefix-arg
6130     (string-to-int
6131      (let ((s (read-string
6132                (format "Level (default %s): "
6133                        (or (gnus-group-group-level) 
6134                            gnus-level-default-subscribed)))))
6135        (if (string-match "^\\s-*$" s)
6136            (int-to-string (or (gnus-group-group-level) 
6137                               gnus-level-default-subscribed))
6138          s)))))
6139   (or (and (>= level 1) (<= level gnus-level-killed))
6140       (error "Illegal level: %d" level))
6141   (let ((groups (gnus-group-process-prefix n))
6142         group)
6143     (while (setq group (pop groups))
6144       (gnus-group-remove-mark group)
6145       (gnus-message 6 "Changed level of %s from %d to %d"
6146                     group (or (gnus-group-group-level) gnus-level-killed)
6147                     level)
6148       (gnus-group-change-level
6149        group level (or (gnus-group-group-level) gnus-level-killed))
6150       (gnus-group-update-group-line)))
6151   (gnus-group-position-point))
6152
6153 (defun gnus-group-unsubscribe-current-group (&optional n)
6154   "Toggle subscription of the current group.
6155 If given numerical prefix, toggle the N next groups."
6156   (interactive "P")
6157   (let ((groups (gnus-group-process-prefix n))
6158         group)
6159     (while groups
6160       (setq group (car groups)
6161             groups (cdr groups))
6162       (gnus-group-remove-mark group)
6163       (gnus-group-unsubscribe-group
6164        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6165                  gnus-level-default-unsubscribed
6166                gnus-level-default-subscribed) t)
6167       (gnus-group-update-group-line))
6168     (gnus-group-next-group 1)))
6169
6170 (defun gnus-group-unsubscribe-group (group &optional level silent)
6171   "Toggle subscription to GROUP.
6172 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6173 group line."
6174   (interactive
6175    (list (completing-read
6176           "Group: " gnus-active-hashtb nil
6177           (gnus-read-active-file-p)
6178           nil 
6179           'gnus-group-history)))
6180   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6181     (cond
6182      ((string-match "^[ \t]$" group)
6183       (error "Empty group name"))
6184      (newsrc
6185       ;; Toggle subscription flag.
6186       (gnus-group-change-level
6187        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6188                                       gnus-level-subscribed)
6189                                   (1+ gnus-level-subscribed)
6190                                 gnus-level-default-subscribed)))
6191       (unless silent
6192         (gnus-group-update-group group)))
6193      ((and (stringp group)
6194            (or (not (gnus-read-active-file-p))
6195                (gnus-active group)))
6196       ;; Add new newsgroup.
6197       (gnus-group-change-level
6198        group
6199        (if level level gnus-level-default-subscribed)
6200        (or (and (member group gnus-zombie-list)
6201                 gnus-level-zombie)
6202            gnus-level-killed)
6203        (and (gnus-group-group-name)
6204             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6205       (unless silent
6206         (gnus-group-update-group group)))
6207      (t (error "No such newsgroup: %s" group)))
6208     (gnus-group-position-point)))
6209
6210 (defun gnus-group-transpose-groups (n)
6211   "Move the current newsgroup up N places.
6212 If given a negative prefix, move down instead.  The difference between
6213 N and the number of steps taken is returned."
6214   (interactive "p")
6215   (or (gnus-group-group-name)
6216       (error "No group on current line"))
6217   (gnus-group-kill-group 1)
6218   (prog1
6219       (forward-line (- n))
6220     (gnus-group-yank-group)
6221     (gnus-group-position-point)))
6222
6223 (defun gnus-group-kill-all-zombies ()
6224   "Kill all zombie newsgroups."
6225   (interactive)
6226   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6227   (setq gnus-zombie-list nil)
6228   (gnus-group-list-groups))
6229
6230 (defun gnus-group-kill-region (begin end)
6231   "Kill newsgroups in current region (excluding current point).
6232 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6233   (interactive "r")
6234   (let ((lines
6235          ;; Count lines.
6236          (save-excursion
6237            (count-lines
6238             (progn
6239               (goto-char begin)
6240               (beginning-of-line)
6241               (point))
6242             (progn
6243               (goto-char end)
6244               (beginning-of-line)
6245               (point))))))
6246     (goto-char begin)
6247     (beginning-of-line)                 ;Important when LINES < 1
6248     (gnus-group-kill-group lines)))
6249
6250 (defun gnus-group-kill-group (&optional n discard)
6251   "Kill the next N groups.
6252 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6253 However, only groups that were alive can be yanked; already killed
6254 groups or zombie groups can't be yanked.
6255 The return value is the name of the group that was killed, or a list
6256 of groups killed."
6257   (interactive "P")
6258   (let ((buffer-read-only nil)
6259         (groups (gnus-group-process-prefix n))
6260         group entry level out)
6261     (if (< (length groups) 10)
6262         ;; This is faster when there are few groups.
6263         (while groups
6264           (push (setq group (pop groups)) out)
6265           (gnus-group-remove-mark group)
6266           (setq level (gnus-group-group-level))
6267           (gnus-delete-line)
6268           (when (and (not discard)
6269                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6270             (push (cons (car entry) (nth 2 entry))
6271                   gnus-list-of-killed-groups))
6272           (gnus-group-change-level
6273            (if entry entry group) gnus-level-killed (if entry nil level)))
6274       ;; If there are lots and lots of groups to be killed, we use
6275       ;; this thing instead.
6276       (let (entry)
6277         (setq groups (nreverse groups))
6278         (while groups
6279           (gnus-group-remove-mark (setq group (pop groups)))
6280           (gnus-delete-line)
6281           (cond
6282            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6283             (push (cons (car entry) (nth 2 entry))
6284                   gnus-list-of-killed-groups)
6285             (setcdr (cdr entry) (cdddr entry)))
6286            ((member group gnus-zombie-list)
6287             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6288         (gnus-make-hashtable-from-newsrc-alist)))
6289
6290     (gnus-group-position-point)
6291     (if (< (length out) 2) (car out) (nreverse out))))
6292
6293 (defun gnus-group-yank-group (&optional arg)
6294   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6295 inserting it before the current newsgroup.  The numeric ARG specifies
6296 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6297 is returned, or (if several groups are yanked) a list of yanked groups
6298 is returned."
6299   (interactive "p")
6300   (setq arg (or arg 1))
6301   (let (info group prev out)
6302     (while (>= (decf arg) 0)
6303       (if (not (setq info (pop gnus-list-of-killed-groups)))
6304           (error "No more newsgroups to yank"))
6305       (push (setq group (nth 1 info)) out)
6306       ;; Find which newsgroup to insert this one before - search
6307       ;; backward until something suitable is found.  If there are no
6308       ;; other newsgroups in this buffer, just make this newsgroup the
6309       ;; first newsgroup.
6310       (setq prev (gnus-group-group-name))
6311       (gnus-group-change-level
6312        info (gnus-info-level (cdr info)) gnus-level-killed
6313        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6314        t)
6315       (gnus-group-insert-group-line-info group))
6316     (forward-line -1)
6317     (gnus-group-position-point)
6318     (if (< (length out) 2) (car out) (nreverse out))))
6319
6320 (defun gnus-group-kill-level (level)
6321   "Kill all groups that is on a certain LEVEL."
6322   (interactive "nKill all groups on level: ")
6323   (cond
6324    ((= level gnus-level-zombie)
6325     (setq gnus-killed-list
6326           (nconc gnus-zombie-list gnus-killed-list))
6327     (setq gnus-zombie-list nil))
6328    ((and (< level gnus-level-zombie)
6329          (> level 0)
6330          (or gnus-expert-user
6331              (gnus-yes-or-no-p
6332               (format
6333                "Do you really want to kill all groups on level %d? "
6334                level))))
6335     (let* ((prev gnus-newsrc-alist)
6336            (alist (cdr prev)))
6337       (while alist
6338         (if (= (gnus-info-level level) level)
6339             (setcdr prev (cdr alist))
6340           (setq prev alist))
6341         (setq alist (cdr alist)))
6342       (gnus-make-hashtable-from-newsrc-alist)
6343       (gnus-group-list-groups)))
6344    (t
6345     (error "Can't kill; illegal level: %d" level))))
6346
6347 (defun gnus-group-list-all-groups (&optional arg)
6348   "List all newsgroups with level ARG or lower.
6349 Default is gnus-level-unsubscribed, which lists all subscribed and most
6350 unsubscribed groups."
6351   (interactive "P")
6352   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6353
6354 ;; Redefine this to list ALL killed groups if prefix arg used.
6355 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6356 (defun gnus-group-list-killed (&optional arg)
6357   "List all killed newsgroups in the group buffer.
6358 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6359 entail asking the server for the groups."
6360   (interactive "P")
6361   ;; Find all possible killed newsgroups if arg.
6362   (when arg
6363     (gnus-get-killed-groups))
6364   (if (not gnus-killed-list)
6365       (gnus-message 6 "No killed groups")
6366     (let (gnus-group-list-mode)
6367       (funcall gnus-group-prepare-function
6368                gnus-level-killed t gnus-level-killed))
6369     (goto-char (point-min)))
6370   (gnus-group-position-point))
6371
6372 (defun gnus-group-list-zombies ()
6373   "List all zombie newsgroups in the group buffer."
6374   (interactive)
6375   (if (not gnus-zombie-list)
6376       (gnus-message 6 "No zombie groups")
6377     (let (gnus-group-list-mode)
6378       (funcall gnus-group-prepare-function
6379                gnus-level-zombie t gnus-level-zombie))
6380     (goto-char (point-min)))
6381   (gnus-group-position-point))
6382
6383 (defun gnus-group-list-active ()
6384   "List all groups that are available from the server(s)."
6385   (interactive)
6386   ;; First we make sure that we have really read the active file.
6387   (unless (gnus-read-active-file-p)
6388     (let ((gnus-read-active-file t))
6389       (gnus-read-active-file)))
6390   ;; Find all groups and sort them.
6391   (let ((groups
6392          (sort
6393           (let (list)
6394             (mapatoms
6395              (lambda (sym)
6396                (and (boundp sym)
6397                     (symbol-value sym)
6398                     (setq list (cons (symbol-name sym) list))))
6399              gnus-active-hashtb)
6400             list)
6401           'string<))
6402         (buffer-read-only nil))
6403     (erase-buffer)
6404     (while groups
6405       (gnus-group-insert-group-line-info (pop groups)))
6406     (goto-char (point-min))))
6407
6408 (defun gnus-activate-all-groups (level)
6409   "Activate absolutely all groups."
6410   (interactive (list 7))
6411   (let ((gnus-activate-level level)
6412         (gnus-activate-foreign-newsgroups level))
6413     (gnus-group-get-new-news)))
6414
6415 (defun gnus-group-get-new-news (&optional arg)
6416   "Get newly arrived articles.
6417 If ARG is a number, it specifies which levels you are interested in
6418 re-scanning.  If ARG is non-nil and not a number, this will force
6419 \"hard\" re-reading of the active files from all servers."
6420   (interactive "P")
6421   (run-hooks 'gnus-get-new-news-hook)
6422   ;; We might read in new NoCeM messages here.
6423   (when (and gnus-use-nocem 
6424              (null arg))
6425     (gnus-nocem-scan-groups))
6426   ;; If ARG is not a number, then we read the active file.
6427   (when (and arg (not (numberp arg)))
6428     (let ((gnus-read-active-file t))
6429       (gnus-read-active-file))
6430     (setq arg nil))
6431
6432   (setq arg (gnus-group-default-level arg t))
6433   (if (and gnus-read-active-file (not arg))
6434       (progn
6435         (gnus-read-active-file)
6436         (gnus-get-unread-articles arg))
6437     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6438       (gnus-get-unread-articles arg)))
6439   (run-hooks 'gnus-after-getting-new-news-hook)
6440   (gnus-group-list-groups))
6441
6442 (defun gnus-group-get-new-news-this-group (&optional n)
6443   "Check for newly arrived news in the current group (and the N-1 next groups).
6444 The difference between N and the number of newsgroup checked is returned.
6445 If N is negative, this group and the N-1 previous groups will be checked."
6446   (interactive "P")
6447   (let* ((groups (gnus-group-process-prefix n))
6448          (ret (if (numberp n) (- n (length groups)) 0))
6449          (beg (unless n (point)))
6450          group)
6451     (while (setq group (pop groups))
6452       (gnus-group-remove-mark group)
6453       (if (gnus-activate-group group 'scan)
6454           (progn
6455             (gnus-get-unread-articles-in-group
6456              (gnus-get-info group) (gnus-active group) t)
6457             (unless (gnus-virtual-group-p group)
6458               (gnus-close-group group))
6459             (gnus-group-update-group group))
6460         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6461     (when beg (goto-char beg))
6462     (when gnus-goto-next-group-when-activating
6463       (gnus-group-next-unread-group 1 t))
6464     (gnus-summary-position-point)
6465     ret))
6466
6467 (defun gnus-group-fetch-faq (group &optional faq-dir)
6468   "Fetch the FAQ for the current group."
6469   (interactive
6470    (list
6471     (gnus-group-real-name (gnus-group-group-name))
6472     (cond (current-prefix-arg
6473            (completing-read
6474             "Faq dir: " (and (listp gnus-group-faq-directory)
6475                              gnus-group-faq-directory))))))
6476   (or faq-dir
6477       (setq faq-dir (if (listp gnus-group-faq-directory)
6478                         (car gnus-group-faq-directory)
6479                       gnus-group-faq-directory)))
6480   (or group (error "No group name given"))
6481   (let ((file (concat (file-name-as-directory faq-dir)
6482                       (gnus-group-real-name group))))
6483     (if (not (file-exists-p file))
6484         (error "No such file: %s" file)
6485       (find-file file))))
6486
6487 (defun gnus-group-describe-group (force &optional group)
6488   "Display a description of the current newsgroup."
6489   (interactive (list current-prefix-arg (gnus-group-group-name)))
6490   (and force (setq gnus-description-hashtb nil))
6491   (let ((method (gnus-find-method-for-group group))
6492         desc)
6493     (or group (error "No group name given"))
6494     (and (or (and gnus-description-hashtb
6495                   ;; We check whether this group's method has been
6496                   ;; queried for a description file.
6497                   (gnus-gethash
6498                    (gnus-group-prefixed-name "" method)
6499                    gnus-description-hashtb))
6500              (setq desc (gnus-group-get-description group))
6501              (gnus-read-descriptions-file method))
6502          (gnus-message 1
6503           (or desc (gnus-gethash group gnus-description-hashtb)
6504               "No description available")))))
6505
6506 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6507 (defun gnus-group-describe-all-groups (&optional force)
6508   "Pop up a buffer with descriptions of all newsgroups."
6509   (interactive "P")
6510   (and force (setq gnus-description-hashtb nil))
6511   (if (not (or gnus-description-hashtb
6512                (gnus-read-all-descriptions-files)))
6513       (error "Couldn't request descriptions file"))
6514   (let ((buffer-read-only nil)
6515         b)
6516     (erase-buffer)
6517     (mapatoms
6518      (lambda (group)
6519        (setq b (point))
6520        (insert (format "      *: %-20s %s\n" (symbol-name group)
6521                        (symbol-value group)))
6522        (gnus-add-text-properties
6523         b (1+ b) (list 'gnus-group group
6524                        'gnus-unread t 'gnus-marked nil
6525                        'gnus-level (1+ gnus-level-subscribed))))
6526      gnus-description-hashtb)
6527     (goto-char (point-min))
6528     (gnus-group-position-point)))
6529
6530 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6531 (defun gnus-group-apropos (regexp &optional search-description)
6532   "List all newsgroups that have names that match a regexp."
6533   (interactive "sGnus apropos (regexp): ")
6534   (let ((prev "")
6535         (obuf (current-buffer))
6536         groups des)
6537     ;; Go through all newsgroups that are known to Gnus.
6538     (mapatoms
6539      (lambda (group)
6540        (and (symbol-name group)
6541             (string-match regexp (symbol-name group))
6542             (setq groups (cons (symbol-name group) groups))))
6543      gnus-active-hashtb)
6544     ;; Also go through all descriptions that are known to Gnus.
6545     (when search-description
6546       (mapatoms
6547        (lambda (group)
6548          (and (string-match regexp (symbol-value group))
6549               (gnus-active (symbol-name group))
6550               (setq groups (cons (symbol-name group) groups))))
6551        gnus-description-hashtb))
6552     (if (not groups)
6553         (gnus-message 3 "No groups matched \"%s\"." regexp)
6554       ;; Print out all the groups.
6555       (save-excursion
6556         (pop-to-buffer "*Gnus Help*")
6557         (buffer-disable-undo (current-buffer))
6558         (erase-buffer)
6559         (setq groups (sort groups 'string<))
6560         (while groups
6561           ;; Groups may be entered twice into the list of groups.
6562           (if (not (string= (car groups) prev))
6563               (progn
6564                 (insert (setq prev (car groups)) "\n")
6565                 (if (and gnus-description-hashtb
6566                          (setq des (gnus-gethash (car groups)
6567                                                  gnus-description-hashtb)))
6568                     (insert "  " des "\n"))))
6569           (setq groups (cdr groups)))
6570         (goto-char (point-min))))
6571     (pop-to-buffer obuf)))
6572
6573 (defun gnus-group-description-apropos (regexp)
6574   "List all newsgroups that have names or descriptions that match a regexp."
6575   (interactive "sGnus description apropos (regexp): ")
6576   (if (not (or gnus-description-hashtb
6577                (gnus-read-all-descriptions-files)))
6578       (error "Couldn't request descriptions file"))
6579   (gnus-group-apropos regexp t))
6580
6581 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6582 (defun gnus-group-list-matching (level regexp &optional all lowest)
6583   "List all groups with unread articles that match REGEXP.
6584 If the prefix LEVEL is non-nil, it should be a number that says which
6585 level to cut off listing groups.
6586 If ALL, also list groups with no unread articles.
6587 If LOWEST, don't list groups with level lower than LOWEST.
6588
6589 This command may read the active file."
6590   (interactive "P\nsList newsgroups matching: ")
6591   ;; First make sure active file has been read.
6592   (when (and level
6593              (>= (prefix-numeric-value level) gnus-level-killed))
6594     (gnus-get-killed-groups))
6595   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6596                            all (or lowest 1) regexp)
6597   (goto-char (point-min))
6598   (gnus-group-position-point))
6599
6600 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6601   "List all groups that match REGEXP.
6602 If the prefix LEVEL is non-nil, it should be a number that says which
6603 level to cut off listing groups.
6604 If LOWEST, don't list groups with level lower than LOWEST."
6605   (interactive "P\nsList newsgroups matching: ")
6606   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6607
6608 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6609 (defun gnus-group-save-newsrc (&optional force)
6610   "Save the Gnus startup files.
6611 If FORCE, force saving whether it is necessary or not."
6612   (interactive "P")
6613   (gnus-save-newsrc-file force))
6614
6615 (defun gnus-group-restart (&optional arg)
6616   "Force Gnus to read the .newsrc file."
6617   (interactive "P")
6618   (when (gnus-yes-or-no-p
6619          (format "Are you sure you want to read %s? "
6620                  gnus-current-startup-file))
6621     (gnus-save-newsrc-file)
6622     (gnus-setup-news 'force)
6623     (gnus-group-list-groups arg)))
6624
6625 (defun gnus-group-read-init-file ()
6626   "Read the Gnus elisp init file."
6627   (interactive)
6628   (gnus-read-init-file))
6629
6630 (defun gnus-group-check-bogus-groups (&optional silent)
6631   "Check bogus newsgroups.
6632 If given a prefix, don't ask for confirmation before removing a bogus
6633 group."
6634   (interactive "P")
6635   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6636   (gnus-group-list-groups))
6637
6638 (defun gnus-group-edit-global-kill (&optional article group)
6639   "Edit the global kill file.
6640 If GROUP, edit that local kill file instead."
6641   (interactive "P")
6642   (setq gnus-current-kill-article article)
6643   (gnus-kill-file-edit-file group)
6644   (gnus-message
6645    6
6646    (substitute-command-keys
6647     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6648             (if group "local" "global")))))
6649
6650 (defun gnus-group-edit-local-kill (article group)
6651   "Edit a local kill file."
6652   (interactive (list nil (gnus-group-group-name)))
6653   (gnus-group-edit-global-kill article group))
6654
6655 (defun gnus-group-force-update ()
6656   "Update `.newsrc' file."
6657   (interactive)
6658   (gnus-save-newsrc-file))
6659
6660 (defun gnus-group-suspend ()
6661   "Suspend the current Gnus session.
6662 In fact, cleanup buffers except for group mode buffer.
6663 The hook gnus-suspend-gnus-hook is called before actually suspending."
6664   (interactive)
6665   (run-hooks 'gnus-suspend-gnus-hook)
6666   ;; Kill Gnus buffers except for group mode buffer.
6667   (let ((group-buf (get-buffer gnus-group-buffer)))
6668     ;; Do this on a separate list in case the user does a ^G before we finish
6669     (let ((gnus-buffer-list
6670            (delq group-buf (delq gnus-dribble-buffer
6671                                  (append gnus-buffer-list nil)))))
6672       (while gnus-buffer-list
6673         (gnus-kill-buffer (car gnus-buffer-list))
6674         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6675     (if group-buf
6676         (progn
6677           (setq gnus-buffer-list (list group-buf))
6678           (bury-buffer group-buf)
6679           (delete-windows-on group-buf t)))))
6680
6681 (defun gnus-group-clear-dribble ()
6682   "Clear all information from the dribble buffer."
6683   (interactive)
6684   (gnus-dribble-clear)
6685   (gnus-message 7 "Cleared dribble buffer"))
6686
6687 (defun gnus-group-exit ()
6688   "Quit reading news after updating .newsrc.eld and .newsrc.
6689 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6690   (interactive)
6691   (when 
6692       (or noninteractive                ;For gnus-batch-kill
6693           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6694           (not gnus-interactive-exit)   ;Without confirmation
6695           gnus-expert-user
6696           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6697     (run-hooks 'gnus-exit-gnus-hook)
6698     ;; Offer to save data from non-quitted summary buffers.
6699     (gnus-offer-save-summaries)
6700     ;; Save the newsrc file(s).
6701     (gnus-save-newsrc-file)
6702     ;; Kill-em-all.
6703     (gnus-close-backends)
6704     ;; Reset everything.
6705     (gnus-clear-system)
6706     ;; Allow the user to do things after cleaning up.
6707     (run-hooks 'gnus-after-exiting-gnus-hook)))
6708
6709 (defun gnus-close-backends ()
6710   ;; Send a close request to all backends that support such a request.
6711   (let ((methods gnus-valid-select-methods)
6712         func)
6713     (while methods
6714       (if (fboundp (setq func (intern (concat (caar methods)
6715                                               "-request-close"))))
6716           (funcall func))
6717       (setq methods (cdr methods)))))
6718
6719 (defun gnus-group-quit ()
6720   "Quit reading news without updating .newsrc.eld or .newsrc.
6721 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6722   (interactive)
6723   (when (or noninteractive              ;For gnus-batch-kill
6724             (zerop (buffer-size))
6725             (not (gnus-server-opened gnus-select-method))
6726             gnus-expert-user
6727             (not gnus-current-startup-file)
6728             (gnus-yes-or-no-p
6729              (format "Quit reading news without saving %s? "
6730                      (file-name-nondirectory gnus-current-startup-file))))
6731     (run-hooks 'gnus-exit-gnus-hook)
6732     (if gnus-use-full-window
6733         (delete-other-windows)
6734       (gnus-remove-some-windows))
6735     (gnus-dribble-save)
6736     (gnus-close-backends)
6737     (gnus-clear-system)
6738     ;; Allow the user to do things after cleaning up.
6739     (run-hooks 'gnus-after-exiting-gnus-hook)))
6740
6741 (defun gnus-offer-save-summaries ()
6742   "Offer to save all active summary buffers."
6743   (save-excursion
6744     (let ((buflist (buffer-list))
6745           buffers bufname)
6746       ;; Go through all buffers and find all summaries.
6747       (while buflist
6748         (and (setq bufname (buffer-name (car buflist)))
6749              (string-match "Summary" bufname)
6750              (save-excursion
6751                (set-buffer bufname)
6752                ;; We check that this is, indeed, a summary buffer.
6753                (and (eq major-mode 'gnus-summary-mode)
6754                     ;; Also make sure this isn't bogus.
6755                     gnus-newsgroup-prepared))
6756              (push bufname buffers))
6757         (setq buflist (cdr buflist)))
6758       ;; Go through all these summary buffers and offer to save them.
6759       (when buffers
6760         (map-y-or-n-p
6761          "Update summary buffer %s? "
6762          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6763          buffers)))))
6764
6765 (defun gnus-group-describe-briefly ()
6766   "Give a one line description of the group mode commands."
6767   (interactive)
6768   (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")))
6769
6770 (defun gnus-group-browse-foreign-server (method)
6771   "Browse a foreign news server.
6772 If called interactively, this function will ask for a select method
6773  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6774 If not, METHOD should be a list where the first element is the method
6775 and the second element is the address."
6776   (interactive
6777    (list (let ((how (completing-read
6778                      "Which backend: "
6779                      (append gnus-valid-select-methods gnus-server-alist)
6780                      nil t (cons "nntp" 0) 'gnus-method-history)))
6781            ;; We either got a backend name or a virtual server name.
6782            ;; If the first, we also need an address.
6783            (if (assoc how gnus-valid-select-methods)
6784                (list (intern how)
6785                      ;; Suggested by mapjph@bath.ac.uk.
6786                      (completing-read
6787                       "Address: "
6788                       (mapcar (lambda (server) (list server))
6789                               gnus-secondary-servers)))
6790              ;; We got a server name, so we find the method.
6791              (gnus-server-to-method how)))))
6792   (gnus-browse-foreign-server method))
6793
6794 \f
6795 ;;;
6796 ;;; Gnus summary mode
6797 ;;;
6798
6799 (defvar gnus-summary-mode-map nil)
6800
6801 (put 'gnus-summary-mode 'mode-class 'special)
6802
6803 (unless gnus-summary-mode-map
6804   (setq gnus-summary-mode-map (make-keymap))
6805   (suppress-keymap gnus-summary-mode-map)
6806
6807   ;; Non-orthogonal keys
6808
6809   (gnus-define-keys gnus-summary-mode-map
6810     " " gnus-summary-next-page
6811     "\177" gnus-summary-prev-page
6812     [delete] gnus-summary-prev-page
6813     "\r" gnus-summary-scroll-up
6814     "n" gnus-summary-next-unread-article
6815     "p" gnus-summary-prev-unread-article
6816     "N" gnus-summary-next-article
6817     "P" gnus-summary-prev-article
6818     "\M-\C-n" gnus-summary-next-same-subject
6819     "\M-\C-p" gnus-summary-prev-same-subject
6820     "\M-n" gnus-summary-next-unread-subject
6821     "\M-p" gnus-summary-prev-unread-subject
6822     "." gnus-summary-first-unread-article
6823     "," gnus-summary-best-unread-article
6824     "\M-s" gnus-summary-search-article-forward
6825     "\M-r" gnus-summary-search-article-backward
6826     "<" gnus-summary-beginning-of-article
6827     ">" gnus-summary-end-of-article
6828     "j" gnus-summary-goto-article
6829     "^" gnus-summary-refer-parent-article
6830     "\M-^" gnus-summary-refer-article
6831     "u" gnus-summary-tick-article-forward
6832     "!" gnus-summary-tick-article-forward
6833     "U" gnus-summary-tick-article-backward
6834     "d" gnus-summary-mark-as-read-forward
6835     "D" gnus-summary-mark-as-read-backward
6836     "E" gnus-summary-mark-as-expirable
6837     "\M-u" gnus-summary-clear-mark-forward
6838     "\M-U" gnus-summary-clear-mark-backward
6839     "k" gnus-summary-kill-same-subject-and-select
6840     "\C-k" gnus-summary-kill-same-subject
6841     "\M-\C-k" gnus-summary-kill-thread
6842     "\M-\C-l" gnus-summary-lower-thread
6843     "e" gnus-summary-edit-article
6844     "#" gnus-summary-mark-as-processable
6845     "\M-#" gnus-summary-unmark-as-processable
6846     "\M-\C-t" gnus-summary-toggle-threads
6847     "\M-\C-s" gnus-summary-show-thread
6848     "\M-\C-h" gnus-summary-hide-thread
6849     "\M-\C-f" gnus-summary-next-thread
6850     "\M-\C-b" gnus-summary-prev-thread
6851     "\M-\C-u" gnus-summary-up-thread
6852     "\M-\C-d" gnus-summary-down-thread
6853     "&" gnus-summary-execute-command
6854     "c" gnus-summary-catchup-and-exit
6855     "\C-w" gnus-summary-mark-region-as-read
6856     "\C-t" gnus-summary-toggle-truncation
6857     "?" gnus-summary-mark-as-dormant
6858     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6859     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6860     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6861     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6862     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6863     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6864     "=" gnus-summary-expand-window
6865     "\C-x\C-s" gnus-summary-reselect-current-group
6866     "\M-g" gnus-summary-rescan-group
6867     "w" gnus-summary-stop-page-breaking
6868     "\C-c\C-r" gnus-summary-caesar-message
6869     "\M-t" gnus-summary-toggle-mime
6870     "f" gnus-summary-followup
6871     "F" gnus-summary-followup-with-original
6872     "C" gnus-summary-cancel-article
6873     "r" gnus-summary-reply
6874     "R" gnus-summary-reply-with-original
6875     "\C-c\C-f" gnus-summary-mail-forward
6876     "o" gnus-summary-save-article
6877     "\C-o" gnus-summary-save-article-mail
6878     "|" gnus-summary-pipe-output
6879     "\M-k" gnus-summary-edit-local-kill
6880     "\M-K" gnus-summary-edit-global-kill
6881     "V" gnus-version
6882     "\C-c\C-d" gnus-summary-describe-group
6883     "q" gnus-summary-exit
6884     "Q" gnus-summary-exit-no-update
6885     "\C-c\C-i" gnus-info-find-node
6886     gnus-mouse-2 gnus-mouse-pick-article
6887     "m" gnus-summary-mail-other-window
6888     "a" gnus-summary-post-news
6889     "x" gnus-summary-limit-to-unread
6890     "s" gnus-summary-isearch-article
6891     "t" gnus-article-hide-headers
6892     "g" gnus-summary-show-article
6893     "l" gnus-summary-goto-last-article
6894     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6895     "\C-d" gnus-summary-enter-digest-group
6896     "\C-c\C-b" gnus-bug
6897     "*" gnus-cache-enter-article
6898     "\M-*" gnus-cache-remove-article
6899     "\M-&" gnus-summary-universal-argument
6900     "\C-l" gnus-recenter
6901     "I" gnus-summary-increase-score
6902     "L" gnus-summary-lower-score
6903
6904     "V" gnus-summary-score-map
6905     "X" gnus-uu-extract-map
6906     "S" gnus-summary-send-map)
6907
6908   ;; Sort of orthogonal keymap
6909   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6910     "t" gnus-summary-tick-article-forward
6911     "!" gnus-summary-tick-article-forward
6912     "d" gnus-summary-mark-as-read-forward
6913     "r" gnus-summary-mark-as-read-forward
6914     "c" gnus-summary-clear-mark-forward
6915     " " gnus-summary-clear-mark-forward
6916     "e" gnus-summary-mark-as-expirable
6917     "x" gnus-summary-mark-as-expirable
6918     "?" gnus-summary-mark-as-dormant
6919     "b" gnus-summary-set-bookmark
6920     "B" gnus-summary-remove-bookmark
6921     "#" gnus-summary-mark-as-processable
6922     "\M-#" gnus-summary-unmark-as-processable
6923     "S" gnus-summary-limit-include-expunged
6924     "C" gnus-summary-catchup
6925     "H" gnus-summary-catchup-to-here
6926     "\C-c" gnus-summary-catchup-all
6927     "k" gnus-summary-kill-same-subject-and-select
6928     "K" gnus-summary-kill-same-subject
6929     "P" gnus-uu-mark-map)
6930
6931   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6932     "c" gnus-summary-clear-above
6933     "u" gnus-summary-tick-above
6934     "m" gnus-summary-mark-above
6935     "k" gnus-summary-kill-below)
6936
6937   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6938     "/" gnus-summary-limit-to-subject
6939     "n" gnus-summary-limit-to-articles
6940     "w" gnus-summary-pop-limit
6941     "s" gnus-summary-limit-to-subject
6942     "a" gnus-summary-limit-to-author
6943     "u" gnus-summary-limit-to-unread
6944     "m" gnus-summary-limit-to-marks
6945     "v" gnus-summary-limit-to-score
6946     "D" gnus-summary-limit-include-dormant
6947     "d" gnus-summary-limit-exclude-dormant
6948     ;;  "t" gnus-summary-limit-exclude-thread
6949     "E" gnus-summary-limit-include-expunged
6950     "c" gnus-summary-limit-exclude-childless-dormant
6951     "C" gnus-summary-limit-mark-excluded-as-read)
6952
6953   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6954     "n" gnus-summary-next-unread-article
6955     "p" gnus-summary-prev-unread-article
6956     "N" gnus-summary-next-article
6957     "P" gnus-summary-prev-article
6958     "\C-n" gnus-summary-next-same-subject
6959     "\C-p" gnus-summary-prev-same-subject
6960     "\M-n" gnus-summary-next-unread-subject
6961     "\M-p" gnus-summary-prev-unread-subject
6962     "f" gnus-summary-first-unread-article
6963     "b" gnus-summary-best-unread-article
6964     "j" gnus-summary-goto-article
6965     "g" gnus-summary-goto-subject
6966     "l" gnus-summary-goto-last-article
6967     "p" gnus-summary-pop-article)
6968
6969   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6970     "k" gnus-summary-kill-thread
6971     "l" gnus-summary-lower-thread
6972     "i" gnus-summary-raise-thread
6973     "T" gnus-summary-toggle-threads
6974     "t" gnus-summary-rethread-current
6975     "^" gnus-summary-reparent-thread
6976     "s" gnus-summary-show-thread
6977     "S" gnus-summary-show-all-threads
6978     "h" gnus-summary-hide-thread
6979     "H" gnus-summary-hide-all-threads
6980     "n" gnus-summary-next-thread
6981     "p" gnus-summary-prev-thread
6982     "u" gnus-summary-up-thread
6983     "o" gnus-summary-top-thread
6984     "d" gnus-summary-down-thread
6985     "#" gnus-uu-mark-thread
6986     "\M-#" gnus-uu-unmark-thread)
6987
6988   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6989     "c" gnus-summary-catchup-and-exit
6990     "C" gnus-summary-catchup-all-and-exit
6991     "E" gnus-summary-exit-no-update
6992     "Q" gnus-summary-exit
6993     "Z" gnus-summary-exit
6994     "n" gnus-summary-catchup-and-goto-next-group
6995     "R" gnus-summary-reselect-current-group
6996     "G" gnus-summary-rescan-group
6997     "N" gnus-summary-next-group
6998     "P" gnus-summary-prev-group)
6999
7000   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7001     " " gnus-summary-next-page
7002     "n" gnus-summary-next-page
7003     "\177" gnus-summary-prev-page
7004     [delete] gnus-summary-prev-page
7005     "p" gnus-summary-prev-page
7006     "\r" gnus-summary-scroll-up
7007     "<" gnus-summary-beginning-of-article
7008     ">" gnus-summary-end-of-article
7009     "b" gnus-summary-beginning-of-article
7010     "e" gnus-summary-end-of-article
7011     "^" gnus-summary-refer-parent-article
7012     "r" gnus-summary-refer-parent-article
7013     "R" gnus-summary-refer-references
7014     "g" gnus-summary-show-article
7015     "s" gnus-summary-isearch-article)
7016
7017   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7018     "b" gnus-article-add-buttons
7019     "B" gnus-article-add-buttons-to-head
7020     "o" gnus-article-treat-overstrike
7021     ;;  "w" gnus-article-word-wrap
7022     "w" gnus-article-fill-cited-article
7023     "c" gnus-article-remove-cr
7024     "L" gnus-article-remove-trailing-blank-lines
7025     "q" gnus-article-de-quoted-unreadable
7026     "f" gnus-article-display-x-face
7027     "l" gnus-summary-stop-page-breaking
7028     "r" gnus-summary-caesar-message
7029     "t" gnus-article-hide-headers
7030     "v" gnus-summary-verbose-headers
7031     "m" gnus-summary-toggle-mime)
7032
7033   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7034     "a" gnus-article-hide
7035     "h" gnus-article-hide-headers
7036     "b" gnus-article-hide-boring-headers
7037     "s" gnus-article-hide-signature
7038     "c" gnus-article-hide-citation
7039     "p" gnus-article-hide-pgp
7040     "\C-c" gnus-article-hide-citation-maybe)
7041
7042   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7043     "a" gnus-article-highlight
7044     "h" gnus-article-highlight-headers
7045     "c" gnus-article-highlight-citation
7046     "s" gnus-article-highlight-signature)
7047
7048   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7049     "z" gnus-article-date-ut
7050     "u" gnus-article-date-ut
7051     "l" gnus-article-date-local
7052     "e" gnus-article-date-lapsed
7053     "o" gnus-article-date-original)
7054
7055   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7056     "v" gnus-version
7057     "f" gnus-summary-fetch-faq
7058     "d" gnus-summary-describe-group
7059     "h" gnus-summary-describe-briefly
7060     "i" gnus-info-find-node)
7061
7062   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7063     "e" gnus-summary-expire-articles
7064     "\M-\C-e" gnus-summary-expire-articles-now
7065     "\177" gnus-summary-delete-article
7066     [delete] gnus-summary-delete-article
7067     "m" gnus-summary-move-article
7068     "r" gnus-summary-respool-article
7069     "w" gnus-summary-edit-article
7070     "c" gnus-summary-copy-article
7071     "B" gnus-summary-crosspost-article
7072     "q" gnus-summary-respool-query
7073     "i" gnus-summary-import-article)
7074
7075   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7076     "o" gnus-summary-save-article
7077     "m" gnus-summary-save-article-mail
7078     "r" gnus-summary-save-article-rmail
7079     "f" gnus-summary-save-article-file
7080     "b" gnus-summary-save-article-body-file
7081     "h" gnus-summary-save-article-folder
7082     "v" gnus-summary-save-article-vm
7083     "p" gnus-summary-pipe-output
7084     "s" gnus-soup-add-article)
7085   )
7086
7087 \f
7088
7089 (defun gnus-summary-mode (&optional group)
7090   "Major mode for reading articles.
7091
7092 All normal editing commands are switched off.
7093 \\<gnus-summary-mode-map>
7094 Each line in this buffer represents one article.  To read an
7095 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7096 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7097 respectively.
7098
7099 You can also post articles and send mail from this buffer.  To
7100 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7101 of an article, type `\\[gnus-summary-reply]'.
7102
7103 There are approx. one gazillion commands you can execute in this
7104 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7105
7106 The following commands are available:
7107
7108 \\{gnus-summary-mode-map}"
7109   (interactive)
7110   (when (and menu-bar-mode
7111              (gnus-visual-p 'summary-menu 'menu))
7112     (gnus-summary-make-menu-bar))
7113   (kill-all-local-variables)
7114   (let ((locals gnus-summary-local-variables))
7115     (while locals
7116       (if (consp (car locals))
7117           (progn
7118             (make-local-variable (caar locals))
7119             (set (caar locals) (eval (cdar locals))))
7120         (make-local-variable (car locals))
7121         (set (car locals) nil))
7122       (setq locals (cdr locals))))
7123   (gnus-make-thread-indent-array)
7124   (gnus-simplify-mode-line)
7125   (setq major-mode 'gnus-summary-mode)
7126   (setq mode-name "Summary")
7127   (make-local-variable 'minor-mode-alist)
7128   (use-local-map gnus-summary-mode-map)
7129   (buffer-disable-undo (current-buffer))
7130   (setq buffer-read-only t)             ;Disable modification
7131   (setq truncate-lines t)
7132   (setq selective-display t)
7133   (setq selective-display-ellipses t)   ;Display `...'
7134   (setq buffer-display-table gnus-summary-display-table)
7135   (setq gnus-newsgroup-name group)
7136   (make-local-variable 'gnus-summary-line-format)
7137   (make-local-variable 'gnus-summary-line-format-spec)
7138   (make-local-variable 'gnus-summary-mark-positions)
7139   (run-hooks 'gnus-summary-mode-hook))
7140
7141 (defun gnus-summary-make-display-table ()
7142   ;; Change the display table.  Odd characters have a tendency to mess
7143   ;; up nicely formatted displays - we make all possible glyphs
7144   ;; display only a single character.
7145
7146   ;; We start from the standard display table, if any.
7147   (setq gnus-summary-display-table
7148         (or (copy-sequence standard-display-table)
7149             (make-display-table)))
7150   ;; Nix out all the control chars...
7151   (let ((i 32))
7152     (while (>= (setq i (1- i)) 0)
7153       (aset gnus-summary-display-table i [??])))
7154   ;; ... but not newline and cr, of course. (cr is necessary for the
7155   ;; selective display).
7156   (aset gnus-summary-display-table ?\n nil)
7157   (aset gnus-summary-display-table ?\r nil)
7158   ;; We nix out any glyphs over 126 that are not set already.
7159   (let ((i 256))
7160     (while (>= (setq i (1- i)) 127)
7161       ;; Only modify if the entry is nil.
7162       (or (aref gnus-summary-display-table i)
7163           (aset gnus-summary-display-table i [??])))))
7164
7165 (defun gnus-summary-clear-local-variables ()
7166   (let ((locals gnus-summary-local-variables))
7167     (while locals
7168       (if (consp (car locals))
7169           (and (vectorp (caar locals))
7170                (set (caar locals) nil))
7171         (and (vectorp (car locals))
7172              (set (car locals) nil)))
7173       (setq locals (cdr locals)))))
7174
7175 ;; Summary data functions.
7176
7177 (defmacro gnus-data-number (data)
7178   `(car ,data))
7179
7180 (defmacro gnus-data-set-number (data number)
7181   `(setcar ,data ,number))
7182
7183 (defmacro gnus-data-mark (data)
7184   `(nth 1 ,data))
7185
7186 (defmacro gnus-data-set-mark (data mark)
7187   `(setcar (nthcdr 1 ,data) ,mark))
7188
7189 (defmacro gnus-data-pos (data)
7190   `(nth 2 ,data))
7191
7192 (defmacro gnus-data-set-pos (data pos)
7193   `(setcar (nthcdr 2 ,data) ,pos))
7194
7195 (defmacro gnus-data-header (data)
7196   `(nth 3 ,data))
7197
7198 (defmacro gnus-data-level (data)
7199   `(nth 4 ,data))
7200
7201 (defmacro gnus-data-unread-p (data)
7202   `(= (nth 1 ,data) gnus-unread-mark))
7203
7204 (defmacro gnus-data-pseudo-p (data)
7205   `(consp (nth 3 ,data)))
7206
7207 (defmacro gnus-data-find (number)
7208   `(assq ,number gnus-newsgroup-data))
7209
7210 (defmacro gnus-data-find-list (number &optional data)
7211   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7212      (memq (assq ,number bdata)
7213            bdata)))
7214
7215 (defmacro gnus-data-make (number mark pos header level)
7216   `(list ,number ,mark ,pos ,header ,level))
7217
7218 (defun gnus-data-enter (after-article number mark pos header level offset)
7219   (let ((data (gnus-data-find-list after-article)))
7220     (or data (error "No such article: %d" after-article))
7221     (setcdr data (cons (gnus-data-make number mark pos header level)
7222                        (cdr data)))
7223     (setq gnus-newsgroup-data-reverse nil)
7224     (gnus-data-update-list (cddr data) offset)))
7225
7226 (defun gnus-data-enter-list (after-article list &optional offset)
7227   (when list
7228     (let ((data (and after-article (gnus-data-find-list after-article)))
7229           (ilist list))
7230       (or data (not after-article) (error "No such article: %d" after-article))
7231       ;; Find the last element in the list to be spliced into the main
7232       ;; list.
7233       (while (cdr list)
7234         (setq list (cdr list)))
7235       (if (not data)
7236           (progn
7237             (setcdr list gnus-newsgroup-data)
7238             (setq gnus-newsgroup-data ilist)
7239             (and offset (gnus-data-update-list (cdr list) offset)))
7240         (setcdr list (cdr data))
7241         (setcdr data ilist)
7242         (and offset (gnus-data-update-list (cdr data) offset)))
7243       (setq gnus-newsgroup-data-reverse nil))))
7244
7245 (defun gnus-data-remove (article &optional offset)
7246   (let ((data gnus-newsgroup-data))
7247     (if (= (gnus-data-number (car data)) article)
7248         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7249               gnus-newsgroup-data-reverse nil)
7250       (while (cdr data)
7251         (and (= (gnus-data-number (cadr data)) article)
7252              (progn
7253                (setcdr data (cddr data))
7254                (and offset (gnus-data-update-list (cdr data) offset))
7255                (setq data nil
7256                      gnus-newsgroup-data-reverse nil)))
7257         (setq data (cdr data))))))
7258
7259 (defmacro gnus-data-list (backward)
7260   `(if ,backward
7261        (or gnus-newsgroup-data-reverse
7262            (setq gnus-newsgroup-data-reverse
7263                  (reverse gnus-newsgroup-data)))
7264      gnus-newsgroup-data))
7265
7266 (defun gnus-data-update-list (data offset)
7267   "Add OFFSET to the POS of all data entries in DATA."
7268   (while data
7269     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7270     (setq data (cdr data))))
7271
7272 (defun gnus-data-compute-positions ()
7273   "Compute the positions of all articles."
7274   (let ((data gnus-newsgroup-data)
7275         pos)
7276     (while data
7277       (when (setq pos (text-property-any
7278                        (point-min) (point-max)
7279                        'gnus-number (gnus-data-number (car data))))
7280         (gnus-data-set-pos (car data) (+ pos 3)))
7281       (setq data (cdr data)))))
7282
7283 (defun gnus-summary-article-pseudo-p (article)
7284   "Say whether this article is a pseudo article or not."
7285   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7286
7287 (defun gnus-article-parent-p (number)
7288   "Say whether this article is a parent or not."
7289   (let ((data (gnus-data-find-list number)))
7290     (and (cdr data)                     ; There has to be an article after...
7291          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7292             (gnus-data-level (nth 1 data))))))
7293
7294 (defun gnus-article-children (number)
7295   "Return a list of all children to NUMBER."
7296   (let* ((data (gnus-data-find-list number))
7297          (level (gnus-data-level (car data)))
7298          children)
7299     (setq data (cdr data))
7300     (while (and data            
7301                 (= (gnus-data-level (car data)) (1+ level)))
7302       (push (gnus-data-number (car data)) children)
7303       (setq data (cdr data)))
7304     children))
7305
7306 (defmacro gnus-summary-skip-intangible ()
7307   "If the current article is intangible, then jump to a different article."
7308   '(let ((to (get-text-property (point) 'gnus-intangible)))
7309     (and to (gnus-summary-goto-subject to))))
7310
7311 (defmacro gnus-summary-article-intangible-p ()
7312   "Say whether this article is intangible or not."
7313   '(get-text-property (point) 'gnus-intangible))
7314
7315 ;; Some summary mode macros.
7316
7317 (defmacro gnus-summary-article-number ()
7318   "The article number of the article on the current line.
7319 If there isn's an article number here, then we return the current
7320 article number."
7321   '(progn
7322      (gnus-summary-skip-intangible)
7323      (or (get-text-property (point) 'gnus-number)
7324          (gnus-summary-last-subject))))
7325
7326 (defmacro gnus-summary-article-header (&optional number)
7327   `(gnus-data-header (gnus-data-find
7328                       ,(or number '(gnus-summary-article-number)))))
7329
7330 (defmacro gnus-summary-thread-level (&optional number)
7331   `(if (and (eq gnus-summary-make-false-root 'dummy)
7332             (get-text-property (point) 'gnus-intangible))
7333        0
7334      (gnus-data-level (gnus-data-find
7335                        ,(or number '(gnus-summary-article-number))))))
7336
7337 (defmacro gnus-summary-article-mark (&optional number)
7338   `(gnus-data-mark (gnus-data-find
7339                     ,(or number '(gnus-summary-article-number)))))
7340
7341 (defmacro gnus-summary-article-pos (&optional number)
7342   `(gnus-data-pos (gnus-data-find
7343                    ,(or number '(gnus-summary-article-number)))))
7344
7345 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7346 (defmacro gnus-summary-article-subject (&optional number)
7347   "Return current subject string or nil if nothing."
7348   `(let ((headers
7349           ,(if number
7350                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7351              '(gnus-data-header (assq (gnus-summary-article-number)
7352                                       gnus-newsgroup-data)))))
7353      (and headers
7354           (vectorp headers)
7355           (mail-header-subject headers))))
7356
7357 (defmacro gnus-summary-article-score (&optional number)
7358   "Return current article score."
7359   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7360                   gnus-newsgroup-scored))
7361        gnus-summary-default-score 0))
7362
7363 (defun gnus-summary-article-children (&optional number)
7364   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7365          (level (gnus-data-level (car data)))
7366          l children)
7367     (while (and (setq data (cdr data))
7368                 (> (setq l (gnus-data-level (car data))) level))
7369       (and (= (1+ level) l)
7370            (setq children (cons (gnus-data-number (car data))
7371                                 children))))
7372     (nreverse children)))
7373
7374 (defun gnus-summary-article-parent (&optional number)
7375   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7376                                     (gnus-data-list t)))
7377          (level (gnus-data-level (car data))))
7378     (if (zerop level)
7379         () ; This is a root.
7380       ;; We search until we find an article with a level less than
7381       ;; this one.  That function has to be the parent.
7382       (while (and (setq data (cdr data))
7383                   (not (< (gnus-data-level (car data)) level))))
7384       (and data (gnus-data-number (car data))))))
7385
7386 (defun gnus-unread-mark-p (mark)
7387   "Say whether MARK is the unread mark."
7388   (= mark gnus-unread-mark))
7389
7390 (defun gnus-read-mark-p (mark)
7391   "Say whether MARK is one of the marks that mark as read.
7392 This is all marks except unread, ticked, dormant, and expirable."
7393   (not (or (= mark gnus-unread-mark)
7394            (= mark gnus-ticked-mark)
7395            (= mark gnus-dormant-mark)
7396            (= mark gnus-expirable-mark))))
7397
7398 ;; Various summary mode internalish functions.
7399
7400 (defun gnus-mouse-pick-article (e)
7401   (interactive "e")
7402   (mouse-set-point e)
7403   (gnus-summary-next-page nil t))
7404
7405 (defun gnus-summary-setup-buffer (group)
7406   "Initialize summary buffer."
7407   (let ((buffer (concat "*Summary " group "*")))
7408     (if (get-buffer buffer)
7409         (progn
7410           (set-buffer buffer)
7411           (setq gnus-summary-buffer (current-buffer))
7412           (not gnus-newsgroup-prepared))
7413       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7414       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7415       (gnus-add-current-to-buffer-list)
7416       (gnus-summary-mode group)
7417       (when gnus-carpal
7418         (gnus-carpal-setup-buffer 'summary))
7419       (unless gnus-single-article-buffer
7420         (make-local-variable 'gnus-article-buffer)
7421         (make-local-variable 'gnus-article-current)
7422         (make-local-variable 'gnus-original-article-buffer))
7423       (setq gnus-newsgroup-name group)
7424       t)))
7425
7426 (defun gnus-set-global-variables ()
7427   ;; Set the global equivalents of the summary buffer-local variables
7428   ;; to the latest values they had.  These reflect the summary buffer
7429   ;; that was in action when the last article was fetched.
7430   (when (eq major-mode 'gnus-summary-mode)
7431     (setq gnus-summary-buffer (current-buffer))
7432     (let ((name gnus-newsgroup-name)
7433           (marked gnus-newsgroup-marked)
7434           (unread gnus-newsgroup-unreads)
7435           (headers gnus-current-headers)
7436           (data gnus-newsgroup-data)
7437           (summary gnus-summary-buffer)
7438           (article-buffer gnus-article-buffer)
7439           (original gnus-original-article-buffer)
7440           (gac gnus-article-current)
7441           (score-file gnus-current-score-file))
7442       (save-excursion
7443         (set-buffer gnus-group-buffer)
7444         (setq gnus-newsgroup-name name)
7445         (setq gnus-newsgroup-marked marked)
7446         (setq gnus-newsgroup-unreads unread)
7447         (setq gnus-current-headers headers)
7448         (setq gnus-newsgroup-data data)
7449         (setq gnus-article-current gac)
7450         (setq gnus-summary-buffer summary)
7451         (setq gnus-article-buffer article-buffer)
7452         (setq gnus-original-article-buffer original)
7453         (setq gnus-current-score-file score-file)))))
7454
7455 (defun gnus-summary-last-article-p (&optional article)
7456   "Return whether ARTICLE is the last article in the buffer."
7457   (if (not (setq article (or article (gnus-summary-article-number))))
7458       t ; All non-existant numbers are the last article. :-)
7459     (not (cdr (gnus-data-find-list article)))))
7460
7461 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7462   "Insert a dummy root in the summary buffer."
7463   (beginning-of-line)
7464   (gnus-add-text-properties
7465    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7466    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7467
7468 (defun gnus-make-thread-indent-array ()
7469   (let ((n 200))
7470     (unless (and gnus-thread-indent-array
7471                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7472       (setq gnus-thread-indent-array (make-vector 201 "")
7473             gnus-thread-indent-array-level gnus-thread-indent-level)
7474       (while (>= n 0)
7475         (aset gnus-thread-indent-array n
7476               (make-string (* n gnus-thread-indent-level) ? ))
7477         (setq n (1- n))))))
7478
7479 (defun gnus-summary-insert-line
7480   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7481                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7482                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7483   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7484          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7485          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7486          (gnus-tmp-score-char
7487           (if (or (null gnus-summary-default-score)
7488                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7489                       gnus-summary-zcore-fuzz)) ? 
7490             (if (< gnus-tmp-score gnus-summary-default-score)
7491                 gnus-score-below-mark gnus-score-over-mark)))
7492          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7493                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7494                                   gnus-cached-mark)
7495                                  (gnus-tmp-replied gnus-replied-mark)
7496                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7497                                   gnus-saved-mark)
7498                                  (t gnus-unread-mark)))
7499          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7500          (gnus-tmp-name
7501           (cond
7502            ((string-match "(.+)" gnus-tmp-from)
7503             (substring gnus-tmp-from
7504                        (1+ (match-beginning 0)) (1- (match-end 0))))
7505            ((string-match "<[^>]+> *$" gnus-tmp-from)
7506             (let ((beg (match-beginning 0)))
7507               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7508                        (substring gnus-tmp-from (1+ (match-beginning 0))
7509                                   (1- (match-end 0))))
7510                   (substring gnus-tmp-from 0 beg))))
7511            (t gnus-tmp-from)))
7512          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7513          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7514          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7515          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7516          (buffer-read-only nil))
7517     (when (string= gnus-tmp-name "")
7518       (setq gnus-tmp-name gnus-tmp-from))
7519     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7520     (gnus-put-text-property
7521      (point)
7522      (progn (eval gnus-summary-line-format-spec) (point))
7523      'gnus-number gnus-tmp-number)
7524     (when (gnus-visual-p 'summary-highlight 'highlight)
7525       (forward-line -1)
7526       (run-hooks 'gnus-summary-update-hook)
7527       (forward-line 1))))
7528
7529 (defun gnus-summary-update-line (&optional dont-update)
7530   ;; Update summary line after change.
7531   (when (and gnus-summary-default-score
7532              (not gnus-summary-inhibit-highlight))
7533     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7534            (article (gnus-summary-article-number))
7535            (score (gnus-summary-article-score article)))
7536       (unless dont-update
7537         (if (and gnus-summary-mark-below
7538                  (< (gnus-summary-article-score)
7539                     gnus-summary-mark-below))
7540             ;; This article has a low score, so we mark it as read.
7541             (when (memq article gnus-newsgroup-unreads)
7542               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7543           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7544             ;; This article was previously marked as read on account
7545             ;; of a low score, but now it has risen, so we mark it as
7546             ;; unread.
7547             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7548         (gnus-summary-update-mark
7549          (if (or (null gnus-summary-default-score)
7550                  (<= (abs (- score gnus-summary-default-score))
7551                      gnus-summary-zcore-fuzz)) ? 
7552            (if (< score gnus-summary-default-score)
7553                gnus-score-below-mark gnus-score-over-mark)) 'score))
7554       ;; Do visual highlighting.
7555       (when (gnus-visual-p 'summary-highlight 'highlight)
7556         (run-hooks 'gnus-summary-update-hook)))))
7557
7558 (defvar gnus-tmp-new-adopts nil)
7559
7560 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7561   ;; Sum up all elements (and sub-elements) in a list.
7562   (let* ((number
7563           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7564           (cond
7565            ((and (consp thread) (cdr thread))
7566             (apply
7567              '+ 1 (mapcar
7568                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7569            ((null thread)
7570             1)
7571            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7572             1)
7573            (t 1))))
7574     (when (and level (zerop level) gnus-tmp-new-adopts)
7575       (incf number
7576             (apply '+ (mapcar
7577                        'gnus-summary-number-of-articles-in-thread
7578                        gnus-tmp-new-adopts))))
7579     (if char
7580         (if (> number 1) gnus-not-empty-thread-mark
7581           gnus-empty-thread-mark)
7582       number)))
7583
7584 (defun gnus-summary-set-local-parameters (group)
7585  "Go through the local params of GROUP and set all variable specs in that list."
7586   (let ((params (gnus-info-params (gnus-get-info group)))
7587         elem)
7588     (while params
7589       (setq elem (car params)
7590             params (cdr params))
7591       (and (consp elem)                 ; Has to be a cons.
7592            (consp (cdr elem))           ; The cdr has to be a list.
7593            (symbolp (car elem))         ; Has to be a symbol in there.
7594            (not (memq (car elem) 
7595                       '(quit-config to-address to-list to-group)))
7596            (progn                       ; So we set it.
7597              (make-local-variable (car elem))
7598              (set (car elem) (eval (nth 1 elem))))))))
7599
7600 (defun gnus-summary-read-group (group &optional show-all no-article
7601                                       kill-buffer no-display)
7602   "Start reading news in newsgroup GROUP.
7603 If SHOW-ALL is non-nil, already read articles are also listed.
7604 If NO-ARTICLE is non-nil, no article is selected initially.
7605 If NO-DISPLAY, don't generate a summary buffer."
7606   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7607   (let* ((new-group (gnus-summary-setup-buffer group))
7608          (quit-config (gnus-group-quit-config group))
7609          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7610     (cond
7611      ;; This summary buffer exists already, so we just select it.
7612      ((not new-group)
7613       (gnus-set-global-variables)
7614       (when kill-buffer
7615         (gnus-kill-or-deaden-summary kill-buffer))
7616       (gnus-configure-windows 'summary 'force)
7617       (gnus-set-mode-line 'summary)
7618       (gnus-summary-position-point)
7619       (message "")
7620       t)
7621      ;; We couldn't select this group.
7622      ((null did-select)
7623       (when (and (eq major-mode 'gnus-summary-mode)
7624                  (not (equal (current-buffer) kill-buffer)))
7625         (kill-buffer (current-buffer))
7626         (if (not quit-config)
7627             (progn
7628               (set-buffer gnus-group-buffer)
7629               (gnus-group-jump-to-group group)
7630               (gnus-group-next-unread-group 1))
7631           (if (not (buffer-name (car quit-config)))
7632               (gnus-configure-windows 'group 'force)
7633             (set-buffer (car quit-config))
7634             (and (eq major-mode 'gnus-summary-mode)
7635                  (gnus-set-global-variables))
7636             (gnus-configure-windows (cdr quit-config)))))
7637       (gnus-message 3 "Can't select group")
7638       nil)
7639      ;; The user did a `C-g' while prompting for number of articles,
7640      ;; so we exit this group.
7641      ((eq did-select 'quit)
7642       (and (eq major-mode 'gnus-summary-mode)
7643            (not (equal (current-buffer) kill-buffer))
7644            (kill-buffer (current-buffer)))
7645       (when kill-buffer
7646         (gnus-kill-or-deaden-summary kill-buffer))
7647       (if (not quit-config)
7648           (progn
7649             (set-buffer gnus-group-buffer)
7650             (gnus-group-jump-to-group group)
7651             (gnus-group-next-unread-group 1)
7652             (gnus-configure-windows 'group 'force))
7653         (if (not (buffer-name (car quit-config)))
7654             (gnus-configure-windows 'group 'force)
7655           (set-buffer (car quit-config))
7656           (and (eq major-mode 'gnus-summary-mode)
7657                (gnus-set-global-variables))
7658           (gnus-configure-windows (cdr quit-config))))
7659       ;; Finally signal the quit.
7660       (signal 'quit nil))
7661      ;; The group was successfully selected.
7662      (t
7663       (gnus-set-global-variables)
7664       ;; Save the active value in effect when the group was entered.
7665       (setq gnus-newsgroup-active
7666             (gnus-copy-sequence
7667              (gnus-active gnus-newsgroup-name)))
7668       ;; You can change the summary buffer in some way with this hook.
7669       (run-hooks 'gnus-select-group-hook)
7670       ;; Set any local variables in the group parameters.
7671       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7672       (gnus-update-format-specifications)
7673       ;; Do score processing.
7674       (when gnus-use-scoring
7675         (gnus-possibly-score-headers))
7676       ;; Check whether to fill in the gaps in the threads.
7677       (when gnus-build-sparse-threads
7678         (gnus-build-sparse-threads))
7679       ;; Find the initial limit.
7680       (if gnus-show-threads
7681           (if show-all
7682               (let ((gnus-newsgroup-dormant nil))
7683                 (gnus-summary-initial-limit show-all))
7684             (gnus-summary-initial-limit show-all))
7685         (setq gnus-newsgroup-limit 
7686               (mapcar 
7687                (lambda (header) (mail-header-number header))
7688                gnus-newsgroup-headers)))
7689       ;; Generate the summary buffer.
7690       (unless no-display
7691         (gnus-summary-prepare))
7692       (when gnus-use-trees
7693         (gnus-tree-open group)
7694         (setq gnus-summary-highlight-line-function
7695               'gnus-tree-highlight-article))
7696       ;; If the summary buffer is empty, but there are some low-scored
7697       ;; articles or some excluded dormants, we include these in the
7698       ;; buffer.
7699       (when (and (zerop (buffer-size))
7700                  (not no-display))
7701         (cond (gnus-newsgroup-dormant
7702                (gnus-summary-limit-include-dormant))
7703               ((and gnus-newsgroup-scored show-all)
7704                (gnus-summary-limit-include-expunged))))
7705       ;; Function `gnus-apply-kill-file' must be called in this hook.
7706       (run-hooks 'gnus-apply-kill-hook)
7707       (if (and (zerop (buffer-size))
7708                (not no-display))
7709           (progn
7710             ;; This newsgroup is empty.
7711             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7712             (gnus-message 6 "No unread news")
7713             (when kill-buffer
7714               (gnus-kill-or-deaden-summary kill-buffer))
7715             ;; Return nil from this function.
7716             nil)
7717         ;; Hide conversation thread subtrees.  We cannot do this in
7718         ;; gnus-summary-prepare-hook since kill processing may not
7719         ;; work with hidden articles.
7720         (and gnus-show-threads
7721              gnus-thread-hide-subtree
7722              (gnus-summary-hide-all-threads))
7723         ;; Show first unread article if requested.
7724         (if (and (not no-article)
7725                  (not no-display)
7726                  gnus-newsgroup-unreads
7727                  gnus-auto-select-first)
7728             (unless (if (eq gnus-auto-select-first 'best)
7729                         (gnus-summary-best-unread-article)
7730                       (gnus-summary-first-unread-article))
7731               (gnus-configure-windows 'summary))
7732           ;; Don't select any articles, just move point to the first
7733           ;; article in the group.
7734           (goto-char (point-min))
7735           (gnus-summary-position-point)
7736           (gnus-set-mode-line 'summary)
7737           (gnus-configure-windows 'summary 'force))
7738         ;; If we are in async mode, we send some info to the backend.
7739         (when gnus-newsgroup-async
7740           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7741         (when kill-buffer
7742           (gnus-kill-or-deaden-summary kill-buffer))
7743         (when (get-buffer-window gnus-group-buffer t)
7744           ;; Gotta use windows, because recenter does wierd stuff if
7745           ;; the current buffer ain't the displayed window.
7746           (let ((owin (selected-window)))
7747             (select-window (get-buffer-window gnus-group-buffer t))
7748             (when (gnus-group-goto-group group)
7749               (recenter))
7750             (select-window owin))))
7751       ;; Mark this buffer as "prepared".
7752       (setq gnus-newsgroup-prepared t)
7753       t))))
7754
7755 (defun gnus-summary-prepare ()
7756   "Generate the summary buffer."
7757   (let ((buffer-read-only nil))
7758     (erase-buffer)
7759     (setq gnus-newsgroup-data nil
7760           gnus-newsgroup-data-reverse nil)
7761     (run-hooks 'gnus-summary-generate-hook)
7762     ;; Generate the buffer, either with threads or without.
7763     (when gnus-newsgroup-headers
7764       (gnus-summary-prepare-threads
7765        (if gnus-show-threads
7766            (gnus-sort-gathered-threads
7767             (funcall gnus-summary-thread-gathering-function
7768                      (gnus-sort-threads
7769                       (gnus-cut-threads (gnus-make-threads)))))
7770          ;; Unthreaded display.
7771          (gnus-sort-articles gnus-newsgroup-headers))))
7772     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7773     ;; Call hooks for modifying summary buffer.
7774     (goto-char (point-min))
7775     (run-hooks 'gnus-summary-prepare-hook)))
7776
7777 (defun gnus-gather-threads-by-subject (threads)
7778   "Gather threads by looking at Subject headers."
7779   (if (not gnus-summary-make-false-root)
7780       threads
7781     (let ((hashtb (gnus-make-hashtable 1023))
7782           (prev threads)
7783           (result threads)
7784           subject hthread whole-subject)
7785       (while threads
7786         (setq whole-subject (mail-header-subject (caar threads)))
7787         (setq subject
7788               (cond
7789                ;; Truncate the subject.
7790                ((numberp gnus-summary-gather-subject-limit)
7791                 (setq subject (gnus-simplify-subject-re whole-subject))
7792                 (if (> (length subject) gnus-summary-gather-subject-limit)
7793                     (substring subject 0 gnus-summary-gather-subject-limit)
7794                   subject))
7795                ;; Fuzzily simplify it.
7796                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7797                 (gnus-simplify-subject-fuzzy whole-subject))
7798                ;; Just remove the leading "Re:".
7799                (t
7800                 (gnus-simplify-subject-re whole-subject))))
7801
7802         (if (and gnus-summary-gather-exclude-subject
7803                  (string-match gnus-summary-gather-exclude-subject
7804                                subject))
7805             ()          ; We don't want to do anything with this article.
7806           ;; We simplify the subject before looking it up in the
7807           ;; hash table.
7808
7809           (if (setq hthread (gnus-gethash subject hashtb))
7810               (progn
7811                 ;; We enter a dummy root into the thread, if we
7812                 ;; haven't done that already.
7813                 (unless (stringp (caar hthread))
7814                   (setcar hthread (list whole-subject (car hthread))))
7815                 ;; We add this new gathered thread to this gathered
7816                 ;; thread.
7817                 (setcdr (car hthread)
7818                         (nconc (cdar hthread) (list (car threads))))
7819                 ;; Remove it from the list of threads.
7820                 (setcdr prev (cdr threads))
7821                 (setq threads prev))
7822             ;; Enter this thread into the hash table.
7823             (gnus-sethash subject threads hashtb)))
7824         (setq prev threads)
7825         (setq threads (cdr threads)))
7826       result)))
7827
7828 (defun gnus-gather-threads-by-references (threads)
7829   "Gather threads by looking at References headers."
7830   (let ((idhashtb (gnus-make-hashtable 1023))
7831         (thhashtb (gnus-make-hashtable 1023))
7832         (prev threads)
7833         (result threads)
7834         ids references id gthread gid entered)
7835     (while threads
7836       (when (setq references (mail-header-references (caar threads)))
7837         (setq id (mail-header-id (caar threads)))
7838         (setq ids (gnus-split-references references))
7839         (setq entered nil)
7840         (while ids
7841           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7842               (progn
7843                 (gnus-sethash (car ids) id idhashtb)
7844                 (gnus-sethash id threads thhashtb))
7845             (setq gthread (gnus-gethash gid thhashtb))
7846             (unless entered
7847               ;; We enter a dummy root into the thread, if we
7848               ;; haven't done that already.
7849               (unless (stringp (caar gthread))
7850                 (setcar gthread (list (mail-header-subject (caar gthread))
7851                                       (car gthread))))
7852               ;; We add this new gathered thread to this gathered
7853               ;; thread.
7854               (setcdr (car gthread)
7855                       (nconc (cdar gthread) (list (car threads)))))
7856             ;; Add it into the thread hash table.
7857             (gnus-sethash id gthread thhashtb)
7858             (setq entered t)
7859             ;; Remove it from the list of threads.
7860             (setcdr prev (cdr threads))
7861             (setq threads prev))
7862           (setq ids (cdr ids))))
7863       (setq prev threads)
7864       (setq threads (cdr threads)))
7865     result))
7866
7867 (defun gnus-sort-gathered-threads (threads)
7868   "Sort subtreads inside each gathered thread by article number."
7869   (let ((result threads))
7870     (while threads
7871       (when (stringp (caar threads))
7872         (setcdr (car threads)
7873                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7874       (setq threads (cdr threads)))
7875     result))
7876
7877 (defun gnus-make-threads ()
7878   "Go through the dependency hashtb and find the roots.  Return all threads."
7879   (let (threads)
7880     (mapatoms
7881      (lambda (refs)
7882        (unless (car (symbol-value refs))
7883          ;; These threads do not refer back to any other articles,
7884          ;; so they're roots.
7885          (setq threads (append (cdr (symbol-value refs)) threads))))
7886      gnus-newsgroup-dependencies)
7887     threads))
7888
7889 (defun gnus-build-sparse-threads ()
7890   (let ((headers gnus-newsgroup-headers)
7891         (deps gnus-newsgroup-dependencies)
7892         header references generation relations 
7893         cthread subject child end pthread relation)
7894     ;; First we create an alist of generations/relations, where 
7895     ;; generations is how much we trust the ralation, and the relation
7896     ;; is parent/child.
7897     (gnus-message 7 "Making sparse threads...")
7898     (save-excursion
7899       (nnheader-set-temp-buffer " *gnus sparse threads*")
7900       (while (setq header (pop headers))
7901         (when (and (setq references (mail-header-references header))
7902                    (not (string= references "")))
7903           (insert references)
7904           (setq child (mail-header-id header)
7905                 subject (mail-header-subject header))
7906           (setq generation 0)
7907           (while (search-backward ">" nil t)
7908             (setq end (1+ (point)))
7909             (when (search-backward "<" nil t)
7910               (push (list (incf generation) 
7911                           child (setq child (buffer-substring (point) end))
7912                           subject)
7913                     relations)))
7914           (push (list (1+ generation) child nil subject) relations)
7915           (erase-buffer)))
7916       (kill-buffer (current-buffer)))
7917     ;; Sort over trustworthiness.
7918     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7919     (while (setq relation (pop relations))
7920       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7921                 (unless (car (symbol-value cthread))
7922                   ;; Make this article the parent of these threads.
7923                   (setcar (symbol-value cthread)
7924                           (vector gnus-reffed-article-number 
7925                                   (cadddr relation) 
7926                                   "" ""
7927                                   (cadr relation) 
7928                                   (or (caddr relation) "") 0 0 "")))
7929               (set cthread (list (vector gnus-reffed-article-number
7930                                          (cadddr relation) 
7931                                          "" "" (cadr relation) 
7932                                          (or (caddr relation) "") 0 0 ""))))
7933         (push gnus-reffed-article-number gnus-newsgroup-limit)
7934         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7935         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7936               gnus-newsgroup-reads)
7937         (decf gnus-reffed-article-number)
7938         ;; Make this new thread the child of its parent.
7939         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7940             (setcdr (symbol-value pthread)
7941                     (nconc (cdr (symbol-value pthread))
7942                            (list (symbol-value cthread))))
7943           (set pthread (list nil (symbol-value cthread))))))
7944     (gnus-message 7 "Making sparse threads...done")))
7945
7946 (defun gnus-build-old-threads ()
7947   ;; Look at all the articles that refer back to old articles, and
7948   ;; fetch the headers for the articles that aren't there.  This will
7949   ;; build complete threads - if the roots haven't been expired by the
7950   ;; server, that is.
7951   (let (id heads)
7952     (mapatoms
7953      (lambda (refs)
7954        (when (not (car (symbol-value refs)))
7955          (setq heads (cdr (symbol-value refs)))
7956          (while heads
7957            (if (memq (mail-header-number (caar heads))
7958                      gnus-newsgroup-dormant)
7959                (setq heads (cdr heads))
7960              (setq id (symbol-name refs))
7961              (while (and (setq id (gnus-build-get-header id))
7962                          (not (car (gnus-gethash
7963                                     id gnus-newsgroup-dependencies)))))
7964              (setq heads nil)))))
7965      gnus-newsgroup-dependencies)))
7966
7967 (defun gnus-build-get-header (id)
7968   ;; Look through the buffer of NOV lines and find the header to
7969   ;; ID.  Enter this line into the dependencies hash table, and return
7970   ;; the id of the parent article (if any).
7971   (let ((deps gnus-newsgroup-dependencies)
7972         found header)
7973     (prog1
7974         (save-excursion
7975           (set-buffer nntp-server-buffer)
7976           (goto-char (point-min))
7977           (while (and (not found) (search-forward id nil t))
7978             (beginning-of-line)
7979             (setq found (looking-at
7980                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7981                                  (regexp-quote id))))
7982             (or found (beginning-of-line 2)))
7983           (when found
7984             (beginning-of-line)
7985             (and
7986              (setq header (gnus-nov-parse-line
7987                            (read (current-buffer)) deps))
7988              (gnus-parent-id (mail-header-references header)))))
7989       (when header
7990         (let ((number (mail-header-number header)))
7991           (push number gnus-newsgroup-limit)
7992           (push header gnus-newsgroup-headers)
7993           (if (memq number gnus-newsgroup-unselected)
7994               (progn
7995                 (push number gnus-newsgroup-unreads)
7996                 (setq gnus-newsgroup-unselected
7997                       (delq number gnus-newsgroup-unselected)))
7998             (push number gnus-newsgroup-ancient)))))))
7999
8000 (defun gnus-summary-update-article (article &optional header)
8001   "Update ARTICLE in the summary buffer."
8002   (set-buffer gnus-summary-buffer)
8003   (let* ((header (or header (gnus-summary-article-header article)))
8004          (id (mail-header-id header))
8005          (data (gnus-data-find article))
8006          (thread (gnus-id-to-thread id))
8007          (parent
8008           (gnus-id-to-thread (or (gnus-parent-id 
8009                                   (mail-header-references header))
8010                                  "tull")))
8011          (buffer-read-only nil)
8012          (old (car thread))
8013          (number (mail-header-number header))
8014          pos)
8015     (when thread
8016       (setcar thread nil)
8017       (when parent
8018         (delq thread parent))
8019       (if (gnus-summary-insert-subject id header)
8020           ;; Set the (possibly) new article number in the data structure.
8021           (gnus-data-set-number data (gnus-id-to-article id))
8022         (setcar thread old)
8023         nil))))
8024
8025 (defun gnus-rebuild-thread (id)
8026   "Rebuild the thread containing ID."
8027   (let ((buffer-read-only nil)
8028         current thread data)
8029     (if (not gnus-show-threads)
8030         (setq thread (list (car (gnus-id-to-thread id))))
8031       ;; Get the thread this article is part of.
8032       (setq thread (gnus-remove-thread id)))
8033     (setq current (save-excursion
8034                     (and (zerop (forward-line -1))
8035                          (gnus-summary-article-number))))
8036     ;; If this is a gathered thread, we have to go some re-gathering.
8037     (when (stringp (car thread))
8038       (let ((subject (car thread))
8039             roots thr)
8040         (setq thread (cdr thread))
8041         (while thread
8042           (unless (memq (setq thr (gnus-id-to-thread
8043                                       (gnus-root-id
8044                                        (mail-header-id (caar thread)))))
8045                         roots)
8046             (push thr roots))
8047           (setq thread (cdr thread)))
8048         ;; We now have all (unique) roots.
8049         (if (= (length roots) 1)
8050             ;; All the loose roots are now one solid root.
8051             (setq thread (car roots))
8052           (setq thread (cons subject (gnus-sort-threads roots))))))
8053     (let (threads)
8054       ;; We then insert this thread into the summary buffer.
8055       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8056         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8057         (setq data (nreverse gnus-newsgroup-data))
8058         (setq threads gnus-newsgroup-threads))
8059       ;; We splice the new data into the data structure.
8060       (gnus-data-enter-list current data)
8061       (gnus-data-compute-positions)
8062       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8063
8064 (defun gnus-id-to-thread (id)
8065   "Return the (sub-)thread where ID appears."
8066   (gnus-gethash id gnus-newsgroup-dependencies))
8067
8068 (defun gnus-id-to-article (id)
8069   "Return the article number of ID."
8070   (let ((thread (gnus-id-to-thread id)))
8071     (when (and thread
8072                (car thread))
8073       (mail-header-number (car thread)))))
8074
8075 (defun gnus-id-to-header (id)
8076   "Return the article headers of ID."
8077   (car (gnus-id-to-thread id)))
8078
8079 (defun gnus-article-displayed-root-p (article)
8080   "Say whether ARTICLE is a root(ish) article."
8081   (let ((level (gnus-summary-thread-level article))
8082         (refs (mail-header-references  (gnus-summary-article-header article)))
8083         particle)
8084     (cond 
8085      ((null level) nil)
8086      ((zerop level) t)
8087      ((null refs) t)
8088      ((null (gnus-parent-id refs)) t)
8089      ((and (= 1 level)
8090            (null (setq particle (gnus-id-to-article
8091                                  (gnus-parent-id refs))))
8092            (null (gnus-summary-thread-level particle)))))))
8093
8094 (defun gnus-root-id (id)
8095   "Return the id of the root of the thread where ID appears."
8096   (let (last-id prev)
8097     (while (and id (setq prev (car (gnus-gethash 
8098                                     id gnus-newsgroup-dependencies))))
8099       (setq last-id id
8100             id (gnus-parent-id (mail-header-references prev))))
8101     last-id))
8102
8103 (defun gnus-remove-thread (id &optional dont-remove)
8104   "Remove the thread that has ID in it."
8105   (let ((dep gnus-newsgroup-dependencies)
8106         headers thread last-id)
8107     ;; First go up in this thread until we find the root.
8108     (setq last-id (gnus-root-id id))
8109     (setq headers (list (car (gnus-id-to-thread last-id))
8110                         (caadr (gnus-id-to-thread last-id))))
8111     ;; We have now found the real root of this thread.  It might have
8112     ;; been gathered into some loose thread, so we have to search
8113     ;; through the threads to find the thread we wanted.
8114     (let ((threads gnus-newsgroup-threads)
8115           sub)
8116       (while threads
8117         (setq sub (car threads))
8118         (if (stringp (car sub))
8119             ;; This is a gathered threads, so we look at the roots
8120             ;; below it to find whether this article in in this
8121             ;; gathered root.
8122             (progn
8123               (setq sub (cdr sub))
8124               (while sub
8125                 (when (member (caar sub) headers)
8126                   (setq thread (car threads)
8127                         threads nil
8128                         sub nil))
8129                 (setq sub (cdr sub))))
8130           ;; It's an ordinary thread, so we check it.
8131           (when (eq (car sub) (car headers))
8132             (setq thread sub
8133                   threads nil)))
8134         (setq threads (cdr threads)))
8135       ;; If this article is in no thread, then it's a root.
8136       (if thread
8137           (unless dont-remove
8138             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8139         (setq thread (gnus-gethash last-id dep)))
8140       (when thread
8141         (prog1
8142             thread ; We return this thread.
8143           (unless dont-remove
8144             (if (stringp (car thread))
8145                 (progn
8146                   ;; If we use dummy roots, then we have to remove the
8147                   ;; dummy root as well.
8148                   (when (eq gnus-summary-make-false-root 'dummy)
8149                     ;; Uhm.
8150                     )
8151                   (setq thread (cdr thread))
8152                   (while thread
8153                     (gnus-remove-thread-1 (car thread))
8154                     (setq thread (cdr thread))))
8155               (gnus-remove-thread-1 thread))))))))
8156
8157 (defun gnus-remove-thread-1 (thread)
8158   "Remove the thread THREAD recursively."
8159   (let ((number (mail-header-number (car thread)))
8160         pos)
8161     (when (setq pos (text-property-any
8162                      (point-min) (point-max) 'gnus-number number))
8163       (goto-char pos)
8164       (gnus-delete-line)
8165       (gnus-data-remove number))
8166     (setq thread (cdr thread))
8167     (while thread
8168       (gnus-remove-thread-1 (pop thread)))))
8169
8170 (defun gnus-sort-threads (threads)
8171   "Sort THREADS."
8172   (if (not gnus-thread-sort-functions)
8173       threads
8174     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8175                     (car gnus-thread-sort-functions)
8176                   `(lambda (t1 t2)
8177                      ,(gnus-make-sort-function 
8178                        (reverse gnus-thread-sort-functions))))))
8179       (gnus-message 7 "Sorting threads...")
8180       (prog1
8181           (sort threads func)
8182         (gnus-message 7 "Sorting threads...done")))))
8183
8184 (defun gnus-sort-articles (articles)
8185   "Sort ARTICLES."
8186   (when gnus-article-sort-functions
8187     (let ((func (if (= 1 (length gnus-article-sort-functions))
8188                     (car gnus-article-sort-functions)
8189                   `(lambda (t1 t2)
8190                      ,(gnus-make-sort-function 
8191                        (reverse gnus-article-sort-functions))))))
8192       (gnus-message 7 "Sorting articles...")
8193       (prog1
8194           (setq gnus-newsgroup-headers (sort articles func))
8195         (gnus-message 7 "Sorting articles...done")))))
8196
8197 (defun gnus-make-sort-function (funs)
8198   "Return a composite sort condition based on the functions in FUNC."
8199   (if (cdr funs)
8200       `(or (,(car funs) t1 t2)
8201            (and (not (,(car funs) t2 t1))
8202                 ,(gnus-make-sort-function (cdr funs))))
8203     `(,(car funs) t1 t2)))
8204                  
8205 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8206 (defmacro gnus-thread-header (thread)
8207   ;; Return header of first article in THREAD.
8208   ;; Note that THREAD must never, ever be anything else than a variable -
8209   ;; using some other form will lead to serious barfage.
8210   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8211   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8212   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8213         (vector thread) 2))
8214
8215 (defsubst gnus-article-sort-by-number (h1 h2)
8216   "Sort articles by article number."
8217   (< (mail-header-number h1)
8218      (mail-header-number h2)))
8219
8220 (defun gnus-thread-sort-by-number (h1 h2)
8221   "Sort threads by root article number."
8222   (gnus-article-sort-by-number
8223    (gnus-thread-header h1) (gnus-thread-header h2)))
8224
8225 (defsubst gnus-article-sort-by-author (h1 h2)
8226   "Sort articles by root author."
8227   (string-lessp
8228    (let ((extract (funcall
8229                    gnus-extract-address-components
8230                    (mail-header-from h1))))
8231      (or (car extract) (cdr extract)))
8232    (let ((extract (funcall
8233                    gnus-extract-address-components
8234                    (mail-header-from h2))))
8235      (or (car extract) (cdr extract)))))
8236
8237 (defun gnus-thread-sort-by-author (h1 h2)
8238   "Sort threads by root author."
8239   (gnus-article-sort-by-author
8240    (gnus-thread-header h1)  (gnus-thread-header h2)))
8241
8242 (defsubst gnus-article-sort-by-subject (h1 h2)
8243   "Sort articles by root subject."
8244   (string-lessp
8245    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8246    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8247
8248 (defun gnus-thread-sort-by-subject (h1 h2)
8249   "Sort threads by root subject."
8250   (gnus-article-sort-by-subject
8251    (gnus-thread-header h1) (gnus-thread-header h2)))
8252
8253 (defsubst gnus-article-sort-by-date (h1 h2)
8254   "Sort articles by root article date."
8255   (string-lessp
8256    (gnus-sortable-date (mail-header-date h1))
8257    (gnus-sortable-date (mail-header-date h2))))
8258
8259 (defun gnus-thread-sort-by-date (h1 h2)
8260   "Sort threads by root article date."
8261   (gnus-article-sort-by-date
8262    (gnus-thread-header h1) (gnus-thread-header h2)))
8263
8264 (defsubst gnus-article-sort-by-score (h1 h2)
8265   "Sort articles by root article score.
8266 Unscored articles will be counted as having a score of zero."
8267   (> (or (cdr (assq (mail-header-number h1)
8268                     gnus-newsgroup-scored))
8269          gnus-summary-default-score 0)
8270      (or (cdr (assq (mail-header-number h2)
8271                     gnus-newsgroup-scored))
8272          gnus-summary-default-score 0)))
8273
8274 (defun gnus-thread-sort-by-score (h1 h2)
8275   "Sort threads by root article score."
8276   (gnus-article-sort-by-score
8277    (gnus-thread-header h1) (gnus-thread-header h2)))
8278
8279 (defun gnus-thread-sort-by-total-score (h1 h2)
8280   "Sort threads by the sum of all scores in the thread.
8281 Unscored articles will be counted as having a score of zero."
8282   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8283
8284 (defun gnus-thread-total-score (thread)
8285   ;;  This function find the total score of THREAD.
8286   (if (consp thread)
8287       (if (stringp (car thread))
8288           (apply gnus-thread-score-function 0
8289                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8290         (gnus-thread-total-score-1 thread))
8291     (gnus-thread-total-score-1 (list thread))))
8292
8293 (defun gnus-thread-total-score-1 (root)
8294   ;; This function find the total score of the thread below ROOT.
8295   (setq root (car root))
8296   (apply gnus-thread-score-function
8297          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8298              gnus-summary-default-score 0)
8299          (mapcar 'gnus-thread-total-score
8300                  (cdr (gnus-gethash (mail-header-id root)
8301                                     gnus-newsgroup-dependencies)))))
8302
8303 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8304 (defvar gnus-tmp-prev-subject nil)
8305 (defvar gnus-tmp-false-parent nil)
8306 (defvar gnus-tmp-root-expunged nil)
8307 (defvar gnus-tmp-dummy-line nil)
8308
8309 (defun gnus-summary-prepare-threads (threads)
8310   "Prepare summary buffer from THREADS and indentation LEVEL.
8311 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8312 or a straight list of headers."
8313   (gnus-message 7 "Generating summary...")
8314
8315   (setq gnus-newsgroup-threads threads)
8316   (beginning-of-line)
8317
8318   (let ((gnus-tmp-level 0)
8319         (default-score (or gnus-summary-default-score 0))
8320         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8321         thread number subject stack state gnus-tmp-gathered beg-match
8322         new-roots gnus-tmp-new-adopts thread-end
8323         gnus-tmp-header gnus-tmp-unread
8324         gnus-tmp-replied gnus-tmp-subject-or-nil
8325         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8326         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8327         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8328
8329     (setq gnus-tmp-prev-subject nil)
8330
8331     (if (vectorp (car threads))
8332         ;; If this is a straight (sic) list of headers, then a
8333         ;; threaded summary display isn't required, so we just create
8334         ;; an unthreaded one.
8335         (gnus-summary-prepare-unthreaded threads)
8336
8337       ;; Do the threaded display.
8338
8339       (while (or threads stack gnus-tmp-new-adopts new-roots)
8340
8341         (if (and (= gnus-tmp-level 0)
8342                  (not (setq gnus-tmp-dummy-line nil))
8343                  (or (not stack)
8344                      (= (caar stack) 0))
8345                  (not gnus-tmp-false-parent)
8346                  (or gnus-tmp-new-adopts new-roots))
8347             (if gnus-tmp-new-adopts
8348                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8349                       thread (list (car gnus-tmp-new-adopts))
8350                       gnus-tmp-header (caar thread)
8351                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8352               (if new-roots
8353                   (setq thread (list (car new-roots))
8354                         gnus-tmp-header (caar thread)
8355                         new-roots (cdr new-roots))))
8356
8357           (if threads
8358               ;; If there are some threads, we do them before the
8359               ;; threads on the stack.
8360               (setq thread threads
8361                     gnus-tmp-header (caar thread))
8362             ;; There were no current threads, so we pop something off
8363             ;; the stack.
8364             (setq state (car stack)
8365                   gnus-tmp-level (car state)
8366                   thread (cdr state)
8367                   stack (cdr stack)
8368                   gnus-tmp-header (caar thread))))
8369
8370         (setq gnus-tmp-false-parent nil)
8371         (setq gnus-tmp-root-expunged nil)
8372         (setq thread-end nil)
8373
8374         (if (stringp gnus-tmp-header)
8375             ;; The header is a dummy root.
8376             (cond
8377              ((eq gnus-summary-make-false-root 'adopt)
8378               ;; We let the first article adopt the rest.
8379               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8380                                                (cddar thread)))
8381               (setq gnus-tmp-gathered
8382                     (nconc (mapcar
8383                             (lambda (h) (mail-header-number (car h)))
8384                             (cddar thread))
8385                            gnus-tmp-gathered))
8386               (setq thread (cons (list (caar thread)
8387                                        (cadar thread))
8388                                  (cdr thread)))
8389               (setq gnus-tmp-level -1
8390                     gnus-tmp-false-parent t))
8391              ((eq gnus-summary-make-false-root 'empty)
8392               ;; We print adopted articles with empty subject fields.
8393               (setq gnus-tmp-gathered
8394                     (nconc (mapcar
8395                             (lambda (h) (mail-header-number (car h)))
8396                             (cddar thread))
8397                            gnus-tmp-gathered))
8398               (setq gnus-tmp-level -1))
8399              ((eq gnus-summary-make-false-root 'dummy)
8400               ;; We remember that we probably want to output a dummy
8401               ;; root.
8402               (setq gnus-tmp-dummy-line gnus-tmp-header)
8403               (setq gnus-tmp-prev-subject gnus-tmp-header))
8404              (t
8405               ;; We do not make a root for the gathered
8406               ;; sub-threads at all.
8407               (setq gnus-tmp-level -1)))
8408
8409           (setq number (mail-header-number gnus-tmp-header)
8410                 subject (mail-header-subject gnus-tmp-header))
8411
8412           (cond
8413            ;; If the thread has changed subject, we might want to make
8414            ;; this subthread into a root.
8415            ((and (null gnus-thread-ignore-subject)
8416                  (not (zerop gnus-tmp-level))
8417                  gnus-tmp-prev-subject
8418                  (not (inline
8419                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8420             (setq new-roots (nconc new-roots (list (car thread)))
8421                   thread-end t
8422                   gnus-tmp-header nil))
8423            ;; If the article lies outside the current limit,
8424            ;; then we do not display it.
8425            ((and (not (memq number gnus-newsgroup-limit))
8426                  (not gnus-tmp-dummy-line))
8427             (setq gnus-tmp-gathered
8428                   (nconc (mapcar
8429                           (lambda (h) (mail-header-number (car h)))
8430                           (cdar thread))
8431                          gnus-tmp-gathered))
8432             (setq gnus-tmp-new-adopts (if (cdar thread)
8433                                           (append gnus-tmp-new-adopts
8434                                                   (cdar thread))
8435                                         gnus-tmp-new-adopts)
8436                   thread-end t
8437                   gnus-tmp-header nil)
8438             (when (zerop gnus-tmp-level)
8439               (setq gnus-tmp-root-expunged t)))
8440            ;; Perhaps this article is to be marked as read?
8441            ((and gnus-summary-mark-below
8442                  (< (or (cdr (assq number gnus-newsgroup-scored))
8443                         default-score)
8444                     gnus-summary-mark-below)
8445                  ;; Don't touch sparse articles.
8446                  (not (memq number gnus-newsgroup-sparse)))
8447             (setq gnus-newsgroup-unreads
8448                   (delq number gnus-newsgroup-unreads))
8449             (if gnus-newsgroup-auto-expire
8450                 (push number gnus-newsgroup-expirable)
8451               (push (cons number gnus-low-score-mark)
8452                     gnus-newsgroup-reads))))
8453
8454           (when gnus-tmp-header
8455             ;; We may have an old dummy line to output before this
8456             ;; article.
8457             (when gnus-tmp-dummy-line
8458               (gnus-summary-insert-dummy-line
8459                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8460               (setq gnus-tmp-dummy-line nil))
8461
8462             ;; Compute the mark.
8463             (setq
8464              gnus-tmp-unread
8465              (cond
8466               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8467               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8468               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8469               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8470               (t (or (cdr (assq number gnus-newsgroup-reads))
8471                      gnus-ancient-mark))))
8472
8473             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8474                                   gnus-tmp-header gnus-tmp-level)
8475                   gnus-newsgroup-data)
8476
8477             ;; Actually insert the line.
8478             (setq
8479              gnus-tmp-subject-or-nil
8480              (cond
8481               ((and gnus-thread-ignore-subject
8482                     gnus-tmp-prev-subject
8483                     (not (inline (gnus-subject-equal
8484                                   gnus-tmp-prev-subject subject))))
8485                subject)
8486               ((zerop gnus-tmp-level)
8487                (if (and (eq gnus-summary-make-false-root 'empty)
8488                         (memq number gnus-tmp-gathered)
8489                         gnus-tmp-prev-subject
8490                         (inline (gnus-subject-equal
8491                                  gnus-tmp-prev-subject subject)))
8492                    gnus-summary-same-subject
8493                  subject))
8494               (t gnus-summary-same-subject)))
8495             (if (and (eq gnus-summary-make-false-root 'adopt)
8496                      (= gnus-tmp-level 1)
8497                      (memq number gnus-tmp-gathered))
8498                 (setq gnus-tmp-opening-bracket ?\<
8499                       gnus-tmp-closing-bracket ?\>)
8500               (setq gnus-tmp-opening-bracket ?\[
8501                     gnus-tmp-closing-bracket ?\]))
8502             (setq
8503              gnus-tmp-indentation
8504              (aref gnus-thread-indent-array gnus-tmp-level)
8505              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8506              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8507                                 gnus-summary-default-score 0)
8508              gnus-tmp-score-char
8509              (if (or (null gnus-summary-default-score)
8510                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8511                          gnus-summary-zcore-fuzz)) ? 
8512                (if (< gnus-tmp-score gnus-summary-default-score)
8513                    gnus-score-below-mark gnus-score-over-mark))
8514              gnus-tmp-replied
8515              (cond ((memq number gnus-newsgroup-processable)
8516                     gnus-process-mark)
8517                    ((memq number gnus-newsgroup-cached)
8518                     gnus-cached-mark)
8519                    ((memq number gnus-newsgroup-replied)
8520                     gnus-replied-mark)
8521                    (t gnus-unread-mark))
8522              gnus-tmp-from (mail-header-from gnus-tmp-header)
8523              gnus-tmp-name
8524              (cond
8525               ((string-match "(.+)" gnus-tmp-from)
8526                (substring gnus-tmp-from
8527                           (1+ (match-beginning 0)) (1- (match-end 0))))
8528               ((string-match "<[^>]+> *$" gnus-tmp-from)
8529                (setq beg-match (match-beginning 0))
8530                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8531                         (substring gnus-tmp-from (1+ (match-beginning 0))
8532                                    (1- (match-end 0))))
8533                    (substring gnus-tmp-from 0 beg-match)))
8534               (t gnus-tmp-from)))
8535             (when (string= gnus-tmp-name "")
8536               (setq gnus-tmp-name gnus-tmp-from))
8537             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8538             (gnus-put-text-property
8539              (point)
8540              (progn (eval gnus-summary-line-format-spec) (point))
8541              'gnus-number number)
8542             (when gnus-visual-p
8543               (forward-line -1)
8544               (run-hooks 'gnus-summary-update-hook)
8545               (forward-line 1))
8546
8547             (setq gnus-tmp-prev-subject subject)))
8548
8549         (when (nth 1 thread)
8550           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8551         (incf gnus-tmp-level)
8552         (setq threads (if thread-end nil (cdar thread)))
8553         (unless threads
8554           (setq gnus-tmp-level 0)))))
8555   (gnus-message 7 "Generating summary...done"))
8556
8557 (defun gnus-summary-prepare-unthreaded (headers)
8558   "Generate an unthreaded summary buffer based on HEADERS."
8559   (let (header number mark)
8560
8561     (while headers
8562       ;; We may have to root out some bad articles...
8563       (when (memq (setq number (mail-header-number
8564                                 (setq header (pop headers))))
8565                   gnus-newsgroup-limit)
8566         ;; Mark article as read when it has a low score.
8567         (when (and gnus-summary-mark-below
8568                    (< (or (cdr (assq number gnus-newsgroup-scored))
8569                           gnus-summary-default-score 0)
8570                       gnus-summary-mark-below))
8571           (setq gnus-newsgroup-unreads
8572                 (delq number gnus-newsgroup-unreads))
8573           (if gnus-newsgroup-auto-expire
8574               (push number gnus-newsgroup-expirable)
8575             (push (cons number gnus-low-score-mark)
8576                   gnus-newsgroup-reads)))
8577
8578         (setq mark
8579               (cond
8580                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8581                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8582                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8583                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8584                (t (or (cdr (assq number gnus-newsgroup-reads))
8585                       gnus-ancient-mark))))
8586         (setq gnus-newsgroup-data
8587               (cons (gnus-data-make number mark (1+ (point)) header 0)
8588                     gnus-newsgroup-data))
8589         (gnus-summary-insert-line
8590          header 0 nil mark (memq number gnus-newsgroup-replied)
8591          (memq number gnus-newsgroup-expirable)
8592          (mail-header-subject header) nil
8593          (cdr (assq number gnus-newsgroup-scored))
8594          (memq number gnus-newsgroup-processable))))))
8595
8596 (defun gnus-select-newsgroup (group &optional read-all)
8597   "Select newsgroup GROUP.
8598 If READ-ALL is non-nil, all articles in the group are selected."
8599   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8600          (info (nth 2 entry))
8601          articles fetched-articles cached)
8602
8603     (or (gnus-check-server
8604          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8605         (error "Couldn't open server"))
8606
8607     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8608         (gnus-activate-group group)     ; Or we can activate it...
8609         (progn                          ; Or we bug out.
8610           (when (equal major-mode 'gnus-summary-mode)
8611             (kill-buffer (current-buffer)))
8612           (error "Couldn't request group %s: %s"
8613                  group (gnus-status-message group))))
8614
8615     (unless (gnus-request-group group t)
8616       (when (equal major-mode 'gnus-summary-mode)
8617         (kill-buffer (current-buffer)))
8618       (error "Couldn't request group %s: %s"
8619              group (gnus-status-message group)))      
8620
8621     (setq gnus-newsgroup-name group)
8622     (setq gnus-newsgroup-unselected nil)
8623     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8624
8625     (and gnus-asynchronous
8626          (gnus-check-backend-function
8627           'request-asynchronous gnus-newsgroup-name)
8628          (setq gnus-newsgroup-async
8629                (gnus-request-asynchronous gnus-newsgroup-name)))
8630
8631     ;; Adjust and set lists of article marks.
8632     (when info
8633       (gnus-adjust-marked-articles info))
8634
8635     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8636     (when (gnus-virtual-group-p group)
8637       (setq cached gnus-newsgroup-cached))
8638
8639     (setq gnus-newsgroup-unreads
8640           (gnus-set-difference
8641            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8642            gnus-newsgroup-dormant))
8643
8644     (setq gnus-newsgroup-processable nil)
8645
8646     (setq articles (gnus-articles-to-read group read-all))
8647
8648     (cond
8649      ((null articles)
8650       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8651       'quit)
8652      ((eq articles 0) nil)
8653      (t
8654       ;; Init the dependencies hash table.
8655       (setq gnus-newsgroup-dependencies
8656             (gnus-make-hashtable (length articles)))
8657       ;; Retrieve the headers and read them in.
8658       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8659       (setq gnus-newsgroup-headers
8660             (if (eq 'nov
8661                     (setq gnus-headers-retrieved-by
8662                           (gnus-retrieve-headers
8663                            articles gnus-newsgroup-name
8664                            ;; We might want to fetch old headers, but
8665                            ;; not if there is only 1 article.
8666                            (and gnus-fetch-old-headers
8667                                 (or (and
8668                                      (not (eq gnus-fetch-old-headers 'some))
8669                                      (not (numberp gnus-fetch-old-headers)))
8670                                     (> (length articles) 1))))))
8671                 (gnus-get-newsgroup-headers-xover articles)
8672               (gnus-get-newsgroup-headers)))
8673       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8674
8675       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8676       (when cached
8677         (setq gnus-newsgroup-cached cached))
8678
8679       ;; Set the initial limit.
8680       (setq gnus-newsgroup-limit (copy-sequence articles))
8681       ;; Remove canceled articles from the list of unread articles.
8682       (setq gnus-newsgroup-unreads
8683             (gnus-set-sorted-intersection
8684              gnus-newsgroup-unreads
8685              (setq fetched-articles
8686                    (mapcar (lambda (headers) (mail-header-number headers))
8687                            gnus-newsgroup-headers))))
8688       ;; Removed marked articles that do not exist.
8689       (gnus-update-missing-marks
8690        (gnus-sorted-complement fetched-articles articles))
8691       ;; We might want to build some more threads first.
8692       (and gnus-fetch-old-headers
8693            (eq gnus-headers-retrieved-by 'nov)
8694            (gnus-build-old-threads))
8695       ;; Check whether auto-expire is to be done in this group.
8696       (setq gnus-newsgroup-auto-expire
8697             (gnus-group-auto-expirable-p group))
8698       ;; Set up the article buffer now, if necessary.
8699       (unless gnus-single-article-buffer
8700         (gnus-article-setup-buffer))
8701       ;; First and last article in this newsgroup.
8702       (and gnus-newsgroup-headers
8703            (setq gnus-newsgroup-begin
8704                  (mail-header-number (car gnus-newsgroup-headers)))
8705            (setq gnus-newsgroup-end
8706                  (mail-header-number
8707                   (gnus-last-element gnus-newsgroup-headers))))
8708       (setq gnus-reffed-article-number -1)
8709       ;; GROUP is successfully selected.
8710       (or gnus-newsgroup-headers t)))))
8711
8712 (defun gnus-articles-to-read (group read-all)
8713   ;; Find out what articles the user wants to read.
8714   (let* ((articles
8715           ;; Select all articles if `read-all' is non-nil, or if there
8716           ;; are no unread articles.
8717           (if (or read-all
8718                   (and (zerop (length gnus-newsgroup-marked))
8719                        (zerop (length gnus-newsgroup-unreads))))
8720               (gnus-uncompress-range (gnus-active group))
8721             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8722                           (copy-sequence gnus-newsgroup-unreads))
8723                   '<)))
8724          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8725          (scored (length scored-list))
8726          (number (length articles))
8727          (marked (+ (length gnus-newsgroup-marked)
8728                     (length gnus-newsgroup-dormant)))
8729          (select
8730           (cond
8731            ((numberp read-all)
8732             read-all)
8733            (t
8734             (condition-case ()
8735                 (cond
8736                  ((and (or (<= scored marked) (= scored number))
8737                        (numberp gnus-large-newsgroup)
8738                        (> number gnus-large-newsgroup))
8739                   (let ((input
8740                          (read-string
8741                           (format
8742                            "How many articles from %s (default %d): "
8743                            gnus-newsgroup-name number))))
8744                     (if (string-match "^[ \t]*$" input) number input)))
8745                  ((and (> scored marked) (< scored number))
8746                   (let ((input
8747                          (read-string
8748                           (format "%s %s (%d scored, %d total): "
8749                                   "How many articles from"
8750                                   group scored number))))
8751                     (if (string-match "^[ \t]*$" input)
8752                         number input)))
8753                  (t number))
8754               (quit nil))))))
8755     (setq select (if (stringp select) (string-to-number select) select))
8756     (if (or (null select) (zerop select))
8757         select
8758       (if (and (not (zerop scored)) (<= (abs select) scored))
8759           (progn
8760             (setq articles (sort scored-list '<))
8761             (setq number (length articles)))
8762         (setq articles (copy-sequence articles)))
8763
8764       (if (< (abs select) number)
8765           (if (< select 0)
8766               ;; Select the N oldest articles.
8767               (setcdr (nthcdr (1- (abs select)) articles) nil)
8768             ;; Select the N most recent articles.
8769             (setq articles (nthcdr (- number select) articles))))
8770       (setq gnus-newsgroup-unselected
8771             (gnus-sorted-intersection
8772              gnus-newsgroup-unreads
8773              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8774       articles)))
8775
8776 (defun gnus-killed-articles (killed articles)
8777   (let (out)
8778     (while articles
8779       (if (inline (gnus-member-of-range (car articles) killed))
8780           (setq out (cons (car articles) out)))
8781       (setq articles (cdr articles)))
8782     out))
8783
8784 (defun gnus-uncompress-marks (marks)
8785   "Uncompress the mark ranges in MARKS."
8786   (let ((uncompressed '(score bookmark))
8787         out)
8788     (while marks
8789       (if (memq (caar marks) uncompressed)
8790           (push (car marks) out)
8791         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8792       (setq marks (cdr marks)))
8793     out))
8794
8795 (defun gnus-adjust-marked-articles (info)
8796   "Set all article lists and remove all marks that are no longer legal."
8797   (let* ((marked-lists (gnus-info-marks info))
8798          (active (gnus-active (gnus-info-group info)))
8799          (min (car active))
8800          (max (cdr active))
8801          (types gnus-article-mark-lists)
8802          (uncompressed '(score bookmark))
8803          marks var articles article mark)
8804
8805     (while marked-lists
8806       (setq marks (pop marked-lists))
8807       (set (setq var (intern (format "gnus-newsgroup-%s"
8808                                      (car (rassq (setq mark (car marks))
8809                                                  types)))))
8810            (if (memq (car marks) uncompressed) (cdr marks)
8811              (gnus-uncompress-range (cdr marks))))
8812
8813       (setq articles (symbol-value var))
8814
8815       ;; All articles have to be subsets of the active articles.
8816       (cond
8817        ;; Adjust "simple" lists.
8818        ((memq mark '(tick dormant expirable reply killed save))
8819         (while articles
8820           (when (or (< (setq article (pop articles)) min) (> article max))
8821             (set var (delq article (symbol-value var))))))
8822        ;; Adjust assocs.
8823        ((memq mark '(score bookmark))
8824         (while articles
8825           (when (or (< (car (setq article (pop articles))) min)
8826                     (> (car article) max))
8827             (set var (delq article (symbol-value var))))))))))
8828
8829 (defun gnus-update-missing-marks (missing)
8830   "Go through the list of MISSING articles and remove them mark lists."
8831   (when missing
8832     (let ((types gnus-article-mark-lists)
8833           var m)
8834       ;; Go through all types.
8835       (while types
8836         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8837         (when (symbol-value var)
8838           ;; This list has articles.  So we delete all missing articles
8839           ;; from it.
8840           (setq m missing)
8841           (while m
8842             (set var (delq (pop m) (symbol-value var)))))))))
8843
8844 (defun gnus-update-marks ()
8845   "Enter the various lists of marked articles into the newsgroup info list."
8846   (let ((types gnus-article-mark-lists)
8847         (info (gnus-get-info gnus-newsgroup-name))
8848         (uncompressed '(score bookmark killed))
8849         type list newmarked symbol)
8850     (when info
8851       ;; Add all marks lists that are non-nil to the list of marks lists.
8852       (while types
8853         (setq type (pop types))
8854         (when (setq list (symbol-value
8855                           (setq symbol
8856                                 (intern (format "gnus-newsgroup-%s"
8857                                                 (car type))))))
8858           (push (cons (cdr type)
8859                       (if (memq (cdr type) uncompressed) list
8860                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8861                 newmarked)))
8862
8863       ;; Enter these new marks into the info of the group.
8864       (if (nthcdr 3 info)
8865           (setcar (nthcdr 3 info) newmarked)
8866         ;; Add the marks lists to the end of the info.
8867         (when newmarked
8868           (setcdr (nthcdr 2 info) (list newmarked))))
8869
8870       ;; Cut off the end of the info if there's nothing else there.
8871       (let ((i 5))
8872         (while (and (> i 2)
8873                     (not (nth i info)))
8874           (when (nthcdr (decf i) info)
8875             (setcdr (nthcdr i info) nil)))))))
8876
8877 (defun gnus-add-marked-articles (group type articles &optional info force)
8878   ;; Add ARTICLES of TYPE to the info of GROUP.
8879   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8880   ;; add, but replace marked articles of TYPE with ARTICLES.
8881   (let ((info (or info (gnus-get-info group)))
8882         (uncompressed '(score bookmark killed))
8883         marked m)
8884     (or (not info)
8885         (and (not (setq marked (nthcdr 3 info)))
8886              (or (null articles)
8887                  (setcdr (nthcdr 2 info)
8888                          (list (list (cons type (gnus-compress-sequence
8889                                                  articles t)))))))
8890         (and (not (setq m (assq type (car marked))))
8891              (or (null articles)
8892                  (setcar marked
8893                          (cons (cons type (gnus-compress-sequence articles t) )
8894                                (car marked)))))
8895         (if force
8896             (if (null articles)
8897                 (setcar (nthcdr 3 info)
8898                         (delq (assq type (car marked)) (car marked)))
8899               (setcdr m (gnus-compress-sequence articles t)))
8900           (setcdr m (gnus-compress-sequence
8901                      (sort (nconc (gnus-uncompress-range (cdr m))
8902                                   (copy-sequence articles)) '<) t))))))
8903
8904 (defun gnus-set-mode-line (where)
8905   "This function sets the mode line of the article or summary buffers.
8906 If WHERE is `summary', the summary mode line format will be used."
8907   ;; Is this mode line one we keep updated?
8908   (when (memq where gnus-updated-mode-lines)
8909     (let (mode-string)
8910       (save-excursion
8911         ;; We evaluate this in the summary buffer since these
8912         ;; variables are buffer-local to that buffer.
8913         (set-buffer gnus-summary-buffer)
8914         ;; We bind all these variables that are used in the `eval' form
8915         ;; below.
8916         (let* ((mformat (symbol-value
8917                          (intern
8918                           (format "gnus-%s-mode-line-format-spec" where))))
8919                (gnus-tmp-group-name gnus-newsgroup-name)
8920                (gnus-tmp-article-number (or gnus-current-article 0))
8921                (gnus-tmp-unread gnus-newsgroup-unreads)
8922                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8923                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8924                (gnus-tmp-unread-and-unselected
8925                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8926                             (zerop gnus-tmp-unselected)) "")
8927                       ((zerop gnus-tmp-unselected)
8928                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8929                       (t (format "{%d(+%d) more}"
8930                                  gnus-tmp-unread-and-unticked
8931                                  gnus-tmp-unselected))))
8932                (gnus-tmp-subject
8933                 (if (and gnus-current-headers
8934                          (vectorp gnus-current-headers))
8935                     (gnus-mode-string-quote
8936                      (mail-header-subject gnus-current-headers)) ""))
8937                max-len
8938                gnus-tmp-header);; passed as argument to any user-format-funcs
8939           (setq mode-string (eval mformat))
8940           (setq max-len (max 4 (if gnus-mode-non-string-length
8941                                    (- (window-width)
8942                                       gnus-mode-non-string-length)
8943                                  (length mode-string))))
8944           ;; We might have to chop a bit of the string off...
8945           (when (> (length mode-string) max-len)
8946             (setq mode-string
8947                   (concat (gnus-truncate-string mode-string (- max-len 3))
8948                           "...")))
8949           ;; Pad the mode string a bit.
8950           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8951       ;; Update the mode line.
8952       (setq mode-line-buffer-identification (list mode-string))
8953       (set-buffer-modified-p t))))
8954
8955 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8956   "Go through the HEADERS list and add all Xrefs to a hash table.
8957 The resulting hash table is returned, or nil if no Xrefs were found."
8958   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8959          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8960          (xref-hashtb (make-vector 63 0))
8961          start group entry number xrefs header)
8962     (while headers
8963       (setq header (pop headers))
8964       (when (and (setq xrefs (mail-header-xref header))
8965                  (not (memq (setq number (mail-header-number header))
8966                             unreads)))
8967         (setq start 0)
8968         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8969           (setq start (match-end 0))
8970           (setq group (if prefix
8971                           (concat prefix (substring xrefs (match-beginning 1)
8972                                                     (match-end 1)))
8973                         (substring xrefs (match-beginning 1) (match-end 1))))
8974           (setq number
8975                 (string-to-int (substring xrefs (match-beginning 2)
8976                                           (match-end 2))))
8977           (if (setq entry (gnus-gethash group xref-hashtb))
8978               (setcdr entry (cons number (cdr entry)))
8979             (gnus-sethash group (cons number nil) xref-hashtb)))))
8980     (and start xref-hashtb)))
8981
8982 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8983   "Look through all the headers and mark the Xrefs as read."
8984   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8985         name entry info xref-hashtb idlist method nth4)
8986     (save-excursion
8987       (set-buffer gnus-group-buffer)
8988       (when (setq xref-hashtb
8989                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8990         (mapatoms
8991          (lambda (group)
8992            (unless (string= from-newsgroup (setq name (symbol-name group)))
8993              (setq idlist (symbol-value group))
8994              ;; Dead groups are not updated.
8995              (and (prog1
8996                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8997                             info (nth 2 entry))
8998                     (if (stringp (setq nth4 (gnus-info-method info)))
8999                         (setq nth4 (gnus-server-to-method nth4))))
9000                   ;; Only do the xrefs if the group has the same
9001                   ;; select method as the group we have just read.
9002                   (or (gnus-methods-equal-p
9003                        nth4 (gnus-find-method-for-group from-newsgroup))
9004                       virtual
9005                       (equal nth4 (setq method (gnus-find-method-for-group
9006                                                 from-newsgroup)))
9007                       (and (equal (car nth4) (car method))
9008                            (equal (nth 1 nth4) (nth 1 method))))
9009                   gnus-use-cross-reference
9010                   (or (not (eq gnus-use-cross-reference t))
9011                       virtual
9012                       ;; Only do cross-references on subscribed
9013                       ;; groups, if that is what is wanted.
9014                       (<= (gnus-info-level info) gnus-level-subscribed))
9015                   (gnus-group-make-articles-read name idlist))))
9016          xref-hashtb)))))
9017
9018 (defun gnus-group-make-articles-read (group articles)
9019   (let* ((num 0)
9020          (entry (gnus-gethash group gnus-newsrc-hashtb))
9021          (info (nth 2 entry))
9022          (active (gnus-active group))
9023          range)
9024     ;; First peel off all illegal article numbers.
9025     (if active
9026         (let ((ids articles)
9027               id first)
9028           (while ids
9029             (setq id (car ids))
9030             (if (and first (> id (cdr active)))
9031                 (progn
9032                   ;; We'll end up in this situation in one particular
9033                   ;; obscure situation.  If you re-scan a group and get
9034                   ;; a new article that is cross-posted to a different
9035                   ;; group that has not been re-scanned, you might get
9036                   ;; crossposted article that has a higher number than
9037                   ;; Gnus believes possible.  So we re-activate this
9038                   ;; group as well.  This might mean doing the
9039                   ;; crossposting thingy will *increase* the number
9040                   ;; of articles in some groups.  Tsk, tsk.
9041                   (setq active (or (gnus-activate-group group) active))))
9042             (if (or (> id (cdr active))
9043                     (< id (car active)))
9044                 (setq articles (delq id articles)))
9045             (setq ids (cdr ids)))))
9046     ;; If the read list is nil, we init it.
9047     (and active
9048          (null (gnus-info-read info))
9049          (> (car active) 1)
9050          (gnus-info-set-read info (cons 1 (1- (car active)))))
9051     ;; Then we add the read articles to the range.
9052     (gnus-info-set-read
9053      info
9054      (setq range
9055            (gnus-add-to-range
9056             (gnus-info-read info) (setq articles (sort articles '<)))))
9057     ;; Then we have to re-compute how many unread
9058     ;; articles there are in this group.
9059     (if active
9060         (progn
9061           (cond
9062            ((not range)
9063             (setq num (- (1+ (cdr active)) (car active))))
9064            ((not (listp (cdr range)))
9065             (setq num (- (cdr active) (- (1+ (cdr range))
9066                                          (car range)))))
9067            (t
9068             (while range
9069               (if (numberp (car range))
9070                   (setq num (1+ num))
9071                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9072               (setq range (cdr range)))
9073             (setq num (- (cdr active) num))))
9074           ;; Update the number of unread articles.
9075           (setcar entry num)
9076           ;; Update the group buffer.
9077           (gnus-group-update-group group t)))))
9078
9079 (defun gnus-methods-equal-p (m1 m2)
9080   (let ((m1 (or m1 gnus-select-method))
9081         (m2 (or m2 gnus-select-method)))
9082     (or (equal m1 m2)
9083         (and (eq (car m1) (car m2))
9084              (or (not (memq 'address (assoc (symbol-name (car m1))
9085                                             gnus-valid-select-methods)))
9086                  (equal (nth 1 m1) (nth 1 m2)))))))
9087
9088 (defsubst gnus-header-value ()
9089   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9090
9091 (defvar gnus-newsgroup-none-id 0)
9092
9093 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9094   (let ((cur nntp-server-buffer)
9095         (dependencies
9096          (or dependencies
9097              (save-excursion (set-buffer gnus-summary-buffer)
9098                              gnus-newsgroup-dependencies)))
9099         headers id id-dep ref-dep end ref)
9100     (save-excursion
9101       (set-buffer nntp-server-buffer)
9102       (run-hooks 'gnus-parse-headers-hook)
9103       (let ((case-fold-search t)
9104             in-reply-to header p lines)
9105         (goto-char (point-min))
9106         ;; Search to the beginning of the next header.  Error messages
9107         ;; do not begin with 2 or 3.
9108         (while (re-search-forward "^[23][0-9]+ " nil t)
9109           (setq id nil
9110                 ref nil)
9111           ;; This implementation of this function, with nine
9112           ;; search-forwards instead of the one re-search-forward and
9113           ;; a case (which basically was the old function) is actually
9114           ;; about twice as fast, even though it looks messier.  You
9115           ;; can't have everything, I guess.  Speed and elegance
9116           ;; doesn't always go hand in hand.
9117           (setq
9118            header
9119            (vector
9120             ;; Number.
9121             (prog1
9122                 (read cur)
9123               (end-of-line)
9124               (setq p (point))
9125               (narrow-to-region (point)
9126                                 (or (and (search-forward "\n.\n" nil t)
9127                                          (- (point) 2))
9128                                     (point))))
9129             ;; Subject.
9130             (progn
9131               (goto-char p)
9132               (if (search-forward "\nsubject: " nil t)
9133                   (gnus-header-value) "(none)"))
9134             ;; From.
9135             (progn
9136               (goto-char p)
9137               (if (search-forward "\nfrom: " nil t)
9138                   (gnus-header-value) "(nobody)"))
9139             ;; Date.
9140             (progn
9141               (goto-char p)
9142               (if (search-forward "\ndate: " nil t)
9143                   (gnus-header-value) ""))
9144             ;; Message-ID.
9145             (progn
9146               (goto-char p)
9147               (if (search-forward "\nmessage-id: " nil t)
9148                   (setq id (gnus-header-value))
9149                 ;; If there was no message-id, we just fake one to make
9150                 ;; subsequent routines simpler.
9151                 (setq id (concat "none+"
9152                                  (int-to-string
9153                                   (setq gnus-newsgroup-none-id
9154                                         (1+ gnus-newsgroup-none-id)))))))
9155             ;; References.
9156             (progn
9157               (goto-char p)
9158               (if (search-forward "\nreferences: " nil t)
9159                   (prog1
9160                       (gnus-header-value)
9161                     (setq end (match-end 0))
9162                     (save-excursion
9163                       (setq ref
9164                             (buffer-substring
9165                              (progn
9166                                (end-of-line)
9167                                (search-backward ">" end t)
9168                                (1+ (point)))
9169                              (progn
9170                                (search-backward "<" end t)
9171                                (point))))))
9172                 ;; Get the references from the in-reply-to header if there
9173                 ;; were no references and the in-reply-to header looks
9174                 ;; promising.
9175                 (if (and (search-forward "\nin-reply-to: " nil t)
9176                          (setq in-reply-to (gnus-header-value))
9177                          (string-match "<[^>]+>" in-reply-to))
9178                     (setq ref (substring in-reply-to (match-beginning 0)
9179                                          (match-end 0)))
9180                   (setq ref ""))))
9181             ;; Chars.
9182             0
9183             ;; Lines.
9184             (progn
9185               (goto-char p)
9186               (if (search-forward "\nlines: " nil t)
9187                   (if (numberp (setq lines (read cur)))
9188                       lines 0)
9189                 0))
9190             ;; Xref.
9191             (progn
9192               (goto-char p)
9193               (and (search-forward "\nxref: " nil t)
9194                    (gnus-header-value)))))
9195           ;; We do the threading while we read the headers.  The
9196           ;; message-id and the last reference are both entered into
9197           ;; the same hash table.  Some tippy-toeing around has to be
9198           ;; done in case an article has arrived before the article
9199           ;; which it refers to.
9200           (if (boundp (setq id-dep (intern id dependencies)))
9201               (if (and (car (symbol-value id-dep))
9202                        (not force-new))
9203                   ;; An article with this Message-ID has already
9204                   ;; been seen, so we ignore this one, except we add
9205                   ;; any additional Xrefs (in case the two articles
9206                   ;; came from different servers).
9207                   (progn
9208                     (mail-header-set-xref
9209                      (car (symbol-value id-dep))
9210                      (concat (or (mail-header-xref
9211                                   (car (symbol-value id-dep))) "")
9212                              (or (mail-header-xref header) "")))
9213                     (setq header nil))
9214                 (setcar (symbol-value id-dep) header))
9215             (set id-dep (list header)))
9216           (when header
9217             (if (boundp (setq ref-dep (intern ref dependencies)))
9218                 (setcdr (symbol-value ref-dep)
9219                         (nconc (cdr (symbol-value ref-dep))
9220                                (list (symbol-value id-dep))))
9221               (set ref-dep (list nil (symbol-value id-dep))))
9222             (setq headers (cons header headers)))
9223           (goto-char (point-max))
9224           (widen))
9225         (nreverse headers)))))
9226
9227 ;; The following macros and functions were written by Felix Lee
9228 ;; <flee@cse.psu.edu>.
9229
9230 (defmacro gnus-nov-read-integer ()
9231   '(prog1
9232        (if (= (following-char) ?\t)
9233            0
9234          (let ((num (condition-case nil (read buffer) (error nil))))
9235            (if (numberp num) num 0)))
9236      (or (eobp) (forward-char 1))))
9237
9238 (defmacro gnus-nov-skip-field ()
9239   '(search-forward "\t" eol 'move))
9240
9241 (defmacro gnus-nov-field ()
9242   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9243
9244 ;; Goes through the xover lines and returns a list of vectors
9245 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9246                                                   force-new dependencies)
9247   "Parse the news overview data in the server buffer, and return a
9248 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9249   ;; Get the Xref when the users reads the articles since most/some
9250   ;; NNTP servers do not include Xrefs when using XOVER.
9251   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9252   (let ((cur nntp-server-buffer)
9253         (dependencies (or dependencies gnus-newsgroup-dependencies))
9254         number headers header)
9255     (save-excursion
9256       (set-buffer nntp-server-buffer)
9257       ;; Allow the user to mangle the headers before parsing them.
9258       (run-hooks 'gnus-parse-headers-hook)
9259       (goto-char (point-min))
9260       (while (and sequence (not (eobp)))
9261         (setq number (read cur))
9262         (while (and sequence (< (car sequence) number))
9263           (setq sequence (cdr sequence)))
9264         (and sequence
9265              (eq number (car sequence))
9266              (progn
9267                (setq sequence (cdr sequence))
9268                (if (setq header
9269                          (inline (gnus-nov-parse-line
9270                                   number dependencies force-new)))
9271                    (setq headers (cons header headers)))))
9272         (forward-line 1))
9273       (setq headers (nreverse headers)))
9274     headers))
9275
9276 ;; This function has to be called with point after the article number
9277 ;; on the beginning of the line.
9278 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9279   (let ((none 0)
9280         (eol (gnus-point-at-eol))
9281         (buffer (current-buffer))
9282         header ref id id-dep ref-dep)
9283
9284     ;; overview: [num subject from date id refs chars lines misc]
9285     (narrow-to-region (point) eol)
9286     (or (eobp) (forward-char))
9287
9288     (condition-case nil
9289         (setq header
9290               (vector
9291                number                   ; number
9292                (gnus-nov-field)         ; subject
9293                (gnus-nov-field)         ; from
9294                (gnus-nov-field)         ; date
9295                (setq id (or (gnus-nov-field)
9296                             (concat "none+"
9297                                     (int-to-string
9298                                      (setq none (1+ none)))))) ; id
9299                (progn
9300                  (save-excursion
9301                    (let ((beg (point)))
9302                      (search-forward "\t" eol)
9303                      (if (search-backward ">" beg t)
9304                          (setq ref
9305                                (buffer-substring
9306                                 (1+ (point))
9307                                 (search-backward "<" beg t)))
9308                        (setq ref nil))))
9309                  (gnus-nov-field))      ; refs
9310                (gnus-nov-read-integer)  ; chars
9311                (gnus-nov-read-integer)  ; lines
9312                (if (= (following-char) ?\n)
9313                    nil
9314                  (gnus-nov-field))      ; misc
9315                ))
9316       (error (progn
9317                (gnus-error 4 "Strange nov line")
9318                (setq header nil)
9319                (goto-char eol))))
9320
9321     (widen)
9322
9323     ;; We build the thread tree.
9324     (when header
9325       (if (boundp (setq id-dep (intern id dependencies)))
9326           (if (and (car (symbol-value id-dep))
9327                    (not force-new))
9328               ;; An article with this Message-ID has already been seen,
9329               ;; so we ignore this one, except we add any additional
9330               ;; Xrefs (in case the two articles came from different
9331               ;; servers.
9332               (progn
9333                 (mail-header-set-xref
9334                  (car (symbol-value id-dep))
9335                  (concat (or (mail-header-xref
9336                               (car (symbol-value id-dep))) "")
9337                          (or (mail-header-xref header) "")))
9338                 (setq header nil))
9339             (setcar (symbol-value id-dep) header))
9340         (set id-dep (list header))))
9341     (if header
9342         (progn
9343           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9344               (setcdr (symbol-value ref-dep)
9345                       (nconc (cdr (symbol-value ref-dep))
9346                              (list (symbol-value id-dep))))
9347             (set ref-dep (list nil (symbol-value id-dep))))))
9348     header))
9349
9350 (defun gnus-article-get-xrefs ()
9351   "Fill in the Xref value in `gnus-current-headers', if necessary.
9352 This is meant to be called in `gnus-article-internal-prepare-hook'."
9353   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9354                                  gnus-current-headers)))
9355     (or (not gnus-use-cross-reference)
9356         (not headers)
9357         (and (mail-header-xref headers)
9358              (not (string= (mail-header-xref headers) "")))
9359         (let ((case-fold-search t)
9360               xref)
9361           (save-restriction
9362             (nnheader-narrow-to-headers)
9363             (goto-char (point-min))
9364             (if (or (and (eq (downcase (following-char)) ?x)
9365                          (looking-at "Xref:"))
9366                     (search-forward "\nXref:" nil t))
9367                 (progn
9368                   (goto-char (1+ (match-end 0)))
9369                   (setq xref (buffer-substring (point)
9370                                                (progn (end-of-line) (point))))
9371                   (mail-header-set-xref headers xref))))))))
9372
9373 (defun gnus-summary-insert-subject (id &optional old-header)
9374   "Find article ID and insert the summary line for that article."
9375   (let ((header (gnus-read-header id))
9376         (number (and (numberp id) id))
9377         pos)
9378     (when header
9379       ;; Rebuild the thread that this article is part of and go to the
9380       ;; article we have fetched.
9381       (when old-header
9382         (when (setq pos (text-property-any
9383                          (point-min) (point-max) 'gnus-number 
9384                          (mail-header-number old-header)))
9385           (goto-char pos)
9386           (gnus-delete-line)
9387           (gnus-data-remove (mail-header-number old-header))))
9388       (when old-header
9389         (mail-header-set-number header (mail-header-number old-header)))
9390       (setq gnus-newsgroup-sparse
9391             (delq (mail-header-number header) gnus-newsgroup-sparse))
9392       (gnus-rebuild-thread (mail-header-id header))
9393       (gnus-summary-goto-subject (setq number (mail-header-number header))
9394                                  nil t))
9395     (when (and (numberp number)
9396                (> number 0))
9397       ;; We have to update the boundaries even if we can't fetch the
9398       ;; article if ID is a number -- so that the next `P' or `N'
9399       ;; command will fetch the previous (or next) article even
9400       ;; if the one we tried to fetch this time has been canceled.
9401       (and (> number gnus-newsgroup-end)
9402            (setq gnus-newsgroup-end number))
9403       (and (< number gnus-newsgroup-begin)
9404            (setq gnus-newsgroup-begin number))
9405       (setq gnus-newsgroup-unselected
9406             (delq number gnus-newsgroup-unselected)))
9407     ;; Report back a success?
9408     (and header (mail-header-number header))))
9409
9410 (defun gnus-summary-work-articles (n)
9411   "Return a list of articles to be worked upon.  The prefix argument,
9412 the list of process marked articles, and the current article will be
9413 taken into consideration."
9414   (cond
9415    ((and n (numberp n))
9416     ;; A numerical prefix has been given.
9417     (let ((backward (< n 0))
9418           (n (abs n))
9419           articles article)
9420       (save-excursion
9421         (while
9422             (and (> n 0)
9423                  (push (setq article (gnus-summary-article-number))
9424                        articles)
9425                  (if backward
9426                      (gnus-summary-find-prev nil article)
9427                    (gnus-summary-find-next nil article)))
9428           (decf n)))
9429       (nreverse articles)))
9430    ((and (boundp 'transient-mark-mode)
9431          transient-mark-mode
9432          mark-active)
9433     ;; Work on the region between point and mark.
9434     (let ((max (max (point) (mark)))
9435           articles article)
9436       (save-excursion
9437         (goto-char (min (point) (mark)))
9438         (while
9439             (and
9440              (push (setq article (gnus-summary-article-number)) articles)
9441              (gnus-summary-find-next nil article)
9442              (< (point) max)))
9443         (nreverse articles))))
9444    (gnus-newsgroup-processable
9445     ;; There are process-marked articles present.
9446     (reverse gnus-newsgroup-processable))
9447    (t
9448     ;; Just return the current article.
9449     (list (gnus-summary-article-number)))))
9450
9451 (defun gnus-summary-search-group (&optional backward use-level)
9452   "Search for next unread newsgroup.
9453 If optional argument BACKWARD is non-nil, search backward instead."
9454   (save-excursion
9455     (set-buffer gnus-group-buffer)
9456     (if (gnus-group-search-forward
9457          backward nil (if use-level (gnus-group-group-level) nil))
9458         (gnus-group-group-name))))
9459
9460 (defun gnus-summary-best-group (&optional exclude-group)
9461   "Find the name of the best unread group.
9462 If EXCLUDE-GROUP, do not go to this group."
9463   (save-excursion
9464     (set-buffer gnus-group-buffer)
9465     (save-excursion
9466       (gnus-group-best-unread-group exclude-group))))
9467
9468 (defun gnus-summary-find-next (&optional unread article backward)
9469   (if backward (gnus-summary-find-prev)
9470     (let* ((dummy (gnus-summary-article-intangible-p))
9471            (article (or article (gnus-summary-article-number)))
9472            (arts (gnus-data-find-list article))
9473            result)
9474       (when (and (not dummy)
9475                  (or (not gnus-summary-check-current)
9476                      (not unread)
9477                      (not (gnus-data-unread-p (car arts)))))
9478         (setq arts (cdr arts)))
9479       (when (setq result
9480                   (if unread
9481                       (progn
9482                         (while arts
9483                           (when (gnus-data-unread-p (car arts))
9484                             (setq result (car arts)
9485                                   arts nil))
9486                           (setq arts (cdr arts)))
9487                         result)
9488                     (car arts)))
9489         (goto-char (gnus-data-pos result))
9490         (gnus-data-number result)))))
9491
9492 (defun gnus-summary-find-prev (&optional unread article)
9493   (let* ((article (or article (gnus-summary-article-number)))
9494          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9495          result)
9496     (when (or (not gnus-summary-check-current)
9497               (not unread)
9498               (not (gnus-data-unread-p (car arts))))
9499       (setq arts (cdr arts)))
9500     (if (setq result
9501               (if unread
9502                   (progn
9503                     (while arts
9504                       (and (gnus-data-unread-p (car arts))
9505                            (setq result (car arts)
9506                                  arts nil))
9507                       (setq arts (cdr arts)))
9508                     result)
9509                 (car arts)))
9510         (progn
9511           (goto-char (gnus-data-pos result))
9512           (gnus-data-number result)))))
9513
9514 (defun gnus-summary-find-subject (subject &optional unread backward article)
9515   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9516          (article (or article (gnus-summary-article-number)))
9517          (articles (gnus-data-list backward))
9518          (arts (gnus-data-find-list article articles))
9519          result)
9520     (when (or (not gnus-summary-check-current)
9521               (not unread)
9522               (not (gnus-data-unread-p (car arts))))
9523       (setq arts (cdr arts)))
9524     (while arts
9525       (and (or (not unread)
9526                (gnus-data-unread-p (car arts)))
9527            (vectorp (gnus-data-header (car arts)))
9528            (gnus-subject-equal
9529             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9530            (setq result (car arts)
9531                  arts nil))
9532       (setq arts (cdr arts)))
9533     (and result
9534          (goto-char (gnus-data-pos result))
9535          (gnus-data-number result))))
9536
9537 (defun gnus-summary-search-forward (&optional unread subject backward)
9538   "Search forward for an article.
9539 If UNREAD, look for unread articles.  If SUBJECT, look for
9540 articles with that subject.  If BACKWARD, search backward instead."
9541   (cond (subject (gnus-summary-find-subject subject unread backward))
9542         (backward (gnus-summary-find-prev unread))
9543         (t (gnus-summary-find-next unread))))
9544
9545 (defun gnus-recenter (&optional n)
9546   "Center point in window and redisplay frame.
9547 Also do horizontal recentering."
9548   (interactive "P")
9549   (when (and gnus-auto-center-summary
9550              (not (eq gnus-auto-center-summary 'vertical)))
9551     (gnus-horizontal-recenter))
9552   (recenter n))
9553
9554 (defun gnus-summary-recenter ()
9555   "Center point in the summary window.
9556 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9557 displayed, no centering will be performed."
9558   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9559   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9560   (let* ((top (cond ((< (window-height) 4) 0)
9561                     ((< (window-height) 7) 1)
9562                     (t 2)))
9563          (height (1- (window-height)))
9564          (bottom (save-excursion (goto-char (point-max))
9565                                  (forward-line (- height))
9566                                  (point)))
9567          (window (get-buffer-window (current-buffer))))
9568     ;; The user has to want it.
9569     (when gnus-auto-center-summary
9570       (when (get-buffer-window gnus-article-buffer)
9571        ;; Only do recentering when the article buffer is displayed,
9572        ;; Set the window start to either `bottom', which is the biggest
9573        ;; possible valid number, or the second line from the top,
9574        ;; whichever is the least.
9575        (set-window-start
9576         window (min bottom (save-excursion 
9577                              (forward-line (- top)) (point)))))
9578       ;; Do horizontal recentering while we're at it.
9579       (when (and (get-buffer-window (current-buffer) t)
9580                  (not (eq gnus-auto-center-summary 'vertical)))
9581         (let ((selected (selected-window)))
9582           (select-window (get-buffer-window (current-buffer) t))
9583           (gnus-summary-position-point)
9584           (gnus-horizontal-recenter)
9585           (select-window selected))))))
9586
9587 (defun gnus-horizontal-recenter ()
9588   "Recenter the current buffer horizontally."
9589   (if (< (current-column) (/ (window-width) 2))
9590       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9591     (let* ((orig (point))
9592            (end (window-end (get-buffer-window (current-buffer) t)))
9593            (max 0))
9594       ;; Find the longest line currently displayed in the window.
9595       (goto-char (window-start))
9596       (while (and (not (eobp)) 
9597                   (< (point) end))
9598         (end-of-line)
9599         (setq max (max max (current-column)))
9600         (forward-line 1))
9601       (goto-char orig)
9602       ;; Scroll horizontally to center (sort of) the point.
9603       (if (> max (window-width))
9604           (set-window-hscroll 
9605            (get-buffer-window (current-buffer) t)
9606            (min (- (current-column) (/ (window-width) 3))
9607                 (+ 2 (- max (window-width)))))
9608         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9609       max)))
9610
9611 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9612 (defun gnus-short-group-name (group &optional levels)
9613   "Collapse GROUP name LEVELS."
9614   (let* ((name "") 
9615          (foreign "")
9616          (depth 0) 
9617          (skip 1)
9618          (levels (or levels
9619                      (progn
9620                        (while (string-match "\\." group skip)
9621                          (setq skip (match-end 0)
9622                                depth (+ depth 1)))
9623                        depth))))
9624     (if (string-match ":" group)
9625         (setq foreign (substring group 0 (match-end 0))
9626               group (substring group (match-end 0))))
9627     (while group
9628       (if (and (string-match "\\." group)
9629                (> levels (- gnus-group-uncollapsed-levels 1)))
9630           (setq name (concat name (substring group 0 1))
9631                 group (substring group (match-end 0))
9632                 levels (- levels 1)
9633                 name (concat name "."))
9634         (setq name (concat foreign name group)
9635               group nil)))
9636     name))
9637
9638 (defun gnus-summary-jump-to-group (newsgroup)
9639   "Move point to NEWSGROUP in group mode buffer."
9640   ;; Keep update point of group mode buffer if visible.
9641   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9642       (save-window-excursion
9643         ;; Take care of tree window mode.
9644         (if (get-buffer-window gnus-group-buffer)
9645             (pop-to-buffer gnus-group-buffer))
9646         (gnus-group-jump-to-group newsgroup))
9647     (save-excursion
9648       ;; Take care of tree window mode.
9649       (if (get-buffer-window gnus-group-buffer)
9650           (pop-to-buffer gnus-group-buffer)
9651         (set-buffer gnus-group-buffer))
9652       (gnus-group-jump-to-group newsgroup))))
9653
9654 ;; This function returns a list of article numbers based on the
9655 ;; difference between the ranges of read articles in this group and
9656 ;; the range of active articles.
9657 (defun gnus-list-of-unread-articles (group)
9658   (let* ((read (gnus-info-read (gnus-get-info group)))
9659          (active (gnus-active group))
9660          (last (cdr active))
9661          first nlast unread)
9662     ;; If none are read, then all are unread.
9663     (if (not read)
9664         (setq first (car active))
9665       ;; If the range of read articles is a single range, then the
9666       ;; first unread article is the article after the last read
9667       ;; article.  Sounds logical, doesn't it?
9668       (if (not (listp (cdr read)))
9669           (setq first (1+ (cdr read)))
9670         ;; `read' is a list of ranges.
9671         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9672                                 (caar read))) 1)
9673             (setq first 1))
9674         (while read
9675           (if first
9676               (while (< first nlast)
9677                 (setq unread (cons first unread))
9678                 (setq first (1+ first))))
9679           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9680           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9681           (setq read (cdr read)))))
9682     ;; And add the last unread articles.
9683     (while (<= first last)
9684       (setq unread (cons first unread))
9685       (setq first (1+ first)))
9686     ;; Return the list of unread articles.
9687     (nreverse unread)))
9688
9689 (defun gnus-list-of-read-articles (group)
9690   "Return a list of unread, unticked and non-dormant articles."
9691   (let* ((info (gnus-get-info group))
9692          (marked (gnus-info-marks info))
9693          (active (gnus-active group)))
9694     (and info active
9695          (gnus-set-difference
9696           (gnus-sorted-complement
9697            (gnus-uncompress-range active)
9698            (gnus-list-of-unread-articles group))
9699           (append
9700            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9701            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9702
9703 ;; Various summary commands
9704
9705 (defun gnus-summary-universal-argument (arg)
9706   "Perform any operation on all articles that are process/prefixed."
9707   (interactive "P")
9708   (gnus-set-global-variables)
9709   (let ((articles (gnus-summary-work-articles arg))
9710         func article)
9711     (if (eq
9712          (setq
9713           func
9714           (key-binding
9715            (read-key-sequence
9716             (substitute-command-keys
9717              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9718              ))))
9719          'undefined)
9720         (gnus-error 1 "Undefined key")
9721       (save-excursion
9722         (while articles
9723           (gnus-summary-goto-subject (setq article (pop articles)))
9724           (command-execute func)
9725           (gnus-summary-remove-process-mark article)))))
9726   (gnus-summary-position-point))
9727
9728 (defun gnus-summary-toggle-truncation (&optional arg)
9729   "Toggle truncation of summary lines.
9730 With arg, turn line truncation on iff arg is positive."
9731   (interactive "P")
9732   (setq truncate-lines
9733         (if (null arg) (not truncate-lines)
9734           (> (prefix-numeric-value arg) 0)))
9735   (redraw-display))
9736
9737 (defun gnus-summary-reselect-current-group (&optional all rescan)
9738   "Exit and then reselect the current newsgroup.
9739 The prefix argument ALL means to select all articles."
9740   (interactive "P")
9741   (gnus-set-global-variables)
9742   (let ((current-subject (gnus-summary-article-number))
9743         (group gnus-newsgroup-name))
9744     (setq gnus-newsgroup-begin nil)
9745     (gnus-summary-exit)
9746     ;; We have to adjust the point of group mode buffer because the
9747     ;; current point was moved to the next unread newsgroup by
9748     ;; exiting.
9749     (gnus-summary-jump-to-group group)
9750     (when rescan
9751       (save-excursion
9752         (gnus-group-get-new-news-this-group 1)))
9753     (gnus-group-read-group all t)
9754     (gnus-summary-goto-subject current-subject)))
9755
9756 (defun gnus-summary-rescan-group (&optional all)
9757   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9758   (interactive "P")
9759   (gnus-summary-reselect-current-group all t))
9760
9761 (defun gnus-summary-update-info ()
9762   (let* ((group gnus-newsgroup-name))
9763     (when gnus-newsgroup-kill-headers
9764       (setq gnus-newsgroup-killed
9765             (gnus-compress-sequence
9766              (nconc
9767               (gnus-set-sorted-intersection
9768                (gnus-uncompress-range gnus-newsgroup-killed)
9769                (setq gnus-newsgroup-unselected
9770                      (sort gnus-newsgroup-unselected '<)))
9771               (setq gnus-newsgroup-unreads
9772                     (sort gnus-newsgroup-unreads '<))) t)))
9773     (unless (listp (cdr gnus-newsgroup-killed))
9774       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9775     (let ((headers gnus-newsgroup-headers))
9776       (run-hooks 'gnus-exit-group-hook)
9777       (unless gnus-save-score
9778         (setq gnus-newsgroup-scored nil))
9779       ;; Set the new ranges of read articles.
9780       (gnus-update-read-articles
9781        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9782       ;; Set the current article marks.
9783       (gnus-update-marks)
9784       ;; Do the cross-ref thing.
9785       (when gnus-use-cross-reference
9786         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9787       ;; Do adaptive scoring, and possibly save score files.
9788       (when gnus-newsgroup-adaptive
9789         (gnus-score-adaptive))
9790       (when gnus-use-scoring
9791         (gnus-score-save))
9792       ;; Do not switch windows but change the buffer to work.
9793       (set-buffer gnus-group-buffer)
9794       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9795           (gnus-group-update-group group)))))
9796
9797 (defun gnus-summary-exit (&optional temporary)
9798   "Exit reading current newsgroup, and then return to group selection mode.
9799 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9800   (interactive)
9801   (gnus-set-global-variables)
9802   (gnus-kill-save-kill-buffer)
9803   (let* ((group gnus-newsgroup-name)
9804          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9805          (mode major-mode)
9806          (buf (current-buffer)))
9807     (run-hooks 'gnus-summary-prepare-exit-hook)
9808     ;; If we have several article buffers, we kill them at exit.
9809     (unless gnus-single-article-buffer
9810       (gnus-kill-buffer gnus-original-article-buffer)
9811       (setq gnus-article-current nil))
9812     (when gnus-use-cache
9813       (gnus-cache-possibly-remove-articles)
9814       (gnus-cache-save-buffers))
9815     (when gnus-use-trees
9816       (gnus-tree-close group))
9817     ;; Make all changes in this group permanent.
9818     (unless quit-config
9819       (gnus-summary-update-info))
9820     (gnus-close-group group)
9821     ;; Make sure where I was, and go to next newsgroup.
9822     (set-buffer gnus-group-buffer)
9823     (unless quit-config
9824       (gnus-group-jump-to-group group)
9825       (gnus-group-next-unread-group 1))
9826     (run-hooks 'gnus-summary-exit-hook)
9827     (if temporary
9828         nil                             ;Nothing to do.
9829       ;; If we have several article buffers, we kill them at exit.
9830       (unless gnus-single-article-buffer
9831         (gnus-kill-buffer gnus-article-buffer)
9832         (gnus-kill-buffer gnus-original-article-buffer)
9833         (setq gnus-article-current nil))
9834       (set-buffer buf)
9835       (if (not gnus-kill-summary-on-exit)
9836           (gnus-deaden-summary)
9837         ;; We set all buffer-local variables to nil.  It is unclear why
9838         ;; this is needed, but if we don't, buffer-local variables are
9839         ;; not garbage-collected, it seems.  This would the lead to en
9840         ;; ever-growing Emacs.
9841         (gnus-summary-clear-local-variables)
9842         (when (get-buffer gnus-article-buffer)
9843           (bury-buffer gnus-article-buffer))
9844         ;; We clear the global counterparts of the buffer-local
9845         ;; variables as well, just to be on the safe side.
9846         (gnus-configure-windows 'group 'force)
9847         (gnus-summary-clear-local-variables)
9848         ;; Return to group mode buffer.
9849         (if (eq mode 'gnus-summary-mode)
9850             (gnus-kill-buffer buf)))
9851       (setq gnus-current-select-method gnus-select-method)
9852       (pop-to-buffer gnus-group-buffer)
9853       ;; Clear the current group name.
9854       (if (not quit-config)
9855           (progn
9856             (gnus-group-jump-to-group group)
9857             (gnus-group-next-unread-group 1)
9858             (gnus-configure-windows 'group 'force))
9859         (if (not (buffer-name (car quit-config)))
9860             (gnus-configure-windows 'group 'force)
9861           (set-buffer (car quit-config))
9862           (and (eq major-mode 'gnus-summary-mode)
9863                (gnus-set-global-variables))
9864           (gnus-configure-windows (cdr quit-config))))
9865       (unless quit-config
9866         (setq gnus-newsgroup-name nil)))))
9867
9868 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9869 (defun gnus-summary-exit-no-update (&optional no-questions)
9870   "Quit reading current newsgroup without updating read article info."
9871   (interactive)
9872   (gnus-set-global-variables)
9873   (let* ((group gnus-newsgroup-name)
9874          (quit-config (gnus-group-quit-config group)))
9875     (when (or no-questions
9876               gnus-expert-user
9877               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9878       ;; If we have several article buffers, we kill them at exit.
9879       (unless gnus-single-article-buffer
9880         (gnus-kill-buffer gnus-article-buffer)
9881         (gnus-kill-buffer gnus-original-article-buffer)
9882         (setq gnus-article-current nil))
9883       (if (not gnus-kill-summary-on-exit)
9884           (gnus-deaden-summary)
9885         (gnus-close-group group)
9886         (gnus-summary-clear-local-variables)
9887         (set-buffer gnus-group-buffer)
9888         (gnus-summary-clear-local-variables)
9889         (when (get-buffer gnus-summary-buffer)
9890           (kill-buffer gnus-summary-buffer)))
9891       (unless gnus-single-article-buffer
9892         (setq gnus-article-current nil))
9893       (when gnus-use-trees
9894         (gnus-tree-close group))
9895       (when (get-buffer gnus-article-buffer)
9896         (bury-buffer gnus-article-buffer))
9897       ;; Return to the group buffer.
9898       (gnus-configure-windows 'group 'force)
9899       ;; Clear the current group name.
9900       (setq gnus-newsgroup-name nil)
9901       (when (equal (gnus-group-group-name) group)
9902         (gnus-group-next-unread-group 1))
9903       (when quit-config
9904         (if (not (buffer-name (car quit-config)))
9905             (gnus-configure-windows 'group 'force)
9906           (set-buffer (car quit-config))
9907           (when (eq major-mode 'gnus-summary-mode)
9908             (gnus-set-global-variables))
9909           (gnus-configure-windows (cdr quit-config)))))))
9910
9911 ;;; Dead summaries.
9912
9913 (defvar gnus-dead-summary-mode-map nil)
9914
9915 (if gnus-dead-summary-mode-map
9916     nil
9917   (setq gnus-dead-summary-mode-map (make-keymap))
9918   (suppress-keymap gnus-dead-summary-mode-map)
9919   (substitute-key-definition
9920    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9921   (let ((keys '("\C-d" "\r" "\177")))
9922     (while keys
9923       (define-key gnus-dead-summary-mode-map
9924         (pop keys) 'gnus-summary-wake-up-the-dead))))
9925
9926 (defvar gnus-dead-summary-mode nil
9927   "Minor mode for Gnus summary buffers.")
9928
9929 (defun gnus-dead-summary-mode (&optional arg)
9930   "Minor mode for Gnus summary buffers."
9931   (interactive "P")
9932   (when (eq major-mode 'gnus-summary-mode)
9933     (make-local-variable 'gnus-dead-summary-mode)
9934     (setq gnus-dead-summary-mode
9935           (if (null arg) (not gnus-dead-summary-mode)
9936             (> (prefix-numeric-value arg) 0)))
9937     (when gnus-dead-summary-mode
9938       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9939         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9940       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9941         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9942               minor-mode-map-alist)))))
9943
9944 (defun gnus-deaden-summary ()
9945   "Make the current summary buffer into a dead summary buffer."
9946   ;; Kill any previous dead summary buffer.
9947   (when (and gnus-dead-summary
9948              (buffer-name gnus-dead-summary))
9949     (save-excursion
9950       (set-buffer gnus-dead-summary)
9951       (when gnus-dead-summary-mode
9952         (kill-buffer (current-buffer)))))
9953   ;; Make this the current dead summary.
9954   (setq gnus-dead-summary (current-buffer))
9955   (gnus-dead-summary-mode 1)
9956   (let ((name (buffer-name)))
9957     (when (string-match "Summary" name)
9958       (rename-buffer
9959        (concat (substring name 0 (match-beginning 0)) "Dead "
9960                (substring name (match-beginning 0))) t))))
9961
9962 (defun gnus-kill-or-deaden-summary (buffer)
9963   "Kill or deaden the summary BUFFER."
9964   (when (and (buffer-name buffer)
9965              (not gnus-single-article-buffer))
9966     (save-excursion
9967       (set-buffer buffer)
9968       (gnus-kill-buffer gnus-article-buffer)
9969       (gnus-kill-buffer gnus-original-article-buffer)))
9970   (cond (gnus-kill-summary-on-exit
9971          (when (and gnus-use-trees
9972                     (and (get-buffer buffer)
9973                          (buffer-name (get-buffer buffer))))
9974            (save-excursion
9975              (set-buffer (get-buffer buffer))
9976              (gnus-tree-close gnus-newsgroup-name)))
9977          (gnus-kill-buffer buffer))
9978         ((and (get-buffer buffer)
9979               (buffer-name (get-buffer buffer)))
9980          (save-excursion
9981            (set-buffer buffer)
9982            (gnus-deaden-summary)))))
9983
9984 (defun gnus-summary-wake-up-the-dead (&rest args)
9985   "Wake up the dead summary buffer."
9986   (interactive)
9987   (gnus-dead-summary-mode -1)
9988   (let ((name (buffer-name)))
9989     (when (string-match "Dead " name)
9990       (rename-buffer
9991        (concat (substring name 0 (match-beginning 0))
9992                (substring name (match-end 0))) t)))
9993   (gnus-message 3 "This dead summary is now alive again"))
9994
9995 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9996 (defun gnus-summary-fetch-faq (&optional faq-dir)
9997   "Fetch the FAQ for the current group.
9998 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9999 in."
10000   (interactive
10001    (list
10002     (if current-prefix-arg
10003         (completing-read
10004          "Faq dir: " (and (listp gnus-group-faq-directory)
10005                           gnus-group-faq-directory)))))
10006   (let (gnus-faq-buffer)
10007     (and (setq gnus-faq-buffer
10008                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10009          (gnus-configure-windows 'summary-faq))))
10010
10011 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10012 (defun gnus-summary-describe-group (&optional force)
10013   "Describe the current newsgroup."
10014   (interactive "P")
10015   (gnus-group-describe-group force gnus-newsgroup-name))
10016
10017 (defun gnus-summary-describe-briefly ()
10018   "Describe summary mode commands briefly."
10019   (interactive)
10020   (gnus-message 6
10021                 (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")))
10022
10023 ;; Walking around group mode buffer from summary mode.
10024
10025 (defun gnus-summary-next-group (&optional no-article target-group backward)
10026   "Exit current newsgroup and then select next unread newsgroup.
10027 If prefix argument NO-ARTICLE is non-nil, no article is selected
10028 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10029 previous group instead."
10030   (interactive "P")
10031   (gnus-set-global-variables)
10032   (let ((current-group gnus-newsgroup-name)
10033         (current-buffer (current-buffer))
10034         entered)
10035     ;; First we semi-exit this group to update Xrefs and all variables.
10036     ;; We can't do a real exit, because the window conf must remain
10037     ;; the same in case the user is prompted for info, and we don't
10038     ;; want the window conf to change before that...
10039     (gnus-summary-exit t)
10040     (while (not entered)
10041       ;; Then we find what group we are supposed to enter.
10042       (set-buffer gnus-group-buffer)
10043       (gnus-group-jump-to-group current-group)
10044       (setq target-group
10045             (or target-group
10046                 (if (eq gnus-keep-same-level 'best)
10047                     (gnus-summary-best-group gnus-newsgroup-name)
10048                   (gnus-summary-search-group backward gnus-keep-same-level))))
10049       (if (not target-group)
10050           ;; There are no further groups, so we return to the group
10051           ;; buffer.
10052           (progn
10053             (gnus-message 5 "Returning to the group buffer")
10054             (setq entered t)
10055             (set-buffer current-buffer)
10056             (gnus-summary-exit))
10057         ;; We try to enter the target group.
10058         (gnus-group-jump-to-group target-group)
10059         (let ((unreads (gnus-group-group-unread)))
10060           (if (and (or (eq t unreads)
10061                        (and unreads (not (zerop unreads))))
10062                    (gnus-summary-read-group
10063                     target-group nil no-article current-buffer))
10064               (setq entered t)
10065             (setq current-group target-group
10066                   target-group nil)))))))
10067
10068 (defun gnus-summary-prev-group (&optional no-article)
10069   "Exit current newsgroup and then select previous unread newsgroup.
10070 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10071   (interactive "P")
10072   (gnus-summary-next-group no-article nil t))
10073
10074 ;; Walking around summary lines.
10075
10076 (defun gnus-summary-first-subject (&optional unread)
10077   "Go to the first unread subject.
10078 If UNREAD is non-nil, go to the first unread article.
10079 Returns the article selected or nil if there are no unread articles."
10080   (interactive "P")
10081   (prog1
10082       (cond
10083        ;; Empty summary.
10084        ((null gnus-newsgroup-data)
10085         (gnus-message 3 "No articles in the group")
10086         nil)
10087        ;; Pick the first article.
10088        ((not unread)
10089         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10090         (gnus-data-number (car gnus-newsgroup-data)))
10091        ;; No unread articles.
10092        ((null gnus-newsgroup-unreads)
10093         (gnus-message 3 "No more unread articles")
10094         nil)
10095        ;; Find the first unread article.
10096        (t
10097         (let ((data gnus-newsgroup-data))
10098           (while (and data
10099                       (not (gnus-data-unread-p (car data))))
10100             (setq data (cdr data)))
10101           (if data
10102               (progn
10103                 (goto-char (gnus-data-pos (car data)))
10104                 (gnus-data-number (car data)))))))
10105     (gnus-summary-position-point)))
10106
10107 (defun gnus-summary-next-subject (n &optional unread dont-display)
10108   "Go to next N'th summary line.
10109 If N is negative, go to the previous N'th subject line.
10110 If UNREAD is non-nil, only unread articles are selected.
10111 The difference between N and the actual number of steps taken is
10112 returned."
10113   (interactive "p")
10114   (let ((backward (< n 0))
10115         (n (abs n)))
10116     (while (and (> n 0)
10117                 (if backward
10118                     (gnus-summary-find-prev unread)
10119                   (gnus-summary-find-next unread)))
10120       (setq n (1- n)))
10121     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10122                                (if unread " unread" "")))
10123     (unless dont-display
10124       (gnus-summary-recenter)
10125       (gnus-summary-position-point))
10126     n))
10127
10128 (defun gnus-summary-next-unread-subject (n)
10129   "Go to next N'th unread summary line."
10130   (interactive "p")
10131   (gnus-summary-next-subject n t))
10132
10133 (defun gnus-summary-prev-subject (n &optional unread)
10134   "Go to previous N'th summary line.
10135 If optional argument UNREAD is non-nil, only unread article is selected."
10136   (interactive "p")
10137   (gnus-summary-next-subject (- n) unread))
10138
10139 (defun gnus-summary-prev-unread-subject (n)
10140   "Go to previous N'th unread summary line."
10141   (interactive "p")
10142   (gnus-summary-next-subject (- n) t))
10143
10144 (defun gnus-summary-goto-subject (article &optional force silent)
10145   "Go the subject line of ARTICLE.
10146 If FORCE, also allow jumping to articles not currently shown."
10147   (let ((b (point))
10148         (data (gnus-data-find article)))
10149     ;; We read in the article if we have to.
10150     (and (not data)
10151          force
10152          (gnus-summary-insert-subject article)
10153          (setq data (gnus-data-find article)))
10154     (goto-char b)
10155     (if (not data)
10156         (progn
10157           (unless silent
10158             (gnus-message 3 "Can't find article %d" article))
10159           nil)
10160       (goto-char (gnus-data-pos data))
10161       article)))
10162
10163 ;; Walking around summary lines with displaying articles.
10164
10165 (defun gnus-summary-expand-window (&optional arg)
10166   "Make the summary buffer take up the entire Emacs frame.
10167 Given a prefix, will force an `article' buffer configuration."
10168   (interactive "P")
10169   (gnus-set-global-variables)
10170   (if arg
10171       (gnus-configure-windows 'article 'force)
10172     (gnus-configure-windows 'summary 'force)))
10173
10174 (defun gnus-summary-display-article (article &optional all-header)
10175   "Display ARTICLE in article buffer."
10176   (gnus-set-global-variables)
10177   (if (null article)
10178       nil
10179     (prog1
10180         (if gnus-summary-display-article-function
10181             (funcall gnus-summary-display-article-function article all-header)
10182           (gnus-article-prepare article all-header))
10183       (run-hooks 'gnus-select-article-hook)
10184       (unless (zerop gnus-current-article)
10185         (gnus-summary-goto-subject gnus-current-article))
10186       (gnus-summary-recenter)
10187       (when gnus-use-trees
10188         (gnus-possibly-generate-tree article)
10189         (gnus-highlight-selected-tree article))
10190       ;; Successfully display article.
10191       (gnus-article-set-window-start
10192        (cdr (assq article gnus-newsgroup-bookmarks))))))
10193
10194 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10195   "Select the current article.
10196 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10197 non-nil, the article will be re-fetched even if it already present in
10198 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10199 be displayed."
10200   ;; Make sure we are in the summary buffer to work around bbdb bug.
10201   (unless (eq major-mode 'gnus-summary-mode)
10202     (set-buffer gnus-summary-buffer))
10203   (let ((article (or article (gnus-summary-article-number)))
10204         (all-headers (not (not all-headers))) ;Must be T or NIL.
10205         gnus-summary-display-article-function
10206         did)
10207     (and (not pseudo)
10208          (gnus-summary-article-pseudo-p article)
10209          (error "This is a pseudo-article."))
10210     (prog1
10211         (save-excursion
10212           (set-buffer gnus-summary-buffer)
10213           (if (or (and gnus-single-article-buffer
10214                        (or (null gnus-current-article)
10215                            (null gnus-article-current)
10216                            (null (get-buffer gnus-article-buffer))
10217                            (not (eq article (cdr gnus-article-current)))
10218                            (not (equal (car gnus-article-current)
10219                                        gnus-newsgroup-name))))
10220                   (and (not gnus-single-article-buffer)
10221                        (or (null gnus-current-article)
10222                            (not (eq gnus-current-article article))))
10223                   force)
10224               ;; The requested article is different from the current article.
10225               (prog1
10226                   (gnus-summary-display-article article all-headers)
10227                 (setq did article))
10228             (if (or all-headers gnus-show-all-headers)
10229                 (gnus-article-show-all-headers))
10230             'old))
10231       (if did
10232           (gnus-article-set-window-start
10233            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10234
10235 (defun gnus-summary-set-current-mark (&optional current-mark)
10236   "Obsolete function."
10237   nil)
10238
10239 (defun gnus-summary-next-article (&optional unread subject backward push)
10240   "Select the next article.
10241 If UNREAD, only unread articles are selected.
10242 If SUBJECT, only articles with SUBJECT are selected.
10243 If BACKWARD, the previous article is selected instead of the next."
10244   (interactive "P")
10245   (gnus-set-global-variables)
10246   (cond
10247    ;; Is there such an article?
10248    ((and (gnus-summary-search-forward unread subject backward)
10249          (or (gnus-summary-display-article (gnus-summary-article-number))
10250              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10251     (gnus-summary-position-point))
10252    ;; If not, we try the first unread, if that is wanted.
10253    ((and subject
10254          gnus-auto-select-same
10255          (or (gnus-summary-first-unread-article)
10256              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10257     (gnus-summary-position-point)
10258     (gnus-message 6 "Wrapped"))
10259    ;; Try to get next/previous article not displayed in this group.
10260    ((and gnus-auto-extend-newsgroup
10261          (not unread) (not subject))
10262     (gnus-summary-goto-article
10263      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10264      nil t))
10265    ;; Go to next/previous group.
10266    (t
10267     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10268         (gnus-summary-jump-to-group gnus-newsgroup-name))
10269     (let ((cmd last-command-char)
10270           (group
10271            (if (eq gnus-keep-same-level 'best)
10272                (gnus-summary-best-group gnus-newsgroup-name)
10273              (gnus-summary-search-group backward gnus-keep-same-level))))
10274       ;; For some reason, the group window gets selected.  We change
10275       ;; it back.
10276       (select-window (get-buffer-window (current-buffer)))
10277       ;; Select next unread newsgroup automagically.
10278       (cond
10279        ((not gnus-auto-select-next)
10280         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10281        ((or (eq gnus-auto-select-next 'quietly)
10282             (and (eq gnus-auto-select-next 'slightly-quietly)
10283                  push)
10284             (and (eq gnus-auto-select-next 'almost-quietly)
10285                  (gnus-summary-last-article-p)))
10286         ;; Select quietly.
10287         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10288             (gnus-summary-exit)
10289           (gnus-message 7 "No more%s articles (%s)..."
10290                         (if unread " unread" "")
10291                         (if group (concat "selecting " group)
10292                           "exiting"))
10293           (gnus-summary-next-group nil group backward)))
10294        (t
10295         (gnus-summary-walk-group-buffer
10296          gnus-newsgroup-name cmd unread backward)))))))
10297
10298 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10299   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10300                       (?\C-p (gnus-group-prev-unread-group 1))))
10301         keve key group ended)
10302     (save-excursion
10303       (set-buffer gnus-group-buffer)
10304       (gnus-summary-jump-to-group from-group)
10305       (setq group
10306             (if (eq gnus-keep-same-level 'best)
10307                 (gnus-summary-best-group gnus-newsgroup-name)
10308               (gnus-summary-search-group backward gnus-keep-same-level))))
10309     (while (not ended)
10310       (gnus-message
10311        5 "No more%s articles%s" (if unread " unread" "")
10312        (if (and group
10313                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10314            (format " (Type %s for %s [%s])"
10315                    (single-key-description cmd) group
10316                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10317          (format " (Type %s to exit %s)"
10318                  (single-key-description cmd)
10319                  gnus-newsgroup-name)))
10320       ;; Confirm auto selection.
10321       (setq key (car (setq keve (gnus-read-event-char))))
10322       (setq ended t)
10323       (cond
10324        ((assq key keystrokes)
10325         (let ((obuf (current-buffer)))
10326           (switch-to-buffer gnus-group-buffer)
10327           (and group
10328                (gnus-group-jump-to-group group))
10329           (eval (cadr (assq key keystrokes)))
10330           (setq group (gnus-group-group-name))
10331           (switch-to-buffer obuf))
10332         (setq ended nil))
10333        ((equal key cmd)
10334         (if (or (not group)
10335                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10336             (gnus-summary-exit)
10337           (gnus-summary-next-group nil group backward)))
10338        (t
10339         (push (cdr keve) unread-command-events))))))
10340
10341 (defun gnus-read-event-char ()
10342   "Get the next event."
10343   (let ((event (read-event)))
10344     (cons (and (numberp event) event) event)))
10345
10346 (defun gnus-summary-next-unread-article ()
10347   "Select unread article after current one."
10348   (interactive)
10349   (gnus-summary-next-article t (and gnus-auto-select-same
10350                                     (gnus-summary-article-subject))))
10351
10352 (defun gnus-summary-prev-article (&optional unread subject)
10353   "Select the article after the current one.
10354 If UNREAD is non-nil, only unread articles are selected."
10355   (interactive "P")
10356   (gnus-summary-next-article unread subject t))
10357
10358 (defun gnus-summary-prev-unread-article ()
10359   "Select unred article before current one."
10360   (interactive)
10361   (gnus-summary-prev-article t (and gnus-auto-select-same
10362                                     (gnus-summary-article-subject))))
10363
10364 (defun gnus-summary-next-page (&optional lines circular)
10365   "Show next page of the selected article.
10366 If at the end of the current article, select the next article.
10367 LINES says how many lines should be scrolled up.
10368
10369 If CIRCULAR is non-nil, go to the start of the article instead of
10370 selecting the next article when reaching the end of the current
10371 article."
10372   (interactive "P")
10373   (setq gnus-summary-buffer (current-buffer))
10374   (gnus-set-global-variables)
10375   (let ((article (gnus-summary-article-number))
10376         (endp nil))
10377     (gnus-configure-windows 'article)
10378     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10379         (if (and (eq gnus-summary-goto-unread 'never)
10380                  (not (gnus-summary-last-article-p article)))
10381             (gnus-summary-next-article)
10382           (gnus-summary-next-unread-article))
10383       (if (or (null gnus-current-article)
10384               (null gnus-article-current)
10385               (/= article (cdr gnus-article-current))
10386               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10387           ;; Selected subject is different from current article's.
10388           (gnus-summary-display-article article)
10389         (gnus-eval-in-buffer-window
10390          gnus-article-buffer
10391          (setq endp (gnus-article-next-page lines)))
10392         (if endp
10393             (cond (circular
10394                    (gnus-summary-beginning-of-article))
10395                   (lines
10396                    (gnus-message 3 "End of message"))
10397                   ((null lines)
10398                    (if (and (eq gnus-summary-goto-unread 'never)
10399                             (not (gnus-summary-last-article-p article)))
10400                        (gnus-summary-next-article)
10401                      (gnus-summary-next-unread-article)))))))
10402     (gnus-summary-recenter)
10403     (gnus-summary-position-point)))
10404
10405 (defun gnus-summary-prev-page (&optional lines)
10406   "Show previous page of selected article.
10407 Argument LINES specifies lines to be scrolled down."
10408   (interactive "P")
10409   (gnus-set-global-variables)
10410   (let ((article (gnus-summary-article-number)))
10411     (gnus-configure-windows 'article)
10412     (if (or (null gnus-current-article)
10413             (null gnus-article-current)
10414             (/= article (cdr gnus-article-current))
10415             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10416         ;; Selected subject is different from current article's.
10417         (gnus-summary-display-article article)
10418       (gnus-summary-recenter)
10419       (gnus-eval-in-buffer-window gnus-article-buffer
10420                                   (gnus-article-prev-page lines))))
10421   (gnus-summary-position-point))
10422
10423 (defun gnus-summary-scroll-up (lines)
10424   "Scroll up (or down) one line current article.
10425 Argument LINES specifies lines to be scrolled up (or down if negative)."
10426   (interactive "p")
10427   (gnus-set-global-variables)
10428   (gnus-configure-windows 'article)
10429   (gnus-summary-show-thread)
10430   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10431     (gnus-eval-in-buffer-window
10432      gnus-article-buffer
10433      (cond ((> lines 0)
10434             (if (gnus-article-next-page lines)
10435                 (gnus-message 3 "End of message")))
10436            ((< lines 0)
10437             (gnus-article-prev-page (- lines))))))
10438   (gnus-summary-recenter)
10439   (gnus-summary-position-point))
10440
10441 (defun gnus-summary-next-same-subject ()
10442   "Select next article which has the same subject as current one."
10443   (interactive)
10444   (gnus-set-global-variables)
10445   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10446
10447 (defun gnus-summary-prev-same-subject ()
10448   "Select previous article which has the same subject as current one."
10449   (interactive)
10450   (gnus-set-global-variables)
10451   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10452
10453 (defun gnus-summary-next-unread-same-subject ()
10454   "Select next unread article which has the same subject as current one."
10455   (interactive)
10456   (gnus-set-global-variables)
10457   (gnus-summary-next-article t (gnus-summary-article-subject)))
10458
10459 (defun gnus-summary-prev-unread-same-subject ()
10460   "Select previous unread article which has the same subject as current one."
10461   (interactive)
10462   (gnus-set-global-variables)
10463   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10464
10465 (defun gnus-summary-first-unread-article ()
10466   "Select the first unread article.
10467 Return nil if there are no unread articles."
10468   (interactive)
10469   (gnus-set-global-variables)
10470   (prog1
10471       (if (gnus-summary-first-subject t)
10472           (progn
10473             (gnus-summary-show-thread)
10474             (gnus-summary-first-subject t)
10475             (gnus-summary-display-article (gnus-summary-article-number))))
10476     (gnus-summary-position-point)))
10477
10478 (defun gnus-summary-best-unread-article ()
10479   "Select the unread article with the highest score."
10480   (interactive)
10481   (gnus-set-global-variables)
10482   (let ((best -1000000)
10483         (data gnus-newsgroup-data)
10484         article score)
10485     (while data
10486       (and (gnus-data-unread-p (car data))
10487            (> (setq score
10488                     (gnus-summary-article-score (gnus-data-number (car data))))
10489               best)
10490            (setq best score
10491                  article (gnus-data-number (car data))))
10492       (setq data (cdr data)))
10493     (prog1
10494         (if article
10495             (gnus-summary-goto-article article)
10496           (error "No unread articles"))
10497       (gnus-summary-position-point))))
10498
10499 (defun gnus-summary-last-subject ()
10500   "Go to the last displayed subject line in the group."
10501   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10502     (when article
10503       (gnus-summary-goto-subject article))))
10504
10505 (defun gnus-summary-goto-article (article &optional all-headers force)
10506   "Fetch ARTICLE and display it if it exists.
10507 If ALL-HEADERS is non-nil, no header lines are hidden."
10508   (interactive
10509    (list
10510     (string-to-int
10511      (completing-read
10512       "Article number: "
10513       (mapcar (lambda (number) (list (int-to-string number)))
10514               gnus-newsgroup-limit)))
10515     current-prefix-arg
10516     t))
10517   (prog1
10518       (if (gnus-summary-goto-subject article force)
10519           (gnus-summary-display-article article all-headers)
10520         (gnus-message 4 "Couldn't go to article %s" article) nil)
10521     (gnus-summary-position-point)))
10522
10523 (defun gnus-summary-goto-last-article ()
10524   "Go to the previously read article."
10525   (interactive)
10526   (prog1
10527       (and gnus-last-article
10528            (gnus-summary-goto-article gnus-last-article))
10529     (gnus-summary-position-point)))
10530
10531 (defun gnus-summary-pop-article (number)
10532   "Pop one article off the history and go to the previous.
10533 NUMBER articles will be popped off."
10534   (interactive "p")
10535   (let (to)
10536     (setq gnus-newsgroup-history
10537           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10538     (if to
10539         (gnus-summary-goto-article (car to))
10540       (error "Article history empty")))
10541   (gnus-summary-position-point))
10542
10543 ;; Summary commands and functions for limiting the summary buffer.
10544
10545 (defun gnus-summary-limit-to-articles (n)
10546   "Limit the summary buffer to the next N articles.
10547 If not given a prefix, use the process marked articles instead."
10548   (interactive "P")
10549   (gnus-set-global-variables)
10550   (prog1
10551       (let ((articles (gnus-summary-work-articles n)))
10552         (setq gnus-newsgroup-processable nil)
10553         (gnus-summary-limit articles))
10554     (gnus-summary-position-point)))
10555
10556 (defun gnus-summary-pop-limit (&optional total)
10557   "Restore the previous limit.
10558 If given a prefix, remove all limits."
10559   (interactive "P")
10560   (gnus-set-global-variables)
10561   (when total 
10562     (setq gnus-newsgroup-limits
10563           (list (mapcar (lambda (h) (mail-header-number h))
10564                         gnus-newsgroup-headers))))
10565   (unless gnus-newsgroup-limits
10566     (error "No limit to pop"))
10567   (prog1
10568       (gnus-summary-limit nil 'pop)
10569     (gnus-summary-position-point)))
10570
10571 (defun gnus-summary-limit-to-subject (subject &optional header)
10572   "Limit the summary buffer to articles that have subjects that match a regexp."
10573   (interactive "sRegexp: ")
10574   (unless header
10575     (setq header "subject"))
10576   (when (not (equal "" subject))
10577     (prog1
10578         (let ((articles (gnus-summary-find-matching
10579                          (or header "subject") subject 'all)))
10580           (or articles (error "Found no matches for \"%s\"" subject))
10581           (gnus-summary-limit articles))
10582       (gnus-summary-position-point))))
10583
10584 (defun gnus-summary-limit-to-author (from)
10585   "Limit the summary buffer to articles that have authors that match a regexp."
10586   (interactive "sRegexp: ")
10587   (gnus-summary-limit-to-subject from "from"))
10588
10589 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10590 (make-obsolete
10591  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10592
10593 (defun gnus-summary-limit-to-unread (&optional all)
10594   "Limit the summary buffer to articles that are not marked as read.
10595 If ALL is non-nil, limit strictly to unread articles."
10596   (interactive "P")
10597   (if all
10598       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10599     (gnus-summary-limit-to-marks
10600      ;; Concat all the marks that say that an article is read and have
10601      ;; those removed.
10602      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10603            gnus-killed-mark gnus-kill-file-mark
10604            gnus-low-score-mark gnus-expirable-mark
10605            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10606      'reverse)))
10607
10608 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10609 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10610
10611 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10612   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10613 If REVERSE, limit the summary buffer to articles that are not marked
10614 with MARKS.  MARKS can either be a string of marks or a list of marks.
10615 Returns how many articles were removed."
10616   (interactive "sMarks: ")
10617   (gnus-set-global-variables)
10618   (prog1
10619       (let ((data gnus-newsgroup-data)
10620             (marks (if (listp marks) marks
10621                      (append marks nil))) ; Transform to list.
10622             articles)
10623         (while data
10624           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10625                  (memq (gnus-data-mark (car data)) marks))
10626                (setq articles (cons (gnus-data-number (car data)) articles)))
10627           (setq data (cdr data)))
10628         (gnus-summary-limit articles))
10629     (gnus-summary-position-point)))
10630
10631 (defun gnus-summary-limit-to-score (&optional score)
10632   "Limit to articles with score at or above SCORE."
10633   (interactive "P")
10634   (gnus-set-global-variables)
10635   (setq score (if score
10636                   (prefix-numeric-value score)
10637                 (or gnus-summary-default-score 0)))
10638   (let ((data gnus-newsgroup-data)
10639         articles)
10640     (while data
10641       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10642                 score)
10643         (push (gnus-data-number (car data)) articles))
10644       (setq data (cdr data)))
10645     (prog1
10646         (gnus-summary-limit articles)
10647       (gnus-summary-position-point))))
10648
10649 (defun gnus-summary-limit-include-dormant ()
10650   "Display all the hidden articles that are marked as dormant."
10651   (interactive)
10652   (gnus-set-global-variables)
10653   (or gnus-newsgroup-dormant
10654       (error "There are no dormant articles in this group"))
10655   (prog1
10656       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10657     (gnus-summary-position-point)))
10658
10659 (defun gnus-summary-limit-exclude-dormant ()
10660   "Hide all dormant articles."
10661   (interactive)
10662   (gnus-set-global-variables)
10663   (prog1
10664       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10665     (gnus-summary-position-point)))
10666
10667 (defun gnus-summary-limit-exclude-childless-dormant ()
10668   "Hide all dormant articles that have no children."
10669   (interactive)
10670   (gnus-set-global-variables)
10671   (let ((data (gnus-data-list t))
10672         articles d children)
10673     ;; Find all articles that are either not dormant or have
10674     ;; children.
10675     (while (setq d (pop data))
10676       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10677                 (and (setq children 
10678                            (gnus-article-children (gnus-data-number d)))
10679                      (let (found)
10680                        (while children
10681                          (when (memq (car children) articles)
10682                            (setq children nil
10683                                  found t))
10684                          (pop children))
10685                        found)))
10686         (push (gnus-data-number d) articles)))
10687     ;; Do the limiting.
10688     (prog1
10689         (gnus-summary-limit articles)
10690       (gnus-summary-position-point))))
10691
10692 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10693   "Mark all unread excluded articles as read.
10694 If ALL, mark even excluded ticked and dormants as read."
10695   (interactive "P")
10696   (let ((articles (gnus-sorted-complement
10697                    (sort
10698                     (mapcar (lambda (h) (mail-header-number h))
10699                             gnus-newsgroup-headers)
10700                     '<)
10701                    (sort gnus-newsgroup-limit '<)))
10702         article)
10703     (setq gnus-newsgroup-unreads nil)
10704     (if all
10705         (setq gnus-newsgroup-dormant nil
10706               gnus-newsgroup-marked nil
10707               gnus-newsgroup-reads
10708               (nconc
10709                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10710                gnus-newsgroup-reads))
10711       (while (setq article (pop articles))
10712         (unless (or (memq article gnus-newsgroup-dormant)
10713                     (memq article gnus-newsgroup-marked))
10714           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10715
10716 (defun gnus-summary-limit (articles &optional pop)
10717   (if pop
10718       ;; We pop the previous limit off the stack and use that.
10719       (setq articles (car gnus-newsgroup-limits)
10720             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10721     ;; We use the new limit, so we push the old limit on the stack.
10722     (setq gnus-newsgroup-limits
10723           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10724   ;; Set the limit.
10725   (setq gnus-newsgroup-limit articles)
10726   (let ((total (length gnus-newsgroup-data))
10727         (data (gnus-data-find-list (gnus-summary-article-number)))
10728         found)
10729     ;; This will do all the work of generating the new summary buffer
10730     ;; according to the new limit.
10731     (gnus-summary-prepare)
10732     ;; Hide any threads, possibly.
10733     (and gnus-show-threads
10734          gnus-thread-hide-subtree
10735          (gnus-summary-hide-all-threads))
10736     ;; Try to return to the article you were at, or one in the
10737     ;; neighborhood.
10738     (if data
10739         ;; We try to find some article after the current one.
10740         (while data
10741           (and (gnus-summary-goto-subject
10742                 (gnus-data-number (car data)) nil t)
10743                (setq data nil
10744                      found t))
10745           (setq data (cdr data))))
10746     (or found
10747         ;; If there is no data, that means that we were after the last
10748         ;; article.  The same goes when we can't find any articles
10749         ;; after the current one.
10750         (progn
10751           (goto-char (point-max))
10752           (gnus-summary-find-prev)))
10753     ;; We return how many articles were removed from the summary
10754     ;; buffer as a result of the new limit.
10755     (- total (length gnus-newsgroup-data))))
10756
10757 (defsubst gnus-cut-thread (thread)
10758   "Go forwards in the thread until we find an article that we want to display."
10759   (when (eq gnus-fetch-old-headers 'some)
10760     ;; Deal with old-fetched headers.
10761     (while (and thread
10762                 (memq (mail-header-number (car thread)) 
10763                       gnus-newsgroup-ancient)
10764                 (<= (length (cdr thread)) 1))
10765       (setq thread (cadr thread))))
10766   ;; Deal with sparse threads.
10767   (when (or (eq gnus-build-sparse-threads 'some)
10768             (eq gnus-build-sparse-threads 'more))
10769     (while (and thread
10770                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10771                 (= (length (cdr thread)) 1))
10772       (setq thread (cadr thread))))
10773   thread)
10774
10775 (defun gnus-cut-threads (threads)
10776   "Cut off all uninteresting articles from the beginning of threads."
10777   (when (or (eq gnus-fetch-old-headers 'some)
10778             (eq gnus-build-sparse-threads 'some)
10779             (eq gnus-build-sparse-threads 'more))
10780     (let ((th threads))
10781       (while th
10782         (setcar th (gnus-cut-thread (car th)))
10783         (setq th (cdr th)))))
10784   ;; Remove nixed out threads.
10785   (delq nil threads))
10786
10787 (defun gnus-summary-initial-limit (&optional show-if-empty)
10788   "Figure out what the initial limit is supposed to be on group entry.
10789 This entails weeding out unwanted dormants, low-scored articles,
10790 fetch-old-headers verbiage, and so on."
10791   ;; Most groups have nothing to remove.
10792   (if (or gnus-inhibit-limiting
10793           (and (null gnus-newsgroup-dormant)
10794                (not (eq gnus-fetch-old-headers 'some))
10795                (null gnus-summary-expunge-below)
10796                (not (eq gnus-build-sparse-threads 'some))
10797                (not (eq gnus-build-sparse-threads 'more))
10798                (null gnus-thread-expunge-below)
10799                (not gnus-use-nocem)))
10800       () ; Do nothing.
10801     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10802     (setq gnus-newsgroup-limit nil)
10803     (mapatoms
10804      (lambda (node)
10805        (unless (car (symbol-value node))
10806          ;; These threads have no parents -- they are roots.
10807          (let ((nodes (cdr (symbol-value node)))
10808                thread)
10809            (while nodes
10810              (if (and gnus-thread-expunge-below
10811                       (< (gnus-thread-total-score (car nodes))
10812                          gnus-thread-expunge-below))
10813                  (gnus-expunge-thread (pop nodes))
10814                (setq thread (pop nodes))
10815                (gnus-summary-limit-children thread))))))
10816      gnus-newsgroup-dependencies)
10817     ;; If this limitation resulted in an empty group, we might
10818     ;; pop the previous limit and use it instead.
10819     (when (and (not gnus-newsgroup-limit)
10820                show-if-empty)
10821       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10822     gnus-newsgroup-limit))
10823
10824 (defun gnus-summary-limit-children (thread)
10825   "Return 1 if this subthread is visible and 0 if it is not."
10826   ;; First we get the number of visible children to this thread.  This
10827   ;; is done by recursing down the thread using this function, so this
10828   ;; will really go down to a leaf article first, before slowly
10829   ;; working its way up towards the root.
10830   (when thread
10831     (let ((children
10832            (if (cdr thread)
10833                (apply '+ (mapcar 'gnus-summary-limit-children
10834                                  (cdr thread)))
10835              0))
10836           (number (mail-header-number (car thread)))
10837           score)
10838       (if (or
10839            ;; If this article is dormant and has absolutely no visible
10840            ;; children, then this article isn't visible.
10841            (and (memq number gnus-newsgroup-dormant)
10842                 (= children 0))
10843            ;; If this is a "fetch-old-headered" and there is only one
10844            ;; visible child (or less), then we don't want this article.
10845            (and (eq gnus-fetch-old-headers 'some)
10846                 (memq number gnus-newsgroup-ancient)
10847                 (zerop children))
10848            ;; If this is a sparsely inserted article with no children,
10849            ;; we don't want it.
10850            (and (eq gnus-build-sparse-threads 'some)
10851                 (memq number gnus-newsgroup-sparse)
10852                 (zerop children))
10853            ;; If we use expunging, and this article is really
10854            ;; low-scored, then we don't want this article.
10855            (when (and gnus-summary-expunge-below
10856                       (< (setq score
10857                                (or (cdr (assq number gnus-newsgroup-scored))
10858                                    gnus-summary-default-score))
10859                          gnus-summary-expunge-below))
10860              ;; We increase the expunge-tally here, but that has
10861              ;; nothing to do with the limits, really.
10862              (incf gnus-newsgroup-expunged-tally)
10863              ;; We also mark as read here, if that's wanted.
10864              (when (and gnus-summary-mark-below
10865                         (< score gnus-summary-mark-below))
10866                (setq gnus-newsgroup-unreads
10867                      (delq number gnus-newsgroup-unreads))
10868                (if gnus-newsgroup-auto-expire
10869                    (push number gnus-newsgroup-expirable)
10870                  (push (cons number gnus-low-score-mark)
10871                        gnus-newsgroup-reads)))
10872              t)
10873            (and gnus-use-nocem
10874                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10875           ;; Nope, invisible article.
10876           0
10877         ;; Ok, this article is to be visible, so we add it to the limit
10878         ;; and return 1.
10879         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10880         1))))
10881
10882 (defun gnus-expunge-thread (thread)
10883   "Mark all articles in THREAD as read."
10884   (let* ((number (mail-header-number (car thread))))
10885     (incf gnus-newsgroup-expunged-tally)
10886     ;; We also mark as read here, if that's wanted.
10887     (setq gnus-newsgroup-unreads
10888           (delq number gnus-newsgroup-unreads))
10889     (if gnus-newsgroup-auto-expire
10890         (push number gnus-newsgroup-expirable)
10891       (push (cons number gnus-low-score-mark)
10892             gnus-newsgroup-reads)))
10893   ;; Go recursively through all subthreads.
10894   (mapcar 'gnus-expunge-thread (cdr thread)))
10895
10896 ;; Summary article oriented commands
10897
10898 (defun gnus-summary-refer-parent-article (n)
10899   "Refer parent article N times.
10900 The difference between N and the number of articles fetched is returned."
10901   (interactive "p")
10902   (gnus-set-global-variables)
10903   (while
10904       (and
10905        (> n 0)
10906        (let* ((header (gnus-summary-article-header))
10907               (ref
10908                ;; If we try to find the parent of the currently
10909                ;; displayed article, then we take a look at the actual
10910                ;; References header, since this is slightly more
10911                ;; reliable than the References field we got from the
10912                ;; server.
10913                (if (and (eq (mail-header-number header)
10914                             (cdr gnus-article-current))
10915                         (equal gnus-newsgroup-name
10916                                (car gnus-article-current)))
10917                    (save-excursion
10918                      (set-buffer gnus-original-article-buffer)
10919                      (nnheader-narrow-to-headers)
10920                      (prog1
10921                          (message-fetch-field "references")
10922                        (widen)))
10923                  ;; It's not the current article, so we take a bet on
10924                  ;; the value we got from the server.
10925                  (mail-header-references header))))
10926          (if (setq ref (or ref (mail-header-references header)))
10927              (or (gnus-summary-refer-article (gnus-parent-id ref))
10928                  (gnus-message 1 "Couldn't find parent"))
10929            (gnus-message 1 "No references in article %d"
10930                          (gnus-summary-article-number))
10931            nil)))
10932     (setq n (1- n)))
10933   (gnus-summary-position-point)
10934   n)
10935
10936 (defun gnus-summary-refer-references ()
10937   "Fetch all articles mentioned in the References header.
10938 Return how many articles were fetched."
10939   (interactive)
10940   (gnus-set-global-variables)
10941   (let ((ref (mail-header-references (gnus-summary-article-header)))
10942         (current (gnus-summary-article-number))
10943         (n 0))
10944     ;; For each Message-ID in the References header...
10945     (while (string-match "<[^>]*>" ref)
10946       (incf n)
10947       ;; ... fetch that article.
10948       (gnus-summary-refer-article
10949        (prog1 (match-string 0 ref)
10950          (setq ref (substring ref (match-end 0))))))
10951     (gnus-summary-goto-subject current)
10952     (gnus-summary-position-point)
10953     n))
10954
10955 (defun gnus-summary-refer-article (message-id)
10956   "Fetch an article specified by MESSAGE-ID."
10957   (interactive "sMessage-ID: ")
10958   (when (and (stringp message-id)
10959              (not (zerop (length message-id))))
10960     ;; Construct the correct Message-ID if necessary.
10961     ;; Suggested by tale@pawl.rpi.edu.
10962     (unless (string-match "^<" message-id)
10963       (setq message-id (concat "<" message-id)))
10964     (unless (string-match ">$" message-id)
10965       (setq message-id (concat message-id ">")))
10966     (let ((header (car (gnus-gethash message-id
10967                                      gnus-newsgroup-dependencies))))
10968       (if header
10969           ;; The article is present in the buffer, to we just go to it.
10970           (gnus-summary-goto-article (mail-header-number header) nil t)
10971         ;; We fetch the article
10972         (let ((gnus-override-method 
10973                (and (gnus-news-group-p gnus-newsgroup-name)
10974                     gnus-refer-article-method))
10975               number)
10976           ;; Start the special refer-article method, if necessary.
10977           (when (and gnus-refer-article-method
10978                      (gnus-news-group-p gnus-newsgroup-name))
10979             (gnus-check-server gnus-refer-article-method))
10980           ;; Fetch the header, and display the article.
10981           (if (setq number (gnus-summary-insert-subject message-id))
10982               (gnus-summary-select-article nil nil nil number)
10983             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10984
10985 (defun gnus-summary-enter-digest-group (&optional force)
10986   "Enter a digest group based on the current article."
10987   (interactive "P")
10988   (gnus-set-global-variables)
10989   (gnus-summary-select-article)
10990   (let ((name (format "%s-%d"
10991                       (gnus-group-prefixed-name
10992                        gnus-newsgroup-name (list 'nndoc ""))
10993                       gnus-current-article))
10994         (ogroup gnus-newsgroup-name)
10995         (case-fold-search t)
10996         (buf (current-buffer))
10997         dig)
10998     (save-excursion
10999       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11000       (insert-buffer-substring gnus-original-article-buffer)
11001       (narrow-to-region
11002        (goto-char (point-min))
11003        (or (search-forward "\n\n" nil t) (point)))
11004       (goto-char (point-min))
11005       (delete-matching-lines "^\\(Path\\):\\|^From ")
11006       (widen))
11007     (unwind-protect
11008         (if (gnus-group-read-ephemeral-group
11009              name `(nndoc ,name (nndoc-address
11010                                  ,(get-buffer dig))
11011                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11012             ;; Make all postings to this group go to the parent group.
11013             (nconc (gnus-info-params (gnus-get-info name))
11014                    (list (cons 'to-group ogroup)))
11015           ;; Couldn't select this doc group.
11016           (switch-to-buffer buf)
11017           (gnus-set-global-variables)
11018           (gnus-configure-windows 'summary)
11019           (gnus-message 3 "Article couldn't be entered?"))
11020       (kill-buffer dig))))
11021
11022 (defun gnus-summary-isearch-article (&optional regexp-p)
11023   "Do incremental search forward on the current article.
11024 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11025   (interactive "P")
11026   (gnus-set-global-variables)
11027   (gnus-summary-select-article)
11028   (gnus-configure-windows 'article)
11029   (gnus-eval-in-buffer-window
11030    gnus-article-buffer
11031    (goto-char (point-min))
11032    (isearch-forward regexp-p)))
11033
11034 (defun gnus-summary-search-article-forward (regexp &optional backward)
11035   "Search for an article containing REGEXP forward.
11036 If BACKWARD, search backward instead."
11037   (interactive
11038    (list (read-string
11039           (format "Search article %s (regexp%s): "
11040                   (if current-prefix-arg "backward" "forward")
11041                   (if gnus-last-search-regexp
11042                       (concat ", default " gnus-last-search-regexp)
11043                     "")))
11044          current-prefix-arg))
11045   (gnus-set-global-variables)
11046   (if (string-equal regexp "")
11047       (setq regexp (or gnus-last-search-regexp ""))
11048     (setq gnus-last-search-regexp regexp))
11049   (if (gnus-summary-search-article regexp backward)
11050       (gnus-article-set-window-start
11051        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
11052     (error "Search failed: \"%s\"" regexp)))
11053
11054 (defun gnus-summary-search-article-backward (regexp)
11055   "Search for an article containing REGEXP backward."
11056   (interactive
11057    (list (read-string
11058           (format "Search article backward (regexp%s): "
11059                   (if gnus-last-search-regexp
11060                       (concat ", default " gnus-last-search-regexp)
11061                     "")))))
11062   (gnus-summary-search-article-forward regexp 'backward))
11063
11064 (defun gnus-summary-search-article (regexp &optional backward)
11065   "Search for an article containing REGEXP.
11066 Optional argument BACKWARD means do search for backward.
11067 `gnus-select-article-hook' is not called during the search."
11068   (let ((gnus-select-article-hook nil)  ;Disable hook.
11069         (gnus-article-display-hook nil)
11070         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11071         (re-search
11072          (if backward
11073              're-search-backward 're-search-forward))
11074         (sum (current-buffer))
11075         (found nil))
11076     ;; Hidden thread subtrees must be searched, too.
11077     (gnus-summary-show-all-threads)
11078     (gnus-summary-select-article)
11079     (set-buffer gnus-article-buffer)
11080     (while (not found)
11081       (gnus-message 7 "Searching article: %d..." gnus-current-article)
11082       (if (if backward
11083               (re-search-backward regexp nil t)
11084             (re-search-forward regexp nil t))
11085           ;; We found the regexp.
11086           (progn
11087             (setq found 'found)
11088             (beginning-of-line)
11089             (set-window-start
11090              (get-buffer-window (current-buffer))
11091              (point)))
11092         ;; We didn't find it, so we go to the next article.
11093         (set-buffer sum)
11094         (if (not (if backward (gnus-summary-find-prev)
11095                    (gnus-summary-find-next)))
11096             ;; No more articles.
11097             (setq found t)
11098           ;; Select the next article and adjust point.
11099           (gnus-summary-select-article)
11100           (set-buffer gnus-article-buffer)
11101           (widen)
11102           (goto-char (if backward (point-max) (point-min))))))
11103     (set-buffer sum)
11104     ;; Return whether we found the regexp.
11105     (eq found 'found)))
11106
11107 (defun gnus-summary-find-matching (header regexp &optional backward unread
11108                                           not-case-fold)
11109   "Return a list of all articles that match REGEXP on HEADER.
11110 The search stars on the current article and goes forwards unless
11111 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11112 If UNREAD is non-nil, only unread articles will
11113 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11114 in the comparisons."
11115   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11116                 (gnus-data-find-list
11117                  (gnus-summary-article-number) (gnus-data-list backward))))
11118         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11119         (case-fold-search (not not-case-fold))
11120         articles d)
11121     (or (fboundp (intern (concat "mail-header-" header)))
11122         (error "%s is not a valid header" header))
11123     (while data
11124       (setq d (car data))
11125       (and (or (not unread)             ; We want all articles...
11126                (gnus-data-unread-p d))  ; Or just unreads.
11127            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11128            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11129            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11130       (setq data (cdr data)))
11131     (nreverse articles)))
11132
11133 (defun gnus-summary-execute-command (header regexp command &optional backward)
11134   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11135 If HEADER is an empty string (or nil), the match is done on the entire
11136 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11137   (interactive
11138    (list (let ((completion-ignore-case t))
11139            (completing-read
11140             "Header name: "
11141             (mapcar (lambda (string) (list string))
11142                     '("Number" "Subject" "From" "Lines" "Date"
11143                       "Message-ID" "Xref" "References" "Body"))
11144             nil 'require-match))
11145          (read-string "Regexp: ")
11146          (read-key-sequence "Command: ")
11147          current-prefix-arg))
11148   (when (equal header "Body")
11149     (setq header ""))
11150   (gnus-set-global-variables)
11151   ;; Hidden thread subtrees must be searched as well.
11152   (gnus-summary-show-all-threads)
11153   ;; We don't want to change current point nor window configuration.
11154   (save-excursion
11155     (save-window-excursion
11156       (gnus-message 6 "Executing %s..." (key-description command))
11157       ;; We'd like to execute COMMAND interactively so as to give arguments.
11158       (gnus-execute header regexp
11159                     `(lambda () (call-interactively ',(key-binding command)))
11160                     backward)
11161       (gnus-message 6 "Executing %s...done" (key-description command)))))
11162
11163 (defun gnus-summary-beginning-of-article ()
11164   "Scroll the article back to the beginning."
11165   (interactive)
11166   (gnus-set-global-variables)
11167   (gnus-summary-select-article)
11168   (gnus-configure-windows 'article)
11169   (gnus-eval-in-buffer-window
11170    gnus-article-buffer
11171    (widen)
11172    (goto-char (point-min))
11173    (and gnus-break-pages (gnus-narrow-to-page))))
11174
11175 (defun gnus-summary-end-of-article ()
11176   "Scroll to the end of the article."
11177   (interactive)
11178   (gnus-set-global-variables)
11179   (gnus-summary-select-article)
11180   (gnus-configure-windows 'article)
11181   (gnus-eval-in-buffer-window
11182    gnus-article-buffer
11183    (widen)
11184    (goto-char (point-max))
11185    (recenter -3)
11186    (and gnus-break-pages (gnus-narrow-to-page))))
11187
11188 (defun gnus-summary-show-article (&optional arg)
11189   "Force re-fetching of the current article.
11190 If ARG (the prefix) is non-nil, show the raw article without any
11191 article massaging functions being run."
11192   (interactive "P")
11193   (gnus-set-global-variables)
11194   (if (not arg)
11195       ;; Select the article the normal way.
11196       (gnus-summary-select-article nil 'force)
11197     ;; Bind the article treatment functions to nil.
11198     (let ((gnus-have-all-headers t)
11199           gnus-article-display-hook
11200           gnus-article-prepare-hook
11201           gnus-break-pages
11202           gnus-visual)
11203       (gnus-summary-select-article nil 'force)))
11204   (gnus-summary-goto-subject gnus-current-article)
11205 ;  (gnus-configure-windows 'article)
11206   (gnus-summary-position-point))
11207
11208 (defun gnus-summary-verbose-headers (&optional arg)
11209   "Toggle permanent full header display.
11210 If ARG is a positive number, turn header display on.
11211 If ARG is a negative number, turn header display off."
11212   (interactive "P")
11213   (gnus-set-global-variables)
11214   (gnus-summary-toggle-header arg)
11215   (setq gnus-show-all-headers
11216         (cond ((or (not (numberp arg))
11217                    (zerop arg))
11218                (not gnus-show-all-headers))
11219               ((natnump arg)
11220                t))))
11221
11222 (defun gnus-summary-toggle-header (&optional arg)
11223   "Show the headers if they are hidden, or hide them if they are shown.
11224 If ARG is a positive number, show the entire header.
11225 If ARG is a negative number, hide the unwanted header lines."
11226   (interactive "P")
11227   (gnus-set-global-variables)
11228   (save-excursion
11229     (set-buffer gnus-article-buffer)
11230     (let* ((buffer-read-only nil)
11231            (inhibit-point-motion-hooks t)
11232            (hidden (text-property-any
11233                     (goto-char (point-min)) (search-forward "\n\n")
11234                     'invisible t))
11235            e)
11236       (goto-char (point-min))
11237       (when (search-forward "\n\n" nil t)
11238         (delete-region (point-min) (1- (point))))
11239       (goto-char (point-min))
11240       (save-excursion
11241         (set-buffer gnus-original-article-buffer)
11242         (goto-char (point-min))
11243         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11244       (insert-buffer-substring gnus-original-article-buffer 1 e)
11245       (let ((gnus-inhibit-hiding t))
11246         (run-hooks 'gnus-article-display-hook))
11247       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11248           (gnus-article-hide-headers)))))
11249
11250 (defun gnus-summary-show-all-headers ()
11251   "Make all header lines visible."
11252   (interactive)
11253   (gnus-set-global-variables)
11254   (gnus-article-show-all-headers))
11255
11256 (defun gnus-summary-toggle-mime (&optional arg)
11257   "Toggle MIME processing.
11258 If ARG is a positive number, turn MIME processing on."
11259   (interactive "P")
11260   (gnus-set-global-variables)
11261   (setq gnus-show-mime
11262         (if (null arg) (not gnus-show-mime)
11263           (> (prefix-numeric-value arg) 0)))
11264   (gnus-summary-select-article t 'force))
11265
11266 (defun gnus-summary-caesar-message (&optional arg)
11267   "Caesar rotate the current article by 13.
11268 The numerical prefix specifies how manu places to rotate each letter
11269 forward."
11270   (interactive "P")
11271   (gnus-set-global-variables)
11272   (gnus-summary-select-article)
11273   (let ((mail-header-separator ""))
11274     (gnus-eval-in-buffer-window
11275      gnus-article-buffer
11276      (save-restriction
11277        (widen)
11278        (let ((start (window-start)))
11279          (news-caesar-buffer-body arg)
11280          (set-window-start (get-buffer-window (current-buffer)) start))))))
11281
11282 (defun gnus-summary-stop-page-breaking ()
11283   "Stop page breaking in the current article."
11284   (interactive)
11285   (gnus-set-global-variables)
11286   (gnus-summary-select-article)
11287   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11288
11289 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11290   "Move the current article to a different newsgroup.
11291 If N is a positive number, move the N next articles.
11292 If N is a negative number, move the N previous articles.
11293 If N is nil and any articles have been marked with the process mark,
11294 move those articles instead.
11295 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11296 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11297 re-spool using this method.
11298
11299 For this function to work, both the current newsgroup and the
11300 newsgroup that you want to move to have to support the `request-move'
11301 and `request-accept' functions."
11302   (interactive "P")
11303   (unless action (setq action 'move))
11304   (gnus-set-global-variables)
11305   ;; Check whether the source group supports the required functions.
11306   (cond ((and (eq action 'move)
11307               (not (gnus-check-backend-function
11308                     'request-move-article gnus-newsgroup-name)))
11309          (error "The current group does not support article moving"))
11310         ((and (eq action 'crosspost)
11311               (not (gnus-check-backend-function
11312                     'request-replace-article gnus-newsgroup-name)))
11313          (error "The current group does not support article editing")))
11314   (let ((articles (gnus-summary-work-articles n))
11315         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11316         (names '((move "Move" "Moving")
11317                  (copy "Copy" "Copying")
11318                  (crosspost "Crosspost" "Crossposting")))
11319         (copy-buf (save-excursion
11320                     (nnheader-set-temp-buffer " *copy article*")))
11321         art-group to-method new-xref article to-groups)
11322     (unless (assq action names)
11323       (error "Unknown action %s" action))
11324     ;; Read the newsgroup name.
11325     (when (and (not to-newsgroup)
11326                (not select-method))
11327       (setq to-newsgroup
11328             (gnus-read-move-group-name
11329              (cadr (assq action names))
11330              (symbol-value (intern (format "gnus-current-%s-group" action)))
11331              articles prefix))
11332       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11333     (setq to-method (or select-method 
11334                         (gnus-find-method-for-group to-newsgroup)))
11335     ;; Check the method we are to move this article to...
11336     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11337         (error "%s does not support article copying" (car to-method)))
11338     (or (gnus-check-server to-method)
11339         (error "Can't open server %s" (car to-method)))
11340     (gnus-message 6 "%s to %s: %s..."
11341                   (caddr (assq action names))
11342                   (or (car select-method) to-newsgroup) articles)
11343     (while articles
11344       (setq article (pop articles))
11345       (setq
11346        art-group
11347        (cond
11348         ;; Move the article.
11349         ((eq action 'move)
11350          (gnus-request-move-article
11351           article                       ; Article to move
11352           gnus-newsgroup-name           ; From newsgrouo
11353           (nth 1 (gnus-find-method-for-group
11354                   gnus-newsgroup-name)) ; Server
11355           (list 'gnus-request-accept-article
11356                 to-newsgroup (list 'quote select-method)
11357                 (not articles))         ; Accept form
11358           (not articles)))              ; Only save nov last time
11359         ;; Copy the article.
11360         ((eq action 'copy)
11361          (save-excursion
11362            (set-buffer copy-buf)
11363            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11364            (gnus-request-accept-article
11365             to-newsgroup select-method (not articles))))
11366         ;; Crosspost the article.
11367         ((eq action 'crosspost)
11368          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11369            (setq new-xref (concat gnus-newsgroup-name ":" article))
11370            (if (and xref (not (string= xref "")))
11371                (progn
11372                  (when (string-match "^Xref: " xref)
11373                    (setq xref (substring xref (match-end 0))))
11374                  (setq new-xref (concat xref " " new-xref)))
11375              (setq new-xref (concat (system-name) " " new-xref)))
11376            (save-excursion
11377              (set-buffer copy-buf)
11378              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11379              (nnheader-replace-header "xref" new-xref)
11380              (gnus-request-accept-article
11381               to-newsgroup select-method (not articles)))))))
11382       (if (not art-group)
11383           (gnus-message 1 "Couldn't %s article %s"
11384                         (cadr (assq action names)) article)
11385         (let* ((entry
11386                 (or
11387                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11388                  (gnus-gethash
11389                   (gnus-group-prefixed-name
11390                    (car art-group)
11391                    (or select-method 
11392                        (gnus-find-method-for-group to-newsgroup)))
11393                   gnus-newsrc-hashtb)))
11394                (info (nth 2 entry))
11395                (to-group (gnus-info-group info)))
11396           ;; Update the group that has been moved to.
11397           (when (and info
11398                      (memq action '(move copy)))
11399             (unless (member to-group to-groups)
11400               (push to-group to-groups))
11401
11402             (unless (memq article gnus-newsgroup-unreads)
11403               (gnus-info-set-read
11404                info (gnus-add-to-range (gnus-info-read info)
11405                                        (list (cdr art-group)))))
11406
11407             ;; Copy any marks over to the new group.
11408             (let ((marks gnus-article-mark-lists)
11409                   (to-article (cdr art-group)))
11410
11411               ;; See whether the article is to be put in the cache.
11412               (when gnus-use-cache
11413                 (gnus-cache-possibly-enter-article
11414                  to-group to-article
11415                  (let ((header (copy-sequence
11416                                 (gnus-summary-article-header article))))
11417                    (mail-header-set-number header to-article)
11418                    header)
11419                  (memq article gnus-newsgroup-marked)
11420                  (memq article gnus-newsgroup-dormant)
11421                  (memq article gnus-newsgroup-unreads)))
11422
11423               (while marks
11424                 (when (memq article (symbol-value
11425                                      (intern (format "gnus-newsgroup-%s"
11426                                                      (caar marks)))))
11427                   ;; If the other group is the same as this group,
11428                   ;; then we have to add the mark to the list.
11429                   (when (equal to-group gnus-newsgroup-name)
11430                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11431                          (cons to-article
11432                                (symbol-value
11433                                 (intern (format "gnus-newsgroup-%s"
11434                                                 (caar marks)))))))
11435                   ;; Copy mark to other group.
11436                   (gnus-add-marked-articles
11437                    to-group (cdar marks) (list to-article) info))
11438                 (setq marks (cdr marks)))))
11439
11440           ;; Update the Xref header in this article to point to
11441           ;; the new crossposted article we have just created.
11442           (when (eq action 'crosspost)
11443             (save-excursion
11444               (set-buffer copy-buf)
11445               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11446               (nnheader-replace-header
11447                "xref" (concat new-xref " " (gnus-group-prefixed-name
11448                                             (car art-group) to-method)
11449                               ":" (cdr art-group)))
11450               (gnus-request-replace-article
11451                article gnus-newsgroup-name (current-buffer)))))
11452
11453         (gnus-summary-goto-subject article)
11454         (when (eq action 'move)
11455           (gnus-summary-mark-article article gnus-canceled-mark)))
11456       (gnus-summary-remove-process-mark article))
11457     ;; Re-activate all groups that have been moved to.
11458     (while to-groups
11459       (gnus-activate-group (pop to-groups)))
11460     
11461     (gnus-kill-buffer copy-buf)
11462     (gnus-summary-position-point)
11463     (gnus-set-mode-line 'summary)))
11464
11465 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11466   "Move the current article to a different newsgroup.
11467 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11468 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11469 re-spool using this method."
11470   (interactive "P")
11471   (gnus-summary-move-article n nil select-method 'copy))
11472
11473 (defun gnus-summary-crosspost-article (&optional n)
11474   "Crosspost the current article to some other group."
11475   (interactive "P")
11476   (gnus-summary-move-article n nil nil 'crosspost))
11477
11478 (defvar gnus-summary-respool-default-method nil
11479   "Default method for respooling an article.  
11480 If nil, use to the current newsgroup method.")
11481
11482 (defun gnus-summary-respool-article (&optional n method)
11483   "Respool the current article.
11484 The article will be squeezed through the mail spooling process again,
11485 which means that it will be put in some mail newsgroup or other
11486 depending on `nnmail-split-methods'.
11487 If N is a positive number, respool the N next articles.
11488 If N is a negative number, respool the N previous articles.
11489 If N is nil and any articles have been marked with the process mark,
11490 respool those articles instead.
11491
11492 Respooling can be done both from mail groups and \"real\" newsgroups.
11493 In the former case, the articles in question will be moved from the
11494 current group into whatever groups they are destined to.  In the
11495 latter case, they will be copied into the relevant groups."
11496   (interactive 
11497    (list current-prefix-arg
11498          (let* ((methods (gnus-methods-using 'respool))
11499                 (methname
11500                  (symbol-name (or gnus-summary-respool-default-method
11501                                   (car (gnus-find-method-for-group
11502                                         gnus-newsgroup-name)))))
11503                 (method
11504                  (gnus-completing-read 
11505                   methname "What backend do you want to use when? "
11506                   methods nil t nil 'gnus-method-history))
11507                 ms)
11508            (cond
11509             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11510              (list (intern method) ""))
11511             ((= 1 (length ms))
11512              (car ms))
11513             (t
11514              (cdr (completing-read 
11515                    "Server name: "
11516                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11517   (gnus-set-global-variables)
11518   (unless method
11519     (error "No method given for respooling"))
11520   (if (assoc (symbol-name
11521               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11522              (gnus-methods-using 'respool))
11523       (gnus-summary-move-article n nil method)
11524     (gnus-summary-copy-article n nil method)))
11525
11526 (defun gnus-summary-import-article (file)
11527   "Import a random file into a mail newsgroup."
11528   (interactive "fImport file: ")
11529   (gnus-set-global-variables)
11530   (let ((group gnus-newsgroup-name)
11531         (now (current-time))
11532         atts lines)
11533     (or (gnus-check-backend-function 'request-accept-article group)
11534         (error "%s does not support article importing" group))
11535     (or (file-readable-p file)
11536         (not (file-regular-p file))
11537         (error "Can't read %s" file))
11538     (save-excursion
11539       (set-buffer (get-buffer-create " *import file*"))
11540       (buffer-disable-undo (current-buffer))
11541       (erase-buffer)
11542       (insert-file-contents file)
11543       (goto-char (point-min))
11544       (unless (nnheader-article-p)
11545         ;; This doesn't look like an article, so we fudge some headers.
11546         (setq atts (file-attributes file)
11547               lines (count-lines (point-min) (point-max)))
11548         (insert "From: " (read-string "From: ") "\n"
11549                 "Subject: " (read-string "Subject: ") "\n"
11550                 "Date: " (timezone-make-date-arpa-standard
11551                           (current-time-string (nth 5 atts))
11552                           (current-time-zone now)
11553                           (current-time-zone now)) "\n"
11554                 "Message-ID: " (gnus-inews-message-id) "\n"
11555                 "Lines: " (int-to-string lines) "\n"
11556                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11557       (gnus-request-accept-article group nil t)
11558       (kill-buffer (current-buffer)))))
11559
11560 (defun gnus-summary-expire-articles (&optional now)
11561   "Expire all articles that are marked as expirable in the current group."
11562   (interactive)
11563   (gnus-set-global-variables)
11564   (when (gnus-check-backend-function
11565          'request-expire-articles gnus-newsgroup-name)
11566     ;; This backend supports expiry.
11567     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11568            (expirable (if total
11569                           (gnus-list-of-read-articles gnus-newsgroup-name)
11570                         (setq gnus-newsgroup-expirable
11571                               (sort gnus-newsgroup-expirable '<))))
11572            (expiry-wait (if now 'immediate
11573                           (gnus-group-get-parameter
11574                            gnus-newsgroup-name 'expiry-wait)))
11575            es)
11576       (when expirable
11577         ;; There are expirable articles in this group, so we run them
11578         ;; through the expiry process.
11579         (gnus-message 6 "Expiring articles...")
11580         ;; The list of articles that weren't expired is returned.
11581         (if expiry-wait
11582             (let ((nnmail-expiry-wait-function nil)
11583                   (nnmail-expiry-wait expiry-wait))
11584               (setq es (gnus-request-expire-articles
11585                         expirable gnus-newsgroup-name)))
11586           (setq es (gnus-request-expire-articles
11587                     expirable gnus-newsgroup-name)))
11588         (or total (setq gnus-newsgroup-expirable es))
11589         ;; We go through the old list of expirable, and mark all
11590         ;; really expired articles as nonexistent.
11591         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11592           (let ((gnus-use-cache nil))
11593             (while expirable
11594               (unless (memq (car expirable) es)
11595                 (when (gnus-data-find (car expirable))
11596                   (gnus-summary-mark-article
11597                    (car expirable) gnus-canceled-mark)))
11598               (setq expirable (cdr expirable)))))
11599         (gnus-message 6 "Expiring articles...done")))))
11600
11601 (defun gnus-summary-expire-articles-now ()
11602   "Expunge all expirable articles in the current group.
11603 This means that *all* articles that are marked as expirable will be
11604 deleted forever, right now."
11605   (interactive)
11606   (gnus-set-global-variables)
11607   (or gnus-expert-user
11608       (gnus-y-or-n-p
11609        "Are you really, really, really sure you want to delete all these messages? ")
11610       (error "Phew!"))
11611   (gnus-summary-expire-articles t))
11612
11613 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11614 (defun gnus-summary-delete-article (&optional n)
11615   "Delete the N next (mail) articles.
11616 This command actually deletes articles.  This is not a marking
11617 command.  The article will disappear forever from your life, never to
11618 return.
11619 If N is negative, delete backwards.
11620 If N is nil and articles have been marked with the process mark,
11621 delete these instead."
11622   (interactive "P")
11623   (gnus-set-global-variables)
11624   (or (gnus-check-backend-function 'request-expire-articles
11625                                    gnus-newsgroup-name)
11626       (error "The current newsgroup does not support article deletion."))
11627   ;; Compute the list of articles to delete.
11628   (let ((articles (gnus-summary-work-articles n))
11629         not-deleted)
11630     (if (and gnus-novice-user
11631              (not (gnus-y-or-n-p
11632                    (format "Do you really want to delete %s forever? "
11633                            (if (> (length articles) 1) 
11634                                (format "these %s articles" (length articles))
11635                              "this article")))))
11636         ()
11637       ;; Delete the articles.
11638       (setq not-deleted (gnus-request-expire-articles
11639                          articles gnus-newsgroup-name 'force))
11640       (while articles
11641         (gnus-summary-remove-process-mark (car articles))
11642         ;; The backend might not have been able to delete the article
11643         ;; after all.
11644         (or (memq (car articles) not-deleted)
11645             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11646         (setq articles (cdr articles))))
11647     (gnus-summary-position-point)
11648     (gnus-set-mode-line 'summary)
11649     not-deleted))
11650
11651 (defun gnus-summary-edit-article (&optional force)
11652   "Enter into a buffer and edit the current article.
11653 This will have permanent effect only in mail groups.
11654 If FORCE is non-nil, allow editing of articles even in read-only
11655 groups."
11656   (interactive "P")
11657   (save-excursion
11658     (set-buffer gnus-summary-buffer)
11659     (gnus-set-global-variables)
11660     (when (and (not force)
11661                (gnus-group-read-only-p))
11662       (error "The current newsgroup does not support article editing."))
11663     (gnus-summary-select-article t nil t)
11664     (gnus-configure-windows 'article)
11665     (select-window (get-buffer-window gnus-article-buffer))
11666     (gnus-message 6 "C-c C-c to end edits")
11667     (setq buffer-read-only nil)
11668     (text-mode)
11669     (use-local-map (copy-keymap (current-local-map)))
11670     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11671     (buffer-enable-undo)
11672     (widen)
11673     (goto-char (point-min))
11674     (search-forward "\n\n" nil t)))
11675
11676 (defun gnus-summary-edit-article-done ()
11677   "Make edits to the current article permanent."
11678   (interactive)
11679   (if (gnus-group-read-only-p)
11680       (progn
11681         (gnus-summary-edit-article-postpone)
11682         (gnus-error
11683          1 "The current newsgroup does not support article editing."))
11684     (let ((buf (format "%s" (buffer-string))))
11685       (erase-buffer)
11686       (insert buf)
11687       (if (not (gnus-request-replace-article
11688                 (cdr gnus-article-current) (car gnus-article-current)
11689                 (current-buffer)))
11690           (error "Couldn't replace article.")
11691         (gnus-article-mode)
11692         (use-local-map gnus-article-mode-map)
11693         (setq buffer-read-only t)
11694         (buffer-disable-undo (current-buffer))
11695         (gnus-configure-windows 'summary)
11696         (gnus-summary-update-article (cdr gnus-article-current))
11697         (when gnus-use-cache
11698           (gnus-cache-update-article 
11699            (cdr gnus-article-current) (car gnus-article-current)))
11700         (when gnus-keep-backlog
11701           (gnus-backlog-remove-article 
11702            (car gnus-article-current) (cdr gnus-article-current))))
11703       (save-excursion
11704         (when (get-buffer gnus-original-article-buffer)
11705           (set-buffer gnus-original-article-buffer)
11706           (setq gnus-original-article nil)))
11707       (setq gnus-article-current nil
11708             gnus-current-article nil)
11709       (run-hooks 'gnus-article-display-hook)
11710       (and (gnus-visual-p 'summary-highlight 'highlight)
11711            (run-hooks 'gnus-visual-mark-article-hook)))))
11712
11713 (defun gnus-summary-edit-article-postpone ()
11714   "Postpone changes to the current article."
11715   (interactive)
11716   (gnus-article-mode)
11717   (use-local-map gnus-article-mode-map)
11718   (setq buffer-read-only t)
11719   (buffer-disable-undo (current-buffer))
11720   (gnus-configure-windows 'summary)
11721   (and (gnus-visual-p 'summary-highlight 'highlight)
11722        (run-hooks 'gnus-visual-mark-article-hook)))
11723
11724 (defun gnus-summary-respool-query ()
11725   "Query where the respool algorithm would put this article."
11726   (interactive)
11727   (gnus-set-global-variables)
11728   (gnus-summary-select-article)
11729   (save-excursion
11730     (set-buffer gnus-article-buffer)
11731     (save-restriction
11732       (goto-char (point-min))
11733       (search-forward "\n\n")
11734       (narrow-to-region (point-min) (point))
11735       (pp-eval-expression
11736        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11737
11738 ;; Summary score commands.
11739
11740 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11741
11742 (defun gnus-summary-raise-score (n)
11743   "Raise the score of the current article by N."
11744   (interactive "p")
11745   (gnus-set-global-variables)
11746   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11747
11748 (defun gnus-summary-set-score (n)
11749   "Set the score of the current article to N."
11750   (interactive "p")
11751   (gnus-set-global-variables)
11752   (save-excursion
11753     (gnus-summary-show-thread)
11754     (let ((buffer-read-only nil))
11755       ;; Set score.
11756       (gnus-summary-update-mark
11757        (if (= n (or gnus-summary-default-score 0)) ? 
11758          (if (< n (or gnus-summary-default-score 0))
11759              gnus-score-below-mark gnus-score-over-mark)) 'score))
11760     (let* ((article (gnus-summary-article-number))
11761            (score (assq article gnus-newsgroup-scored)))
11762       (if score (setcdr score n)
11763         (setq gnus-newsgroup-scored
11764               (cons (cons article n) gnus-newsgroup-scored))))
11765     (gnus-summary-update-line)))
11766
11767 (defun gnus-summary-current-score ()
11768   "Return the score of the current article."
11769   (interactive)
11770   (gnus-set-global-variables)
11771   (gnus-message 1 "%s" (gnus-summary-article-score)))
11772
11773 ;; Summary marking commands.
11774
11775 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11776   "Mark articles which has the same subject as read, and then select the next.
11777 If UNMARK is positive, remove any kind of mark.
11778 If UNMARK is negative, tick articles."
11779   (interactive "P")
11780   (gnus-set-global-variables)
11781   (if unmark
11782       (setq unmark (prefix-numeric-value unmark)))
11783   (let ((count
11784          (gnus-summary-mark-same-subject
11785           (gnus-summary-article-subject) unmark)))
11786     ;; Select next unread article.  If auto-select-same mode, should
11787     ;; select the first unread article.
11788     (gnus-summary-next-article t (and gnus-auto-select-same
11789                                       (gnus-summary-article-subject)))
11790     (gnus-message 7 "%d article%s marked as %s"
11791                   count (if (= count 1) " is" "s are")
11792                   (if unmark "unread" "read"))))
11793
11794 (defun gnus-summary-kill-same-subject (&optional unmark)
11795   "Mark articles which has the same subject as read.
11796 If UNMARK is positive, remove any kind of mark.
11797 If UNMARK is negative, tick articles."
11798   (interactive "P")
11799   (gnus-set-global-variables)
11800   (if unmark
11801       (setq unmark (prefix-numeric-value unmark)))
11802   (let ((count
11803          (gnus-summary-mark-same-subject
11804           (gnus-summary-article-subject) unmark)))
11805     ;; If marked as read, go to next unread subject.
11806     (if (null unmark)
11807         ;; Go to next unread subject.
11808         (gnus-summary-next-subject 1 t))
11809     (gnus-message 7 "%d articles are marked as %s"
11810                   count (if unmark "unread" "read"))))
11811
11812 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11813   "Mark articles with same SUBJECT as read, and return marked number.
11814 If optional argument UNMARK is positive, remove any kinds of marks.
11815 If optional argument UNMARK is negative, mark articles as unread instead."
11816   (let ((count 1))
11817     (save-excursion
11818       (cond
11819        ((null unmark)                   ; Mark as read.
11820         (while (and
11821                 (progn
11822                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11823                   (gnus-summary-show-thread) t)
11824                 (gnus-summary-find-subject subject))
11825           (setq count (1+ count))))
11826        ((> unmark 0)                    ; Tick.
11827         (while (and
11828                 (progn
11829                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11830                   (gnus-summary-show-thread) t)
11831                 (gnus-summary-find-subject subject))
11832           (setq count (1+ count))))
11833        (t                               ; Mark as unread.
11834         (while (and
11835                 (progn
11836                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11837                   (gnus-summary-show-thread) t)
11838                 (gnus-summary-find-subject subject))
11839           (setq count (1+ count)))))
11840       (gnus-set-mode-line 'summary)
11841       ;; Return the number of marked articles.
11842       count)))
11843
11844 (defun gnus-summary-mark-as-processable (n &optional unmark)
11845   "Set the process mark on the next N articles.
11846 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11847 the process mark instead.  The difference between N and the actual
11848 number of articles marked is returned."
11849   (interactive "p")
11850   (gnus-set-global-variables)
11851   (let ((backward (< n 0))
11852         (n (abs n)))
11853     (while (and
11854             (> n 0)
11855             (if unmark
11856                 (gnus-summary-remove-process-mark
11857                  (gnus-summary-article-number))
11858               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11859             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11860       (setq n (1- n)))
11861     (if (/= 0 n) (gnus-message 7 "No more articles"))
11862     (gnus-summary-recenter)
11863     (gnus-summary-position-point)
11864     n))
11865
11866 (defun gnus-summary-unmark-as-processable (n)
11867   "Remove the process mark from the next N articles.
11868 If N is negative, mark backward instead.  The difference between N and
11869 the actual number of articles marked is returned."
11870   (interactive "p")
11871   (gnus-set-global-variables)
11872   (gnus-summary-mark-as-processable n t))
11873
11874 (defun gnus-summary-unmark-all-processable ()
11875   "Remove the process mark from all articles."
11876   (interactive)
11877   (gnus-set-global-variables)
11878   (save-excursion
11879     (while gnus-newsgroup-processable
11880       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11881   (gnus-summary-position-point))
11882
11883 (defun gnus-summary-mark-as-expirable (n)
11884   "Mark N articles forward as expirable.
11885 If N is negative, mark backward instead.  The difference between N and
11886 the actual number of articles marked is returned."
11887   (interactive "p")
11888   (gnus-set-global-variables)
11889   (gnus-summary-mark-forward n gnus-expirable-mark))
11890
11891 (defun gnus-summary-mark-article-as-replied (article)
11892   "Mark ARTICLE replied and update the summary line."
11893   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11894   (let ((buffer-read-only nil))
11895     (when (gnus-summary-goto-subject article)
11896       (gnus-summary-update-secondary-mark article))))
11897
11898 (defun gnus-summary-set-bookmark (article)
11899   "Set a bookmark in current article."
11900   (interactive (list (gnus-summary-article-number)))
11901   (gnus-set-global-variables)
11902   (if (or (not (get-buffer gnus-article-buffer))
11903           (not gnus-current-article)
11904           (not gnus-article-current)
11905           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11906       (error "No current article selected"))
11907   ;; Remove old bookmark, if one exists.
11908   (let ((old (assq article gnus-newsgroup-bookmarks)))
11909     (if old (setq gnus-newsgroup-bookmarks
11910                   (delq old gnus-newsgroup-bookmarks))))
11911   ;; Set the new bookmark, which is on the form
11912   ;; (article-number . line-number-in-body).
11913   (setq gnus-newsgroup-bookmarks
11914         (cons
11915          (cons article
11916                (save-excursion
11917                  (set-buffer gnus-article-buffer)
11918                  (count-lines
11919                   (min (point)
11920                        (save-excursion
11921                          (goto-char (point-min))
11922                          (search-forward "\n\n" nil t)
11923                          (point)))
11924                   (point))))
11925          gnus-newsgroup-bookmarks))
11926   (gnus-message 6 "A bookmark has been added to the current article."))
11927
11928 (defun gnus-summary-remove-bookmark (article)
11929   "Remove the bookmark from the current article."
11930   (interactive (list (gnus-summary-article-number)))
11931   (gnus-set-global-variables)
11932   ;; Remove old bookmark, if one exists.
11933   (let ((old (assq article gnus-newsgroup-bookmarks)))
11934     (if old
11935         (progn
11936           (setq gnus-newsgroup-bookmarks
11937                 (delq old gnus-newsgroup-bookmarks))
11938           (gnus-message 6 "Removed bookmark."))
11939       (gnus-message 6 "No bookmark in current article."))))
11940
11941 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11942 (defun gnus-summary-mark-as-dormant (n)
11943   "Mark N articles forward as dormant.
11944 If N is negative, mark backward instead.  The difference between N and
11945 the actual number of articles marked is returned."
11946   (interactive "p")
11947   (gnus-set-global-variables)
11948   (gnus-summary-mark-forward n gnus-dormant-mark))
11949
11950 (defun gnus-summary-set-process-mark (article)
11951   "Set the process mark on ARTICLE and update the summary line."
11952   (setq gnus-newsgroup-processable
11953         (cons article
11954               (delq article gnus-newsgroup-processable)))
11955   (when (gnus-summary-goto-subject article)
11956     (gnus-summary-show-thread)
11957     (gnus-summary-update-secondary-mark article)))
11958
11959 (defun gnus-summary-remove-process-mark (article)
11960   "Remove the process mark from ARTICLE and update the summary line."
11961   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11962   (when (gnus-summary-goto-subject article)
11963     (gnus-summary-show-thread)
11964     (gnus-summary-update-secondary-mark article)))
11965
11966 (defun gnus-summary-set-saved-mark (article)
11967   "Set the process mark on ARTICLE and update the summary line."
11968   (push article gnus-newsgroup-saved)
11969   (when (gnus-summary-goto-subject article)
11970     (gnus-summary-update-secondary-mark article)))
11971
11972 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11973   "Mark N articles as read forwards.
11974 If N is negative, mark backwards instead.
11975 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11976 marked as unread.
11977 The difference between N and the actual number of articles marked is
11978 returned."
11979   (interactive "p")
11980   (gnus-set-global-variables)
11981   (let ((backward (< n 0))
11982         (gnus-summary-goto-unread
11983          (and gnus-summary-goto-unread
11984               (not (eq gnus-summary-goto-unread 'never))
11985               (not (memq mark (list gnus-unread-mark
11986                                     gnus-ticked-mark gnus-dormant-mark)))))
11987         (n (abs n))
11988         (mark (or mark gnus-del-mark)))
11989     (while (and (> n 0)
11990                 (gnus-summary-mark-article nil mark no-expire)
11991                 (zerop (gnus-summary-next-subject
11992                         (if backward -1 1)
11993                         (and gnus-summary-goto-unread
11994                              (not (eq gnus-summary-goto-unread 'never)))
11995                         t)))
11996       (setq n (1- n)))
11997     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11998     (gnus-summary-recenter)
11999     (gnus-summary-position-point)
12000     (gnus-set-mode-line 'summary)
12001     n))
12002
12003 (defun gnus-summary-mark-article-as-read (mark)
12004   "Mark the current article quickly as read with MARK."
12005   (let ((article (gnus-summary-article-number)))
12006     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12007     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12008     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12009     (setq gnus-newsgroup-reads
12010           (cons (cons article mark) gnus-newsgroup-reads))
12011     ;; Possibly remove from cache, if that is used.
12012     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12013     ;; Allow the backend to change the mark.
12014     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12015     ;; Check for auto-expiry.
12016     (when (and gnus-newsgroup-auto-expire
12017                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12018                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12019                    (= mark gnus-ancient-mark)
12020                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12021       (setq mark gnus-expirable-mark)
12022       (push article gnus-newsgroup-expirable))
12023     ;; Set the mark in the buffer.
12024     (gnus-summary-update-mark mark 'unread)
12025     t))
12026
12027 (defun gnus-summary-mark-article-as-unread (mark)
12028   "Mark the current article quickly as unread with MARK."
12029   (let ((article (gnus-summary-article-number)))
12030     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12031     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12032     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12033     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12034     (cond ((= mark gnus-ticked-mark)
12035            (push article gnus-newsgroup-marked))
12036           ((= mark gnus-dormant-mark)
12037            (push article gnus-newsgroup-dormant))
12038           (t
12039            (push article gnus-newsgroup-unreads)))
12040     (setq gnus-newsgroup-reads
12041           (delq (assq article gnus-newsgroup-reads)
12042                 gnus-newsgroup-reads))
12043
12044     ;; See whether the article is to be put in the cache.
12045     (and gnus-use-cache
12046          (vectorp (gnus-summary-article-header article))
12047          (save-excursion
12048            (gnus-cache-possibly-enter-article
12049             gnus-newsgroup-name article
12050             (gnus-summary-article-header article)
12051             (= mark gnus-ticked-mark)
12052             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12053
12054     ;; Fix the mark.
12055     (gnus-summary-update-mark mark 'unread)
12056     t))
12057
12058 (defun gnus-summary-mark-article (&optional article mark no-expire)
12059   "Mark ARTICLE with MARK.  MARK can be any character.
12060 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12061 `??' (dormant) and `?E' (expirable).
12062 If MARK is nil, then the default character `?D' is used.
12063 If ARTICLE is nil, then the article on the current line will be
12064 marked."
12065   ;; The mark might be a string.
12066   (and (stringp mark)
12067        (setq mark (aref mark 0)))
12068   ;; If no mark is given, then we check auto-expiring.
12069   (and (not no-expire)
12070        gnus-newsgroup-auto-expire
12071        (or (not mark)
12072            (and (numberp mark)
12073                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12074                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12075                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12076        (setq mark gnus-expirable-mark))
12077   (let* ((mark (or mark gnus-del-mark))
12078          (article (or article (gnus-summary-article-number))))
12079     (or article (error "No article on current line"))
12080     (if (or (= mark gnus-unread-mark)
12081             (= mark gnus-ticked-mark)
12082             (= mark gnus-dormant-mark))
12083         (gnus-mark-article-as-unread article mark)
12084       (gnus-mark-article-as-read article mark))
12085
12086     ;; See whether the article is to be put in the cache.
12087     (and gnus-use-cache
12088          (not (= mark gnus-canceled-mark))
12089          (vectorp (gnus-summary-article-header article))
12090          (save-excursion
12091            (gnus-cache-possibly-enter-article
12092             gnus-newsgroup-name article
12093             (gnus-summary-article-header article)
12094             (= mark gnus-ticked-mark)
12095             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12096
12097     (if (gnus-summary-goto-subject article nil t)
12098         (let ((buffer-read-only nil))
12099           (gnus-summary-show-thread)
12100           ;; Fix the mark.
12101           (gnus-summary-update-mark mark 'unread)
12102           t))))
12103
12104 (defun gnus-summary-update-secondary-mark (article)
12105   "Update the secondary (read, process, cache) mark."
12106   (gnus-summary-update-mark
12107    (cond ((memq article gnus-newsgroup-processable)
12108           gnus-process-mark)
12109          ((memq article gnus-newsgroup-cached)
12110           gnus-cached-mark)
12111          ((memq article gnus-newsgroup-replied)
12112           gnus-replied-mark)
12113          ((memq article gnus-newsgroup-saved)
12114           gnus-saved-mark)
12115          (t gnus-unread-mark))
12116    'replied)
12117   (when (gnus-visual-p 'summary-highlight 'highlight)
12118     (run-hooks 'gnus-summary-update-hook))
12119   t)
12120
12121 (defun gnus-summary-update-mark (mark type)
12122   (beginning-of-line)
12123   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12124         (buffer-read-only nil))
12125     (when (and forward
12126                (<= (+ forward (point)) (point-max)))
12127       ;; Go to the right position on the line.
12128       (goto-char (+ forward (point)))
12129       ;; Replace the old mark with the new mark.
12130       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12131       ;; Optionally update the marks by some user rule.
12132       (when (eq type 'unread)
12133         (gnus-data-set-mark
12134          (gnus-data-find (gnus-summary-article-number)) mark)
12135         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12136
12137 (defun gnus-mark-article-as-read (article &optional mark)
12138   "Enter ARTICLE in the pertinent lists and remove it from others."
12139   ;; Make the article expirable.
12140   (let ((mark (or mark gnus-del-mark)))
12141     (if (= mark gnus-expirable-mark)
12142         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12143       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12144     ;; Remove from unread and marked lists.
12145     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12146     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12147     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12148     (push (cons article mark) gnus-newsgroup-reads)
12149     ;; Possibly remove from cache, if that is used.
12150     (when gnus-use-cache
12151       (gnus-cache-enter-remove-article article))))
12152
12153 (defun gnus-mark-article-as-unread (article &optional mark)
12154   "Enter ARTICLE in the pertinent lists and remove it from others."
12155   (let ((mark (or mark gnus-ticked-mark)))
12156     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12157     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12158     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12159     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12160     (cond ((= mark gnus-ticked-mark)
12161            (push article gnus-newsgroup-marked))
12162           ((= mark gnus-dormant-mark)
12163            (push article gnus-newsgroup-dormant))
12164           (t
12165            (push article gnus-newsgroup-unreads)))
12166     (setq gnus-newsgroup-reads
12167           (delq (assq article gnus-newsgroup-reads)
12168                 gnus-newsgroup-reads))))
12169
12170 (defalias 'gnus-summary-mark-as-unread-forward
12171   'gnus-summary-tick-article-forward)
12172 (make-obsolete 'gnus-summary-mark-as-unread-forward
12173                'gnus-summary-tick-article-forward)
12174 (defun gnus-summary-tick-article-forward (n)
12175   "Tick N articles forwards.
12176 If N is negative, tick backwards instead.
12177 The difference between N and the number of articles ticked is returned."
12178   (interactive "p")
12179   (gnus-summary-mark-forward n gnus-ticked-mark))
12180
12181 (defalias 'gnus-summary-mark-as-unread-backward
12182   'gnus-summary-tick-article-backward)
12183 (make-obsolete 'gnus-summary-mark-as-unread-backward
12184                'gnus-summary-tick-article-backward)
12185 (defun gnus-summary-tick-article-backward (n)
12186   "Tick N articles backwards.
12187 The difference between N and the number of articles ticked is returned."
12188   (interactive "p")
12189   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12190
12191 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12192 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12193 (defun gnus-summary-tick-article (&optional article clear-mark)
12194   "Mark current article as unread.
12195 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12196 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12197   (interactive)
12198   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12199                                        gnus-ticked-mark)))
12200
12201 (defun gnus-summary-mark-as-read-forward (n)
12202   "Mark N articles as read forwards.
12203 If N is negative, mark backwards instead.
12204 The difference between N and the actual number of articles marked is
12205 returned."
12206   (interactive "p")
12207   (gnus-summary-mark-forward n gnus-del-mark t))
12208
12209 (defun gnus-summary-mark-as-read-backward (n)
12210   "Mark the N articles as read backwards.
12211 The difference between N and the actual number of articles marked is
12212 returned."
12213   (interactive "p")
12214   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12215
12216 (defun gnus-summary-mark-as-read (&optional article mark)
12217   "Mark current article as read.
12218 ARTICLE specifies the article to be marked as read.
12219 MARK specifies a string to be inserted at the beginning of the line."
12220   (gnus-summary-mark-article article mark))
12221
12222 (defun gnus-summary-clear-mark-forward (n)
12223   "Clear marks from N articles forward.
12224 If N is negative, clear backward instead.
12225 The difference between N and the number of marks cleared is returned."
12226   (interactive "p")
12227   (gnus-summary-mark-forward n gnus-unread-mark))
12228
12229 (defun gnus-summary-clear-mark-backward (n)
12230   "Clear marks from N articles backward.
12231 The difference between N and the number of marks cleared is returned."
12232   (interactive "p")
12233   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12234
12235 (defun gnus-summary-mark-unread-as-read ()
12236   "Intended to be used by `gnus-summary-mark-article-hook'."
12237   (when (memq gnus-current-article gnus-newsgroup-unreads)
12238     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12239
12240 (defun gnus-summary-mark-read-and-unread-as-read ()
12241   "Intended to be used by `gnus-summary-mark-article-hook'."
12242   (let ((mark (gnus-summary-article-mark)))
12243     (when (or (gnus-unread-mark-p mark)
12244               (gnus-read-mark-p mark))
12245       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12246
12247 (defun gnus-summary-mark-region-as-read (point mark all)
12248   "Mark all unread articles between point and mark as read.
12249 If given a prefix, mark all articles between point and mark as read,
12250 even ticked and dormant ones."
12251   (interactive "r\nP")
12252   (save-excursion
12253     (let (article)
12254       (goto-char point)
12255       (beginning-of-line)
12256       (while (and
12257               (< (point) mark)
12258               (progn
12259                 (when (or all
12260                           (memq (setq article (gnus-summary-article-number))
12261                                 gnus-newsgroup-unreads))
12262                   (gnus-summary-mark-article article gnus-del-mark))
12263                 t)
12264               (gnus-summary-find-next))))))
12265
12266 (defun gnus-summary-mark-below (score mark)
12267   "Mark articles with score less than SCORE with MARK."
12268   (interactive "P\ncMark: ")
12269   (gnus-set-global-variables)
12270   (setq score (if score
12271                   (prefix-numeric-value score)
12272                 (or gnus-summary-default-score 0)))
12273   (save-excursion
12274     (set-buffer gnus-summary-buffer)
12275     (goto-char (point-min))
12276     (while 
12277         (progn
12278           (and (< (gnus-summary-article-score) score)
12279                (gnus-summary-mark-article nil mark))
12280           (gnus-summary-find-next)))))
12281
12282 (defun gnus-summary-kill-below (&optional score)
12283   "Mark articles with score below SCORE as read."
12284   (interactive "P")
12285   (gnus-set-global-variables)
12286   (gnus-summary-mark-below score gnus-killed-mark))
12287
12288 (defun gnus-summary-clear-above (&optional score)
12289   "Clear all marks from articles with score above SCORE."
12290   (interactive "P")
12291   (gnus-set-global-variables)
12292   (gnus-summary-mark-above score gnus-unread-mark))
12293
12294 (defun gnus-summary-tick-above (&optional score)
12295   "Tick all articles with score above SCORE."
12296   (interactive "P")
12297   (gnus-set-global-variables)
12298   (gnus-summary-mark-above score gnus-ticked-mark))
12299
12300 (defun gnus-summary-mark-above (score mark)
12301   "Mark articles with score over SCORE with MARK."
12302   (interactive "P\ncMark: ")
12303   (gnus-set-global-variables)
12304   (setq score (if score
12305                   (prefix-numeric-value score)
12306                 (or gnus-summary-default-score 0)))
12307   (save-excursion
12308     (set-buffer gnus-summary-buffer)
12309     (goto-char (point-min))
12310     (while (and (progn
12311                   (if (> (gnus-summary-article-score) score)
12312                       (gnus-summary-mark-article nil mark))
12313                   t)
12314                 (gnus-summary-find-next)))))
12315
12316 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12317 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12318 (defun gnus-summary-limit-include-expunged ()
12319   "Display all the hidden articles that were expunged for low scores."
12320   (interactive)
12321   (gnus-set-global-variables)
12322   (let ((buffer-read-only nil))
12323     (let ((scored gnus-newsgroup-scored)
12324           headers h)
12325       (while scored
12326         (or (gnus-summary-goto-subject (caar scored))
12327             (and (setq h (gnus-summary-article-header (caar scored)))
12328                  (< (cdar scored) gnus-summary-expunge-below)
12329                  (setq headers (cons h headers))))
12330         (setq scored (cdr scored)))
12331       (or headers (error "No expunged articles hidden."))
12332       (goto-char (point-min))
12333       (gnus-summary-prepare-unthreaded (nreverse headers)))
12334     (goto-char (point-min))
12335     (gnus-summary-position-point)))
12336
12337 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12338   "Mark all articles not marked as unread in this newsgroup as read.
12339 If prefix argument ALL is non-nil, all articles are marked as read.
12340 If QUIETLY is non-nil, no questions will be asked.
12341 If TO-HERE is non-nil, it should be a point in the buffer.  All
12342 articles before this point will be marked as read.
12343 The number of articles marked as read is returned."
12344   (interactive "P")
12345   (gnus-set-global-variables)
12346   (prog1
12347       (if (or quietly
12348               (not gnus-interactive-catchup) ;Without confirmation?
12349               gnus-expert-user
12350               (gnus-y-or-n-p
12351                (if all
12352                    "Mark absolutely all articles as read? "
12353                  "Mark all unread articles as read? ")))
12354           (if (and not-mark
12355                    (not gnus-newsgroup-adaptive)
12356                    (not gnus-newsgroup-auto-expire))
12357               (progn
12358                 (when all
12359                   (setq gnus-newsgroup-marked nil
12360                         gnus-newsgroup-dormant nil))
12361                 (setq gnus-newsgroup-unreads nil))
12362             ;; We actually mark all articles as canceled, which we
12363             ;; have to do when using auto-expiry or adaptive scoring.
12364             (gnus-summary-show-all-threads)
12365             (if (gnus-summary-first-subject (not all))
12366                 (while (and
12367                         (if to-here (< (point) to-here) t)
12368                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12369                         (gnus-summary-find-next (not all)))))
12370             (unless to-here
12371               (setq gnus-newsgroup-unreads nil))
12372             (gnus-set-mode-line 'summary)))
12373     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12374       (if (and (not to-here) (eq 'nnvirtual (car method)))
12375           (nnvirtual-catchup-group
12376            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12377     (gnus-summary-position-point)))
12378
12379 (defun gnus-summary-catchup-to-here (&optional all)
12380   "Mark all unticked articles before the current one as read.
12381 If ALL is non-nil, also mark ticked and dormant articles as read."
12382   (interactive "P")
12383   (gnus-set-global-variables)
12384   (save-excursion
12385     (let ((beg (point)))
12386       ;; We check that there are unread articles.
12387       (when (or all (gnus-summary-find-prev))
12388         (gnus-summary-catchup all t beg))))
12389   (gnus-summary-position-point))
12390
12391 (defun gnus-summary-catchup-all (&optional quietly)
12392   "Mark all articles in this newsgroup as read."
12393   (interactive "P")
12394   (gnus-set-global-variables)
12395   (gnus-summary-catchup t quietly))
12396
12397 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12398   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12399 If prefix argument ALL is non-nil, all articles are marked as read."
12400   (interactive "P")
12401   (gnus-set-global-variables)
12402   (gnus-summary-catchup all quietly nil 'fast)
12403   ;; Select next newsgroup or exit.
12404   (if (eq gnus-auto-select-next 'quietly)
12405       (gnus-summary-next-group nil)
12406     (gnus-summary-exit)))
12407
12408 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12409   "Mark all articles in this newsgroup as read, and then exit."
12410   (interactive "P")
12411   (gnus-set-global-variables)
12412   (gnus-summary-catchup-and-exit t quietly))
12413
12414 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12415 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12416   "Mark all articles in this group as read and select the next group.
12417 If given a prefix, mark all articles, unread as well as ticked, as
12418 read."
12419   (interactive "P")
12420   (gnus-set-global-variables)
12421   (save-excursion
12422     (gnus-summary-catchup all))
12423   (gnus-summary-next-article t nil nil t))
12424
12425 ;; Thread-based commands.
12426
12427 (defun gnus-summary-articles-in-thread (&optional article)
12428   "Return a list of all articles in the current thread.
12429 If ARTICLE is non-nil, return all articles in the thread that starts
12430 with that article."
12431   (let* ((article (or article (gnus-summary-article-number)))
12432          (data (gnus-data-find-list article))
12433          (top-level (gnus-data-level (car data)))
12434          (top-subject
12435           (cond ((null gnus-thread-operation-ignore-subject)
12436                  (gnus-simplify-subject-re
12437                   (mail-header-subject (gnus-data-header (car data)))))
12438                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12439                  (gnus-simplify-subject-fuzzy
12440                   (mail-header-subject (gnus-data-header (car data)))))
12441                 (t nil)))
12442          (end-point (save-excursion
12443                       (if (gnus-summary-go-to-next-thread) 
12444                           (point) (point-max))))
12445          articles)
12446     (while (and data
12447                 (< (gnus-data-pos (car data)) end-point))
12448       (when (or (not top-subject)
12449                 (string= top-subject
12450                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12451                              (gnus-simplify-subject-fuzzy
12452                               (mail-header-subject
12453                                (gnus-data-header (car data))))
12454                            (gnus-simplify-subject-re
12455                             (mail-header-subject
12456                              (gnus-data-header (car data)))))))
12457         (push (gnus-data-number (car data)) articles))
12458       (unless (and (setq data (cdr data))
12459                    (> (gnus-data-level (car data)) top-level))
12460         (setq data nil)))
12461     ;; Return the list of articles.
12462     (nreverse articles)))
12463
12464 (defun gnus-summary-rethread-current ()
12465   "Rethread the thread the current article is part of."
12466   (interactive)
12467   (gnus-set-global-variables)
12468   (let* ((gnus-show-threads t)
12469          (article (gnus-summary-article-number))
12470          (id (mail-header-id (gnus-summary-article-header)))
12471          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12472     (unless id
12473       (error "No article on the current line"))
12474     (gnus-rebuild-thread id)
12475     (gnus-summary-goto-subject article)))
12476
12477 (defun gnus-summary-reparent-thread ()
12478   "Make current article child of the marked (or previous) article.
12479
12480 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12481 is non-nil or the Subject: of both articles are the same."
12482   (interactive)
12483   (or (not (gnus-group-read-only-p))
12484       (error "The current newsgroup does not support article editing."))
12485   (or (<= (length gnus-newsgroup-processable) 1)
12486       (error "No more than one article may be marked."))
12487   (save-window-excursion
12488     (let ((gnus-article-buffer " *reparent*")
12489           (current-article (gnus-summary-article-number))
12490           ; first grab the marked article, otherwise one line up.
12491           (parent-article (if (not (null gnus-newsgroup-processable))
12492                               (car gnus-newsgroup-processable)
12493                             (save-excursion
12494                               (if (eq (forward-line -1) 0)
12495                                   (gnus-summary-article-number)
12496                                 (error "Beginning of summary buffer."))))))
12497       (or (not (eq current-article parent-article))
12498           (error "An article may not be self-referential."))
12499       (let ((message-id (mail-header-id 
12500                          (gnus-summary-article-header parent-article))))
12501         (or (and message-id (not (equal message-id "")))
12502             (error "No message-id in desired parent."))
12503         (gnus-summary-select-article t t nil current-article)
12504         (set-buffer gnus-article-buffer)
12505         (setq buffer-read-only nil)
12506         (let ((buf (format "%s" (buffer-string))))
12507           (erase-buffer)
12508           (insert buf))
12509         (goto-char (point-min))
12510         (if (search-forward-regexp "^References: " nil t)
12511             (insert message-id " " )
12512           (insert "References: " message-id "\n"))
12513         (or (gnus-request-replace-article current-article
12514                                           (car gnus-article-current)
12515                                           gnus-article-buffer)
12516             (error "Couldn't replace article."))
12517         (set-buffer gnus-summary-buffer)
12518         (gnus-summary-unmark-all-processable)
12519         (gnus-summary-rethread-current)
12520         (gnus-message 3 "Article %d is now the child of article %d."
12521                       current-article parent-article)))))
12522
12523 (defun gnus-summary-toggle-threads (&optional arg)
12524   "Toggle showing conversation threads.
12525 If ARG is positive number, turn showing conversation threads on."
12526   (interactive "P")
12527   (gnus-set-global-variables)
12528   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12529     (setq gnus-show-threads
12530           (if (null arg) (not gnus-show-threads)
12531             (> (prefix-numeric-value arg) 0)))
12532     (gnus-summary-prepare)
12533     (gnus-summary-goto-subject current)
12534     (gnus-summary-position-point)))
12535
12536 (defun gnus-summary-show-all-threads ()
12537   "Show all threads."
12538   (interactive)
12539   (gnus-set-global-variables)
12540   (save-excursion
12541     (let ((buffer-read-only nil))
12542       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12543   (gnus-summary-position-point))
12544
12545 (defun gnus-summary-show-thread ()
12546   "Show thread subtrees.
12547 Returns nil if no thread was there to be shown."
12548   (interactive)
12549   (gnus-set-global-variables)
12550   (let ((buffer-read-only nil)
12551         (orig (point))
12552         ;; first goto end then to beg, to have point at beg after let
12553         (end (progn (end-of-line) (point)))
12554         (beg (progn (beginning-of-line) (point))))
12555     (prog1
12556         ;; Any hidden lines here?
12557         (search-forward "\r" end t)
12558       (subst-char-in-region beg end ?\^M ?\n t)
12559       (goto-char orig)
12560       (gnus-summary-position-point))))
12561
12562 (defun gnus-summary-hide-all-threads ()
12563   "Hide all thread subtrees."
12564   (interactive)
12565   (gnus-set-global-variables)
12566   (save-excursion
12567     (goto-char (point-min))
12568     (gnus-summary-hide-thread)
12569     (while (zerop (gnus-summary-next-thread 1 t))
12570       (gnus-summary-hide-thread)))
12571   (gnus-summary-position-point))
12572
12573 (defun gnus-summary-hide-thread ()
12574   "Hide thread subtrees.
12575 Returns nil if no threads were there to be hidden."
12576   (interactive)
12577   (gnus-set-global-variables)
12578   (let ((buffer-read-only nil)
12579         (start (point))
12580         (article (gnus-summary-article-number)))
12581     (goto-char start)
12582     ;; Go forward until either the buffer ends or the subthread
12583     ;; ends.
12584     (when (and (not (eobp))
12585                (or (zerop (gnus-summary-next-thread 1 t))
12586                    (goto-char (point-max))))
12587       (prog1
12588           (if (and (> (point) start)
12589                    (search-backward "\n" start t))
12590               (progn
12591                 (subst-char-in-region start (point) ?\n ?\^M)
12592                 (gnus-summary-goto-subject article))
12593             (goto-char start)
12594             nil)
12595         ;;(gnus-summary-position-point)
12596         ))))
12597
12598 (defun gnus-summary-go-to-next-thread (&optional previous)
12599   "Go to the same level (or less) next thread.
12600 If PREVIOUS is non-nil, go to previous thread instead.
12601 Return the article number moved to, or nil if moving was impossible."
12602   (let ((level (gnus-summary-thread-level))
12603         (way (if previous -1 1))
12604         (beg (point)))
12605     (forward-line way)
12606     (while (and (not (eobp))
12607                 (< level (gnus-summary-thread-level)))
12608       (forward-line way))
12609     (if (eobp)
12610         (progn
12611           (goto-char beg)
12612           nil)
12613       (setq beg (point))
12614       (prog1
12615           (gnus-summary-article-number)
12616         (goto-char beg)))))
12617
12618 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12619   "Go to the same level (or less) next thread.
12620 If PREVIOUS is non-nil, go to previous thread instead.
12621 Return the article number moved to, or nil if moving was impossible."
12622   (if (and (eq gnus-summary-make-false-root 'dummy)
12623            (gnus-summary-article-intangible-p))
12624       (let ((beg (point)))
12625         (while (and (zerop (forward-line 1))
12626                     (not (gnus-summary-article-intangible-p))
12627                     (not (zerop (save-excursion 
12628                                   (gnus-summary-thread-level))))))
12629         (if (eobp)
12630             (progn
12631               (goto-char beg)
12632               nil)
12633           (point)))
12634     (let* ((level (gnus-summary-thread-level))
12635            (article (gnus-summary-article-number))
12636            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12637            oart)
12638       (while data
12639         (if (<= (gnus-data-level (car data)) level)
12640             (setq oart (gnus-data-number (car data))
12641                   data nil)
12642           (setq data (cdr data))))
12643       (and oart
12644            (gnus-summary-goto-subject oart)))))
12645
12646 (defun gnus-summary-next-thread (n &optional silent)
12647   "Go to the same level next N'th thread.
12648 If N is negative, search backward instead.
12649 Returns the difference between N and the number of skips actually
12650 done.
12651
12652 If SILENT, don't output messages."
12653   (interactive "p")
12654   (gnus-set-global-variables)
12655   (let ((backward (< n 0))
12656         (n (abs n))
12657         old dum int)
12658     (while (and (> n 0)
12659                 (gnus-summary-go-to-next-thread backward))
12660       (decf n))
12661     (unless silent 
12662       (gnus-summary-position-point))
12663     (when (and (not silent) (/= 0 n))
12664       (gnus-message 7 "No more threads"))
12665     n))
12666
12667 (defun gnus-summary-prev-thread (n)
12668   "Go to the same level previous N'th thread.
12669 Returns the difference between N and the number of skips actually
12670 done."
12671   (interactive "p")
12672   (gnus-set-global-variables)
12673   (gnus-summary-next-thread (- n)))
12674
12675 (defun gnus-summary-go-down-thread ()
12676   "Go down one level in the current thread."
12677   (let ((children (gnus-summary-article-children)))
12678     (and children
12679          (gnus-summary-goto-subject (car children)))))
12680
12681 (defun gnus-summary-go-up-thread ()
12682   "Go up one level in the current thread."
12683   (let ((parent (gnus-summary-article-parent)))
12684     (and parent
12685          (gnus-summary-goto-subject parent))))
12686
12687 (defun gnus-summary-down-thread (n)
12688   "Go down thread N steps.
12689 If N is negative, go up instead.
12690 Returns the difference between N and how many steps down that were
12691 taken."
12692   (interactive "p")
12693   (gnus-set-global-variables)
12694   (let ((up (< n 0))
12695         (n (abs n)))
12696     (while (and (> n 0)
12697                 (if up (gnus-summary-go-up-thread)
12698                   (gnus-summary-go-down-thread)))
12699       (setq n (1- n)))
12700     (gnus-summary-position-point)
12701     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12702     n))
12703
12704 (defun gnus-summary-up-thread (n)
12705   "Go up thread N steps.
12706 If N is negative, go up instead.
12707 Returns the difference between N and how many steps down that were
12708 taken."
12709   (interactive "p")
12710   (gnus-set-global-variables)
12711   (gnus-summary-down-thread (- n)))
12712
12713 (defun gnus-summary-top-thread ()
12714   "Go to the top of the thread."
12715   (interactive)
12716   (gnus-set-global-variables)
12717   (while (gnus-summary-go-up-thread))
12718   (gnus-summary-article-number))
12719
12720 (defun gnus-summary-kill-thread (&optional unmark)
12721   "Mark articles under current thread as read.
12722 If the prefix argument is positive, remove any kinds of marks.
12723 If the prefix argument is negative, tick articles instead."
12724   (interactive "P")
12725   (gnus-set-global-variables)
12726   (if unmark
12727       (setq unmark (prefix-numeric-value unmark)))
12728   (let ((articles (gnus-summary-articles-in-thread)))
12729     (save-excursion
12730       ;; Expand the thread.
12731       (gnus-summary-show-thread)
12732       ;; Mark all the articles.
12733       (while articles
12734         (gnus-summary-goto-subject (car articles))
12735         (cond ((null unmark)
12736                (gnus-summary-mark-article-as-read gnus-killed-mark))
12737               ((> unmark 0)
12738                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12739               (t
12740                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12741         (setq articles (cdr articles))))
12742     ;; Hide killed subtrees.
12743     (and (null unmark)
12744          gnus-thread-hide-killed
12745          (gnus-summary-hide-thread))
12746     ;; If marked as read, go to next unread subject.
12747     (if (null unmark)
12748         ;; Go to next unread subject.
12749         (gnus-summary-next-subject 1 t)))
12750   (gnus-set-mode-line 'summary))
12751
12752 ;; Summary sorting commands
12753
12754 (defun gnus-summary-sort-by-number (&optional reverse)
12755   "Sort summary buffer by article number.
12756 Argument REVERSE means reverse order."
12757   (interactive "P")
12758   (gnus-summary-sort 'number reverse))
12759
12760 (defun gnus-summary-sort-by-author (&optional reverse)
12761   "Sort summary buffer by author name alphabetically.
12762 If case-fold-search is non-nil, case of letters is ignored.
12763 Argument REVERSE means reverse order."
12764   (interactive "P")
12765   (gnus-summary-sort 'author reverse))
12766
12767 (defun gnus-summary-sort-by-subject (&optional reverse)
12768   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12769 If case-fold-search is non-nil, case of letters is ignored.
12770 Argument REVERSE means reverse order."
12771   (interactive "P")
12772   (gnus-summary-sort 'subject reverse))
12773
12774 (defun gnus-summary-sort-by-date (&optional reverse)
12775   "Sort summary buffer by date.
12776 Argument REVERSE means reverse order."
12777   (interactive "P")
12778   (gnus-summary-sort 'date reverse))
12779
12780 (defun gnus-summary-sort-by-score (&optional reverse)
12781   "Sort summary buffer by score.
12782 Argument REVERSE means reverse order."
12783   (interactive "P")
12784   (gnus-summary-sort 'score reverse))
12785
12786 (defun gnus-summary-sort (predicate reverse)
12787   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12788   (gnus-set-global-variables)
12789   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12790          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12791          (gnus-thread-sort-functions
12792           (list
12793            (if (not reverse)
12794                thread
12795              `(lambda (t1 t2)
12796                 (,thread t2 t1)))))
12797          (gnus-article-sort-functions
12798           (list
12799            (if (not reverse)
12800                article
12801              `(lambda (t1 t2)
12802                 (,article t2 t1)))))
12803          (buffer-read-only)
12804          (gnus-summary-prepare-hook nil))
12805     ;; We do the sorting by regenerating the threads.
12806     (gnus-summary-prepare)
12807     ;; Hide subthreads if needed.
12808     (when (and gnus-show-threads gnus-thread-hide-subtree)
12809       (gnus-summary-hide-all-threads)))
12810   ;; If in async mode, we send some info to the backend.
12811   (when gnus-newsgroup-async
12812     (gnus-request-asynchronous
12813      gnus-newsgroup-name gnus-newsgroup-data)))
12814
12815 (defun gnus-sortable-date (date)
12816   "Make sortable string by string-lessp from DATE.
12817 Timezone package is used."
12818   (setq date (timezone-fix-time date nil nil))
12819   (timezone-make-sortable-date
12820    (aref date 0) (aref date 2) (aref date 2)
12821    (timezone-make-time-string
12822     (aref date 3) (aref date 4) (aref date 5))))
12823
12824 ;; Summary saving commands.
12825
12826 (defun gnus-summary-save-article (&optional n not-saved)
12827   "Save the current article using the default saver function.
12828 If N is a positive number, save the N next articles.
12829 If N is a negative number, save the N previous articles.
12830 If N is nil and any articles have been marked with the process mark,
12831 save those articles instead.
12832 The variable `gnus-default-article-saver' specifies the saver function."
12833   (interactive "P")
12834   (gnus-set-global-variables)
12835   (let ((articles (gnus-summary-work-articles n))
12836         file header article)
12837     (while articles
12838       (setq header (gnus-summary-article-header
12839                     (setq article (pop articles))))
12840       (if (not (vectorp header))
12841           ;; This is a pseudo-article.
12842           (if (assq 'name header)
12843               (gnus-copy-file (cdr (assq 'name header)))
12844             (gnus-message 1 "Article %d is unsaveable" article))
12845         ;; This is a real article.
12846         (save-window-excursion
12847           (gnus-summary-select-article t nil nil article))
12848         (unless gnus-save-all-headers
12849           ;; Remove headers accoring to `gnus-saved-headers'.
12850           (let ((gnus-visible-headers
12851                  (or gnus-saved-headers gnus-visible-headers)))
12852             (gnus-article-hide-headers nil t)))
12853         ;; Remove any X-Gnus lines.
12854         (save-excursion
12855           (set-buffer gnus-article-buffer)
12856           (save-restriction
12857             (let ((buffer-read-only nil))
12858               (nnheader-narrow-to-headers)
12859               (while (re-search-forward "^X-Gnus" nil t)
12860                 (gnus-delete-line)))))
12861         (save-window-excursion
12862           (if (not gnus-default-article-saver)
12863               (error "No default saver is defined.")
12864             (setq file (funcall
12865                         gnus-default-article-saver
12866                         (cond
12867                          ((not gnus-prompt-before-saving)
12868                           'default)
12869                          ((eq gnus-prompt-before-saving 'always)
12870                           nil)
12871                          (t file))))))
12872         (gnus-summary-remove-process-mark article)
12873         (unless not-saved
12874           (gnus-summary-set-saved-mark article))))
12875     (gnus-summary-position-point)
12876     n))
12877
12878 (defun gnus-summary-pipe-output (&optional arg)
12879   "Pipe the current article to a subprocess.
12880 If N is a positive number, pipe the N next articles.
12881 If N is a negative number, pipe the N previous articles.
12882 If N is nil and any articles have been marked with the process mark,
12883 pipe those articles instead."
12884   (interactive "P")
12885   (gnus-set-global-variables)
12886   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12887     (gnus-summary-save-article arg t))
12888   (gnus-configure-windows 'pipe))
12889
12890 (defun gnus-summary-save-article-mail (&optional arg)
12891   "Append the current article to an mail file.
12892 If N is a positive number, save the N next articles.
12893 If N is a negative number, save the N previous articles.
12894 If N is nil and any articles have been marked with the process mark,
12895 save those articles instead."
12896   (interactive "P")
12897   (gnus-set-global-variables)
12898   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12899     (gnus-summary-save-article arg)))
12900
12901 (defun gnus-summary-save-article-rmail (&optional arg)
12902   "Append the current article to an rmail file.
12903 If N is a positive number, save the N next articles.
12904 If N is a negative number, save the N previous articles.
12905 If N is nil and any articles have been marked with the process mark,
12906 save those articles instead."
12907   (interactive "P")
12908   (gnus-set-global-variables)
12909   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12910     (gnus-summary-save-article arg)))
12911
12912 (defun gnus-summary-save-article-file (&optional arg)
12913   "Append the current article to a file.
12914 If N is a positive number, save the N next articles.
12915 If N is a negative number, save the N previous articles.
12916 If N is nil and any articles have been marked with the process mark,
12917 save those articles instead."
12918   (interactive "P")
12919   (gnus-set-global-variables)
12920   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12921     (gnus-summary-save-article arg)))
12922
12923 (defun gnus-summary-save-article-body-file (&optional arg)
12924   "Append the current article body to a file.
12925 If N is a positive number, save the N next articles.
12926 If N is a negative number, save the N previous articles.
12927 If N is nil and any articles have been marked with the process mark,
12928 save those articles instead."
12929   (interactive "P")
12930   (gnus-set-global-variables)
12931   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12932     (gnus-summary-save-article arg)))
12933
12934 (defun gnus-get-split-value (methods)
12935   "Return a value based on the split METHODS."
12936   (let (split-name method result match)
12937     (when methods
12938       (save-excursion
12939         (set-buffer gnus-original-article-buffer)
12940         (save-restriction
12941           (nnheader-narrow-to-headers)
12942           (while methods
12943             (goto-char (point-min))
12944             (setq method (pop methods))
12945             (setq match (car method))
12946             (when (cond
12947                    ((stringp match)
12948                     ;; Regular expression.
12949                     (condition-case ()
12950                         (re-search-forward match nil t)
12951                       (error nil)))
12952                    ((gnus-functionp match)
12953                     ;; Function.
12954                     (save-restriction
12955                       (widen)
12956                       (setq result (funcall match gnus-newsgroup-name))))
12957                    ((consp match)
12958                     ;; Form.
12959                     (save-restriction
12960                       (widen)
12961                       (setq result (eval match)))))
12962               (setq split-name (append (cdr method) split-name))
12963               (cond ((stringp result)
12964                      (push result split-name))
12965                     ((consp result)
12966                      (setq split-name (append result split-name)))))))))
12967     split-name))
12968
12969 (defun gnus-read-move-group-name (prompt default articles prefix)
12970   "Read a group name."
12971   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12972          group-map
12973          (dum (mapatoms
12974                (lambda (g) 
12975                  (and (boundp g)
12976                       (symbol-name g)
12977                       (memq 'respool
12978                             (assoc (symbol-name
12979                                     (car (gnus-find-method-for-group
12980                                           (symbol-name g))))
12981                                    gnus-valid-select-methods))
12982                       (push (list (symbol-name g)) group-map)))
12983                gnus-active-hashtb))
12984          (prom
12985           (format "%s %s to:"
12986                   prompt
12987                   (if (> (length articles) 1)
12988                       (format "these %d articles" (length articles))
12989                     "this article")))
12990          (to-newsgroup
12991           (cond
12992            ((null split-name)
12993             (gnus-completing-read default prom
12994                                   group-map nil nil prefix
12995                                   'gnus-group-history))
12996            ((= 1 (length split-name))
12997             (gnus-completing-read (car split-name) prom group-map
12998                                   nil nil nil
12999                                   'gnus-group-history))
13000            (t
13001             (gnus-completing-read nil prom 
13002                                   (mapcar (lambda (el) (list el))
13003                                           (nreverse split-name))
13004                                   nil nil nil
13005                                   'gnus-group-history)))))
13006     (when to-newsgroup
13007       (if (or (string= to-newsgroup "")
13008               (string= to-newsgroup prefix))
13009           (setq to-newsgroup (or default "")))
13010       (or (gnus-active to-newsgroup)
13011           (gnus-activate-group to-newsgroup)
13012           (error "No such group: %s" to-newsgroup)))
13013     to-newsgroup))
13014
13015 (defun gnus-read-save-file-name (prompt default-name)
13016   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13017          (file
13018           ;; Let the split methods have their say.
13019           (cond
13020            ;; No split name was found.
13021            ((null split-name)
13022             (read-file-name
13023              (concat prompt " (default "
13024                      (file-name-nondirectory default-name) ") ")
13025              (file-name-directory default-name)
13026              default-name))
13027            ;; A single split name was found
13028            ((= 1 (length split-name))
13029             (read-file-name
13030              (concat prompt " (default " (car split-name) ") ")
13031              gnus-article-save-directory
13032              (concat gnus-article-save-directory (car split-name))))
13033            ;; A list of splits was found.
13034            (t
13035             (setq split-name (nreverse split-name))
13036             (let (result)
13037               (let ((file-name-history (nconc split-name file-name-history)))
13038                 (setq result
13039                       (read-file-name
13040                        (concat prompt " (`M-p' for defaults) ")
13041                        gnus-article-save-directory
13042                        (car split-name))))
13043               (car (push result file-name-history)))))))
13044     ;; If we have read a directory, we append the default file name.
13045     (when (file-directory-p file)
13046       (setq file (concat (file-name-as-directory file)
13047                          (file-name-nondirectory default-name))))
13048     ;; Possibly translate some charaters.
13049     (nnheader-translate-file-chars file)))
13050
13051 (defun gnus-article-archive-name (group)
13052   "Return the first instance of an \"Archive-name\" in the current buffer."
13053   (let ((case-fold-search t))
13054     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13055       (match-string 1))))
13056
13057 (defun gnus-summary-save-in-rmail (&optional filename)
13058   "Append this article to Rmail file.
13059 Optional argument FILENAME specifies file name.
13060 Directory to save to is default to `gnus-article-save-directory'."
13061   (interactive)
13062   (gnus-set-global-variables)
13063   (let ((default-name
13064           (funcall gnus-rmail-save-name gnus-newsgroup-name
13065                    gnus-current-headers gnus-newsgroup-last-rmail)))
13066     (setq filename
13067           (cond ((eq filename 'default)
13068                  default-name)
13069                 (filename filename)
13070                 (t (gnus-read-save-file-name
13071                     "Save in rmail file:" default-name))))
13072     (gnus-make-directory (file-name-directory filename))
13073     (gnus-eval-in-buffer-window
13074      gnus-original-article-buffer
13075      (save-excursion
13076        (save-restriction
13077          (widen)
13078          (gnus-output-to-rmail filename))))
13079     ;; Remember the directory name to save articles
13080     (setq gnus-newsgroup-last-rmail filename)))
13081
13082 (defun gnus-summary-save-in-mail (&optional filename)
13083   "Append this article to Unix mail file.
13084 Optional argument FILENAME specifies file name.
13085 Directory to save to is default to `gnus-article-save-directory'."
13086   (interactive)
13087   (gnus-set-global-variables)
13088   (let ((default-name
13089           (funcall gnus-mail-save-name gnus-newsgroup-name
13090                    gnus-current-headers gnus-newsgroup-last-mail)))
13091     (setq filename
13092           (cond ((eq filename 'default)
13093                  default-name)
13094                 (filename filename)
13095                 (t (gnus-read-save-file-name
13096                     "Save in Unix mail file:" default-name))))
13097     (setq filename
13098           (expand-file-name filename
13099                             (and default-name
13100                                  (file-name-directory default-name))))
13101     (gnus-make-directory (file-name-directory filename))
13102     (gnus-eval-in-buffer-window
13103      gnus-original-article-buffer
13104      (save-excursion
13105        (save-restriction
13106          (widen)
13107          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13108              (gnus-output-to-rmail filename)
13109            (let ((mail-use-rfc822 t))
13110              (rmail-output filename 1 t t))))))
13111     ;; Remember the directory name to save articles.
13112     (setq gnus-newsgroup-last-mail filename)))
13113
13114 (defun gnus-summary-save-in-file (&optional filename)
13115   "Append this article to file.
13116 Optional argument FILENAME specifies file name.
13117 Directory to save to is default to `gnus-article-save-directory'."
13118   (interactive)
13119   (gnus-set-global-variables)
13120   (let ((default-name
13121           (funcall gnus-file-save-name gnus-newsgroup-name
13122                    gnus-current-headers gnus-newsgroup-last-file)))
13123     (setq filename
13124           (cond ((eq filename 'default)
13125                  default-name)
13126                 (filename filename)
13127                 (t (gnus-read-save-file-name
13128                     "Save in file:" default-name))))
13129     (gnus-make-directory (file-name-directory filename))
13130     (gnus-eval-in-buffer-window
13131      gnus-original-article-buffer
13132      (save-excursion
13133        (save-restriction
13134          (widen)
13135          (gnus-output-to-file filename))))
13136     ;; Remember the directory name to save articles.
13137     (setq gnus-newsgroup-last-file filename)))
13138
13139 (defun gnus-summary-save-body-in-file (&optional filename)
13140   "Append this article body to a file.
13141 Optional argument FILENAME specifies file name.
13142 The directory to save in defaults to `gnus-article-save-directory'."
13143   (interactive)
13144   (gnus-set-global-variables)
13145   (let ((default-name
13146           (funcall gnus-file-save-name gnus-newsgroup-name
13147                    gnus-current-headers gnus-newsgroup-last-file)))
13148     (setq filename
13149           (cond ((eq filename 'default)
13150                  default-name)
13151                 (filename filename)
13152                 (t (gnus-read-save-file-name
13153                     "Save body in file:" default-name))))
13154     (gnus-make-directory (file-name-directory filename))
13155     (gnus-eval-in-buffer-window
13156      gnus-article-buffer
13157      (save-excursion
13158        (save-restriction
13159          (widen)
13160          (goto-char (point-min))
13161          (and (search-forward "\n\n" nil t)
13162               (narrow-to-region (point) (point-max)))
13163          (gnus-output-to-file filename))))
13164     ;; Remember the directory name to save articles.
13165     (setq gnus-newsgroup-last-file filename)))
13166
13167 (defun gnus-summary-save-in-pipe (&optional command)
13168   "Pipe this article to subprocess."
13169   (interactive)
13170   (gnus-set-global-variables)
13171   (setq command
13172         (cond ((eq command 'default)
13173                gnus-last-shell-command)
13174               (command command)
13175               (t (read-string "Shell command on article: "
13176                               gnus-last-shell-command))))
13177   (if (string-equal command "")
13178       (setq command gnus-last-shell-command))
13179   (gnus-eval-in-buffer-window
13180    gnus-article-buffer
13181    (save-restriction
13182      (widen)
13183      (shell-command-on-region (point-min) (point-max) command nil)))
13184   (setq gnus-last-shell-command command))
13185
13186 ;; Summary extract commands
13187
13188 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13189   (let ((buffer-read-only nil)
13190         (article (gnus-summary-article-number))
13191         after-article b e)
13192     (or (gnus-summary-goto-subject article)
13193         (error (format "No such article: %d" article)))
13194     (gnus-summary-position-point)
13195     ;; If all commands are to be bunched up on one line, we collect
13196     ;; them here.
13197     (if gnus-view-pseudos-separately
13198         ()
13199       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13200             files action)
13201         (while ps
13202           (setq action (cdr (assq 'action (car ps))))
13203           (setq files (list (cdr (assq 'name (car ps)))))
13204           (while (and ps (cdr ps)
13205                       (string= (or action "1")
13206                                (or (cdr (assq 'action (cadr ps))) "2")))
13207             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13208             (setcdr ps (cddr ps)))
13209           (if (not files)
13210               ()
13211             (if (not (string-match "%s" action))
13212                 (setq files (cons " " files)))
13213             (setq files (cons " " files))
13214             (and (assq 'execute (car ps))
13215                  (setcdr (assq 'execute (car ps))
13216                          (funcall (if (string-match "%s" action)
13217                                       'format 'concat)
13218                                   action
13219                                   (mapconcat (lambda (f) f) files " ")))))
13220           (setq ps (cdr ps)))))
13221     (if (and gnus-view-pseudos (not not-view))
13222         (while pslist
13223           (and (assq 'execute (car pslist))
13224                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13225                                      (eq gnus-view-pseudos 'not-confirm)))
13226           (setq pslist (cdr pslist)))
13227       (save-excursion
13228         (while pslist
13229           (setq after-article (or (cdr (assq 'article (car pslist)))
13230                                   (gnus-summary-article-number)))
13231           (gnus-summary-goto-subject after-article)
13232           (forward-line 1)
13233           (setq b (point))
13234           (insert "    " (file-name-nondirectory
13235                                 (cdr (assq 'name (car pslist))))
13236                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13237           (setq e (point))
13238           (forward-line -1)             ; back to `b'
13239           (gnus-add-text-properties
13240            b e (list 'gnus-number gnus-reffed-article-number
13241                      gnus-mouse-face-prop gnus-mouse-face))
13242           (gnus-data-enter
13243            after-article gnus-reffed-article-number
13244            gnus-unread-mark b (car pslist) 0 (- e b))
13245           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13246           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13247           (setq pslist (cdr pslist)))))))
13248
13249 (defun gnus-pseudos< (p1 p2)
13250   (let ((c1 (cdr (assq 'action p1)))
13251         (c2 (cdr (assq 'action p2))))
13252     (and c1 c2 (string< c1 c2))))
13253
13254 (defun gnus-request-pseudo-article (props)
13255   (cond ((assq 'execute props)
13256          (gnus-execute-command (cdr (assq 'execute props)))))
13257   (let ((gnus-current-article (gnus-summary-article-number)))
13258     (run-hooks 'gnus-mark-article-hook)))
13259
13260 (defun gnus-execute-command (command &optional automatic)
13261   (save-excursion
13262     (gnus-article-setup-buffer)
13263     (set-buffer gnus-article-buffer)
13264     (setq buffer-read-only nil)
13265     (let ((command (if automatic command (read-string "Command: " command)))
13266           ;; Just binding this here doesn't help, because there might
13267           ;; be output from the process after exiting the scope of 
13268           ;; this `let'.
13269           ;; (buffer-read-only nil)
13270           )
13271       (erase-buffer)
13272       (insert "$ " command "\n\n")
13273       (if gnus-view-pseudo-asynchronously
13274           (start-process "gnus-execute" nil "sh" "-c" command)
13275         (call-process "sh" nil t nil "-c" command)))))
13276
13277 (defun gnus-copy-file (file &optional to)
13278   "Copy FILE to TO."
13279   (interactive
13280    (list (read-file-name "Copy file: " default-directory)
13281          (read-file-name "Copy file to: " default-directory)))
13282   (gnus-set-global-variables)
13283   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13284   (and (file-directory-p to)
13285        (setq to (concat (file-name-as-directory to)
13286                         (file-name-nondirectory file))))
13287   (copy-file file to))
13288
13289 ;; Summary kill commands.
13290
13291 (defun gnus-summary-edit-global-kill (article)
13292   "Edit the \"global\" kill file."
13293   (interactive (list (gnus-summary-article-number)))
13294   (gnus-set-global-variables)
13295   (gnus-group-edit-global-kill article))
13296
13297 (defun gnus-summary-edit-local-kill ()
13298   "Edit a local kill file applied to the current newsgroup."
13299   (interactive)
13300   (gnus-set-global-variables)
13301   (setq gnus-current-headers (gnus-summary-article-header))
13302   (gnus-set-global-variables)
13303   (gnus-group-edit-local-kill
13304    (gnus-summary-article-number) gnus-newsgroup-name))
13305
13306 \f
13307 ;;;
13308 ;;; Gnus article mode
13309 ;;;
13310
13311 (put 'gnus-article-mode 'mode-class 'special)
13312
13313 (if gnus-article-mode-map
13314     nil
13315   (setq gnus-article-mode-map (make-keymap))
13316   (suppress-keymap gnus-article-mode-map)
13317
13318   (gnus-define-keys gnus-article-mode-map
13319     " " gnus-article-goto-next-page
13320     "\177" gnus-article-goto-prev-page
13321     [delete] gnus-article-goto-prev-page
13322     "\C-c^" gnus-article-refer-article
13323     "h" gnus-article-show-summary
13324     "s" gnus-article-show-summary
13325     "\C-c\C-m" gnus-article-mail
13326     "?" gnus-article-describe-briefly
13327     gnus-mouse-2 gnus-article-push-button
13328     "\r" gnus-article-press-button
13329     "\t" gnus-article-next-button
13330     "\M-\t" gnus-article-prev-button
13331     "\C-c\C-b" gnus-bug)
13332
13333   (substitute-key-definition
13334    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13335
13336 (defun gnus-article-mode ()
13337   "Major mode for displaying an article.
13338
13339 All normal editing commands are switched off.
13340
13341 The following commands are available:
13342
13343 \\<gnus-article-mode-map>
13344 \\[gnus-article-next-page]\t Scroll the article one page forwards
13345 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13346 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13347 \\[gnus-article-show-summary]\t Display the summary buffer
13348 \\[gnus-article-mail]\t Send a reply to the address near point
13349 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13350 \\[gnus-info-find-node]\t Go to the Gnus info node"
13351   (interactive)
13352   (when (and menu-bar-mode
13353              (gnus-visual-p 'article-menu 'menu))
13354     (gnus-article-make-menu-bar))
13355   (kill-all-local-variables)
13356   (gnus-simplify-mode-line)
13357   (setq mode-name "Article")
13358   (setq major-mode 'gnus-article-mode)
13359   (make-local-variable 'minor-mode-alist)
13360   (or (assq 'gnus-show-mime minor-mode-alist)
13361       (setq minor-mode-alist
13362             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13363   (use-local-map gnus-article-mode-map)
13364   (make-local-variable 'page-delimiter)
13365   (setq page-delimiter gnus-page-delimiter)
13366   (buffer-disable-undo (current-buffer))
13367   (setq buffer-read-only t)             ;Disable modification
13368   (run-hooks 'gnus-article-mode-hook))
13369
13370 (defun gnus-article-setup-buffer ()
13371   "Initialize the article buffer."
13372   (let* ((name (if gnus-single-article-buffer "*Article*"
13373                  (concat "*Article " gnus-newsgroup-name "*")))
13374          (original
13375           (progn (string-match "\\*Article" name)
13376                  (concat " *Original Article"
13377                          (substring name (match-end 0))))))
13378     (setq gnus-article-buffer name)
13379     (setq gnus-original-article-buffer original)
13380     ;; This might be a variable local to the summary buffer.
13381     (unless gnus-single-article-buffer
13382       (save-excursion
13383         (set-buffer gnus-summary-buffer)
13384         (setq gnus-article-buffer name)
13385         (setq gnus-original-article-buffer original)
13386         (gnus-set-global-variables))
13387       (make-local-variable 'gnus-summary-buffer))
13388     ;; Init original article buffer.
13389     (save-excursion
13390       (set-buffer (get-buffer-create gnus-original-article-buffer))
13391       (buffer-disable-undo (current-buffer))
13392       (setq major-mode 'gnus-original-article-mode)
13393       (make-local-variable 'gnus-original-article))
13394     (if (get-buffer name)
13395         (save-excursion
13396           (set-buffer name)
13397           (buffer-disable-undo (current-buffer))
13398           (setq buffer-read-only t)
13399           (gnus-add-current-to-buffer-list)
13400           (or (eq major-mode 'gnus-article-mode)
13401               (gnus-article-mode))
13402           (current-buffer))
13403       (save-excursion
13404         (set-buffer (get-buffer-create name))
13405         (gnus-add-current-to-buffer-list)
13406         (gnus-article-mode)
13407         (current-buffer)))))
13408
13409 ;; Set article window start at LINE, where LINE is the number of lines
13410 ;; from the head of the article.
13411 (defun gnus-article-set-window-start (&optional line)
13412   (set-window-start
13413    (get-buffer-window gnus-article-buffer t)
13414    (save-excursion
13415      (set-buffer gnus-article-buffer)
13416      (goto-char (point-min))
13417      (if (not line)
13418          (point-min)
13419        (gnus-message 6 "Moved to bookmark")
13420        (search-forward "\n\n" nil t)
13421        (forward-line line)
13422        (point)))))
13423
13424 (defun gnus-kill-all-overlays ()
13425   "Delete all overlays in the current buffer."
13426   (when (fboundp 'overlay-lists)
13427     (let* ((overlayss (overlay-lists))
13428            (buffer-read-only nil)
13429            (overlays (nconc (car overlayss) (cdr overlayss))))
13430       (while overlays
13431         (delete-overlay (pop overlays))))))
13432
13433 (defun gnus-request-article-this-buffer (article group)
13434   "Get an article and insert it into this buffer."
13435   (let (do-update-line)
13436     (prog1
13437         (save-excursion
13438           (erase-buffer)
13439           (gnus-kill-all-overlays)
13440           (setq group (or group gnus-newsgroup-name))
13441
13442           ;; Open server if it has closed.
13443           (gnus-check-server (gnus-find-method-for-group group))
13444
13445           ;; Using `gnus-request-article' directly will insert the article into
13446           ;; `nntp-server-buffer' - so we'll save some time by not having to
13447           ;; copy it from the server buffer into the article buffer.
13448
13449           ;; We only request an article by message-id when we do not have the
13450           ;; headers for it, so we'll have to get those.
13451           (when (stringp article)
13452             (let ((gnus-override-method gnus-refer-article-method))
13453               (gnus-read-header article)))
13454
13455           ;; If the article number is negative, that means that this article
13456           ;; doesn't belong in this newsgroup (possibly), so we find its
13457           ;; message-id and request it by id instead of number.
13458           (when (and (numberp article)
13459                      gnus-summary-buffer
13460                      (get-buffer gnus-summary-buffer)
13461                      (buffer-name (get-buffer gnus-summary-buffer)))
13462             (save-excursion
13463               (set-buffer gnus-summary-buffer)
13464               (let ((header (gnus-summary-article-header article)))
13465                 (if (< article 0)
13466                     (cond 
13467                      ((memq article gnus-newsgroup-sparse)
13468                       ;; This is a sparse gap article.
13469                       (setq do-update-line article)
13470                       (setq article (mail-header-id header))
13471                       (let ((gnus-override-method gnus-refer-article-method))
13472                         (gnus-read-header article))
13473                       (setq gnus-newsgroup-sparse
13474                             (delq article gnus-newsgroup-sparse)))
13475                      ((vectorp header)
13476                       ;; It's a real article.
13477                       (setq article (mail-header-id header)))
13478                      (t
13479                       ;; It is an extracted pseudo-article.
13480                       (setq article 'pseudo)
13481                       (gnus-request-pseudo-article header))))
13482                 
13483                 (let ((method (gnus-find-method-for-group 
13484                                gnus-newsgroup-name)))
13485                   (if (not (eq (car method) 'nneething))
13486                       ()
13487                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13488                                        (mail-header-subject header))))
13489                       (if (file-directory-p dir)
13490                           (progn
13491                             (setq article 'nneething)
13492                             (gnus-group-enter-directory dir)))))))))
13493
13494           (cond
13495            ;; Refuse to select canceled articles.
13496            ((and (numberp article)
13497                  gnus-summary-buffer
13498                  (get-buffer gnus-summary-buffer)
13499                  (buffer-name (get-buffer gnus-summary-buffer))
13500                  (eq (cdr (save-excursion
13501                             (set-buffer gnus-summary-buffer)
13502                             (assq article gnus-newsgroup-reads)))
13503                      gnus-canceled-mark))
13504             nil)
13505            ;; We first check `gnus-original-article-buffer'.
13506            ((and (get-buffer gnus-original-article-buffer)
13507                  (numberp article)
13508                  (save-excursion
13509                    (set-buffer gnus-original-article-buffer)
13510                    (and (equal (car gnus-original-article) group)
13511                         (eq (cdr gnus-original-article) article))))
13512             (insert-buffer-substring gnus-original-article-buffer)
13513             'article)
13514            ;; Check the backlog.
13515            ((and gnus-keep-backlog
13516                  (gnus-backlog-request-article group article (current-buffer)))
13517             'article)
13518            ;; Check the cache.
13519            ((and gnus-use-cache
13520                  (numberp article)
13521                  (gnus-cache-request-article article group))
13522             'article)
13523            ;; Get the article and put into the article buffer.
13524            ((or (stringp article) (numberp article))
13525             (let ((gnus-override-method
13526                    (and (stringp article) gnus-refer-article-method))
13527                   (buffer-read-only nil))
13528               (erase-buffer)
13529               (gnus-kill-all-overlays)
13530               (if (gnus-request-article article group (current-buffer))
13531                   (progn
13532                     (and gnus-keep-backlog
13533                          (numberp article)
13534                          (gnus-backlog-enter-article
13535                           group article (current-buffer)))
13536                     'article))))
13537            ;; It was a pseudo.
13538            (t article)))
13539
13540       ;; Take the article from the original article buffer
13541       ;; and place it in the buffer it's supposed to be in.
13542       (when (and (get-buffer gnus-article-buffer)
13543                  (numberp article)
13544                  (equal (buffer-name (current-buffer))
13545                         (buffer-name (get-buffer gnus-article-buffer))))
13546         (save-excursion
13547           (if (get-buffer gnus-original-article-buffer)
13548               (set-buffer (get-buffer gnus-original-article-buffer))
13549             (set-buffer (get-buffer-create gnus-original-article-buffer))
13550             (buffer-disable-undo (current-buffer))
13551             (setq major-mode 'gnus-original-article-mode)
13552             (setq buffer-read-only t)
13553             (gnus-add-current-to-buffer-list))
13554           (let (buffer-read-only)
13555             (erase-buffer)
13556             (insert-buffer-substring gnus-article-buffer))
13557           (setq gnus-original-article (cons group article))))
13558     
13559       ;; Update sparse articles.
13560       (when (and do-update-line
13561                  (or (numberp article)
13562                      (stringp article)))
13563         (let ((buf (current-buffer)))
13564           (set-buffer gnus-summary-buffer)
13565           (gnus-summary-update-article do-update-line)
13566           (gnus-summary-goto-subject do-update-line nil t)
13567           (set-window-point (get-buffer-window (current-buffer) t)
13568                             (point))
13569           (set-buffer buf))))))
13570
13571 (defun gnus-read-header (id &optional header)
13572   "Read the headers of article ID and enter them into the Gnus system."
13573   (let ((group gnus-newsgroup-name)
13574         (gnus-override-method 
13575          (and (gnus-news-group-p gnus-newsgroup-name)
13576               gnus-refer-article-method))       
13577         where)
13578     ;; First we check to see whether the header in question is already
13579     ;; fetched.
13580     (if (stringp id)
13581         ;; This is a Message-ID.
13582         (setq header (or header (gnus-id-to-header id)))
13583       ;; This is an article number.
13584       (setq header (or header (gnus-summary-article-header id))))
13585     (if (and header
13586              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13587         ;; We have found the header.
13588         header
13589       ;; We have to really fetch the header to this article.
13590       (when (setq where
13591                   (if (gnus-check-backend-function 'request-head group)
13592                       (gnus-request-head id group)
13593                     (gnus-request-article id group)))
13594         (save-excursion
13595           (set-buffer nntp-server-buffer)
13596           (and (search-forward "\n\n" nil t)
13597                (delete-region (1- (point)) (point-max)))
13598           (goto-char (point-max))
13599           (insert ".\n")
13600           (goto-char (point-min))
13601           (insert "211 ")
13602           (princ (cond
13603                   ((numberp id) id)
13604                   ((cdr where) (cdr where))
13605                   (header (mail-header-number header))
13606                   (t gnus-reffed-article-number))
13607                  (current-buffer))
13608           (insert " Article retrieved.\n"))
13609         ;(when (and header
13610         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13611         ;  (setcar (gnus-id-to-thread id) nil))
13612         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13613             ()                          ; Malformed head.
13614           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13615             (if (and (stringp id)
13616                      (not (string= (gnus-group-real-name group)
13617                                    (car where))))
13618                 ;; If we fetched by Message-ID and the article came
13619                 ;; from a different group, we fudge some bogus article
13620                 ;; numbers for this article.
13621                 (mail-header-set-number header gnus-reffed-article-number))
13622             (decf gnus-reffed-article-number)
13623             (push header gnus-newsgroup-headers)
13624             (setq gnus-current-headers header)
13625             (push (mail-header-number header) gnus-newsgroup-limit))
13626           header)))))
13627
13628 (defun gnus-article-prepare (article &optional all-headers header)
13629   "Prepare ARTICLE in article mode buffer.
13630 ARTICLE should either be an article number or a Message-ID.
13631 If ARTICLE is an id, HEADER should be the article headers.
13632 If ALL-HEADERS is non-nil, no headers are hidden."
13633   (save-excursion
13634     ;; Make sure we start in a summary buffer.
13635     (unless (eq major-mode 'gnus-summary-mode)
13636       (set-buffer gnus-summary-buffer))
13637     (setq gnus-summary-buffer (current-buffer))
13638     ;; Make sure the connection to the server is alive.
13639     (unless (gnus-server-opened
13640              (gnus-find-method-for-group gnus-newsgroup-name))
13641       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13642       (gnus-request-group gnus-newsgroup-name t))
13643     (let* ((article (if header (mail-header-number header) article))
13644            (summary-buffer (current-buffer))
13645            (internal-hook gnus-article-internal-prepare-hook)
13646            (group gnus-newsgroup-name)
13647            result)
13648       (save-excursion
13649         (gnus-article-setup-buffer)
13650         (set-buffer gnus-article-buffer)
13651         ;; Deactivate active regions.
13652         (when (and (boundp 'transient-mark-mode)
13653                    transient-mark-mode)
13654           (setq mark-active nil))
13655         (if (not (setq result (let ((buffer-read-only nil))
13656                                 (gnus-request-article-this-buffer
13657                                  article group))))
13658             ;; There is no such article.
13659             (save-excursion
13660               (when (and (numberp article)
13661                          (not (memq article gnus-newsgroup-sparse)))
13662                 (setq gnus-article-current
13663                       (cons gnus-newsgroup-name article))
13664                 (set-buffer gnus-summary-buffer)
13665                 (setq gnus-current-article article)
13666                 (gnus-summary-mark-article article gnus-canceled-mark))
13667               (unless (memq article gnus-newsgroup-sparse)
13668                 (gnus-error
13669                  1 "No such article (may have expired or been canceled)")))
13670           (if (or (eq result 'pseudo) (eq result 'nneething))
13671               (progn
13672                 (save-excursion
13673                   (set-buffer summary-buffer)
13674                   (setq gnus-last-article gnus-current-article
13675                         gnus-newsgroup-history (cons gnus-current-article
13676                                                      gnus-newsgroup-history)
13677                         gnus-current-article 0
13678                         gnus-current-headers nil
13679                         gnus-article-current nil)
13680                   (if (eq result 'nneething)
13681                       (gnus-configure-windows 'summary)
13682                     (gnus-configure-windows 'article))
13683                   (gnus-set-global-variables))
13684                 (gnus-set-mode-line 'article))
13685             ;; The result from the `request' was an actual article -
13686             ;; or at least some text that is now displayed in the
13687             ;; article buffer.
13688             (if (and (numberp article)
13689                      (not (eq article gnus-current-article)))
13690                 ;; Seems like a new article has been selected.
13691                 ;; `gnus-current-article' must be an article number.
13692                 (save-excursion
13693                   (set-buffer summary-buffer)
13694                   (setq gnus-last-article gnus-current-article
13695                         gnus-newsgroup-history (cons gnus-current-article
13696                                                      gnus-newsgroup-history)
13697                         gnus-current-article article
13698                         gnus-current-headers
13699                         (gnus-summary-article-header gnus-current-article)
13700                         gnus-article-current
13701                         (cons gnus-newsgroup-name gnus-current-article))
13702                   (unless (vectorp gnus-current-headers)
13703                     (setq gnus-current-headers nil))
13704                   (gnus-summary-show-thread)
13705                   (run-hooks 'gnus-mark-article-hook)
13706                   (gnus-set-mode-line 'summary)
13707                   (and (gnus-visual-p 'article-highlight 'highlight)
13708                        (run-hooks 'gnus-visual-mark-article-hook))
13709                   ;; Set the global newsgroup variables here.
13710                   ;; Suggested by Jim Sisolak
13711                   ;; <sisolak@trans4.neep.wisc.edu>.
13712                   (gnus-set-global-variables)
13713                   (setq gnus-have-all-headers
13714                         (or all-headers gnus-show-all-headers))
13715                   (and gnus-use-cache
13716                        (vectorp (gnus-summary-article-header article))
13717                        (gnus-cache-possibly-enter-article
13718                         group article
13719                         (gnus-summary-article-header article)
13720                         (memq article gnus-newsgroup-marked)
13721                         (memq article gnus-newsgroup-dormant)
13722                         (memq article gnus-newsgroup-unreads)))))
13723             (when (or (numberp article)
13724                       (stringp article))
13725               ;; Hooks for getting information from the article.
13726               ;; This hook must be called before being narrowed.
13727               (let (buffer-read-only)
13728                 (run-hooks 'internal-hook)
13729                 (run-hooks 'gnus-article-prepare-hook)
13730                 ;; Decode MIME message.
13731                 (if gnus-show-mime
13732                     (if (or (not gnus-strict-mime)
13733                             (gnus-fetch-field "Mime-Version"))
13734                         (funcall gnus-show-mime-method)
13735                       (funcall gnus-decode-encoded-word-method)))
13736                 ;; Perform the article display hooks.
13737                 (run-hooks 'gnus-article-display-hook))
13738               ;; Do page break.
13739               (goto-char (point-min))
13740               (and gnus-break-pages (gnus-narrow-to-page)))
13741             (gnus-set-mode-line 'article)
13742             (gnus-configure-windows 'article)
13743             (goto-char (point-min))
13744             t))))))
13745
13746 (defun gnus-article-show-all-headers ()
13747   "Show all article headers in article mode buffer."
13748   (save-excursion
13749     (gnus-article-setup-buffer)
13750     (set-buffer gnus-article-buffer)
13751     (let ((buffer-read-only nil))
13752       (gnus-unhide-text (point-min) (point-max)))))
13753
13754 (defun gnus-article-hide-headers-if-wanted ()
13755   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13756 Provided for backwards compatibility."
13757   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13758       gnus-inhibit-hiding
13759       (gnus-article-hide-headers)))
13760
13761 (defsubst gnus-article-header-rank ()
13762   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13763   (let ((list gnus-sorted-header-list)
13764         (i 0))
13765     (while list
13766       (when (looking-at (car list))
13767         (setq list nil))
13768       (setq list (cdr list))
13769       (incf i))
13770     i))
13771
13772 (defun gnus-article-hide-headers (&optional arg delete)
13773   "Toggle whether to hide unwanted headers and possibly sort them as well.
13774 If given a negative prefix, always show; if given a positive prefix,
13775 always hide."
13776   (interactive "P")
13777   (unless (gnus-article-check-hidden-text 'headers arg)
13778     ;; This function might be inhibited.
13779     (unless gnus-inhibit-hiding
13780       (save-excursion
13781         (set-buffer gnus-article-buffer)
13782         (save-restriction
13783           (let ((buffer-read-only nil)
13784                 (props (nconc (list 'gnus-type 'headers)
13785                               gnus-hidden-properties))
13786                 (max (1+ (length gnus-sorted-header-list)))
13787                 (ignored (when (not (stringp gnus-visible-headers))
13788                            (cond ((stringp gnus-ignored-headers)
13789                                   gnus-ignored-headers)
13790                                  ((listp gnus-ignored-headers)
13791                                   (mapconcat 'identity gnus-ignored-headers
13792                                              "\\|")))))
13793                 (visible
13794                  (cond ((stringp gnus-visible-headers)
13795                         gnus-visible-headers)
13796                        ((and gnus-visible-headers
13797                              (listp gnus-visible-headers))
13798                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13799                 want-list beg)
13800             ;; First we narrow to just the headers.
13801             (widen)
13802             (goto-char (point-min))
13803             ;; Hide any "From " lines at the beginning of (mail) articles.
13804             (while (looking-at "From ")
13805               (forward-line 1))
13806             (unless (bobp)
13807               (gnus-hide-text (point-min) (point) props))
13808             ;; Then treat the rest of the header lines.
13809             (narrow-to-region
13810              (point)
13811              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13812             ;; Then we use the two regular expressions
13813             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13814             ;; select which header lines is to remain visible in the
13815             ;; article buffer.
13816             (goto-char (point-min))
13817             (while (re-search-forward "^[^ \t]*:" nil t)
13818               (beginning-of-line)
13819               ;; We add the headers we want to keep to a list and delete
13820               ;; them from the buffer.
13821               (gnus-put-text-property 
13822                (point) (1+ (point)) 'message-rank
13823                (if (or (and visible (looking-at visible))
13824                        (and ignored
13825                             (not (looking-at ignored))))
13826                    (gnus-article-header-rank) 
13827                  (+ 2 max)))
13828               (forward-line 1))
13829             (message-sort-headers-1)
13830             (when (setq beg (text-property-any 
13831                              (point-min) (point-max) 'message-rank (+ 2 max)))
13832               ;; We make the unwanted headers invisible.
13833               (if delete
13834                   (delete-region beg (point-max))
13835                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13836                 (gnus-hide-text-type beg (point-max) 'headers))
13837               ;; Work around XEmacs lossage.
13838               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13839
13840 (defun gnus-article-hide-boring-headers (&optional arg)
13841   "Toggle hiding of headers that aren't very interesting.
13842 If given a negative prefix, always show; if given a positive prefix,
13843 always hide."
13844   (interactive "P")
13845   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13846     (save-excursion
13847       (set-buffer gnus-article-buffer)
13848       (save-restriction
13849         (let ((buffer-read-only nil)
13850               (list gnus-boring-article-headers)
13851               (inhibit-point-motion-hooks t)
13852               elem)
13853           (nnheader-narrow-to-headers)
13854           (while list
13855             (setq elem (pop list))
13856             (goto-char (point-min))
13857             (cond
13858              ;; Hide empty headers.
13859              ((eq elem 'empty)
13860               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13861                 (forward-line -1)
13862                 (gnus-hide-text-type
13863                  (progn (beginning-of-line) (point))
13864                  (progn 
13865                    (end-of-line)
13866                    (if (re-search-forward "^[^ \t]" nil t)
13867                        (match-beginning 0)
13868                      (point-max)))
13869                  'boring-headers)))
13870              ;; Hide boring Newsgroups header.
13871              ((eq elem 'newsgroups)
13872               (when (equal (message-fetch-field "newsgroups")
13873                            (gnus-group-real-name gnus-newsgroup-name))
13874                 (gnus-article-hide-header "newsgroups")))
13875              ((eq elem 'followup-to)
13876               (when (equal (message-fetch-field "followup-to")
13877                            (message-fetch-field "newsgroups"))
13878                 (gnus-article-hide-header "followup-to")))
13879              ((eq elem 'reply-to)
13880               (let ((from (message-fetch-field "from"))
13881                     (reply-to (message-fetch-field "reply-to")))
13882                 (when (and
13883                        from reply-to
13884                        (equal 
13885                         (nth 1 (funcall gnus-extract-address-components from))
13886                         (nth 1 (funcall gnus-extract-address-components
13887                                         reply-to))))
13888                   (gnus-article-hide-header "reply-to"))))
13889              ((eq elem 'date)
13890               (let ((date (message-fetch-field "date")))
13891                 (when (and date
13892                            (< (gnus-days-between date (current-time-string))
13893                               4))
13894                   (gnus-article-hide-header "date")))))))))))
13895
13896 (defun gnus-article-hide-header (header)
13897   (save-excursion
13898     (goto-char (point-min))
13899     (when (re-search-forward (concat "^" header ":") nil t)
13900       (gnus-hide-text-type
13901        (progn (beginning-of-line) (point))
13902        (progn 
13903          (end-of-line)
13904          (if (re-search-forward "^[^ \t]" nil t)
13905              (match-beginning 0)
13906            (point-max)))
13907        'boring-headers))))
13908
13909 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13910 (defun gnus-article-treat-overstrike ()
13911   "Translate overstrikes into bold text."
13912   (interactive)
13913   (save-excursion
13914     (set-buffer gnus-article-buffer)
13915     (let ((buffer-read-only nil))
13916       (while (search-forward "\b" nil t)
13917         (let ((next (following-char))
13918               (previous (char-after (- (point) 2))))
13919           (cond ((eq next previous)
13920                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13921                  (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
13922                 ((eq next ?_)
13923                  (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
13924                  (gnus-put-text-property
13925                   (- (point) 2) (1- (point)) 'face 'underline))
13926                 ((eq previous ?_)
13927                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13928                  (gnus-put-text-property
13929                   (point) (1+ (point))  'face 'underline))))))))
13930
13931 (defun gnus-article-word-wrap ()
13932   "Format too long lines."
13933   (interactive)
13934   (save-excursion
13935     (set-buffer gnus-article-buffer)
13936     (let ((buffer-read-only nil))
13937       (widen)
13938       (goto-char (point-min))
13939       (search-forward "\n\n" nil t)
13940       (end-of-line 1)
13941       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13942             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13943             (adaptive-fill-mode t))
13944         (while (not (eobp))
13945           (and (>= (current-column) (min fill-column (window-width)))
13946                (/= (preceding-char) ?:)
13947                (fill-paragraph nil))
13948           (end-of-line 2))))))
13949
13950 (defun gnus-article-remove-cr ()
13951   "Remove carriage returns from an article."
13952   (interactive)
13953   (save-excursion
13954     (set-buffer gnus-article-buffer)
13955     (let ((buffer-read-only nil))
13956       (goto-char (point-min))
13957       (while (search-forward "\r" nil t)
13958         (replace-match "" t t)))))
13959
13960 (defun gnus-article-remove-trailing-blank-lines ()
13961   "Remove all trailing blank lines from the article."
13962   (interactive)
13963   (save-excursion
13964     (set-buffer gnus-article-buffer)
13965     (let ((buffer-read-only nil))
13966       (goto-char (point-max))
13967       (delete-region
13968        (point)
13969        (progn
13970          (while (looking-at "^[ \t]*$")
13971            (forward-line -1))
13972          (forward-line 1)
13973          (point))))))
13974
13975 (defun gnus-article-display-x-face (&optional force)
13976   "Look for an X-Face header and display it if present."
13977   (interactive (list 'force))
13978   (save-excursion
13979     (set-buffer gnus-article-buffer)
13980     ;; Delete the old process, if any.
13981     (when (process-status "gnus-x-face")
13982       (delete-process "gnus-x-face"))
13983     (let ((inhibit-point-motion-hooks t)
13984           (case-fold-search nil)
13985           from)
13986       (save-restriction
13987         (nnheader-narrow-to-headers)
13988         (setq from (message-fetch-field "from"))
13989         (goto-char (point-min))
13990         (when (and gnus-article-x-face-command
13991                    (or force
13992                        ;; Check whether this face is censored.
13993                        (not gnus-article-x-face-too-ugly)
13994                        (and gnus-article-x-face-too-ugly from
13995                             (not (string-match gnus-article-x-face-too-ugly
13996                                                from))))
13997                    ;; Has to be present.
13998                    (re-search-forward "^X-Face: " nil t))
13999           ;; We now have the area of the buffer where the X-Face is stored.
14000           (let ((beg (point))
14001                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14002             ;; We display the face.
14003             (if (symbolp gnus-article-x-face-command)
14004                 ;; The command is a lisp function, so we call it.
14005                 (if (gnus-functionp gnus-article-x-face-command)
14006                     (funcall gnus-article-x-face-command beg end)
14007                   (error "%s is not a function" gnus-article-x-face-command))
14008               ;; The command is a string, so we interpret the command
14009               ;; as a, well, command, and fork it off.
14010               (let ((process-connection-type nil))
14011                 (process-kill-without-query
14012                  (start-process
14013                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14014                 (process-send-region "gnus-x-face" beg end)
14015                 (process-send-eof "gnus-x-face")))))))))
14016
14017 (defalias 'gnus-header-decode-quoted-printable 'gnus-decode-rfc1522)
14018 (defun gnus-decode-rfc1522 ()
14019   "Hack to remove QP encoding from headers."
14020   (let ((case-fold-search t)
14021         (inhibit-point-motion-hooks t)
14022         (buffer-read-only nil)
14023         string)
14024     (save-restriction
14025       (narrow-to-region
14026        (goto-char (point-min))
14027        (or (search-forward "\n\n" nil t) (point-max)))
14028
14029       (while (re-search-forward 
14030               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14031         (setq string (match-string 1))
14032         (narrow-to-region (match-beginning 0) (match-end 0))
14033         (delete-region (point-min) (point-max))
14034         (insert string)
14035         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14036         (subst-char-in-region (point-min) (point-max) ?_ ? )
14037         (widen)
14038         (goto-char (point-min))))))
14039
14040 (defun gnus-article-de-quoted-unreadable (&optional force)
14041   "Do a naive translation of a quoted-printable-encoded article.
14042 This is in no way, shape or form meant as a replacement for real MIME
14043 processing, but is simply a stop-gap measure until MIME support is
14044 written.
14045 If FORCE, decode the article whether it is marked as quoted-printable
14046 or not."
14047   (interactive (list 'force))
14048   (save-excursion
14049     (set-buffer gnus-article-buffer)
14050     (let ((case-fold-search t)
14051           (buffer-read-only nil)
14052           (type (gnus-fetch-field "content-transfer-encoding")))
14053       (gnus-decode-rfc1522)
14054       (when (or force
14055                 (and type (string-match "quoted-printable" (downcase type))))
14056         (goto-char (point-min))
14057         (search-forward "\n\n" nil 'move)
14058         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14059
14060 (defun gnus-mime-decode-quoted-printable (from to)
14061   "Decode Quoted-Printable in the region between FROM and TO."
14062   (interactive "r")
14063   (goto-char from)
14064   (while (search-forward "=" to t)
14065     (cond ((eq (following-char) ?\n)
14066            (delete-char -1)
14067            (delete-char 1))
14068           ((looking-at "[0-9A-F][0-9A-F]")
14069            (subst-char-in-region
14070             (1- (point)) (point) ?=
14071             (hexl-hex-string-to-integer
14072              (buffer-substring (point) (+ 2 (point)))))
14073            (delete-char 2))
14074           ((looking-at "=")
14075            (delete-char 1))
14076           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14077
14078 (defun gnus-article-hide-pgp (&optional arg)
14079   "Toggle hiding of any PGP headers and signatures in the current article.
14080 If given a negative prefix, always show; if given a positive prefix,
14081 always hide."
14082   (interactive "P")
14083   (unless (gnus-article-check-hidden-text 'pgp arg)
14084     (save-excursion
14085       (set-buffer gnus-article-buffer)
14086       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14087             buffer-read-only beg end)
14088         (widen)
14089         (goto-char (point-min))
14090         ;; Hide the "header".
14091         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14092              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14093         (setq beg (point))
14094         ;; Hide the actual signature.
14095         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14096              (setq end (1+ (match-beginning 0)))
14097              (gnus-hide-text
14098               end
14099               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14100                   (match-end 0)
14101                 ;; Perhaps we shouldn't hide to the end of the buffer
14102                 ;; if there is no end to the signature?
14103                 (point-max))
14104               props))
14105         ;; Hide "- " PGP quotation markers.
14106         (when (and beg end)
14107           (narrow-to-region beg end)
14108           (goto-char (point-min))
14109           (while (re-search-forward "^- " nil t)
14110             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14111           (widen))))))
14112
14113 (defun gnus-article-hide-signature (&optional arg)
14114   "Hide the signature in the current article.
14115 If given a negative prefix, always show; if given a positive prefix,
14116 always hide."
14117   (interactive "P")
14118   (unless (gnus-article-check-hidden-text 'signature arg)
14119     (save-excursion
14120       (set-buffer gnus-article-buffer)
14121       (save-restriction
14122         (let ((buffer-read-only nil))
14123           (when (gnus-narrow-to-signature)
14124             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14125
14126 (defun gnus-article-strip-leading-blank-lines ()
14127   "Remove all blank lines from the beginning of the article."
14128   (interactive)
14129   (save-excursion
14130     (set-buffer gnus-article-buffer)
14131     (let (buffer-read-only)
14132       (goto-char (point-min))
14133       (when (search-forward "\n\n" nil t)
14134         (while (looking-at "[ \t]$")
14135           (gnus-delete-line))))))
14136
14137 (defun gnus-narrow-to-signature ()
14138   "Narrow to the signature."
14139   (widen)
14140   (goto-char (point-max))
14141   (when (re-search-backward gnus-signature-separator nil t)
14142     (forward-line 1)
14143     (when (or (null gnus-signature-limit)
14144               (and (numberp gnus-signature-limit)
14145                    (< (- (point-max) (point)) gnus-signature-limit))
14146               (and (gnus-functionp gnus-signature-limit)
14147                    (funcall gnus-signature-limit))
14148               (and (stringp gnus-signature-limit)
14149                    (not (re-search-forward gnus-signature-limit nil t))))
14150       (narrow-to-region (point) (point-max))
14151       t)))
14152
14153 (defun gnus-article-check-hidden-text (type arg)
14154   "Return nil if hiding is necessary."
14155   (save-excursion
14156     (set-buffer gnus-article-buffer)
14157     (let ((hide (gnus-article-hidden-text-p type)))
14158       (cond ((or (and (null arg) (eq hide 'hidden))
14159                  (and arg (< 0 (prefix-numeric-value arg))))
14160              (gnus-article-show-hidden-text type))
14161             ((eq hide 'shown)
14162              (gnus-article-show-hidden-text type t))
14163             (t nil)))))
14164
14165 (defun gnus-article-hidden-text-p (type)
14166   "Say whether the current buffer contains hidden text of type TYPE."
14167   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14168     (when pos
14169       (if (get-text-property pos 'invisible)
14170           'hidden
14171         'shown))))
14172
14173 (defun gnus-article-hide (&optional arg force)
14174   "Hide all the gruft in the current article.
14175 This means that PGP stuff, signatures, cited text and (some)
14176 headers will be hidden.
14177 If given a prefix, show the hidden text instead."
14178   (interactive (list current-prefix-arg 'force))
14179   (gnus-article-hide-headers arg)
14180   (gnus-article-hide-pgp arg)
14181   (gnus-article-hide-citation-maybe arg force)
14182   (gnus-article-hide-signature arg))
14183
14184 (defun gnus-article-show-hidden-text (type &optional hide)
14185   "Show all hidden text of type TYPE.
14186 If HIDE, hide the text instead."
14187   (save-excursion
14188     (set-buffer gnus-article-buffer)
14189     (let ((buffer-read-only nil)
14190           (inhibit-point-motion-hooks t)
14191           (beg (point-min)))
14192       (while (gnus-goto-char (text-property-any
14193                               beg (point-max) 'gnus-type type))
14194         (setq beg (point))
14195         (forward-char)
14196         (if hide
14197             (gnus-hide-text beg (point) gnus-hidden-properties)
14198           (gnus-unhide-text beg (point)))
14199         (setq beg (point)))
14200       t)))
14201
14202 (defvar gnus-article-time-units
14203   `((year . ,(* 365.25 24 60 60))
14204     (week . ,(* 7 24 60 60))
14205     (day . ,(* 24 60 60))
14206     (hour . ,(* 60 60))
14207     (minute . 60)
14208     (second . 1))
14209   "Mapping from time units to seconds.")
14210
14211 (defun gnus-article-date-ut (&optional type highlight)
14212   "Convert DATE date to universal time in the current article.
14213 If TYPE is `local', convert to local time; if it is `lapsed', output
14214 how much time has lapsed since DATE."
14215   (interactive (list 'ut t))
14216   (let* ((header (or gnus-current-headers
14217                      (gnus-summary-article-header) ""))
14218          (date (and (vectorp header) (mail-header-date header)))
14219          (date-regexp "^Date: \\|^X-Sent: ")
14220          (now (current-time))
14221          (inhibit-point-motion-hooks t)
14222          bface eface)
14223     (when (and date (not (string= date "")))
14224       (save-excursion
14225         (set-buffer gnus-article-buffer)
14226         (save-restriction
14227           (nnheader-narrow-to-headers)
14228           (let ((buffer-read-only nil))
14229             ;; Delete any old Date headers.
14230             (if (re-search-forward date-regexp nil t)
14231                 (progn
14232                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14233                         eface (get-text-property (1- (gnus-point-at-eol))
14234                                                  'face))
14235                   (message-remove-header date-regexp t)
14236                   (beginning-of-line))
14237               (goto-char (point-max)))
14238             (insert (gnus-make-date-line date type))
14239             ;; Do highlighting.
14240             (forward-line -1)
14241             (when (and (gnus-visual-p 'article-highlight 'highlight)
14242                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14243               (gnus-put-text-property (match-beginning 1) (match-end 1)
14244                                  'face bface)
14245               (gnus-put-text-property (match-beginning 2) (match-end 2)
14246                                  'face eface))))))))
14247
14248 (defun gnus-make-date-line (date type)
14249   "Return a DATE line of TYPE."
14250   (cond
14251    ;; Convert to the local timezone.  We have to slap a
14252    ;; `condition-case' round the calls to the timezone
14253    ;; functions since they aren't particularly resistant to
14254    ;; buggy dates.
14255    ((eq type 'local)
14256     (concat "Date: " (condition-case ()
14257                          (timezone-make-date-arpa-standard date)
14258                        (error date))
14259             "\n"))
14260    ;; Convert to Universal Time.
14261    ((eq type 'ut)
14262     (concat "Date: "
14263             (condition-case ()
14264                 (timezone-make-date-arpa-standard date nil "UT")
14265               (error date))
14266             "\n"))
14267    ;; Get the original date from the article.
14268    ((eq type 'original)
14269     (concat "Date: " date "\n"))
14270    ;; Do an X-Sent lapsed format.
14271    ((eq type 'lapsed)
14272     ;; If the date is seriously mangled, the timezone
14273     ;; functions are liable to bug out, so we condition-case
14274     ;; the entire thing.
14275     (let* ((now (current-time))
14276            (real-time
14277             (condition-case ()
14278                 (gnus-time-minus
14279                  (gnus-encode-date
14280                   (timezone-make-date-arpa-standard
14281                    (current-time-string now)
14282                    (current-time-zone now) "UT"))
14283                  (gnus-encode-date
14284                   (timezone-make-date-arpa-standard
14285                    date nil "UT")))
14286               (error '(0 0))))
14287            (real-sec (+ (* (float (car real-time)) 65536)
14288                         (cadr real-time)))
14289            (sec (abs real-sec))
14290            num prev)
14291       (cond
14292        ((equal real-time '(0 0))
14293         "X-Sent: Unknown\n")
14294        ((zerop sec)
14295         "X-Sent: Now\n")
14296        (t
14297         (concat
14298          "X-Sent: "
14299          ;; This is a bit convoluted, but basically we go
14300          ;; through the time units for years, weeks, etc,
14301          ;; and divide things to see whether that results
14302          ;; in positive answers.
14303          (mapconcat
14304           (lambda (unit)
14305             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14306                 ;; The (remaining) seconds are too few to
14307                 ;; be divided into this time unit.
14308                 ""
14309               ;; It's big enough, so we output it.
14310               (setq sec (- sec (* num (cdr unit))))
14311               (prog1
14312                   (concat (if prev ", " "") (int-to-string
14313                                              (floor num))
14314                           " " (symbol-name (car unit))
14315                           (if (> num 1) "s" ""))
14316                 (setq prev t))))
14317           gnus-article-time-units "")
14318          ;; If dates are odd, then it might appear like the
14319          ;; article was sent in the future.
14320          (if (> real-sec 0)
14321              " ago\n"
14322            " in the future\n"))))))
14323    (t
14324     (error "Unknown conversion type: %s" type))))
14325
14326 (defun gnus-article-date-local (&optional highlight)
14327   "Convert the current article date to the local timezone."
14328   (interactive (list t))
14329   (gnus-article-date-ut 'local highlight))
14330
14331 (defun gnus-article-date-original (&optional highlight)
14332   "Convert the current article date to what it was originally.
14333 This is only useful if you have used some other date conversion
14334 function and want to see what the date was before converting."
14335   (interactive (list t))
14336   (gnus-article-date-ut 'original highlight))
14337
14338 (defun gnus-article-date-lapsed (&optional highlight)
14339   "Convert the current article date to time lapsed since it was sent."
14340   (interactive (list t))
14341   (gnus-article-date-ut 'lapsed highlight))
14342
14343 (defun gnus-article-maybe-highlight ()
14344   "Do some article highlighting if `gnus-visual' is non-nil."
14345   (if (gnus-visual-p 'article-highlight 'highlight)
14346       (gnus-article-highlight-some)))
14347
14348 ;; Article savers.
14349
14350 (defun gnus-output-to-rmail (file-name)
14351   "Append the current article to an Rmail file named FILE-NAME."
14352   (require 'rmail)
14353   ;; Most of these codes are borrowed from rmailout.el.
14354   (setq file-name (expand-file-name file-name))
14355   (setq rmail-default-rmail-file file-name)
14356   (let ((artbuf (current-buffer))
14357         (tmpbuf (get-buffer-create " *Gnus-output*")))
14358     (save-excursion
14359       (or (get-file-buffer file-name)
14360           (file-exists-p file-name)
14361           (if (gnus-yes-or-no-p
14362                (concat "\"" file-name "\" does not exist, create it? "))
14363               (let ((file-buffer (create-file-buffer file-name)))
14364                 (save-excursion
14365                   (set-buffer file-buffer)
14366                   (rmail-insert-rmail-file-header)
14367                   (let ((require-final-newline nil))
14368                     (write-region (point-min) (point-max) file-name t 1)))
14369                 (kill-buffer file-buffer))
14370             (error "Output file does not exist")))
14371       (set-buffer tmpbuf)
14372       (buffer-disable-undo (current-buffer))
14373       (erase-buffer)
14374       (insert-buffer-substring artbuf)
14375       (gnus-convert-article-to-rmail)
14376       ;; Decide whether to append to a file or to an Emacs buffer.
14377       (let ((outbuf (get-file-buffer file-name)))
14378         (if (not outbuf)
14379             (append-to-file (point-min) (point-max) file-name)
14380           ;; File has been visited, in buffer OUTBUF.
14381           (set-buffer outbuf)
14382           (let ((buffer-read-only nil)
14383                 (msg (and (boundp 'rmail-current-message)
14384                           (symbol-value 'rmail-current-message))))
14385             ;; If MSG is non-nil, buffer is in RMAIL mode.
14386             (if msg
14387                 (progn (widen)
14388                        (narrow-to-region (point-max) (point-max))))
14389             (insert-buffer-substring tmpbuf)
14390             (if msg
14391                 (progn
14392                   (goto-char (point-min))
14393                   (widen)
14394                   (search-backward "\^_")
14395                   (narrow-to-region (point) (point-max))
14396                   (goto-char (1+ (point-min)))
14397                   (rmail-count-new-messages t)
14398                   (rmail-show-message msg)))))))
14399     (kill-buffer tmpbuf)))
14400
14401 (defun gnus-output-to-file (file-name)
14402   "Append the current article to a file named FILE-NAME."
14403   (setq file-name (expand-file-name file-name))
14404   (let ((artbuf (current-buffer))
14405         (tmpbuf (get-buffer-create " *Gnus-output*")))
14406     (save-excursion
14407       (set-buffer tmpbuf)
14408       (buffer-disable-undo (current-buffer))
14409       (erase-buffer)
14410       (insert-buffer-substring artbuf)
14411       ;; Append newline at end of the buffer as separator, and then
14412       ;; save it to file.
14413       (goto-char (point-max))
14414       (insert "\n")
14415       (append-to-file (point-min) (point-max) file-name))
14416     (kill-buffer tmpbuf)))
14417
14418 (defun gnus-convert-article-to-rmail ()
14419   "Convert article in current buffer to Rmail message format."
14420   (let ((buffer-read-only nil))
14421     ;; Convert article directly into Babyl format.
14422     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14423     (goto-char (point-min))
14424     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14425     (while (search-forward "\n\^_" nil t) ;single char
14426       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14427     (goto-char (point-max))
14428     (insert "\^_")))
14429
14430 (defun gnus-narrow-to-page (&optional arg)
14431   "Narrow the article buffer to a page.
14432 If given a numerical ARG, move forward ARG pages."
14433   (interactive "P")
14434   (setq arg (if arg (prefix-numeric-value arg) 0))
14435   (save-excursion
14436     (set-buffer gnus-article-buffer)
14437     (goto-char (point-min))
14438     (widen)
14439     (when (gnus-visual-p 'page-marker)
14440       (let ((buffer-read-only nil))
14441         (gnus-remove-text-with-property 'gnus-prev)
14442         (gnus-remove-text-with-property 'gnus-next)))
14443     (when
14444         (cond ((< arg 0)
14445                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14446               ((> arg 0)
14447                (re-search-forward page-delimiter nil 'move arg)))
14448       (goto-char (match-end 0)))
14449     (narrow-to-region
14450      (point)
14451      (if (re-search-forward page-delimiter nil 'move)
14452          (match-beginning 0)
14453        (point)))
14454     (when (and (gnus-visual-p 'page-marker)
14455                (not (= (point-min) 1)))
14456       (save-excursion
14457         (goto-char (point-min))
14458         (gnus-insert-prev-page-button)))
14459     (when (and (gnus-visual-p 'page-marker)
14460                (not (= (1- (point-max)) (buffer-size))))
14461       (save-excursion
14462         (goto-char (point-max))
14463         (gnus-insert-next-page-button)))))
14464
14465 ;; Article mode commands
14466
14467 (defun gnus-article-goto-next-page ()
14468   "Show the next page of the article."
14469   (interactive)
14470   (when (gnus-article-next-page)
14471     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14472
14473 (defun gnus-article-goto-prev-page ()
14474   "Show the next page of the article."
14475   (interactive)
14476   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14477     (gnus-article-prev-page nil)))
14478
14479 (defun gnus-article-next-page (&optional lines)
14480   "Show the next page of the current article.
14481 If end of article, return non-nil.  Otherwise return nil.
14482 Argument LINES specifies lines to be scrolled up."
14483   (interactive "p")
14484   (move-to-window-line -1)
14485   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14486   (if (save-excursion
14487         (end-of-line)
14488         (and (pos-visible-in-window-p)  ;Not continuation line.
14489              (eobp)))
14490       ;; Nothing in this page.
14491       (if (or (not gnus-break-pages)
14492               (save-excursion
14493                 (save-restriction
14494                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14495           t                             ;Nothing more.
14496         (gnus-narrow-to-page 1)         ;Go to next page.
14497         nil)
14498     ;; More in this page.
14499     (condition-case ()
14500         (scroll-up lines)
14501       (end-of-buffer
14502        ;; Long lines may cause an end-of-buffer error.
14503        (goto-char (point-max))))
14504     (move-to-window-line 0)
14505     nil))
14506
14507 (defun gnus-article-prev-page (&optional lines)
14508   "Show previous page of current article.
14509 Argument LINES specifies lines to be scrolled down."
14510   (interactive "p")
14511   (move-to-window-line 0)
14512   (if (and gnus-break-pages
14513            (bobp)
14514            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14515       (progn
14516         (gnus-narrow-to-page -1)        ;Go to previous page.
14517         (goto-char (point-max))
14518         (recenter -1))
14519     (prog1
14520         (condition-case ()
14521             (scroll-down lines)
14522           (error nil))
14523       (move-to-window-line 0))))
14524
14525 (defun gnus-article-refer-article ()
14526   "Read article specified by message-id around point."
14527   (interactive)
14528   (let ((point (point)))
14529     (search-forward ">" nil t)          ;Move point to end of "<....>".
14530     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14531         (let ((message-id (match-string 1)))
14532           (goto-char point)
14533           (set-buffer gnus-summary-buffer)
14534           (gnus-summary-refer-article message-id))
14535       (goto-char (point))
14536       (error "No references around point"))))
14537
14538 (defun gnus-article-show-summary ()
14539   "Reconfigure windows to show summary buffer."
14540   (interactive)
14541   (gnus-configure-windows 'article)
14542   (gnus-summary-goto-subject gnus-current-article))
14543
14544 (defun gnus-article-describe-briefly ()
14545   "Describe article mode commands briefly."
14546   (interactive)
14547   (gnus-message 6
14548                 (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")))
14549
14550 (defun gnus-article-summary-command ()
14551   "Execute the last keystroke in the summary buffer."
14552   (interactive)
14553   (let ((obuf (current-buffer))
14554         (owin (current-window-configuration))
14555         func)
14556     (switch-to-buffer gnus-summary-buffer 'norecord)
14557     (setq func (lookup-key (current-local-map) (this-command-keys)))
14558     (call-interactively func)
14559     (set-buffer obuf)
14560     (set-window-configuration owin)
14561     (set-window-point (get-buffer-window (current-buffer)) (point))))
14562
14563 (defun gnus-article-summary-command-nosave ()
14564   "Execute the last keystroke in the summary buffer."
14565   (interactive)
14566   (let (func)
14567     (pop-to-buffer gnus-summary-buffer 'norecord)
14568     (setq func (lookup-key (current-local-map) (this-command-keys)))
14569     (call-interactively func)))
14570
14571 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14572   "Read a summary buffer key sequence and execute it from the article buffer."
14573   (interactive "P")
14574   (let ((nosaves
14575          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14576            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14577            "=" "^" "\M-^"))
14578         keys)
14579     (save-excursion
14580       (set-buffer gnus-summary-buffer)
14581       (push (or key last-command-event) unread-command-events)
14582       (setq keys (read-key-sequence nil)))
14583     (message "")
14584
14585     (if (member keys nosaves)
14586         (let (func)
14587           (pop-to-buffer gnus-summary-buffer 'norecord)
14588           (if (setq func (lookup-key (current-local-map) keys))
14589               (call-interactively func)
14590             (ding)))
14591       (let ((obuf (current-buffer))
14592             (owin (current-window-configuration))
14593             (opoint (point))
14594             func in-buffer)
14595         (if not-restore-window
14596             (pop-to-buffer gnus-summary-buffer 'norecord)
14597           (switch-to-buffer gnus-summary-buffer 'norecord))
14598         (setq in-buffer (current-buffer))
14599         (if (setq func (lookup-key (current-local-map) keys))
14600             (call-interactively func)
14601           (ding))
14602         (when (eq in-buffer (current-buffer))
14603           (set-buffer obuf)
14604           (unless not-restore-window
14605             (set-window-configuration owin))
14606           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14607
14608 \f
14609 ;;;
14610 ;;; Kill file handling.
14611 ;;;
14612
14613 ;;;###autoload
14614 (defalias 'gnus-batch-kill 'gnus-batch-score)
14615 ;;;###autoload
14616 (defun gnus-batch-score ()
14617   "Run batched scoring.
14618 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14619 Newsgroups is a list of strings in Bnews format.  If you want to score
14620 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14621 score the alt hierarchy, you'd say \"!alt.all\"."
14622   (interactive)
14623   (let* ((yes-and-no
14624           (gnus-newsrc-parse-options
14625            (apply (function concat)
14626                   (mapcar (lambda (g) (concat g " "))
14627                           command-line-args-left))))
14628          (gnus-expert-user t)
14629          (nnmail-spool-file nil)
14630          (gnus-use-dribble-file nil)
14631          (yes (car yes-and-no))
14632          (no (cdr yes-and-no))
14633          group newsrc entry
14634          ;; Disable verbose message.
14635          gnus-novice-user gnus-large-newsgroup)
14636     ;; Eat all arguments.
14637     (setq command-line-args-left nil)
14638     ;; Start Gnus.
14639     (gnus)
14640     ;; Apply kills to specified newsgroups in command line arguments.
14641     (setq newsrc (cdr gnus-newsrc-alist))
14642     (while newsrc
14643       (setq group (caar newsrc))
14644       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14645       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14646                (and (car entry)
14647                     (or (eq (car entry) t)
14648                         (not (zerop (car entry)))))
14649                (if yes (string-match yes group) t)
14650                (or (null no) (not (string-match no group))))
14651           (progn
14652             (gnus-summary-read-group group nil t nil t)
14653             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14654                  (gnus-summary-exit))))
14655       (setq newsrc (cdr newsrc)))
14656     ;; Exit Emacs.
14657     (switch-to-buffer gnus-group-buffer)
14658     (gnus-group-save-newsrc)))
14659
14660 (defun gnus-apply-kill-file ()
14661   "Apply a kill file to the current newsgroup.
14662 Returns the number of articles marked as read."
14663   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14664           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14665       (gnus-apply-kill-file-internal)
14666     0))
14667
14668 (defun gnus-kill-save-kill-buffer ()
14669   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14670     (when (get-file-buffer file)
14671       (save-excursion
14672         (set-buffer (get-file-buffer file))
14673         (and (buffer-modified-p) (save-buffer))
14674         (kill-buffer (current-buffer))))))
14675
14676 (defvar gnus-kill-file-name "KILL"
14677   "Suffix of the kill files.")
14678
14679 (defun gnus-newsgroup-kill-file (newsgroup)
14680   "Return the name of a kill file name for NEWSGROUP.
14681 If NEWSGROUP is nil, return the global kill file name instead."
14682   (cond 
14683    ;; The global KILL file is placed at top of the directory.
14684    ((or (null newsgroup)
14685         (string-equal newsgroup ""))
14686     (expand-file-name gnus-kill-file-name
14687                       gnus-kill-files-directory))
14688    ;; Append ".KILL" to newsgroup name.
14689    ((gnus-use-long-file-name 'not-kill)
14690     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14691                               "." gnus-kill-file-name)
14692                       gnus-kill-files-directory))
14693    ;; Place "KILL" under the hierarchical directory.
14694    (t
14695     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14696                               "/" gnus-kill-file-name)
14697                       gnus-kill-files-directory))))
14698
14699 \f
14700 ;;;
14701 ;;; Dribble file
14702 ;;;
14703
14704 (defvar gnus-dribble-ignore nil)
14705 (defvar gnus-dribble-eval-file nil)
14706
14707 (defun gnus-dribble-file-name ()
14708   "Return the dribble file for the current .newsrc."
14709   (concat
14710    (if gnus-dribble-directory
14711        (concat (file-name-as-directory gnus-dribble-directory)
14712                (file-name-nondirectory gnus-current-startup-file))
14713      gnus-current-startup-file)
14714    "-dribble"))
14715
14716 (defun gnus-dribble-enter (string)
14717   "Enter STRING into the dribble buffer."
14718   (if (and (not gnus-dribble-ignore)
14719            gnus-dribble-buffer
14720            (buffer-name gnus-dribble-buffer))
14721       (let ((obuf (current-buffer)))
14722         (set-buffer gnus-dribble-buffer)
14723         (insert string "\n")
14724         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14725         (set-buffer obuf))))
14726
14727 (defun gnus-dribble-read-file ()
14728   "Read the dribble file from disk."
14729   (let ((dribble-file (gnus-dribble-file-name)))
14730     (save-excursion
14731       (set-buffer (setq gnus-dribble-buffer
14732                         (get-buffer-create
14733                          (file-name-nondirectory dribble-file))))
14734       (gnus-add-current-to-buffer-list)
14735       (erase-buffer)
14736       (setq buffer-file-name dribble-file)
14737       (auto-save-mode t)
14738       (buffer-disable-undo (current-buffer))
14739       (bury-buffer (current-buffer))
14740       (set-buffer-modified-p nil)
14741       (let ((auto (make-auto-save-file-name))
14742             (gnus-dribble-ignore t)
14743             modes)
14744         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14745           ;; Load whichever file is newest -- the auto save file
14746           ;; or the "real" file.
14747           (if (file-newer-than-file-p auto dribble-file)
14748               (insert-file-contents auto)
14749             (insert-file-contents dribble-file))
14750           (unless (zerop (buffer-size))
14751             (set-buffer-modified-p t))
14752           ;; Set the file modes to reflect the .newsrc file modes.
14753           (save-buffer)
14754           (when (and (file-exists-p gnus-current-startup-file)
14755                      (setq modes (file-modes gnus-current-startup-file)))
14756             (set-file-modes dribble-file modes))
14757           ;; Possibly eval the file later.
14758           (when (gnus-y-or-n-p
14759                  "Auto-save file exists.  Do you want to read it? ")
14760             (setq gnus-dribble-eval-file t)))))))
14761
14762 (defun gnus-dribble-eval-file ()
14763   (if (not gnus-dribble-eval-file)
14764       ()
14765     (setq gnus-dribble-eval-file nil)
14766     (save-excursion
14767       (let ((gnus-dribble-ignore t))
14768         (set-buffer gnus-dribble-buffer)
14769         (eval-buffer (current-buffer))))))
14770
14771 (defun gnus-dribble-delete-file ()
14772   (if (file-exists-p (gnus-dribble-file-name))
14773       (delete-file (gnus-dribble-file-name)))
14774   (if gnus-dribble-buffer
14775       (save-excursion
14776         (set-buffer gnus-dribble-buffer)
14777         (let ((auto (make-auto-save-file-name)))
14778           (if (file-exists-p auto)
14779               (delete-file auto))
14780           (erase-buffer)
14781           (set-buffer-modified-p nil)))))
14782
14783 (defun gnus-dribble-save ()
14784   (if (and gnus-dribble-buffer
14785            (buffer-name gnus-dribble-buffer))
14786       (save-excursion
14787         (set-buffer gnus-dribble-buffer)
14788         (save-buffer))))
14789
14790 (defun gnus-dribble-clear ()
14791   (save-excursion
14792     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14793         (progn
14794           (set-buffer gnus-dribble-buffer)
14795           (erase-buffer)
14796           (set-buffer-modified-p nil)
14797           (setq buffer-saved-size (buffer-size))))))
14798
14799 \f
14800 ;;;
14801 ;;; Server Communication
14802 ;;;
14803
14804 (defun gnus-start-news-server (&optional confirm)
14805   "Open a method for getting news.
14806 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14807   (let (how)
14808     (if gnus-current-select-method
14809         ;; Stream is already opened.
14810         nil
14811       ;; Open NNTP server.
14812       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14813       (if confirm
14814           (progn
14815             ;; Read server name with completion.
14816             (setq gnus-nntp-server
14817                   (completing-read "NNTP server: "
14818                                    (mapcar (lambda (server) (list server))
14819                                            (cons (list gnus-nntp-server)
14820                                                  gnus-secondary-servers))
14821                                    nil nil gnus-nntp-server))))
14822
14823       (if (and gnus-nntp-server
14824                (stringp gnus-nntp-server)
14825                (not (string= gnus-nntp-server "")))
14826           (setq gnus-select-method
14827                 (cond ((or (string= gnus-nntp-server "")
14828                            (string= gnus-nntp-server "::"))
14829                        (list 'nnspool (system-name)))
14830                       ((string-match "^:" gnus-nntp-server)
14831                        (list 'nnmh gnus-nntp-server
14832                              (list 'nnmh-directory
14833                                    (file-name-as-directory
14834                                     (expand-file-name
14835                                      (concat "~/" (substring
14836                                                    gnus-nntp-server 1)))))
14837                              (list 'nnmh-get-new-mail nil)))
14838                       (t
14839                        (list 'nntp gnus-nntp-server)))))
14840
14841       (setq how (car gnus-select-method))
14842       (cond ((eq how 'nnspool)
14843              (require 'nnspool)
14844              (gnus-message 5 "Looking up local news spool..."))
14845             ((eq how 'nnmh)
14846              (require 'nnmh)
14847              (gnus-message 5 "Looking up mh spool..."))
14848             (t
14849              (require 'nntp)))
14850       (setq gnus-current-select-method gnus-select-method)
14851       (run-hooks 'gnus-open-server-hook)
14852       (or
14853        ;; gnus-open-server-hook might have opened it
14854        (gnus-server-opened gnus-select-method)
14855        (gnus-open-server gnus-select-method)
14856        (gnus-y-or-n-p
14857         (format
14858          "%s (%s) open error: '%s'.     Continue? "
14859          (car gnus-select-method) (cadr gnus-select-method)
14860          (gnus-status-message gnus-select-method)))
14861        (gnus-error 1 "Couldn't open server on %s"
14862                    (nth 1 gnus-select-method))))))
14863
14864 (defun gnus-check-group (group)
14865   "Try to make sure that the server where GROUP exists is alive."
14866   (let ((method (gnus-find-method-for-group group)))
14867     (or (gnus-server-opened method)
14868         (gnus-open-server method))))
14869
14870 (defun gnus-check-server (&optional method silent)
14871   "Check whether the connection to METHOD is down.
14872 If METHOD is nil, use `gnus-select-method'.
14873 If it is down, start it up (again)."
14874   (let ((method (or method gnus-select-method)))
14875     ;; Transform virtual server names into select methods.
14876     (when (stringp method)
14877       (setq method (gnus-server-to-method method)))
14878     (if (gnus-server-opened method)
14879         ;; The stream is already opened.
14880         t
14881       ;; Open the server.
14882       (unless silent
14883         (gnus-message 5 "Opening %s server%s..." (car method)
14884                       (if (equal (nth 1 method) "") ""
14885                         (format " on %s" (nth 1 method)))))
14886       (run-hooks 'gnus-open-server-hook)
14887       (prog1
14888           (gnus-open-server method)
14889         (unless silent
14890           (message ""))))))
14891
14892 (defun gnus-get-function (method function)
14893   "Return a function symbol based on METHOD and FUNCTION."
14894   ;; Translate server names into methods.
14895   (unless method
14896     (error "Attempted use of a nil select method"))
14897   (when (stringp method)
14898     (setq method (gnus-server-to-method method)))
14899   (let ((func (intern (format "%s-%s" (car method) function))))
14900     ;; If the functions isn't bound, we require the backend in
14901     ;; question.
14902     (unless (fboundp func)
14903       (require (car method))
14904       (unless (fboundp func)
14905         ;; This backend doesn't implement this function.
14906         (error "No such function: %s" func)))
14907     func))
14908
14909 \f
14910 ;;;
14911 ;;; Interface functions to the backends.
14912 ;;;
14913
14914 (defun gnus-open-server (method)
14915   "Open a connection to METHOD."
14916   (when (stringp method)
14917     (setq method (gnus-server-to-method method)))
14918   (let ((elem (assoc method gnus-opened-servers)))
14919     ;; If this method was previously denied, we just return nil.
14920     (if (eq (nth 1 elem) 'denied)
14921         (progn
14922           (gnus-message 1 "Denied server")
14923           nil)
14924       ;; Open the server.
14925       (let ((result
14926              (funcall (gnus-get-function method 'open-server)
14927                       (nth 1 method) (nthcdr 2 method))))
14928         ;; If this hasn't been opened before, we add it to the list.
14929         (unless elem
14930           (setq elem (list method nil)
14931                 gnus-opened-servers (cons elem gnus-opened-servers)))
14932         ;; Set the status of this server.
14933         (setcar (cdr elem) (if result 'ok 'denied))
14934         ;; Return the result from the "open" call.
14935         result))))
14936
14937 (defun gnus-close-server (method)
14938   "Close the connection to METHOD."
14939   (when (stringp method)
14940     (setq method (gnus-server-to-method method)))
14941   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14942
14943 (defun gnus-request-list (method)
14944   "Request the active file from METHOD."
14945   (when (stringp method)
14946     (setq method (gnus-server-to-method method)))
14947   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14948
14949 (defun gnus-request-list-newsgroups (method)
14950   "Request the newsgroups file from METHOD."
14951   (when (stringp method)
14952     (setq method (gnus-server-to-method method)))
14953   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14954
14955 (defun gnus-request-newgroups (date method)
14956   "Request all new groups since DATE from METHOD."
14957   (when (stringp method)
14958     (setq method (gnus-server-to-method method)))
14959   (funcall (gnus-get-function method 'request-newgroups)
14960            date (nth 1 method)))
14961
14962 (defun gnus-server-opened (method)
14963   "Check whether a connection to METHOD has been opened."
14964   (when (stringp method)
14965     (setq method (gnus-server-to-method method)))
14966   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14967
14968 (defun gnus-status-message (method)
14969   "Return the status message from METHOD.
14970 If METHOD is a string, it is interpreted as a group name.   The method
14971 this group uses will be queried."
14972   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14973                   method)))
14974     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14975
14976 (defun gnus-request-group (group &optional dont-check method)
14977   "Request GROUP.  If DONT-CHECK, no information is required."
14978   (let ((method (or method (gnus-find-method-for-group group))))
14979     (when (stringp method)
14980       (setq method (gnus-server-to-method method)))
14981     (funcall (gnus-get-function method 'request-group)
14982              (gnus-group-real-name group) (nth 1 method) dont-check)))
14983
14984 (defun gnus-request-asynchronous (group &optional articles)
14985   "Request that GROUP behave asynchronously.
14986 ARTICLES is the `data' of the group."
14987   (let ((method (gnus-find-method-for-group group)))
14988     (funcall (gnus-get-function method 'request-asynchronous)
14989              (gnus-group-real-name group) (nth 1 method) articles)))
14990
14991 (defun gnus-list-active-group (group)
14992   "Request active information on GROUP."
14993   (let ((method (gnus-find-method-for-group group))
14994         (func 'list-active-group))
14995     (when (gnus-check-backend-function func group)
14996       (funcall (gnus-get-function method func)
14997                (gnus-group-real-name group) (nth 1 method)))))
14998
14999 (defun gnus-request-group-description (group)
15000   "Request a description of GROUP."
15001   (let ((method (gnus-find-method-for-group group))
15002         (func 'request-group-description))
15003     (when (gnus-check-backend-function func group)
15004       (funcall (gnus-get-function method func)
15005                (gnus-group-real-name group) (nth 1 method)))))
15006
15007 (defun gnus-close-group (group)
15008   "Request the GROUP be closed."
15009   (let ((method (gnus-find-method-for-group group)))
15010     (funcall (gnus-get-function method 'close-group)
15011              (gnus-group-real-name group) (nth 1 method))))
15012
15013 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15014   "Request headers for ARTICLES in GROUP.
15015 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15016   (let ((method (gnus-find-method-for-group group)))
15017     (if (and gnus-use-cache (numberp (car articles)))
15018         (gnus-cache-retrieve-headers articles group fetch-old)
15019       (funcall (gnus-get-function method 'retrieve-headers)
15020                articles (gnus-group-real-name group) (nth 1 method)
15021                fetch-old))))
15022
15023 (defun gnus-retrieve-groups (groups method)
15024   "Request active information on GROUPS from METHOD."
15025   (when (stringp method)
15026     (setq method (gnus-server-to-method method)))
15027   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15028
15029 (defun gnus-request-type (group &optional article)
15030   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15031   (let ((method (gnus-find-method-for-group group)))
15032     (if (not (gnus-check-backend-function 'request-type (car method)))
15033         'unknown
15034       (funcall (gnus-get-function method 'request-type)
15035                (gnus-group-real-name group) article))))
15036
15037 (defun gnus-request-update-mark (group article mark)
15038   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15039   (let ((method (gnus-find-method-for-group group)))
15040     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15041         mark
15042       (funcall (gnus-get-function method 'request-update-mark)
15043                (gnus-group-real-name group) article mark))))
15044
15045 (defun gnus-request-article (article group &optional buffer)
15046   "Request the ARTICLE in GROUP.
15047 ARTICLE can either be an article number or an article Message-ID.
15048 If BUFFER, insert the article in that group."
15049   (let ((method (gnus-find-method-for-group group)))
15050     (funcall (gnus-get-function method 'request-article)
15051              article (gnus-group-real-name group) (nth 1 method) buffer)))
15052
15053 (defun gnus-request-head (article group)
15054   "Request the head of ARTICLE in GROUP."
15055   (let ((method (gnus-find-method-for-group group)))
15056     (funcall (gnus-get-function method 'request-head)
15057              article (gnus-group-real-name group) (nth 1 method))))
15058
15059 (defun gnus-request-body (article group)
15060   "Request the body of ARTICLE in GROUP."
15061   (let ((method (gnus-find-method-for-group group)))
15062     (funcall (gnus-get-function method 'request-body)
15063              article (gnus-group-real-name group) (nth 1 method))))
15064
15065 (defun gnus-request-post (method)
15066   "Post the current buffer using METHOD."
15067   (when (stringp method)
15068     (setq method (gnus-server-to-method method)))
15069   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15070
15071 (defun gnus-request-scan (group method)
15072   "Request a SCAN being performed in GROUP from METHOD.
15073 If GROUP is nil, all groups on METHOD are scanned."
15074   (let ((method (if group (gnus-find-method-for-group group) method)))
15075     (funcall (gnus-get-function method 'request-scan)
15076              (and group (gnus-group-real-name group)) (nth 1 method))))
15077
15078 (defsubst gnus-request-update-info (info method)
15079   "Request that METHOD update INFO."
15080   (when (stringp method)
15081     (setq method (gnus-server-to-method method)))
15082   (when (gnus-check-backend-function 'request-update-info (car method))
15083     (funcall (gnus-get-function method 'request-update-info)
15084              (gnus-group-real-name (gnus-info-group info))
15085              info (nth 1 method))))
15086
15087 (defun gnus-request-expire-articles (articles group &optional force)
15088   (let ((method (gnus-find-method-for-group group)))
15089     (funcall (gnus-get-function method 'request-expire-articles)
15090              articles (gnus-group-real-name group) (nth 1 method)
15091              force)))
15092
15093 (defun gnus-request-move-article
15094   (article group server accept-function &optional last)
15095   (let ((method (gnus-find-method-for-group group)))
15096     (funcall (gnus-get-function method 'request-move-article)
15097              article (gnus-group-real-name group)
15098              (nth 1 method) accept-function last)))
15099
15100 (defun gnus-request-accept-article (group method &optional last)
15101   ;; Make sure there's a newline at the end of the article.
15102   (when (stringp method)
15103     (setq method (gnus-server-to-method method)))
15104   (when (and (not method)
15105              (stringp group))
15106     (setq method (gnus-find-method-for-group group)))
15107   (goto-char (point-max))
15108   (unless (bolp)
15109     (insert "\n"))
15110   (let ((func (car (or method (gnus-find-method-for-group group)))))
15111     (funcall (intern (format "%s-request-accept-article" func))
15112              (if (stringp group) (gnus-group-real-name group) group)
15113              (cadr method)
15114              last)))
15115
15116 (defun gnus-request-replace-article (article group buffer)
15117   (let ((func (car (gnus-find-method-for-group group))))
15118     (funcall (intern (format "%s-request-replace-article" func))
15119              article (gnus-group-real-name group) buffer)))
15120
15121 (defun gnus-request-associate-buffer (group)
15122   (let ((method (gnus-find-method-for-group group)))
15123     (funcall (gnus-get-function method 'request-associate-buffer)
15124              (gnus-group-real-name group))))
15125
15126 (defun gnus-request-restore-buffer (article group)
15127   "Request a new buffer restored to the state of ARTICLE."
15128   (let ((method (gnus-find-method-for-group group)))
15129     (funcall (gnus-get-function method 'request-restore-buffer)
15130              article (gnus-group-real-name group) (nth 1 method))))
15131
15132 (defun gnus-request-create-group (group &optional method)
15133   (when (stringp method)
15134     (setq method (gnus-server-to-method method)))
15135   (let ((method (or method (gnus-find-method-for-group group))))
15136     (funcall (gnus-get-function method 'request-create-group)
15137              (gnus-group-real-name group) (nth 1 method))))
15138
15139 (defun gnus-request-delete-group (group &optional force)
15140   (let ((method (gnus-find-method-for-group group)))
15141     (funcall (gnus-get-function method 'request-delete-group)
15142              (gnus-group-real-name group) force (nth 1 method))))
15143
15144 (defun gnus-request-rename-group (group new-name)
15145   (let ((method (gnus-find-method-for-group group)))
15146     (funcall (gnus-get-function method 'request-rename-group)
15147              (gnus-group-real-name group)
15148              (gnus-group-real-name new-name) (nth 1 method))))
15149
15150 (defun gnus-member-of-valid (symbol group)
15151   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15152   (memq symbol (assoc
15153                 (symbol-name (car (gnus-find-method-for-group group)))
15154                 gnus-valid-select-methods)))
15155
15156 (defun gnus-method-option-p (method option)
15157   "Return non-nil if select METHOD has OPTION as a parameter."
15158   (when (stringp method)
15159     (setq method (gnus-server-to-method method)))
15160   (memq option (assoc (format "%s" (car method))
15161                       gnus-valid-select-methods)))
15162
15163 (defun gnus-server-extend-method (group method)
15164   ;; This function "extends" a virtual server.  If the server is
15165   ;; "hello", and the select method is ("hello" (my-var "something"))
15166   ;; in the group "alt.alt", this will result in a new virtual server
15167   ;; called "hello+alt.alt".
15168   (let ((entry
15169          (gnus-copy-sequence
15170           (if (equal (car method) "native") gnus-select-method
15171             (cdr (assoc (car method) gnus-server-alist))))))
15172     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15173     (nconc entry (cdr method))))
15174
15175 (defun gnus-find-method-for-group (group &optional info)
15176   "Find the select method that GROUP uses."
15177   (or gnus-override-method
15178       (and (not group)
15179            gnus-select-method)
15180       (let ((info (or info (gnus-get-info group)))
15181             method)
15182         (if (or (not info)
15183                 (not (setq method (gnus-info-method info)))
15184                 (equal method "native"))
15185             gnus-select-method
15186           (setq method
15187                 (cond ((stringp method)
15188                        (gnus-server-to-method method))
15189                       ((stringp (car method))
15190                        (gnus-server-extend-method group method))
15191                       (t
15192                        method)))
15193           (cond ((equal (cadr method) "")
15194                  method)
15195                 ((null (cadr method))
15196                  (list (car method) ""))
15197                 (t
15198                  (gnus-server-add-address method)))))))
15199
15200 (defun gnus-check-backend-function (func group)
15201   "Check whether GROUP supports function FUNC."
15202   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15203                   group)))
15204     (fboundp (intern (format "%s-%s" method func)))))
15205
15206 (defun gnus-methods-using (feature)
15207   "Find all methods that have FEATURE."
15208   (let ((valids gnus-valid-select-methods)
15209         outs)
15210     (while valids
15211       (if (memq feature (car valids))
15212           (setq outs (cons (car valids) outs)))
15213       (setq valids (cdr valids)))
15214     outs))
15215
15216 \f
15217 ;;;
15218 ;;; Active & Newsrc File Handling
15219 ;;;
15220
15221 (defun gnus-setup-news (&optional rawfile level dont-connect)
15222   "Setup news information.
15223 If RAWFILE is non-nil, the .newsrc file will also be read.
15224 If LEVEL is non-nil, the news will be set up at level LEVEL."
15225   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15226
15227     (when init 
15228       ;; Clear some variables to re-initialize news information.
15229       (setq gnus-newsrc-alist nil
15230             gnus-active-hashtb nil)
15231       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15232       (gnus-read-newsrc-file rawfile))
15233
15234     (when (and (not (assoc "archive" gnus-server-alist))
15235                gnus-message-archive-method)
15236       (push (cons "archive" gnus-message-archive-method)
15237             gnus-server-alist))
15238
15239     ;; If we don't read the complete active file, we fill in the
15240     ;; hashtb here.
15241     (if (or (null gnus-read-active-file)
15242             (eq gnus-read-active-file 'some))
15243         (gnus-update-active-hashtb-from-killed))
15244
15245     ;; Read the active file and create `gnus-active-hashtb'.
15246     ;; If `gnus-read-active-file' is nil, then we just create an empty
15247     ;; hash table.  The partial filling out of the hash table will be
15248     ;; done in `gnus-get-unread-articles'.
15249     (and gnus-read-active-file
15250          (not level)
15251          (gnus-read-active-file))
15252
15253     (or gnus-active-hashtb
15254         (setq gnus-active-hashtb (make-vector 4095 0)))
15255
15256     ;; Initialize the cache.
15257     (when gnus-use-cache
15258       (gnus-cache-open))
15259
15260     ;; Possibly eval the dribble file.
15261     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15262
15263     (gnus-update-format-specifications)
15264
15265     ;; See whether we need to read the description file.
15266     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15267              (not gnus-description-hashtb)
15268              (not dont-connect)
15269              gnus-read-active-file)
15270         (gnus-read-all-descriptions-files))
15271
15272     ;; Find new newsgroups and treat them.
15273     (if (and init gnus-check-new-newsgroups (not level)
15274              (gnus-check-server gnus-select-method))
15275         (gnus-find-new-newsgroups))
15276
15277     ;; We might read in new NoCeM messages here.
15278     (when (and gnus-use-nocem 
15279                (not level)
15280                (not dont-connect))
15281       (gnus-nocem-scan-groups))
15282
15283     ;; Find the number of unread articles in each non-dead group.
15284     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15285       (gnus-get-unread-articles level))
15286
15287     (if (and init gnus-check-bogus-newsgroups
15288              gnus-read-active-file (not level)
15289              (gnus-server-opened gnus-select-method))
15290         (gnus-check-bogus-newsgroups))))
15291
15292 (defun gnus-find-new-newsgroups (&optional arg)
15293   "Search for new newsgroups and add them.
15294 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15295 The `-n' option line from .newsrc is respected.
15296 If ARG (the prefix), use the `ask-server' method to query
15297 the server for new groups."
15298   (interactive "P")
15299   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15300                        (null gnus-read-active-file)
15301                        (eq gnus-read-active-file 'some))
15302                    'ask-server gnus-check-new-newsgroups)))
15303     (unless (gnus-check-first-time-used)
15304       (if (or (consp check)
15305               (eq check 'ask-server))
15306           ;; Ask the server for new groups.
15307           (gnus-ask-server-for-new-groups)
15308         ;; Go through the active hashtb and look for new groups.
15309         (let ((groups 0)
15310               group new-newsgroups)
15311           (gnus-message 5 "Looking for new newsgroups...")
15312           (unless gnus-have-read-active-file
15313             (gnus-read-active-file))
15314           (setq gnus-newsrc-last-checked-date (current-time-string))
15315           (unless gnus-killed-hashtb
15316             (gnus-make-hashtable-from-killed))
15317           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15318           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15319           (mapatoms
15320            (lambda (sym)
15321              (if (or (null (setq group (symbol-name sym)))
15322                      (not (boundp sym))
15323                      (null (symbol-value sym))
15324                      (gnus-gethash group gnus-killed-hashtb)
15325                      (gnus-gethash group gnus-newsrc-hashtb))
15326                  ()
15327                (let ((do-sub (gnus-matches-options-n group)))
15328                  (cond
15329                   ((eq do-sub 'subscribe)
15330                    (setq groups (1+ groups))
15331                    (gnus-sethash group group gnus-killed-hashtb)
15332                    (funcall gnus-subscribe-options-newsgroup-method group))
15333                   ((eq do-sub 'ignore)
15334                    nil)
15335                   (t
15336                    (setq groups (1+ groups))
15337                    (gnus-sethash group group gnus-killed-hashtb)
15338                    (if gnus-subscribe-hierarchical-interactive
15339                        (setq new-newsgroups (cons group new-newsgroups))
15340                      (funcall gnus-subscribe-newsgroup-method group)))))))
15341            gnus-active-hashtb)
15342           (when new-newsgroups
15343             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15344           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15345           (if (> groups 0)
15346               (gnus-message 6 "%d new newsgroup%s arrived."
15347                             groups (if (> groups 1) "s have" " has"))
15348             (gnus-message 6 "No new newsgroups.")))))))
15349
15350 (defun gnus-matches-options-n (group)
15351   ;; Returns `subscribe' if the group is to be unconditionally
15352   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15353   ;; no match for the group.
15354
15355   ;; First we check the two user variables.
15356   (cond
15357    ((and gnus-options-subscribe
15358          (string-match gnus-options-subscribe group))
15359     'subscribe)
15360    ((and gnus-auto-subscribed-groups
15361          (string-match gnus-auto-subscribed-groups group))
15362     'subscribe)
15363    ((and gnus-options-not-subscribe
15364          (string-match gnus-options-not-subscribe group))
15365     'ignore)
15366    ;; Then we go through the list that was retrieved from the .newsrc
15367    ;; file.  This list has elements on the form
15368    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15369    ;; is in the reverse order of the options line) is returned.
15370    (t
15371     (let ((regs gnus-newsrc-options-n))
15372       (while (and regs
15373                   (not (string-match (caar regs) group)))
15374         (setq regs (cdr regs)))
15375       (and regs (cdar regs))))))
15376
15377 (defun gnus-ask-server-for-new-groups ()
15378   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15379          (methods (cons gnus-select-method
15380                         (nconc
15381                          (when gnus-message-archive-method
15382                            (list "archive"))
15383                          (append
15384                           (and (consp gnus-check-new-newsgroups)
15385                                gnus-check-new-newsgroups)
15386                           gnus-secondary-select-methods))))
15387          (groups 0)
15388          (new-date (current-time-string))
15389          group new-newsgroups got-new method hashtb
15390          gnus-override-subscribe-method)
15391     ;; Go through both primary and secondary select methods and
15392     ;; request new newsgroups.
15393     (while (setq method (gnus-server-get-method nil (pop methods)))
15394       (setq new-newsgroups nil)
15395       (setq gnus-override-subscribe-method method)
15396       (when (and (gnus-check-server method)
15397                  (gnus-request-newgroups date method))
15398         (save-excursion
15399           (setq got-new t)
15400           (setq hashtb (gnus-make-hashtable 100))
15401           (set-buffer nntp-server-buffer)
15402           ;; Enter all the new groups into a hashtable.
15403           (gnus-active-to-gnus-format method hashtb 'ignore))
15404         ;; Now all new groups from `method' are in `hashtb'.
15405         (mapatoms
15406          (lambda (group-sym)
15407            (if (or (null (setq group (symbol-name group-sym)))
15408                    (not (boundp group-sym))
15409                    (null (symbol-value group-sym))
15410                    (gnus-gethash group gnus-newsrc-hashtb)
15411                    (member group gnus-zombie-list)
15412                    (member group gnus-killed-list))
15413                ;; The group is already known.
15414                ()
15415              ;; Make this group active.
15416              (when (symbol-value group-sym)
15417                (gnus-set-active group (symbol-value group-sym)))
15418              ;; Check whether we want it or not.
15419              (let ((do-sub (gnus-matches-options-n group)))
15420                (cond
15421                 ((eq do-sub 'subscribe)
15422                  (incf groups)
15423                  (gnus-sethash group group gnus-killed-hashtb)
15424                  (funcall gnus-subscribe-options-newsgroup-method group))
15425                 ((eq do-sub 'ignore)
15426                  nil)
15427                 (t
15428                  (incf groups)
15429                  (gnus-sethash group group gnus-killed-hashtb)
15430                  (if gnus-subscribe-hierarchical-interactive
15431                      (push group new-newsgroups)
15432                    (funcall gnus-subscribe-newsgroup-method group)))))))
15433          hashtb))
15434       (when new-newsgroups
15435         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15436     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15437     (when (> groups 0)
15438       (gnus-message 6 "%d new newsgroup%s arrived."
15439                     groups (if (> groups 1) "s have" " has")))
15440     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15441     got-new))
15442
15443 (defun gnus-check-first-time-used ()
15444   (if (or (> (length gnus-newsrc-alist) 1)
15445           (file-exists-p gnus-startup-file)
15446           (file-exists-p (concat gnus-startup-file ".el"))
15447           (file-exists-p (concat gnus-startup-file ".eld")))
15448       nil
15449     (gnus-message 6 "First time user; subscribing you to default groups")
15450     (unless (gnus-read-active-file-p)
15451       (gnus-read-active-file))
15452     (setq gnus-newsrc-last-checked-date (current-time-string))
15453     (let ((groups gnus-default-subscribed-newsgroups)
15454           group)
15455       (if (eq groups t)
15456           nil
15457         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15458         (mapatoms
15459          (lambda (sym)
15460            (if (null (setq group (symbol-name sym)))
15461                ()
15462              (let ((do-sub (gnus-matches-options-n group)))
15463                (cond
15464                 ((eq do-sub 'subscribe)
15465                  (gnus-sethash group group gnus-killed-hashtb)
15466                  (funcall gnus-subscribe-options-newsgroup-method group))
15467                 ((eq do-sub 'ignore)
15468                  nil)
15469                 (t
15470                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15471          gnus-active-hashtb)
15472         (while groups
15473           (if (gnus-active (car groups))
15474               (gnus-group-change-level
15475                (car groups) gnus-level-default-subscribed gnus-level-killed))
15476           (setq groups (cdr groups)))
15477         (gnus-group-make-help-group)
15478         (and gnus-novice-user
15479              (gnus-message 7 "`A k' to list killed groups"))))))
15480
15481 (defun gnus-subscribe-group (group previous &optional method)
15482   (gnus-group-change-level
15483    (if method
15484        (list t group gnus-level-default-subscribed nil nil method)
15485      group)
15486    gnus-level-default-subscribed gnus-level-killed previous t))
15487
15488 ;; `gnus-group-change-level' is the fundamental function for changing
15489 ;; subscription levels of newsgroups.  This might mean just changing
15490 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15491 ;; again, which subscribes/unsubscribes a group, which is equally
15492 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15493 ;; from 8-9 to 1-7 means that you remove the group from the list of
15494 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15495 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15496 ;; which is trivial.
15497 ;; ENTRY can either be a string (newsgroup name) or a list (if
15498 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15499 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15500 ;; entries.
15501 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15502 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15503 ;; after.
15504 (defun gnus-group-change-level (entry level &optional oldlevel
15505                                       previous fromkilled)
15506   (let (group info active num)
15507     ;; Glean what info we can from the arguments
15508     (if (consp entry)
15509         (if fromkilled (setq group (nth 1 entry))
15510           (setq group (car (nth 2 entry))))
15511       (setq group entry))
15512     (if (and (stringp entry)
15513              oldlevel
15514              (< oldlevel gnus-level-zombie))
15515         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15516     (if (and (not oldlevel)
15517              (consp entry))
15518         (setq oldlevel (gnus-info-level (nth 2 entry)))
15519       (setq oldlevel (or oldlevel 9)))
15520     (if (stringp previous)
15521         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15522
15523     (if (and (>= oldlevel gnus-level-zombie)
15524              (gnus-gethash group gnus-newsrc-hashtb))
15525         ;; We are trying to subscribe a group that is already
15526         ;; subscribed.
15527         ()                              ; Do nothing.
15528
15529       (or (gnus-ephemeral-group-p group)
15530           (gnus-dribble-enter
15531            (format "(gnus-group-change-level %S %S %S %S %S)"
15532                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15533
15534       ;; Then we remove the newgroup from any old structures, if needed.
15535       ;; If the group was killed, we remove it from the killed or zombie
15536       ;; list.  If not, and it is in fact going to be killed, we remove
15537       ;; it from the newsrc hash table and assoc.
15538       (cond
15539        ((>= oldlevel gnus-level-zombie)
15540         (if (= oldlevel gnus-level-zombie)
15541             (setq gnus-zombie-list (delete group gnus-zombie-list))
15542           (setq gnus-killed-list (delete group gnus-killed-list))))
15543        (t
15544         (if (and (>= level gnus-level-zombie)
15545                  entry)
15546             (progn
15547               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15548               (if (nth 3 entry)
15549                   (setcdr (gnus-gethash (car (nth 3 entry))
15550                                         gnus-newsrc-hashtb)
15551                           (cdr entry)))
15552               (setcdr (cdr entry) (cdddr entry))))))
15553
15554       ;; Finally we enter (if needed) the list where it is supposed to
15555       ;; go, and change the subscription level.  If it is to be killed,
15556       ;; we enter it into the killed or zombie list.
15557       (cond 
15558        ((>= level gnus-level-zombie)
15559         ;; Remove from the hash table.
15560         (gnus-sethash group nil gnus-newsrc-hashtb)
15561         ;; We do not enter foreign groups into the list of dead
15562         ;; groups.
15563         (unless (gnus-group-foreign-p group)
15564           (if (= level gnus-level-zombie)
15565               (setq gnus-zombie-list (cons group gnus-zombie-list))
15566             (setq gnus-killed-list (cons group gnus-killed-list)))))
15567        (t
15568         ;; If the list is to be entered into the newsrc assoc, and
15569         ;; it was killed, we have to create an entry in the newsrc
15570         ;; hashtb format and fix the pointers in the newsrc assoc.
15571         (if (< oldlevel gnus-level-zombie)
15572             ;; It was alive, and it is going to stay alive, so we
15573             ;; just change the level and don't change any pointers or
15574             ;; hash table entries.
15575             (setcar (cdaddr entry) level)
15576           (if (listp entry)
15577               (setq info (cdr entry)
15578                     num (car entry))
15579             (setq active (gnus-active group))
15580             (setq num
15581                   (if active (- (1+ (cdr active)) (car active)) t))
15582             ;; Check whether the group is foreign.  If so, the
15583             ;; foreign select method has to be entered into the
15584             ;; info.
15585             (let ((method (or gnus-override-subscribe-method
15586                               (gnus-group-method group))))
15587               (if (eq method gnus-select-method)
15588                   (setq info (list group level nil))
15589                 (setq info (list group level nil nil method)))))
15590           (unless previous
15591             (setq previous
15592                   (let ((p gnus-newsrc-alist))
15593                     (while (cddr p)
15594                       (setq p (cdr p)))
15595                     p)))
15596           (setq entry (cons info (cddr previous)))
15597           (if (cdr previous)
15598               (progn
15599                 (setcdr (cdr previous) entry)
15600                 (gnus-sethash group (cons num (cdr previous))
15601                               gnus-newsrc-hashtb))
15602             (setcdr previous entry)
15603             (gnus-sethash group (cons num previous)
15604                           gnus-newsrc-hashtb))
15605           (when (cdr entry)
15606             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15607       (when gnus-group-change-level-function
15608         (funcall gnus-group-change-level-function group level oldlevel)))))
15609
15610 (defun gnus-kill-newsgroup (newsgroup)
15611   "Obsolete function.  Kills a newsgroup."
15612   (gnus-group-change-level
15613    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15614
15615 (defun gnus-check-bogus-newsgroups (&optional confirm)
15616   "Remove bogus newsgroups.
15617 If CONFIRM is non-nil, the user has to confirm the deletion of every
15618 newsgroup."
15619   (let ((newsrc (cdr gnus-newsrc-alist))
15620         bogus group entry info)
15621     (gnus-message 5 "Checking bogus newsgroups...")
15622     (unless (gnus-read-active-file-p)
15623       (gnus-read-active-file))
15624     (when (gnus-read-active-file-p)
15625       ;; Find all bogus newsgroup that are subscribed.
15626       (while newsrc
15627         (setq info (pop newsrc)
15628               group (gnus-info-group info))
15629         (unless (or (gnus-active group) ; Active
15630                     (gnus-info-method info) ; Foreign
15631                     (and confirm
15632                          (not (gnus-y-or-n-p
15633                                (format "Remove bogus newsgroup: %s " group)))))
15634           ;; Found a bogus newsgroup.
15635           (push group bogus)))
15636       ;; Remove all bogus subscribed groups by first killing them, and
15637       ;; then removing them from the list of killed groups.
15638       (while bogus
15639         (when (setq entry (gnus-gethash (setq group (pop bogus))
15640                                         gnus-newsrc-hashtb))
15641           (gnus-group-change-level entry gnus-level-killed)
15642           (setq gnus-killed-list (delete group gnus-killed-list))))
15643       ;; Then we remove all bogus groups from the list of killed and
15644       ;; zombie groups.  They are are removed without confirmation.
15645       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15646             killed)
15647         (while dead-lists
15648           (setq killed (symbol-value (car dead-lists)))
15649           (while killed
15650             (unless (gnus-active (setq group (pop killed)))
15651               ;; The group is bogus.
15652               ;; !!!Slow as hell.
15653               (set (car dead-lists)
15654                    (delete group (symbol-value (car dead-lists))))))
15655           (setq dead-lists (cdr dead-lists))))
15656       (gnus-message 5 "Checking bogus newsgroups...done"))))
15657
15658 (defun gnus-check-duplicate-killed-groups ()
15659   "Remove duplicates from the list of killed groups."
15660   (interactive)
15661   (let ((killed gnus-killed-list))
15662     (while killed
15663       (gnus-message 9 "%d" (length killed))
15664       (setcdr killed (delete (car killed) (cdr killed)))
15665       (setq killed (cdr killed)))))
15666
15667 ;; We want to inline a function from gnus-cache, so we cheat here:
15668 (eval-when-compile
15669   (provide 'gnus)
15670   (require 'gnus-cache))
15671
15672 (defun gnus-get-unread-articles-in-group (info active &optional update)
15673   (when active
15674     ;; Allow the backend to update the info in the group.
15675     (when (and update 
15676                (gnus-request-update-info
15677                 info (gnus-find-method-for-group (gnus-info-group info))))
15678       (gnus-activate-group (gnus-info-group info) nil t))
15679     (let* ((range (gnus-info-read info))
15680            (num 0))
15681       ;; If a cache is present, we may have to alter the active info.
15682       (when (and gnus-use-cache info)
15683         (inline (gnus-cache-possibly-alter-active 
15684                  (gnus-info-group info) active)))
15685       ;; Modify the list of read articles according to what articles
15686       ;; are available; then tally the unread articles and add the
15687       ;; number to the group hash table entry.
15688       (cond
15689        ((zerop (cdr active))
15690         (setq num 0))
15691        ((not range)
15692         (setq num (- (1+ (cdr active)) (car active))))
15693        ((not (listp (cdr range)))
15694         ;; Fix a single (num . num) range according to the
15695         ;; active hash table.
15696         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15697         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15698         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15699         ;; Compute number of unread articles.
15700         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15701        (t
15702         ;; The read list is a list of ranges.  Fix them according to
15703         ;; the active hash table.
15704         ;; First peel off any elements that are below the lower
15705         ;; active limit.
15706         (while (and (cdr range)
15707                     (>= (car active)
15708                         (or (and (atom (cadr range)) (cadr range))
15709                             (caadr range))))
15710           (if (numberp (car range))
15711               (setcar range
15712                       (cons (car range)
15713                             (or (and (numberp (cadr range))
15714                                      (cadr range))
15715                                 (cdadr range))))
15716             (setcdr (car range)
15717                     (or (and (numberp (nth 1 range)) (nth 1 range))
15718                         (cdadr range))))
15719           (setcdr range (cddr range)))
15720         ;; Adjust the first element to be the same as the lower limit.
15721         (if (and (not (atom (car range)))
15722                  (< (cdar range) (car active)))
15723             (setcdr (car range) (1- (car active))))
15724         ;; Then we want to peel off any elements that are higher
15725         ;; than the upper active limit.
15726         (let ((srange range))
15727           ;; Go past all legal elements.
15728           (while (and (cdr srange)
15729                       (<= (or (and (atom (cadr srange))
15730                                    (cadr srange))
15731                               (caadr srange)) (cdr active)))
15732             (setq srange (cdr srange)))
15733           (if (cdr srange)
15734               ;; Nuke all remaining illegal elements.
15735               (setcdr srange nil))
15736
15737           ;; Adjust the final element.
15738           (if (and (not (atom (car srange)))
15739                    (> (cdar srange) (cdr active)))
15740               (setcdr (car srange) (cdr active))))
15741         ;; Compute the number of unread articles.
15742         (while range
15743           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15744                                       (cdar range)))
15745                               (or (and (atom (car range)) (car range))
15746                                   (caar range)))))
15747           (setq range (cdr range)))
15748         (setq num (max 0 (- (cdr active) num)))))
15749       ;; Set the number of unread articles.
15750       (when info
15751         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15752       num)))
15753
15754 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15755 ;; and compute how many unread articles there are in each group.
15756 (defun gnus-get-unread-articles (&optional level)
15757   (let* ((newsrc (cdr gnus-newsrc-alist))
15758          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15759          (foreign-level
15760           (min
15761            (cond ((and gnus-activate-foreign-newsgroups
15762                        (not (numberp gnus-activate-foreign-newsgroups)))
15763                   (1+ gnus-level-subscribed))
15764                  ((numberp gnus-activate-foreign-newsgroups)
15765                   gnus-activate-foreign-newsgroups)
15766                  (t 0))
15767            level))
15768          info group active method)
15769     (gnus-message 5 "Checking new news...")
15770
15771     (while newsrc
15772       (setq active (gnus-active (setq group (gnus-info-group
15773                                              (setq info (pop newsrc))))))
15774
15775       ;; Check newsgroups.  If the user doesn't want to check them, or
15776       ;; they can't be checked (for instance, if the news server can't
15777       ;; be reached) we just set the number of unread articles in this
15778       ;; newsgroup to t.  This means that Gnus thinks that there are
15779       ;; unread articles, but it has no idea how many.
15780       (if (and (setq method (gnus-info-method info))
15781                (not (gnus-server-equal
15782                      gnus-select-method
15783                      (setq method (gnus-server-get-method nil method))))
15784                (not (gnus-secondary-method-p method)))
15785           ;; These groups are foreign.  Check the level.
15786           (when (<= (gnus-info-level info) foreign-level)
15787             (setq active (gnus-activate-group group 'scan))
15788             (unless (inline (gnus-virtual-group-p group))
15789               (inline (gnus-close-group group)))
15790             (when (fboundp (intern (concat (symbol-name (car method))
15791                                            "-request-update-info")))
15792               (inline (gnus-request-update-info info method))))
15793         ;; These groups are native or secondary.
15794         (when (and (<= (gnus-info-level info) level)
15795                    (not gnus-read-active-file))
15796           (setq active (gnus-activate-group group 'scan))
15797           (inline (gnus-close-group group))))
15798
15799       ;; Get the number of unread articles in the group.
15800       (if active
15801           (inline (gnus-get-unread-articles-in-group info active))
15802         ;; The group couldn't be reached, so we nix out the number of
15803         ;; unread articles and stuff.
15804         (gnus-set-active group nil)
15805         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15806
15807     (gnus-message 5 "Checking new news...done")))
15808
15809 ;; Create a hash table out of the newsrc alist.  The `car's of the
15810 ;; alist elements are used as keys.
15811 (defun gnus-make-hashtable-from-newsrc-alist ()
15812   (let ((alist gnus-newsrc-alist)
15813         (ohashtb gnus-newsrc-hashtb)
15814         prev)
15815     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15816     (setq alist
15817           (setq prev (setq gnus-newsrc-alist
15818                            (if (equal (caar gnus-newsrc-alist)
15819                                       "dummy.group")
15820                                gnus-newsrc-alist
15821                              (cons (list "dummy.group" 0 nil) alist)))))
15822     (while alist
15823       (gnus-sethash
15824        (caar alist)
15825        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15826              prev)
15827        gnus-newsrc-hashtb)
15828       (setq prev alist
15829             alist (cdr alist)))))
15830
15831 (defun gnus-make-hashtable-from-killed ()
15832   "Create a hash table from the killed and zombie lists."
15833   (let ((lists '(gnus-killed-list gnus-zombie-list))
15834         list)
15835     (setq gnus-killed-hashtb
15836           (gnus-make-hashtable
15837            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15838     (while (setq list (pop lists))
15839       (setq list (symbol-value list))
15840       (while list
15841         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15842
15843 (defun gnus-activate-group (group &optional scan dont-check)
15844   ;; Check whether a group has been activated or not.
15845   ;; If SCAN, request a scan of that group as well.
15846   (let ((method (gnus-find-method-for-group group))
15847         active)
15848     (and (gnus-check-server method)
15849          ;; We escape all bugs and quit here to make it possible to
15850          ;; continue if a group is so out-there that it reports bugs
15851          ;; and stuff.
15852          (progn
15853            (and scan
15854                 (gnus-check-backend-function 'request-scan (car method))
15855                 (gnus-request-scan group method))
15856            t)
15857          (condition-case ()
15858              (gnus-request-group group dont-check)
15859         ;   (error nil)
15860            (quit nil))
15861          (save-excursion
15862            (set-buffer nntp-server-buffer)
15863            (goto-char (point-min))
15864            ;; Parse the result we got from `gnus-request-group'.
15865            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15866                 (progn
15867                   (goto-char (match-beginning 1))
15868                   (gnus-set-active
15869                    group (setq active (cons (read (current-buffer))
15870                                             (read (current-buffer)))))
15871                   ;; Return the new active info.
15872                   active))))))
15873
15874 (defun gnus-update-read-articles (group unread)
15875   "Update the list of read and ticked articles in GROUP using the
15876 UNREAD and TICKED lists.
15877 Note: UNSELECTED has to be sorted over `<'.
15878 Returns whether the updating was successful."
15879   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15880          (entry (gnus-gethash group gnus-newsrc-hashtb))
15881          (info (nth 2 entry))
15882          (prev 1)
15883          (unread (sort (copy-sequence unread) '<))
15884          read)
15885     (if (or (not info) (not active))
15886         ;; There is no info on this group if it was, in fact,
15887         ;; killed.  Gnus stores no information on killed groups, so
15888         ;; there's nothing to be done.
15889         ;; One could store the information somewhere temporarily,
15890         ;; perhaps...  Hmmm...
15891         ()
15892       ;; Remove any negative articles numbers.
15893       (while (and unread (< (car unread) 0))
15894         (setq unread (cdr unread)))
15895       ;; Remove any expired article numbers
15896       (while (and unread (< (car unread) (car active)))
15897         (setq unread (cdr unread)))
15898       ;; Compute the ranges of read articles by looking at the list of
15899       ;; unread articles.
15900       (while unread
15901         (if (/= (car unread) prev)
15902             (setq read (cons (if (= prev (1- (car unread))) prev
15903                                (cons prev (1- (car unread)))) read)))
15904         (setq prev (1+ (car unread)))
15905         (setq unread (cdr unread)))
15906       (when (<= prev (cdr active))
15907         (setq read (cons (cons prev (cdr active)) read)))
15908       ;; Enter this list into the group info.
15909       (gnus-info-set-read
15910        info (if (> (length read) 1) (nreverse read) read))
15911       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15912       (gnus-get-unread-articles-in-group info (gnus-active group))
15913       t)))
15914
15915 (defun gnus-make-articles-unread (group articles)
15916   "Mark ARTICLES in GROUP as unread."
15917   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15918                           (gnus-gethash (gnus-group-real-name group)
15919                                         gnus-newsrc-hashtb))))
15920          (ranges (gnus-info-read info))
15921          news article)
15922     (while articles
15923       (when (gnus-member-of-range
15924              (setq article (pop articles)) ranges)
15925         (setq news (cons article news))))
15926     (when news
15927       (gnus-info-set-read
15928        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15929       (gnus-group-update-group group t))))
15930
15931 ;; Enter all dead groups into the hashtb.
15932 (defun gnus-update-active-hashtb-from-killed ()
15933   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15934         (lists (list gnus-killed-list gnus-zombie-list))
15935         killed)
15936     (while lists
15937       (setq killed (car lists))
15938       (while killed
15939         (gnus-sethash (car killed) nil hashtb)
15940         (setq killed (cdr killed)))
15941       (setq lists (cdr lists)))))
15942
15943 (defun gnus-get-killed-groups ()
15944   "Go through the active hashtb and all all unknown groups as killed."
15945   ;; First make sure active file has been read.
15946   (unless (gnus-read-active-file-p)
15947     (let ((gnus-read-active-file t))
15948       (gnus-read-active-file)))
15949   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15950   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15951   (mapatoms
15952    (lambda (sym)
15953      (let ((groups 0)
15954            (group (symbol-name sym)))
15955        (if (or (null group)
15956                (gnus-gethash group gnus-killed-hashtb)
15957                (gnus-gethash group gnus-newsrc-hashtb))
15958            ()
15959          (let ((do-sub (gnus-matches-options-n group)))
15960            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
15961                ()
15962              (setq groups (1+ groups))
15963              (setq gnus-killed-list
15964                    (cons group gnus-killed-list))
15965              (gnus-sethash group group gnus-killed-hashtb))))))
15966    gnus-active-hashtb))
15967
15968 ;; Get the active file(s) from the backend(s).
15969 (defun gnus-read-active-file ()
15970   (gnus-group-set-mode-line)
15971   (let ((methods 
15972          (append
15973           (if (gnus-check-server gnus-select-method)
15974               ;; The native server is available.
15975               (cons gnus-select-method gnus-secondary-select-methods)
15976             ;; The native server is down, so we just do the
15977             ;; secondary ones.
15978             gnus-secondary-select-methods)
15979           ;; Also read from the archive server.
15980           (when gnus-message-archive-method
15981             (list "archive"))))
15982         list-type)
15983     (setq gnus-have-read-active-file nil)
15984     (save-excursion
15985       (set-buffer nntp-server-buffer)
15986       (while methods
15987         (let* ((method (if (stringp (car methods))
15988                            (gnus-server-get-method nil (car methods))
15989                          (car methods)))
15990                (where (nth 1 method))
15991                (mesg (format "Reading active file%s via %s..."
15992                              (if (and where (not (zerop (length where))))
15993                                  (concat " from " where) "")
15994                              (car method))))
15995           (gnus-message 5 mesg)
15996           (when (gnus-check-server method)
15997             ;; Request that the backend scan its incoming messages.
15998             (and (gnus-check-backend-function 'request-scan (car method))
15999                  (gnus-request-scan nil method))
16000             (cond
16001              ((and (eq gnus-read-active-file 'some)
16002                    (gnus-check-backend-function 'retrieve-groups (car method)))
16003               (let ((newsrc (cdr gnus-newsrc-alist))
16004                     (gmethod (gnus-server-get-method nil method))
16005                     groups info)
16006                 (while (setq info (pop newsrc))
16007                   (when (gnus-server-equal
16008                          (gnus-find-method-for-group 
16009                           (gnus-info-group info) info)
16010                          gmethod)
16011                     (push (gnus-group-real-name (gnus-info-group info)) 
16012                           groups)))
16013                 (when groups
16014                   (gnus-check-server method)
16015                   (setq list-type (gnus-retrieve-groups groups method))
16016                   (cond
16017                    ((not list-type)
16018                     (gnus-error
16019                      1.2 "Cannot read partial active file from %s server."
16020                      (car method)))
16021                    ((eq list-type 'active)
16022                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16023                    (t
16024                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16025              (t
16026               (if (not (gnus-request-list method))
16027                   (unless (equal method gnus-message-archive-method)
16028                     (gnus-error 1 "Cannot read active file from %s server."
16029                                 (car method)))
16030                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16031                 ;; We mark this active file as read.
16032                 (push method gnus-have-read-active-file)
16033                 (gnus-message 5 "%sdone" mesg))))))
16034         (setq methods (cdr methods))))))
16035
16036 ;; Read an active file and place the results in `gnus-active-hashtb'.
16037 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16038   (unless method
16039     (setq method gnus-select-method))
16040   (let ((cur (current-buffer))
16041         (hashtb (or hashtb
16042                     (if (and gnus-active-hashtb
16043                              (not (equal method gnus-select-method)))
16044                         gnus-active-hashtb
16045                       (setq gnus-active-hashtb
16046                             (if (equal method gnus-select-method)
16047                                 (gnus-make-hashtable
16048                                  (count-lines (point-min) (point-max)))
16049                               (gnus-make-hashtable 4096)))))))
16050     ;; Delete unnecessary lines.
16051     (goto-char (point-min))
16052     (while (search-forward "\nto." nil t)
16053       (delete-region (1+ (match-beginning 0))
16054                      (progn (forward-line 1) (point))))
16055     (or (string= gnus-ignored-newsgroups "")
16056         (progn
16057           (goto-char (point-min))
16058           (delete-matching-lines gnus-ignored-newsgroups)))
16059     ;; Make the group names readable as a lisp expression even if they
16060     ;; contain special characters.
16061     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16062     (goto-char (point-max))
16063     (while (re-search-backward "[][';?()#]" nil t)
16064       (insert ?\\))
16065     ;; If these are groups from a foreign select method, we insert the
16066     ;; group prefix in front of the group names.
16067     (and method (not (gnus-server-equal
16068                       (gnus-server-get-method nil method)
16069                       (gnus-server-get-method nil gnus-select-method)))
16070          (let ((prefix (gnus-group-prefixed-name "" method)))
16071            (goto-char (point-min))
16072            (while (and (not (eobp))
16073                        (progn (insert prefix)
16074                               (zerop (forward-line 1)))))))
16075     ;; Store the active file in a hash table.
16076     (goto-char (point-min))
16077     (if (string-match "%[oO]" gnus-group-line-format)
16078         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16079         ;; If we want information on moderated groups, we use this
16080         ;; loop...
16081         (let* ((mod-hashtb (make-vector 7 0))
16082                (m (intern "m" mod-hashtb))
16083                group max min)
16084           (while (not (eobp))
16085             (condition-case nil
16086                 (progn
16087                   (narrow-to-region (point) (gnus-point-at-eol))
16088                   (setq group (let ((obarray hashtb)) (read cur)))
16089                   (if (and (numberp (setq max (read cur)))
16090                            (numberp (setq min (read cur)))
16091                            (progn
16092                              (skip-chars-forward " \t")
16093                              (not
16094                               (or (= (following-char) ?=)
16095                                   (= (following-char) ?x)
16096                                   (= (following-char) ?j)))))
16097                       (set group (cons min max))
16098                     (set group nil))
16099                   ;; Enter moderated groups into a list.
16100                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16101                       (setq gnus-moderated-list
16102                             (cons (symbol-name group) gnus-moderated-list))))
16103               (error
16104                (and group
16105                     (symbolp group)
16106                     (set group nil))))
16107             (widen)
16108             (forward-line 1)))
16109       ;; And if we do not care about moderation, we use this loop,
16110       ;; which is faster.
16111       (let (group max min)
16112         (while (not (eobp))
16113           (condition-case ()
16114               (progn
16115                 (narrow-to-region (point) (gnus-point-at-eol))
16116                 ;; group gets set to a symbol interned in the hash table
16117                 ;; (what a hack!!) - jwz
16118                 (setq group (let ((obarray hashtb)) (read cur)))
16119                 (if (and (numberp (setq max (read cur)))
16120                          (numberp (setq min (read cur)))
16121                          (progn
16122                            (skip-chars-forward " \t")
16123                            (not
16124                             (or (= (following-char) ?=)
16125                                 (= (following-char) ?x)
16126                                 (= (following-char) ?j)))))
16127                     (set group (cons min max))
16128                   (set group nil)))
16129             (error
16130              (progn
16131                (and group
16132                     (symbolp group)
16133                     (set group nil))
16134                (or ignore-errors
16135                    (gnus-message 3 "Warning - illegal active: %s"
16136                                  (buffer-substring
16137                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16138           (widen)
16139           (forward-line 1))))))
16140
16141 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16142   ;; Parse a "groups" active file.
16143   (let ((cur (current-buffer))
16144         (hashtb (or hashtb
16145                     (if (and method gnus-active-hashtb)
16146                         gnus-active-hashtb
16147                       (setq gnus-active-hashtb
16148                             (gnus-make-hashtable
16149                              (count-lines (point-min) (point-max)))))))
16150         (prefix (and method
16151                      (not (gnus-server-equal
16152                            (gnus-server-get-method nil method)
16153                            (gnus-server-get-method nil gnus-select-method)))
16154                      (gnus-group-prefixed-name "" method))))
16155
16156     (goto-char (point-min))
16157     ;; We split this into to separate loops, one with the prefix
16158     ;; and one without to speed the reading up somewhat.
16159     (if prefix
16160         (let (min max opoint group)
16161           (while (not (eobp))
16162             (condition-case ()
16163                 (progn
16164                   (read cur) (read cur)
16165                   (setq min (read cur)
16166                         max (read cur)
16167                         opoint (point))
16168                   (skip-chars-forward " \t")
16169                   (insert prefix)
16170                   (goto-char opoint)
16171                   (set (let ((obarray hashtb)) (read cur))
16172                        (cons min max)))
16173               (error (and group (symbolp group) (set group nil))))
16174             (forward-line 1)))
16175       (let (min max group)
16176         (while (not (eobp))
16177           (condition-case ()
16178               (if (= (following-char) ?2)
16179                   (progn
16180                     (read cur) (read cur)
16181                     (setq min (read cur)
16182                           max (read cur))
16183                     (set (setq group (let ((obarray hashtb)) (read cur)))
16184                          (cons min max))))
16185             (error (and group (symbolp group) (set group nil))))
16186           (forward-line 1))))))
16187
16188 (defun gnus-read-newsrc-file (&optional force)
16189   "Read startup file.
16190 If FORCE is non-nil, the .newsrc file is read."
16191   ;; Reset variables that might be defined in the .newsrc.eld file.
16192   (let ((variables gnus-variable-list))
16193     (while variables
16194       (set (car variables) nil)
16195       (setq variables (cdr variables))))
16196   (let* ((newsrc-file gnus-current-startup-file)
16197          (quick-file (concat newsrc-file ".el")))
16198     (save-excursion
16199       ;; We always load the .newsrc.eld file.  If always contains
16200       ;; much information that can not be gotten from the .newsrc
16201       ;; file (ticked articles, killed groups, foreign methods, etc.)
16202       (gnus-read-newsrc-el-file quick-file)
16203
16204       (if (and (file-exists-p gnus-current-startup-file)
16205                (or force
16206                    (and (file-newer-than-file-p newsrc-file quick-file)
16207                         (file-newer-than-file-p newsrc-file
16208                                                 (concat quick-file "d")))
16209                    (not gnus-newsrc-alist)))
16210           ;; We read the .newsrc file.  Note that if there if a
16211           ;; .newsrc.eld file exists, it has already been read, and
16212           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16213           ;; the .newsrc file, Gnus will only use the information it
16214           ;; can find there for changing the data already read -
16215           ;; ie. reading the .newsrc file will not trash the data
16216           ;; already read (except for read articles).
16217           (save-excursion
16218             (gnus-message 5 "Reading %s..." newsrc-file)
16219             (set-buffer (find-file-noselect newsrc-file))
16220             (buffer-disable-undo (current-buffer))
16221             (gnus-newsrc-to-gnus-format)
16222             (kill-buffer (current-buffer))
16223             (gnus-message 5 "Reading %s...done" newsrc-file)))
16224
16225       ;; Read any slave files.
16226       (unless gnus-slave
16227         (gnus-master-read-slave-newsrc))
16228       
16229       ;; Convert old to new.
16230       (gnus-convert-old-newsrc))))
16231
16232 (defun gnus-continuum-version (version)
16233   "Return VERSION as a floating point number."
16234   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16235     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16236            (number (match-string 2 version))
16237            major minor least)
16238       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16239       (setq major (string-to-number (match-string 1 number)))
16240       (setq minor (string-to-number (match-string 2 number)))
16241       (setq least (if (match-beginning 3)
16242                       (string-to-number (match-string 3 number))
16243                     0))
16244       (string-to-number
16245        (if (zerop major)
16246            (format "%s00%02d%02d"
16247                    (cond 
16248                     ((string= alpha "(ding)") "4.99")
16249                     ((string= alpha "September") "5.01")
16250                     ((string= alpha "Red") "5.03"))
16251                    minor least)
16252          (format "%d.%02d%20d" major minor least))))))
16253
16254 (defun gnus-convert-old-newsrc ()
16255   "Convert old newsrc into the new format, if needed."
16256   (let ((fcv (and gnus-newsrc-file-version
16257                   (gnus-continuum-version gnus-newsrc-file-version))))
16258     (cond
16259      ;; No .newsrc.eld file was loaded.
16260      ((null fcv) nil)
16261      ;; Gnus 5 .newsrc.eld was loaded.
16262      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16263       (gnus-convert-old-ticks)))))
16264
16265 (defun gnus-convert-old-ticks ()
16266   (let ((newsrc (cdr gnus-newsrc-alist))
16267         marks info dormant ticked)
16268     (while (setq info (pop newsrc))
16269       (when (setq marks (gnus-info-marks info))
16270         (setq dormant (cdr (assq 'dormant marks))
16271               ticked (cdr (assq 'tick marks)))
16272         (when (or dormant ticked)
16273           (gnus-info-set-read
16274            info
16275            (gnus-add-to-range
16276             (gnus-info-read info)
16277             (nconc (gnus-uncompress-range dormant)
16278                    (gnus-uncompress-range ticked)))))))))
16279
16280 (defun gnus-read-newsrc-el-file (file)
16281   (let ((ding-file (concat file "d")))
16282     ;; We always, always read the .eld file.
16283     (gnus-message 5 "Reading %s..." ding-file)
16284     (let (gnus-newsrc-assoc)
16285       (condition-case nil
16286           (load ding-file t t t)
16287         (error
16288          (gnus-error 1 "Error in %s" ding-file)))
16289       (when gnus-newsrc-assoc
16290         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16291     (gnus-make-hashtable-from-newsrc-alist)
16292     (when (file-newer-than-file-p file ding-file)
16293       ;; Old format quick file
16294       (gnus-message 5 "Reading %s..." file)
16295       ;; The .el file is newer than the .eld file, so we read that one
16296       ;; as well.
16297       (gnus-read-old-newsrc-el-file file))))
16298
16299 ;; Parse the old-style quick startup file
16300 (defun gnus-read-old-newsrc-el-file (file)
16301   (let (newsrc killed marked group m info)
16302     (prog1
16303         (let ((gnus-killed-assoc nil)
16304               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16305           (prog1
16306               (condition-case nil
16307                   (load file t t t)
16308                 (error nil))
16309             (setq newsrc gnus-newsrc-assoc
16310                   killed gnus-killed-assoc
16311                   marked gnus-marked-assoc)))
16312       (setq gnus-newsrc-alist nil)
16313       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16314         (if info
16315             (progn
16316               (gnus-info-set-read info (cddr group))
16317               (gnus-info-set-level
16318                info (if (nth 1 group) gnus-level-default-subscribed
16319                       gnus-level-default-unsubscribed))
16320               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16321           (push (setq info
16322                       (list (car group)
16323                             (if (nth 1 group) gnus-level-default-subscribed
16324                               gnus-level-default-unsubscribed)
16325                             (cddr group)))
16326                 gnus-newsrc-alist))
16327         ;; Copy marks into info.
16328         (when (setq m (assoc (car group) marked))
16329           (unless (nthcdr 3 info)
16330             (nconc info (list nil)))
16331           (gnus-info-set-marks
16332            info (list (cons 'tick (gnus-compress-sequence 
16333                                    (sort (cdr m) '<) t))))))
16334       (setq newsrc killed)
16335       (while newsrc
16336         (setcar newsrc (caar newsrc))
16337         (setq newsrc (cdr newsrc)))
16338       (setq gnus-killed-list killed))
16339     ;; The .el file version of this variable does not begin with
16340     ;; "options", while the .eld version does, so we just add it if it
16341     ;; isn't there.
16342     (and
16343      gnus-newsrc-options
16344      (progn
16345        (and (not (string-match "^ *options" gnus-newsrc-options))
16346             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16347        (and (not (string-match "\n$" gnus-newsrc-options))
16348             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16349        ;; Finally, if we read some options lines, we parse them.
16350        (or (string= gnus-newsrc-options "")
16351            (gnus-newsrc-parse-options gnus-newsrc-options))))
16352
16353     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16354     (gnus-make-hashtable-from-newsrc-alist)))
16355
16356 (defun gnus-make-newsrc-file (file)
16357   "Make server dependent file name by catenating FILE and server host name."
16358   (let* ((file (expand-file-name file nil))
16359          (real-file (concat file "-" (nth 1 gnus-select-method))))
16360     (if (or (file-exists-p real-file)
16361             (file-exists-p (concat real-file ".el"))
16362             (file-exists-p (concat real-file ".eld")))
16363         real-file file)))
16364
16365 (defun gnus-newsrc-to-gnus-format ()
16366   (setq gnus-newsrc-options "")
16367   (setq gnus-newsrc-options-n nil)
16368
16369   (or gnus-active-hashtb
16370       (setq gnus-active-hashtb (make-vector 4095 0)))
16371   (let ((buf (current-buffer))
16372         (already-read (> (length gnus-newsrc-alist) 1))
16373         group subscribed options-symbol newsrc Options-symbol
16374         symbol reads num1)
16375     (goto-char (point-min))
16376     ;; We intern the symbol `options' in the active hashtb so that we
16377     ;; can `eq' against it later.
16378     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16379     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16380
16381     (while (not (eobp))
16382       ;; We first read the first word on the line by narrowing and
16383       ;; then reading into `gnus-active-hashtb'.  Most groups will
16384       ;; already exist in that hashtb, so this will save some string
16385       ;; space.
16386       (narrow-to-region
16387        (point)
16388        (progn (skip-chars-forward "^ \t!:\n") (point)))
16389       (goto-char (point-min))
16390       (setq symbol
16391             (and (/= (point-min) (point-max))
16392                  (let ((obarray gnus-active-hashtb)) (read buf))))
16393       (widen)
16394       ;; Now, the symbol we have read is either `options' or a group
16395       ;; name.  If it is an options line, we just add it to a string.
16396       (cond
16397        ((or (eq symbol options-symbol)
16398             (eq symbol Options-symbol))
16399         (setq gnus-newsrc-options
16400               ;; This concating is quite inefficient, but since our
16401               ;; thorough studies show that approx 99.37% of all
16402               ;; .newsrc files only contain a single options line, we
16403               ;; don't give a damn, frankly, my dear.
16404               (concat gnus-newsrc-options
16405                       (buffer-substring
16406                        (gnus-point-at-bol)
16407                        ;; Options may continue on the next line.
16408                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16409                                 (progn (beginning-of-line) (point)))
16410                            (point)))))
16411         (forward-line -1))
16412        (symbol
16413         ;; Group names can be just numbers.  
16414         (when (numberp symbol) 
16415           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16416         (or (boundp symbol) (set symbol nil))
16417         ;; It was a group name.
16418         (setq subscribed (= (following-char) ?:)
16419               group (symbol-name symbol)
16420               reads nil)
16421         (if (eolp)
16422             ;; If the line ends here, this is clearly a buggy line, so
16423             ;; we put point a the beginning of line and let the cond
16424             ;; below do the error handling.
16425             (beginning-of-line)
16426           ;; We skip to the beginning of the ranges.
16427           (skip-chars-forward "!: \t"))
16428         ;; We are now at the beginning of the list of read articles.
16429         ;; We read them range by range.
16430         (while
16431             (cond
16432              ((looking-at "[0-9]+")
16433               ;; We narrow and read a number instead of buffer-substring/
16434               ;; string-to-int because it's faster.  narrow/widen is
16435               ;; faster than save-restriction/narrow, and save-restriction
16436               ;; produces a garbage object.
16437               (setq num1 (progn
16438                            (narrow-to-region (match-beginning 0) (match-end 0))
16439                            (read buf)))
16440               (widen)
16441               ;; If the next character is a dash, then this is a range.
16442               (if (= (following-char) ?-)
16443                   (progn
16444                     ;; We read the upper bound of the range.
16445                     (forward-char 1)
16446                     (if (not (looking-at "[0-9]+"))
16447                         ;; This is a buggy line, by we pretend that
16448                         ;; it's kinda OK.  Perhaps the user should be
16449                         ;; dinged?
16450                         (setq reads (cons num1 reads))
16451                       (setq reads
16452                             (cons
16453                              (cons num1
16454                                    (progn
16455                                      (narrow-to-region (match-beginning 0)
16456                                                        (match-end 0))
16457                                      (read buf)))
16458                              reads))
16459                       (widen)))
16460                 ;; It was just a simple number, so we add it to the
16461                 ;; list of ranges.
16462                 (setq reads (cons num1 reads)))
16463               ;; If the next char in ?\n, then we have reached the end
16464               ;; of the line and return nil.
16465               (/= (following-char) ?\n))
16466              ((= (following-char) ?\n)
16467               ;; End of line, so we end.
16468               nil)
16469              (t
16470               ;; Not numbers and not eol, so this might be a buggy
16471               ;; line...
16472               (or (eobp)
16473                   ;; If it was eob instead of ?\n, we allow it.
16474                   (progn
16475                     ;; The line was buggy.
16476                     (setq group nil)
16477                     (gnus-error 3.1 "Mangled line: %s"
16478                                 (buffer-substring (gnus-point-at-bol)
16479                                                   (gnus-point-at-eol)))))
16480               nil))
16481           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16482           ;; we allow them, because it's a common mistake to put a
16483           ;; space after the comma.
16484           (skip-chars-forward ", "))
16485
16486         ;; We have already read .newsrc.eld, so we gently update the
16487         ;; data in the hash table with the information we have just
16488         ;; read.
16489         (when group
16490           (let ((info (gnus-get-info group))
16491                 level)
16492             (if info
16493                 ;; There is an entry for this file in the alist.
16494                 (progn
16495                   (gnus-info-set-read info (nreverse reads))
16496                   ;; We update the level very gently.  In fact, we
16497                   ;; only change it if there's been a status change
16498                   ;; from subscribed to unsubscribed, or vice versa.
16499                   (setq level (gnus-info-level info))
16500                   (cond ((and (<= level gnus-level-subscribed)
16501                               (not subscribed))
16502                          (setq level (if reads
16503                                          gnus-level-default-unsubscribed
16504                                        (1+ gnus-level-default-unsubscribed))))
16505                         ((and (> level gnus-level-subscribed) subscribed)
16506                          (setq level gnus-level-default-subscribed)))
16507                   (gnus-info-set-level info level))
16508               ;; This is a new group.
16509               (setq info (list group
16510                                (if subscribed
16511                                    gnus-level-default-subscribed
16512                                  (if reads
16513                                      (1+ gnus-level-subscribed)
16514                                    gnus-level-default-unsubscribed))
16515                                (nreverse reads))))
16516             (setq newsrc (cons info newsrc))))))
16517       (forward-line 1))
16518
16519     (setq newsrc (nreverse newsrc))
16520
16521     (if (not already-read)
16522         ()
16523       ;; We now have two newsrc lists - `newsrc', which is what we
16524       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16525       ;; what we've read from .newsrc.eld.  We have to merge these
16526       ;; lists.  We do this by "attaching" any (foreign) groups in the
16527       ;; gnus-newsrc-alist to the (native) group that precedes them.
16528       (let ((rc (cdr gnus-newsrc-alist))
16529             (prev gnus-newsrc-alist)
16530             entry mentry)
16531         (while rc
16532           (or (null (nth 4 (car rc)))   ; It's a native group.
16533               (assoc (caar rc) newsrc) ; It's already in the alist.
16534               (if (setq entry (assoc (caar prev) newsrc))
16535                   (setcdr (setq mentry (memq entry newsrc))
16536                           (cons (car rc) (cdr mentry)))
16537                 (setq newsrc (cons (car rc) newsrc))))
16538           (setq prev rc
16539                 rc (cdr rc)))))
16540
16541     (setq gnus-newsrc-alist newsrc)
16542     ;; We make the newsrc hashtb.
16543     (gnus-make-hashtable-from-newsrc-alist)
16544
16545     ;; Finally, if we read some options lines, we parse them.
16546     (or (string= gnus-newsrc-options "")
16547         (gnus-newsrc-parse-options gnus-newsrc-options))))
16548
16549 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16550 ;; The return value will be a list on the form
16551 ;; ((regexp1 . ignore)
16552 ;;  (regexp2 . subscribe)...)
16553 ;; When handling new newsgroups, groups that match a `ignore' regexp
16554 ;; will be ignored, and groups that match a `subscribe' regexp will be
16555 ;; subscribed.  A line like
16556 ;; options -n !all rec.all
16557 ;; will lead to a list that looks like
16558 ;; (("^rec\\..+" . subscribe)
16559 ;;  ("^.+" . ignore))
16560 ;; So all "rec.*" groups will be subscribed, while all the other
16561 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16562 ;; different from "options -n rec.all !all".
16563 (defun gnus-newsrc-parse-options (options)
16564   (let (out eol)
16565     (save-excursion
16566       (gnus-set-work-buffer)
16567       (insert (regexp-quote options))
16568       ;; First we treat all continuation lines.
16569       (goto-char (point-min))
16570       (while (re-search-forward "\n[ \t]+" nil t)
16571         (replace-match " " t t))
16572       ;; Then we transform all "all"s into ".+"s.
16573       (goto-char (point-min))
16574       (while (re-search-forward "\\ball\\b" nil t)
16575         (replace-match ".+" t t))
16576       (goto-char (point-min))
16577       ;; We remove all other options than the "-n" ones.
16578       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16579         (replace-match " ")
16580         (forward-char -1))
16581       (goto-char (point-min))
16582
16583       ;; We are only interested in "options -n" lines - we
16584       ;; ignore the other option lines.
16585       (while (re-search-forward "[ \t]-n" nil t)
16586         (setq eol
16587               (or (save-excursion
16588                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16589                          (- (point) 2)))
16590                   (gnus-point-at-eol)))
16591         ;; Search for all "words"...
16592         (while (re-search-forward "[^ \t,\n]+" eol t)
16593           (if (= (char-after (match-beginning 0)) ?!)
16594               ;; If the word begins with a bang (!), this is a "not"
16595               ;; spec.  We put this spec (minus the bang) and the
16596               ;; symbol `ignore' into the list.
16597               (setq out (cons (cons (concat
16598                                      "^" (buffer-substring
16599                                           (1+ (match-beginning 0))
16600                                           (match-end 0)))
16601                                     'ignore) out))
16602             ;; There was no bang, so this is a "yes" spec.
16603             (setq out (cons (cons (concat "^" (match-string 0))
16604                                   'subscribe) out)))))
16605
16606       (setq gnus-newsrc-options-n out))))
16607
16608 (defun gnus-save-newsrc-file (&optional force)
16609   "Save .newsrc file."
16610   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16611   ;; from the variable gnus-newsrc-alist.
16612   (when (and (or gnus-newsrc-alist gnus-killed-list)
16613              gnus-current-startup-file)
16614     (save-excursion
16615       (if (and (or gnus-use-dribble-file gnus-slave)
16616                (not force)
16617                (or (not gnus-dribble-buffer)
16618                    (not (buffer-name gnus-dribble-buffer))
16619                    (zerop (save-excursion
16620                             (set-buffer gnus-dribble-buffer)
16621                             (buffer-size)))))
16622           (gnus-message 4 "(No changes need to be saved)")
16623         (run-hooks 'gnus-save-newsrc-hook)
16624         (if gnus-slave
16625             (gnus-slave-save-newsrc)
16626           ;; Save .newsrc.
16627           (when gnus-save-newsrc-file
16628             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16629             (gnus-gnus-to-newsrc-format)
16630             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16631           ;; Save .newsrc.eld.
16632           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16633           (make-local-variable 'version-control)
16634           (setq version-control 'never)
16635           (setq buffer-file-name
16636                 (concat gnus-current-startup-file ".eld"))
16637           (gnus-add-current-to-buffer-list)
16638           (buffer-disable-undo (current-buffer))
16639           (erase-buffer)
16640           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16641           (gnus-gnus-to-quick-newsrc-format)
16642           (run-hooks 'gnus-save-quick-newsrc-hook)
16643           (save-buffer)
16644           (kill-buffer (current-buffer))
16645           (gnus-message
16646            5 "Saving %s.eld...done" gnus-current-startup-file))
16647         (gnus-dribble-delete-file)
16648         (gnus-group-set-mode-line)))))
16649
16650 (defun gnus-gnus-to-quick-newsrc-format ()
16651   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16652   (insert ";; Gnus startup file.\n")
16653   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16654   (insert ";; to read .newsrc.\n")
16655   (insert "(setq gnus-newsrc-file-version "
16656           (prin1-to-string gnus-version) ")\n")
16657   (let ((variables
16658          (if gnus-save-killed-list gnus-variable-list
16659            ;; Remove the `gnus-killed-list' from the list of variables
16660            ;; to be saved, if required.
16661            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16662         ;; Peel off the "dummy" group.
16663         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16664         variable)
16665     ;; Insert the variables into the file.
16666     (while variables
16667       (when (and (boundp (setq variable (pop variables)))
16668                  (symbol-value variable))
16669         (insert "(setq " (symbol-name variable) " '")
16670         (prin1 (symbol-value variable) (current-buffer))
16671         (insert ")\n")))))
16672
16673 (defun gnus-gnus-to-newsrc-format ()
16674   ;; Generate and save the .newsrc file.
16675   (save-excursion
16676     (set-buffer (create-file-buffer gnus-current-startup-file))
16677     (let ((newsrc (cdr gnus-newsrc-alist))
16678           (standard-output (current-buffer))
16679           info ranges range method)
16680       (setq buffer-file-name gnus-current-startup-file)
16681       (buffer-disable-undo (current-buffer))
16682       (erase-buffer)
16683       ;; Write options.
16684       (if gnus-newsrc-options (insert gnus-newsrc-options))
16685       ;; Write subscribed and unsubscribed.
16686       (while (setq info (pop newsrc))
16687         ;; Don't write foreign groups to .newsrc.
16688         (when (or (null (setq method (gnus-info-method info)))
16689                   (equal method "native")
16690                   (gnus-server-equal method gnus-select-method))
16691           (insert (gnus-info-group info)
16692                   (if (> (gnus-info-level info) gnus-level-subscribed)
16693                       "!" ":"))
16694           (when (setq ranges (gnus-info-read info))
16695             (insert " ")
16696             (if (not (listp (cdr ranges)))
16697                 (if (= (car ranges) (cdr ranges))
16698                     (princ (car ranges))
16699                   (princ (car ranges))
16700                   (insert "-")
16701                   (princ (cdr ranges)))
16702               (while (setq range (pop ranges))
16703                 (if (or (atom range) (= (car range) (cdr range)))
16704                     (princ (or (and (atom range) range) (car range)))
16705                   (princ (car range))
16706                   (insert "-")
16707                   (princ (cdr range)))
16708                 (if ranges (insert ",")))))
16709           (insert "\n")))
16710       (make-local-variable 'version-control)
16711       (setq version-control 'never)
16712       ;; It has been reported that sometime the modtime on the .newsrc
16713       ;; file seems to be off.  We really do want to overwrite it, so
16714       ;; we clear the modtime here before saving.  It's a bit odd,
16715       ;; though...
16716       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16717       ;; delete the silly thing entirely first.  but this fails to provide
16718       ;; such niceties as .newsrc~ creation.
16719       (if gnus-modtime-botch
16720           (delete-file gnus-startup-file)
16721         (clear-visited-file-modtime))
16722       (run-hooks 'gnus-save-standard-newsrc-hook)
16723       (save-buffer)
16724       (kill-buffer (current-buffer)))))
16725
16726 \f
16727 ;;;
16728 ;;; Slave functions.
16729 ;;;
16730
16731 (defun gnus-slave-save-newsrc ()
16732   (save-excursion
16733     (set-buffer gnus-dribble-buffer)
16734     (let ((slave-name
16735            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16736       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16737
16738 (defun gnus-master-read-slave-newsrc ()
16739   (let ((slave-files
16740          (directory-files
16741           (file-name-directory gnus-current-startup-file)
16742           t (concat
16743              "^" (regexp-quote
16744                   (concat
16745                    (file-name-nondirectory gnus-current-startup-file)
16746                    "-slave-")))
16747           t))
16748         file)
16749     (if (not slave-files)
16750         ()                              ; There are no slave files to read.
16751       (gnus-message 7 "Reading slave newsrcs...")
16752       (save-excursion
16753         (set-buffer (get-buffer-create " *gnus slave*"))
16754         (buffer-disable-undo (current-buffer))
16755         (setq slave-files
16756               (sort (mapcar (lambda (file)
16757                               (list (nth 5 (file-attributes file)) file))
16758                             slave-files)
16759                     (lambda (f1 f2)
16760                       (or (< (caar f1) (caar f2))
16761                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16762         (while slave-files
16763           (erase-buffer)
16764           (setq file (nth 1 (car slave-files)))
16765           (insert-file-contents file)
16766           (if (condition-case ()
16767                   (progn
16768                     (eval-buffer (current-buffer))
16769                     t)
16770                 (error
16771                  (gnus-error 3.2 "Possible error in %s" file)
16772                  nil))
16773               (or gnus-slave ; Slaves shouldn't delete these files.
16774                   (condition-case ()
16775                       (delete-file file)
16776                     (error nil))))
16777           (setq slave-files (cdr slave-files))))
16778       (gnus-message 7 "Reading slave newsrcs...done"))))
16779
16780 \f
16781 ;;;
16782 ;;; Group description.
16783 ;;;
16784
16785 (defun gnus-read-all-descriptions-files ()
16786   (let ((methods (cons gnus-select-method 
16787                        (nconc
16788                         (when gnus-message-archive-method
16789                           (list "archive"))
16790                         gnus-secondary-select-methods))))
16791     (while methods
16792       (gnus-read-descriptions-file (car methods))
16793       (setq methods (cdr methods)))
16794     t))
16795
16796 (defun gnus-read-descriptions-file (&optional method)
16797   (let ((method (or method gnus-select-method)))
16798     (when (stringp method)
16799       (setq method (gnus-server-to-method method)))
16800     ;; We create the hashtable whether we manage to read the desc file
16801     ;; to avoid trying to re-read after a failed read.
16802     (or gnus-description-hashtb
16803         (setq gnus-description-hashtb
16804               (gnus-make-hashtable (length gnus-active-hashtb))))
16805     ;; Mark this method's desc file as read.
16806     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16807                   gnus-description-hashtb)
16808
16809     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16810     (cond
16811      ((not (gnus-check-server method))
16812       (gnus-message 1 "Couldn't open server")
16813       nil)
16814      ((not (gnus-request-list-newsgroups method))
16815       (gnus-message 1 "Couldn't read newsgroups descriptions")
16816       nil)
16817      (t
16818       (let (group)
16819         (save-excursion
16820           (save-restriction
16821             (set-buffer nntp-server-buffer)
16822             (goto-char (point-min))
16823             (if (or (search-forward "\n.\n" nil t)
16824                     (goto-char (point-max)))
16825                 (progn
16826                   (beginning-of-line)
16827                   (narrow-to-region (point-min) (point))))
16828             (goto-char (point-min))
16829             (while (not (eobp))
16830               ;; If we get an error, we set group to 0, which is not a
16831               ;; symbol...
16832               (setq group
16833                     (condition-case ()
16834                         (let ((obarray gnus-description-hashtb))
16835                           ;; Group is set to a symbol interned in this
16836                           ;; hash table.
16837                           (read nntp-server-buffer))
16838                       (error 0)))
16839               (skip-chars-forward " \t")
16840               ;; ...  which leads to this line being effectively ignored.
16841               (and (symbolp group)
16842                    (set group (buffer-substring
16843                                (point) (progn (end-of-line) (point)))))
16844               (forward-line 1))))
16845         (gnus-message 5 "Reading descriptions file...done")
16846         t)))))
16847
16848 (defun gnus-group-get-description (group)
16849   "Get the description of a group by sending XGTITLE to the server."
16850   (when (gnus-request-group-description group)
16851     (save-excursion
16852       (set-buffer nntp-server-buffer)
16853       (goto-char (point-min))
16854       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16855         (match-string 1)))))
16856
16857 \f
16858 ;;;
16859 ;;; Buffering of read articles.
16860 ;;;
16861
16862 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16863 (defvar gnus-backlog-articles nil)
16864 (defvar gnus-backlog-hashtb nil)
16865
16866 (defun gnus-backlog-buffer ()
16867   "Return the backlog buffer."
16868   (or (get-buffer gnus-backlog-buffer)
16869       (save-excursion
16870         (set-buffer (get-buffer-create gnus-backlog-buffer))
16871         (buffer-disable-undo (current-buffer))
16872         (setq buffer-read-only t)
16873         (gnus-add-current-to-buffer-list)
16874         (get-buffer gnus-backlog-buffer))))
16875
16876 (defun gnus-backlog-setup ()
16877   "Initialize backlog variables."
16878   (unless gnus-backlog-hashtb
16879     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16880
16881 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16882
16883 (defun gnus-backlog-shutdown ()
16884   "Clear all backlog variables and buffers."
16885   (when (get-buffer gnus-backlog-buffer)
16886     (kill-buffer gnus-backlog-buffer))
16887   (setq gnus-backlog-hashtb nil
16888         gnus-backlog-articles nil))
16889
16890 (defun gnus-backlog-enter-article (group number buffer)
16891   (gnus-backlog-setup)
16892   (let ((ident (intern (concat group ":" (int-to-string number))
16893                        gnus-backlog-hashtb))
16894         b)
16895     (if (memq ident gnus-backlog-articles)
16896         () ; It's already kept.
16897       ;; Remove the oldest article, if necessary.
16898       (and (numberp gnus-keep-backlog)
16899            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16900            (gnus-backlog-remove-oldest-article))
16901       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16902       ;; Insert the new article.
16903       (save-excursion
16904         (set-buffer (gnus-backlog-buffer))
16905         (let (buffer-read-only)
16906           (goto-char (point-max))
16907           (or (bolp) (insert "\n"))
16908           (setq b (point))
16909           (insert-buffer-substring buffer)
16910           ;; Tag the beginning of the article with the ident.
16911           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
16912
16913 (defun gnus-backlog-remove-oldest-article ()
16914   (save-excursion
16915     (set-buffer (gnus-backlog-buffer))
16916     (goto-char (point-min))
16917     (if (zerop (buffer-size))
16918         () ; The buffer is empty.
16919       (let ((ident (get-text-property (point) 'gnus-backlog))
16920             buffer-read-only)
16921         ;; Remove the ident from the list of articles.
16922         (when ident
16923           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16924         ;; Delete the article itself.
16925         (delete-region
16926          (point) (next-single-property-change
16927                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16928
16929 (defun gnus-backlog-remove-article (group number)
16930   "Remove article NUMBER in GROUP from the backlog."
16931   (when (numberp number)
16932     (gnus-backlog-setup)
16933     (let ((ident (intern (concat group ":" (int-to-string number))
16934                          gnus-backlog-hashtb))
16935           beg end)
16936       (when (memq ident gnus-backlog-articles)
16937         ;; It was in the backlog.
16938         (save-excursion
16939           (set-buffer (gnus-backlog-buffer))
16940           (let (buffer-read-only)
16941             (when (setq beg (text-property-any
16942                              (point-min) (point-max) 'gnus-backlog
16943                              ident))
16944               ;; Find the end (i. e., the beginning of the next article).
16945               (setq end
16946                     (next-single-property-change
16947                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16948               (delete-region beg end)
16949               ;; Return success.
16950               t)))))))
16951
16952 (defun gnus-backlog-request-article (group number buffer)
16953   (when (numberp number)
16954     (gnus-backlog-setup)
16955     (let ((ident (intern (concat group ":" (int-to-string number))
16956                          gnus-backlog-hashtb))
16957           beg end)
16958       (when (memq ident gnus-backlog-articles)
16959         ;; It was in the backlog.
16960         (save-excursion
16961           (set-buffer (gnus-backlog-buffer))
16962           (if (not (setq beg (text-property-any
16963                               (point-min) (point-max) 'gnus-backlog
16964                               ident)))
16965               ;; It wasn't in the backlog after all.
16966               (ignore
16967                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16968             ;; Find the end (i. e., the beginning of the next article).
16969             (setq end
16970                   (next-single-property-change
16971                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16972         (let ((buffer-read-only nil))
16973           (erase-buffer)
16974           (insert-buffer-substring gnus-backlog-buffer beg end)
16975           t)))))
16976
16977 ;; Allow redefinition of Gnus functions.
16978
16979 (gnus-ems-redefine)
16980
16981 (provide 'gnus)
16982
16983 ;;; gnus.el ends here