*** 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
35 (eval-when-compile (require 'cl))
36
37 ;; Site dependent variables.  These variables should be defined in
38 ;; paths.el.
39
40 (defvar gnus-default-nntp-server nil
41   "Specify a default NNTP server.
42 This variable should be defined in paths.el, and should never be set
43 by the user.
44 If you want to change servers, you should use `gnus-select-method'.
45 See the documentation to that variable.")
46
47 (defvar gnus-backup-default-subscribed-newsgroups
48   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
49   "Default default new newsgroups the first time Gnus is run.
50 Should be set in paths.el, and shouldn't be touched by the user.")
51
52 (defvar gnus-local-domain nil
53   "Local domain name without a host name.
54 The DOMAINNAME environment variable is used instead if it is defined.
55 If the `system-name' function returns the full Internet name, there is
56 no need to set this variable.")
57
58 (defvar gnus-local-organization nil
59   "String with a description of what organization (if any) the user belongs to.
60 The ORGANIZATION environment variable is used instead if it is defined.
61 If this variable contains a function, this function will be called
62 with the current newsgroup name as the argument.  The function should
63 return a string.
64
65 In any case, if the string (either in the variable, in the environment
66 variable, or returned by the function) is a file name, the contents of
67 this file will be used as the organization.")
68
69 (defvar gnus-use-generic-from nil
70   "If nil, the full host name will be the system name prepended to the domain name.
71 If this is a string, the full host name will be this string.
72 If this is non-nil, non-string, the domain name will be used as the
73 full host name.")
74
75 (defvar gnus-use-generic-path nil
76   "If nil, use the NNTP server name in the Path header.
77 If stringp, use this; if non-nil, use no host name (user name only).")
78
79 ;; Customization variables
80
81 ;; Don't touch this variable.
82 (defvar gnus-nntp-service "nntp"
83   "*NNTP service name (\"nntp\" or 119).
84 This is an obsolete variable, which is scarcely used.  If you use an
85 nntp server for your newsgroup and want to change the port number
86 used to 899, you would say something along these lines:
87
88  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
89
90 (defvar gnus-nntpserver-file "/etc/nntpserver"
91   "*A file with only the name of the nntp server in it.")
92
93 ;; This function is used to check both the environment variable
94 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
95 ;; an nntp server name default.
96 (defun gnus-getenv-nntpserver ()
97   (or (getenv "NNTPSERVER")
98       (and (file-readable-p gnus-nntpserver-file)
99            (save-excursion
100              (set-buffer (get-buffer-create " *gnus nntp*"))
101              (buffer-disable-undo (current-buffer))
102              (insert-file-contents gnus-nntpserver-file)
103              (let ((name (buffer-string)))
104                (prog1
105                    (if (string-match "^[ \t\n]*$" name)
106                        nil
107                      name)
108                  (kill-buffer (current-buffer))))))))
109
110 (defvar gnus-select-method
111   (nconc
112    (list 'nntp (or (condition-case ()
113                        (gnus-getenv-nntpserver)
114                      (error nil))
115                    (if (and gnus-default-nntp-server
116                             (not (string= gnus-default-nntp-server "")))
117                        gnus-default-nntp-server)
118                    (system-name)))
119    (if (or (null gnus-nntp-service)
120            (equal gnus-nntp-service "nntp"))
121        nil
122      (list gnus-nntp-service)))
123   "*Default method for selecting a newsgroup.
124 This variable should be a list, where the first element is how the
125 news is to be fetched, the second is the address.
126
127 For instance, if you want to get your news via NNTP from
128 \"flab.flab.edu\", you could say:
129
130 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
131
132 If you want to use your local spool, say:
133
134 (setq gnus-select-method (list 'nnspool (system-name)))
135
136 If you use this variable, you must set `gnus-nntp-server' to nil.
137
138 There is a lot more to know about select methods and virtual servers -
139 see the manual for details.")
140
141 (defvar gnus-message-archive-method 
142   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
143              (nnfolder-active-file "~/Mail/archive/active")
144              (nnfolder-get-new-mail nil)
145              (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.")
148
149 (defvar gnus-refer-article-method nil
150   "*Preferred method for fetching an article by Message-ID.
151 If you are reading news from the local spool (with nnspool), fetching
152 articles by Message-ID is painfully slow.  By setting this method to an
153 nntp method, you might get acceptable results.
154
155 The value of this variable must be a valid select method as discussed
156 in the documentation of `gnus-select-method'.")
157
158 (defvar gnus-secondary-select-methods nil
159   "*A list of secondary methods that will be used for reading news.
160 This is a list where each element is a complete select method (see
161 `gnus-select-method').
162
163 If, for instance, you want to read your mail with the nnml backend,
164 you could set this variable:
165
166 (setq gnus-secondary-select-methods '((nnml \"\")))")
167
168 (defvar gnus-secondary-servers nil
169   "*List of NNTP servers that the user can choose between interactively.
170 To make Gnus query you for a server, you have to give `gnus' a
171 non-numeric prefix - `C-u M-x gnus', in short.")
172
173 (defvar gnus-nntp-server nil
174   "*The name of the host running the NNTP server.
175 This variable is semi-obsolete.  Use the `gnus-select-method'
176 variable instead.")
177
178 (defvar gnus-startup-file "~/.newsrc"
179   "*Your `.newsrc' file.
180 `.newsrc-SERVER' will be used instead if that exists.")
181
182 (defvar gnus-init-file "~/.gnus"
183   "*Your Gnus elisp startup file.
184 If a file with the .el or .elc suffixes exist, it will be read
185 instead.")
186
187 (defvar gnus-group-faq-directory
188   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
189     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
190     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
191     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
192     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
193     "/ftp@ftp.sunet.se:/pub/usenet/"
194     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
195     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
196     "/ftp@ftp.hk.super.net:/mirror/faqs/")
197   "*Directory where the group FAQs are stored.
198 This will most commonly be on a remote machine, and the file will be
199 fetched by ange-ftp.
200
201 This variable can also be a list of directories.  In that case, the
202 first element in the list will be used by default, and the others will
203 be used as backup sites.
204
205 Note that Gnus uses an aol machine as the default directory.  If this
206 feels fundamentally unclean, just think of it as a way to finally get
207 something of value back from them.
208
209 If the default site is too slow, try one of these:
210
211    North America: mirrors.aol.com                /pub/rtfm/usenet
212                   ftp.seas.gwu.edu               /pub/rtfm
213                   rtfm.mit.edu                   /pub/usenet/news.answers
214    Europe:        ftp.uni-paderborn.de           /pub/FAQ
215                   src.doc.ic.ac.uk               /usenet/news-FAQS
216                   ftp.sunet.se                   /pub/usenet
217    Asia:          nctuccca.edu.tw                /USENET/FAQ
218                   hwarang.postech.ac.kr          /pub/usenet/news.answers
219                   ftp.hk.super.net               /mirror/faqs")
220
221 (defvar gnus-group-archive-directory
222   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
223   "*The address of the (ding) archives.")
224
225 (defvar gnus-group-recent-archive-directory
226   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
227   "*The address of the most recent (ding) articles.")
228
229 (defvar gnus-default-subscribed-newsgroups nil
230   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
231 It should be a list of strings.
232 If it is `t', Gnus will not do anything special the first time it is
233 started; it'll just use the normal newsgroups subscription methods.")
234
235 (defvar gnus-use-cross-reference t
236   "*Non-nil means that cross referenced articles will be marked as read.
237 If nil, ignore cross references.  If t, mark articles as read in
238 subscribed newsgroups.  If neither t nor nil, mark as read in all
239 newsgroups.")
240
241 (defvar gnus-single-article-buffer t
242   "*If non-nil, display all articles in the same buffer.
243 If nil, each group will get its own article buffer.")
244
245 (defvar gnus-use-dribble-file t
246   "*Non-nil means that Gnus will use a dribble file to store user updates.
247 If Emacs should crash without saving the .newsrc files, complete
248 information can be restored from the dribble file.")
249
250 (defvar gnus-dribble-directory nil
251   "*The directory where dribble files will be saved.
252 If this variable is nil, the directory where the .newsrc files are
253 saved will be used.")
254
255 (defvar gnus-asynchronous nil
256   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
257
258 (defvar gnus-kill-summary-on-exit t
259   "*If non-nil, kill the summary buffer when you exit from it.
260 If nil, the summary will become a \"*Dead Summary*\" buffer, and
261 it will be killed sometime later.")
262
263 (defvar gnus-large-newsgroup 200
264   "*The number of articles which indicates a large newsgroup.
265 If the number of articles in a newsgroup is greater than this value,
266 confirmation is required for selecting the newsgroup.")
267
268 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
269 (defvar gnus-no-groups-message "No news is horrible news"
270   "*Message displayed by Gnus when no groups are available.")
271
272 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
273   "*Non-nil means that the default name of a file to save articles in is the group name.
274 If it's nil, the directory form of the group name is used instead.
275
276 If this variable is a list, and the list contains the element
277 `not-score', long file names will not be used for score files; if it
278 contains the element `not-save', long file names will not be used for
279 saving; and if it contains the element `not-kill', long file names
280 will not be used for kill files.")
281
282 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
283   "*Name of the directory articles will be saved in (default \"~/News\").
284 Initialized from the SAVEDIR environment variable.")
285
286 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
287   "*Name of the directory where kill files will be stored (default \"~/News\").
288 Initialized from the SAVEDIR environment variable.")
289
290 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
291   "*A function to save articles in your favorite format.
292 The function must be interactively callable (in other words, it must
293 be an Emacs command).
294
295 Gnus provides the following functions:
296
297 * gnus-summary-save-in-rmail (Rmail format)
298 * gnus-summary-save-in-mail (Unix mail format)
299 * gnus-summary-save-in-folder (MH folder)
300 * gnus-summary-save-in-file (article format).
301 * gnus-summary-save-in-vm (use VM's folder format).")
302
303 (defvar gnus-prompt-before-saving 'always
304   "*This variable says how much prompting is to be done when saving articles.
305 If it is nil, no prompting will be done, and the articles will be
306 saved to the default files.  If this variable is `always', each and
307 every article that is saved will be preceded by a prompt, even when
308 saving large batches of articles.  If this variable is neither nil not
309 `always', there the user will be prompted once for a file name for
310 each invocation of the saving commands.")
311
312 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
313   "*A function generating a file name to save articles in Rmail format.
314 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
315
316 (defvar gnus-mail-save-name (function gnus-plain-save-name)
317   "*A function generating a file name to save articles in Unix mail format.
318 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
319
320 (defvar gnus-folder-save-name (function gnus-folder-save-name)
321   "*A function generating a file name to save articles in MH folder.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
323
324 (defvar gnus-file-save-name (function gnus-numeric-save-name)
325   "*A function generating a file name to save articles in article format.
326 The function is called with NEWSGROUP, HEADERS, and optional
327 LAST-FILE.")
328
329 (defvar gnus-split-methods
330   '((gnus-article-archive-name))
331   "*Variable used to suggest where articles are to be saved.
332 For instance, if you would like to save articles related to Gnus in
333 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
334 you could set this variable to something like:
335
336  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
337    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
338
339 This variable is an alist where the where the key is the match and the
340 value is a list of possible files to save in if the match is non-nil.
341
342 If the match is a string, it is used as a regexp match on the
343 article.  If the match is a symbol, that symbol will be funcalled
344 from the buffer of the article to be saved with the newsgroup as the
345 parameter.  If it is a list, it will be evaled in the same buffer.
346
347 If this form or function returns a string, this string will be used as
348 a possible file name; and if it returns a non-nil list, that list will
349 be used as possible file names.")
350
351 (defvar gnus-move-split-methods nil
352   "*Variable used to suggest where articles are to be moved to.
353 It uses the same syntax as the `gnus-split-methods' variable.")
354
355 (defvar gnus-save-score nil
356   "*If non-nil, save group scoring info.")
357
358 (defvar gnus-use-adaptive-scoring nil
359   "*If non-nil, use some adaptive scoring scheme.")
360
361 (defvar gnus-use-cache nil
362   "*If nil, Gnus will ignore the article cache.
363 If `passive', it will allow entering (and reading) articles
364 explicitly entered into the cache.  If anything else, use the
365 cache to the full extent of the law.")
366
367 (defvar gnus-use-trees nil
368   "*If non-nil, display a thread tree buffer.")
369
370 (defvar gnus-use-grouplens nil
371   "*If non-nil, use GroupLens ratings.")
372
373 (defvar gnus-keep-backlog nil
374   "*If non-nil, Gnus will keep read articles for later re-retrieval.
375 If it is a number N, then Gnus will only keep the last N articles
376 read.  If it is neither nil nor a number, Gnus will keep all read
377 articles.  This is not a good idea.")
378
379 (defvar gnus-use-nocem nil
380   "*If non-nil, Gnus will read NoCeM cancel messages.")
381
382 (defvar gnus-use-demon nil
383   "If non-nil, Gnus might use some demons.")
384
385 (defvar gnus-use-scoring t
386   "*If non-nil, enable scoring.")
387
388 (defvar gnus-use-picons nil
389   "*If non-nil, display picons.")
390
391 (defvar gnus-fetch-old-headers nil
392   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
393 If an unread article in the group refers to an older, already read (or
394 just marked as read) article, the old article will not normally be
395 displayed in the Summary buffer.  If this variable is non-nil, Gnus
396 will attempt to grab the headers to the old articles, and thereby
397 build complete threads.  If it has the value `some', only enough
398 headers to connect otherwise loose threads will be displayed.
399 This variable can also be a number.  In that case, no more than that
400 number of old headers will be fetched.
401
402 The server has to support NOV for any of this to work.")
403
404 ;see gnus-cus.el
405 ;(defvar gnus-visual t
406 ;  "*If non-nil, will do various highlighting.
407 ;If nil, no mouse highlights (or any other highlights) will be
408 ;performed.  This might speed up Gnus some when generating large group
409 ;and summary buffers.")
410
411 (defvar gnus-novice-user t
412   "*Non-nil means that you are a usenet novice.
413 If non-nil, verbose messages may be displayed and confirmations may be
414 required.")
415
416 (defvar gnus-expert-user nil
417   "*Non-nil means that you will never be asked for confirmation about anything.
418 And that means *anything*.")
419
420 (defvar gnus-verbose 7
421   "*Integer that says how verbose Gnus should be.
422 The higher the number, the more messages Gnus will flash to say what
423 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
424 display most important messages; and at ten, Gnus will keep on
425 jabbering all the time.")
426
427 (defvar gnus-keep-same-level nil
428   "*Non-nil means that the next newsgroup after the current will be on the same level.
429 When you type, for instance, `n' after reading the last article in the
430 current newsgroup, you will go to the next newsgroup.  If this variable
431 is nil, the next newsgroup will be the next from the group
432 buffer.
433 If this variable is non-nil, Gnus will either put you in the
434 next newsgroup with the same level, or, if no such newsgroup is
435 available, the next newsgroup with the lowest possible level higher
436 than the current level.
437 If this variable is `best', Gnus will make the next newsgroup the one
438 with the best level.")
439
440 (defvar gnus-summary-make-false-root 'adopt
441   "*nil means that Gnus won't gather loose threads.
442 If the root of a thread has expired or been read in a previous
443 session, the information necessary to build a complete thread has been
444 lost.  Instead of having many small sub-threads from this original thread
445 scattered all over the summary buffer, Gnus can gather them.
446
447 If non-nil, Gnus will try to gather all loose sub-threads from an
448 original thread into one large thread.
449
450 If this variable is non-nil, it should be one of `none', `adopt',
451 `dummy' or `empty'.
452
453 If this variable is `none', Gnus will not make a false root, but just
454 present the sub-threads after another.
455 If this variable is `dummy', Gnus will create a dummy root that will
456 have all the sub-threads as children.
457 If this variable is `adopt', Gnus will make one of the \"children\"
458 the parent and mark all the step-children as such.
459 If this variable is `empty', the \"children\" are printed with empty
460 subject fields.  (Or rather, they will be printed with a string
461 given by the `gnus-summary-same-subject' variable.)")
462
463 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
464   "*A regexp to match subjects to be excluded from loose thread gathering.
465 As loose thread gathering is done on subjects only, that means that
466 there can be many false gatherings performed.  By rooting out certain
467 common subjects, gathering might become saner.")
468
469 (defvar gnus-summary-gather-subject-limit nil
470   "*Maximum length of subject comparisons when gathering loose threads.
471 Use nil to compare full subjects.  Setting this variable to a low
472 number will help gather threads that have been corrupted by
473 newsreaders chopping off subject lines, but it might also mean that
474 unrelated articles that have subject that happen to begin with the
475 same few characters will be incorrectly gathered.
476
477 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
478 comparing subjects.")
479
480 (defvar gnus-simplify-ignored-prefixes nil
481   "*Regexp, matches for which are removed from subject lines when simplifying.")
482
483 (defvar gnus-build-sparse-threads nil
484   "*If non-nil, fill in the gaps in threads.
485 If `some', only fill in the gaps that are needed to tie loose threads
486 together.  If `more', fill in all leaf nodes that Gnus can find.  If
487 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
488
489 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
490   "Function used for gathering loose threads.
491 There are two pre-defined functions: `gnus-gather-threads-by-subject',
492 which only takes Subjects into consideration; and
493 `gnus-gather-threads-by-references', which compared the References
494 headers of the articles to find matches.")
495
496 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
497 (defvar gnus-summary-same-subject ""
498   "*String indicating that the current article has the same subject as the previous.
499 This variable will only be used if the value of
500 `gnus-summary-make-false-root' is `empty'.")
501
502 (defvar gnus-summary-goto-unread t
503   "*If non-nil, marking commands will go to the next unread article.
504 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
505 whether it is read or not.")
506
507 (defvar gnus-group-goto-unread t
508   "*If non-nil, movement commands will go to the next unread and subscribed group.")
509
510 (defvar gnus-goto-next-group-when-activating t
511   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
512
513 (defvar gnus-check-new-newsgroups t
514   "*Non-nil means that Gnus will add new newsgroups at startup.
515 If this variable is `ask-server', Gnus will ask the server for new
516 groups since the last time it checked.  This means that the killed list
517 is no longer necessary, so you could set `gnus-save-killed-list' to
518 nil.
519
520 A variant is to have this variable be a list of select methods.  Gnus
521 will then use the `ask-server' method on all these select methods to
522 query for new groups from all those servers.
523
524 Eg.
525   (setq gnus-check-new-newsgroups
526         '((nntp \"some.server\") (nntp \"other.server\")))
527
528 If this variable is nil, then you have to tell Gnus explicitly to
529 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
530
531 (defvar gnus-check-bogus-newsgroups nil
532   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
533 If this variable is nil, then you have to tell Gnus explicitly to
534 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
535
536 (defvar gnus-read-active-file t
537   "*Non-nil means that Gnus will read the entire active file at startup.
538 If this variable is nil, Gnus will only know about the groups in your
539 `.newsrc' file.
540
541 If this variable is `some', Gnus will try to only read the relevant
542 parts of the active file from the server.  Not all servers support
543 this, and it might be quite slow with other servers, but this should
544 generally be faster than both the t and nil value.
545
546 If you set this variable to nil or `some', you probably still want to
547 be told about new newsgroups that arrive.  To do that, set
548 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
549 properly with all servers.")
550
551 (defvar gnus-level-subscribed 5
552   "*Groups with levels less than or equal to this variable are subscribed.")
553
554 (defvar gnus-level-unsubscribed 7
555   "*Groups with levels less than or equal to this variable are unsubscribed.
556 Groups with levels less than `gnus-level-subscribed', which should be
557 less than this variable, are subscribed.")
558
559 (defvar gnus-level-zombie 8
560   "*Groups with this level are zombie groups.")
561
562 (defvar gnus-level-killed 9
563   "*Groups with this level are killed.")
564
565 (defvar gnus-level-default-subscribed 3
566   "*New subscribed groups will be subscribed at this level.")
567
568 (defvar gnus-level-default-unsubscribed 6
569   "*New unsubscribed groups will be unsubscribed at this level.")
570
571 (defvar gnus-activate-level (1+ gnus-level-subscribed)
572   "*Groups higher than this level won't be activated on startup.
573 Setting this variable to something log might save lots of time when
574 you have many groups that you aren't interested in.")
575
576 (defvar gnus-activate-foreign-newsgroups 4
577   "*If nil, Gnus will not check foreign newsgroups at startup.
578 If it is non-nil, it should be a number between one and nine.  Foreign
579 newsgroups that have a level lower or equal to this number will be
580 activated on startup.  For instance, if you want to active all
581 subscribed newsgroups, but not the rest, you'd set this variable to
582 `gnus-level-subscribed'.
583
584 If you subscribe to lots of newsgroups from different servers, startup
585 might take a while.  By setting this variable to nil, you'll save time,
586 but you won't be told how many unread articles there are in the
587 groups.")
588
589 (defvar gnus-save-newsrc-file t
590   "*Non-nil means that Gnus will save the `.newsrc' file.
591 Gnus always saves its own startup file, which is called
592 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
593 be readily understood by other newsreaders.  If you don't plan on
594 using other newsreaders, set this variable to nil to save some time on
595 exit.")
596
597 (defvar gnus-save-killed-list t
598   "*If non-nil, save the list of killed groups to the startup file.
599 If you set this variable to nil, you'll save both time (when starting
600 and quitting) and space (both memory and disk), but it will also mean
601 that Gnus has no record of which groups are new and which are old, so
602 the automatic new newsgroups subscription methods become meaningless.
603
604 You should always set `gnus-check-new-newsgroups' to `ask-server' or
605 nil if you set this variable to nil.")
606
607 (defvar gnus-interactive-catchup t
608   "*If non-nil, require your confirmation when catching up a group.")
609
610 (defvar gnus-interactive-post t
611   "*If non-nil, group name will be asked for when posting.")
612
613 (defvar gnus-interactive-exit t
614   "*If non-nil, require your confirmation when exiting Gnus.")
615
616 (defvar gnus-kill-killed t
617   "*If non-nil, Gnus will apply kill files to already killed articles.
618 If it is nil, Gnus will never apply kill files to articles that have
619 already been through the scoring process, which might very well save lots
620 of time.")
621
622 (defvar gnus-extract-address-components 'gnus-extract-address-components
623   "*Function for extracting address components from a From header.
624 Two pre-defined function exist: `gnus-extract-address-components',
625 which is the default, quite fast, and too simplistic solution, and
626 `mail-extract-address-components', which works much better, but is
627 slower.")
628
629 (defvar gnus-summary-default-score 0
630   "*Default article score level.
631 If this variable is nil, scoring will be disabled.")
632
633 (defvar gnus-summary-zcore-fuzz 0
634   "*Fuzziness factor for the zcore in the summary buffer.
635 Articles with scores closer than this to `gnus-summary-default-score'
636 will not be marked.")
637
638 (defvar gnus-simplify-subject-fuzzy-regexp nil
639   "*Strings to be removed when doing fuzzy matches.
640 This can either be a regular expression or list of regular expressions
641 that will be removed from subject strings if fuzzy subject
642 simplification is selected.")
643
644 (defvar gnus-permanently-visible-groups nil
645   "*Regexp to match groups that should always be listed in the group buffer.
646 This means that they will still be listed when there are no unread
647 articles in the groups.")
648
649 (defvar gnus-list-groups-with-ticked-articles t
650   "*If non-nil, list groups that have only ticked articles.
651 If nil, only list groups that have unread articles.")
652
653 (defvar gnus-group-default-list-level gnus-level-subscribed
654   "*Default listing level.
655 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
656
657 (defvar gnus-group-use-permanent-levels nil
658   "*If non-nil, once you set a level, Gnus will use this level.")
659
660 (defvar gnus-group-list-inactive-groups t
661   "*If non-nil, inactive groups will be listed.")
662
663 (defvar gnus-show-mime nil
664   "*If non-nil, do mime processing of articles.
665 The articles will simply be fed to the function given by
666 `gnus-show-mime-method'.")
667
668 (defvar gnus-strict-mime t
669   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
670
671 (defvar gnus-show-mime-method 'metamail-buffer
672   "*Function to process a MIME message.
673 The function is called from the article buffer.")
674
675 (defvar gnus-decode-encoded-word-method (lambda ())
676   "*Function to decode a MIME encoded-words.
677 The function is called from the article buffer.")
678
679 (defvar gnus-show-threads t
680   "*If non-nil, display threads in summary mode.")
681
682 (defvar gnus-thread-hide-subtree nil
683   "*If non-nil, hide all threads initially.
684 If threads are hidden, you have to run the command
685 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
686 to expose hidden threads.")
687
688 (defvar gnus-thread-hide-killed t
689   "*If non-nil, hide killed threads automatically.")
690
691 (defvar gnus-thread-ignore-subject nil
692   "*If non-nil, ignore subjects and do all threading based on the Reference header.
693 If nil, which is the default, articles that have different subjects
694 from their parents will start separate threads.")
695
696 (defvar gnus-thread-operation-ignore-subject t
697   "*If non-nil, subjects will be ignored when doing thread commands.
698 This affects commands like `gnus-summary-kill-thread' and
699 `gnus-summary-lower-thread'.
700
701 If this variable is nil, articles in the same thread with different
702 subjects will not be included in the operation in question.  If this
703 variable is `fuzzy', only articles that have subjects that are fuzzily
704 equal will be included.")
705
706 (defvar gnus-thread-indent-level 4
707   "*Number that says how much each sub-thread should be indented.")
708
709 (defvar gnus-ignored-newsgroups
710   (purecopy (mapconcat 'identity
711                        '("^to\\."       ; not "real" groups
712                          "^[0-9. \t]+ " ; all digits in name
713                          "[][\"#'()]"   ; bogus characters
714                          )
715                        "\\|"))
716   "*A regexp to match uninteresting newsgroups in the active file.
717 Any lines in the active file matching this regular expression are
718 removed from the newsgroup list before anything else is done to it,
719 thus making them effectively non-existent.")
720
721 (defvar gnus-ignored-headers
722   "^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:"
723   "*All headers that match this regexp will be hidden.
724 This variable can also be a list of regexps of headers to be ignored.
725 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
726
727 (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-"
728   "*All headers that do not match this regexp will be hidden.
729 This variable can also be a list of regexp of headers to remain visible.
730 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
731
732 (defvar gnus-sorted-header-list
733   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
734     "^Cc:" "^Date:" "^Organization:")
735   "*This variable is a list of regular expressions.
736 If it is non-nil, headers that match the regular expressions will
737 be placed first in the article buffer in the sequence specified by
738 this list.")
739
740 (defvar gnus-boring-article-headers
741   '(empty followup-to reply-to)
742   "*Headers that are only to be displayed if they have interesting data.
743 Possible values in this list are `empty', `newsgroups', `followup-to',
744 `reply-to', and `date'.")
745
746 (defvar gnus-show-all-headers nil
747   "*If non-nil, don't hide any headers.")
748
749 (defvar gnus-save-all-headers t
750   "*If non-nil, don't remove any headers before saving.")
751
752 (defvar gnus-saved-headers gnus-visible-headers
753   "*Headers to keep if `gnus-save-all-headers' is nil.
754 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
755 If that variable is nil, however, all headers that match this regexp
756 will be kept while the rest will be deleted before saving.")
757
758 (defvar gnus-inhibit-startup-message nil
759   "*If non-nil, the startup message will not be displayed.")
760
761 (defvar gnus-signature-separator "^-- *$"
762   "Regexp matching signature separator.")
763
764 (defvar gnus-signature-limit nil
765   "Provide a limit to what is considered a signature.
766 If it is a number, no signature may not be longer (in characters) than
767 that number.  If it is a function, the function will be called without
768 any parameters, and if it returns nil, there is no signature in the
769 buffer.  If it is a string, it will be used as a regexp.  If it
770 matches, the text in question is not a signature.")
771
772 (defvar gnus-auto-extend-newsgroup t
773   "*If non-nil, extend newsgroup forward and backward when requested.")
774
775 (defvar gnus-auto-select-first t
776   "*If nil, don't select the first unread article when entering a group.
777 If this variable is `best', select the highest-scored unread article
778 in the group.  If neither nil nor `best', select the first unread
779 article.
780
781 If you want to prevent automatic selection of the first unread article
782 in some newsgroups, set the variable to nil in
783 `gnus-select-group-hook'.")
784
785 (defvar gnus-auto-select-next t
786   "*If non-nil, offer to go to the next group from the end of the previous.
787 If the value is t and the next newsgroup is empty, Gnus will exit
788 summary mode and go back to group mode.  If the value is neither nil
789 nor t, Gnus will select the following unread newsgroup.  In
790 particular, if the value is the symbol `quietly', the next unread
791 newsgroup will be selected without any confirmation, and if it is
792 `almost-quietly', the next group will be selected without any
793 confirmation if you are located on the last article in the group.
794 Finally, if this variable is `slightly-quietly', the `Z n' command
795 will go to the next group without confirmation.")
796
797 (defvar gnus-auto-select-same nil
798   "*If non-nil, select the next article with the same subject.")
799
800 (defvar gnus-summary-check-current nil
801   "*If non-nil, consider the current article when moving.
802 The \"unread\" movement commands will stay on the same line if the
803 current article is unread.")
804
805 (defvar gnus-auto-center-summary t
806   "*If non-nil, always center the current summary buffer.
807 In particular, if `vertical' do only vertical recentering.  If non-nil
808 and non-`vertical', do both horizontal and vertical recentering.")
809
810 (defvar gnus-break-pages t
811   "*If non-nil, do page breaking on articles.
812 The page delimiter is specified by the `gnus-page-delimiter'
813 variable.")
814
815 (defvar gnus-page-delimiter "^\^L"
816   "*Regexp describing what to use as article page delimiters.
817 The default value is \"^\^L\", which is a form linefeed at the
818 beginning of a line.")
819
820 (defvar gnus-use-full-window t
821   "*If non-nil, use the entire Emacs screen.")
822
823 (defvar gnus-window-configuration nil
824   "Obsolete variable.  See `gnus-buffer-configuration'.")
825
826 (defvar gnus-window-min-width 2
827   "*Minimum width of Gnus buffers.")
828
829 (defvar gnus-window-min-height 1
830   "*Minimum height of Gnus buffers.")
831
832 (defvar gnus-buffer-configuration
833   '((group
834      (vertical 1.0
835                (group 1.0 point)
836                (if gnus-carpal '(group-carpal 4))))
837     (summary
838      (vertical 1.0
839                (summary 1.0 point)
840                (if gnus-carpal '(summary-carpal 4))))
841     (article
842      (cond 
843       (gnus-use-picons
844        '(frame 1.0
845                (vertical 1.0
846                          (summary 0.25 point)
847                          (if gnus-carpal '(summary-carpal 4))
848                          (article 1.0))
849                (vertical ((height . 5) (width . 15)
850                           (user-position . t)
851                           (left . -1) (top . 1))
852                          (picons 1.0))))
853       (gnus-use-trees
854        '(vertical 1.0
855                   (summary 0.25 point)
856                   (tree 0.25)
857                   (article 1.0)))
858       (t
859        '(vertical 1.0
860                  (summary 0.25 point)
861                  (if gnus-carpal '(summary-carpal 4))
862                  (if gnus-use-trees '(tree 0.25))
863                  (article 1.0)))))
864     (server
865      (vertical 1.0
866                (server 1.0 point)
867                (if gnus-carpal '(server-carpal 2))))
868     (browse
869      (vertical 1.0
870                (browse 1.0 point)
871                (if gnus-carpal '(browse-carpal 2))))
872     (group-mail
873      (vertical 1.0
874                (mail 1.0 point)))
875     (group-post
876      (vertical 1.0
877                (post 1.0 point)))
878     (summary-mail
879      (vertical 1.0
880                (mail 1.0 point)))
881     (summary-reply
882      (vertical 1.0
883                (article-copy 0.5)
884                (mail 1.0 point)))
885     (pick
886      (vertical 1.0
887                (article 1.0 point)))
888     (info
889      (vertical 1.0
890                (info 1.0 point)))
891     (summary-faq
892      (vertical 1.0
893                (summary 0.25)
894                (faq 1.0 point)))
895     (edit-group
896      (vertical 1.0
897                (group 0.5)
898                (edit-group 1.0 point)))
899     (edit-server
900      (vertical 1.0
901                (server 0.5)
902                (edit-server 1.0 point)))
903     (edit-score
904      (vertical 1.0
905                (summary 0.25)
906                (edit-score 1.0 point)))
907     (post
908      (vertical 1.0
909                (post 1.0 point)))
910     (reply
911      (vertical 1.0
912                (article-copy 0.5)
913                (mail 1.0 point)))
914     (mail-forward
915      (vertical 1.0
916                (mail 1.0 point)))
917     (post-forward
918      (vertical 1.0
919                (post 1.0 point)))
920     (reply-yank
921      (vertical 1.0
922                (mail 1.0 point)))
923     (mail-bounce
924      (vertical 1.0
925                (article 0.5)
926                (mail 1.0 point)))
927     (draft
928      (vertical 1.0
929                (draft 1.0 point)))
930     (pipe
931      (vertical 1.0
932                (summary 0.25 point)
933                (if gnus-carpal '(summary-carpal 4))
934                ("*Shell Command Output*" 1.0)))
935     (followup
936      (vertical 1.0
937                (article-copy 0.5)
938                (post 1.0 point)))
939     (followup-yank
940      (vertical 1.0
941                (post 1.0 point))))
942   "Window configuration for all possible Gnus buffers.
943 This variable is a list of lists.  Each of these lists has a NAME and
944 a RULE.  The NAMEs are commonsense names like `group', which names a
945 rule used when displaying the group buffer; `summary', which names a
946 rule for what happens when you enter a group and do not display an
947 article buffer; and so on.  See the value of this variable for a
948 complete list of NAMEs.
949
950 Each RULE is a list of vectors.  The first element in this vector is
951 the name of the buffer to be displayed; the second element is the
952 percentage of the screen this buffer is to occupy (a number in the
953 0.0-0.99 range); the optional third element is `point', which should
954 be present to denote which buffer point is to go to after making this
955 buffer configuration.")
956
957 (defvar gnus-window-to-buffer
958   '((group . gnus-group-buffer)
959     (summary . gnus-summary-buffer)
960     (article . gnus-article-buffer)
961     (server . gnus-server-buffer)
962     (browse . "*Gnus Browse Server*")
963     (edit-group . gnus-group-edit-buffer)
964     (edit-server . gnus-server-edit-buffer)
965     (group-carpal . gnus-carpal-group-buffer)
966     (summary-carpal . gnus-carpal-summary-buffer)
967     (server-carpal . gnus-carpal-server-buffer)
968     (browse-carpal . gnus-carpal-browse-buffer)
969     (edit-score . gnus-score-edit-buffer)
970     (mail . gnus-mail-buffer)
971     (post . gnus-post-news-buffer)
972     (faq . gnus-faq-buffer)
973     (picons . "*Picons*")
974     (tree . gnus-tree-buffer)
975     (info . gnus-info-buffer)
976     (article-copy . gnus-article-copy)
977     (draft . gnus-draft-buffer))
978   "Mapping from short symbols to buffer names or buffer variables.")
979
980 (defvar gnus-carpal nil
981   "*If non-nil, display clickable icons.")
982
983 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
984   "*Function called with a group name when new group is detected.
985 A few pre-made functions are supplied: `gnus-subscribe-randomly'
986 inserts new groups at the beginning of the list of groups;
987 `gnus-subscribe-alphabetically' inserts new groups in strict
988 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
989 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
990 for your decision; `gnus-subscribe-killed' kills all new groups.")
991
992 ;; Suggested by a bug report by Hallvard B Furuseth.
993 ;; <h.b.furuseth@usit.uio.no>.
994 (defvar gnus-subscribe-options-newsgroup-method
995   (function gnus-subscribe-alphabetically)
996   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
997 If, for instance, you want to subscribe to all newsgroups in the
998 \"no\" and \"alt\" hierarchies, you'd put the following in your
999 .newsrc file:
1000
1001 options -n no.all alt.all
1002
1003 Gnus will the subscribe all new newsgroups in these hierarchies with
1004 the subscription method in this variable.")
1005
1006 (defvar gnus-subscribe-hierarchical-interactive nil
1007   "*If non-nil, Gnus will offer to subscribe hierarchically.
1008 When a new hierarchy appears, Gnus will ask the user:
1009
1010 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1011
1012 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1013 subscribe to all newsgroups in the hierarchy and `s' will skip this
1014 hierarchy in its entirety.")
1015
1016 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1017   "*Function used for sorting the group buffer.
1018 This function will be called with group info entries as the arguments
1019 for the groups to be sorted.  Pre-made functions include
1020 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1021 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1022 `gnus-group-sort-by-rank'.
1023
1024 This variable can also be a list of sorting functions.  In that case,
1025 the most significant sort function should be the last function in the
1026 list.")
1027
1028 ;; Mark variables suggested by Thomas Michanek
1029 ;; <Thomas.Michanek@telelogic.se>.
1030 (defvar gnus-unread-mark ? 
1031   "*Mark used for unread articles.")
1032 (defvar gnus-ticked-mark ?!
1033   "*Mark used for ticked articles.")
1034 (defvar gnus-dormant-mark ??
1035   "*Mark used for dormant articles.")
1036 (defvar gnus-del-mark ?r
1037   "*Mark used for del'd articles.")
1038 (defvar gnus-read-mark ?R
1039   "*Mark used for read articles.")
1040 (defvar gnus-expirable-mark ?E
1041   "*Mark used for expirable articles.")
1042 (defvar gnus-killed-mark ?K
1043   "*Mark used for killed articles.")
1044 (defvar gnus-souped-mark ?F
1045   "*Mark used for killed articles.")
1046 (defvar gnus-kill-file-mark ?X
1047   "*Mark used for articles killed by kill files.")
1048 (defvar gnus-low-score-mark ?Y
1049   "*Mark used for articles with a low score.")
1050 (defvar gnus-catchup-mark ?C
1051   "*Mark used for articles that are caught up.")
1052 (defvar gnus-replied-mark ?A
1053   "*Mark used for articles that have been replied to.")
1054 (defvar gnus-cached-mark ?*
1055   "*Mark used for articles that are in the cache.")
1056 (defvar gnus-saved-mark ?S
1057   "*Mark used for articles that have been saved to.")
1058 (defvar gnus-process-mark ?#
1059   "*Process mark.")
1060 (defvar gnus-ancient-mark ?O
1061   "*Mark used for ancient articles.")
1062 (defvar gnus-sparse-mark ?Q
1063   "*Mark used for sparsely reffed articles.")
1064 (defvar gnus-canceled-mark ?G
1065   "*Mark used for canceled articles.")
1066 (defvar gnus-score-over-mark ?+
1067   "*Score mark used for articles with high scores.")
1068 (defvar gnus-score-below-mark ?-
1069   "*Score mark used for articles with low scores.")
1070 (defvar gnus-empty-thread-mark ? 
1071   "*There is no thread under the article.")
1072 (defvar gnus-not-empty-thread-mark ?=
1073   "*There is a thread under the article.")
1074
1075 (defvar gnus-view-pseudo-asynchronously nil
1076   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1077
1078 (defvar gnus-view-pseudos nil
1079   "*If `automatic', pseudo-articles will be viewed automatically.
1080 If `not-confirm', pseudos will be viewed automatically, and the user
1081 will not be asked to confirm the command.")
1082
1083 (defvar gnus-view-pseudos-separately t
1084   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1085 If nil, all files that use the same viewing command will be given as a
1086 list of parameters to that command.")
1087
1088 (defvar gnus-insert-pseudo-articles t
1089   "*If non-nil, insert pseudo-articles when decoding articles.")
1090
1091 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1092   "*Format of group lines.
1093 It works along the same lines as a normal formatting string,
1094 with some simple extensions.
1095
1096 %M    Only marked articles (character, \"*\" or \" \")
1097 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1098 %L    Level of subscribedness (integer)
1099 %N    Number of unread articles (integer)
1100 %I    Number of dormant articles (integer)
1101 %i    Number of ticked and dormant (integer)
1102 %T    Number of ticked articles (integer)
1103 %R    Number of read articles (integer)
1104 %t    Total number of articles (integer)
1105 %y    Number of unread, unticked articles (integer)
1106 %G    Group name (string)
1107 %g    Qualified group name (string)
1108 %D    Group description (string)
1109 %s    Select method (string)
1110 %o    Moderated group (char, \"m\")
1111 %p    Process mark (char)
1112 %O    Moderated group (string, \"(m)\" or \"\")
1113 %P    Topic indentation (string)
1114 %l    Whether there are GroupLens predictions for this group (string)
1115 %n    Select from where (string)
1116 %z    A string that look like `<%s:%n>' if a foreign select method is used
1117 %u    User defined specifier.  The next character in the format string should
1118       be a letter.  Gnus will call the function gnus-user-format-function-X,
1119       where X is the letter following %u.  The function will be passed the
1120       current header as argument.  The function should return a string, which
1121       will be inserted into the buffer just like information from any other
1122       group specifier.
1123
1124 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1125 the mouse point move inside the area.  There can only be one such area.
1126
1127 Note that this format specification is not always respected.  For
1128 reasons of efficiency, when listing killed groups, this specification
1129 is ignored altogether.  If the spec is changed considerably, your
1130 output may end up looking strange when listing both alive and killed
1131 groups.
1132
1133 If you use %o or %O, reading the active file will be slower and quite
1134 a bit of extra memory will be used. %D will also worsen performance.
1135 Also note that if you change the format specification to include any
1136 of these specs, you must probably re-start Gnus to see them go into
1137 effect.")
1138
1139 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1140   "*The format specification of the lines in the summary buffer.
1141
1142 It works along the same lines as a normal formatting string,
1143 with some simple extensions.
1144
1145 %N   Article number, left padded with spaces (string)
1146 %S   Subject (string)
1147 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1148 %n   Name of the poster (string)
1149 %a   Extracted name of the poster (string)
1150 %A   Extracted address of the poster (string)
1151 %F   Contents of the From: header (string)
1152 %x   Contents of the Xref: header (string)
1153 %D   Date of the article (string)
1154 %d   Date of the article (string) in DD-MMM format
1155 %M   Message-id of the article (string)
1156 %r   References of the article (string)
1157 %c   Number of characters in the article (integer)
1158 %L   Number of lines in the article (integer)
1159 %I   Indentation based on thread level (a string of spaces)
1160 %T   A string with two possible values: 80 spaces if the article
1161      is on thread level two or larger and 0 spaces on level one
1162 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1163 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1164 %[   Opening bracket (character, \"[\" or \"<\")
1165 %]   Closing bracket (character, \"]\" or \">\")
1166 %>   Spaces of length thread-level (string)
1167 %<   Spaces of length (- 20 thread-level) (string)
1168 %i   Article score (number)
1169 %z   Article zcore (character)
1170 %t   Number of articles under the current thread (number).
1171 %e   Whether the thread is empty or not (character).
1172 %l   GroupLens score (number)
1173 %u   User defined specifier.  The next character in the format string should
1174      be a letter.  Gnus will call the function gnus-user-format-function-X,
1175      where X is the letter following %u.  The function will be passed the
1176      current header as argument.  The function should return a string, which
1177      will be inserted into the summary just like information from any other
1178      summary specifier.
1179
1180 Text between %( and %) will be highlighted with `gnus-mouse-face'
1181 when the mouse point is placed inside the area.  There can only be one
1182 such area.
1183
1184 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1185 with care.  For reasons of efficiency, Gnus will compute what column
1186 these characters will end up in, and \"hard-code\" that.  This means that
1187 it is illegal to have these specs after a variable-length spec.  Well,
1188 you might not be arrested, but your summary buffer will look strange,
1189 which is bad enough.
1190
1191 The smart choice is to have these specs as for to the left as
1192 possible.
1193
1194 This restriction may disappear in later versions of Gnus.")
1195
1196 (defvar gnus-summary-dummy-line-format
1197   "*  %(:                          :%) %S\n"
1198   "*The format specification for the dummy roots in the summary buffer.
1199 It works along the same lines as a normal formatting string,
1200 with some simple extensions.
1201
1202 %S  The subject")
1203
1204 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1205   "*The format specification for the summary mode line.
1206 It works along the same lines as a normal formatting string,
1207 with some simple extensions:
1208
1209 %G  Group name
1210 %p  Unprefixed group name
1211 %A  Current article number
1212 %V  Gnus version
1213 %U  Number of unread articles in the group
1214 %e  Number of unselected articles in the group
1215 %Z  A string with unread/unselected article counts
1216 %g  Shortish group name
1217 %S  Subject of the current article
1218 %u  User-defined spec
1219 %s  Current score file name
1220 %d  Number of dormant articles
1221 %r  Number of articles that have been marked as read in this session
1222 %E  Number of articles expunged by the score files")
1223
1224 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1225   "*The format specification for the article mode line.
1226 See `gnus-summary-mode-line-format' for a closer description.")
1227
1228 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1229   "*The format specification for the group mode line.
1230 It works along the same lines as a normal formatting string,
1231 with some simple extensions:
1232
1233 %S   The native news server.
1234 %M   The native select method.")
1235
1236 (defvar gnus-valid-select-methods
1237   '(("nntp" post address prompt-address)
1238     ("nnspool" post address)
1239     ("nnvirtual" post-mail virtual prompt-address)
1240     ("nnmbox" mail respool address)
1241     ("nnml" mail respool address)
1242     ("nnmh" mail respool address)
1243     ("nndir" post-mail prompt-address address)
1244     ("nneething" none address prompt-address)
1245     ("nndoc" none address prompt-address)
1246     ("nnbabyl" mail address respool)
1247     ("nnkiboze" post address virtual)
1248     ("nnsoup" post-mail address)
1249     ("nndraft" post-mail)
1250     ("nnfolder" mail respool address))
1251   "An alist of valid select methods.
1252 The first element of each list lists should be a string with the name
1253 of the select method.  The other elements may be be the category of
1254 this method (ie. `post', `mail', `none' or whatever) or other
1255 properties that this method has (like being respoolable).
1256 If you implement a new select method, all you should have to change is
1257 this variable.  I think.")
1258
1259 (defvar gnus-updated-mode-lines '(group article summary tree)
1260   "*List of buffers that should update their mode lines.
1261 The list may contain the symbols `group', `article' and `summary'.  If
1262 the corresponding symbol is present, Gnus will keep that mode line
1263 updated with information that may be pertinent.
1264 If this variable is nil, screen refresh may be quicker.")
1265
1266 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1267 (defvar gnus-mode-non-string-length nil
1268   "*Max length of mode-line non-string contents.
1269 If this is nil, Gnus will take space as is needed, leaving the rest
1270 of the modeline intact.")
1271
1272 ;see gnus-cus.el
1273 ;(defvar gnus-mouse-face 'highlight
1274 ;  "*Face used for mouse highlighting in Gnus.
1275 ;No mouse highlights will be done if `gnus-visual' is nil.")
1276
1277 (defvar gnus-summary-mark-below nil
1278   "*Mark all articles with a score below this variable as read.
1279 This variable is local to each summary buffer and usually set by the
1280 score file.")
1281
1282 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1283   "*List of functions used for sorting articles in the summary buffer.
1284 This variable is only used when not using a threaded display.")
1285
1286 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1287   "*List of functions used for sorting threads in the summary buffer.
1288 By default, threads are sorted by article number.
1289
1290 Each function takes two threads and return non-nil if the first thread
1291 should be sorted before the other.  If you use more than one function,
1292 the primary sort function should be the last.  You should probably
1293 always include `gnus-thread-sort-by-number' in the list of sorting
1294 functions -- preferably first.
1295
1296 Ready-mady functions include `gnus-thread-sort-by-number',
1297 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1298 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1299 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1300
1301 (defvar gnus-thread-score-function '+
1302   "*Function used for calculating the total score of a thread.
1303
1304 The function is called with the scores of the article and each
1305 subthread and should then return the score of the thread.
1306
1307 Some functions you can use are `+', `max', or `min'.")
1308
1309 (defvar gnus-summary-expunge-below nil
1310   "All articles that have a score less than this variable will be expunged.")
1311
1312 (defvar gnus-thread-expunge-below nil
1313   "All threads that have a total score less than this variable will be expunged.
1314 See `gnus-thread-score-function' for en explanation of what a
1315 \"thread score\" is.")
1316
1317 (defvar gnus-auto-subscribed-groups
1318   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1319   "*All new groups that match this regexp will be subscribed automatically.
1320 Note that this variable only deals with new groups.  It has no effect
1321 whatsoever on old groups.")
1322
1323 (defvar gnus-options-subscribe nil
1324   "*All new groups matching this regexp will be subscribed unconditionally.
1325 Note that this variable deals only with new newsgroups.  This variable
1326 does not affect old newsgroups.")
1327
1328 (defvar gnus-options-not-subscribe nil
1329   "*All new groups matching this regexp will be ignored.
1330 Note that this variable deals only with new newsgroups.  This variable
1331 does not affect old (already subscribed) newsgroups.")
1332
1333 (defvar gnus-auto-expirable-newsgroups nil
1334   "*Groups in which to automatically mark read articles as expirable.
1335 If non-nil, this should be a regexp that should match all groups in
1336 which to perform auto-expiry.  This only makes sense for mail groups.")
1337
1338 (defvar gnus-total-expirable-newsgroups nil
1339   "*Groups in which to perform expiry of all read articles.
1340 Use with extreme caution.  All groups that match this regexp will be
1341 expiring - which means that all read articles will be deleted after
1342 (say) one week.  (This only goes for mail groups and the like, of
1343 course.)")
1344
1345 (defvar gnus-group-uncollapsed-levels 1
1346   "Number of group name elements to leave alone when making a short group name.")
1347
1348 (defvar gnus-hidden-properties '(invisible t intangible t)
1349   "Property list to use for hiding text.")
1350
1351 (defvar gnus-modtime-botch nil
1352   "*Non-nil means .newsrc should be deleted prior to save.  
1353 Its use is due to the bogus appearance that .newsrc was modified on
1354 disc.")
1355
1356 ;; Hooks.
1357
1358 (defvar gnus-group-mode-hook nil
1359   "*A hook for Gnus group mode.")
1360
1361 (defvar gnus-summary-mode-hook nil
1362   "*A hook for Gnus summary mode.
1363 This hook is run before any variables are set in the summary buffer.")
1364
1365 (defvar gnus-article-mode-hook nil
1366   "*A hook for Gnus article mode.")
1367
1368 (defvar gnus-summary-prepare-exit-hook nil
1369   "*A hook called when preparing to exit from the summary buffer.
1370 It calls `gnus-summary-expire-articles' by default.")
1371 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1372
1373 (defvar gnus-summary-exit-hook nil
1374   "*A hook called on exit from the summary buffer.")
1375
1376 (defvar gnus-group-catchup-group-hook nil
1377   "*A hook run when catching up a group from the group buffer.")
1378
1379 (defvar gnus-open-server-hook nil
1380   "*A hook called just before opening connection to the news server.")
1381
1382 (defvar gnus-load-hook nil
1383   "*A hook run while Gnus is loaded.")
1384
1385 (defvar gnus-startup-hook nil
1386   "*A hook called at startup.
1387 This hook is called after Gnus is connected to the NNTP server.")
1388
1389 (defvar gnus-get-new-news-hook nil
1390   "*A hook run just before Gnus checks for new news.")
1391
1392 (defvar gnus-after-getting-new-news-hook nil
1393   "*A hook run after Gnus checks for new news.")
1394
1395 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1396   "*A function that is called to generate the group buffer.
1397 The function is called with three arguments: The first is a number;
1398 all group with a level less or equal to that number should be listed,
1399 if the second is non-nil, empty groups should also be displayed.  If
1400 the third is non-nil, it is a number.  No groups with a level lower
1401 than this number should be displayed.
1402
1403 The only current function implemented is `gnus-group-prepare-flat'.")
1404
1405 (defvar gnus-group-prepare-hook nil
1406   "*A hook called after the group buffer has been generated.
1407 If you want to modify the group buffer, you can use this hook.")
1408
1409 (defvar gnus-summary-prepare-hook nil
1410   "*A hook called after the summary buffer has been generated.
1411 If you want to modify the summary buffer, you can use this hook.")
1412
1413 (defvar gnus-summary-generate-hook nil
1414   "*A hook run just before generating the summary buffer.
1415 This hook is commonly used to customize threading variables and the
1416 like.")
1417
1418 (defvar gnus-article-prepare-hook nil
1419   "*A hook called after an article has been prepared in the article buffer.
1420 If you want to run a special decoding program like nkf, use this hook.")
1421
1422 ;(defvar gnus-article-display-hook nil
1423 ;  "*A hook called after the article is displayed in the article buffer.
1424 ;The hook is designed to change the contents of the article
1425 ;buffer.  Typical functions that this hook may contain are
1426 ;`gnus-article-hide-headers' (hide selected headers),
1427 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1428 ;`gnus-article-hide-signature' (hide signature) and
1429 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1432 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1433
1434 (defvar gnus-article-x-face-command
1435   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1436   "String or function to be executed to display an X-Face header.
1437 If it is a string, the command will be executed in a sub-shell
1438 asynchronously.  The compressed face will be piped to this command.")
1439
1440 (defvar gnus-article-x-face-too-ugly nil
1441   "Regexp matching posters whose face shouldn't be shown automatically.")
1442
1443 (defvar gnus-select-group-hook nil
1444   "*A hook called when a newsgroup is selected.
1445
1446 If you'd like to simplify subjects like the
1447 `gnus-summary-next-same-subject' command does, you can use the
1448 following hook:
1449
1450  (setq gnus-select-group-hook
1451       (list
1452         (lambda ()
1453           (mapcar (lambda (header)
1454                      (mail-header-set-subject
1455                       header
1456                       (gnus-simplify-subject
1457                        (mail-header-subject header) 're-only)))
1458                   gnus-newsgroup-headers))))")
1459
1460 (defvar gnus-select-article-hook nil
1461   "*A hook called when an article is selected.")
1462
1463 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1464   "*A hook called to apply kill files to a group.
1465 This hook is intended to apply a kill file to the selected newsgroup.
1466 The function `gnus-apply-kill-file' is called by default.
1467
1468 Since a general kill file is too heavy to use only for a few
1469 newsgroups, I recommend you to use a lighter hook function.  For
1470 example, if you'd like to apply a kill file to articles which contains
1471 a string `rmgroup' in subject in newsgroup `control', you can use the
1472 following hook:
1473
1474  (setq gnus-apply-kill-hook
1475       (list
1476         (lambda ()
1477           (cond ((string-match \"control\" gnus-newsgroup-name)
1478                  (gnus-kill \"Subject\" \"rmgroup\")
1479                  (gnus-expunge \"X\"))))))")
1480
1481 (defvar gnus-visual-mark-article-hook
1482   (list 'gnus-highlight-selected-summary)
1483   "*Hook run after selecting an article in the summary buffer.
1484 It is meant to be used for highlighting the article in some way.  It
1485 is not run if `gnus-visual' is nil.")
1486
1487 (defvar gnus-parse-headers-hook nil
1488   "*A hook called before parsing the headers.")
1489
1490 (defvar gnus-exit-group-hook nil
1491   "*A hook called when exiting (not quitting) summary mode.")
1492
1493 (defvar gnus-suspend-gnus-hook nil
1494   "*A hook called when suspending (not exiting) Gnus.")
1495
1496 (defvar gnus-exit-gnus-hook nil
1497   "*A hook called when exiting Gnus.")
1498
1499 (defvar gnus-after-exiting-gnus-hook nil
1500   "*A hook called after exiting Gnus.")
1501
1502 (defvar gnus-save-newsrc-hook nil
1503   "*A hook called before saving any of the newsrc files.")
1504
1505 (defvar gnus-save-quick-newsrc-hook nil
1506   "*A hook called just before saving the quick newsrc file.
1507 Can be used to turn version control on or off.")
1508
1509 (defvar gnus-save-standard-newsrc-hook nil
1510   "*A hook called just before saving the standard newsrc file.
1511 Can be used to turn version control on or off.")
1512
1513 (defvar gnus-summary-update-hook
1514   (list 'gnus-summary-highlight-line)
1515   "*A hook called when a summary line is changed.
1516 The hook will not be called if `gnus-visual' is nil.
1517
1518 The default function `gnus-summary-highlight-line' will
1519 highlight the line according to the `gnus-summary-highlight'
1520 variable.")
1521
1522 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1523   "*A hook called when a group line is changed.
1524 The hook will not be called if `gnus-visual' is nil.
1525
1526 The default function `gnus-group-highlight-line' will
1527 highlight the line according to the `gnus-group-highlight'
1528 variable.")
1529
1530 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1531   "*A hook called when an article is selected for the first time.
1532 The hook is intended to mark an article as read (or unread)
1533 automatically when it is selected.")
1534
1535 (defvar gnus-group-change-level-function nil
1536   "Function run when a group level is changed.
1537 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1538
1539 ;; Remove any hilit infestation.
1540 (add-hook 'gnus-startup-hook
1541           (lambda ()
1542             (remove-hook 'gnus-summary-prepare-hook
1543                          'hilit-rehighlight-buffer-quietly)
1544             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1545             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1546             (remove-hook 'gnus-article-prepare-hook
1547                          'hilit-rehighlight-buffer-quietly)))
1548
1549 \f
1550 ;; Internal variables
1551
1552 (defvar gnus-server-alist nil
1553   "List of available servers.")
1554
1555 (defvar gnus-group-indentation-function nil)
1556
1557 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1558
1559 (defvar gnus-goto-missing-group-function nil)
1560
1561 (defvar gnus-override-subscribe-method nil)
1562
1563 (defvar gnus-group-goto-next-group-function nil
1564   "Function to override finding the next group after listing groups.")
1565
1566 (defconst gnus-article-mark-lists
1567   '((marked . tick) (replied . reply)
1568     (expirable . expire) (killed . killed)
1569     (bookmarks . bookmark) (dormant . dormant)
1570     (scored . score) (saved . save)
1571     (cached . cache)
1572     ))
1573
1574 ;; Avoid highlighting in kill files.
1575 (defvar gnus-summary-inhibit-highlight nil)
1576 (defvar gnus-newsgroup-selected-overlay nil)
1577
1578 (defvar gnus-inhibit-hiding nil)
1579 (defvar gnus-group-indentation "")
1580 (defvar gnus-inhibit-limiting nil)
1581 (defvar gnus-created-frames nil)
1582
1583 (defvar gnus-article-mode-map nil)
1584 (defvar gnus-dribble-buffer nil)
1585 (defvar gnus-headers-retrieved-by nil)
1586 (defvar gnus-article-reply nil)
1587 (defvar gnus-override-method nil)
1588 (defvar gnus-article-check-size nil)
1589
1590 (defvar gnus-current-score-file nil)
1591 (defvar gnus-newsgroup-adaptive-score-file nil)
1592 (defvar gnus-scores-exclude-files nil)
1593
1594 (defvar gnus-opened-servers nil)
1595
1596 (defvar gnus-current-move-group nil)
1597
1598 (defvar gnus-newsgroup-dependencies nil)
1599 (defvar gnus-newsgroup-async nil)
1600 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1601
1602 (defvar gnus-newsgroup-adaptive nil)
1603
1604 (defvar gnus-summary-display-table nil)
1605 (defvar gnus-summary-display-article-function nil)
1606
1607 (defvar gnus-summary-highlight-line-function nil
1608   "Function called after highlighting a summary line.")
1609
1610 (defvar gnus-group-line-format-alist
1611   `((?M gnus-tmp-marked-mark ?c)
1612     (?S gnus-tmp-subscribed ?c)
1613     (?L gnus-tmp-level ?d)
1614     (?N (cond ((eq number t) "*" )
1615               ((numberp number) 
1616                (int-to-string
1617                 (+ number
1618                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1619                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1620               (t number)) ?s)
1621     (?R gnus-tmp-number-of-read ?s)
1622     (?t gnus-tmp-number-total ?d)
1623     (?y gnus-tmp-number-of-unread ?s)
1624     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1625     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1626     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1627            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1628     (?g gnus-tmp-group ?s)
1629     (?G gnus-tmp-qualified-group ?s)
1630     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1631     (?D gnus-tmp-newsgroup-description ?s)
1632     (?o gnus-tmp-moderated ?c)
1633     (?O gnus-tmp-moderated-string ?s)
1634     (?p gnus-tmp-process-marked ?c)
1635     (?s gnus-tmp-news-server ?s)
1636     (?n gnus-tmp-news-method ?s)
1637     (?P gnus-group-indentation ?s)
1638     (?l gnus-tmp-grouplens ?s)
1639     (?z gnus-tmp-news-method-string ?s)
1640     (?u gnus-tmp-user-defined ?s)))
1641
1642 (defvar gnus-summary-line-format-alist
1643   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1644     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1645     (?s gnus-tmp-subject-or-nil ?s)
1646     (?n gnus-tmp-name ?s)
1647     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1648         ?s)
1649     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1650             gnus-tmp-from) ?s)
1651     (?F gnus-tmp-from ?s)
1652     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1653     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1654     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1655     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1656     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1657     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1658     (?L gnus-tmp-lines ?d)
1659     (?I gnus-tmp-indentation ?s)
1660     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1661     (?R gnus-tmp-replied ?c)
1662     (?\[ gnus-tmp-opening-bracket ?c)
1663     (?\] gnus-tmp-closing-bracket ?c)
1664     (?\> (make-string gnus-tmp-level ? ) ?s)
1665     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1666     (?i gnus-tmp-score ?d)
1667     (?z gnus-tmp-score-char ?c)
1668     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1669     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1670     (?U gnus-tmp-unread ?c)
1671     (?t (gnus-summary-number-of-articles-in-thread
1672          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1673         ?d)
1674     (?e (gnus-summary-number-of-articles-in-thread
1675          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1676         ?c)
1677     (?u gnus-tmp-user-defined ?s))
1678   "An alist of format specifications that can appear in summary lines,
1679 and what variables they correspond with, along with the type of the
1680 variable (string, integer, character, etc).")
1681
1682 (defvar gnus-summary-dummy-line-format-alist
1683   `((?S gnus-tmp-subject ?s)
1684     (?N gnus-tmp-number ?d)
1685     (?u gnus-tmp-user-defined ?s)))
1686
1687 (defvar gnus-summary-mode-line-format-alist
1688   `((?G gnus-tmp-group-name ?s)
1689     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1690     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1691     (?A gnus-tmp-article-number ?d)
1692     (?Z gnus-tmp-unread-and-unselected ?s)
1693     (?V gnus-version ?s)
1694     (?U gnus-tmp-unread ?d)
1695     (?S gnus-tmp-subject ?s)
1696     (?e gnus-tmp-unselected ?d)
1697     (?u gnus-tmp-user-defined ?s)
1698     (?d (length gnus-newsgroup-dormant) ?d)
1699     (?t (length gnus-newsgroup-marked) ?d)
1700     (?r (length gnus-newsgroup-reads) ?d)
1701     (?E gnus-newsgroup-expunged-tally ?d)
1702     (?s (gnus-current-score-file-nondirectory) ?s)))
1703
1704 (defvar gnus-article-mode-line-format-alist
1705   gnus-summary-mode-line-format-alist)
1706
1707 (defvar gnus-group-mode-line-format-alist
1708   `((?S gnus-tmp-news-server ?s)
1709     (?M gnus-tmp-news-method ?s)
1710     (?u gnus-tmp-user-defined ?s)))
1711
1712 (defvar gnus-have-read-active-file nil)
1713
1714 (defconst gnus-maintainer
1715   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1716   "The mail address of the Gnus maintainers.")
1717
1718 (defconst gnus-version "September Gnus v0.56"
1719   "Version number for this version of Gnus.")
1720
1721 (defvar gnus-info-nodes
1722   '((gnus-group-mode            "(gnus)The Group Buffer")
1723     (gnus-summary-mode          "(gnus)The Summary Buffer")
1724     (gnus-article-mode          "(gnus)The Article Buffer"))
1725   "Assoc list of major modes and related Info nodes.")
1726
1727 (defvar gnus-group-buffer "*Group*")
1728 (defvar gnus-summary-buffer "*Summary*")
1729 (defvar gnus-article-buffer "*Article*")
1730 (defvar gnus-server-buffer "*Server*")
1731
1732 (defvar gnus-work-buffer " *gnus work*")
1733
1734 (defvar gnus-original-article-buffer " *Original Article*")
1735 (defvar gnus-original-article nil)
1736
1737 (defvar gnus-buffer-list nil
1738   "Gnus buffers that should be killed on exit.")
1739
1740 (defvar gnus-slave nil
1741   "Whether this Gnus is a slave or not.")
1742
1743 (defvar gnus-variable-list
1744   '(gnus-newsrc-options gnus-newsrc-options-n
1745     gnus-newsrc-last-checked-date
1746     gnus-newsrc-alist gnus-server-alist
1747     gnus-killed-list gnus-zombie-list
1748     gnus-topic-topology gnus-topic-alist
1749     gnus-format-specs)
1750   "Gnus variables saved in the quick startup file.")
1751
1752 (defvar gnus-newsrc-options nil
1753   "Options line in the .newsrc file.")
1754
1755 (defvar gnus-newsrc-options-n nil
1756   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1757
1758 (defvar gnus-newsrc-last-checked-date nil
1759   "Date Gnus last asked server for new newsgroups.")
1760
1761 (defvar gnus-topic-topology nil
1762   "The complete topic hierarchy.")
1763
1764 (defvar gnus-topic-alist nil
1765   "The complete topic-group alist.")
1766
1767 (defvar gnus-newsrc-alist nil
1768   "Assoc list of read articles.
1769 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1770
1771 (defvar gnus-newsrc-hashtb nil
1772   "Hashtable of gnus-newsrc-alist.")
1773
1774 (defvar gnus-killed-list nil
1775   "List of killed newsgroups.")
1776
1777 (defvar gnus-killed-hashtb nil
1778   "Hash table equivalent of gnus-killed-list.")
1779
1780 (defvar gnus-zombie-list nil
1781   "List of almost dead newsgroups.")
1782
1783 (defvar gnus-description-hashtb nil
1784   "Descriptions of newsgroups.")
1785
1786 (defvar gnus-list-of-killed-groups nil
1787   "List of newsgroups that have recently been killed by the user.")
1788
1789 (defvar gnus-active-hashtb nil
1790   "Hashtable of active articles.")
1791
1792 (defvar gnus-moderated-list nil
1793   "List of moderated newsgroups.")
1794
1795 (defvar gnus-group-marked nil)
1796
1797 (defvar gnus-current-startup-file nil
1798   "Startup file for the current host.")
1799
1800 (defvar gnus-last-search-regexp nil
1801   "Default regexp for article search command.")
1802
1803 (defvar gnus-last-shell-command nil
1804   "Default shell command on article.")
1805
1806 (defvar gnus-current-select-method nil
1807   "The current method for selecting a newsgroup.")
1808
1809 (defvar gnus-group-list-mode nil)
1810
1811 (defvar gnus-article-internal-prepare-hook nil)
1812
1813 (defvar gnus-newsgroup-name nil)
1814 (defvar gnus-newsgroup-begin nil)
1815 (defvar gnus-newsgroup-end nil)
1816 (defvar gnus-newsgroup-last-rmail nil)
1817 (defvar gnus-newsgroup-last-mail nil)
1818 (defvar gnus-newsgroup-last-folder nil)
1819 (defvar gnus-newsgroup-last-file nil)
1820 (defvar gnus-newsgroup-auto-expire nil)
1821 (defvar gnus-newsgroup-active nil)
1822
1823 (defvar gnus-newsgroup-data nil)
1824 (defvar gnus-newsgroup-data-reverse nil)
1825 (defvar gnus-newsgroup-limit nil)
1826 (defvar gnus-newsgroup-limits nil)
1827
1828 (defvar gnus-newsgroup-unreads nil
1829   "List of unread articles in the current newsgroup.")
1830
1831 (defvar gnus-newsgroup-unselected nil
1832   "List of unselected unread articles in the current newsgroup.")
1833
1834 (defvar gnus-newsgroup-reads nil
1835   "Alist of read articles and article marks in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-expunged-tally nil)
1838
1839 (defvar gnus-newsgroup-marked nil
1840   "List of ticked articles in the current newsgroup (a subset of unread art).")
1841
1842 (defvar gnus-newsgroup-killed nil
1843   "List of ranges of articles that have been through the scoring process.")
1844
1845 (defvar gnus-newsgroup-cached nil
1846   "List of articles that come from the article cache.")
1847
1848 (defvar gnus-newsgroup-saved nil
1849   "List of articles that have been saved.")
1850
1851 (defvar gnus-newsgroup-kill-headers nil)
1852
1853 (defvar gnus-newsgroup-replied nil
1854   "List of articles that have been replied to in the current newsgroup.")
1855
1856 (defvar gnus-newsgroup-expirable nil
1857   "List of articles in the current newsgroup that can be expired.")
1858
1859 (defvar gnus-newsgroup-processable nil
1860   "List of articles in the current newsgroup that can be processed.")
1861
1862 (defvar gnus-newsgroup-bookmarks nil
1863   "List of articles in the current newsgroup that have bookmarks.")
1864
1865 (defvar gnus-newsgroup-dormant nil
1866   "List of dormant articles in the current newsgroup.")
1867
1868 (defvar gnus-newsgroup-scored nil
1869   "List of scored articles in the current newsgroup.")
1870
1871 (defvar gnus-newsgroup-headers nil
1872   "List of article headers in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-threads nil)
1875
1876 (defvar gnus-newsgroup-prepared nil
1877   "Whether the current group has been prepared properly.")
1878
1879 (defvar gnus-newsgroup-ancient nil
1880   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1881
1882 (defvar gnus-newsgroup-sparse nil)
1883
1884 (defvar gnus-current-article nil)
1885 (defvar gnus-article-current nil)
1886 (defvar gnus-current-headers nil)
1887 (defvar gnus-have-all-headers nil)
1888 (defvar gnus-last-article nil)
1889 (defvar gnus-newsgroup-history nil)
1890 (defvar gnus-current-kill-article nil)
1891
1892 ;; Save window configuration.
1893 (defvar gnus-prev-winconf nil)
1894
1895 (defvar gnus-summary-mark-positions nil)
1896 (defvar gnus-group-mark-positions nil)
1897
1898 (defvar gnus-reffed-article-number nil)
1899
1900 ;;; Let the byte-compiler know that we know about this variable.
1901 (defvar rmail-default-rmail-file)
1902
1903 (defvar gnus-cache-removable-articles nil)
1904
1905 (defvar gnus-dead-summary nil)
1906
1907 (defconst gnus-summary-local-variables
1908   '(gnus-newsgroup-name
1909     gnus-newsgroup-begin gnus-newsgroup-end
1910     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1911     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1912     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1913     gnus-newsgroup-unselected gnus-newsgroup-marked
1914     gnus-newsgroup-reads gnus-newsgroup-saved
1915     gnus-newsgroup-replied gnus-newsgroup-expirable
1916     gnus-newsgroup-processable gnus-newsgroup-killed
1917     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1918     gnus-newsgroup-headers gnus-newsgroup-threads
1919     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1920     gnus-current-article gnus-current-headers gnus-have-all-headers
1921     gnus-last-article gnus-article-internal-prepare-hook
1922     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1923     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1924     gnus-newsgroup-async 
1925     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1926     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1927     gnus-newsgroup-history gnus-newsgroup-ancient
1928     gnus-newsgroup-sparse
1929     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1930     gnus-newsgroup-adaptive-score-file
1931     (gnus-newsgroup-expunged-tally . 0)
1932     gnus-cache-removable-articles gnus-newsgroup-cached
1933     gnus-newsgroup-data gnus-newsgroup-data-reverse
1934     gnus-newsgroup-limit gnus-newsgroup-limits)
1935   "Variables that are buffer-local to the summary buffers.")
1936
1937 (defconst gnus-bug-message
1938   "Sending a bug report to the Gnus Towers.
1939 ========================================
1940
1941 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1942 be sent to the Gnus Bug Exterminators.
1943
1944 At the bottom of the buffer you'll see lots of variable settings.
1945 Please do not delete those.  They will tell the Bug People what your
1946 environment is, so that it will be easier to locate the bugs.
1947
1948 If you have found a bug that makes Emacs go \"beep\", set
1949 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1950 and include the backtrace in your bug report.
1951
1952 Please describe the bug in annoying, painstaking detail.
1953
1954 Thank you for your help in stamping out bugs.
1955 ")
1956
1957 ;;; End of variables.
1958
1959 ;; Define some autoload functions Gnus might use.
1960 (eval-and-compile
1961
1962   ;; This little mapcar goes through the list below and marks the
1963   ;; symbols in question as autoloaded functions.
1964   (mapcar
1965    (lambda (package)
1966      (let ((interactive (nth 1 (memq ':interactive package))))
1967        (mapcar
1968         (lambda (function)
1969           (let (keymap)
1970             (when (consp function)
1971               (setq keymap (car (memq 'keymap function)))
1972               (setq function (car function)))
1973             (autoload function (car package) nil interactive keymap)))
1974         (if (eq (nth 1 package) ':interactive)
1975             (cdddr package)
1976           (cdr package)))))
1977    '(("metamail" metamail-buffer)
1978      ("info" Info-goto-node)
1979      ("hexl" hexl-hex-string-to-integer)
1980      ("pp" pp pp-to-string pp-eval-expression)
1981      ("mail-extr" mail-extract-address-components)
1982      ("nnmail" nnmail-split-fancy nnmail-article-group)
1983      ("nnvirtual" nnvirtual-catchup-group)
1984      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1985       timezone-make-sortable-date timezone-make-time-string)
1986      ("sendmail" mail-position-on-field mail-setup)
1987      ("rmailout" rmail-output)
1988      ("rnewspost" news-mail-other-window news-reply-yank-original
1989       news-caesar-buffer-body)
1990      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1991       rmail-show-message)
1992      ("gnus-soup" :interactive t
1993       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1994       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1995      ("nnsoup" nnsoup-pack-replies)
1996      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1997       gnus-Folder-save-name gnus-folder-save-name)
1998      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1999      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2000       gnus-server-make-menu-bar gnus-article-make-menu-bar
2001       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2002       gnus-summary-highlight-line gnus-carpal-setup-buffer
2003       gnus-group-highlight-line
2004       gnus-article-add-button gnus-insert-next-page-button
2005       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2006      ("gnus-vis" :interactive t
2007       gnus-article-push-button gnus-article-press-button
2008       gnus-article-highlight gnus-article-highlight-some
2009       gnus-article-highlight-headers gnus-article-highlight-signature
2010       gnus-article-add-buttons gnus-article-add-buttons-to-head
2011       gnus-article-next-button gnus-article-prev-button)
2012      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2013       gnus-demon-add-disconnection gnus-demon-add-handler
2014       gnus-demon-remove-handler)
2015      ("gnus-demon" :interactive t
2016       gnus-demon-init gnus-demon-cancel)
2017      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2018       gnus-tree-open gnus-tree-close)
2019      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2020       gnus-nocem-unwanted-article-p)
2021      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2022      ("gnus-srvr" gnus-browse-foreign-server)
2023      ("gnus-cite" :interactive t
2024       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2025       gnus-article-hide-citation gnus-article-fill-cited-article)
2026      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2027       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2028       gnus-execute gnus-expunge)
2029      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2030       gnus-cache-possibly-remove-articles gnus-cache-request-article
2031       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2032       gnus-cache-enter-remove-article gnus-cached-article-p
2033       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2034      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2035       gnus-cache-remove-article)
2036      ("gnus-score" :interactive t
2037       gnus-summary-increase-score gnus-summary-lower-score
2038       gnus-score-flush-cache gnus-score-close
2039       gnus-score-raise-same-subject-and-select
2040       gnus-score-raise-same-subject gnus-score-default
2041       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2042       gnus-score-lower-same-subject gnus-score-lower-thread
2043       gnus-possibly-score-headers)
2044      ("gnus-score"
2045       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2046       gnus-current-score-file-nondirectory gnus-score-adaptive
2047       gnus-score-find-trace gnus-score-file-name)
2048      ("gnus-edit" :interactive t gnus-score-customize)
2049      ("gnus-topic" :interactive t gnus-topic-mode)
2050      ("gnus-topic" gnus-topic-remove-group)
2051      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2052      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2053      ("gnus-uu" :interactive t
2054       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2055       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2056       gnus-uu-mark-by-regexp gnus-uu-mark-all
2057       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2058       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2059       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2060       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2061       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2062       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2063       gnus-uu-decode-binhex-view)
2064      ("gnus-msg" (gnus-summary-send-map keymap)
2065       gnus-mail-yank-original gnus-mail-send-and-exit
2066       gnus-sendmail-setup-mail gnus-article-mail
2067       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2068      ("gnus-msg" :interactive t
2069       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2070       gnus-summary-followup gnus-summary-followup-with-original
2071       gnus-summary-followup-and-reply
2072       gnus-summary-followup-and-reply-with-original
2073       gnus-summary-cancel-article gnus-summary-supersede-article
2074       gnus-post-news gnus-inews-news gnus-cancel-news
2075       gnus-summary-reply gnus-summary-reply-with-original
2076       gnus-summary-mail-forward gnus-summary-mail-other-window
2077       gnus-bug)
2078      ("gnus-picon" :interactive t gnus-article-display-picons
2079       gnus-group-display-picons gnus-picons-article-display-x-face)
2080      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2081       gnus-grouplens-mode)
2082      ("gnus-vm" gnus-vm-mail-setup)
2083      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2084       gnus-summary-save-article-vm gnus-yank-article))))
2085
2086 \f
2087
2088 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2089 ;; If you want the cursor to go somewhere else, set these two
2090 ;; functions in some startup hook to whatever you want.
2091 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2092 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2093
2094 ;;; Various macros and substs.
2095
2096 (defun gnus-header-from (header)
2097   (mail-header-from header))
2098
2099 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2100   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2101   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2102     `(let ((,tempvar (selected-window)))
2103        (unwind-protect
2104            (progn
2105              (pop-to-buffer ,buffer)
2106              ,@forms)
2107          (select-window ,tempvar)))))
2108
2109 (defmacro gnus-gethash (string hashtable)
2110   "Get hash value of STRING in HASHTABLE."
2111   `(symbol-value (intern-soft ,string ,hashtable)))
2112
2113 (defmacro gnus-sethash (string value hashtable)
2114   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2115   `(set (intern ,string ,hashtable) ,value))
2116
2117 (defmacro gnus-intern-safe (string hashtable)
2118   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2119   `(let ((symbol (intern ,string ,hashtable)))
2120      (or (boundp symbol)
2121          (set symbol nil))
2122      symbol))
2123
2124 (defmacro gnus-group-unread (group)
2125   "Get the currently computed number of unread articles in GROUP."
2126   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2127
2128 (defmacro gnus-group-entry (group)
2129   "Get the newsrc entry for GROUP."
2130   `(gnus-gethash ,group gnus-newsrc-hashtb))
2131
2132 (defmacro gnus-active (group)
2133   "Get active info on GROUP."
2134   `(gnus-gethash ,group gnus-active-hashtb))
2135
2136 (defmacro gnus-set-active (group active)
2137   "Set GROUP's active info."
2138   `(gnus-sethash ,group ,active gnus-active-hashtb))
2139
2140 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2141 ;;   function `substring' might cut on a middle of multi-octet
2142 ;;   character.
2143 (defun gnus-truncate-string (str width)
2144   (substring str 0 width))
2145
2146 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2147 ;; to limit the length of a string.  This function is necessary since
2148 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2149 (defsubst gnus-limit-string (str width)
2150   (if (> (length str) width)
2151       (substring str 0 width)
2152     str))
2153
2154 (defsubst gnus-simplify-subject-re (subject)
2155   "Remove \"Re:\" from subject lines."
2156   (if (string-match "^[Rr][Ee]: *" subject)
2157       (substring subject (match-end 0))
2158     subject))
2159
2160 (defsubst gnus-functionp (form)
2161   "Return non-nil if FORM is funcallable."
2162   (or (and (symbolp form) (fboundp form))
2163       (and (listp form) (eq (car form) 'lambda))))
2164
2165 (defsubst gnus-goto-char (point)
2166   (and point (goto-char point)))
2167
2168 (defmacro gnus-buffer-exists-p (buffer)
2169   `(let ((buffer ,buffer))
2170      (and buffer
2171           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2172                    buffer))))
2173
2174 (defmacro gnus-kill-buffer (buffer)
2175   `(let ((buf ,buffer))
2176      (if (gnus-buffer-exists-p buf)
2177          (kill-buffer buf))))
2178
2179 (defsubst gnus-point-at-bol ()
2180   "Return point at the beginning of the line."
2181   (let ((p (point)))
2182     (beginning-of-line)
2183     (prog1
2184         (point)
2185       (goto-char p))))
2186
2187 (defsubst gnus-point-at-eol ()
2188   "Return point at the end of the line."
2189   (let ((p (point)))
2190     (end-of-line)
2191     (prog1
2192         (point)
2193       (goto-char p))))
2194
2195 (defun gnus-alive-p ()
2196   "Say whether Gnus is running or not."
2197   (and gnus-group-buffer
2198        (get-buffer gnus-group-buffer)))
2199
2200 ;; Delete the current line (and the next N lines.);
2201 (defmacro gnus-delete-line (&optional n)
2202   `(delete-region (progn (beginning-of-line) (point))
2203                   (progn (forward-line ,(or n 1)) (point))))
2204
2205 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2206 (defvar gnus-init-inhibit nil)
2207 (defun gnus-read-init-file (&optional inhibit-next)
2208   (if gnus-init-inhibit
2209       (setq gnus-init-inhibit nil)
2210     (setq gnus-init-inhibit inhibit-next)
2211     (and gnus-init-file
2212          (or (and (file-exists-p gnus-init-file)
2213                   ;; Don't try to load a directory.
2214                   (not (file-directory-p gnus-init-file)))
2215              (file-exists-p (concat gnus-init-file ".el"))
2216              (file-exists-p (concat gnus-init-file ".elc")))
2217          (condition-case var
2218              (load gnus-init-file nil t)
2219            (error
2220             (error "Error in %s: %s" gnus-init-file var))))))
2221
2222 ;; Info access macros.
2223
2224 (defmacro gnus-info-group (info)
2225   `(nth 0 ,info))
2226 (defmacro gnus-info-rank (info)
2227   `(nth 1 ,info))
2228 (defmacro gnus-info-read (info)
2229   `(nth 2 ,info))
2230 (defmacro gnus-info-marks (info)
2231   `(nth 3 ,info))
2232 (defmacro gnus-info-method (info)
2233   `(nth 4 ,info))
2234 (defmacro gnus-info-params (info)
2235   `(nth 5 ,info))
2236
2237 (defmacro gnus-info-level (info)
2238   `(let ((rank (gnus-info-rank ,info)))
2239      (if (consp rank)
2240          (car rank)
2241        rank)))
2242 (defmacro gnus-info-score (info)
2243   `(let ((rank (gnus-info-rank ,info)))
2244      (or (and (consp rank) (cdr rank)) 0)))
2245
2246 (defmacro gnus-info-set-group (info group)
2247   `(setcar ,info ,group))
2248 (defmacro gnus-info-set-rank (info rank)
2249   `(setcar (nthcdr 1 ,info) ,rank))
2250 (defmacro gnus-info-set-read (info read)
2251   `(setcar (nthcdr 2 ,info) ,read))
2252 (defmacro gnus-info-set-marks (info marks)
2253   `(setcar (nthcdr 3 ,info) ,marks))
2254 (defmacro gnus-info-set-method (info method)
2255   `(setcar (nthcdr 4 ,info) ,method))
2256 (defmacro gnus-info-set-params (info params)
2257   `(setcar (nthcdr 5 ,info) ,params))
2258
2259 (defmacro gnus-info-set-level (info level)
2260   `(let ((rank (cdr ,info)))
2261      (if (consp (car rank))
2262          (setcar (car rank) ,level)
2263        (setcar rank ,level))))
2264 (defmacro gnus-info-set-score (info score)
2265   `(let ((rank (cdr ,info)))
2266      (if (consp (car rank))
2267          (setcdr (car rank) ,score)
2268        (setcar rank (cons (car rank) ,score)))))
2269
2270 (defmacro gnus-get-info (group)
2271   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2272
2273 (defun gnus-byte-code (func)
2274   "Return a form that can be `eval'ed based on FUNC."
2275   (let ((fval (symbol-function func)))
2276     (if (byte-code-function-p fval)
2277         (let ((flist (append fval nil)))
2278           (setcar flist 'byte-code)
2279           flist)
2280       (cons 'progn (cddr fval)))))
2281
2282 ;;; Load the compatability functions.
2283
2284 (require 'gnus-cus)
2285 (require 'gnus-ems)
2286
2287 \f
2288
2289 ;; Format specs.  The chunks below are the machine-generated forms
2290 ;; that are to be evaled as the result of the default format strings.
2291 ;; We write them in here to get them byte-compiled.  That way the
2292 ;; default actions will be quite fast, while still retaining the full
2293 ;; flexibility of the user-defined format specs.
2294
2295 ;; First we have lots of dummy defvars to let the compiler know these
2296 ;; are really dynamic variables.
2297
2298 (defvar gnus-tmp-unread)
2299 (defvar gnus-tmp-replied)
2300 (defvar gnus-tmp-score-char)
2301 (defvar gnus-tmp-indentation)
2302 (defvar gnus-tmp-opening-bracket)
2303 (defvar gnus-tmp-lines)
2304 (defvar gnus-tmp-name)
2305 (defvar gnus-tmp-closing-bracket)
2306 (defvar gnus-tmp-subject-or-nil)
2307 (defvar gnus-tmp-subject)
2308 (defvar gnus-tmp-marked)
2309 (defvar gnus-tmp-marked-mark)
2310 (defvar gnus-tmp-subscribed)
2311 (defvar gnus-tmp-process-marked)
2312 (defvar gnus-tmp-number-of-unread)
2313 (defvar gnus-tmp-group-name)
2314 (defvar gnus-tmp-group)
2315 (defvar gnus-tmp-article-number)
2316 (defvar gnus-tmp-unread-and-unselected)
2317 (defvar gnus-tmp-news-method)
2318 (defvar gnus-tmp-news-server)
2319 (defvar gnus-tmp-article-number)
2320 (defvar gnus-mouse-face)
2321 (defvar gnus-mouse-face-prop)
2322
2323 (defun gnus-summary-line-format-spec ()
2324   (insert gnus-tmp-unread gnus-tmp-replied
2325           gnus-tmp-score-char gnus-tmp-indentation)
2326   (put-text-property
2327    (point)
2328    (progn
2329      (insert
2330       gnus-tmp-opening-bracket
2331       (format "%4d: %-20s"
2332               gnus-tmp-lines
2333               (if (> (length gnus-tmp-name) 20)
2334                   (substring gnus-tmp-name 0 20)
2335                 gnus-tmp-name))
2336       gnus-tmp-closing-bracket)
2337      (point))
2338    gnus-mouse-face-prop gnus-mouse-face)
2339   (insert " " gnus-tmp-subject-or-nil "\n"))
2340
2341 (defvar gnus-summary-line-format-spec
2342   (gnus-byte-code 'gnus-summary-line-format-spec))
2343
2344 (defun gnus-summary-dummy-line-format-spec ()
2345   (insert "*  ")
2346   (put-text-property
2347    (point)
2348    (progn
2349      (insert ":                          :")
2350      (point))
2351    gnus-mouse-face-prop gnus-mouse-face)
2352   (insert " " gnus-tmp-subject "\n"))
2353
2354 (defvar gnus-summary-dummy-line-format-spec
2355   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2356
2357 (defun gnus-group-line-format-spec ()
2358   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2359           gnus-tmp-process-marked
2360           gnus-group-indentation
2361           (format "%5s: " gnus-tmp-number-of-unread))
2362   (put-text-property
2363    (point)
2364    (progn
2365      (insert gnus-tmp-group "\n")
2366      (1- (point)))
2367    gnus-mouse-face-prop gnus-mouse-face))
2368 (defvar gnus-group-line-format-spec
2369   (gnus-byte-code 'gnus-group-line-format-spec))
2370
2371 (defvar gnus-format-specs
2372   `((version . ,emacs-version)
2373     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2374     (summary-dummy ,gnus-summary-dummy-line-format
2375                    ,gnus-summary-dummy-line-format-spec)
2376     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2377
2378 (defvar gnus-article-mode-line-format-spec nil)
2379 (defvar gnus-summary-mode-line-format-spec nil)
2380 (defvar gnus-group-mode-line-format-spec nil)
2381
2382 ;;; Phew.  All that gruft is over, fortunately.
2383
2384 \f
2385 ;;;
2386 ;;; Gnus Utility Functions
2387 ;;;
2388
2389 (defun gnus-extract-address-components (from)
2390   (let (name address)
2391     ;; First find the address - the thing with the @ in it.  This may
2392     ;; not be accurate in mail addresses, but does the trick most of
2393     ;; the time in news messages.
2394     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2395         (setq address (substring from (match-beginning 0) (match-end 0))))
2396     ;; Then we check whether the "name <address>" format is used.
2397     (and address
2398          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2399          ;; Linear white space is not required.
2400          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2401          (and (setq name (substring from 0 (match-beginning 0)))
2402               ;; Strip any quotes from the name.
2403               (string-match "\".*\"" name)
2404               (setq name (substring name 1 (1- (match-end 0))))))
2405     ;; If not, then "address (name)" is used.
2406     (or name
2407         (and (string-match "(.+)" from)
2408              (setq name (substring from (1+ (match-beginning 0))
2409                                    (1- (match-end 0)))))
2410         (and (string-match "()" from)
2411              (setq name address))
2412         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2413         ;; XOVER might not support folded From headers.
2414         (and (string-match "(.*" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (match-end 0)))))
2417     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2418     (list (or name from) (or address from))))
2419
2420 (defun gnus-fetch-field (field)
2421   "Return the value of the header FIELD of current article."
2422   (save-excursion
2423     (save-restriction
2424       (let ((case-fold-search t))
2425         (nnheader-narrow-to-headers)
2426         (mail-fetch-field field)))))
2427
2428 (defun gnus-goto-colon ()
2429   (beginning-of-line)
2430   (search-forward ":" (gnus-point-at-eol) t))
2431
2432 ;;;###autoload
2433 (defun gnus-update-format (var)
2434   "Update the format specification near point."
2435   (interactive
2436    (list
2437     (save-excursion
2438       (eval-defun nil)
2439       ;; Find the end of the current word.
2440       (re-search-forward "[ \t\n]" nil t)
2441       ;; Search backward.
2442       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2443         (match-string 1)))))
2444   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2445                               (match-string 1 var))))
2446          (entry (assq type gnus-format-specs))
2447          value spec)
2448     (when entry
2449       (setq gnus-format-specs (delq entry gnus-format-specs)))
2450     (set
2451      (intern (format "%s-spec" var))
2452      (gnus-parse-format (setq value (symbol-value (intern var)))
2453                         (symbol-value (intern (format "%s-alist" var)))
2454                         (not (string-match "mode" var))))
2455     (setq spec (symbol-value (intern (format "%s-spec" var))))
2456     (push (list type value spec) gnus-format-specs)
2457
2458     (pop-to-buffer "*Gnus Format*")
2459     (erase-buffer)
2460     (lisp-interaction-mode)
2461     (insert (pp-to-string spec))))
2462
2463 (defun gnus-update-format-specifications (&optional force)
2464   "Update all (necessary) format specifications."
2465   ;; Make the indentation array.
2466   (gnus-make-thread-indent-array)
2467
2468   ;; See whether all the stored info needs to be flushed.
2469   (when (or force
2470             (not (equal emacs-version
2471                         (cdr (assq 'version gnus-format-specs)))))
2472     (setq gnus-format-specs nil))
2473
2474   ;; Go through all the formats and see whether they need updating.
2475   (let ((types '(summary summary-dummy group
2476                          summary-mode group-mode article-mode))
2477         new-format entry type val)
2478     (while (setq type (pop types))
2479       ;; Jump to the proper buffer to find out the value of
2480       ;; the variable, if possible.  (It may be buffer-local.)
2481       (save-excursion
2482         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2483               val)
2484           (when (and (boundp buffer)
2485                      (setq val (symbol-value buffer))
2486                      (get-buffer val)
2487                      (buffer-name (get-buffer val)))
2488             (set-buffer (get-buffer val)))
2489           (setq new-format (symbol-value
2490                             (intern (format "gnus-%s-line-format" type))))))
2491       (setq entry (cdr (assq type gnus-format-specs)))
2492       (if (and entry
2493                (equal (car entry) new-format))
2494           ;; Use the old format.
2495           (set (intern (format "gnus-%s-line-format-spec" type))
2496                (cadr entry))
2497         ;; This is a new format.
2498         (setq val
2499               (if (not (stringp new-format))
2500                   ;; This is a function call or something.
2501                   new-format
2502                 ;; This is a "real" format.
2503                 (gnus-parse-format
2504                  new-format
2505                  (symbol-value
2506                   (intern (format "gnus-%s-line-format-alist"
2507                                   (if (eq type 'article-mode)
2508                                       'summary-mode type))))
2509                  (not (string-match "mode$" (symbol-name type))))))
2510         ;; Enter the new format spec into the list.
2511         (if entry
2512             (progn
2513               (setcar (cdr entry) val)
2514               (setcar entry new-format))
2515           (push (list type new-format val) gnus-format-specs))
2516         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2517
2518   (unless (assq 'version gnus-format-specs)
2519     (push (cons 'version emacs-version) gnus-format-specs))
2520
2521   (gnus-update-group-mark-positions)
2522   (gnus-update-summary-mark-positions))
2523
2524 (defun gnus-update-summary-mark-positions ()
2525   "Compute where the summary marks are to go."
2526   (save-excursion
2527     (let ((gnus-replied-mark 129)
2528           (gnus-score-below-mark 130)
2529           (gnus-score-over-mark 130)
2530           (thread nil)
2531           (gnus-visual nil)
2532           pos)
2533       (gnus-set-work-buffer)
2534       (gnus-summary-insert-line
2535        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2536       (goto-char (point-min))
2537       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2538                                          (- (point) 2)))))
2539       (goto-char (point-min))
2540       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2541             pos)
2542       (goto-char (point-min))
2543       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2544             pos)
2545       (setq gnus-summary-mark-positions pos))))
2546
2547 (defun gnus-update-group-mark-positions ()
2548   (save-excursion
2549     (let ((gnus-process-mark 128)
2550           (gnus-group-marked '("dummy.group")))
2551       (gnus-set-active "dummy.group" '(0 . 0))
2552       (gnus-set-work-buffer)
2553       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2554       (goto-char (point-min))
2555       (setq gnus-group-mark-positions
2556             (list (cons 'process (and (search-forward "\200" nil t)
2557                                       (- (point) 2))))))))
2558
2559 (defvar gnus-mouse-face-0 'highlight)
2560 (defvar gnus-mouse-face-1 'highlight)
2561 (defvar gnus-mouse-face-2 'highlight)
2562 (defvar gnus-mouse-face-3 'highlight)
2563 (defvar gnus-mouse-face-4 'highlight)
2564
2565 (defun gnus-mouse-face-function (form type)
2566   `(put-text-property
2567     (point) (progn ,@form (point))
2568     gnus-mouse-face-prop
2569     ,(if (equal type 0)
2570          'gnus-mouse-face
2571        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2572
2573 (defvar gnus-face-0 'bold)
2574 (defvar gnus-face-1 'italic)
2575 (defvar gnus-face-2 'bold-italic)
2576 (defvar gnus-face-3 'bold)
2577 (defvar gnus-face-4 'bold)
2578
2579 (defun gnus-face-face-function (form type)
2580   `(put-text-property
2581     (point) (progn ,@form (point))
2582     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2583
2584 (defun gnus-max-width-function (el max-width)
2585   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2586   (if (symbolp el)
2587       `(if (> (length ,el) ,max-width)
2588            (substring ,el 0 ,max-width)
2589          ,el)
2590     `(let ((val (eval ,el)))
2591        (if (numberp val)
2592            (setq val (int-to-string val)))
2593        (if (> (length val) ,max-width)
2594            (substring val 0 ,max-width)
2595          val))))
2596
2597 (defun gnus-parse-format (format spec-alist &optional insert)
2598   ;; This function parses the FORMAT string with the help of the
2599   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2600   ;; string.  If the FORMAT string contains the specifiers %( and %)
2601   ;; the text between them will have the mouse-face text property.
2602   (if (string-match
2603        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2604        format)
2605       (gnus-parse-complex-format format spec-alist)
2606     ;; This is a simple format.
2607     (gnus-parse-simple-format format spec-alist insert)))
2608
2609 (defun gnus-parse-complex-format (format spec-alist)
2610   (save-excursion
2611     (gnus-set-work-buffer)
2612     (insert format)
2613     (goto-char (point-min))
2614     (while (re-search-forward "\"" nil t)
2615       (replace-match "\\\"" nil t))
2616     (goto-char (point-min))
2617     (insert "(\"")
2618     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2619       (let ((number (if (match-beginning 1)
2620                         (match-string 1) "0"))
2621             (delim (aref (match-string 2) 0)))
2622         (if (or (= delim ?\() (= delim ?\{))
2623             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2624                                    " " number " \""))
2625           (replace-match "\")\""))))
2626     (goto-char (point-max))
2627     (insert "\")")
2628     (goto-char (point-min))
2629     (let ((form (read (current-buffer))))
2630       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2631
2632 (defun gnus-complex-form-to-spec (form spec-alist)
2633   (delq nil
2634         (mapcar
2635          (lambda (sform)
2636            (if (stringp sform)
2637                (gnus-parse-simple-format sform spec-alist t)
2638              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2639                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2640                       (nth 1 sform))))
2641          form)))
2642
2643 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2644   ;; This function parses the FORMAT string with the help of the
2645   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2646   ;; string.
2647   (let ((max-width 0)
2648         spec flist fstring newspec elem beg result dontinsert)
2649     (save-excursion
2650       (gnus-set-work-buffer)
2651       (insert format)
2652       (goto-char (point-min))
2653       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2654                                 nil t)
2655         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2656               (setq newspec "%"
2657                     beg (1+ (match-beginning 0)))
2658           ;; First check if there are any specs that look anything like
2659           ;; "%12,12A", ie. with a "max width specification".  These have
2660           ;; to be treated specially.
2661           (if (setq beg (match-beginning 1))
2662               (setq max-width
2663                     (string-to-int
2664                      (buffer-substring
2665                       (1+ (match-beginning 1)) (match-end 1))))
2666             (setq max-width 0)
2667             (setq beg (match-beginning 2)))
2668           ;; Find the specification from `spec-alist'.
2669           (unless (setq elem (cdr (assq spec spec-alist)))
2670             (setq elem '("*" ?s)))
2671           ;; Treat user defined format specifiers specially.
2672           (when (eq (car elem) 'gnus-tmp-user-defined)
2673             (setq elem
2674                   (list
2675                    (list (intern (concat "gnus-user-format-function-"
2676                                          (match-string 3)))
2677                          'gnus-tmp-header) ?s))
2678             (delete-region (match-beginning 3) (match-end 3)))
2679           (if (not (zerop max-width))
2680               (let ((el (car elem)))
2681                 (cond ((= (cadr elem) ?c)
2682                        (setq el (list 'char-to-string el)))
2683                       ((= (cadr elem) ?d)
2684                        (setq el (list 'int-to-string el))))
2685                 (setq flist (cons (gnus-max-width-function el max-width)
2686                                   flist))
2687                 (setq newspec ?s))
2688             (progn
2689               (setq flist (cons (car elem) flist))
2690               (setq newspec (cadr elem)))))
2691         ;; Remove the old specification (and possibly a ",12" string).
2692         (delete-region beg (match-end 2))
2693         ;; Insert the new specification.
2694         (goto-char beg)
2695         (insert newspec))
2696       (setq fstring (buffer-substring 1 (point-max))))
2697     ;; Do some postprocessing to increase efficiency.
2698     (setq
2699      result
2700      (cond
2701       ;; Emptyness.
2702       ((string= fstring "")
2703        nil)
2704       ;; Not a format string.
2705       ((not (string-match "%" fstring))
2706        (list fstring))
2707       ;; A format string with just a single string spec.
2708       ((string= fstring "%s")
2709        (list (car flist)))
2710       ;; A single character.
2711       ((string= fstring "%c")
2712        (list (car flist)))
2713       ;; A single number.
2714       ((string= fstring "%d")
2715        (setq dontinsert)
2716        (if insert
2717            (list `(princ ,(car flist)))
2718          (list `(int-to-string ,(car flist)))))
2719       ;; Just lots of chars and strings.
2720       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2721        (nreverse flist))
2722       ;; A single string spec at the beginning of the spec.
2723       ((string-match "\\`%[sc][^%]+\\'" fstring)
2724        (list (car flist) (substring fstring 2)))
2725       ;; A single string spec in the middle of the spec.
2726       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2727        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2728       ;; A single string spec in the end of the spec.
2729       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2730        (list (match-string 1 fstring) (car flist)))
2731       ;; A more complex spec.
2732       (t
2733        (list (cons 'format (cons fstring (nreverse flist)))))))
2734
2735     (if insert
2736         (when result
2737           (if dontinsert
2738               result
2739             (cons 'insert result)))
2740       (cond ((stringp result)
2741              result)
2742             ((consp result)
2743              (cons 'concat result))
2744             (t "")))))
2745
2746 (defun gnus-eval-format (format &optional alist props)
2747   "Eval the format variable FORMAT, using ALIST.
2748 If PROPS, insert the result."
2749   (let ((form (gnus-parse-format format alist props)))
2750     (if props
2751         (add-text-properties (point) (progn (eval form) (point)) props)
2752       (eval form))))
2753
2754 (defun gnus-remove-text-with-property (prop)
2755   "Delete all text in the current buffer with text property PROP."
2756   (save-excursion
2757     (goto-char (point-min))
2758     (while (not (eobp))
2759       (while (get-text-property (point) prop)
2760         (delete-char 1))
2761       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2762
2763 (defun gnus-set-work-buffer ()
2764   (if (get-buffer gnus-work-buffer)
2765       (progn
2766         (set-buffer gnus-work-buffer)
2767         (erase-buffer))
2768     (set-buffer (get-buffer-create gnus-work-buffer))
2769     (kill-all-local-variables)
2770     (buffer-disable-undo (current-buffer))
2771     (gnus-add-current-to-buffer-list)))
2772
2773 ;; Article file names when saving.
2774
2775 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2776   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2777 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2778 Otherwise, it is like ~/News/news/group/num."
2779   (let ((default
2780           (expand-file-name
2781            (concat (if (gnus-use-long-file-name 'not-save)
2782                        (gnus-capitalize-newsgroup newsgroup)
2783                      (gnus-newsgroup-directory-form newsgroup))
2784                    "/" (int-to-string (mail-header-number headers)))
2785            (or gnus-article-save-directory "~/News"))))
2786     (if (and last-file
2787              (string-equal (file-name-directory default)
2788                            (file-name-directory last-file))
2789              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2790         default
2791       (or last-file default))))
2792
2793 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2794   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2795 If variable `gnus-use-long-file-name' is non-nil, it is
2796 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2797   (let ((default
2798           (expand-file-name
2799            (concat (if (gnus-use-long-file-name 'not-save)
2800                        newsgroup
2801                      (gnus-newsgroup-directory-form newsgroup))
2802                    "/" (int-to-string (mail-header-number headers)))
2803            (or gnus-article-save-directory "~/News"))))
2804     (if (and last-file
2805              (string-equal (file-name-directory default)
2806                            (file-name-directory last-file))
2807              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2808         default
2809       (or last-file default))))
2810
2811 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2812   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2813 If variable `gnus-use-long-file-name' is non-nil, it is
2814 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2815   (or last-file
2816       (expand-file-name
2817        (if (gnus-use-long-file-name 'not-save)
2818            (gnus-capitalize-newsgroup newsgroup)
2819          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2820        (or gnus-article-save-directory "~/News"))))
2821
2822 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2823   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2824 If variable `gnus-use-long-file-name' is non-nil, it is
2825 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2826   (or last-file
2827       (expand-file-name
2828        (if (gnus-use-long-file-name 'not-save)
2829            newsgroup
2830          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2831        (or gnus-article-save-directory "~/News"))))
2832
2833 ;; For subscribing new newsgroup
2834
2835 (defun gnus-subscribe-hierarchical-interactive (groups)
2836   (let ((groups (sort groups 'string<))
2837         prefixes prefix start ans group starts)
2838     (while groups
2839       (setq prefixes (list "^"))
2840       (while (and groups prefixes)
2841         (while (not (string-match (car prefixes) (car groups)))
2842           (setq prefixes (cdr prefixes)))
2843         (setq prefix (car prefixes))
2844         (setq start (1- (length prefix)))
2845         (if (and (string-match "[^\\.]\\." (car groups) start)
2846                  (cdr groups)
2847                  (setq prefix
2848                        (concat "^" (substring (car groups) 0 (match-end 0))))
2849                  (string-match prefix (cadr groups)))
2850             (progn
2851               (setq prefixes (cons prefix prefixes))
2852               (message "Descend hierarchy %s? ([y]nsq): "
2853                        (substring prefix 1 (1- (length prefix))))
2854               (setq ans (read-char))
2855               (cond ((= ans ?n)
2856                      (while (and groups
2857                                  (string-match prefix
2858                                                (setq group (car groups))))
2859                        (setq gnus-killed-list
2860                              (cons group gnus-killed-list))
2861                        (gnus-sethash group group gnus-killed-hashtb)
2862                        (setq groups (cdr groups)))
2863                      (setq starts (cdr starts)))
2864                     ((= ans ?s)
2865                      (while (and groups
2866                                  (string-match prefix
2867                                                (setq group (car groups))))
2868                        (gnus-sethash group group gnus-killed-hashtb)
2869                        (gnus-subscribe-alphabetically (car groups))
2870                        (setq groups (cdr groups)))
2871                      (setq starts (cdr starts)))
2872                     ((= ans ?q)
2873                      (while groups
2874                        (setq group (car groups))
2875                        (setq gnus-killed-list (cons group gnus-killed-list))
2876                        (gnus-sethash group group gnus-killed-hashtb)
2877                        (setq groups (cdr groups))))
2878                     (t nil)))
2879           (message "Subscribe %s? ([n]yq)" (car groups))
2880           (setq ans (read-char))
2881           (setq group (car groups))
2882           (cond ((= ans ?y)
2883                  (gnus-subscribe-alphabetically (car groups))
2884                  (gnus-sethash group group gnus-killed-hashtb))
2885                 ((= ans ?q)
2886                  (while groups
2887                    (setq group (car groups))
2888                    (setq gnus-killed-list (cons group gnus-killed-list))
2889                    (gnus-sethash group group gnus-killed-hashtb)
2890                    (setq groups (cdr groups))))
2891                 (t
2892                  (setq gnus-killed-list (cons group gnus-killed-list))
2893                  (gnus-sethash group group gnus-killed-hashtb)))
2894           (setq groups (cdr groups)))))))
2895
2896 (defun gnus-subscribe-randomly (newsgroup)
2897   "Subscribe new NEWSGROUP by making it the first newsgroup."
2898   (gnus-subscribe-newsgroup newsgroup))
2899
2900 (defun gnus-subscribe-alphabetically (newgroup)
2901   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2902   (let ((groups (cdr gnus-newsrc-alist))
2903         before)
2904     (while (and (not before) groups)
2905       (if (string< newgroup (caar groups))
2906           (setq before (caar groups))
2907         (setq groups (cdr groups))))
2908     (gnus-subscribe-newsgroup newgroup before)))
2909
2910 (defun gnus-subscribe-hierarchically (newgroup)
2911   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2912   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2913   (save-excursion
2914     (set-buffer (find-file-noselect gnus-current-startup-file))
2915     (let ((groupkey newgroup)
2916           before)
2917       (while (and (not before) groupkey)
2918         (goto-char (point-min))
2919         (let ((groupkey-re
2920                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2921           (while (and (re-search-forward groupkey-re nil t)
2922                       (progn
2923                         (setq before (match-string 1))
2924                         (string< before newgroup)))))
2925         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2926         (setq groupkey
2927               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2928                   (substring groupkey (match-beginning 1) (match-end 1)))))
2929       (gnus-subscribe-newsgroup newgroup before))))
2930
2931 (defun gnus-subscribe-interactively (group)
2932   "Subscribe the new GROUP interactively.
2933 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2934 it is killed."
2935   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2936       (gnus-subscribe-hierarchically group)
2937     (push group gnus-killed-list)))
2938
2939 (defun gnus-subscribe-zombies (group)
2940   "Make the new GROUP into a zombie group."
2941   (push group gnus-zombie-list))
2942
2943 (defun gnus-subscribe-killed (group)
2944   "Make the new GROUP a killed group."
2945   (push group gnus-killed-list))
2946
2947 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2948   "Subscribe new NEWSGROUP.
2949 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2950 the first newsgroup."
2951   ;; We subscribe the group by changing its level to `subscribed'.
2952   (gnus-group-change-level
2953    newsgroup gnus-level-default-subscribed
2954    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2955   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2956
2957 ;; For directories
2958
2959 (defun gnus-newsgroup-directory-form (newsgroup)
2960   "Make hierarchical directory name from NEWSGROUP name."
2961   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2962         (len (length newsgroup))
2963         idx)
2964     ;; If this is a foreign group, we don't want to translate the
2965     ;; entire name.
2966     (if (setq idx (string-match ":" newsgroup))
2967         (aset newsgroup idx ?/)
2968       (setq idx 0))
2969     ;; Replace all occurrences of `.' with `/'.
2970     (while (< idx len)
2971       (if (= (aref newsgroup idx) ?.)
2972           (aset newsgroup idx ?/))
2973       (setq idx (1+ idx)))
2974     newsgroup))
2975
2976 (defun gnus-newsgroup-savable-name (group)
2977   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2978   ;; with dots.
2979   (nnheader-replace-chars-in-string group ?/ ?.))
2980
2981 (defun gnus-make-directory (dir)
2982   "Make DIRECTORY recursively."
2983   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2984   ;; of the many mysteries of the universe.
2985   (let* ((dir (expand-file-name dir default-directory))
2986          dirs err)
2987     (if (string-match "/$" dir)
2988         (setq dir (substring dir 0 (match-beginning 0))))
2989     ;; First go down the path until we find a directory that exists.
2990     (while (not (file-exists-p dir))
2991       (setq dirs (cons dir dirs))
2992       (string-match "/[^/]+$" dir)
2993       (setq dir (substring dir 0 (match-beginning 0))))
2994     ;; Then create all the subdirs.
2995     (while (and dirs (not err))
2996       (condition-case ()
2997           (make-directory (car dirs))
2998         (error (setq err t)))
2999       (setq dirs (cdr dirs)))
3000     ;; We return whether we were successful or not.
3001     (not dirs)))
3002
3003 (defun gnus-capitalize-newsgroup (newsgroup)
3004   "Capitalize NEWSGROUP name."
3005   (and (not (zerop (length newsgroup)))
3006        (concat (char-to-string (upcase (aref newsgroup 0)))
3007                (substring newsgroup 1))))
3008
3009 ;; Various... things.
3010
3011 (defun gnus-simplify-subject (subject &optional re-only)
3012   "Remove `Re:' and words in parentheses.
3013 If RE-ONLY is non-nil, strip leading `Re:'s only."
3014   (let ((case-fold-search t))           ;Ignore case.
3015     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3016     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3017       (setq subject (substring subject (match-end 0))))
3018     ;; Remove uninteresting prefixes.
3019     (if (and (not re-only)
3020              gnus-simplify-ignored-prefixes
3021              (string-match gnus-simplify-ignored-prefixes subject))
3022         (setq subject (substring subject (match-end 0))))
3023     ;; Remove words in parentheses from end.
3024     (unless re-only
3025       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3026         (setq subject (substring subject 0 (match-beginning 0)))))
3027     ;; Return subject string.
3028     subject))
3029
3030 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3031 ;; all whitespace.
3032 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3033 (defun gnus-simplify-buffer-fuzzy ()
3034   (goto-char (point-min))
3035   (while (search-forward "\t" nil t)
3036     (replace-match " " t t))
3037   (goto-char (point-min))
3038   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3039   (goto-char (match-beginning 0))
3040   (while (or
3041           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3042           (looking-at "^[[].*: .*[]]$"))
3043     (goto-char (point-min))
3044     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3045                               nil t)
3046       (replace-match "" t t))
3047     (goto-char (point-min))
3048     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3049       (goto-char (match-end 0))
3050       (delete-char -1)
3051       (delete-region
3052        (progn (goto-char (match-beginning 0)))
3053        (re-search-forward ":"))))
3054   (goto-char (point-min))
3055   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3056     (replace-match "" t t))
3057   (goto-char (point-min))
3058   (while (re-search-forward "  +" nil t)
3059     (replace-match " " t t))
3060   (goto-char (point-min))
3061   (while (re-search-forward " $" nil t)
3062     (replace-match "" t t))
3063   (goto-char (point-min))
3064   (while (re-search-forward "^ +" nil t)
3065     (replace-match "" t t))
3066   (goto-char (point-min))
3067   (when gnus-simplify-subject-fuzzy-regexp
3068     (if (listp gnus-simplify-subject-fuzzy-regexp)
3069         (let ((list gnus-simplify-subject-fuzzy-regexp))
3070           (while list
3071             (goto-char (point-min))
3072             (while (re-search-forward (car list) nil t)
3073               (replace-match "" t t))
3074             (setq list (cdr list))))
3075       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3076         (replace-match "" t t)))))
3077
3078 (defun gnus-simplify-subject-fuzzy (subject)
3079   "Siplify a subject string fuzzily."
3080   (save-excursion
3081     (gnus-set-work-buffer)
3082     (let ((case-fold-search t))
3083       (insert subject)
3084       (inline (gnus-simplify-buffer-fuzzy))
3085       (buffer-string))))
3086
3087 ;; Add the current buffer to the list of buffers to be killed on exit.
3088 (defun gnus-add-current-to-buffer-list ()
3089   (or (memq (current-buffer) gnus-buffer-list)
3090       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3091
3092 (defun gnus-string> (s1 s2)
3093   (not (or (string< s1 s2)
3094            (string= s1 s2))))
3095
3096 ;;; General various misc type functions.
3097
3098 (defun gnus-clear-system ()
3099   "Clear all variables and buffers."
3100   ;; Clear Gnus variables.
3101   (let ((variables gnus-variable-list))
3102     (while variables
3103       (set (car variables) nil)
3104       (setq variables (cdr variables))))
3105   ;; Clear other internal variables.
3106   (setq gnus-list-of-killed-groups nil
3107         gnus-have-read-active-file nil
3108         gnus-newsrc-alist nil
3109         gnus-newsrc-hashtb nil
3110         gnus-killed-list nil
3111         gnus-zombie-list nil
3112         gnus-killed-hashtb nil
3113         gnus-active-hashtb nil
3114         gnus-moderated-list nil
3115         gnus-description-hashtb nil
3116         gnus-current-headers nil
3117         gnus-thread-indent-array nil
3118         gnus-newsgroup-headers nil
3119         gnus-newsgroup-name nil
3120         gnus-server-alist nil
3121         gnus-group-list-mode nil
3122         gnus-opened-servers nil
3123         gnus-current-select-method nil)
3124   (gnus-shutdown 'gnus)
3125   ;; Kill the startup file.
3126   (and gnus-current-startup-file
3127        (get-file-buffer gnus-current-startup-file)
3128        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3129   ;; Clear the dribble buffer.
3130   (gnus-dribble-clear)
3131   ;; Kill global KILL file buffer.
3132   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3133     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3134   (gnus-kill-buffer nntp-server-buffer)
3135   ;; Kill Gnus buffers.
3136   (while gnus-buffer-list
3137     (gnus-kill-buffer (pop gnus-buffer-list)))
3138   ;; Remove Gnus frames.
3139   (while gnus-created-frames
3140     (when (frame-live-p (car gnus-created-frames))
3141       ;; We slap a condition-case around this `delete-frame' to ensure 
3142       ;; agains errors if we try do delete the single frame that's left.
3143       (condition-case ()
3144           (delete-frame (car gnus-created-frames))
3145         (error nil)))
3146     (pop gnus-created-frames)))
3147
3148 (defun gnus-windows-old-to-new (setting)
3149   ;; First we take care of the really, really old Gnus 3 actions.
3150   (when (symbolp setting)
3151     (setq setting
3152           ;; Take care of ooold GNUS 3.x values.
3153           (cond ((eq setting 'SelectArticle) 'article)
3154                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3155                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3156                 (t setting))))
3157   (if (or (listp setting)
3158           (not (and gnus-window-configuration
3159                     (memq setting '(group summary article)))))
3160       setting
3161     (let* ((setting (if (eq setting 'group)
3162                         (if (assq 'newsgroup gnus-window-configuration)
3163                             'newsgroup
3164                           'newsgroups) setting))
3165            (elem (cadr (assq setting gnus-window-configuration)))
3166            (total (apply '+ elem))
3167            (types '(group summary article))
3168            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3169            (i 0)
3170            perc
3171            out)
3172       (while (< i 3)
3173         (or (not (numberp (nth i elem)))
3174             (zerop (nth i elem))
3175             (progn
3176               (setq perc  (/ (float (nth 0 elem)) total))
3177               (setq out (cons (if (eq pbuf (nth i types))
3178                                   (vector (nth i types) perc 'point)
3179                                 (vector (nth i types) perc))
3180                               out))))
3181         (setq i (1+ i)))
3182       (list (nreverse out)))))
3183
3184 ;;;###autoload
3185 (defun gnus-add-configuration (conf)
3186   "Add the window configuration CONF to `gnus-buffer-configuration'."
3187   (setq gnus-buffer-configuration
3188         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3189                          gnus-buffer-configuration))))
3190
3191 (defvar gnus-frame-list nil)
3192
3193 (defun gnus-configure-frame (split &optional window)
3194   "Split WINDOW according to SPLIT."
3195   (unless window
3196     (setq window (get-buffer-window (current-buffer))))
3197   (select-window window)
3198   ;; This might be an old-stylee buffer config.
3199   (when (vectorp split)
3200     (setq split (append split nil)))
3201   (when (or (consp (car split))
3202             (vectorp (car split)))
3203     (push 1.0 split)
3204     (push 'vertical split))
3205   ;; The SPLIT might be something that is to be evaled to
3206   ;; return a new SPLIT.
3207   (while (and (not (assq (car split) gnus-window-to-buffer))
3208               (gnus-functionp (car split)))
3209     (setq split (eval split)))
3210   (let* ((type (car split))
3211          (subs (cddr split))
3212          (len (if (eq type 'horizontal) (window-width) (window-height)))
3213          (total 0)
3214          (window-min-width (or gnus-window-min-width window-min-width))
3215          (window-min-height (or gnus-window-min-height window-min-height))
3216          s result new-win rest comp-subs size sub)
3217     (cond
3218      ;; Nothing to do here.
3219      ((null split))
3220      ;; Don't switch buffers.
3221      ((null type)
3222       (and (memq 'point split) window))
3223      ;; This is a buffer to be selected.
3224      ((not (memq type '(frame horizontal vertical)))
3225       (let ((buffer (cond ((stringp type) type)
3226                           (t (cdr (assq type gnus-window-to-buffer)))))
3227             buf)
3228         (unless buffer
3229           (error "Illegal buffer type: %s" type))
3230         (unless (setq buf (get-buffer (if (symbolp buffer)
3231                                           (symbol-value buffer) buffer)))
3232           (setq buf (get-buffer-create (if (symbolp buffer)
3233                                            (symbol-value buffer) buffer))))
3234         (switch-to-buffer buf)
3235         ;; We return the window if it has the `point' spec.
3236         (and (memq 'point split) window)))
3237      ;; This is a frame split.
3238      ((eq type 'frame)
3239       (unless gnus-frame-list
3240         (setq gnus-frame-list (list (window-frame
3241                                      (get-buffer-window (current-buffer))))))
3242       (let ((i 0)
3243             params frame fresult)
3244         (while (< i (length subs))
3245           ;; Frame parameter is gotten from the sub-split.
3246           (setq params (cadr (elt subs i)))
3247           ;; It should be a list.
3248           (unless (listp params)
3249             (setq params nil))
3250           ;; Create a new frame?
3251           (unless (setq frame (elt gnus-frame-list i))
3252             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3253             (push frame gnus-created-frames))
3254           ;; Is the old frame still alive?
3255           (unless (frame-live-p frame)
3256             (setcar (nthcdr i gnus-frame-list)
3257                     (setq frame (make-frame params))))
3258           ;; Select the frame in question and do more splits there.
3259           (select-frame frame)
3260           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3261           (incf i))
3262         ;; Select the frame that has the selected buffer.
3263         (when fresult
3264           (select-frame (window-frame fresult)))))
3265      ;; This is a normal split.
3266      (t
3267       (when (> (length subs) 0)
3268         ;; First we have to compute the sizes of all new windows.
3269         (while subs
3270           (setq sub (append (pop subs) nil))
3271           (while (and (not (assq (car sub) gnus-window-to-buffer))
3272                       (gnus-functionp (car sub)))
3273             (setq sub (eval sub)))
3274           (when sub
3275             (push sub comp-subs)
3276             (setq size (cadar comp-subs))
3277             (cond ((equal size 1.0)
3278                    (setq rest (car comp-subs))
3279                    (setq s 0))
3280                   ((floatp size)
3281                    (setq s (floor (* size len))))
3282                   ((integerp size)
3283                    (setq s size))
3284                   (t
3285                    (error "Illegal size: %s" size)))
3286             ;; Try to make sure that we are inside the safe limits.
3287             (cond ((zerop s))
3288                   ((eq type 'horizontal)
3289                    (setq s (max s window-min-width)))
3290                   ((eq type 'vertical)
3291                    (setq s (max s window-min-height))))
3292             (setcar (cdar comp-subs) s)
3293             (incf total s)))
3294         ;; Take care of the "1.0" spec.
3295         (if rest
3296             (setcar (cdr rest) (- len total))
3297           (error "No 1.0 specs in %s" split))
3298         ;; The we do the actual splitting in a nice recursive
3299         ;; fashion.
3300         (setq comp-subs (nreverse comp-subs))
3301         (while comp-subs
3302           (if (null (cdr comp-subs))
3303               (setq new-win window)
3304             (setq new-win
3305                   (split-window window (cadar comp-subs)
3306                                 (eq type 'horizontal))))
3307           (setq result (or (gnus-configure-frame
3308                             (car comp-subs) window) result))
3309           (select-window new-win)
3310           (setq window new-win)
3311           (setq comp-subs (cdr comp-subs))))
3312       ;; Return the proper window, if any.
3313       (when result
3314         (select-window result))))))
3315
3316 (defvar gnus-frame-split-p nil)
3317
3318 (defun gnus-configure-windows (setting &optional force)
3319   (setq setting (gnus-windows-old-to-new setting))
3320   (let ((split (if (symbolp setting)
3321                    (cadr (assq setting gnus-buffer-configuration))
3322                  setting))
3323         all-visible)
3324
3325     (setq gnus-frame-split-p nil)
3326
3327     (unless split
3328       (error "No such setting: %s" setting))
3329
3330     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3331              (not force))
3332         ;; All the windows mentioned are already visible, so we just
3333         ;; put point in the assigned buffer, and do not touch the
3334         ;; winconf.
3335         (select-window all-visible)
3336
3337       ;; Either remove all windows or just remove all Gnus windows.
3338       (let ((frame (selected-frame)))
3339         (unwind-protect
3340             (if gnus-use-full-window
3341                 ;; We want to remove all other windows.
3342                 (if (not gnus-frame-split-p)
3343                     ;; This is not a `frame' split, so we ignore the
3344                     ;; other frames.  
3345                     (delete-other-windows)
3346                   ;; This is a `frame' split, so we delete all windows
3347                   ;; on all frames.
3348                   (mapcar 
3349                    (lambda (frame)
3350                      (unless (eq (cdr (assq 'minibuffer
3351                                             (frame-parameters frame)))
3352                                  'only)
3353                        (select-frame frame)
3354                        (delete-other-windows)))
3355                    (frame-list)))
3356               ;; Just remove some windows.
3357               (gnus-remove-some-windows)
3358               (switch-to-buffer nntp-server-buffer))
3359           (select-frame frame)))
3360
3361       (switch-to-buffer nntp-server-buffer)
3362       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3363
3364 (defun gnus-all-windows-visible-p (split)
3365   "Say whether all buffers in SPLIT are currently visible.
3366 In particular, the value returned will be the window that
3367 should have point."
3368   (let ((stack (list split))
3369         (all-visible t)
3370         type buffer win buf)
3371     (while (and (setq split (pop stack))
3372                 all-visible)
3373       ;; Be backwards compatible.
3374       (when (vectorp split)
3375         (setq split (append split nil)))
3376       (when (or (consp (car split))
3377                 (vectorp (car split)))
3378         (push 1.0 split)
3379         (push 'vertical split))
3380       ;; The SPLIT might be something that is to be evaled to
3381       ;; return a new SPLIT.
3382       (while (and (not (assq (car split) gnus-window-to-buffer))
3383                   (gnus-functionp (car split)))
3384         (setq split (eval split)))
3385
3386       (setq type (elt split 0))
3387       (cond
3388        ;; Nothing here.
3389        ((null split) t)
3390        ;; A buffer.
3391        ((not (memq type '(horizontal vertical frame)))
3392         (setq buffer (cond ((stringp type) type)
3393                            (t (cdr (assq type gnus-window-to-buffer)))))
3394         (unless buffer
3395           (error "Illegal buffer type: %s" type))
3396         (when (setq buf (get-buffer (if (symbolp buffer)
3397                                         (symbol-value buffer)
3398                                       buffer)))
3399           (setq win (get-buffer-window buf t)))
3400         (if win
3401             (when (memq 'point split)
3402                 (setq all-visible win))
3403           (setq all-visible nil)))
3404        (t
3405         (when (eq type 'frame)
3406           (setq gnus-frame-split-p t))
3407         (setq stack (append (cddr split) stack)))))
3408     (unless (eq all-visible t)
3409       all-visible)))
3410
3411 (defun gnus-window-top-edge (&optional window)
3412   (nth 1 (window-edges window)))
3413
3414 (defun gnus-remove-some-windows ()
3415   (let ((buffers gnus-window-to-buffer)
3416         buf bufs lowest-buf lowest)
3417     (save-excursion
3418       ;; Remove windows on all known Gnus buffers.
3419       (while buffers
3420         (setq buf (cdar buffers))
3421         (if (symbolp buf)
3422             (setq buf (and (boundp buf) (symbol-value buf))))
3423         (and buf
3424              (get-buffer-window buf)
3425              (progn
3426                (setq bufs (cons buf bufs))
3427                (pop-to-buffer buf)
3428                (if (or (not lowest)
3429                        (< (gnus-window-top-edge) lowest))
3430                    (progn
3431                      (setq lowest (gnus-window-top-edge))
3432                      (setq lowest-buf buf)))))
3433         (setq buffers (cdr buffers)))
3434       ;; Remove windows on *all* summary buffers.
3435       (walk-windows
3436        (lambda (win)
3437          (let ((buf (window-buffer win)))
3438            (if (string-match    "^\\*Summary" (buffer-name buf))
3439                (progn
3440                  (setq bufs (cons buf bufs))
3441                  (pop-to-buffer buf)
3442                  (if (or (not lowest)
3443                          (< (gnus-window-top-edge) lowest))
3444                      (progn
3445                        (setq lowest-buf buf)
3446                        (setq lowest (gnus-window-top-edge)))))))))
3447       (and lowest-buf
3448            (progn
3449              (pop-to-buffer lowest-buf)
3450              (switch-to-buffer nntp-server-buffer)))
3451       (while bufs
3452         (and (not (eq (car bufs) lowest-buf))
3453              (delete-windows-on (car bufs)))
3454         (setq bufs (cdr bufs))))))
3455
3456 (defun gnus-version ()
3457   "Version numbers of this version of Gnus."
3458   (interactive)
3459   (let ((methods gnus-valid-select-methods)
3460         (mess gnus-version)
3461         meth)
3462     ;; Go through all the legal select methods and add their version
3463     ;; numbers to the total version string.  Only the backends that are
3464     ;; currently in use will have their message numbers taken into
3465     ;; consideration.
3466     (while methods
3467       (setq meth (intern (concat (caar methods) "-version")))
3468       (and (boundp meth)
3469            (stringp (symbol-value meth))
3470            (setq mess (concat mess "; " (symbol-value meth))))
3471       (setq methods (cdr methods)))
3472     (gnus-message 2 mess)))
3473
3474 (defun gnus-info-find-node ()
3475   "Find Info documentation of Gnus."
3476   (interactive)
3477   ;; Enlarge info window if needed.
3478   (let ((mode major-mode)
3479         gnus-info-buffer)
3480     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3481     (setq gnus-info-buffer (current-buffer))
3482     (gnus-configure-windows 'info)))
3483
3484 (defun gnus-days-between (date1 date2)
3485   ;; Return the number of days between date1 and date2.
3486   (- (gnus-day-number date1) (gnus-day-number date2)))
3487
3488 (defun gnus-day-number (date)
3489   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3490                      (timezone-parse-date date))))
3491     (timezone-absolute-from-gregorian
3492      (nth 1 dat) (nth 2 dat) (car dat))))
3493
3494 (defun gnus-encode-date (date)
3495   "Convert DATE to internal time."
3496   (let* ((parse (timezone-parse-date date))
3497          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3498          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3499     (encode-time (caddr time) (cadr time) (car time)
3500                  (caddr date) (cadr date) (car date) (nth 4 date))))
3501
3502 (defun gnus-time-minus (t1 t2)
3503   "Subtract two internal times."
3504   (let ((borrow (< (cadr t1) (cadr t2))))
3505     (list (- (car t1) (car t2) (if borrow 1 0))
3506           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3507
3508 (defun gnus-file-newer-than (file date)
3509   (let ((fdate (nth 5 (file-attributes file))))
3510     (or (> (car fdate) (car date))
3511         (and (= (car fdate) (car date))
3512              (> (nth 1 fdate) (nth 1 date))))))
3513
3514 (defmacro gnus-local-set-keys (&rest plist)
3515   "Set the keys in PLIST in the current keymap."
3516   `(gnus-define-keys-1 (current-local-map) ',plist))
3517
3518 (defmacro gnus-define-keys (keymap &rest plist)
3519   "Define all keys in PLIST in KEYMAP."
3520   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3521
3522 (put 'gnus-define-keys 'lisp-indent-function 1)
3523 (put 'gnus-define-keys 'lisp-indent-hook 1)
3524 (put 'gnus-define-keymap 'lisp-indent-function 1)
3525 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3526
3527 (defmacro gnus-define-keymap (keymap &rest plist)
3528   "Define all keys in PLIST in KEYMAP."
3529   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3530
3531 (defun gnus-define-keys-1 (keymap plist)
3532   (when (null keymap)
3533     (error "Can't set keys in a null keymap"))
3534   (cond ((symbolp keymap)
3535          (setq keymap (symbol-value keymap)))
3536         ((keymapp keymap))
3537         ((listp keymap)
3538          (set (car keymap) nil)
3539          (define-prefix-command (car keymap))
3540          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3541          (setq keymap (symbol-value (car keymap)))))
3542   (let (key)
3543     (while plist
3544       (when (symbolp (setq key (pop plist)))
3545         (setq key (symbol-value key)))
3546       (define-key keymap key (pop plist)))))
3547
3548 (defun gnus-group-read-only-p (&optional group)
3549   "Check whether GROUP supports editing or not.
3550 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3551 that that variable is buffer-local to the summary buffers."
3552   (let ((group (or group gnus-newsgroup-name)))
3553     (not (gnus-check-backend-function 'request-replace-article group))))
3554
3555 (defun gnus-group-total-expirable-p (group)
3556   "Check whether GROUP is total-expirable or not."
3557   (let ((params (gnus-info-params (gnus-get-info group))))
3558     (or (memq 'total-expire params)
3559         (cdr (assq 'total-expire params)) ; (total-expire . t)
3560         (and gnus-total-expirable-newsgroups ; Check var.
3561              (string-match gnus-total-expirable-newsgroups group)))))
3562
3563 (defun gnus-group-auto-expirable-p (group)
3564   "Check whether GROUP is total-expirable or not."
3565   (let ((params (gnus-info-params (gnus-get-info group))))
3566     (or (memq 'auto-expire params)
3567         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3568         (and gnus-auto-expirable-newsgroups ; Check var.
3569              (string-match gnus-auto-expirable-newsgroups group)))))
3570
3571 (defun gnus-virtual-group-p (group)
3572   "Say whether GROUP is virtual or not."
3573   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3574                         gnus-valid-select-methods)))
3575
3576 (defsubst gnus-simplify-subject-fully (subject)
3577   "Simplify a subject string according to the user's wishes."
3578   (cond
3579    ((null gnus-summary-gather-subject-limit)
3580     (gnus-simplify-subject-re subject))
3581    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3582     (gnus-simplify-subject-fuzzy subject))
3583    ((numberp gnus-summary-gather-subject-limit)
3584     (gnus-limit-string (gnus-simplify-subject-re subject)
3585                        gnus-summary-gather-subject-limit))
3586    (t
3587     subject)))
3588
3589 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3590   "Check whether two subjects are equal.  If optional argument
3591 simple-first is t, first argument is already simplified."
3592   (cond
3593    ((null simple-first)
3594     (equal (gnus-simplify-subject-fully s1)
3595            (gnus-simplify-subject-fully s2)))
3596    (t
3597     (equal s1
3598            (gnus-simplify-subject-fully s2)))))
3599
3600 ;; Returns a list of writable groups.
3601 (defun gnus-writable-groups ()
3602   (let ((alist gnus-newsrc-alist)
3603         groups group)
3604     (while (setq group (car (pop alist)))
3605       (unless (gnus-group-read-only-p group)
3606         (push group groups)))
3607     (nreverse groups)))
3608
3609 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3610 ;; the echo area.
3611 (defun gnus-y-or-n-p (prompt)
3612   (prog1
3613       (y-or-n-p prompt)
3614     (message "")))
3615
3616 (defun gnus-yes-or-no-p (prompt)
3617   (prog1
3618       (yes-or-no-p prompt)
3619     (message "")))
3620
3621 ;; Check whether to use long file names.
3622 (defun gnus-use-long-file-name (symbol)
3623   ;; The variable has to be set...
3624   (and gnus-use-long-file-name
3625        ;; If it isn't a list, then we return t.
3626        (or (not (listp gnus-use-long-file-name))
3627            ;; If it is a list, and the list contains `symbol', we
3628            ;; return nil.
3629            (not (memq symbol gnus-use-long-file-name)))))
3630
3631 ;; I suspect there's a better way, but I haven't taken the time to do
3632 ;; it yet. -erik selberg@cs.washington.edu
3633 (defun gnus-dd-mmm (messy-date)
3634   "Return a string like DD-MMM from a big messy string"
3635   (let ((datevec (timezone-parse-date messy-date)))
3636     (format "%2s-%s"
3637             (condition-case ()
3638                 ;; Make sure leading zeroes are stripped.
3639                 (number-to-string (string-to-number (aref datevec 2)))
3640               (error "??"))
3641             (capitalize
3642              (or (car
3643                   (nth (1- (string-to-number (aref datevec 1)))
3644                        timezone-months-assoc))
3645                  "???")))))
3646
3647 ;; Make a hash table (default and minimum size is 255).
3648 ;; Optional argument HASHSIZE specifies the table size.
3649 (defun gnus-make-hashtable (&optional hashsize)
3650   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3651
3652 ;; Make a number that is suitable for hashing; bigger than MIN and one
3653 ;; less than 2^x.
3654 (defun gnus-create-hash-size (min)
3655   (let ((i 1))
3656     (while (< i min)
3657       (setq i (* 2 i)))
3658     (1- i)))
3659
3660 ;; Show message if message has a lower level than `gnus-verbose'.
3661 ;; Guideline for numbers:
3662 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3663 ;; for things that take a long time, 7 - not very important messages
3664 ;; on stuff, 9 - messages inside loops.
3665 (defun gnus-message (level &rest args)
3666   (if (<= level gnus-verbose)
3667       (apply 'message args)
3668     ;; We have to do this format thingy here even if the result isn't
3669     ;; shown - the return value has to be the same as the return value
3670     ;; from `message'.
3671     (apply 'format args)))
3672
3673 ;; Generate a unique new group name.
3674 (defun gnus-generate-new-group-name (leaf)
3675   (let ((name leaf)
3676         (num 0))
3677     (while (gnus-gethash name gnus-newsrc-hashtb)
3678       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3679     name))
3680
3681 (defsubst gnus-hide-text (b e props)
3682   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3683   (add-text-properties b e props)
3684   (when (memq 'intangible props)
3685     (put-text-property (max (1- b) (point-min))
3686                        b 'intangible (cddr (memq 'intangible props)))))
3687
3688 (defsubst gnus-unhide-text (b e)
3689   "Remove hidden text properties from region between B and E."
3690   (remove-text-properties b e gnus-hidden-properties)
3691   (when (memq 'intangible gnus-hidden-properties)
3692     (put-text-property (max (1- b) (point-min))
3693                        b 'intangible nil)))
3694
3695 (defun gnus-hide-text-type (b e type)
3696   "Hide text of TYPE between B and E."
3697   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3698
3699 ;; Find out whether the gnus-visual TYPE is wanted.
3700 (defun gnus-visual-p (&optional type class)
3701   (and gnus-visual                      ; Has to be non-nil, at least.
3702        (if (not type)                   ; We don't care about type.
3703            gnus-visual
3704          (if (listp gnus-visual)        ; It's a list, so we check it.
3705              (or (memq type gnus-visual)
3706                  (memq class gnus-visual))
3707            t))))
3708
3709 (defun gnus-parent-id (references)
3710   "Return the last Message-ID in REFERENCES."
3711   (when (and references
3712              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3713     (substring references (match-beginning 1) (match-end 1))))
3714
3715 (defun gnus-split-references (references)
3716   "Return a list of Message-IDs in REFERENCES."
3717   (let ((beg 0)
3718         ids)
3719     (while (string-match "<[^>]+>" references beg)
3720       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3721             ids))
3722     (nreverse ids)))
3723
3724 (defun gnus-buffer-live-p (buffer)
3725   "Say whether BUFFER is alive or not."
3726   (and buffer
3727        (get-buffer buffer)
3728        (buffer-name (get-buffer buffer))))
3729
3730 (defun gnus-ephemeral-group-p (group)
3731   "Say whether GROUP is ephemeral or not."
3732   (gnus-group-get-parameter group 'quit-config))
3733
3734 (defun gnus-group-quit-config (group)
3735   "Return the quit-config of GROUP."
3736   (gnus-group-get-parameter group 'quit-config))
3737
3738 (defun gnus-simplify-mode-line ()
3739   "Make mode lines a bit simpler."
3740   (setq mode-line-modified "-- ")
3741   (when (listp mode-line-format)
3742     (make-local-variable 'mode-line-format)
3743     (setq mode-line-format (copy-sequence mode-line-format))
3744     (when (equal (nth 3 mode-line-format) "   ")
3745       (setcar (nthcdr 3 mode-line-format) " "))))
3746
3747 ;;; List and range functions
3748
3749 (defun gnus-last-element (list)
3750   "Return last element of LIST."
3751   (while (cdr list)
3752     (setq list (cdr list)))
3753   (car list))
3754
3755 (defun gnus-copy-sequence (list)
3756   "Do a complete, total copy of a list."
3757   (if (and (consp list) (not (consp (cdr list))))
3758       (cons (car list) (cdr list))
3759     (mapcar (lambda (elem) (if (consp elem)
3760                                (if (consp (cdr elem))
3761                                    (gnus-copy-sequence elem)
3762                                  (cons (car elem) (cdr elem)))
3763                              elem))
3764             list)))
3765
3766 (defun gnus-set-difference (list1 list2)
3767   "Return a list of elements of LIST1 that do not appear in LIST2."
3768   (let ((list1 (copy-sequence list1)))
3769     (while list2
3770       (setq list1 (delq (car list2) list1))
3771       (setq list2 (cdr list2)))
3772     list1))
3773
3774 (defun gnus-sorted-complement (list1 list2)
3775   "Return a list of elements of LIST1 that do not appear in LIST2.
3776 Both lists have to be sorted over <."
3777   (let (out)
3778     (if (or (null list1) (null list2))
3779         (or list1 list2)
3780       (while (and list1 list2)
3781         (cond ((= (car list1) (car list2))
3782                (setq list1 (cdr list1)
3783                      list2 (cdr list2)))
3784               ((< (car list1) (car list2))
3785                (setq out (cons (car list1) out))
3786                (setq list1 (cdr list1)))
3787               (t
3788                (setq out (cons (car list2) out))
3789                (setq list2 (cdr list2)))))
3790       (nconc (nreverse out) (or list1 list2)))))
3791
3792 (defun gnus-intersection (list1 list2)
3793   (let ((result nil))
3794     (while list2
3795       (if (memq (car list2) list1)
3796           (setq result (cons (car list2) result)))
3797       (setq list2 (cdr list2)))
3798     result))
3799
3800 (defun gnus-sorted-intersection (list1 list2)
3801   ;; LIST1 and LIST2 have to be sorted over <.
3802   (let (out)
3803     (while (and list1 list2)
3804       (cond ((= (car list1) (car list2))
3805              (setq out (cons (car list1) out)
3806                    list1 (cdr list1)
3807                    list2 (cdr list2)))
3808             ((< (car list1) (car list2))
3809              (setq list1 (cdr list1)))
3810             (t
3811              (setq list2 (cdr list2)))))
3812     (nreverse out)))
3813
3814 (defun gnus-set-sorted-intersection (list1 list2)
3815   ;; LIST1 and LIST2 have to be sorted over <.
3816   ;; This function modifies LIST1.
3817   (let* ((top (cons nil list1))
3818          (prev top))
3819     (while (and list1 list2)
3820       (cond ((= (car list1) (car list2))
3821              (setq prev list1
3822                    list1 (cdr list1)
3823                    list2 (cdr list2)))
3824             ((< (car list1) (car list2))
3825              (setcdr prev (cdr list1))
3826              (setq list1 (cdr list1)))
3827             (t
3828              (setq list2 (cdr list2)))))
3829     (setcdr prev nil)
3830     (cdr top)))
3831
3832 (defun gnus-compress-sequence (numbers &optional always-list)
3833   "Convert list of numbers to a list of ranges or a single range.
3834 If ALWAYS-LIST is non-nil, this function will always release a list of
3835 ranges."
3836   (let* ((first (car numbers))
3837          (last (car numbers))
3838          result)
3839     (if (null numbers)
3840         nil
3841       (if (not (listp (cdr numbers)))
3842           numbers
3843         (while numbers
3844           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3845                 ((= (1+ last) (car numbers)) ;Still in sequence
3846                  (setq last (car numbers)))
3847                 (t                      ;End of one sequence
3848                  (setq result
3849                        (cons (if (= first last) first
3850                                (cons first last)) result))
3851                  (setq first (car numbers))
3852                  (setq last  (car numbers))))
3853           (setq numbers (cdr numbers)))
3854         (if (and (not always-list) (null result))
3855             (if (= first last) (list first) (cons first last))
3856           (nreverse (cons (if (= first last) first (cons first last))
3857                           result)))))))
3858
3859 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3860 (defun gnus-uncompress-range (ranges)
3861   "Expand a list of ranges into a list of numbers.
3862 RANGES is either a single range on the form `(num . num)' or a list of
3863 these ranges."
3864   (let (first last result)
3865     (cond
3866      ((null ranges)
3867       nil)
3868      ((not (listp (cdr ranges)))
3869       (setq first (car ranges))
3870       (setq last (cdr ranges))
3871       (while (<= first last)
3872         (setq result (cons first result))
3873         (setq first (1+ first)))
3874       (nreverse result))
3875      (t
3876       (while ranges
3877         (if (atom (car ranges))
3878             (if (numberp (car ranges))
3879                 (setq result (cons (car ranges) result)))
3880           (setq first (caar ranges))
3881           (setq last  (cdar ranges))
3882           (while (<= first last)
3883             (setq result (cons first result))
3884             (setq first (1+ first))))
3885         (setq ranges (cdr ranges)))
3886       (nreverse result)))))
3887
3888 (defun gnus-add-to-range (ranges list)
3889   "Return a list of ranges that has all articles from both RANGES and LIST.
3890 Note: LIST has to be sorted over `<'."
3891   (if (not ranges)
3892       (gnus-compress-sequence list t)
3893     (setq list (copy-sequence list))
3894     (or (listp (cdr ranges))
3895         (setq ranges (list ranges)))
3896     (let ((out ranges)
3897           ilist lowest highest temp)
3898       (while (and ranges list)
3899         (setq ilist list)
3900         (setq lowest (or (and (atom (car ranges)) (car ranges))
3901                          (caar ranges)))
3902         (while (and list (cdr list) (< (cadr list) lowest))
3903           (setq list (cdr list)))
3904         (if (< (car ilist) lowest)
3905             (progn
3906               (setq temp list)
3907               (setq list (cdr list))
3908               (setcdr temp nil)
3909               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3910         (setq highest (or (and (atom (car ranges)) (car ranges))
3911                           (cdar ranges)))
3912         (while (and list (<= (car list) highest))
3913           (setq list (cdr list)))
3914         (setq ranges (cdr ranges)))
3915       (if list
3916           (setq out (nconc (gnus-compress-sequence list t) out)))
3917       (setq out (sort out (lambda (r1 r2)
3918                             (< (or (and (atom r1) r1) (car r1))
3919                                (or (and (atom r2) r2) (car r2))))))
3920       (setq ranges out)
3921       (while ranges
3922         (if (atom (car ranges))
3923             (if (cdr ranges)
3924                 (if (atom (cadr ranges))
3925                     (if (= (1+ (car ranges)) (cadr ranges))
3926                         (progn
3927                           (setcar ranges (cons (car ranges)
3928                                                (cadr ranges)))
3929                           (setcdr ranges (cddr ranges))))
3930                   (if (= (1+ (car ranges)) (caadr ranges))
3931                       (progn
3932                         (setcar (cadr ranges) (car ranges))
3933                         (setcar ranges (cadr ranges))
3934                         (setcdr ranges (cddr ranges))))))
3935           (if (cdr ranges)
3936               (if (atom (cadr ranges))
3937                   (if (= (1+ (cdar ranges)) (cadr ranges))
3938                       (progn
3939                         (setcdr (car ranges) (cadr ranges))
3940                         (setcdr ranges (cddr ranges))))
3941                 (if (= (1+ (cdar ranges)) (caadr ranges))
3942                     (progn
3943                       (setcdr (car ranges) (cdadr ranges))
3944                       (setcdr ranges (cddr ranges)))))))
3945         (setq ranges (cdr ranges)))
3946       out)))
3947
3948 (defun gnus-remove-from-range (ranges list)
3949   "Return a list of ranges that has all articles from LIST removed from RANGES.
3950 Note: LIST has to be sorted over `<'."
3951   ;; !!! This function shouldn't look like this, but I've got a headache.
3952   (gnus-compress-sequence
3953    (gnus-sorted-complement
3954     (gnus-uncompress-range ranges) list)))
3955
3956 (defun gnus-member-of-range (number ranges)
3957   (if (not (listp (cdr ranges)))
3958       (and (>= number (car ranges))
3959            (<= number (cdr ranges)))
3960     (let ((not-stop t))
3961       (while (and ranges
3962                   (if (numberp (car ranges))
3963                       (>= number (car ranges))
3964                     (>= number (caar ranges)))
3965                   not-stop)
3966         (if (if (numberp (car ranges))
3967                 (= number (car ranges))
3968               (and (>= number (caar ranges))
3969                    (<= number (cdar ranges))))
3970             (setq not-stop nil))
3971         (setq ranges (cdr ranges)))
3972       (not not-stop))))
3973
3974 (defun gnus-range-length (range)
3975   "Return the length RANGE would have if uncompressed."
3976   (length (gnus-uncompress-range range)))
3977
3978 (defun gnus-sublist-p (list sublist)
3979   "Test whether all elements in SUBLIST are members of LIST."
3980   (let ((sublistp t))
3981     (while sublist
3982       (unless (memq (pop sublist) list)
3983         (setq sublistp nil
3984               sublist nil)))
3985     sublistp))
3986
3987 \f
3988 ;;;
3989 ;;; Gnus group mode
3990 ;;;
3991
3992 (defvar gnus-group-mode-map nil)
3993 (put 'gnus-group-mode 'mode-class 'special)
3994
3995 (unless gnus-group-mode-map
3996   (setq gnus-group-mode-map (make-keymap))
3997   (suppress-keymap gnus-group-mode-map)
3998
3999   (gnus-define-keys gnus-group-mode-map
4000     " " gnus-group-read-group
4001     "=" gnus-group-select-group
4002     "\r" gnus-group-select-group
4003     "\M-\r" gnus-group-quick-select-group
4004     "j" gnus-group-jump-to-group
4005     "n" gnus-group-next-unread-group
4006     "p" gnus-group-prev-unread-group
4007     "\177" gnus-group-prev-unread-group
4008     [delete] gnus-group-prev-unread-group
4009     "N" gnus-group-next-group
4010     "P" gnus-group-prev-group
4011     "\M-n" gnus-group-next-unread-group-same-level
4012     "\M-p" gnus-group-prev-unread-group-same-level
4013     "," gnus-group-best-unread-group
4014     "." gnus-group-first-unread-group
4015     "u" gnus-group-unsubscribe-current-group
4016     "U" gnus-group-unsubscribe-group
4017     "c" gnus-group-catchup-current
4018     "C" gnus-group-catchup-current-all
4019     "l" gnus-group-list-groups
4020     "L" gnus-group-list-all-groups
4021     "m" gnus-group-mail
4022     "g" gnus-group-get-new-news
4023     "\M-g" gnus-group-get-new-news-this-group
4024     "R" gnus-group-restart
4025     "r" gnus-group-read-init-file
4026     "B" gnus-group-browse-foreign-server
4027     "b" gnus-group-check-bogus-groups
4028     "F" gnus-find-new-newsgroups
4029     "\C-c\C-d" gnus-group-describe-group
4030     "\M-d" gnus-group-describe-all-groups
4031     "\C-c\C-a" gnus-group-apropos
4032     "\C-c\M-\C-a" gnus-group-description-apropos
4033     "a" gnus-group-post-news
4034     "\ek" gnus-group-edit-local-kill
4035     "\eK" gnus-group-edit-global-kill
4036     "\C-k" gnus-group-kill-group
4037     "\C-y" gnus-group-yank-group
4038     "\C-w" gnus-group-kill-region
4039     "\C-x\C-t" gnus-group-transpose-groups
4040     "\C-c\C-l" gnus-group-list-killed
4041     "\C-c\C-x" gnus-group-expire-articles
4042     "\C-c\M-\C-x" gnus-group-expire-all-groups
4043     "V" gnus-version
4044     "s" gnus-group-save-newsrc
4045     "z" gnus-group-suspend
4046     "Z" gnus-group-clear-dribble
4047     "q" gnus-group-exit
4048     "Q" gnus-group-quit
4049     "?" gnus-group-describe-briefly
4050     "\C-c\C-i" gnus-info-find-node
4051     "\M-e" gnus-group-edit-group-method
4052     "^" gnus-group-enter-server-mode
4053     gnus-mouse-2 gnus-mouse-pick-group
4054     "<" beginning-of-buffer
4055     ">" end-of-buffer
4056     "\C-c\C-b" gnus-bug
4057     "\C-c\C-s" gnus-group-sort-groups
4058     "t" gnus-topic-mode
4059     "\C-c\M-g" gnus-activate-all-groups
4060     "\M-&" gnus-group-universal-argument
4061     "#" gnus-group-mark-group
4062     "\M-#" gnus-group-unmark-group)
4063
4064   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4065     "m" gnus-group-mark-group
4066     "u" gnus-group-unmark-group
4067     "w" gnus-group-mark-region
4068     "m" gnus-group-mark-buffer
4069     "r" gnus-group-mark-regexp
4070     "U" gnus-group-unmark-all-groups)
4071
4072   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4073     "d" gnus-group-make-directory-group
4074     "h" gnus-group-make-help-group
4075     "a" gnus-group-make-archive-group
4076     "k" gnus-group-make-kiboze-group
4077     "m" gnus-group-make-group
4078     "E" gnus-group-edit-group
4079     "e" gnus-group-edit-group-method
4080     "p" gnus-group-edit-group-parameters
4081     "v" gnus-group-add-to-virtual
4082     "V" gnus-group-make-empty-virtual
4083     "D" gnus-group-enter-directory
4084     "f" gnus-group-make-doc-group
4085     "r" gnus-group-rename-group
4086     "\177" gnus-group-delete-group
4087     [delete] gnus-group-delete-group)
4088
4089    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4090      "b" gnus-group-brew-soup
4091      "w" gnus-soup-save-areas
4092      "s" gnus-soup-send-replies
4093      "p" gnus-soup-pack-packet
4094      "r" nnsoup-pack-replies)
4095
4096    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4097      "s" gnus-group-sort-groups
4098      "a" gnus-group-sort-groups-by-alphabet
4099      "u" gnus-group-sort-groups-by-unread
4100      "l" gnus-group-sort-groups-by-level
4101      "v" gnus-group-sort-groups-by-score
4102      "r" gnus-group-sort-groups-by-rank
4103      "m" gnus-group-sort-groups-by-method)
4104
4105    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4106      "k" gnus-group-list-killed
4107      "z" gnus-group-list-zombies
4108      "s" gnus-group-list-groups
4109      "u" gnus-group-list-all-groups
4110      "A" gnus-group-list-active
4111      "a" gnus-group-apropos
4112      "d" gnus-group-description-apropos
4113      "m" gnus-group-list-matching
4114      "M" gnus-group-list-all-matching
4115      "l" gnus-group-list-level)
4116
4117    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4118      "f" gnus-score-flush-cache)
4119
4120    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4121      "f" gnus-group-fetch-faq)
4122
4123    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4124      "l" gnus-group-set-current-level
4125      "t" gnus-group-unsubscribe-current-group
4126      "s" gnus-group-unsubscribe-group
4127      "k" gnus-group-kill-group
4128      "y" gnus-group-yank-group
4129      "w" gnus-group-kill-region
4130      "\C-k" gnus-group-kill-level
4131      "z" gnus-group-kill-all-zombies))
4132
4133 (defun gnus-group-mode ()
4134   "Major mode for reading news.
4135
4136 All normal editing commands are switched off.
4137 \\<gnus-group-mode-map>
4138 The group buffer lists (some of) the groups available.  For instance,
4139 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4140 lists all zombie groups.
4141
4142 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4143 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4144
4145 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4146
4147 The following commands are available:
4148
4149 \\{gnus-group-mode-map}"
4150   (interactive)
4151   (when (and menu-bar-mode
4152              (gnus-visual-p 'group-menu 'menu))
4153     (gnus-group-make-menu-bar))
4154   (kill-all-local-variables)
4155   (gnus-simplify-mode-line)
4156   (setq major-mode 'gnus-group-mode)
4157   (setq mode-name "Group")
4158   (gnus-group-set-mode-line)
4159   (setq mode-line-process nil)
4160   (use-local-map gnus-group-mode-map)
4161   (buffer-disable-undo (current-buffer))
4162   (setq truncate-lines t)
4163   (setq buffer-read-only t)
4164   (run-hooks 'gnus-group-mode-hook))
4165
4166 (defun gnus-mouse-pick-group (e)
4167   "Enter the group under the mouse pointer."
4168   (interactive "e")
4169   (mouse-set-point e)
4170   (gnus-group-read-group nil))
4171
4172 ;; Look at LEVEL and find out what the level is really supposed to be.
4173 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4174 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4175 (defun gnus-group-default-level (&optional level number-or-nil)
4176   (cond
4177    (gnus-group-use-permanent-levels
4178     (setq gnus-group-default-list-level
4179           (or level gnus-group-default-list-level))
4180     (or gnus-group-default-list-level gnus-level-subscribed))
4181    (number-or-nil
4182     level)
4183    (t
4184     (or level gnus-group-default-list-level gnus-level-subscribed))))
4185
4186 ;;;###autoload
4187 (defun gnus-slave-no-server (&optional arg)
4188   "Read network news as a slave, without connecting to local server"
4189   (interactive "P")
4190   (gnus-no-server arg t))
4191
4192 ;;;###autoload
4193 (defun gnus-no-server (&optional arg slave)
4194   "Read network news.
4195 If ARG is a positive number, Gnus will use that as the
4196 startup level.  If ARG is nil, Gnus will be started at level 2.
4197 If ARG is non-nil and not a positive number, Gnus will
4198 prompt the user for the name of an NNTP server to use.
4199 As opposed to `gnus', this command will not connect to the local server."
4200   (interactive "P")
4201   (let ((gnus-group-use-permanent-levels t))
4202     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4203   (make-local-variable 'gnus-group-use-permanent-levels)
4204   (setq gnus-group-use-permanent-levels t))
4205
4206 ;;;###autoload
4207 (defun gnus-slave (&optional arg)
4208   "Read news as a slave."
4209   (interactive "P")
4210   (gnus arg nil 'slave))
4211
4212 ;;;###autoload
4213 (defun gnus-other-frame (&optional arg)
4214   "Pop up a frame to read news."
4215   (interactive "P")
4216   (if (get-buffer gnus-group-buffer)
4217       (let ((pop-up-frames t))
4218         (gnus arg))
4219     (select-frame (make-frame))
4220     (gnus arg)))
4221
4222 ;;;###autoload
4223 (defun gnus (&optional arg dont-connect slave)
4224   "Read network news.
4225 If ARG is non-nil and a positive number, Gnus will use that as the
4226 startup level.  If ARG is non-nil and not a positive number, Gnus will
4227 prompt the user for the name of an NNTP server to use."
4228   (interactive "P")
4229
4230   (if (get-buffer gnus-group-buffer)
4231       (progn
4232         (switch-to-buffer gnus-group-buffer)
4233         (gnus-group-get-new-news))
4234
4235     (gnus-clear-system)
4236     (nnheader-init-server-buffer)
4237     (gnus-read-init-file)
4238     (setq gnus-slave slave)
4239
4240     (gnus-group-setup-buffer)
4241     (let ((buffer-read-only nil))
4242       (erase-buffer)
4243       (if (not gnus-inhibit-startup-message)
4244           (progn
4245             (gnus-group-startup-message)
4246             (sit-for 0))))
4247
4248     (let ((level (and (numberp arg) (> arg 0) arg))
4249           did-connect)
4250       (unwind-protect
4251           (progn
4252             (or dont-connect
4253                 (setq did-connect
4254                       (gnus-start-news-server (and arg (not level))))))
4255         (if (and (not dont-connect)
4256                  (not did-connect))
4257             (gnus-group-quit)
4258           (run-hooks 'gnus-startup-hook)
4259           ;; NNTP server is successfully open.
4260
4261           ;; Find the current startup file name.
4262           (setq gnus-current-startup-file
4263                 (gnus-make-newsrc-file gnus-startup-file))
4264
4265           ;; Read the dribble file.
4266           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4267
4268           ;; Allow using GroupLens predictions.
4269           (when gnus-use-grouplens
4270             (bbb-login)
4271             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4272
4273           (gnus-summary-make-display-table)
4274           ;; Do the actual startup.
4275           (gnus-setup-news nil level dont-connect)
4276           ;; Generate the group buffer.
4277           (gnus-group-list-groups level)
4278           (gnus-group-first-unread-group)
4279           (gnus-configure-windows 'group)
4280           (gnus-group-set-mode-line))))))
4281
4282 (defun gnus-unload ()
4283   "Unload all Gnus features."
4284   (interactive)
4285   (or (boundp 'load-history)
4286       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4287   (let ((history load-history)
4288         feature)
4289     (while history
4290       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4291            (setq feature (cdr (assq 'provide (car history))))
4292            (unload-feature feature 'force))
4293       (setq history (cdr history)))))
4294
4295 (defun gnus-compile ()
4296   "Byte-compile the user-defined format specs."
4297   (interactive)
4298   (let ((entries gnus-format-specs)
4299         entry gnus-tmp-func)
4300     (save-excursion
4301       (gnus-message 7 "Compiling format specs...")
4302
4303       (while entries
4304         (setq entry (pop entries))
4305         (if (eq (car entry) 'version)
4306             (setq gnus-format-specs (delq entry gnus-format-specs))
4307           (when (and (listp (caddr entry))
4308                      (not (eq 'byte-code (caaddr entry))))
4309             (fset 'gnus-tmp-func
4310                   `(lambda () ,(caddr entry)))
4311             (byte-compile 'gnus-tmp-func)
4312             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4313
4314       (push (cons 'version emacs-version) gnus-format-specs)
4315
4316       (gnus-message 7 "Compiling user specs...done"))))
4317
4318 (defun gnus-indent-rigidly (start end arg)
4319   "Indent rigidly using only spaces and no tabs."
4320   (save-excursion
4321     (save-restriction
4322       (narrow-to-region start end)
4323       (indent-rigidly start end arg)
4324       (goto-char (point-min))
4325       (while (search-forward "\t" nil t)
4326         (replace-match "        " t t)))))
4327
4328 (defun gnus-group-startup-message (&optional x y)
4329   "Insert startup message in current buffer."
4330   ;; Insert the message.
4331   (erase-buffer)
4332   (insert
4333    (format "              %s
4334           _    ___ _             _
4335           _ ___ __ ___  __    _ ___
4336           __   _     ___    __  ___
4337               _           ___     _
4338              _  _ __             _
4339              ___   __            _
4340                    __           _
4341                     _      _   _
4342                    _      _    _
4343                       _  _    _
4344                   __  ___
4345                  _   _ _     _
4346                 _   _
4347               _    _
4348              _    _
4349             _
4350           __
4351
4352 "
4353            ""))
4354   ;; And then hack it.
4355   (gnus-indent-rigidly (point-min) (point-max)
4356                        (/ (max (- (window-width) (or x 46)) 0) 2))
4357   (goto-char (point-min))
4358   (forward-line 1)
4359   (let* ((pheight (count-lines (point-min) (point-max)))
4360          (wheight (window-height))
4361          (rest (- wheight pheight)))
4362     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4363   ;; Fontify some.
4364   (goto-char (point-min))
4365   (and (search-forward "Praxis" nil t)
4366        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4367   (goto-char (point-min))
4368   (let* ((mode-string (gnus-group-set-mode-line)))
4369     (setq mode-line-buffer-identification
4370           (list (concat gnus-version (substring (car mode-string) 4))))
4371     (set-buffer-modified-p t)))
4372
4373 (defun gnus-group-setup-buffer ()
4374   (or (get-buffer gnus-group-buffer)
4375       (progn
4376         (switch-to-buffer gnus-group-buffer)
4377         (gnus-add-current-to-buffer-list)
4378         (gnus-group-mode)
4379         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4380
4381 (defun gnus-group-list-groups (&optional level unread lowest)
4382   "List newsgroups with level LEVEL or lower that have unread articles.
4383 Default is all subscribed groups.
4384 If argument UNREAD is non-nil, groups with no unread articles are also
4385 listed."
4386   (interactive (list (if current-prefix-arg
4387                          (prefix-numeric-value current-prefix-arg)
4388                        (or
4389                         (gnus-group-default-level nil t)
4390                         gnus-group-default-list-level
4391                         gnus-level-subscribed))))
4392   (or level
4393       (setq level (car gnus-group-list-mode)
4394             unread (cdr gnus-group-list-mode)))
4395   (setq level (gnus-group-default-level level))
4396   (gnus-group-setup-buffer)             ;May call from out of group buffer
4397   (gnus-update-format-specifications)
4398   (let ((case-fold-search nil)
4399         (props (text-properties-at (gnus-point-at-bol)))
4400         (group (gnus-group-group-name)))
4401     (set-buffer gnus-group-buffer)
4402     (funcall gnus-group-prepare-function level unread lowest)
4403     (if (zerop (buffer-size))
4404         (gnus-message 5 gnus-no-groups-message)
4405       (goto-char (point-max))
4406       (when (or (not gnus-group-goto-next-group-function)
4407                 (not (funcall gnus-group-goto-next-group-function 
4408                               group props)))
4409         (if (not group)
4410             ;; Go to the first group with unread articles.
4411             (gnus-group-search-forward t)
4412           ;; Find the right group to put point on.  If the current group
4413           ;; has disappeared in the new listing, try to find the next
4414           ;; one.        If no next one can be found, just leave point at the
4415           ;; first newsgroup in the buffer.
4416           (if (not (gnus-goto-char
4417                     (text-property-any
4418                      (point-min) (point-max)
4419                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4420               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4421                 (while (and newsrc
4422                             (not (gnus-goto-char
4423                                   (text-property-any
4424                                    (point-min) (point-max) 'gnus-group
4425                                    (gnus-intern-safe
4426                                     (caar newsrc) gnus-active-hashtb)))))
4427                   (setq newsrc (cdr newsrc)))
4428                 (or newsrc (progn (goto-char (point-max))
4429                                   (forward-line -1)))))))
4430       ;; Adjust cursor point.
4431       (gnus-group-position-point))))
4432
4433 (defun gnus-group-list-level (level &optional all)
4434   "List groups on LEVEL.
4435 If ALL (the prefix), also list groups that have no unread articles."
4436   (interactive "nList groups on level: \nP")
4437   (gnus-group-list-groups level all level))
4438
4439 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4440   "List all newsgroups with unread articles of level LEVEL or lower.
4441 If ALL is non-nil, list groups that have no unread articles.
4442 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4443 If REGEXP, only list groups matching REGEXP."
4444   (set-buffer gnus-group-buffer)
4445   (let ((buffer-read-only nil)
4446         (newsrc (cdr gnus-newsrc-alist))
4447         (lowest (or lowest 1))
4448         info clevel unread group params)
4449     (erase-buffer)
4450     (if (< lowest gnus-level-zombie)
4451         ;; List living groups.
4452         (while newsrc
4453           (setq info (car newsrc)
4454                 group (gnus-info-group info)
4455                 params (gnus-info-params info)
4456                 newsrc (cdr newsrc)
4457                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4458           (and unread                   ; This group might be bogus
4459                (or (not regexp)
4460                    (string-match regexp group))
4461                (<= (setq clevel (gnus-info-level info)) level)
4462                (>= clevel lowest)
4463                (or all                  ; We list all groups?
4464                    (if (eq unread t)    ; Unactivated?
4465                        gnus-group-list-inactive-groups ; We list unactivated 
4466                      (> unread 0))      ; We list groups with unread articles
4467                    (and gnus-list-groups-with-ticked-articles
4468                         (cdr (assq 'tick (gnus-info-marks info))))
4469                                         ; And groups with tickeds
4470                    ;; Check for permanent visibility.
4471                    (and gnus-permanently-visible-groups
4472                         (string-match gnus-permanently-visible-groups
4473                                       group))
4474                    (memq 'visible params)
4475                    (cdr (assq 'visible params)))
4476                (gnus-group-insert-group-line
4477                 group (gnus-info-level info)
4478                 (gnus-info-marks info) unread (gnus-info-method info)))))
4479
4480     ;; List dead groups.
4481     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4482          (gnus-group-prepare-flat-list-dead
4483           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4484           gnus-level-zombie ?Z
4485           regexp))
4486     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4487          (gnus-group-prepare-flat-list-dead
4488           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4489           gnus-level-killed ?K regexp))
4490
4491     (gnus-group-set-mode-line)
4492     (setq gnus-group-list-mode (cons level all))
4493     (run-hooks 'gnus-group-prepare-hook)))
4494
4495 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4496   ;; List zombies and killed lists somewhat faster, which was
4497   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4498   ;; this by ignoring the group format specification altogether.
4499   (let (group)
4500     (if regexp
4501         ;; This loop is used when listing groups that match some
4502         ;; regexp.
4503         (while groups
4504           (setq group (pop groups))
4505           (when (string-match regexp group)
4506             (add-text-properties
4507              (point) (prog1 (1+ (point))
4508                        (insert " " mark "     *: " group "\n"))
4509              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4510                    'gnus-unread t
4511                    'gnus-level level))))
4512       ;; This loop is used when listing all groups.
4513       (while groups
4514         (add-text-properties
4515          (point) (prog1 (1+ (point))
4516                    (insert " " mark "     *: "
4517                            (setq group (pop groups)) "\n"))
4518          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4519                'gnus-unread t
4520                'gnus-level level))))))
4521
4522 (defmacro gnus-group-real-name (group)
4523   "Find the real name of a foreign newsgroup."
4524   `(let ((gname ,group))
4525      (if (string-match ":[^:]+$" gname)
4526          (substring gname (1+ (match-beginning 0)))
4527        gname)))
4528
4529 (defsubst gnus-server-add-address (method)
4530   (let ((method-name (symbol-name (car method))))
4531     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4532              (not (assq (intern (concat method-name "-address")) method)))
4533         (append method (list (list (intern (concat method-name "-address"))
4534                                    (nth 1 method))))
4535       method)))
4536
4537 (defsubst gnus-server-get-method (group method)
4538   ;; Input either a server name, and extended server name, or a
4539   ;; select method, and return a select method.
4540   (cond ((stringp method)
4541          (gnus-server-to-method method))
4542         ((and (stringp (car method)) group)
4543          (gnus-server-extend-method group method))
4544         (t
4545          (gnus-server-add-address method))))
4546
4547 (defun gnus-server-to-method (server)
4548   "Map virtual server names to select methods."
4549   (or 
4550    ;; Perhaps this is the native server?
4551    (and (equal server "native") gnus-select-method)
4552    ;; It should be in the server alist.
4553    (cdr (assoc server gnus-server-alist))
4554    ;; If not, we look through all the opened server
4555    ;; to see whether we can find it there.
4556    (let ((opened gnus-opened-servers))
4557      (while (and opened
4558                  (not (equal server (format "%s:%s" (caaar opened)
4559                                             (cadaar opened)))))
4560        (pop opened))
4561      (caar opened))))
4562
4563 (defmacro gnus-method-equal (ss1 ss2)
4564   "Say whether two servers are equal."
4565   `(let ((s1 ,ss1)
4566          (s2 ,ss2))
4567      (or (equal s1 s2)
4568          (and (= (length s1) (length s2))
4569               (progn
4570                 (while (and s1 (member (car s1) s2))
4571                   (setq s1 (cdr s1)))
4572                 (null s1))))))
4573
4574 (defun gnus-server-equal (m1 m2)
4575   "Say whether two methods are equal."
4576   (let ((m1 (cond ((null m1) gnus-select-method)
4577                   ((stringp m1) (gnus-server-to-method m1))
4578                   (t m1)))
4579         (m2 (cond ((null m2) gnus-select-method)
4580                   ((stringp m2) (gnus-server-to-method m2))
4581                   (t m2))))
4582     (gnus-method-equal m1 m2)))
4583
4584 (defun gnus-group-prefixed-name (group method)
4585   "Return the whole name from GROUP and METHOD."
4586   (and (stringp method) (setq method (gnus-server-to-method method)))
4587   (concat (format "%s" (car method))
4588           (if (and
4589                (or (assoc (format "%s" (car method)) 
4590                           (gnus-methods-using 'address))
4591                    (gnus-server-equal method gnus-message-archive-method))
4592                (nth 1 method)
4593                (not (string= (nth 1 method) "")))
4594               (concat "+" (nth 1 method)))
4595           ":" group))
4596
4597 (defun gnus-group-real-prefix (group)
4598   "Return the prefix of the current group name."
4599   (if (string-match "^[^:]+:" group)
4600       (substring group 0 (match-end 0))
4601     ""))
4602
4603 (defun gnus-group-method (group)
4604   "Return the server or method used for selecting GROUP."
4605   (let ((prefix (gnus-group-real-prefix group)))
4606     (if (equal prefix "")
4607         gnus-select-method
4608       (let ((servers gnus-opened-servers)
4609             (server "")
4610             backend possible found)
4611         (if (string-match "^[^\\+]+\\+" prefix)
4612             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4613                   server (substring prefix (match-end 0) (1- (length prefix))))
4614           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4615         (while servers
4616           (when (eq (caaar servers) backend)
4617             (setq possible (caar servers))
4618             (when (equal (cadaar servers) server)
4619               (setq found (caar servers))))
4620           (pop servers))
4621         (or (car (rassoc found gnus-server-alist))
4622             found
4623             (car (rassoc possible gnus-server-alist))
4624             possible
4625             (list backend server))))))
4626
4627 (defsubst gnus-secondary-method-p (method)
4628   "Return whether METHOD is a secondary select method."
4629   (let ((methods gnus-secondary-select-methods)
4630         (gmethod (gnus-server-get-method nil method)))
4631     (while (and methods
4632                 (not (equal (gnus-server-get-method nil (car methods))
4633                             gmethod)))
4634       (setq methods (cdr methods)))
4635     methods))
4636
4637 (defun gnus-group-foreign-p (group)
4638   "Say whether a group is foreign or not."
4639   (and (not (gnus-group-native-p group))
4640        (not (gnus-group-secondary-p group))))
4641
4642 (defun gnus-group-native-p (group)
4643   "Say whether the group is native or not."
4644   (not (string-match ":" group)))
4645
4646 (defun gnus-group-secondary-p (group)
4647   "Say whether the group is secondary or not."
4648   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4649
4650 (defun gnus-group-get-parameter (group &optional symbol)
4651   "Returns the group parameters for GROUP.
4652 If SYMBOL, return the value of that symbol in the group parameters."
4653   (let ((params (gnus-info-params (gnus-get-info group))))
4654     (if symbol
4655         (gnus-group-parameter-value params symbol)
4656       params)))
4657
4658 (defun gnus-group-parameter-value (params symbol)
4659   "Return the value of SYMBOL in group PARAMS."
4660   (or (car (memq symbol params))        ; It's either a simple symbol
4661       (cdr (assq symbol params))))      ; or a cons.
4662
4663 (defun gnus-group-add-parameter (group param)
4664   "Add parameter PARAM to GROUP."
4665   (let ((info (gnus-get-info group)))
4666     (if (not info)
4667         () ; This is a dead group.  We just ignore it.
4668       ;; Cons the new param to the old one and update.
4669       (gnus-group-set-info (cons param (gnus-info-params info))
4670                            group 'params))))
4671
4672 (defun gnus-group-set-parameter (group name value)
4673   "Set parameter NAME to VALUE in GROUP."
4674   (let ((info (gnus-get-info group)))
4675     (if (not info)
4676         () ; This is a dead group.  We just ignore it.
4677       (let ((old-params (gnus-info-params info))
4678             (new-params (list (cons name value))))
4679         (while old-params
4680           (if (or (not (listp (car old-params)))
4681                   (not (eq (caar old-params) name)))
4682               (setq new-params (append new-params (list (car old-params)))))
4683           (setq old-params (cdr old-params)))
4684         (gnus-group-set-info new-params group 'params)))))
4685
4686 (defun gnus-group-add-score (group &optional score)
4687   "Add SCORE to the GROUP score.
4688 If SCORE is nil, add 1 to the score of GROUP."
4689   (let ((info (gnus-get-info group)))
4690     (when info
4691       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4692
4693 (defun gnus-summary-bubble-group ()
4694   "Increase the score of the current group.
4695 This is a handy function to add to `gnus-summary-exit-hook' to
4696 increase the score of each group you read."
4697   (gnus-group-add-score gnus-newsgroup-name))
4698
4699 (defun gnus-group-set-info (info &optional method-only-group part)
4700   (let* ((entry (gnus-gethash
4701                  (or method-only-group (gnus-info-group info))
4702                  gnus-newsrc-hashtb))
4703          (part-info info)
4704          (info (if method-only-group (nth 2 entry) info))
4705          method)
4706     (when method-only-group
4707       (unless entry
4708         (error "Trying to change non-existent group %s" method-only-group))
4709       ;; We have received parts of the actual group info - either the
4710       ;; select method or the group parameters.  We first check
4711       ;; whether we have to extend the info, and if so, do that.
4712       (let ((len (length info))
4713             (total (if (eq part 'method) 5 6)))
4714         (when (< len total)
4715           (setcdr (nthcdr (1- len) info)
4716                   (make-list (- total len) nil)))
4717         ;; Then we enter the new info.
4718         (setcar (nthcdr (1- total) info) part-info)))
4719     (unless entry
4720       ;; This is a new group, so we just create it.
4721       (save-excursion
4722         (set-buffer gnus-group-buffer)
4723         (setq method (gnus-info-method info))
4724         (when (gnus-server-equal method "native")
4725           (setq method nil))
4726         (if method
4727             ;; It's a foreign group...
4728             (gnus-group-make-group
4729              (gnus-group-real-name (gnus-info-group info))
4730              (if (stringp method) method
4731                (prin1-to-string (car method)))
4732              (and (consp method)
4733                   (nth 1 (gnus-info-method info))))
4734           ;; It's a native group.
4735           (gnus-group-make-group (gnus-info-group info)))
4736         (gnus-message 6 "Note: New group created")
4737         (setq entry
4738               (gnus-gethash (gnus-group-prefixed-name
4739                              (gnus-group-real-name (gnus-info-group info))
4740                              (or (gnus-info-method info) gnus-select-method))
4741                             gnus-newsrc-hashtb))))
4742     ;; Whether it was a new group or not, we now have the entry, so we
4743     ;; can do the update.
4744     (if entry
4745         (progn
4746           (setcar (nthcdr 2 entry) info)
4747           (when (and (not (eq (car entry) t))
4748                      (gnus-active (gnus-info-group info)))
4749             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4750       (error "No such group: %s" (gnus-info-group info)))))
4751
4752 (defun gnus-group-set-method-info (group select-method)
4753   (gnus-group-set-info select-method group 'method))
4754
4755 (defun gnus-group-set-params-info (group params)
4756   (gnus-group-set-info params group 'params))
4757
4758 (defun gnus-group-update-group-line ()
4759   "Update the current line in the group buffer."
4760   (let* ((buffer-read-only nil)
4761          (group (gnus-group-group-name))
4762          (gnus-group-indentation (gnus-group-group-indentation))
4763          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4764     (and entry
4765          (not (gnus-ephemeral-group-p group))
4766          (gnus-dribble-enter
4767           (concat "(gnus-group-set-info '"
4768                   (prin1-to-string (nth 2 entry)) ")")))
4769     (gnus-delete-line)
4770     (gnus-group-insert-group-line-info group)
4771     (forward-line -1)
4772     (gnus-group-position-point)))
4773
4774 (defun gnus-group-insert-group-line-info (group)
4775   "Insert GROUP on the current line."
4776   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4777         active info)
4778     (if entry
4779         (progn
4780           ;; (Un)subscribed group.
4781           (setq info (nth 2 entry))
4782           (gnus-group-insert-group-line
4783            group (gnus-info-level info) (gnus-info-marks info)
4784            (or (car entry) t) (gnus-info-method info)))
4785       ;; This group is dead.
4786       (gnus-group-insert-group-line
4787        group
4788        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4789        nil
4790        (if (setq active (gnus-active group))
4791            (- (1+ (cdr active)) (car active)) 0)
4792        nil))))
4793
4794 (defun gnus-group-insert-group-line
4795   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4796                   gnus-tmp-method)
4797   "Insert a group line in the group buffer."
4798   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4799          (gnus-tmp-number-total
4800           (if gnus-tmp-active
4801               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4802             0))
4803          (gnus-tmp-number-of-unread
4804           (if (numberp number) (int-to-string (max 0 number))
4805             "*"))
4806          (gnus-tmp-number-of-read
4807           (if (numberp number)
4808               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4809             "*"))
4810          (gnus-tmp-subscribed
4811           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4812                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4813                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4814                 (t ?K)))
4815          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4816          (gnus-tmp-newsgroup-description
4817           (if gnus-description-hashtb
4818               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4819             ""))
4820          (gnus-tmp-moderated
4821           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4822          (gnus-tmp-moderated-string
4823           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4824          (gnus-tmp-method
4825           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4826          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4827          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4828          (gnus-tmp-news-method-string
4829           (if gnus-tmp-method
4830               (format "(%s:%s)" (car gnus-tmp-method)
4831                       (cadr gnus-tmp-method)) ""))
4832          (gnus-tmp-marked-mark
4833           (if (and (numberp number)
4834                    (zerop number)
4835                    (cdr (assq 'tick gnus-tmp-marked)))
4836               ?* ? ))
4837          (gnus-tmp-process-marked
4838           (if (member gnus-tmp-group gnus-group-marked)
4839               gnus-process-mark ? ))
4840          (gnus-tmp-grouplens
4841           (or (and gnus-use-grouplens
4842                    (bbb-grouplens-group-p gnus-tmp-group))
4843               ""))
4844          (buffer-read-only nil)
4845          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4846     (beginning-of-line)
4847     (add-text-properties
4848      (point)
4849      (prog1 (1+ (point))
4850        ;; Insert the text.
4851        (eval gnus-group-line-format-spec))
4852      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4853        gnus-unread ,(if (numberp number)
4854                         (string-to-int gnus-tmp-number-of-unread)
4855                       t)
4856        gnus-marked ,gnus-tmp-marked-mark
4857        gnus-indentation ,gnus-group-indentation
4858        gnus-level ,gnus-tmp-level))
4859     (when (gnus-visual-p 'group-highlight 'highlight)
4860       (forward-line -1)
4861       (run-hooks 'gnus-group-update-hook)
4862       (forward-line))
4863     ;; Allow XEmacs to remove front-sticky text properties.
4864     (gnus-group-remove-excess-properties)))
4865
4866 (defun gnus-group-update-group (group &optional visible-only)
4867   "Update all lines where GROUP appear.
4868 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4869 already."
4870   (save-excursion
4871     (set-buffer gnus-group-buffer)
4872     ;; The buffer may be narrowed.
4873     (save-restriction
4874       (widen)
4875       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4876             (loc (point-min))
4877             found buffer-read-only)
4878         ;; Enter the current status into the dribble buffer.
4879         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4880           (if (and entry (not (gnus-ephemeral-group-p group)))
4881               (gnus-dribble-enter
4882                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4883                        ")"))))
4884         ;; Find all group instances.  If topics are in use, each group
4885         ;; may be listed in more than once.
4886         (while (setq loc (text-property-any
4887                           loc (point-max) 'gnus-group ident))
4888           (setq found t)
4889           (goto-char loc)
4890           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4891             (gnus-delete-line)
4892             (gnus-group-insert-group-line-info group))
4893           (setq loc (1+ loc)))
4894         (unless (or found visible-only)
4895           ;; No such line in the buffer, find out where it's supposed to
4896           ;; go, and insert it there (or at the end of the buffer).
4897           (if gnus-goto-missing-group-function
4898               (funcall gnus-goto-missing-group-function group)
4899             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4900               (while (and entry (car entry)
4901                           (not
4902                            (gnus-goto-char
4903                             (text-property-any
4904                              (point-min) (point-max)
4905                              'gnus-group (gnus-intern-safe
4906                                           (caar entry) gnus-active-hashtb)))))
4907                 (setq entry (cdr entry)))
4908               (or entry (goto-char (point-max)))))
4909           ;; Finally insert the line.
4910           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4911             (gnus-group-insert-group-line-info group)))
4912         (gnus-group-set-mode-line)))))
4913
4914 (defun gnus-group-set-mode-line ()
4915   (when (memq 'group gnus-updated-mode-lines)
4916     (let* ((gformat (or gnus-group-mode-line-format-spec
4917                         (setq gnus-group-mode-line-format-spec
4918                               (gnus-parse-format
4919                                gnus-group-mode-line-format
4920                                gnus-group-mode-line-format-alist))))
4921            (gnus-tmp-news-server (cadr gnus-select-method))
4922            (gnus-tmp-news-method (car gnus-select-method))
4923            (max-len 60)
4924            gnus-tmp-header                      ;Dummy binding for user-defined formats
4925            ;; Get the resulting string.
4926            (mode-string (eval gformat)))
4927       ;; If the line is too long, we chop it off.
4928       (when (> (length mode-string) max-len)
4929         (setq mode-string (substring mode-string 0 (- max-len 4))))
4930       (prog1
4931           (setq mode-line-buffer-identification (list mode-string))
4932         (set-buffer-modified-p t)))))
4933
4934 (defun gnus-group-group-name ()
4935   "Get the name of the newsgroup on the current line."
4936   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4937     (and group (symbol-name group))))
4938
4939 (defun gnus-group-group-level ()
4940   "Get the level of the newsgroup on the current line."
4941   (get-text-property (gnus-point-at-bol) 'gnus-level))
4942
4943 (defun gnus-group-group-indentation ()
4944   "Get the indentation of the newsgroup on the current line."
4945   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4946       (and gnus-group-indentation-function
4947            (funcall gnus-group-indentation-function))
4948       ""))
4949
4950 (defun gnus-group-group-unread ()
4951   "Get the number of unread articles of the newsgroup on the current line."
4952   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4953
4954 (defun gnus-group-search-forward (&optional backward all level first-too)
4955   "Find the next newsgroup with unread articles.
4956 If BACKWARD is non-nil, find the previous newsgroup instead.
4957 If ALL is non-nil, just find any newsgroup.
4958 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4959 group exists.
4960 If FIRST-TOO, the current line is also eligible as a target."
4961   (let ((way (if backward -1 1))
4962         (low gnus-level-killed)
4963         (beg (point))
4964         pos found lev)
4965     (if (and backward (progn (beginning-of-line)) (bobp))
4966         nil
4967       (or first-too (forward-line way))
4968       (while (and
4969               (not (eobp))
4970               (not (setq
4971                     found
4972                     (and (or all
4973                              (and
4974                               (let ((unread
4975                                      (get-text-property (point) 'gnus-unread)))
4976                                 (and (numberp unread) (> unread 0)))
4977                               (setq lev (get-text-property (point)
4978                                                            'gnus-level))
4979                               (<= lev gnus-level-subscribed)))
4980                          (or (not level)
4981                              (and (setq lev (get-text-property (point)
4982                                                                'gnus-level))
4983                                   (or (= lev level)
4984                                       (and (< lev low)
4985                                            (< level lev)
4986                                            (progn
4987                                              (setq low lev)
4988                                              (setq pos (point))
4989                                              nil))))))))
4990               (zerop (forward-line way)))))
4991     (if found
4992         (progn (gnus-group-position-point) t)
4993       (goto-char (or pos beg))
4994       (and pos t))))
4995
4996 ;;; Gnus group mode commands
4997
4998 ;; Group marking.
4999
5000 (defun gnus-group-mark-group (n &optional unmark no-advance)
5001   "Mark the current group."
5002   (interactive "p")
5003   (let ((buffer-read-only nil)
5004         group)
5005     (while
5006         (and (> n 0)
5007              (setq group (gnus-group-group-name))
5008              (progn
5009                (beginning-of-line)
5010                (forward-char
5011                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5012                (delete-char 1)
5013                (if unmark
5014                    (progn
5015                      (insert " ")
5016                      (setq gnus-group-marked (delete group gnus-group-marked)))
5017                  (insert "#")
5018                  (setq gnus-group-marked
5019                        (cons group (delete group gnus-group-marked))))
5020                t)
5021              (or no-advance (zerop (gnus-group-next-group 1))))
5022       (setq n (1- n)))
5023     (gnus-summary-position-point)
5024     n))
5025
5026 (defun gnus-group-unmark-group (n)
5027   "Remove the mark from the current group."
5028   (interactive "p")
5029   (gnus-group-mark-group n 'unmark)
5030   (gnus-group-position-point))
5031
5032 (defun gnus-group-unmark-all-groups ()
5033   "Unmark all groups."
5034   (interactive)
5035   (let ((groups gnus-group-marked))
5036     (save-excursion
5037       (while groups
5038         (gnus-group-remove-mark (pop groups)))))
5039   (gnus-group-position-point))
5040
5041 (defun gnus-group-mark-region (unmark beg end)
5042   "Mark all groups between point and mark.
5043 If UNMARK, remove the mark instead."
5044   (interactive "P\nr")
5045   (let ((num (count-lines beg end)))
5046     (save-excursion
5047       (goto-char beg)
5048       (- num (gnus-group-mark-group num unmark)))))
5049
5050 (defun gnus-group-mark-buffer (&optional unmark)
5051   "Mark all groups in the buffer.
5052 If UNMARK, remove the mark instead."
5053   (interactive "P")
5054   (gnus-group-mark-region unmark (point-min) (point-max)))
5055
5056 (defun gnus-group-mark-regexp (regexp)
5057   "Mark all groups that match some regexp."
5058   (interactive "sMark (regexp): ")
5059   (let ((alist (cdr gnus-newsrc-alist))
5060         group)
5061     (while alist
5062       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5063         (gnus-group-set-mark group))))
5064   (gnus-group-position-point))
5065
5066 (defun gnus-group-remove-mark (group)
5067   "Remove the process mark from GROUP and move point there.
5068 Return nil if the group isn't displayed."
5069   (if (gnus-group-goto-group group)
5070       (save-excursion
5071         (gnus-group-mark-group 1 'unmark t)
5072         t)
5073     (setq gnus-group-marked
5074           (delete group gnus-group-marked))
5075     nil))
5076
5077 (defun gnus-group-set-mark (group)
5078   "Set the process mark on GROUP."
5079   (if (gnus-group-goto-group group) 
5080       (save-excursion
5081         (gnus-group-mark-group 1 nil t))
5082     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5083
5084 (defun gnus-group-universal-argument (arg &optional groups func)
5085   "Perform any command on all groups accoring to the process/prefix convention."
5086   (interactive "P")
5087   (let ((groups (or groups (gnus-group-process-prefix arg)))
5088         group func)
5089     (if (eq (setq func (or func
5090                            (key-binding
5091                             (read-key-sequence
5092                              (substitute-command-keys
5093                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5094             'undefined)
5095         (progn
5096           (message "Undefined key")
5097           (ding))
5098       (while groups
5099         (gnus-group-remove-mark (setq group (pop groups)))
5100         (command-execute func))))
5101   (gnus-group-position-point))
5102
5103 (defun gnus-group-process-prefix (n)
5104   "Return a list of groups to work on.
5105 Take into consideration N (the prefix) and the list of marked groups."
5106   (cond
5107    (n
5108     (setq n (prefix-numeric-value n))
5109     ;; There is a prefix, so we return a list of the N next
5110     ;; groups.
5111     (let ((way (if (< n 0) -1 1))
5112           (n (abs n))
5113           group groups)
5114       (save-excursion
5115         (while (and (> n 0)
5116                     (setq group (gnus-group-group-name)))
5117           (setq groups (cons group groups))
5118           (setq n (1- n))
5119           (gnus-group-next-group way)))
5120       (nreverse groups)))
5121    ((and (boundp 'transient-mark-mode)
5122          transient-mark-mode
5123          mark-active)
5124     ;; Work on the region between point and mark.
5125     (let ((max (max (point) (mark)))
5126           groups)
5127       (save-excursion
5128         (goto-char (min (point) (mark)))
5129         (while
5130             (and
5131              (push (gnus-group-group-name) groups)
5132              (zerop (gnus-group-next-group 1))
5133              (< (point) max)))
5134         (nreverse groups))))
5135    (gnus-group-marked
5136     ;; No prefix, but a list of marked articles.
5137     (reverse gnus-group-marked))
5138    (t
5139     ;; Neither marked articles or a prefix, so we return the
5140     ;; current group.
5141     (let ((group (gnus-group-group-name)))
5142       (and group (list group))))))
5143
5144 ;; Selecting groups.
5145
5146 (defun gnus-group-read-group (&optional all no-article group)
5147   "Read news in this newsgroup.
5148 If the prefix argument ALL is non-nil, already read articles become
5149 readable.  IF ALL is a number, fetch this number of articles.  If the
5150 optional argument NO-ARTICLE is non-nil, no article will be
5151 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5152 group."
5153   (interactive "P")
5154   (let ((group (or group (gnus-group-group-name)))
5155         number active marked entry)
5156     (or group (error "No group on current line"))
5157     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5158                                             group gnus-newsrc-hashtb)))))
5159     ;; This group might be a dead group.  In that case we have to get
5160     ;; the number of unread articles from `gnus-active-hashtb'.
5161     (setq number
5162           (cond ((numberp all) all)
5163                 (entry (car entry))
5164                 ((setq active (gnus-active group))
5165                  (- (1+ (cdr active)) (car active)))))
5166     (gnus-summary-read-group
5167      group (or all (and (numberp number)
5168                         (zerop (+ number (length (cdr (assq 'tick marked)))
5169                                   (length (cdr (assq 'dormant marked)))))))
5170      no-article)))
5171
5172 (defun gnus-group-select-group (&optional all)
5173   "Select this newsgroup.
5174 No article is selected automatically.
5175 If ALL is non-nil, already read articles become readable.
5176 If ALL is a number, fetch this number of articles."
5177   (interactive "P")
5178   (gnus-group-read-group all t))
5179
5180 (defun gnus-group-quick-select-group (&optional all)
5181   "Select the current group \"quickly\".
5182 This means that no highlighting or scoring will be performed."
5183   (interactive "P")
5184   (let (gnus-visual
5185         gnus-score-find-score-files-function
5186         gnus-apply-kill-hook
5187         gnus-summary-expunge-below)
5188     (gnus-group-read-group all t)))
5189
5190 (defun gnus-group-visible-select-group (&optional all)
5191   "Select the current group without hiding any articles."
5192   (interactive "P")
5193   (let ((gnus-inhibit-limiting t))
5194     (gnus-group-read-group all t)))
5195
5196 ;;;###autoload
5197 (defun gnus-fetch-group (group)
5198   "Start Gnus if necessary and enter GROUP.
5199 Returns whether the fetching was successful or not."
5200   (interactive "sGroup name: ")
5201   (or (get-buffer gnus-group-buffer)
5202       (gnus))
5203   (gnus-group-read-group nil nil group))
5204
5205 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5206 ;; if selection was successful.
5207 (defun gnus-group-read-ephemeral-group
5208   (group method &optional activate quit-config)
5209   (let ((group (if (gnus-group-foreign-p group) group
5210                  (gnus-group-prefixed-name group method))))
5211     (gnus-sethash
5212      group
5213      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5214                      ((quit-config . ,(if quit-config quit-config
5215                                         (cons (current-buffer) 'summary))))))
5216      gnus-newsrc-hashtb)
5217     (set-buffer gnus-group-buffer)
5218     (or (gnus-check-server method)
5219         (error "Unable to contact server: %s" (gnus-status-message method)))
5220     (if activate (or (gnus-request-group group)
5221                      (error "Couldn't request group")))
5222     (condition-case ()
5223         (gnus-group-read-group t t group)
5224       (error nil)
5225       (quit nil))))
5226
5227 (defun gnus-group-jump-to-group (group)
5228   "Jump to newsgroup GROUP."
5229   (interactive
5230    (list (completing-read
5231           "Group: " gnus-active-hashtb nil
5232           (memq gnus-select-method gnus-have-read-active-file))))
5233
5234   (if (equal group "")
5235       (error "Empty group name"))
5236
5237   (let ((b (text-property-any
5238             (point-min) (point-max)
5239             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5240     (unless (gnus-ephemeral-group-p group)
5241       (if b
5242           ;; Either go to the line in the group buffer...
5243           (goto-char b)
5244         ;; ... or insert the line.
5245         (or
5246          (gnus-active group)
5247          (gnus-activate-group group)
5248          (error "%s error: %s" group (gnus-status-message group)))
5249
5250         (gnus-group-update-group group)
5251         (goto-char (text-property-any
5252                     (point-min) (point-max)
5253                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5254     ;; Adjust cursor point.
5255     (gnus-group-position-point)))
5256
5257 (defun gnus-group-goto-group (group)
5258   "Goto to newsgroup GROUP."
5259   (when group
5260     (let ((b (text-property-any (point-min) (point-max)
5261                                 'gnus-group (gnus-intern-safe
5262                                              group gnus-active-hashtb))))
5263       (and b (goto-char b)))))
5264
5265 (defun gnus-group-next-group (n)
5266   "Go to next N'th newsgroup.
5267 If N is negative, search backward instead.
5268 Returns the difference between N and the number of skips actually
5269 done."
5270   (interactive "p")
5271   (gnus-group-next-unread-group n t))
5272
5273 (defun gnus-group-next-unread-group (n &optional all level)
5274   "Go to next N'th unread newsgroup.
5275 If N is negative, search backward instead.
5276 If ALL is non-nil, choose any newsgroup, unread or not.
5277 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5278 such group can be found, the next group with a level higher than
5279 LEVEL.
5280 Returns the difference between N and the number of skips actually
5281 made."
5282   (interactive "p")
5283   (let ((backward (< n 0))
5284         (n (abs n)))
5285     (while (and (> n 0)
5286                 (gnus-group-search-forward
5287                  backward (or (not gnus-group-goto-unread) all) level))
5288       (setq n (1- n)))
5289     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5290                                (if level " on this level or higher" "")))
5291     n))
5292
5293 (defun gnus-group-prev-group (n)
5294   "Go to previous N'th newsgroup.
5295 Returns the difference between N and the number of skips actually
5296 done."
5297   (interactive "p")
5298   (gnus-group-next-unread-group (- n) t))
5299
5300 (defun gnus-group-prev-unread-group (n)
5301   "Go to previous N'th unread newsgroup.
5302 Returns the difference between N and the number of skips actually
5303 done."
5304   (interactive "p")
5305   (gnus-group-next-unread-group (- n)))
5306
5307 (defun gnus-group-next-unread-group-same-level (n)
5308   "Go to next N'th unread newsgroup on the same level.
5309 If N is negative, search backward instead.
5310 Returns the difference between N and the number of skips actually
5311 done."
5312   (interactive "p")
5313   (gnus-group-next-unread-group n t (gnus-group-group-level))
5314   (gnus-group-position-point))
5315
5316 (defun gnus-group-prev-unread-group-same-level (n)
5317   "Go to next N'th unread newsgroup on the same level.
5318 Returns the difference between N and the number of skips actually
5319 done."
5320   (interactive "p")
5321   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5322   (gnus-group-position-point))
5323
5324 (defun gnus-group-best-unread-group (&optional exclude-group)
5325   "Go to the group with the highest level.
5326 If EXCLUDE-GROUP, do not go to that group."
5327   (interactive)
5328   (goto-char (point-min))
5329   (let ((best 100000)
5330         unread best-point)
5331     (while (setq unread (get-text-property (point) 'gnus-unread))
5332       (if (and (numberp unread) (> unread 0))
5333           (progn
5334             (if (and (< (get-text-property (point) 'gnus-level) best)
5335                      (or (not exclude-group)
5336                          (not (equal exclude-group (gnus-group-group-name)))))
5337                 (progn
5338                   (setq best (get-text-property (point) 'gnus-level))
5339                   (setq best-point (point))))))
5340       (forward-line 1))
5341     (if best-point (goto-char best-point))
5342     (gnus-summary-position-point)
5343     (and best-point (gnus-group-group-name))))
5344
5345 (defun gnus-group-first-unread-group ()
5346   "Go to the first group with unread articles."
5347   (interactive)
5348   (prog1
5349       (let ((opoint (point))
5350             unread)
5351         (goto-char (point-min))
5352         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5353                 (and (numberp unread)   ; Not a topic.
5354                      (not (zerop unread))) ; Has unread articles.
5355                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5356             (point)                     ; Success.
5357           (goto-char opoint)
5358           nil))                         ; Not success.
5359     (gnus-group-position-point)))
5360
5361 (defun gnus-group-enter-server-mode ()
5362   "Jump to the server buffer."
5363   (interactive)
5364   (gnus-enter-server-buffer))
5365
5366 (defun gnus-group-make-group (name &optional method address)
5367   "Add a new newsgroup.
5368 The user will be prompted for a NAME, for a select METHOD, and an
5369 ADDRESS."
5370   (interactive
5371    (cons
5372     (read-string "Group name: ")
5373     (let ((method
5374            (completing-read
5375             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5376             nil t)))
5377       (if (assoc method gnus-valid-select-methods)
5378           (list method
5379                 (if (memq 'prompt-address
5380                           (assoc method gnus-valid-select-methods))
5381                     (read-string "Address: ")
5382                   ""))
5383         (list method "")))))
5384
5385   (save-excursion
5386     (set-buffer gnus-group-buffer)
5387     (let* ((meth (and method (if address (list (intern method) address)
5388                                method)))
5389            (nname (if method (gnus-group-prefixed-name name meth) name))
5390            info)
5391       (and (gnus-gethash nname gnus-newsrc-hashtb)
5392            (error "Group %s already exists" nname))
5393       (gnus-group-change-level
5394        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5395        gnus-level-default-subscribed gnus-level-killed
5396        (and (gnus-group-group-name)
5397             (gnus-gethash (gnus-group-group-name)
5398                           gnus-newsrc-hashtb))
5399        t)
5400       (gnus-set-active nname (cons 1 0))
5401       (or (gnus-ephemeral-group-p name)
5402           (gnus-dribble-enter
5403            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5404       (gnus-group-insert-group-line-info nname)
5405
5406       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5407         (require (car meth)))
5408       (gnus-check-server meth)
5409       (and (gnus-check-backend-function 'request-create-group nname)
5410            (gnus-request-create-group nname))
5411       t)))
5412
5413 (defun gnus-group-delete-group (group &optional force)
5414   "Delete the current group.
5415 If FORCE (the prefix) is non-nil, all the articles in the group will
5416 be deleted.  This is \"deleted\" as in \"removed forever from the face
5417 of the Earth\".  There is no undo."
5418   (interactive
5419    (list (gnus-group-group-name)
5420          current-prefix-arg))
5421   (or group (error "No group to rename"))
5422   (or (gnus-check-backend-function 'request-delete-group group)
5423       (error "This backend does not support group deletion"))
5424   (prog1
5425       (if (not (gnus-yes-or-no-p
5426                 (format
5427                  "Do you really want to delete %s%s? "
5428                  group (if force " and all its contents" ""))))
5429           () ; Whew!
5430         (gnus-message 6 "Deleting group %s..." group)
5431         (if (not (gnus-request-delete-group group force))
5432             (progn
5433               (gnus-message 3 "Couldn't delete group %s" group)
5434               (ding))
5435           (gnus-message 6 "Deleting group %s...done" group)
5436           (gnus-group-goto-group group)
5437           (gnus-group-kill-group 1 t)
5438           (gnus-sethash group nil gnus-active-hashtb)
5439           t))
5440     (gnus-group-position-point)))
5441
5442 (defun gnus-group-rename-group (group new-name)
5443   (interactive
5444    (list
5445     (gnus-group-group-name)
5446     (progn
5447       (or (gnus-check-backend-function
5448            'request-rename-group (gnus-group-group-name))
5449           (error "This backend does not support renaming groups"))
5450       (read-string "New group name: "))))
5451
5452   (or (gnus-check-backend-function 'request-rename-group group)
5453       (error "This backend does not support renaming groups"))
5454
5455   (or group (error "No group to rename"))
5456   (and (string-match "^[ \t]*$" new-name)
5457        (error "Not a valid group name"))
5458
5459   ;; We find the proper prefixed name.
5460   (setq new-name
5461         (gnus-group-prefixed-name
5462          (gnus-group-real-name new-name)
5463          (gnus-info-method (gnus-get-info group))))
5464
5465   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5466   (prog1
5467       (if (not (gnus-request-rename-group group new-name))
5468           (progn
5469             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5470             (ding))
5471         ;; We rename the group internally by killing it...
5472         (gnus-group-goto-group group)
5473         (gnus-group-kill-group)
5474         ;; ... changing its name ...
5475         (setcar (cdar gnus-list-of-killed-groups) new-name)
5476         ;; ... and then yanking it.  Magic!
5477         (gnus-group-yank-group)
5478         (gnus-set-active new-name (gnus-active group))
5479         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5480         new-name)
5481     (gnus-group-position-point)))
5482
5483 (defun gnus-group-edit-group (group &optional part)
5484   "Edit the group on the current line."
5485   (interactive (list (gnus-group-group-name)))
5486   (let* ((part (or part 'info))
5487          (done-func `(lambda ()
5488                        "Exit editing mode and update the information."
5489                        (interactive)
5490                        (gnus-group-edit-group-done ',part ,group)))
5491          (winconf (current-window-configuration))
5492          info)
5493     (or group (error "No group on current line"))
5494     (or (setq info (gnus-get-info group))
5495         (error "Killed group; can't be edited"))
5496     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5497     (gnus-configure-windows 'edit-group)
5498     (gnus-add-current-to-buffer-list)
5499     (emacs-lisp-mode)
5500     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5501     (use-local-map (copy-keymap emacs-lisp-mode-map))
5502     (local-set-key "\C-c\C-c" done-func)
5503     (make-local-variable 'gnus-prev-winconf)
5504     (setq gnus-prev-winconf winconf)
5505     (erase-buffer)
5506     (insert
5507      (cond
5508       ((eq part 'method)
5509        ";; Type `C-c C-c' after editing the select method.\n\n")
5510       ((eq part 'params)
5511        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5512       ((eq part 'info)
5513        ";; Type `C-c C-c' after editing the group info.\n\n")))
5514     (insert
5515      (pp-to-string
5516       (cond ((eq part 'method)
5517              (or (gnus-info-method info) "native"))
5518             ((eq part 'params)
5519              (gnus-info-params info))
5520             (t info)))
5521      "\n")))
5522
5523 (defun gnus-group-edit-group-method (group)
5524   "Edit the select method of GROUP."
5525   (interactive (list (gnus-group-group-name)))
5526   (gnus-group-edit-group group 'method))
5527
5528 (defun gnus-group-edit-group-parameters (group)
5529   "Edit the group parameters of GROUP."
5530   (interactive (list (gnus-group-group-name)))
5531   (gnus-group-edit-group group 'params))
5532
5533 (defun gnus-group-edit-group-done (part group)
5534   "Get info from buffer, update variables and jump to the group buffer."
5535   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5536   (goto-char (point-min))
5537   (let* ((form (read (current-buffer)))
5538          (winconf gnus-prev-winconf)
5539          (method (cond ((eq part 'info) (nth 4 form))
5540                        ((eq part 'method) form)
5541                        (t nil)))
5542          (info (cond ((eq part 'info) form)
5543                      ((eq part 'method) (gnus-get-info group))
5544                      (t nil)))
5545          (new-group (if info
5546                       (if (or (not method)
5547                               (gnus-server-equal
5548                                gnus-select-method method))
5549                           (gnus-group-real-name (car info))
5550                         (gnus-group-prefixed-name
5551                          (gnus-group-real-name (car info)) method))
5552                       nil)))
5553     (when (and new-group
5554                (not (equal new-group group)))
5555       (when (gnus-group-goto-group group)
5556         (gnus-group-kill-group 1))
5557       (gnus-activate-group new-group))
5558     ;; Set the info.
5559     (if (and info new-group)
5560         (progn
5561           (setq info (gnus-copy-sequence info))
5562           (setcar info new-group)
5563           (unless (gnus-server-equal method "native")
5564             (unless (nthcdr 3 info)
5565               (nconc info (list nil nil)))
5566             (unless (nthcdr 4 info)
5567               (nconc info (list nil)))
5568             (gnus-info-set-method info method))
5569           (gnus-group-set-info info))
5570       (gnus-group-set-info form (or new-group group) part))
5571     (kill-buffer (current-buffer))
5572     (and winconf (set-window-configuration winconf))
5573     (set-buffer gnus-group-buffer)
5574     (gnus-group-update-group (or new-group group))
5575     (gnus-group-position-point)))
5576
5577 (defun gnus-group-make-help-group ()
5578   "Create the Gnus documentation group."
5579   (interactive)
5580   (let ((path load-path)
5581         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5582         file dir)
5583     (and (gnus-gethash name gnus-newsrc-hashtb)
5584          (error "Documentation group already exists"))
5585     (while path
5586       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5587             file nil)
5588       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5589                 (file-exists-p
5590                  (setq file (concat (file-name-directory
5591                                      (directory-file-name dir))
5592                                     "etc/gnus-tut.txt"))))
5593         (setq path nil)))
5594     (if (not file)
5595         (message "Couldn't find doc group")
5596       (gnus-group-make-group
5597        (gnus-group-real-name name)
5598        (list 'nndoc "gnus-help"
5599              (list 'nndoc-address file)
5600              (list 'nndoc-article-type 'mbox)))))
5601   (gnus-group-position-point))
5602
5603 (defun gnus-group-make-doc-group (file type)
5604   "Create a group that uses a single file as the source."
5605   (interactive
5606    (list (read-file-name "File name: ")
5607          (and current-prefix-arg 'ask)))
5608   (when (eq type 'ask)
5609     (let ((err "")
5610           char found)
5611       (while (not found)
5612         (message
5613          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5614          err)
5615         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5616                           ((= char ?b) 'babyl)
5617                           ((= char ?d) 'digest)
5618                           ((= char ?f) 'forward)
5619                           ((= char ?a) 'mmfd)
5620                           (t (setq err (format "%c unknown. " char))
5621                              nil))))
5622       (setq type found)))
5623   (let* ((file (expand-file-name file))
5624          (name (gnus-generate-new-group-name
5625                 (gnus-group-prefixed-name
5626                  (file-name-nondirectory file) '(nndoc "")))))
5627     (gnus-group-make-group
5628      (gnus-group-real-name name)
5629      (list 'nndoc (file-name-nondirectory file)
5630            (list 'nndoc-address file)
5631            (list 'nndoc-article-type (or type 'guess))))
5632     (forward-line -1)
5633     (gnus-group-position-point)))
5634
5635 (defun gnus-group-make-archive-group (&optional all)
5636   "Create the (ding) Gnus archive group of the most recent articles.
5637 Given a prefix, create a full group."
5638   (interactive "P")
5639   (let ((group (gnus-group-prefixed-name
5640                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5641     (and (gnus-gethash group gnus-newsrc-hashtb)
5642          (error "Archive group already exists"))
5643     (gnus-group-make-group
5644      (gnus-group-real-name group)
5645      (list 'nndir (if all "hpc" "edu")
5646            (list 'nndir-directory
5647                  (if all gnus-group-archive-directory
5648                    gnus-group-recent-archive-directory)))))
5649   (forward-line -1)
5650   (gnus-group-position-point))
5651
5652 (defun gnus-group-make-directory-group (dir)
5653   "Create an nndir group.
5654 The user will be prompted for a directory.  The contents of this
5655 directory will be used as a newsgroup.  The directory should contain
5656 mail messages or news articles in files that have numeric names."
5657   (interactive
5658    (list (read-file-name "Create group from directory: ")))
5659   (or (file-exists-p dir) (error "No such directory"))
5660   (or (file-directory-p dir) (error "Not a directory"))
5661   (let ((ext "")
5662         (i 0)
5663         group)
5664     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5665       (setq group
5666             (gnus-group-prefixed-name
5667              (concat (file-name-as-directory (directory-file-name dir))
5668                      ext)
5669              '(nndir "")))
5670       (setq ext (format "<%d>" (setq i (1+ i)))))
5671     (gnus-group-make-group
5672      (gnus-group-real-name group)
5673      (list 'nndir group (list 'nndir-directory dir))))
5674   (forward-line -1)
5675   (gnus-group-position-point))
5676
5677 (defun gnus-group-make-kiboze-group (group address scores)
5678   "Create an nnkiboze group.
5679 The user will be prompted for a name, a regexp to match groups, and
5680 score file entries for articles to include in the group."
5681   (interactive
5682    (list
5683     (read-string "nnkiboze group name: ")
5684     (read-string "Source groups (regexp): ")
5685     (let ((headers (mapcar (lambda (group) (list group))
5686                            '("subject" "from" "number" "date" "message-id"
5687                              "references" "chars" "lines" "xref"
5688                              "followup" "all" "body" "head")))
5689           scores header regexp regexps)
5690       (while (not (equal "" (setq header (completing-read
5691                                           "Match on header: " headers nil t))))
5692         (setq regexps nil)
5693         (while (not (equal "" (setq regexp (read-string
5694                                             (format "Match on %s (string): "
5695                                                     header)))))
5696           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5697         (setq scores (cons (cons header regexps) scores)))
5698       scores)))
5699   (gnus-group-make-group group "nnkiboze" address)
5700   (save-excursion
5701     (gnus-set-work-buffer)
5702     (let (emacs-lisp-mode-hook)
5703       (pp scores (current-buffer)))
5704     (write-region (point-min) (point-max)
5705                   (gnus-score-file-name (concat "nnkiboze:" group))))
5706   (forward-line -1)
5707   (gnus-group-position-point))
5708
5709 (defun gnus-group-add-to-virtual (n vgroup)
5710   "Add the current group to a virtual group."
5711   (interactive
5712    (list current-prefix-arg
5713          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5714                           "nnvirtual:")))
5715   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5716       (error "%s is not an nnvirtual group" vgroup))
5717   (let* ((groups (gnus-group-process-prefix n))
5718          (method (gnus-info-method (gnus-get-info vgroup))))
5719     (setcar (cdr method)
5720             (concat
5721              (nth 1 method) "\\|"
5722              (mapconcat
5723               (lambda (s)
5724                 (gnus-group-remove-mark s)
5725                 (concat "\\(^" (regexp-quote s) "$\\)"))
5726               groups "\\|"))))
5727   (gnus-group-position-point))
5728
5729 (defun gnus-group-make-empty-virtual (group)
5730   "Create a new, fresh, empty virtual group."
5731   (interactive "sCreate new, empty virtual group: ")
5732   (let* ((method (list 'nnvirtual "^$"))
5733          (pgroup (gnus-group-prefixed-name group method)))
5734     ;; Check whether it exists already.
5735     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5736          (error "Group %s already exists." pgroup))
5737     ;; Subscribe the new group after the group on the current line.
5738     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5739     (gnus-group-update-group pgroup)
5740     (forward-line -1)
5741     (gnus-group-position-point)))
5742
5743 (defun gnus-group-enter-directory (dir)
5744   "Enter an ephemeral nneething group."
5745   (interactive "DDirectory to read: ")
5746   (let* ((method (list 'nneething dir))
5747          (leaf (gnus-group-prefixed-name
5748                 (file-name-nondirectory (directory-file-name dir))
5749                 method))
5750          (name (gnus-generate-new-group-name leaf)))
5751     (let ((nneething-read-only t))
5752       (or (gnus-group-read-ephemeral-group
5753            name method t
5754            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5755                                       'summary 'group)))
5756           (error "Couldn't enter %s" dir)))))
5757
5758 ;; Group sorting commands
5759 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5760
5761 (defun gnus-group-sort-groups (func &optional reverse)
5762   "Sort the group buffer according to FUNC.
5763 If REVERSE, reverse the sorting order."
5764   (interactive (list gnus-group-sort-function
5765                      current-prefix-arg))
5766   (let ((func (cond 
5767                ((not (listp func)) func)
5768                ((null func) func)
5769                ((= 1 (length func)) (car func))
5770                (t `(lambda (t1 t2)
5771                      ,(gnus-make-sort-function 
5772                        (reverse func)))))))
5773     ;; We peel off the dummy group from the alist.
5774     (when func
5775       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5776         (pop gnus-newsrc-alist))
5777       ;; Do the sorting.
5778       (setq gnus-newsrc-alist
5779             (sort gnus-newsrc-alist func))
5780       (when reverse
5781         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5782       ;; Regenerate the hash table.
5783       (gnus-make-hashtable-from-newsrc-alist)
5784       (gnus-group-list-groups))))
5785
5786 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5787   "Sort the group buffer alphabetically by group name.
5788 If REVERSE, sort in reverse order."
5789   (interactive "P")
5790   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5791
5792 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5793   "Sort the group buffer by number of unread articles.
5794 If REVERSE, sort in reverse order."
5795   (interactive "P")
5796   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5797
5798 (defun gnus-group-sort-groups-by-level (&optional reverse)
5799   "Sort the group buffer by group level.
5800 If REVERSE, sort in reverse order."
5801   (interactive "P")
5802   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5803
5804 (defun gnus-group-sort-groups-by-score (&optional reverse)
5805   "Sort the group buffer by group score.
5806 If REVERSE, sort in reverse order."
5807   (interactive "P")
5808   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5809
5810 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5811   "Sort the group buffer by group rank.
5812 If REVERSE, sort in reverse order."
5813   (interactive "P")
5814   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5815
5816 (defun gnus-group-sort-groups-by-method (&optional reverse)
5817   "Sort the group buffer alphabetically by backend name.
5818 If REVERSE, sort in reverse order."
5819   (interactive "P")
5820   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5821
5822 (defun gnus-group-sort-by-alphabet (info1 info2)
5823   "Sort alphabetically."
5824   (string< (gnus-info-group info1) (gnus-info-group info2)))
5825
5826 (defun gnus-group-sort-by-unread (info1 info2)
5827   "Sort by number of unread articles."
5828   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5829         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5830     (< (or (and (numberp n1) n1) 0)
5831        (or (and (numberp n2) n2) 0))))
5832
5833 (defun gnus-group-sort-by-level (info1 info2)
5834   "Sort by level."
5835   (< (gnus-info-level info1) (gnus-info-level info2)))
5836
5837 (defun gnus-group-sort-by-method (info1 info2)
5838   "Sort alphabetically by backend name."
5839   (string< (symbol-name (car (gnus-find-method-for-group
5840                               (gnus-info-group info1) info1)))
5841            (symbol-name (car (gnus-find-method-for-group
5842                               (gnus-info-group info2) info2)))))
5843
5844 (defun gnus-group-sort-by-score (info1 info2)
5845   "Sort by group score."
5846   (< (gnus-info-score info1) (gnus-info-score info2)))
5847
5848 (defun gnus-group-sort-by-rank (info1 info2)
5849   "Sort by level and score."
5850   (let ((level1 (gnus-info-level info1))
5851         (level2 (gnus-info-level info2)))
5852     (or (< level1 level2)
5853         (and (= level1 level2)
5854              (< (gnus-info-score info1) (gnus-info-score info2))))))
5855
5856 ;; Group catching up.
5857
5858 (defun gnus-group-clear-data (n)
5859   "Clear all marks and read ranges from the current group."
5860   (interactive "P")
5861   (let ((groups (gnus-group-process-prefix n))
5862         group info)
5863     (while (setq group (pop groups))
5864       (setq info (gnus-get-info group))
5865       (gnus-info-set-read info nil)
5866       (when (gnus-info-marks info)
5867         (gnus-info-set-marks info nil))
5868       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5869       (when (gnus-group-goto-group group)
5870         (gnus-group-remove-mark group)
5871         (gnus-group-update-group-line)))))
5872
5873 (defun gnus-group-catchup-current (&optional n all)
5874   "Mark all articles not marked as unread in current newsgroup as read.
5875 If prefix argument N is numeric, the ARG next newsgroups will be
5876 caught up.  If ALL is non-nil, marked articles will also be marked as
5877 read.  Cross references (Xref: header) of articles are ignored.
5878 The difference between N and actual number of newsgroups that were
5879 caught up is returned."
5880   (interactive "P")
5881   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5882                gnus-expert-user
5883                (gnus-y-or-n-p
5884                 (if all
5885                     "Do you really want to mark all articles as read? "
5886                   "Mark all unread articles as read? "))))
5887       n
5888     (let ((groups (gnus-group-process-prefix n))
5889           (ret 0))
5890       (while groups
5891         ;; Virtual groups have to be given special treatment.
5892         (let ((method (gnus-find-method-for-group (car groups))))
5893           (if (eq 'nnvirtual (car method))
5894               (nnvirtual-catchup-group
5895                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5896         (gnus-group-remove-mark (car groups))
5897         (if (prog1
5898                 (gnus-group-goto-group (car groups))
5899               (gnus-group-catchup (car groups) all))
5900             (gnus-group-update-group-line)
5901           (setq ret (1+ ret)))
5902         (setq groups (cdr groups)))
5903       (gnus-group-next-unread-group 1)
5904       ret)))
5905
5906 (defun gnus-group-catchup-current-all (&optional n)
5907   "Mark all articles in current newsgroup as read.
5908 Cross references (Xref: header) of articles are ignored."
5909   (interactive "P")
5910   (gnus-group-catchup-current n 'all))
5911
5912 (defun gnus-group-catchup (group &optional all)
5913   "Mark all articles in GROUP as read.
5914 If ALL is non-nil, all articles are marked as read.
5915 The return value is the number of articles that were marked as read,
5916 or nil if no action could be taken."
5917   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5918          (num (car entry)))
5919     ;; Do the updating only if the newsgroup isn't killed.
5920     (if (not (numberp (car entry)))
5921         (gnus-message 1 "Can't catch up; non-active group")
5922       ;; Do auto-expirable marks if that's required.
5923       (when (gnus-group-auto-expirable-p group)
5924         (gnus-add-marked-articles
5925          group 'expire (gnus-list-of-unread-articles group))
5926         (when all
5927           (let ((marks (nth 3 (nth 2 entry))))
5928             (gnus-add-marked-articles
5929              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5930             (gnus-add-marked-articles
5931              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5932       (when entry
5933         (gnus-update-read-articles group nil)
5934         ;; Also nix out the lists of marks and dormants.
5935         (when all
5936           (gnus-add-marked-articles group 'tick nil nil 'force)
5937           (gnus-add-marked-articles group 'dormant nil nil 'force))
5938         (run-hooks 'gnus-group-catchup-group-hook)
5939         num))))
5940
5941 (defun gnus-group-expire-articles (&optional n)
5942   "Expire all expirable articles in the current newsgroup."
5943   (interactive "P")
5944   (let ((groups (gnus-group-process-prefix n))
5945         group)
5946     (unless groups
5947       (error "No groups to expire"))
5948     (while (setq group (pop groups))
5949       (gnus-group-remove-mark group)
5950       (when (gnus-check-backend-function 'request-expire-articles group)
5951         (gnus-message 6 "Expiring articles in %s..." group)
5952         (let* ((info (gnus-get-info group))
5953                (expirable (if (gnus-group-total-expirable-p group)
5954                               (cons nil (gnus-list-of-read-articles group))
5955                             (assq 'expire (gnus-info-marks info))))
5956                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5957           (when expirable
5958             (setcdr
5959              expirable
5960              (gnus-compress-sequence
5961               (if expiry-wait
5962                   ;; We set the expiry variables to the groupp
5963                   ;; parameter. 
5964                   (let ((nnmail-expiry-wait-function nil)
5965                         (nnmail-expiry-wait expiry-wait))
5966                     (gnus-request-expire-articles
5967                      (gnus-uncompress-sequence (cdr expirable)) group))
5968                 ;; Just expire using the normal expiry values.
5969                 (gnus-request-expire-articles
5970                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5971           (gnus-message 6 "Expiring articles in %s...done" group)))
5972       (gnus-group-position-point))))
5973
5974 (defun gnus-group-expire-all-groups ()
5975   "Expire all expirable articles in all newsgroups."
5976   (interactive)
5977   (save-excursion
5978     (gnus-message 5 "Expiring...")
5979     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5980                                      (cdr gnus-newsrc-alist))))
5981       (gnus-group-expire-articles nil)))
5982   (gnus-group-position-point)
5983   (gnus-message 5 "Expiring...done"))
5984
5985 (defun gnus-group-set-current-level (n level)
5986   "Set the level of the next N groups to LEVEL."
5987   (interactive
5988    (list
5989     current-prefix-arg
5990     (string-to-int
5991      (let ((s (read-string
5992                (format "Level (default %s): "
5993                        (or (gnus-group-group-level) 
5994                            gnus-level-default-subscribed)))))
5995        (if (string-match "^\\s-*$" s)
5996            (int-to-string (or (gnus-group-group-level) 
5997                               gnus-level-default-subscribed))
5998          s)))))
5999   (or (and (>= level 1) (<= level gnus-level-killed))
6000       (error "Illegal level: %d" level))
6001   (let ((groups (gnus-group-process-prefix n))
6002         group)
6003     (while (setq group (pop groups))
6004       (gnus-group-remove-mark group)
6005       (gnus-message 6 "Changed level of %s from %d to %d"
6006                     group (or (gnus-group-group-level) gnus-level-killed)
6007                     level)
6008       (gnus-group-change-level
6009        group level (or (gnus-group-group-level) gnus-level-killed))
6010       (gnus-group-update-group-line)))
6011   (gnus-group-position-point))
6012
6013 (defun gnus-group-unsubscribe-current-group (&optional n)
6014   "Toggle subscription of the current group.
6015 If given numerical prefix, toggle the N next groups."
6016   (interactive "P")
6017   (let ((groups (gnus-group-process-prefix n))
6018         group)
6019     (while groups
6020       (setq group (car groups)
6021             groups (cdr groups))
6022       (gnus-group-remove-mark group)
6023       (gnus-group-unsubscribe-group
6024        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6025                  gnus-level-default-unsubscribed
6026                gnus-level-default-subscribed) t)
6027       (gnus-group-update-group-line))
6028     (gnus-group-next-group 1)))
6029
6030 (defun gnus-group-unsubscribe-group (group &optional level silent)
6031   "Toggle subscription to GROUP.
6032 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6033 group line."
6034   (interactive
6035    (list (completing-read
6036           "Group: " gnus-active-hashtb nil
6037           (memq gnus-select-method gnus-have-read-active-file))))
6038   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6039     (cond
6040      ((string-match "^[ \t]$" group)
6041       (error "Empty group name"))
6042      (newsrc
6043       ;; Toggle subscription flag.
6044       (gnus-group-change-level
6045        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6046                                       gnus-level-subscribed)
6047                                   (1+ gnus-level-subscribed)
6048                                 gnus-level-default-subscribed)))
6049       (unless silent
6050         (gnus-group-update-group group)))
6051      ((and (stringp group)
6052            (or (not (memq gnus-select-method gnus-have-read-active-file))
6053                (gnus-active group)))
6054       ;; Add new newsgroup.
6055       (gnus-group-change-level
6056        group
6057        (if level level gnus-level-default-subscribed)
6058        (or (and (member group gnus-zombie-list)
6059                 gnus-level-zombie)
6060            gnus-level-killed)
6061        (and (gnus-group-group-name)
6062             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6063       (unless silent
6064         (gnus-group-update-group group)))
6065      (t (error "No such newsgroup: %s" group)))
6066     (gnus-group-position-point)))
6067
6068 (defun gnus-group-transpose-groups (n)
6069   "Move the current newsgroup up N places.
6070 If given a negative prefix, move down instead.  The difference between
6071 N and the number of steps taken is returned."
6072   (interactive "p")
6073   (or (gnus-group-group-name)
6074       (error "No group on current line"))
6075   (gnus-group-kill-group 1)
6076   (prog1
6077       (forward-line (- n))
6078     (gnus-group-yank-group)
6079     (gnus-group-position-point)))
6080
6081 (defun gnus-group-kill-all-zombies ()
6082   "Kill all zombie newsgroups."
6083   (interactive)
6084   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6085   (setq gnus-zombie-list nil)
6086   (gnus-group-list-groups))
6087
6088 (defun gnus-group-kill-region (begin end)
6089   "Kill newsgroups in current region (excluding current point).
6090 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6091   (interactive "r")
6092   (let ((lines
6093          ;; Count lines.
6094          (save-excursion
6095            (count-lines
6096             (progn
6097               (goto-char begin)
6098               (beginning-of-line)
6099               (point))
6100             (progn
6101               (goto-char end)
6102               (beginning-of-line)
6103               (point))))))
6104     (goto-char begin)
6105     (beginning-of-line)                 ;Important when LINES < 1
6106     (gnus-group-kill-group lines)))
6107
6108 (defun gnus-group-kill-group (&optional n discard)
6109   "Kill the next N groups.
6110 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6111 However, only groups that were alive can be yanked; already killed
6112 groups or zombie groups can't be yanked.
6113 The return value is the name of the group that was killed, or a list
6114 of groups killed."
6115   (interactive "P")
6116   (let ((buffer-read-only nil)
6117         (groups (gnus-group-process-prefix n))
6118         group entry level out)
6119     (if (< (length groups) 10)
6120         ;; This is faster when there are few groups.
6121         (while groups
6122           (push (setq group (pop groups)) out)
6123           (gnus-group-remove-mark group)
6124           (setq level (gnus-group-group-level))
6125           (gnus-delete-line)
6126           (when (and (not discard)
6127                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6128             (push (cons (car entry) (nth 2 entry))
6129                   gnus-list-of-killed-groups))
6130           (gnus-group-change-level
6131            (if entry entry group) gnus-level-killed (if entry nil level)))
6132       ;; If there are lots and lots of groups to be killed, we use
6133       ;; this thing instead.
6134       (let (entry)
6135         (setq groups (nreverse groups))
6136         (while groups
6137           (gnus-group-remove-mark (setq group (pop groups)))
6138           (gnus-delete-line)
6139           (cond
6140            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6141             (push (cons (car entry) (nth 2 entry))
6142                   gnus-list-of-killed-groups)
6143             (setcdr (cdr entry) (cdddr entry)))
6144            ((member group gnus-zombie-list)
6145             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6146         (gnus-make-hashtable-from-newsrc-alist)))
6147
6148     (gnus-group-position-point)
6149     (if (< (length out) 2) (car out) (nreverse out))))
6150
6151 (defun gnus-group-yank-group (&optional arg)
6152   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6153 inserting it before the current newsgroup.  The numeric ARG specifies
6154 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6155 is returned, or (if several groups are yanked) a list of yanked groups
6156 is returned."
6157   (interactive "p")
6158   (setq arg (or arg 1))
6159   (let (info group prev out)
6160     (while (>= (decf arg) 0)
6161       (if (not (setq info (pop gnus-list-of-killed-groups)))
6162           (error "No more newsgroups to yank"))
6163       (push (setq group (nth 1 info)) out)
6164       ;; Find which newsgroup to insert this one before - search
6165       ;; backward until something suitable is found.  If there are no
6166       ;; other newsgroups in this buffer, just make this newsgroup the
6167       ;; first newsgroup.
6168       (setq prev (gnus-group-group-name))
6169       (gnus-group-change-level
6170        info (gnus-info-level (cdr info)) gnus-level-killed
6171        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6172        t)
6173       (gnus-group-insert-group-line-info group))
6174     (forward-line -1)
6175     (gnus-group-position-point)
6176     (if (< (length out) 2) (car out) (nreverse out))))
6177
6178 (defun gnus-group-kill-level (level)
6179   "Kill all groups that is on a certain LEVEL."
6180   (interactive "nKill all groups on level: ")
6181   (cond
6182    ((= level gnus-level-zombie)
6183     (setq gnus-killed-list
6184           (nconc gnus-zombie-list gnus-killed-list))
6185     (setq gnus-zombie-list nil))
6186    ((and (< level gnus-level-zombie)
6187          (> level 0)
6188          (or gnus-expert-user
6189              (gnus-yes-or-no-p
6190               (format
6191                "Do you really want to kill all groups on level %d? "
6192                level))))
6193     (let* ((prev gnus-newsrc-alist)
6194            (alist (cdr prev)))
6195       (while alist
6196         (if (= (gnus-info-level level) level)
6197             (setcdr prev (cdr alist))
6198           (setq prev alist))
6199         (setq alist (cdr alist)))
6200       (gnus-make-hashtable-from-newsrc-alist)
6201       (gnus-group-list-groups)))
6202    (t
6203     (error "Can't kill; illegal level: %d" level))))
6204
6205 (defun gnus-group-list-all-groups (&optional arg)
6206   "List all newsgroups with level ARG or lower.
6207 Default is gnus-level-unsubscribed, which lists all subscribed and most
6208 unsubscribed groups."
6209   (interactive "P")
6210   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6211
6212 ;; Redefine this to list ALL killed groups if prefix arg used.
6213 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6214 (defun gnus-group-list-killed (&optional arg)
6215   "List all killed newsgroups in the group buffer.
6216 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6217 entail asking the server for the groups."
6218   (interactive "P")
6219   ;; Find all possible killed newsgroups if arg.
6220   (when arg
6221     ;; First make sure active file has been read.
6222     (unless gnus-have-read-active-file
6223       (let ((gnus-read-active-file t))
6224         (gnus-read-active-file)))
6225     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6226     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6227     (mapatoms
6228      (lambda (sym)
6229        (let ((groups 0)
6230              (group (symbol-name sym)))
6231          (if (or (null group)
6232                  (gnus-gethash group gnus-killed-hashtb)
6233                  (gnus-gethash group gnus-newsrc-hashtb))
6234              ()
6235            (let ((do-sub (gnus-matches-options-n group)))
6236              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6237                  ()
6238                (setq groups (1+ groups))
6239                (setq gnus-killed-list
6240                      (cons group gnus-killed-list))
6241                (gnus-sethash group group gnus-killed-hashtb))))))
6242      gnus-active-hashtb))
6243   (if (not gnus-killed-list)
6244       (gnus-message 6 "No killed groups")
6245     (let (gnus-group-list-mode)
6246       (funcall gnus-group-prepare-function
6247                gnus-level-killed t gnus-level-killed))
6248     (goto-char (point-min)))
6249   (gnus-group-position-point))
6250
6251 (defun gnus-group-list-zombies ()
6252   "List all zombie newsgroups in the group buffer."
6253   (interactive)
6254   (if (not gnus-zombie-list)
6255       (gnus-message 6 "No zombie groups")
6256     (let (gnus-group-list-mode)
6257       (funcall gnus-group-prepare-function
6258                gnus-level-zombie t gnus-level-zombie))
6259     (goto-char (point-min)))
6260   (gnus-group-position-point))
6261
6262 (defun gnus-group-list-active ()
6263   "List all groups that are available from the server(s)."
6264   (interactive)
6265   ;; First we make sure that we have really read the active file.
6266   (unless gnus-have-read-active-file
6267     (let ((gnus-read-active-file t))
6268       (gnus-read-active-file)))
6269   ;; Find all groups and sort them.
6270   (let ((groups
6271          (sort
6272           (let (list)
6273             (mapatoms
6274              (lambda (sym)
6275                (and (symbol-value sym)
6276                     (setq list (cons (symbol-name sym) list))))
6277              gnus-active-hashtb)
6278             list)
6279           'string<))
6280         (buffer-read-only nil))
6281     (erase-buffer)
6282     (while groups
6283       (gnus-group-insert-group-line-info (car groups))
6284       (setq groups (cdr groups)))
6285     (goto-char (point-min))))
6286
6287 (defun gnus-activate-all-groups (level)
6288   "Activate absolutely all groups."
6289   (interactive (list 7))
6290   (let ((gnus-activate-level level)
6291         (gnus-activate-foreign-newsgroups level))
6292     (gnus-group-get-new-news)))
6293
6294 (defun gnus-group-get-new-news (&optional arg)
6295   "Get newly arrived articles.
6296 If ARG is a number, it specifies which levels you are interested in
6297 re-scanning.  If ARG is non-nil and not a number, this will force
6298 \"hard\" re-reading of the active files from all servers."
6299   (interactive "P")
6300   (run-hooks 'gnus-get-new-news-hook)
6301   ;; We might read in new NoCeM messages here.
6302   (when (and gnus-use-nocem 
6303              (null arg))
6304     (gnus-nocem-scan-groups))
6305   ;; If ARG is not a number, then we read the active file.
6306   (when (and arg (not (numberp arg)))
6307     (let ((gnus-read-active-file t))
6308       (gnus-read-active-file))
6309     (setq arg nil))
6310
6311   (setq arg (gnus-group-default-level arg t))
6312   (if (and gnus-read-active-file (not arg))
6313       (progn
6314         (gnus-read-active-file)
6315         (gnus-get-unread-articles arg))
6316     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6317       (gnus-get-unread-articles arg)))
6318   (run-hooks 'gnus-after-getting-new-news-hook)
6319   (gnus-group-list-groups))
6320
6321 (defun gnus-group-get-new-news-this-group (&optional n)
6322   "Check for newly arrived news in the current group (and the N-1 next groups).
6323 The difference between N and the number of newsgroup checked is returned.
6324 If N is negative, this group and the N-1 previous groups will be checked."
6325   (interactive "P")
6326   (let* ((groups (gnus-group-process-prefix n))
6327          (ret (if (numberp n) (- n (length groups)) 0))
6328          group)
6329     (while groups
6330       (setq group (car groups)
6331             groups (cdr groups))
6332       (gnus-group-remove-mark group)
6333       (unless (gnus-get-new-news-in-group group)
6334         (ding)
6335         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6336     (when gnus-goto-next-group-when-activating
6337       (gnus-group-next-unread-group 1 t))
6338     (gnus-summary-position-point)
6339     ret))
6340
6341 (defun gnus-get-new-news-in-group (group)
6342   (when (and group (gnus-activate-group group 'scan))
6343     (gnus-get-unread-articles-in-group
6344      (gnus-get-info group) (gnus-active group) t)
6345     (gnus-close-group group)
6346     (when (gnus-group-goto-group group)
6347       (gnus-group-update-group-line))
6348     t))
6349
6350 (defun gnus-group-fetch-faq (group &optional faq-dir)
6351   "Fetch the FAQ for the current group."
6352   (interactive
6353    (list
6354     (gnus-group-real-name (gnus-group-group-name))
6355     (cond (current-prefix-arg
6356            (completing-read
6357             "Faq dir: " (and (listp gnus-group-faq-directory)
6358                              gnus-group-faq-directory))))))
6359   (or faq-dir
6360       (setq faq-dir (if (listp gnus-group-faq-directory)
6361                         (car gnus-group-faq-directory)
6362                       gnus-group-faq-directory)))
6363   (or group (error "No group name given"))
6364   (let ((file (concat (file-name-as-directory faq-dir)
6365                       (gnus-group-real-name group))))
6366     (if (not (file-exists-p file))
6367         (error "No such file: %s" file)
6368       (find-file file))))
6369
6370 (defun gnus-group-describe-group (force &optional group)
6371   "Display a description of the current newsgroup."
6372   (interactive (list current-prefix-arg (gnus-group-group-name)))
6373   (and force (setq gnus-description-hashtb nil))
6374   (let ((method (gnus-find-method-for-group group))
6375         desc)
6376     (or group (error "No group name given"))
6377     (and (or (and gnus-description-hashtb
6378                   ;; We check whether this group's method has been
6379                   ;; queried for a description file.
6380                   (gnus-gethash
6381                    (gnus-group-prefixed-name "" method)
6382                    gnus-description-hashtb))
6383              (setq desc (gnus-group-get-description group))
6384              (gnus-read-descriptions-file method))
6385          (message
6386           (or desc (gnus-gethash group gnus-description-hashtb)
6387               "No description available")))))
6388
6389 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6390 (defun gnus-group-describe-all-groups (&optional force)
6391   "Pop up a buffer with descriptions of all newsgroups."
6392   (interactive "P")
6393   (and force (setq gnus-description-hashtb nil))
6394   (if (not (or gnus-description-hashtb
6395                (gnus-read-all-descriptions-files)))
6396       (error "Couldn't request descriptions file"))
6397   (let ((buffer-read-only nil)
6398         b)
6399     (erase-buffer)
6400     (mapatoms
6401      (lambda (group)
6402        (setq b (point))
6403        (insert (format "      *: %-20s %s\n" (symbol-name group)
6404                        (symbol-value group)))
6405        (add-text-properties
6406         b (1+ b) (list 'gnus-group group
6407                        'gnus-unread t 'gnus-marked nil
6408                        'gnus-level (1+ gnus-level-subscribed))))
6409      gnus-description-hashtb)
6410     (goto-char (point-min))
6411     (gnus-group-position-point)))
6412
6413 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6414 (defun gnus-group-apropos (regexp &optional search-description)
6415   "List all newsgroups that have names that match a regexp."
6416   (interactive "sGnus apropos (regexp): ")
6417   (let ((prev "")
6418         (obuf (current-buffer))
6419         groups des)
6420     ;; Go through all newsgroups that are known to Gnus.
6421     (mapatoms
6422      (lambda (group)
6423        (and (symbol-name group)
6424             (string-match regexp (symbol-name group))
6425             (setq groups (cons (symbol-name group) groups))))
6426      gnus-active-hashtb)
6427     ;; Also go through all descriptions that are known to Gnus.
6428     (when search-description
6429       (mapatoms
6430        (lambda (group)
6431          (and (string-match regexp (symbol-value group))
6432               (gnus-active (symbol-name group))
6433               (setq groups (cons (symbol-name group) groups))))
6434        gnus-description-hashtb))
6435     (if (not groups)
6436         (gnus-message 3 "No groups matched \"%s\"." regexp)
6437       ;; Print out all the groups.
6438       (save-excursion
6439         (pop-to-buffer "*Gnus Help*")
6440         (buffer-disable-undo (current-buffer))
6441         (erase-buffer)
6442         (setq groups (sort groups 'string<))
6443         (while groups
6444           ;; Groups may be entered twice into the list of groups.
6445           (if (not (string= (car groups) prev))
6446               (progn
6447                 (insert (setq prev (car groups)) "\n")
6448                 (if (and gnus-description-hashtb
6449                          (setq des (gnus-gethash (car groups)
6450                                                  gnus-description-hashtb)))
6451                     (insert "  " des "\n"))))
6452           (setq groups (cdr groups)))
6453         (goto-char (point-min))))
6454     (pop-to-buffer obuf)))
6455
6456 (defun gnus-group-description-apropos (regexp)
6457   "List all newsgroups that have names or descriptions that match a regexp."
6458   (interactive "sGnus description apropos (regexp): ")
6459   (if (not (or gnus-description-hashtb
6460                (gnus-read-all-descriptions-files)))
6461       (error "Couldn't request descriptions file"))
6462   (gnus-group-apropos regexp t))
6463
6464 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6465 (defun gnus-group-list-matching (level regexp &optional all lowest)
6466   "List all groups with unread articles that match REGEXP.
6467 If the prefix LEVEL is non-nil, it should be a number that says which
6468 level to cut off listing groups.
6469 If ALL, also list groups with no unread articles.
6470 If LOWEST, don't list groups with level lower than LOWEST."
6471   (interactive "P\nsList newsgroups matching: ")
6472   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6473                            all (or lowest 1) regexp)
6474   (goto-char (point-min))
6475   (gnus-group-position-point))
6476
6477 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6478   "List all groups that match REGEXP.
6479 If the prefix LEVEL is non-nil, it should be a number that says which
6480 level to cut off listing groups.
6481 If LOWEST, don't list groups with level lower than LOWEST."
6482   (interactive "P\nsList newsgroups matching: ")
6483   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6484
6485 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6486 (defun gnus-group-save-newsrc (&optional force)
6487   "Save the Gnus startup files.
6488 If FORCE, force saving whether it is necessary or not."
6489   (interactive "P")
6490   (gnus-save-newsrc-file force))
6491
6492 (defun gnus-group-restart (&optional arg)
6493   "Force Gnus to read the .newsrc file."
6494   (interactive "P")
6495   (when (gnus-yes-or-no-p
6496          (format "Are you sure you want to read %s? "
6497                  gnus-current-startup-file))
6498     (gnus-save-newsrc-file)
6499     (gnus-setup-news 'force)
6500     (gnus-group-list-groups arg)))
6501
6502 (defun gnus-group-read-init-file ()
6503   "Read the Gnus elisp init file."
6504   (interactive)
6505   (gnus-read-init-file))
6506
6507 (defun gnus-group-check-bogus-groups (&optional silent)
6508   "Check bogus newsgroups.
6509 If given a prefix, don't ask for confirmation before removing a bogus
6510 group."
6511   (interactive "P")
6512   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6513   (gnus-group-list-groups))
6514
6515 (defun gnus-group-edit-global-kill (&optional article group)
6516   "Edit the global kill file.
6517 If GROUP, edit that local kill file instead."
6518   (interactive "P")
6519   (setq gnus-current-kill-article article)
6520   (gnus-kill-file-edit-file group)
6521   (gnus-message
6522    6
6523    (substitute-command-keys
6524     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6525             (if group "local" "global")))))
6526
6527 (defun gnus-group-edit-local-kill (article group)
6528   "Edit a local kill file."
6529   (interactive (list nil (gnus-group-group-name)))
6530   (gnus-group-edit-global-kill article group))
6531
6532 (defun gnus-group-force-update ()
6533   "Update `.newsrc' file."
6534   (interactive)
6535   (gnus-save-newsrc-file))
6536
6537 (defun gnus-group-suspend ()
6538   "Suspend the current Gnus session.
6539 In fact, cleanup buffers except for group mode buffer.
6540 The hook gnus-suspend-gnus-hook is called before actually suspending."
6541   (interactive)
6542   (run-hooks 'gnus-suspend-gnus-hook)
6543   ;; Kill Gnus buffers except for group mode buffer.
6544   (let ((group-buf (get-buffer gnus-group-buffer)))
6545     ;; Do this on a separate list in case the user does a ^G before we finish
6546     (let ((gnus-buffer-list
6547            (delq group-buf (delq gnus-dribble-buffer
6548                                  (append gnus-buffer-list nil)))))
6549       (while gnus-buffer-list
6550         (gnus-kill-buffer (car gnus-buffer-list))
6551         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6552     (if group-buf
6553         (progn
6554           (setq gnus-buffer-list (list group-buf))
6555           (bury-buffer group-buf)
6556           (delete-windows-on group-buf t)))))
6557
6558 (defun gnus-group-clear-dribble ()
6559   "Clear all information from the dribble buffer."
6560   (interactive)
6561   (gnus-dribble-clear)
6562   (gnus-message 7 "Cleared dribble buffer"))
6563
6564 (defun gnus-group-exit ()
6565   "Quit reading news after updating .newsrc.eld and .newsrc.
6566 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6567   (interactive)
6568   (when 
6569       (or noninteractive                ;For gnus-batch-kill
6570           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6571           (not gnus-interactive-exit)   ;Without confirmation
6572           gnus-expert-user
6573           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6574     (run-hooks 'gnus-exit-gnus-hook)
6575     ;; Offer to save data from non-quitted summary buffers.
6576     (gnus-offer-save-summaries)
6577     ;; Save the newsrc file(s).
6578     (gnus-save-newsrc-file)
6579     ;; Kill-em-all.
6580     (gnus-close-backends)
6581     ;; Reset everything.
6582     (gnus-clear-system)
6583     ;; Allow the user to do things after cleaning up.
6584     (run-hooks 'gnus-after-exiting-gnus-hook)))
6585
6586 (defun gnus-close-backends ()
6587   ;; Send a close request to all backends that support such a request.
6588   (let ((methods gnus-valid-select-methods)
6589         func)
6590     (while methods
6591       (if (fboundp (setq func (intern (concat (caar methods)
6592                                               "-request-close"))))
6593           (funcall func))
6594       (setq methods (cdr methods)))))
6595
6596 (defun gnus-group-quit ()
6597   "Quit reading news without updating .newsrc.eld or .newsrc.
6598 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6599   (interactive)
6600   (when (or noninteractive              ;For gnus-batch-kill
6601             (zerop (buffer-size))
6602             (not (gnus-server-opened gnus-select-method))
6603             gnus-expert-user
6604             (not gnus-current-startup-file)
6605             (gnus-yes-or-no-p
6606              (format "Quit reading news without saving %s? "
6607                      (file-name-nondirectory gnus-current-startup-file))))
6608     (run-hooks 'gnus-exit-gnus-hook)
6609     (if gnus-use-full-window
6610         (delete-other-windows)
6611       (gnus-remove-some-windows))
6612     (gnus-dribble-save)
6613     (gnus-close-backends)
6614     (gnus-clear-system)
6615     ;; Allow the user to do things after cleaning up.
6616     (run-hooks 'gnus-after-exiting-gnus-hook)))
6617
6618 (defun gnus-offer-save-summaries ()
6619   "Offer to save all active summary buffers."
6620   (save-excursion
6621     (let ((buflist (buffer-list))
6622           buffers bufname)
6623       ;; Go through all buffers and find all summaries.
6624       (while buflist
6625         (and (setq bufname (buffer-name (car buflist)))
6626              (string-match "Summary" bufname)
6627              (save-excursion
6628                (set-buffer bufname)
6629                ;; We check that this is, indeed, a summary buffer.
6630                (and (eq major-mode 'gnus-summary-mode)
6631                     ;; Also make sure this isn't bogus.
6632                     gnus-newsgroup-prepared))
6633              (push bufname buffers))
6634         (setq buflist (cdr buflist)))
6635       ;; Go through all these summary buffers and offer to save them.
6636       (when buffers
6637         (map-y-or-n-p
6638          "Update summary buffer %s? "
6639          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6640          buffers)))))
6641
6642 (defun gnus-group-describe-briefly ()
6643   "Give a one line description of the group mode commands."
6644   (interactive)
6645   (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")))
6646
6647 (defun gnus-group-browse-foreign-server (method)
6648   "Browse a foreign news server.
6649 If called interactively, this function will ask for a select method
6650  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6651 If not, METHOD should be a list where the first element is the method
6652 and the second element is the address."
6653   (interactive
6654    (list (let ((how (completing-read
6655                      "Which backend: "
6656                      (append gnus-valid-select-methods gnus-server-alist)
6657                      nil t (cons "nntp" 0))))
6658            ;; We either got a backend name or a virtual server name.
6659            ;; If the first, we also need an address.
6660            (if (assoc how gnus-valid-select-methods)
6661                (list (intern how)
6662                      ;; Suggested by mapjph@bath.ac.uk.
6663                      (completing-read
6664                       "Address: "
6665                       (mapcar (lambda (server) (list server))
6666                               gnus-secondary-servers)))
6667              ;; We got a server name, so we find the method.
6668              (gnus-server-to-method how)))))
6669   (gnus-browse-foreign-server method))
6670
6671 \f
6672 ;;;
6673 ;;; Gnus summary mode
6674 ;;;
6675
6676 (defvar gnus-summary-mode-map nil)
6677
6678 (put 'gnus-summary-mode 'mode-class 'special)
6679
6680 (unless gnus-summary-mode-map
6681   (setq gnus-summary-mode-map (make-keymap))
6682   (suppress-keymap gnus-summary-mode-map)
6683
6684   ;; Non-orthogonal keys
6685
6686   (gnus-define-keys gnus-summary-mode-map
6687     " " gnus-summary-next-page
6688     "\177" gnus-summary-prev-page
6689     [delete] gnus-summary-prev-page
6690     "\r" gnus-summary-scroll-up
6691     "n" gnus-summary-next-unread-article
6692     "p" gnus-summary-prev-unread-article
6693     "N" gnus-summary-next-article
6694     "P" gnus-summary-prev-article
6695     "\M-\C-n" gnus-summary-next-same-subject
6696     "\M-\C-p" gnus-summary-prev-same-subject
6697     "\M-n" gnus-summary-next-unread-subject
6698     "\M-p" gnus-summary-prev-unread-subject
6699     "." gnus-summary-first-unread-article
6700     "," gnus-summary-best-unread-article
6701     "\M-s" gnus-summary-search-article-forward
6702     "\M-r" gnus-summary-search-article-backward
6703     "<" gnus-summary-beginning-of-article
6704     ">" gnus-summary-end-of-article
6705     "j" gnus-summary-goto-article
6706     "^" gnus-summary-refer-parent-article
6707     "\M-^" gnus-summary-refer-article
6708     "u" gnus-summary-tick-article-forward
6709     "!" gnus-summary-tick-article-forward
6710     "U" gnus-summary-tick-article-backward
6711     "d" gnus-summary-mark-as-read-forward
6712     "D" gnus-summary-mark-as-read-backward
6713     "E" gnus-summary-mark-as-expirable
6714     "\M-u" gnus-summary-clear-mark-forward
6715     "\M-U" gnus-summary-clear-mark-backward
6716     "k" gnus-summary-kill-same-subject-and-select
6717     "\C-k" gnus-summary-kill-same-subject
6718     "\M-\C-k" gnus-summary-kill-thread
6719     "\M-\C-l" gnus-summary-lower-thread
6720     "e" gnus-summary-edit-article
6721     "#" gnus-summary-mark-as-processable
6722     "\M-#" gnus-summary-unmark-as-processable
6723     "\M-\C-t" gnus-summary-toggle-threads
6724     "\M-\C-s" gnus-summary-show-thread
6725     "\M-\C-h" gnus-summary-hide-thread
6726     "\M-\C-f" gnus-summary-next-thread
6727     "\M-\C-b" gnus-summary-prev-thread
6728     "\M-\C-u" gnus-summary-up-thread
6729     "\M-\C-d" gnus-summary-down-thread
6730     "&" gnus-summary-execute-command
6731     "c" gnus-summary-catchup-and-exit
6732     "\C-w" gnus-summary-mark-region-as-read
6733     "\C-t" gnus-summary-toggle-truncation
6734     "?" gnus-summary-mark-as-dormant
6735     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6736     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6737     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6738     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6739     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6740     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6741     "=" gnus-summary-expand-window
6742     "\C-x\C-s" gnus-summary-reselect-current-group
6743     "\M-g" gnus-summary-rescan-group
6744     "w" gnus-summary-stop-page-breaking
6745     "\C-c\C-r" gnus-summary-caesar-message
6746     "\M-t" gnus-summary-toggle-mime
6747     "f" gnus-summary-followup
6748     "F" gnus-summary-followup-with-original
6749     "C" gnus-summary-cancel-article
6750     "r" gnus-summary-reply
6751     "R" gnus-summary-reply-with-original
6752     "\C-c\C-f" gnus-summary-mail-forward
6753     "o" gnus-summary-save-article
6754     "\C-o" gnus-summary-save-article-mail
6755     "|" gnus-summary-pipe-output
6756     "\M-k" gnus-summary-edit-local-kill
6757     "\M-K" gnus-summary-edit-global-kill
6758     "V" gnus-version
6759     "\C-c\C-d" gnus-summary-describe-group
6760     "q" gnus-summary-exit
6761     "Q" gnus-summary-exit-no-update
6762     "\C-c\C-i" gnus-info-find-node
6763     gnus-mouse-2 gnus-mouse-pick-article
6764     "m" gnus-summary-mail-other-window
6765     "a" gnus-summary-post-news
6766     "x" gnus-summary-limit-to-unread
6767     "s" gnus-summary-isearch-article
6768     "t" gnus-article-hide-headers
6769     "g" gnus-summary-show-article
6770     "l" gnus-summary-goto-last-article
6771     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6772     "\C-d" gnus-summary-enter-digest-group
6773     "\C-c\C-b" gnus-bug
6774     "*" gnus-cache-enter-article
6775     "\M-*" gnus-cache-remove-article
6776     "\M-&" gnus-summary-universal-argument
6777     "\C-l" gnus-recenter
6778     "I" gnus-summary-increase-score
6779     "L" gnus-summary-lower-score
6780
6781     "V" gnus-summary-score-map
6782     "X" gnus-uu-extract-map
6783     "S" gnus-summary-send-map)
6784
6785   ;; Sort of orthogonal keymap
6786   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6787     "t" gnus-summary-tick-article-forward
6788     "!" gnus-summary-tick-article-forward
6789     "d" gnus-summary-mark-as-read-forward
6790     "r" gnus-summary-mark-as-read-forward
6791     "c" gnus-summary-clear-mark-forward
6792     " " gnus-summary-clear-mark-forward
6793     "e" gnus-summary-mark-as-expirable
6794     "x" gnus-summary-mark-as-expirable
6795     "?" gnus-summary-mark-as-dormant
6796     "b" gnus-summary-set-bookmark
6797     "B" gnus-summary-remove-bookmark
6798     "#" gnus-summary-mark-as-processable
6799     "\M-#" gnus-summary-unmark-as-processable
6800     "S" gnus-summary-limit-include-expunged
6801     "C" gnus-summary-catchup
6802     "H" gnus-summary-catchup-to-here
6803     "\C-c" gnus-summary-catchup-all
6804     "k" gnus-summary-kill-same-subject-and-select
6805     "K" gnus-summary-kill-same-subject
6806     "P" gnus-uu-mark-map)
6807
6808   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6809     "c" gnus-summary-clear-above
6810     "u" gnus-summary-tick-above
6811     "m" gnus-summary-mark-above
6812     "k" gnus-summary-kill-below)
6813
6814   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6815     "/" gnus-summary-limit-to-subject
6816     "n" gnus-summary-limit-to-articles
6817     "w" gnus-summary-pop-limit
6818     "s" gnus-summary-limit-to-subject
6819     "a" gnus-summary-limit-to-author
6820     "u" gnus-summary-limit-to-unread
6821     "m" gnus-summary-limit-to-marks
6822     "v" gnus-summary-limit-to-score
6823     "D" gnus-summary-limit-include-dormant
6824     "d" gnus-summary-limit-exclude-dormant
6825     ;;  "t" gnus-summary-limit-exclude-thread
6826     "E" gnus-summary-limit-include-expunged
6827     "c" gnus-summary-limit-exclude-childless-dormant
6828     "C" gnus-summary-limit-mark-excluded-as-read)
6829
6830   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6831     "n" gnus-summary-next-unread-article
6832     "p" gnus-summary-prev-unread-article
6833     "N" gnus-summary-next-article
6834     "P" gnus-summary-prev-article
6835     "\C-n" gnus-summary-next-same-subject
6836     "\C-p" gnus-summary-prev-same-subject
6837     "\M-n" gnus-summary-next-unread-subject
6838     "\M-p" gnus-summary-prev-unread-subject
6839     "f" gnus-summary-first-unread-article
6840     "b" gnus-summary-best-unread-article
6841     "j" gnus-summary-goto-article
6842     "g" gnus-summary-goto-subject
6843     "l" gnus-summary-goto-last-article
6844     "p" gnus-summary-pop-article)
6845
6846   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6847     "k" gnus-summary-kill-thread
6848     "l" gnus-summary-lower-thread
6849     "i" gnus-summary-raise-thread
6850     "T" gnus-summary-toggle-threads
6851     "t" gnus-summary-rethread-current
6852     "^" gnus-summary-reparent-thread
6853     "s" gnus-summary-show-thread
6854     "S" gnus-summary-show-all-threads
6855     "h" gnus-summary-hide-thread
6856     "H" gnus-summary-hide-all-threads
6857     "n" gnus-summary-next-thread
6858     "p" gnus-summary-prev-thread
6859     "u" gnus-summary-up-thread
6860     "o" gnus-summary-top-thread
6861     "d" gnus-summary-down-thread
6862     "#" gnus-uu-mark-thread
6863     "\M-#" gnus-uu-unmark-thread)
6864
6865   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6866     "c" gnus-summary-catchup-and-exit
6867     "C" gnus-summary-catchup-all-and-exit
6868     "E" gnus-summary-exit-no-update
6869     "Q" gnus-summary-exit
6870     "Z" gnus-summary-exit
6871     "n" gnus-summary-catchup-and-goto-next-group
6872     "R" gnus-summary-reselect-current-group
6873     "G" gnus-summary-rescan-group
6874     "N" gnus-summary-next-group
6875     "P" gnus-summary-prev-group)
6876
6877   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6878     " " gnus-summary-next-page
6879     "n" gnus-summary-next-page
6880     "\177" gnus-summary-prev-page
6881     [delete] gnus-summary-prev-page
6882     "p" gnus-summary-prev-page
6883     "\r" gnus-summary-scroll-up
6884     "<" gnus-summary-beginning-of-article
6885     ">" gnus-summary-end-of-article
6886     "b" gnus-summary-beginning-of-article
6887     "e" gnus-summary-end-of-article
6888     "^" gnus-summary-refer-parent-article
6889     "r" gnus-summary-refer-parent-article
6890     "R" gnus-summary-refer-references
6891     "g" gnus-summary-show-article
6892     "s" gnus-summary-isearch-article)
6893
6894   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6895     "b" gnus-article-add-buttons
6896     "B" gnus-article-add-buttons-to-head
6897     "o" gnus-article-treat-overstrike
6898     ;;  "w" gnus-article-word-wrap
6899     "w" gnus-article-fill-cited-article
6900     "c" gnus-article-remove-cr
6901     "L" gnus-article-remove-trailing-blank-lines
6902     "q" gnus-article-de-quoted-unreadable
6903     "f" gnus-article-display-x-face
6904     "l" gnus-summary-stop-page-breaking
6905     "r" gnus-summary-caesar-message
6906     "t" gnus-article-hide-headers
6907     "v" gnus-summary-verbose-headers
6908     "m" gnus-summary-toggle-mime)
6909
6910   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6911     "a" gnus-article-hide
6912     "h" gnus-article-hide-headers
6913     "b" gnus-article-hide-boring-headers
6914     "s" gnus-article-hide-signature
6915     "c" gnus-article-hide-citation
6916     "p" gnus-article-hide-pgp
6917     "\C-c" gnus-article-hide-citation-maybe)
6918
6919   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6920     "a" gnus-article-highlight
6921     "h" gnus-article-highlight-headers
6922     "c" gnus-article-highlight-citation
6923     "s" gnus-article-highlight-signature)
6924
6925   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6926     "z" gnus-article-date-ut
6927     "u" gnus-article-date-ut
6928     "l" gnus-article-date-local
6929     "e" gnus-article-date-lapsed
6930     "o" gnus-article-date-original)
6931
6932   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6933     "v" gnus-version
6934     "f" gnus-summary-fetch-faq
6935     "d" gnus-summary-describe-group
6936     "h" gnus-summary-describe-briefly
6937     "i" gnus-info-find-node)
6938
6939   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6940     "e" gnus-summary-expire-articles
6941     "\M-\C-e" gnus-summary-expire-articles-now
6942     "\177" gnus-summary-delete-article
6943     [delete] gnus-summary-delete-article
6944     "m" gnus-summary-move-article
6945     "r" gnus-summary-respool-article
6946     "w" gnus-summary-edit-article
6947     "c" gnus-summary-copy-article
6948     "B" gnus-summary-crosspost-article
6949     "q" gnus-summary-respool-query
6950     "i" gnus-summary-import-article)
6951
6952   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6953     "o" gnus-summary-save-article
6954     "m" gnus-summary-save-article-mail
6955     "r" gnus-summary-save-article-rmail
6956     "f" gnus-summary-save-article-file
6957     "b" gnus-summary-save-article-body-file
6958     "h" gnus-summary-save-article-folder
6959     "v" gnus-summary-save-article-vm
6960     "p" gnus-summary-pipe-output
6961     "s" gnus-soup-add-article)
6962   )
6963
6964 \f
6965
6966 (defun gnus-summary-mode (&optional group)
6967   "Major mode for reading articles.
6968
6969 All normal editing commands are switched off.
6970 \\<gnus-summary-mode-map>
6971 Each line in this buffer represents one article.  To read an
6972 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6973 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6974 respectively.
6975
6976 You can also post articles and send mail from this buffer.  To
6977 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6978 of an article, type `\\[gnus-summary-reply]'.
6979
6980 There are approx. one gazillion commands you can execute in this
6981 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6982
6983 The following commands are available:
6984
6985 \\{gnus-summary-mode-map}"
6986   (interactive)
6987   (when (and menu-bar-mode
6988              (gnus-visual-p 'summary-menu 'menu))
6989     (gnus-summary-make-menu-bar))
6990   (kill-all-local-variables)
6991   (let ((locals gnus-summary-local-variables))
6992     (while locals
6993       (if (consp (car locals))
6994           (progn
6995             (make-local-variable (caar locals))
6996             (set (caar locals) (eval (cdar locals))))
6997         (make-local-variable (car locals))
6998         (set (car locals) nil))
6999       (setq locals (cdr locals))))
7000   (gnus-make-thread-indent-array)
7001   (gnus-simplify-mode-line)
7002   (setq major-mode 'gnus-summary-mode)
7003   (setq mode-name "Summary")
7004   (make-local-variable 'minor-mode-alist)
7005   (use-local-map gnus-summary-mode-map)
7006   (buffer-disable-undo (current-buffer))
7007   (setq buffer-read-only t)             ;Disable modification
7008   (setq truncate-lines t)
7009   (setq selective-display t)
7010   (setq selective-display-ellipses t)   ;Display `...'
7011   (setq buffer-display-table gnus-summary-display-table)
7012   (setq gnus-newsgroup-name group)
7013   (run-hooks 'gnus-summary-mode-hook))
7014
7015 (defun gnus-summary-make-display-table ()
7016   ;; Change the display table.  Odd characters have a tendency to mess
7017   ;; up nicely formatted displays - we make all possible glyphs
7018   ;; display only a single character.
7019
7020   ;; We start from the standard display table, if any.
7021   (setq gnus-summary-display-table
7022         (or (copy-sequence standard-display-table)
7023             (make-display-table)))
7024   ;; Nix out all the control chars...
7025   (let ((i 32))
7026     (while (>= (setq i (1- i)) 0)
7027       (aset gnus-summary-display-table i [??])))
7028   ;; ... but not newline and cr, of course. (cr is necessary for the
7029   ;; selective display).
7030   (aset gnus-summary-display-table ?\n nil)
7031   (aset gnus-summary-display-table ?\r nil)
7032   ;; We nix out any glyphs over 126 that are not set already.
7033   (let ((i 256))
7034     (while (>= (setq i (1- i)) 127)
7035       ;; Only modify if the entry is nil.
7036       (or (aref gnus-summary-display-table i)
7037           (aset gnus-summary-display-table i [??])))))
7038
7039 (defun gnus-summary-clear-local-variables ()
7040   (let ((locals gnus-summary-local-variables))
7041     (while locals
7042       (if (consp (car locals))
7043           (and (vectorp (caar locals))
7044                (set (caar locals) nil))
7045         (and (vectorp (car locals))
7046              (set (car locals) nil)))
7047       (setq locals (cdr locals)))))
7048
7049 ;; Summary data functions.
7050
7051 (defmacro gnus-data-number (data)
7052   `(car ,data))
7053
7054 (defmacro gnus-data-set-number (data number)
7055   `(setcar ,data ,number))
7056
7057 (defmacro gnus-data-mark (data)
7058   `(nth 1 ,data))
7059
7060 (defmacro gnus-data-set-mark (data mark)
7061   `(setcar (nthcdr 1 ,data) ,mark))
7062
7063 (defmacro gnus-data-pos (data)
7064   `(nth 2 ,data))
7065
7066 (defmacro gnus-data-set-pos (data pos)
7067   `(setcar (nthcdr 2 ,data) ,pos))
7068
7069 (defmacro gnus-data-header (data)
7070   `(nth 3 ,data))
7071
7072 (defmacro gnus-data-level (data)
7073   `(nth 4 ,data))
7074
7075 (defmacro gnus-data-unread-p (data)
7076   `(= (nth 1 ,data) gnus-unread-mark))
7077
7078 (defmacro gnus-data-pseudo-p (data)
7079   `(consp (nth 3 ,data)))
7080
7081 (defmacro gnus-data-find (number)
7082   `(assq ,number gnus-newsgroup-data))
7083
7084 (defmacro gnus-data-find-list (number &optional data)
7085   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7086      (memq (assq ,number bdata)
7087            bdata)))
7088
7089 (defmacro gnus-data-make (number mark pos header level)
7090   `(list ,number ,mark ,pos ,header ,level))
7091
7092 (defun gnus-data-enter (after-article number mark pos header level offset)
7093   (let ((data (gnus-data-find-list after-article)))
7094     (or data (error "No such article: %d" after-article))
7095     (setcdr data (cons (gnus-data-make number mark pos header level)
7096                        (cdr data)))
7097     (setq gnus-newsgroup-data-reverse nil)
7098     (gnus-data-update-list (cddr data) offset)))
7099
7100 (defun gnus-data-enter-list (after-article list &optional offset)
7101   (when list
7102     (let ((data (and after-article (gnus-data-find-list after-article)))
7103           (ilist list))
7104       (or data (not after-article) (error "No such article: %d" after-article))
7105       ;; Find the last element in the list to be spliced into the main
7106       ;; list.
7107       (while (cdr list)
7108         (setq list (cdr list)))
7109       (if (not data)
7110           (progn
7111             (setcdr list gnus-newsgroup-data)
7112             (setq gnus-newsgroup-data ilist)
7113             (and offset (gnus-data-update-list (cdr list) offset)))
7114         (setcdr list (cdr data))
7115         (setcdr data ilist)
7116         (and offset (gnus-data-update-list (cdr data) offset)))
7117       (setq gnus-newsgroup-data-reverse nil))))
7118
7119 (defun gnus-data-remove (article &optional offset)
7120   (let ((data gnus-newsgroup-data))
7121     (if (= (gnus-data-number (car data)) article)
7122         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7123               gnus-newsgroup-data-reverse nil)
7124       (while (cdr data)
7125         (and (= (gnus-data-number (cadr data)) article)
7126              (progn
7127                (setcdr data (cddr data))
7128                (and offset (gnus-data-update-list (cdr data) offset))
7129                (setq data nil
7130                      gnus-newsgroup-data-reverse nil)))
7131         (setq data (cdr data))))))
7132
7133 (defmacro gnus-data-list (backward)
7134   `(if ,backward
7135        (or gnus-newsgroup-data-reverse
7136            (setq gnus-newsgroup-data-reverse
7137                  (reverse gnus-newsgroup-data)))
7138      gnus-newsgroup-data))
7139
7140 (defun gnus-data-update-list (data offset)
7141   "Add OFFSET to the POS of all data entries in DATA."
7142   (while data
7143     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7144     (setq data (cdr data))))
7145
7146 (defun gnus-data-compute-positions ()
7147   "Compute the positions of all articles."
7148   (let ((data gnus-newsgroup-data)
7149         pos)
7150     (while data
7151       (when (setq pos (text-property-any
7152                        (point-min) (point-max)
7153                        'gnus-number (gnus-data-number (car data))))
7154         (gnus-data-set-pos (car data) (+ pos 3)))
7155       (setq data (cdr data)))))
7156
7157 (defun gnus-summary-article-pseudo-p (article)
7158   "Say whether this article is a pseudo article or not."
7159   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7160
7161 (defun gnus-article-parent-p (number)
7162   "Say whether this article is a parent or not."
7163   (let ((data (gnus-data-find-list number)))
7164     (and (cdr data)                     ; There has to be an article after...
7165          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7166             (gnus-data-level (nth 1 data))))))
7167
7168 (defun gnus-article-children (number)
7169   "Return a list of all children to NUMBER."
7170   (let* ((data (gnus-data-find-list number))
7171          (level (gnus-data-level (car data)))
7172          children)
7173     (setq data (cdr data))
7174     (while (and data            
7175                 (= (gnus-data-level (car data)) (1+ level)))
7176       (push (gnus-data-number (car data)) children)
7177       (setq data (cdr data)))
7178     children))
7179
7180 (defmacro gnus-summary-skip-intangible ()
7181   "If the current article is intangible, then jump to a different article."
7182   '(let ((to (get-text-property (point) 'gnus-intangible)))
7183     (and to (gnus-summary-goto-subject to))))
7184
7185 (defmacro gnus-summary-article-intangible-p ()
7186   "Say whether this article is intangible or not."
7187   '(get-text-property (point) 'gnus-intangible))
7188
7189 ;; Some summary mode macros.
7190
7191 (defmacro gnus-summary-article-number ()
7192   "The article number of the article on the current line.
7193 If there isn's an article number here, then we return the current
7194 article number."
7195   '(progn
7196      (gnus-summary-skip-intangible)
7197      (or (get-text-property (point) 'gnus-number)
7198          (gnus-summary-last-subject))))
7199
7200 (defmacro gnus-summary-article-header (&optional number)
7201   `(gnus-data-header (gnus-data-find
7202                       ,(or number '(gnus-summary-article-number)))))
7203
7204 (defmacro gnus-summary-thread-level (&optional number)
7205   `(if (and (eq gnus-summary-make-false-root 'dummy)
7206             (get-text-property (point) 'gnus-intangible))
7207        0
7208      (gnus-data-level (gnus-data-find
7209                        ,(or number '(gnus-summary-article-number))))))
7210
7211 (defmacro gnus-summary-article-mark (&optional number)
7212   `(gnus-data-mark (gnus-data-find
7213                     ,(or number '(gnus-summary-article-number)))))
7214
7215 (defmacro gnus-summary-article-pos (&optional number)
7216   `(gnus-data-pos (gnus-data-find
7217                    ,(or number '(gnus-summary-article-number)))))
7218
7219 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7220 (defmacro gnus-summary-article-subject (&optional number)
7221   "Return current subject string or nil if nothing."
7222   `(let ((headers
7223           ,(if number
7224                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7225              '(gnus-data-header (assq (gnus-summary-article-number)
7226                                       gnus-newsgroup-data)))))
7227      (and headers
7228           (vectorp headers)
7229           (mail-header-subject headers))))
7230
7231 (defmacro gnus-summary-article-score (&optional number)
7232   "Return current article score."
7233   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7234                   gnus-newsgroup-scored))
7235        gnus-summary-default-score 0))
7236
7237 (defun gnus-summary-article-children (&optional number)
7238   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7239          (level (gnus-data-level (car data)))
7240          l children)
7241     (while (and (setq data (cdr data))
7242                 (> (setq l (gnus-data-level (car data))) level))
7243       (and (= (1+ level) l)
7244            (setq children (cons (gnus-data-number (car data))
7245                                 children))))
7246     (nreverse children)))
7247
7248 (defun gnus-summary-article-parent (&optional number)
7249   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7250                                     (gnus-data-list t)))
7251          (level (gnus-data-level (car data))))
7252     (if (zerop level)
7253         () ; This is a root.
7254       ;; We search until we find an article with a level less than
7255       ;; this one.  That function has to be the parent.
7256       (while (and (setq data (cdr data))
7257                   (not (< (gnus-data-level (car data)) level))))
7258       (and data (gnus-data-number (car data))))))
7259
7260 (defun gnus-unread-mark-p (mark)
7261   "Say whether MARK is the unread mark."
7262   (= mark gnus-unread-mark))
7263
7264 (defun gnus-read-mark-p (mark)
7265   "Say whether MARK is one of the marks that mark as read.
7266 This is all marks except unread, ticked, dormant, and expirable."
7267   (not (or (= mark gnus-unread-mark)
7268            (= mark gnus-ticked-mark)
7269            (= mark gnus-dormant-mark)
7270            (= mark gnus-expirable-mark))))
7271
7272 ;; Various summary mode internalish functions.
7273
7274 (defun gnus-mouse-pick-article (e)
7275   (interactive "e")
7276   (mouse-set-point e)
7277   (gnus-summary-next-page nil t))
7278
7279 (defun gnus-summary-setup-buffer (group)
7280   "Initialize summary buffer."
7281   (let ((buffer (concat "*Summary " group "*")))
7282     (if (get-buffer buffer)
7283         (progn
7284           (set-buffer buffer)
7285           (setq gnus-summary-buffer (current-buffer))
7286           (not gnus-newsgroup-prepared))
7287       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7288       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7289       (gnus-add-current-to-buffer-list)
7290       (gnus-summary-mode group)
7291       (when gnus-carpal
7292         (gnus-carpal-setup-buffer 'summary))
7293       (unless gnus-single-article-buffer
7294         (make-local-variable 'gnus-article-buffer)
7295         (make-local-variable 'gnus-article-current)
7296         (make-local-variable 'gnus-original-article-buffer))
7297       (setq gnus-newsgroup-name group)
7298       t)))
7299
7300 (defun gnus-set-global-variables ()
7301   ;; Set the global equivalents of the summary buffer-local variables
7302   ;; to the latest values they had.  These reflect the summary buffer
7303   ;; that was in action when the last article was fetched.
7304   (when (eq major-mode 'gnus-summary-mode)
7305     (setq gnus-summary-buffer (current-buffer))
7306     (let ((name gnus-newsgroup-name)
7307           (marked gnus-newsgroup-marked)
7308           (unread gnus-newsgroup-unreads)
7309           (headers gnus-current-headers)
7310           (data gnus-newsgroup-data)
7311           (summary gnus-summary-buffer)
7312           (article-buffer gnus-article-buffer)
7313           (original gnus-original-article-buffer)
7314           (gac gnus-article-current)
7315           (score-file gnus-current-score-file))
7316       (save-excursion
7317         (set-buffer gnus-group-buffer)
7318         (setq gnus-newsgroup-name name)
7319         (setq gnus-newsgroup-marked marked)
7320         (setq gnus-newsgroup-unreads unread)
7321         (setq gnus-current-headers headers)
7322         (setq gnus-newsgroup-data data)
7323         (setq gnus-article-current gac)
7324         (setq gnus-summary-buffer summary)
7325         (setq gnus-article-buffer article-buffer)
7326         (setq gnus-original-article-buffer original)
7327         (setq gnus-current-score-file score-file)))))
7328
7329 (defun gnus-summary-last-article-p (&optional article)
7330   "Return whether ARTICLE is the last article in the buffer."
7331   (if (not (setq article (or article (gnus-summary-article-number))))
7332       t ; All non-existant numbers are the last article. :-)
7333     (cdr (gnus-data-find-list article))))
7334
7335 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7336   "Insert a dummy root in the summary buffer."
7337   (beginning-of-line)
7338   (add-text-properties
7339    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7340    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7341
7342 (defvar gnus-thread-indent-array nil)
7343 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7344 (defun gnus-make-thread-indent-array ()
7345   (let ((n 200))
7346     (unless (and gnus-thread-indent-array
7347                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7348       (setq gnus-thread-indent-array (make-vector 201 "")
7349             gnus-thread-indent-array-level gnus-thread-indent-level)
7350       (while (>= n 0)
7351         (aset gnus-thread-indent-array n
7352               (make-string (* n gnus-thread-indent-level) ? ))
7353         (setq n (1- n))))))
7354
7355 (defun gnus-summary-insert-line
7356   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7357                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7358                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7359   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7360          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7361          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7362          (gnus-tmp-score-char
7363           (if (or (null gnus-summary-default-score)
7364                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7365                       gnus-summary-zcore-fuzz)) ? 
7366             (if (< gnus-tmp-score gnus-summary-default-score)
7367                 gnus-score-below-mark gnus-score-over-mark)))
7368          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7369                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7370                                   gnus-cached-mark)
7371                                  (gnus-tmp-replied gnus-replied-mark)
7372                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7373                                   gnus-saved-mark)
7374                                  (t gnus-unread-mark)))
7375          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7376          (gnus-tmp-name
7377           (cond
7378            ((string-match "(.+)" gnus-tmp-from)
7379             (substring gnus-tmp-from
7380                        (1+ (match-beginning 0)) (1- (match-end 0))))
7381            ((string-match "<[^>]+> *$" gnus-tmp-from)
7382             (let ((beg (match-beginning 0)))
7383               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7384                        (substring gnus-tmp-from (1+ (match-beginning 0))
7385                                   (1- (match-end 0))))
7386                   (substring gnus-tmp-from 0 beg))))
7387            (t gnus-tmp-from)))
7388          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7389          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7390          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7391          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7392          (buffer-read-only nil))
7393     (when (string= gnus-tmp-name "")
7394       (setq gnus-tmp-name gnus-tmp-from))
7395     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7396     (put-text-property
7397      (point)
7398      (progn (eval gnus-summary-line-format-spec) (point))
7399      'gnus-number gnus-tmp-number)
7400     (when (gnus-visual-p 'summary-highlight 'highlight)
7401       (forward-line -1)
7402       (run-hooks 'gnus-summary-update-hook)
7403       (forward-line 1))))
7404
7405 (defun gnus-summary-update-line (&optional dont-update)
7406   ;; Update summary line after change.
7407   (when (and gnus-summary-default-score
7408              (not gnus-summary-inhibit-highlight))
7409     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7410            (article (gnus-summary-article-number))
7411            (score (gnus-summary-article-score article)))
7412       (unless dont-update
7413         (if (and gnus-summary-mark-below
7414                  (< (gnus-summary-article-score)
7415                     gnus-summary-mark-below))
7416             ;; This article has a low score, so we mark it as read.
7417             (when (memq article gnus-newsgroup-unreads)
7418               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7419           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7420             ;; This article was previously marked as read on account
7421             ;; of a low score, but now it has risen, so we mark it as
7422             ;; unread.
7423             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7424         (gnus-summary-update-mark
7425          (if (or (null gnus-summary-default-score)
7426                  (<= (abs (- score gnus-summary-default-score))
7427                      gnus-summary-zcore-fuzz)) ? 
7428            (if (< score gnus-summary-default-score)
7429                gnus-score-below-mark gnus-score-over-mark)) 'score))
7430       ;; Do visual highlighting.
7431       (when (gnus-visual-p 'summary-highlight 'highlight)
7432         (run-hooks 'gnus-summary-update-hook)))))
7433
7434 (defvar gnus-tmp-new-adopts nil)
7435
7436 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7437   ;; Sum up all elements (and sub-elements) in a list.
7438   (let* ((number
7439           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7440           (cond
7441            ((and (consp thread) (cdr thread))
7442             (apply
7443              '+ 1 (mapcar
7444                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7445            ((null thread)
7446             1)
7447            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7448             1)
7449            (t 1))))
7450     (when (and level (zerop level) gnus-tmp-new-adopts)
7451       (incf number
7452             (apply '+ (mapcar
7453                        'gnus-summary-number-of-articles-in-thread
7454                        gnus-tmp-new-adopts))))
7455     (if char
7456         (if (> number 1) gnus-not-empty-thread-mark
7457           gnus-empty-thread-mark)
7458       number)))
7459
7460 (defun gnus-summary-set-local-parameters (group)
7461  "Go through the local params of GROUP and set all variable specs in that list."
7462   (let ((params (gnus-info-params (gnus-get-info group)))
7463         elem)
7464     (while params
7465       (setq elem (car params)
7466             params (cdr params))
7467       (and (consp elem)                 ; Has to be a cons.
7468            (consp (cdr elem))           ; The cdr has to be a list.
7469            (symbolp (car elem))         ; Has to be a symbol in there.
7470            (not (memq (car elem) 
7471                       '(quit-config to-address to-list to-group)))
7472            (progn                       ; So we set it.
7473              (make-local-variable (car elem))
7474              (set (car elem) (eval (nth 1 elem))))))))
7475
7476 (defun gnus-summary-read-group (group &optional show-all no-article
7477                                       kill-buffer no-display)
7478   "Start reading news in newsgroup GROUP.
7479 If SHOW-ALL is non-nil, already read articles are also listed.
7480 If NO-ARTICLE is non-nil, no article is selected initially.
7481 If NO-DISPLAY, don't generate a summary buffer."
7482   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7483   (let* ((new-group (gnus-summary-setup-buffer group))
7484          (quit-config (gnus-group-quit-config group))
7485          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7486     (cond
7487      ;; This summary buffer exists already, so we just select it.
7488      ((not new-group)
7489       (gnus-set-global-variables)
7490       (when kill-buffer
7491         (gnus-kill-or-deaden-summary kill-buffer))
7492       (gnus-configure-windows 'summary 'force)
7493       (gnus-set-mode-line 'summary)
7494       (gnus-summary-position-point)
7495       (message "")
7496       t)
7497      ;; We couldn't select this group.
7498      ((null did-select)
7499       (when (and (eq major-mode 'gnus-summary-mode)
7500                  (not (equal (current-buffer) kill-buffer)))
7501         (kill-buffer (current-buffer))
7502         (if (not quit-config)
7503             (progn
7504               (set-buffer gnus-group-buffer)
7505               (gnus-group-jump-to-group group)
7506               (gnus-group-next-unread-group 1))
7507           (if (not (buffer-name (car quit-config)))
7508               (gnus-configure-windows 'group 'force)
7509             (set-buffer (car quit-config))
7510             (and (eq major-mode 'gnus-summary-mode)
7511                  (gnus-set-global-variables))
7512             (gnus-configure-windows (cdr quit-config)))))
7513       (gnus-message 3 "Can't select group")
7514       nil)
7515      ;; The user did a `C-g' while prompting for number of articles,
7516      ;; so we exit this group.
7517      ((eq did-select 'quit)
7518       (and (eq major-mode 'gnus-summary-mode)
7519            (not (equal (current-buffer) kill-buffer))
7520            (kill-buffer (current-buffer)))
7521       (when kill-buffer
7522         (gnus-kill-or-deaden-summary kill-buffer))
7523       (if (not quit-config)
7524           (progn
7525             (set-buffer gnus-group-buffer)
7526             (gnus-group-jump-to-group group)
7527             (gnus-group-next-unread-group 1)
7528             (gnus-configure-windows 'group 'force))
7529         (if (not (buffer-name (car quit-config)))
7530             (gnus-configure-windows 'group 'force)
7531           (set-buffer (car quit-config))
7532           (and (eq major-mode 'gnus-summary-mode)
7533                (gnus-set-global-variables))
7534           (gnus-configure-windows (cdr quit-config))))
7535       ;; Finally signal the quit.
7536       (signal 'quit nil))
7537      ;; The group was successfully selected.
7538      (t
7539       (gnus-set-global-variables)
7540       ;; Save the active value in effect when the group was entered.
7541       (setq gnus-newsgroup-active
7542             (gnus-copy-sequence
7543              (gnus-active gnus-newsgroup-name)))
7544       ;; You can change the summary buffer in some way with this hook.
7545       (run-hooks 'gnus-select-group-hook)
7546       ;; Set any local variables in the group parameters.
7547       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7548       (gnus-update-format-specifications)
7549       ;; Do score processing.
7550       (when gnus-use-scoring
7551         (gnus-possibly-score-headers))
7552       ;; Check whether to fill in the gaps in the threads.
7553       (when gnus-build-sparse-threads
7554         (gnus-build-sparse-threads))
7555       ;; Find the initial limit.
7556       (if show-all
7557           (let ((gnus-newsgroup-dormant nil))
7558             (gnus-summary-initial-limit show-all))
7559         (gnus-summary-initial-limit show-all))
7560       ;; Generate the summary buffer.
7561       (unless no-display
7562         (gnus-summary-prepare))
7563       (when gnus-use-trees
7564         (gnus-tree-open group)
7565         (setq gnus-summary-highlight-line-function
7566               'gnus-tree-highlight-article))
7567       ;; If the summary buffer is empty, but there are some low-scored
7568       ;; articles or some excluded dormants, we include these in the
7569       ;; buffer.
7570       (when (and (zerop (buffer-size))
7571                  (not no-display))
7572         (cond (gnus-newsgroup-dormant
7573                (gnus-summary-limit-include-dormant))
7574               ((and gnus-newsgroup-scored show-all)
7575                (gnus-summary-limit-include-expunged))))
7576       ;; Function `gnus-apply-kill-file' must be called in this hook.
7577       (run-hooks 'gnus-apply-kill-hook)
7578       (if (and (zerop (buffer-size))
7579                (not no-display))
7580           (progn
7581             ;; This newsgroup is empty.
7582             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7583             (gnus-message 6 "No unread news")
7584             (when kill-buffer
7585               (gnus-kill-or-deaden-summary kill-buffer))
7586             ;; Return nil from this function.
7587             nil)
7588         ;; Hide conversation thread subtrees.  We cannot do this in
7589         ;; gnus-summary-prepare-hook since kill processing may not
7590         ;; work with hidden articles.
7591         (and gnus-show-threads
7592              gnus-thread-hide-subtree
7593              (gnus-summary-hide-all-threads))
7594         ;; Show first unread article if requested.
7595         (if (and (not no-article)
7596                  (not no-display)
7597                  gnus-newsgroup-unreads
7598                  gnus-auto-select-first)
7599             (if (eq gnus-auto-select-first 'best)
7600                 (gnus-summary-best-unread-article)
7601               (gnus-summary-first-unread-article))
7602           ;; Don't select any articles, just move point to the first
7603           ;; article in the group.
7604           (goto-char (point-min))
7605           (gnus-summary-position-point)
7606           (gnus-set-mode-line 'summary)
7607           (gnus-configure-windows 'summary 'force))
7608         ;; If we are in async mode, we send some info to the backend.
7609         (when gnus-newsgroup-async
7610           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7611         (when kill-buffer
7612           (gnus-kill-or-deaden-summary kill-buffer))
7613         (when (get-buffer-window gnus-group-buffer t)
7614           ;; Gotta use windows, because recenter does wierd stuff if
7615           ;; the current buffer ain't the displayed window.
7616           (let ((owin (selected-window)))
7617             (select-window (get-buffer-window gnus-group-buffer t))
7618             (when (gnus-group-goto-group group)
7619               (recenter))
7620             (select-window owin))))
7621       ;; Mark this buffer as "prepared".
7622       (setq gnus-newsgroup-prepared t)
7623       t))))
7624
7625 (defun gnus-summary-prepare ()
7626   "Generate the summary buffer."
7627   (let ((buffer-read-only nil))
7628     (erase-buffer)
7629     (setq gnus-newsgroup-data nil
7630           gnus-newsgroup-data-reverse nil)
7631     (run-hooks 'gnus-summary-generate-hook)
7632     ;; Generate the buffer, either with threads or without.
7633     (when gnus-newsgroup-headers
7634       (gnus-summary-prepare-threads
7635        (if gnus-show-threads
7636            (gnus-sort-gathered-threads
7637             (funcall gnus-summary-thread-gathering-function
7638                      (gnus-sort-threads
7639                       (gnus-cut-threads (gnus-make-threads)))))
7640          ;; Unthreaded display.
7641          (gnus-sort-articles gnus-newsgroup-headers))))
7642     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7643     ;; Call hooks for modifying summary buffer.
7644     (goto-char (point-min))
7645     (run-hooks 'gnus-summary-prepare-hook)))
7646
7647 (defun gnus-gather-threads-by-subject (threads)
7648   "Gather threads by looking at Subject headers."
7649   (if (not gnus-summary-make-false-root)
7650       threads
7651     (let ((hashtb (gnus-make-hashtable 1023))
7652           (prev threads)
7653           (result threads)
7654           subject hthread whole-subject)
7655       (while threads
7656         (setq whole-subject (mail-header-subject (caar threads)))
7657         (setq subject
7658               (cond
7659                ;; Truncate the subject.
7660                ((numberp gnus-summary-gather-subject-limit)
7661                 (setq subject (gnus-simplify-subject-re whole-subject))
7662                 (if (> (length subject) gnus-summary-gather-subject-limit)
7663                     (substring subject 0 gnus-summary-gather-subject-limit)
7664                   subject))
7665                ;; Fuzzily simplify it.
7666                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7667                 (gnus-simplify-subject-fuzzy whole-subject))
7668                ;; Just remove the leading "Re:".
7669                (t
7670                 (gnus-simplify-subject-re whole-subject))))
7671
7672         (if (and gnus-summary-gather-exclude-subject
7673                  (string-match gnus-summary-gather-exclude-subject
7674                                subject))
7675             ()          ; We don't want to do anything with this article.
7676           ;; We simplify the subject before looking it up in the
7677           ;; hash table.
7678
7679           (if (setq hthread (gnus-gethash subject hashtb))
7680               (progn
7681                 ;; We enter a dummy root into the thread, if we
7682                 ;; haven't done that already.
7683                 (unless (stringp (caar hthread))
7684                   (setcar hthread (list whole-subject (car hthread))))
7685                 ;; We add this new gathered thread to this gathered
7686                 ;; thread.
7687                 (setcdr (car hthread)
7688                         (nconc (cdar hthread) (list (car threads))))
7689                 ;; Remove it from the list of threads.
7690                 (setcdr prev (cdr threads))
7691                 (setq threads prev))
7692             ;; Enter this thread into the hash table.
7693             (gnus-sethash subject threads hashtb)))
7694         (setq prev threads)
7695         (setq threads (cdr threads)))
7696       result)))
7697
7698 (defun gnus-gather-threads-by-references (threads)
7699   "Gather threads by looking at References headers."
7700   (let ((idhashtb (gnus-make-hashtable 1023))
7701         (thhashtb (gnus-make-hashtable 1023))
7702         (prev threads)
7703         (result threads)
7704         ids references id gthread gid entered)
7705     (while threads
7706       (when (setq references (mail-header-references (caar threads)))
7707         (setq id (mail-header-id (caar threads)))
7708         (setq ids (gnus-split-references references))
7709         (setq entered nil)
7710         (while ids
7711           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7712               (progn
7713                 (gnus-sethash (car ids) id idhashtb)
7714                 (gnus-sethash id threads thhashtb))
7715             (setq gthread (gnus-gethash gid thhashtb))
7716             (unless entered
7717               ;; We enter a dummy root into the thread, if we
7718               ;; haven't done that already.
7719               (unless (stringp (caar gthread))
7720                 (setcar gthread (list (mail-header-subject (caar gthread))
7721                                       (car gthread))))
7722               ;; We add this new gathered thread to this gathered
7723               ;; thread.
7724               (setcdr (car gthread)
7725                       (nconc (cdar gthread) (list (car threads)))))
7726             ;; Add it into the thread hash table.
7727             (gnus-sethash id gthread thhashtb)
7728             (setq entered t)
7729             ;; Remove it from the list of threads.
7730             (setcdr prev (cdr threads))
7731             (setq threads prev))
7732           (setq ids (cdr ids))))
7733       (setq prev threads)
7734       (setq threads (cdr threads)))
7735     result))
7736
7737 (defun gnus-sort-gathered-threads (threads)
7738   "Sort subtreads inside each gathered thread by article number."
7739   (let ((result threads))
7740     (while threads
7741       (when (stringp (caar threads))
7742         (setcdr (car threads)
7743                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7744       (setq threads (cdr threads)))
7745     result))
7746
7747 (defun gnus-make-threads ()
7748   "Go through the dependency hashtb and find the roots.  Return all threads."
7749   (let (threads)
7750     (mapatoms
7751      (lambda (refs)
7752        (unless (car (symbol-value refs))
7753          ;; These threads do not refer back to any other articles,
7754          ;; so they're roots.
7755          (setq threads (append (cdr (symbol-value refs)) threads))))
7756      gnus-newsgroup-dependencies)
7757     threads))
7758
7759 (defun gnus-build-sparse-threads ()
7760   (let ((headers gnus-newsgroup-headers)
7761         (deps gnus-newsgroup-dependencies)
7762         header references generation relations 
7763         cthread subject child end pthread relation)
7764     ;; First we create an alist of generations/relations, where 
7765     ;; generations is how much we trust the ralation, and the relation
7766     ;; is parent/child.
7767     (gnus-message 7 "Making sparse threads...")
7768     (save-excursion
7769       (nnheader-set-temp-buffer " *gnus sparse threads*")
7770       (while (setq header (pop headers))
7771         (when (and (setq references (mail-header-references header))
7772                    (not (string= references "")))
7773           (insert references)
7774           (setq child (mail-header-id header)
7775                 subject (mail-header-subject header))
7776           (setq generation 0)
7777           (while (search-backward ">" nil t)
7778             (setq end (1+ (point)))
7779             (when (search-backward "<" nil t)
7780               (push (list (incf generation) 
7781                           child (setq child (buffer-substring (point) end))
7782                           subject)
7783                     relations)))
7784           (push (list (1+ generation) child nil subject) relations)
7785           (erase-buffer)))
7786       (kill-buffer (current-buffer)))
7787     ;; Sort over trustworthiness.
7788     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7789     (while (setq relation (pop relations))
7790       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7791                 (unless (car (symbol-value cthread))
7792                   ;; Make this article the parent of these threads.
7793                   (setcar (symbol-value cthread)
7794                           (vector gnus-reffed-article-number 
7795                                   (cadddr relation) 
7796                                   "" ""
7797                                   (cadr relation) 
7798                                   (or (caddr relation) "") 0 0 "")))
7799               (set cthread (list (vector gnus-reffed-article-number
7800                                          (cadddr relation) 
7801                                          "" "" (cadr relation) 
7802                                          (or (caddr relation) "") 0 0 ""))))
7803         (push gnus-reffed-article-number gnus-newsgroup-limit)
7804         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7805         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7806               gnus-newsgroup-reads)
7807         (decf gnus-reffed-article-number)
7808         ;; Make this new thread the child of its parent.
7809         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7810             (setcdr (symbol-value pthread)
7811                     (nconc (cdr (symbol-value pthread))
7812                            (list (symbol-value cthread))))
7813           (set pthread (list nil (symbol-value cthread))))))
7814     (gnus-message 7 "Making sparse threads...done")))
7815
7816 (defun gnus-build-old-threads ()
7817   ;; Look at all the articles that refer back to old articles, and
7818   ;; fetch the headers for the articles that aren't there.  This will
7819   ;; build complete threads - if the roots haven't been expired by the
7820   ;; server, that is.
7821   (let (id heads)
7822     (mapatoms
7823      (lambda (refs)
7824        (when (not (car (symbol-value refs)))
7825          (setq heads (cdr (symbol-value refs)))
7826          (while heads
7827            (if (memq (mail-header-number (caar heads))
7828                      gnus-newsgroup-dormant)
7829                (setq heads (cdr heads))
7830              (setq id (symbol-name refs))
7831              (while (and (setq id (gnus-build-get-header id))
7832                          (not (car (gnus-gethash
7833                                     id gnus-newsgroup-dependencies)))))
7834              (setq heads nil)))))
7835      gnus-newsgroup-dependencies)))
7836
7837 (defun gnus-build-get-header (id)
7838   ;; Look through the buffer of NOV lines and find the header to
7839   ;; ID.  Enter this line into the dependencies hash table, and return
7840   ;; the id of the parent article (if any).
7841   (let ((deps gnus-newsgroup-dependencies)
7842         found header)
7843     (prog1
7844         (save-excursion
7845           (set-buffer nntp-server-buffer)
7846           (goto-char (point-min))
7847           (while (and (not found) (search-forward id nil t))
7848             (beginning-of-line)
7849             (setq found (looking-at
7850                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7851                                  (regexp-quote id))))
7852             (or found (beginning-of-line 2)))
7853           (when found
7854             (beginning-of-line)
7855             (and
7856              (setq header (gnus-nov-parse-line
7857                            (read (current-buffer)) deps))
7858              (gnus-parent-id (mail-header-references header)))))
7859       (when header
7860         (let ((number (mail-header-number header)))
7861           (push number gnus-newsgroup-limit)
7862           (push header gnus-newsgroup-headers)
7863           (if (memq number gnus-newsgroup-unselected)
7864               (progn
7865                 (push number gnus-newsgroup-unreads)
7866                 (setq gnus-newsgroup-unselected
7867                       (delq number gnus-newsgroup-unselected)))
7868             (push number gnus-newsgroup-ancient)))))))
7869
7870 (defun gnus-summary-update-article (article &optional header)
7871   "Update ARTICLE in the summary buffer."
7872   (set-buffer gnus-summary-buffer)
7873   (let* ((header (or header (gnus-summary-article-header article)))
7874          (id (mail-header-id header))
7875          (data (gnus-data-find article))
7876          (thread (gnus-id-to-thread id))
7877          (parent
7878           (gnus-id-to-thread (or (gnus-parent-id 
7879                                   (mail-header-references header))
7880                                  "tull")))
7881          (buffer-read-only nil)
7882          (old (car thread))
7883          (number (mail-header-number header))
7884          pos)
7885     (when thread
7886       (setcar thread nil)
7887       (when parent
7888         (delq thread parent))
7889       (if (gnus-summary-insert-subject id header)
7890           ;; Set the (possibly) new article number in the data structure.
7891           (gnus-data-set-number data (gnus-id-to-article id))
7892         (setcar thread old)
7893         nil))))
7894
7895 (defun gnus-rebuild-thread (id)
7896   "Rebuild the thread containing ID."
7897   (let ((buffer-read-only nil)
7898         current thread data)
7899     (if (not gnus-show-threads)
7900         (setq thread (list (car (gnus-id-to-thread id))))
7901       ;; Get the thread this article is part of.
7902       (setq thread (gnus-remove-thread id)))
7903     (setq current (save-excursion
7904                     (and (zerop (forward-line -1))
7905                          (gnus-summary-article-number))))
7906     ;; If this is a gathered thread, we have to go some re-gathering.
7907     (when (stringp (car thread))
7908       (let ((subject (car thread))
7909             roots thr)
7910         (setq thread (cdr thread))
7911         (while thread
7912           (unless (memq (setq thr (gnus-id-to-thread
7913                                       (gnus-root-id
7914                                        (mail-header-id (caar thread)))))
7915                         roots)
7916             (push thr roots))
7917           (setq thread (cdr thread)))
7918         ;; We now have all (unique) roots.
7919         (if (= (length roots) 1)
7920             ;; All the loose roots are now one solid root.
7921             (setq thread (car roots))
7922           (setq thread (cons subject (gnus-sort-threads roots))))))
7923     (let (threads)
7924       ;; We then insert this thread into the summary buffer.
7925       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7926         (gnus-summary-prepare-threads (list thread))
7927         (setq data (nreverse gnus-newsgroup-data))
7928         (setq threads gnus-newsgroup-threads))
7929       ;; We splice the new data into the data structure.
7930       (gnus-data-enter-list current data)
7931       (gnus-data-compute-positions)
7932       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7933
7934 (defun gnus-id-to-thread (id)
7935   "Return the (sub-)thread where ID appears."
7936   (gnus-gethash id gnus-newsgroup-dependencies))
7937
7938 (defun gnus-id-to-article (id)
7939   "Return the article number of ID."
7940   (let ((thread (gnus-id-to-thread id)))
7941     (when thread
7942       (mail-header-number (car thread)))))
7943
7944 (defun gnus-id-to-header (id)
7945   "Return the article headers of ID."
7946   (car (gnus-id-to-thread id)))
7947
7948 (defun gnus-article-displayed-root-p (article)
7949   "Say whether ARTICLE is a root(ish) article."
7950   (let ((level (gnus-summary-thread-level article))
7951         particle)
7952     (cond 
7953      ((null level) nil)
7954      ((zerop level) t)
7955      ((and (= 1 level)
7956            (null (setq particle (gnus-id-to-article
7957                                  (gnus-parent-id 
7958                                   (mail-header-references 
7959                                    (gnus-summary-article-header article))))))
7960            (null (gnus-summary-thread-level particle)))))))
7961
7962 (defun gnus-root-id (id)
7963   "Return the id of the root of the thread where ID appears."
7964   (let (last-id prev)
7965     (while (and id (setq prev (car (gnus-gethash 
7966                                     id gnus-newsgroup-dependencies))))
7967       (setq last-id id
7968             id (gnus-parent-id (mail-header-references prev))))
7969     last-id))
7970
7971 (defun gnus-remove-thread (id &optional dont-remove)
7972   "Remove the thread that has ID in it."
7973   (let ((dep gnus-newsgroup-dependencies)
7974         headers thread last-id)
7975     ;; First go up in this thread until we find the root.
7976     (setq last-id (gnus-root-id id))
7977     (setq headers (list (car (gnus-id-to-thread last-id))
7978                         (caadr (gnus-id-to-thread last-id))))
7979     ;; We have now found the real root of this thread.  It might have
7980     ;; been gathered into some loose thread, so we have to search
7981     ;; through the threads to find the thread we wanted.
7982     (let ((threads gnus-newsgroup-threads)
7983           sub)
7984       (while threads
7985         (setq sub (car threads))
7986         (if (stringp (car sub))
7987             ;; This is a gathered threads, so we look at the roots
7988             ;; below it to find whether this article in in this
7989             ;; gathered root.
7990             (progn
7991               (setq sub (cdr sub))
7992               (while sub
7993                 (when (member (caar sub) headers)
7994                   (setq thread (car threads)
7995                         threads nil
7996                         sub nil))
7997                 (setq sub (cdr sub))))
7998           ;; It's an ordinary thread, so we check it.
7999           (when (eq (car sub) (car headers))
8000             (setq thread sub
8001                   threads nil)))
8002         (setq threads (cdr threads)))
8003       ;; If this article is in no thread, then it's a root.
8004       (if thread
8005           (unless dont-remove
8006             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8007         (setq thread (gnus-gethash last-id dep)))
8008       (when thread
8009         (prog1
8010             thread ; We return this thread.
8011           (unless dont-remove
8012             (if (stringp (car thread))
8013                 (progn
8014                   ;; If we use dummy roots, then we have to remove the
8015                   ;; dummy root as well.
8016                   (when (eq gnus-summary-make-false-root 'dummy)
8017                     ;; Uhm.
8018                     )
8019                   (setq thread (cdr thread))
8020                   (while thread
8021                     (gnus-remove-thread-1 (car thread))
8022                     (setq thread (cdr thread))))
8023               (gnus-remove-thread-1 thread))))))))
8024
8025 (defun gnus-remove-thread-1 (thread)
8026   "Remove the thread THREAD recursively."
8027   (let ((number (mail-header-number (car thread)))
8028         pos)
8029     (when (setq pos (text-property-any
8030                      (point-min) (point-max) 'gnus-number number))
8031       (goto-char pos)
8032       (gnus-delete-line)
8033       (gnus-data-remove number))
8034     (setq thread (cdr thread))
8035     (while thread
8036       (gnus-remove-thread-1 (pop thread)))))
8037
8038 (defun gnus-sort-threads (threads)
8039   "Sort THREADS."
8040   (if (not gnus-thread-sort-functions)
8041       threads
8042     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8043                     (car gnus-thread-sort-functions)
8044                   `(lambda (t1 t2)
8045                      ,(gnus-make-sort-function 
8046                        (reverse gnus-thread-sort-functions))))))
8047       (gnus-message 7 "Sorting threads...")
8048       (prog1
8049           (sort threads func)
8050         (gnus-message 7 "Sorting threads...done")))))
8051
8052 (defun gnus-sort-articles (articles)
8053   "Sort ARTICLES."
8054   (when gnus-article-sort-functions
8055     (let ((func (if (= 1 (length gnus-article-sort-functions))
8056                     (car gnus-article-sort-functions)
8057                   `(lambda (t1 t2)
8058                      ,(gnus-make-sort-function 
8059                        (reverse gnus-article-sort-functions))))))
8060       (gnus-message 7 "Sorting articles...")
8061       (prog1
8062           (setq gnus-newsgroup-headers (sort articles func))
8063         (gnus-message 7 "Sorting articles...done")))))
8064
8065 (defun gnus-make-sort-function (funs)
8066   "Return a composite sort condition based on the functions in FUNC."
8067   (if (cdr funs)
8068       `(or (,(car funs) t1 t2)
8069            (and (not (,(car funs) t2 t1))
8070                 ,(gnus-make-sort-function (cdr funs))))
8071     `(,(car funs) t1 t2)))
8072                  
8073 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8074 (defmacro gnus-thread-header (thread)
8075   ;; Return header of first article in THREAD.
8076   ;; Note that THREAD must never, ever be anything else than a variable -
8077   ;; using some other form will lead to serious barfage.
8078   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8079   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8080   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8081         (vector thread) 2))
8082
8083 (defsubst gnus-article-sort-by-number (h1 h2)
8084   "Sort articles by article number."
8085   (< (mail-header-number h1)
8086      (mail-header-number h2)))
8087
8088 (defun gnus-thread-sort-by-number (h1 h2)
8089   "Sort threads by root article number."
8090   (gnus-article-sort-by-number
8091    (gnus-thread-header h1) (gnus-thread-header h2)))
8092
8093 (defsubst gnus-article-sort-by-author (h1 h2)
8094   "Sort articles by root author."
8095   (string-lessp
8096    (let ((extract (funcall
8097                    gnus-extract-address-components
8098                    (mail-header-from h1))))
8099      (or (car extract) (cdr extract)))
8100    (let ((extract (funcall
8101                    gnus-extract-address-components
8102                    (mail-header-from h2))))
8103      (or (car extract) (cdr extract)))))
8104
8105 (defun gnus-thread-sort-by-author (h1 h2)
8106   "Sort threads by root author."
8107   (gnus-article-sort-by-author
8108    (gnus-thread-header h1)  (gnus-thread-header h2)))
8109
8110 (defsubst gnus-article-sort-by-subject (h1 h2)
8111   "Sort articles by root subject."
8112   (string-lessp
8113    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8114    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8115
8116 (defun gnus-thread-sort-by-subject (h1 h2)
8117   "Sort threads by root subject."
8118   (gnus-article-sort-by-subject
8119    (gnus-thread-header h1) (gnus-thread-header h2)))
8120
8121 (defsubst gnus-article-sort-by-date (h1 h2)
8122   "Sort articles by root article date."
8123   (string-lessp
8124    (gnus-sortable-date (mail-header-date h1))
8125    (gnus-sortable-date (mail-header-date h2))))
8126
8127 (defun gnus-thread-sort-by-date (h1 h2)
8128   "Sort threads by root article date."
8129   (gnus-article-sort-by-date
8130    (gnus-thread-header h1) (gnus-thread-header h2)))
8131
8132 (defsubst gnus-article-sort-by-score (h1 h2)
8133   "Sort articles by root article score.
8134 Unscored articles will be counted as having a score of zero."
8135   (> (or (cdr (assq (mail-header-number h1)
8136                     gnus-newsgroup-scored))
8137          gnus-summary-default-score 0)
8138      (or (cdr (assq (mail-header-number h2)
8139                     gnus-newsgroup-scored))
8140          gnus-summary-default-score 0)))
8141
8142 (defun gnus-thread-sort-by-score (h1 h2)
8143   "Sort threads by root article score."
8144   (gnus-article-sort-by-score
8145    (gnus-thread-header h1) (gnus-thread-header h2)))
8146
8147 (defun gnus-thread-sort-by-total-score (h1 h2)
8148   "Sort threads by the sum of all scores in the thread.
8149 Unscored articles will be counted as having a score of zero."
8150   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8151
8152 (defun gnus-thread-total-score (thread)
8153   ;;  This function find the total score of THREAD.
8154   (if (consp thread)
8155       (if (stringp (car thread))
8156           (apply gnus-thread-score-function 0
8157                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8158         (gnus-thread-total-score-1 thread))
8159     (gnus-thread-total-score-1 (list thread))))
8160
8161 (defun gnus-thread-total-score-1 (root)
8162   ;; This function find the total score of the thread below ROOT.
8163   (setq root (car root))
8164   (apply gnus-thread-score-function
8165          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8166              gnus-summary-default-score 0)
8167          (mapcar 'gnus-thread-total-score
8168                  (cdr (gnus-gethash (mail-header-id root)
8169                                     gnus-newsgroup-dependencies)))))
8170
8171 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8172 (defvar gnus-tmp-prev-subject nil)
8173 (defvar gnus-tmp-false-parent nil)
8174 (defvar gnus-tmp-root-expunged nil)
8175 (defvar gnus-tmp-dummy-line nil)
8176
8177 (defun gnus-summary-prepare-threads (threads)
8178   "Prepare summary buffer from THREADS and indentation LEVEL.
8179 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8180 or a straight list of headers."
8181   (gnus-message 7 "Generating summary...")
8182
8183   (setq gnus-newsgroup-threads threads)
8184   (beginning-of-line)
8185
8186   (let ((gnus-tmp-level 0)
8187         (default-score (or gnus-summary-default-score 0))
8188         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8189         thread number subject stack state gnus-tmp-gathered beg-match
8190         new-roots gnus-tmp-new-adopts thread-end
8191         gnus-tmp-header gnus-tmp-unread
8192         gnus-tmp-replied gnus-tmp-subject-or-nil
8193         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8194         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8195         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8196
8197     (setq gnus-tmp-prev-subject nil)
8198
8199     (if (vectorp (car threads))
8200         ;; If this is a straight (sic) list of headers, then a
8201         ;; threaded summary display isn't required, so we just create
8202         ;; an unthreaded one.
8203         (gnus-summary-prepare-unthreaded threads)
8204
8205       ;; Do the threaded display.
8206
8207       (while (or threads stack gnus-tmp-new-adopts new-roots)
8208
8209         (if (and (= gnus-tmp-level 0)
8210                  (not (setq gnus-tmp-dummy-line nil))
8211                  (or (not stack)
8212                      (= (caar stack) 0))
8213                  (not gnus-tmp-false-parent)
8214                  (or gnus-tmp-new-adopts new-roots))
8215             (if gnus-tmp-new-adopts
8216                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8217                       thread (list (car gnus-tmp-new-adopts))
8218                       gnus-tmp-header (caar thread)
8219                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8220               (if new-roots
8221                   (setq thread (list (car new-roots))
8222                         gnus-tmp-header (caar thread)
8223                         new-roots (cdr new-roots))))
8224
8225           (if threads
8226               ;; If there are some threads, we do them before the
8227               ;; threads on the stack.
8228               (setq thread threads
8229                     gnus-tmp-header (caar thread))
8230             ;; There were no current threads, so we pop something off
8231             ;; the stack.
8232             (setq state (car stack)
8233                   gnus-tmp-level (car state)
8234                   thread (cdr state)
8235                   stack (cdr stack)
8236                   gnus-tmp-header (caar thread))))
8237
8238         (setq gnus-tmp-false-parent nil)
8239         (setq gnus-tmp-root-expunged nil)
8240         (setq thread-end nil)
8241
8242         (if (stringp gnus-tmp-header)
8243             ;; The header is a dummy root.
8244             (cond
8245              ((eq gnus-summary-make-false-root 'adopt)
8246               ;; We let the first article adopt the rest.
8247               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8248                                                (cddar thread)))
8249               (setq gnus-tmp-gathered
8250                     (nconc (mapcar
8251                             (lambda (h) (mail-header-number (car h)))
8252                             (cddar thread))
8253                            gnus-tmp-gathered))
8254               (setq thread (cons (list (caar thread)
8255                                        (cadar thread))
8256                                  (cdr thread)))
8257               (setq gnus-tmp-level -1
8258                     gnus-tmp-false-parent t))
8259              ((eq gnus-summary-make-false-root 'empty)
8260               ;; We print adopted articles with empty subject fields.
8261               (setq gnus-tmp-gathered
8262                     (nconc (mapcar
8263                             (lambda (h) (mail-header-number (car h)))
8264                             (cddar thread))
8265                            gnus-tmp-gathered))
8266               (setq gnus-tmp-level -1))
8267              ((eq gnus-summary-make-false-root 'dummy)
8268               ;; We remember that we probably want to output a dummy
8269               ;; root.
8270               (setq gnus-tmp-dummy-line gnus-tmp-header)
8271               (setq gnus-tmp-prev-subject gnus-tmp-header))
8272              (t
8273               ;; We do not make a root for the gathered
8274               ;; sub-threads at all.
8275               (setq gnus-tmp-level -1)))
8276
8277           (setq number (mail-header-number gnus-tmp-header)
8278                 subject (mail-header-subject gnus-tmp-header))
8279
8280           (cond
8281            ;; If the thread has changed subject, we might want to make
8282            ;; this subthread into a root.
8283            ((and (null gnus-thread-ignore-subject)
8284                  (not (zerop gnus-tmp-level))
8285                  gnus-tmp-prev-subject
8286                  (not (inline
8287                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8288             (setq new-roots (nconc new-roots (list (car thread)))
8289                   thread-end t
8290                   gnus-tmp-header nil))
8291            ;; If the article lies outside the current limit,
8292            ;; then we do not display it.
8293            ((and (not (memq number gnus-newsgroup-limit))
8294                  (not gnus-tmp-dummy-line))
8295             (setq gnus-tmp-gathered
8296                   (nconc (mapcar
8297                           (lambda (h) (mail-header-number (car h)))
8298                           (cdar thread))
8299                          gnus-tmp-gathered))
8300             (setq gnus-tmp-new-adopts (if (cdar thread)
8301                                           (append gnus-tmp-new-adopts
8302                                                   (cdar thread))
8303                                         gnus-tmp-new-adopts)
8304                   thread-end t
8305                   gnus-tmp-header nil)
8306             (when (zerop gnus-tmp-level)
8307               (setq gnus-tmp-root-expunged t)))
8308            ;; Perhaps this article is to be marked as read?
8309            ((and gnus-summary-mark-below
8310                  (< (or (cdr (assq number gnus-newsgroup-scored))
8311                         default-score)
8312                     gnus-summary-mark-below)
8313                  ;; Don't touch sparse articles.
8314                  (not (memq number gnus-newsgroup-sparse)))
8315             (setq gnus-newsgroup-unreads
8316                   (delq number gnus-newsgroup-unreads))
8317             (if gnus-newsgroup-auto-expire
8318                 (push number gnus-newsgroup-expirable)
8319               (push (cons number gnus-low-score-mark)
8320                     gnus-newsgroup-reads))))
8321
8322           (when gnus-tmp-header
8323             ;; We may have an old dummy line to output before this
8324             ;; article.
8325             (when gnus-tmp-dummy-line
8326               (gnus-summary-insert-dummy-line
8327                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8328               (setq gnus-tmp-dummy-line nil))
8329
8330             ;; Compute the mark.
8331             (setq
8332              gnus-tmp-unread
8333              (cond
8334               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8335               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8336               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8337               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8338               (t (or (cdr (assq number gnus-newsgroup-reads))
8339                      gnus-ancient-mark))))
8340
8341             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8342                                   gnus-tmp-header gnus-tmp-level)
8343                   gnus-newsgroup-data)
8344
8345             ;; Actually insert the line.
8346             (setq
8347              gnus-tmp-subject-or-nil
8348              (cond
8349               ((and gnus-thread-ignore-subject
8350                     gnus-tmp-prev-subject
8351                     (not (inline (gnus-subject-equal
8352                                   gnus-tmp-prev-subject subject))))
8353                subject)
8354               ((zerop gnus-tmp-level)
8355                (if (and (eq gnus-summary-make-false-root 'empty)
8356                         (memq number gnus-tmp-gathered)
8357                         gnus-tmp-prev-subject
8358                         (inline (gnus-subject-equal
8359                                  gnus-tmp-prev-subject subject)))
8360                    gnus-summary-same-subject
8361                  subject))
8362               (t gnus-summary-same-subject)))
8363             (if (and (eq gnus-summary-make-false-root 'adopt)
8364                      (= gnus-tmp-level 1)
8365                      (memq number gnus-tmp-gathered))
8366                 (setq gnus-tmp-opening-bracket ?\<
8367                       gnus-tmp-closing-bracket ?\>)
8368               (setq gnus-tmp-opening-bracket ?\[
8369                     gnus-tmp-closing-bracket ?\]))
8370             (setq
8371              gnus-tmp-indentation
8372              (aref gnus-thread-indent-array gnus-tmp-level)
8373              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8374              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8375                                 gnus-summary-default-score 0)
8376              gnus-tmp-score-char
8377              (if (or (null gnus-summary-default-score)
8378                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8379                          gnus-summary-zcore-fuzz)) ? 
8380                (if (< gnus-tmp-score gnus-summary-default-score)
8381                    gnus-score-below-mark gnus-score-over-mark))
8382              gnus-tmp-replied
8383              (cond ((memq number gnus-newsgroup-processable)
8384                     gnus-process-mark)
8385                    ((memq number gnus-newsgroup-cached)
8386                     gnus-cached-mark)
8387                    ((memq number gnus-newsgroup-replied)
8388                     gnus-replied-mark)
8389                    (t gnus-unread-mark))
8390              gnus-tmp-from (mail-header-from gnus-tmp-header)
8391              gnus-tmp-name
8392              (cond
8393               ((string-match "(.+)" gnus-tmp-from)
8394                (substring gnus-tmp-from
8395                           (1+ (match-beginning 0)) (1- (match-end 0))))
8396               ((string-match "<[^>]+> *$" gnus-tmp-from)
8397                (setq beg-match (match-beginning 0))
8398                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8399                         (substring gnus-tmp-from (1+ (match-beginning 0))
8400                                    (1- (match-end 0))))
8401                    (substring gnus-tmp-from 0 beg-match)))
8402               (t gnus-tmp-from)))
8403             (when (string= gnus-tmp-name "")
8404               (setq gnus-tmp-name gnus-tmp-from))
8405             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8406             (put-text-property
8407              (point)
8408              (progn (eval gnus-summary-line-format-spec) (point))
8409              'gnus-number number)
8410             (when gnus-visual-p
8411               (forward-line -1)
8412               (run-hooks 'gnus-summary-update-hook)
8413               (forward-line 1))
8414
8415             (setq gnus-tmp-prev-subject subject)))
8416
8417         (when (nth 1 thread)
8418           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8419         (incf gnus-tmp-level)
8420         (setq threads (if thread-end nil (cdar thread)))
8421         (unless threads
8422           (setq gnus-tmp-level 0)))))
8423   (gnus-message 7 "Generating summary...done"))
8424
8425 (defun gnus-summary-prepare-unthreaded (headers)
8426   "Generate an unthreaded summary buffer based on HEADERS."
8427   (let (header number mark)
8428
8429     (while headers
8430       (setq header (car headers)
8431             headers (cdr headers)
8432             number (mail-header-number header))
8433
8434       ;; We may have to root out some bad articles...
8435       (when (memq number gnus-newsgroup-limit)
8436         (when (and gnus-summary-mark-below
8437                    (< (or (cdr (assq number gnus-newsgroup-scored))
8438                           gnus-summary-default-score 0)
8439                       gnus-summary-mark-below))
8440           (setq gnus-newsgroup-unreads
8441                 (delq number gnus-newsgroup-unreads))
8442           (if gnus-newsgroup-auto-expire
8443               (push number gnus-newsgroup-expirable)
8444             (push (cons number gnus-low-score-mark)
8445                   gnus-newsgroup-reads)))
8446
8447         (setq mark
8448               (cond
8449                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8450                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8451                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8452                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8453                (t (or (cdr (assq number gnus-newsgroup-reads))
8454                       gnus-ancient-mark))))
8455         (setq gnus-newsgroup-data
8456               (cons (gnus-data-make number mark (1+ (point)) header 0)
8457                     gnus-newsgroup-data))
8458         (gnus-summary-insert-line
8459          header 0 nil mark (memq number gnus-newsgroup-replied)
8460          (memq number gnus-newsgroup-expirable)
8461          (mail-header-subject header) nil
8462          (cdr (assq number gnus-newsgroup-scored))
8463          (memq number gnus-newsgroup-processable))))))
8464
8465 (defun gnus-select-newsgroup (group &optional read-all)
8466   "Select newsgroup GROUP.
8467 If READ-ALL is non-nil, all articles in the group are selected."
8468   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8469          (info (nth 2 entry))
8470          articles fetched-articles cached)
8471
8472     (or (gnus-check-server
8473          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8474         (error "Couldn't open server"))
8475
8476     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8477         (gnus-activate-group group) ; Or we can activate it...
8478         (progn ; Or we bug out.
8479           (when (equal major-mode 'gnus-summary-mode)
8480             (kill-buffer (current-buffer)))
8481           (error "Couldn't request group %s: %s"
8482                  group (gnus-status-message group))))
8483
8484     (setq gnus-newsgroup-name group)
8485     (setq gnus-newsgroup-unselected nil)
8486     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8487
8488     (and gnus-asynchronous
8489          (gnus-check-backend-function
8490           'request-asynchronous gnus-newsgroup-name)
8491          (setq gnus-newsgroup-async
8492                (gnus-request-asynchronous gnus-newsgroup-name)))
8493
8494     ;; Adjust and set lists of article marks.
8495     (when info
8496       (gnus-adjust-marked-articles info))
8497
8498     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8499     (when (gnus-virtual-group-p group)
8500       (setq cached gnus-newsgroup-cached))
8501
8502     (setq gnus-newsgroup-unreads
8503           (gnus-set-difference
8504            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8505            gnus-newsgroup-dormant))
8506
8507     (setq gnus-newsgroup-processable nil)
8508
8509     (setq articles (gnus-articles-to-read group read-all))
8510
8511     (cond
8512      ((null articles)
8513       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8514       'quit)
8515      ((eq articles 0) nil)
8516      (t
8517       ;; Init the dependencies hash table.
8518       (setq gnus-newsgroup-dependencies
8519             (gnus-make-hashtable (length articles)))
8520       ;; Retrieve the headers and read them in.
8521       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8522       (setq gnus-newsgroup-headers
8523             (if (eq 'nov
8524                     (setq gnus-headers-retrieved-by
8525                           (gnus-retrieve-headers
8526                            articles gnus-newsgroup-name
8527                            ;; We might want to fetch old headers, but
8528                            ;; not if there is only 1 article.
8529                            (and gnus-fetch-old-headers
8530                                 (or (and
8531                                      (not (eq gnus-fetch-old-headers 'some))
8532                                      (not (numberp gnus-fetch-old-headers)))
8533                                     (> (length articles) 1))))))
8534                 (gnus-get-newsgroup-headers-xover articles)
8535               (gnus-get-newsgroup-headers)))
8536       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8537
8538       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8539       (when cached
8540         (setq gnus-newsgroup-cached cached))
8541
8542       ;; Set the initial limit.
8543       (setq gnus-newsgroup-limit (copy-sequence articles))
8544       ;; Remove canceled articles from the list of unread articles.
8545       (setq gnus-newsgroup-unreads
8546             (gnus-set-sorted-intersection
8547              gnus-newsgroup-unreads
8548              (setq fetched-articles
8549                    (mapcar (lambda (headers) (mail-header-number headers))
8550                            gnus-newsgroup-headers))))
8551       ;; Removed marked articles that do not exist.
8552       (gnus-update-missing-marks
8553        (gnus-sorted-complement fetched-articles articles))
8554       ;; We might want to build some more threads first.
8555       (and gnus-fetch-old-headers
8556            (eq gnus-headers-retrieved-by 'nov)
8557            (gnus-build-old-threads))
8558       ;; Check whether auto-expire is to be done in this group.
8559       (setq gnus-newsgroup-auto-expire
8560             (gnus-group-auto-expirable-p group))
8561       ;; Set up the article buffer now, if necessary.
8562       (unless gnus-single-article-buffer
8563         (gnus-article-setup-buffer))
8564       ;; First and last article in this newsgroup.
8565       (and gnus-newsgroup-headers
8566            (setq gnus-newsgroup-begin
8567                  (mail-header-number (car gnus-newsgroup-headers)))
8568            (setq gnus-newsgroup-end
8569                  (mail-header-number
8570                   (gnus-last-element gnus-newsgroup-headers))))
8571       (setq gnus-reffed-article-number -1)
8572       ;; GROUP is successfully selected.
8573       (or gnus-newsgroup-headers t)))))
8574
8575 (defun gnus-articles-to-read (group read-all)
8576   ;; Find out what articles the user wants to read.
8577   (let* ((articles
8578           ;; Select all articles if `read-all' is non-nil, or if there
8579           ;; are no unread articles.
8580           (if (or read-all
8581                   (and (zerop (length gnus-newsgroup-marked))
8582                        (zerop (length gnus-newsgroup-unreads))))
8583               (gnus-uncompress-range (gnus-active group))
8584             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8585                           (copy-sequence gnus-newsgroup-unreads))
8586                   '<)))
8587          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8588          (scored (length scored-list))
8589          (number (length articles))
8590          (marked (+ (length gnus-newsgroup-marked)
8591                     (length gnus-newsgroup-dormant)))
8592          (select
8593           (cond
8594            ((numberp read-all)
8595             read-all)
8596            (t
8597             (condition-case ()
8598                 (cond
8599                  ((and (or (<= scored marked) (= scored number))
8600                        (numberp gnus-large-newsgroup)
8601                        (> number gnus-large-newsgroup))
8602                   (let ((input
8603                          (read-string
8604                           (format
8605                            "How many articles from %s (default %d): "
8606                            gnus-newsgroup-name number))))
8607                     (if (string-match "^[ \t]*$" input) number input)))
8608                  ((and (> scored marked) (< scored number))
8609                   (let ((input
8610                          (read-string
8611                           (format "%s %s (%d scored, %d total): "
8612                                   "How many articles from"
8613                                   group scored number))))
8614                     (if (string-match "^[ \t]*$" input)
8615                         number input)))
8616                  (t number))
8617               (quit nil))))))
8618     (setq select (if (stringp select) (string-to-number select) select))
8619     (if (or (null select) (zerop select))
8620         select
8621       (if (and (not (zerop scored)) (<= (abs select) scored))
8622           (progn
8623             (setq articles (sort scored-list '<))
8624             (setq number (length articles)))
8625         (setq articles (copy-sequence articles)))
8626
8627       (if (< (abs select) number)
8628           (if (< select 0)
8629               ;; Select the N oldest articles.
8630               (setcdr (nthcdr (1- (abs select)) articles) nil)
8631             ;; Select the N most recent articles.
8632             (setq articles (nthcdr (- number select) articles))))
8633       (setq gnus-newsgroup-unselected
8634             (gnus-sorted-intersection
8635              gnus-newsgroup-unreads
8636              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8637       articles)))
8638
8639 (defun gnus-killed-articles (killed articles)
8640   (let (out)
8641     (while articles
8642       (if (inline (gnus-member-of-range (car articles) killed))
8643           (setq out (cons (car articles) out)))
8644       (setq articles (cdr articles)))
8645     out))
8646
8647 (defun gnus-uncompress-marks (marks)
8648   "Uncompress the mark ranges in MARKS."
8649   (let ((uncompressed '(score bookmark))
8650         out)
8651     (while marks
8652       (if (memq (caar marks) uncompressed)
8653           (push (car marks) out)
8654         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8655       (setq marks (cdr marks)))
8656     out))
8657
8658 (defun gnus-adjust-marked-articles (info)
8659   "Set all article lists and remove all marks that are no longer legal."
8660   (let* ((marked-lists (gnus-info-marks info))
8661          (active (gnus-active (gnus-info-group info)))
8662          (min (car active))
8663          (max (cdr active))
8664          (types gnus-article-mark-lists)
8665          (uncompressed '(score bookmark))
8666          marks var articles article mark)
8667
8668     (while marked-lists
8669       (setq marks (pop marked-lists))
8670       (set (setq var (intern (format "gnus-newsgroup-%s"
8671                                      (car (rassq (setq mark (car marks))
8672                                                  types)))))
8673            (if (memq (car marks) uncompressed) (cdr marks)
8674              (gnus-uncompress-range (cdr marks))))
8675
8676       (setq articles (symbol-value var))
8677
8678       ;; All articles have to be subsets of the active articles.
8679       (cond
8680        ;; Adjust "simple" lists.
8681        ((memq mark '(tick dormant expirable reply killed save))
8682         (while articles
8683           (when (or (< (setq article (pop articles)) min) (> article max))
8684             (set var (delq article (symbol-value var))))))
8685        ;; Adjust assocs.
8686        ((memq mark '(score bookmark))
8687         (while articles
8688           (when (or (< (car (setq article (pop articles))) min)
8689                     (> (car article) max))
8690             (set var (delq article (symbol-value var))))))))))
8691
8692 (defun gnus-update-missing-marks (missing)
8693   "Go through the list of MISSING articles and remove them mark lists."
8694   (when missing
8695     (let ((types gnus-article-mark-lists)
8696           var m)
8697       ;; Go through all types.
8698       (while types
8699         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8700         (when (symbol-value var)
8701           ;; This list has articles.  So we delete all missing articles
8702           ;; from it.
8703           (setq m missing)
8704           (while m
8705             (set var (delq (pop m) (symbol-value var)))))))))
8706
8707 (defun gnus-update-marks ()
8708   "Enter the various lists of marked articles into the newsgroup info list."
8709   (let ((types gnus-article-mark-lists)
8710         (info (gnus-get-info gnus-newsgroup-name))
8711         (uncompressed '(score bookmark killed))
8712         type list newmarked symbol)
8713     (when info
8714       ;; Add all marks lists that are non-nil to the list of marks lists.
8715       (while types
8716         (setq type (pop types))
8717         (when (setq list (symbol-value
8718                           (setq symbol
8719                                 (intern (format "gnus-newsgroup-%s"
8720                                                 (car type))))))
8721           (push (cons (cdr type)
8722                       (if (memq (cdr type) uncompressed) list
8723                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8724                 newmarked)))
8725
8726       ;; Enter these new marks into the info of the group.
8727       (if (nthcdr 3 info)
8728           (setcar (nthcdr 3 info) newmarked)
8729         ;; Add the marks lists to the end of the info.
8730         (when newmarked
8731           (setcdr (nthcdr 2 info) (list newmarked))))
8732
8733       ;; Cut off the end of the info if there's nothing else there.
8734       (let ((i 5))
8735         (while (and (> i 2)
8736                     (not (nth i info)))
8737           (when (nthcdr (decf i) info)
8738             (setcdr (nthcdr i info) nil)))))))
8739
8740 (defun gnus-add-marked-articles (group type articles &optional info force)
8741   ;; Add ARTICLES of TYPE to the info of GROUP.
8742   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8743   ;; add, but replace marked articles of TYPE with ARTICLES.
8744   (let ((info (or info (gnus-get-info group)))
8745         (uncompressed '(score bookmark killed))
8746         marked m)
8747     (or (not info)
8748         (and (not (setq marked (nthcdr 3 info)))
8749              (or (null articles)
8750                  (setcdr (nthcdr 2 info)
8751                          (list (list (cons type (gnus-compress-sequence
8752                                                  articles t)))))))
8753         (and (not (setq m (assq type (car marked))))
8754              (or (null articles)
8755                  (setcar marked
8756                          (cons (cons type (gnus-compress-sequence articles t) )
8757                                (car marked)))))
8758         (if force
8759             (if (null articles)
8760                 (setcar (nthcdr 3 info)
8761                         (delq (assq type (car marked)) (car marked)))
8762               (setcdr m (gnus-compress-sequence articles t)))
8763           (setcdr m (gnus-compress-sequence
8764                      (sort (nconc (gnus-uncompress-range (cdr m))
8765                                   (copy-sequence articles)) '<) t))))))
8766
8767 (defun gnus-set-mode-line (where)
8768   "This function sets the mode line of the article or summary buffers.
8769 If WHERE is `summary', the summary mode line format will be used."
8770   ;; Is this mode line one we keep updated?
8771   (when (memq where gnus-updated-mode-lines)
8772     (let (mode-string)
8773       (save-excursion
8774         ;; We evaluate this in the summary buffer since these
8775         ;; variables are buffer-local to that buffer.
8776         (set-buffer gnus-summary-buffer)
8777         ;; We bind all these variables that are used in the `eval' form
8778         ;; below.
8779         (let* ((mformat (symbol-value
8780                          (intern
8781                           (format "gnus-%s-mode-line-format-spec" where))))
8782                (gnus-tmp-group-name gnus-newsgroup-name)
8783                (gnus-tmp-article-number (or gnus-current-article 0))
8784                (gnus-tmp-unread gnus-newsgroup-unreads)
8785                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8786                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8787                (gnus-tmp-unread-and-unselected
8788                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8789                             (zerop gnus-tmp-unselected)) "")
8790                       ((zerop gnus-tmp-unselected)
8791                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8792                       (t (format "{%d(+%d) more}"
8793                                  gnus-tmp-unread-and-unticked
8794                                  gnus-tmp-unselected))))
8795                (gnus-tmp-subject
8796                 (if (and gnus-current-headers
8797                          (vectorp gnus-current-headers))
8798                     (mail-header-subject gnus-current-headers) ""))
8799                max-len
8800                gnus-tmp-header);; passed as argument to any user-format-funcs
8801           (setq mode-string (eval mformat))
8802           (setq max-len (max 4 (if gnus-mode-non-string-length
8803                                    (- (frame-width)
8804                                       gnus-mode-non-string-length)
8805                                  (length mode-string))))
8806           ;; We might have to chop a bit of the string off...
8807           (when (> (length mode-string) max-len)
8808             (setq mode-string
8809                   (concat (gnus-truncate-string mode-string (- max-len 3))
8810                           "...")))
8811           ;; Pad the mode string a bit.
8812           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8813       ;; Update the mode line.
8814       (setq mode-line-buffer-identification (list mode-string))
8815       (set-buffer-modified-p t))))
8816
8817 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8818   "Go through the HEADERS list and add all Xrefs to a hash table.
8819 The resulting hash table is returned, or nil if no Xrefs were found."
8820   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8821          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8822          (xref-hashtb (make-vector 63 0))
8823          start group entry number xrefs header)
8824     (while headers
8825       (setq header (pop headers))
8826       (when (and (setq xrefs (mail-header-xref header))
8827                  (not (memq (setq number (mail-header-number header))
8828                             unreads)))
8829         (setq start 0)
8830         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8831           (setq start (match-end 0))
8832           (setq group (if prefix
8833                           (concat prefix (substring xrefs (match-beginning 1)
8834                                                     (match-end 1)))
8835                         (substring xrefs (match-beginning 1) (match-end 1))))
8836           (setq number
8837                 (string-to-int (substring xrefs (match-beginning 2)
8838                                           (match-end 2))))
8839           (if (setq entry (gnus-gethash group xref-hashtb))
8840               (setcdr entry (cons number (cdr entry)))
8841             (gnus-sethash group (cons number nil) xref-hashtb)))))
8842     (and start xref-hashtb)))
8843
8844 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8845   "Look through all the headers and mark the Xrefs as read."
8846   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8847         name entry info xref-hashtb idlist method nth4)
8848     (save-excursion
8849       (set-buffer gnus-group-buffer)
8850       (when (setq xref-hashtb
8851                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8852         (mapatoms
8853          (lambda (group)
8854            (unless (string= from-newsgroup (setq name (symbol-name group)))
8855              (setq idlist (symbol-value group))
8856              ;; Dead groups are not updated.
8857              (and (prog1
8858                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8859                             info (nth 2 entry))
8860                     (if (stringp (setq nth4 (gnus-info-method info)))
8861                         (setq nth4 (gnus-server-to-method nth4))))
8862                   ;; Only do the xrefs if the group has the same
8863                   ;; select method as the group we have just read.
8864                   (or (gnus-methods-equal-p
8865                        nth4 (gnus-find-method-for-group from-newsgroup))
8866                       virtual
8867                       (equal nth4 (setq method (gnus-find-method-for-group
8868                                                 from-newsgroup)))
8869                       (and (equal (car nth4) (car method))
8870                            (equal (nth 1 nth4) (nth 1 method))))
8871                   gnus-use-cross-reference
8872                   (or (not (eq gnus-use-cross-reference t))
8873                       virtual
8874                       ;; Only do cross-references on subscribed
8875                       ;; groups, if that is what is wanted.
8876                       (<= (gnus-info-level info) gnus-level-subscribed))
8877                   (gnus-group-make-articles-read name idlist))))
8878          xref-hashtb)))))
8879
8880 (defun gnus-group-make-articles-read (group articles)
8881   (let* ((num 0)
8882          (entry (gnus-gethash group gnus-newsrc-hashtb))
8883          (info (nth 2 entry))
8884          (active (gnus-active group))
8885          range)
8886     ;; First peel off all illegal article numbers.
8887     (if active
8888         (let ((ids articles)
8889               id first)
8890           (while ids
8891             (setq id (car ids))
8892             (if (and first (> id (cdr active)))
8893                 (progn
8894                   ;; We'll end up in this situation in one particular
8895                   ;; obscure situation.  If you re-scan a group and get
8896                   ;; a new article that is cross-posted to a different
8897                   ;; group that has not been re-scanned, you might get
8898                   ;; crossposted article that has a higher number than
8899                   ;; Gnus believes possible.  So we re-activate this
8900                   ;; group as well.  This might mean doing the
8901                   ;; crossposting thingy will *increase* the number
8902                   ;; of articles in some groups.  Tsk, tsk.
8903                   (setq active (or (gnus-activate-group group) active))))
8904             (if (or (> id (cdr active))
8905                     (< id (car active)))
8906                 (setq articles (delq id articles)))
8907             (setq ids (cdr ids)))))
8908     ;; If the read list is nil, we init it.
8909     (and active
8910          (null (gnus-info-read info))
8911          (> (car active) 1)
8912          (gnus-info-set-read info (cons 1 (1- (car active)))))
8913     ;; Then we add the read articles to the range.
8914     (gnus-info-set-read
8915      info
8916      (setq range
8917            (gnus-add-to-range
8918             (gnus-info-read info) (setq articles (sort articles '<)))))
8919     ;; Then we have to re-compute how many unread
8920     ;; articles there are in this group.
8921     (if active
8922         (progn
8923           (cond
8924            ((not range)
8925             (setq num (- (1+ (cdr active)) (car active))))
8926            ((not (listp (cdr range)))
8927             (setq num (- (cdr active) (- (1+ (cdr range))
8928                                          (car range)))))
8929            (t
8930             (while range
8931               (if (numberp (car range))
8932                   (setq num (1+ num))
8933                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8934               (setq range (cdr range)))
8935             (setq num (- (cdr active) num))))
8936           ;; Update the number of unread articles.
8937           (setcar entry num)
8938           ;; Update the group buffer.
8939           (gnus-group-update-group group t)))))
8940
8941 (defun gnus-methods-equal-p (m1 m2)
8942   (let ((m1 (or m1 gnus-select-method))
8943         (m2 (or m2 gnus-select-method)))
8944     (or (equal m1 m2)
8945         (and (eq (car m1) (car m2))
8946              (or (not (memq 'address (assoc (symbol-name (car m1))
8947                                             gnus-valid-select-methods)))
8948                  (equal (nth 1 m1) (nth 1 m2)))))))
8949
8950 (defsubst gnus-header-value ()
8951   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8952
8953 (defvar gnus-newsgroup-none-id 0)
8954
8955 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8956   (let ((cur nntp-server-buffer)
8957         (dependencies
8958          (or dependencies
8959              (save-excursion (set-buffer gnus-summary-buffer)
8960                              gnus-newsgroup-dependencies)))
8961         headers id id-dep ref-dep end ref)
8962     (save-excursion
8963       (set-buffer nntp-server-buffer)
8964       (let ((case-fold-search t)
8965             in-reply-to header p lines)
8966         (goto-char (point-min))
8967         ;; Search to the beginning of the next header.  Error messages
8968         ;; do not begin with 2 or 3.
8969         (while (re-search-forward "^[23][0-9]+ " nil t)
8970           (setq id nil
8971                 ref nil)
8972           ;; This implementation of this function, with nine
8973           ;; search-forwards instead of the one re-search-forward and
8974           ;; a case (which basically was the old function) is actually
8975           ;; about twice as fast, even though it looks messier.  You
8976           ;; can't have everything, I guess.  Speed and elegance
8977           ;; doesn't always go hand in hand.
8978           (setq
8979            header
8980            (vector
8981             ;; Number.
8982             (prog1
8983                 (read cur)
8984               (end-of-line)
8985               (setq p (point))
8986               (narrow-to-region (point)
8987                                 (or (and (search-forward "\n.\n" nil t)
8988                                          (- (point) 2))
8989                                     (point))))
8990             ;; Subject.
8991             (progn
8992               (goto-char p)
8993               (if (search-forward "\nsubject: " nil t)
8994                   (gnus-header-value) "(none)"))
8995             ;; From.
8996             (progn
8997               (goto-char p)
8998               (if (search-forward "\nfrom: " nil t)
8999                   (gnus-header-value) "(nobody)"))
9000             ;; Date.
9001             (progn
9002               (goto-char p)
9003               (if (search-forward "\ndate: " nil t)
9004                   (gnus-header-value) ""))
9005             ;; Message-ID.
9006             (progn
9007               (goto-char p)
9008               (if (search-forward "\nmessage-id: " nil t)
9009                   (setq id (gnus-header-value))
9010                 ;; If there was no message-id, we just fake one to make
9011                 ;; subsequent routines simpler.
9012                 (setq id (concat "none+"
9013                                  (int-to-string
9014                                   (setq gnus-newsgroup-none-id
9015                                         (1+ gnus-newsgroup-none-id)))))))
9016             ;; References.
9017             (progn
9018               (goto-char p)
9019               (if (search-forward "\nreferences: " nil t)
9020                   (prog1
9021                       (gnus-header-value)
9022                     (setq end (match-end 0))
9023                     (save-excursion
9024                       (setq ref
9025                             (buffer-substring
9026                              (progn
9027                                (end-of-line)
9028                                (search-backward ">" end t)
9029                                (1+ (point)))
9030                              (progn
9031                                (search-backward "<" end t)
9032                                (point))))))
9033                 ;; Get the references from the in-reply-to header if there
9034                 ;; were no references and the in-reply-to header looks
9035                 ;; promising.
9036                 (if (and (search-forward "\nin-reply-to: " nil t)
9037                          (setq in-reply-to (gnus-header-value))
9038                          (string-match "<[^>]+>" in-reply-to))
9039                     (setq ref (substring in-reply-to (match-beginning 0)
9040                                          (match-end 0)))
9041                   (setq ref ""))))
9042             ;; Chars.
9043             0
9044             ;; Lines.
9045             (progn
9046               (goto-char p)
9047               (if (search-forward "\nlines: " nil t)
9048                   (if (numberp (setq lines (read cur)))
9049                       lines 0)
9050                 0))
9051             ;; Xref.
9052             (progn
9053               (goto-char p)
9054               (and (search-forward "\nxref: " nil t)
9055                    (gnus-header-value)))))
9056           ;; We do the threading while we read the headers.  The
9057           ;; message-id and the last reference are both entered into
9058           ;; the same hash table.  Some tippy-toeing around has to be
9059           ;; done in case an article has arrived before the article
9060           ;; which it refers to.
9061           (if (boundp (setq id-dep (intern id dependencies)))
9062               (if (and (car (symbol-value id-dep))
9063                        (not force-new))
9064                   ;; An article with this Message-ID has already
9065                   ;; been seen, so we ignore this one, except we add
9066                   ;; any additional Xrefs (in case the two articles
9067                   ;; came from different servers).
9068                   (progn
9069                     (mail-header-set-xref
9070                      (car (symbol-value id-dep))
9071                      (concat (or (mail-header-xref
9072                                   (car (symbol-value id-dep))) "")
9073                              (or (mail-header-xref header) "")))
9074                     (setq header nil))
9075                 (setcar (symbol-value id-dep) header))
9076             (set id-dep (list header)))
9077           (when header
9078             (if (boundp (setq ref-dep (intern ref dependencies)))
9079                 (setcdr (symbol-value ref-dep)
9080                         (nconc (cdr (symbol-value ref-dep))
9081                                (list (symbol-value id-dep))))
9082               (set ref-dep (list nil (symbol-value id-dep))))
9083             (setq headers (cons header headers)))
9084           (goto-char (point-max))
9085           (widen))
9086         (nreverse headers)))))
9087
9088 ;; The following macros and functions were written by Felix Lee
9089 ;; <flee@cse.psu.edu>.
9090
9091 (defmacro gnus-nov-read-integer ()
9092   '(prog1
9093        (if (= (following-char) ?\t)
9094            0
9095          (let ((num (condition-case nil (read buffer) (error nil))))
9096            (if (numberp num) num 0)))
9097      (or (eobp) (forward-char 1))))
9098
9099 (defmacro gnus-nov-skip-field ()
9100   '(search-forward "\t" eol 'move))
9101
9102 (defmacro gnus-nov-field ()
9103   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9104
9105 ;; Goes through the xover lines and returns a list of vectors
9106 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9107   "Parse the news overview data in the server buffer, and return a
9108 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9109   ;; Get the Xref when the users reads the articles since most/some
9110   ;; NNTP servers do not include Xrefs when using XOVER.
9111   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9112   (let ((cur nntp-server-buffer)
9113         (dependencies gnus-newsgroup-dependencies)
9114         number headers header)
9115     (save-excursion
9116       (set-buffer nntp-server-buffer)
9117       ;; Allow the user to mangle the headers before parsing them.
9118       (run-hooks 'gnus-parse-headers-hook)
9119       ;; Allow the user to mangle the headers before parsing them.
9120       (run-hooks 'gnus-parse-headers-hook)
9121       (goto-char (point-min))
9122       (while (and sequence (not (eobp)))
9123         (setq number (read cur))
9124         (while (and sequence (< (car sequence) number))
9125           (setq sequence (cdr sequence)))
9126         (and sequence
9127              (eq number (car sequence))
9128              (progn
9129                (setq sequence (cdr sequence))
9130                (if (setq header
9131                          (inline (gnus-nov-parse-line
9132                                   number dependencies force-new)))
9133                    (setq headers (cons header headers)))))
9134         (forward-line 1))
9135       (setq headers (nreverse headers)))
9136     headers))
9137
9138 ;; This function has to be called with point after the article number
9139 ;; on the beginning of the line.
9140 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9141   (let ((none 0)
9142         (eol (gnus-point-at-eol))
9143         (buffer (current-buffer))
9144         header ref id id-dep ref-dep)
9145
9146     ;; overview: [num subject from date id refs chars lines misc]
9147     (narrow-to-region (point) eol)
9148     (or (eobp) (forward-char))
9149
9150     (condition-case nil
9151         (setq header
9152               (vector
9153                number                   ; number
9154                (gnus-nov-field)         ; subject
9155                (gnus-nov-field)         ; from
9156                (gnus-nov-field)         ; date
9157                (setq id (or (gnus-nov-field)
9158                             (concat "none+"
9159                                     (int-to-string
9160                                      (setq none (1+ none)))))) ; id
9161                (progn
9162                  (save-excursion
9163                    (let ((beg (point)))
9164                      (search-forward "\t" eol)
9165                      (if (search-backward ">" beg t)
9166                          (setq ref
9167                                (buffer-substring
9168                                 (1+ (point))
9169                                 (search-backward "<" beg t)))
9170                        (setq ref nil))))
9171                  (gnus-nov-field))      ; refs
9172                (gnus-nov-read-integer)  ; chars
9173                (gnus-nov-read-integer)  ; lines
9174                (if (= (following-char) ?\n)
9175                    nil
9176                  (gnus-nov-field))      ; misc
9177                ))
9178       (error (progn
9179                (ding)
9180                (gnus-message 4 "Strange nov line")
9181                (setq header nil)
9182                (goto-char eol))))
9183
9184     (widen)
9185
9186     ;; We build the thread tree.
9187     (when header
9188       (if (boundp (setq id-dep (intern id dependencies)))
9189           (if (and (car (symbol-value id-dep))
9190                    (not force-new))
9191               ;; An article with this Message-ID has already been seen,
9192               ;; so we ignore this one, except we add any additional
9193               ;; Xrefs (in case the two articles came from different
9194               ;; servers.
9195               (progn
9196                 (mail-header-set-xref
9197                  (car (symbol-value id-dep))
9198                  (concat (or (mail-header-xref
9199                               (car (symbol-value id-dep))) "")
9200                          (or (mail-header-xref header) "")))
9201                 (setq header nil))
9202             (setcar (symbol-value id-dep) header))
9203         (set id-dep (list header))))
9204     (if header
9205         (progn
9206           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9207               (setcdr (symbol-value ref-dep)
9208                       (nconc (cdr (symbol-value ref-dep))
9209                              (list (symbol-value id-dep))))
9210             (set ref-dep (list nil (symbol-value id-dep))))))
9211     header))
9212
9213 (defun gnus-article-get-xrefs ()
9214   "Fill in the Xref value in `gnus-current-headers', if necessary.
9215 This is meant to be called in `gnus-article-internal-prepare-hook'."
9216   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9217                                  gnus-current-headers)))
9218     (or (not gnus-use-cross-reference)
9219         (not headers)
9220         (and (mail-header-xref headers)
9221              (not (string= (mail-header-xref headers) "")))
9222         (let ((case-fold-search t)
9223               xref)
9224           (save-restriction
9225             (nnheader-narrow-to-headers)
9226             (goto-char (point-min))
9227             (if (or (and (eq (downcase (following-char)) ?x)
9228                          (looking-at "Xref:"))
9229                     (search-forward "\nXref:" nil t))
9230                 (progn
9231                   (goto-char (1+ (match-end 0)))
9232                   (setq xref (buffer-substring (point)
9233                                                (progn (end-of-line) (point))))
9234                   (mail-header-set-xref headers xref))))))))
9235
9236 (defun gnus-summary-insert-subject (id &optional old-header)
9237   "Find article ID and insert the summary line for that article."
9238   (let ((header (gnus-read-header id))
9239         (number (and (numberp id) id))
9240         pos)
9241     (when header
9242       ;; Rebuild the thread that this article is part of and go to the
9243       ;; article we have fetched.
9244       (when old-header
9245         (when (setq pos (text-property-any
9246                          (point-min) (point-max) 'gnus-number 
9247                          (mail-header-number old-header)))
9248           (goto-char pos)
9249           (gnus-delete-line)
9250           (gnus-data-remove (mail-header-number old-header))))
9251       (gnus-rebuild-thread (mail-header-id header))
9252       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9253     (when (and (numberp number)
9254                (> number 0))
9255       ;; We have to update the boundaries even if we can't fetch the
9256       ;; article if ID is a number -- so that the next `P' or `N'
9257       ;; command will fetch the previous (or next) article even
9258       ;; if the one we tried to fetch this time has been canceled.
9259       (and (> number gnus-newsgroup-end)
9260            (setq gnus-newsgroup-end number))
9261       (and (< number gnus-newsgroup-begin)
9262            (setq gnus-newsgroup-begin number))
9263       (setq gnus-newsgroup-unselected
9264             (delq number gnus-newsgroup-unselected)))
9265     ;; Report back a success?
9266     (and header (mail-header-number header))))
9267
9268 (defun gnus-summary-work-articles (n)
9269   "Return a list of articles to be worked upon.  The prefix argument,
9270 the list of process marked articles, and the current article will be
9271 taken into consideration."
9272   (cond
9273    ((and n (numberp n))
9274     ;; A numerical prefix has been given.
9275     (let ((backward (< n 0))
9276           (n (abs n))
9277           articles article)
9278       (save-excursion
9279         (while
9280             (and (> n 0)
9281                  (push (setq article (gnus-summary-article-number))
9282                        articles)
9283                  (if backward
9284                      (gnus-summary-find-prev nil article)
9285                    (gnus-summary-find-next nil article)))
9286           (decf n)))
9287       (nreverse articles)))
9288    ((and (boundp 'transient-mark-mode)
9289          transient-mark-mode
9290          mark-active)
9291     ;; Work on the region between point and mark.
9292     (let ((max (max (point) (mark)))
9293           articles article)
9294       (save-excursion
9295         (goto-char (min (point) (mark)))
9296         (while
9297             (and
9298              (push (setq article (gnus-summary-article-number)) articles)
9299              (gnus-summary-find-next nil article)
9300              (< (point) max)))
9301         (nreverse articles))))
9302    (gnus-newsgroup-processable
9303     ;; There are process-marked articles present.
9304     (reverse gnus-newsgroup-processable))
9305    (t
9306     ;; Just return the current article.
9307     (list (gnus-summary-article-number)))))
9308
9309 (defun gnus-summary-search-group (&optional backward use-level)
9310   "Search for next unread newsgroup.
9311 If optional argument BACKWARD is non-nil, search backward instead."
9312   (save-excursion
9313     (set-buffer gnus-group-buffer)
9314     (if (gnus-group-search-forward
9315          backward nil (if use-level (gnus-group-group-level) nil))
9316         (gnus-group-group-name))))
9317
9318 (defun gnus-summary-best-group (&optional exclude-group)
9319   "Find the name of the best unread group.
9320 If EXCLUDE-GROUP, do not go to this group."
9321   (save-excursion
9322     (set-buffer gnus-group-buffer)
9323     (save-excursion
9324       (gnus-group-best-unread-group exclude-group))))
9325
9326 (defun gnus-summary-find-next (&optional unread article backward)
9327   (if backward (gnus-summary-find-prev)
9328     (let* ((article (or article (gnus-summary-article-number)))
9329            (arts (gnus-data-find-list article))
9330            result)
9331       (when (or (not gnus-summary-check-current)
9332                 (not unread)
9333                 (not (gnus-data-unread-p (car arts))))
9334         (setq arts (cdr arts)))
9335       (when (setq result
9336                   (if unread
9337                       (progn
9338                         (while arts
9339                           (when (gnus-data-unread-p (car arts))
9340                             (setq result (car arts)
9341                                   arts nil))
9342                           (setq arts (cdr arts)))
9343                         result)
9344                     (car arts)))
9345         (goto-char (gnus-data-pos result))
9346         (gnus-data-number result)))))
9347
9348 (defun gnus-summary-find-prev (&optional unread article)
9349   (let* ((article (or article (gnus-summary-article-number)))
9350          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9351          result)
9352     (when (or (not gnus-summary-check-current)
9353               (not unread)
9354               (not (gnus-data-unread-p (car arts))))
9355       (setq arts (cdr arts)))
9356     (if (setq result
9357               (if unread
9358                   (progn
9359                     (while arts
9360                       (and (gnus-data-unread-p (car arts))
9361                            (setq result (car arts)
9362                                  arts nil))
9363                       (setq arts (cdr arts)))
9364                     result)
9365                 (car arts)))
9366         (progn
9367           (goto-char (gnus-data-pos result))
9368           (gnus-data-number result)))))
9369
9370 (defun gnus-summary-find-subject (subject &optional unread backward article)
9371   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9372          (article (or article (gnus-summary-article-number)))
9373          (articles (gnus-data-list backward))
9374          (arts (gnus-data-find-list article articles))
9375          result)
9376     (when (or (not gnus-summary-check-current)
9377               (not unread)
9378               (not (gnus-data-unread-p (car arts))))
9379       (setq arts (cdr arts)))
9380     (while arts
9381       (and (or (not unread)
9382                (gnus-data-unread-p (car arts)))
9383            (vectorp (gnus-data-header (car arts)))
9384            (gnus-subject-equal
9385             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9386            (setq result (car arts)
9387                  arts nil))
9388       (setq arts (cdr arts)))
9389     (and result
9390          (goto-char (gnus-data-pos result))
9391          (gnus-data-number result))))
9392
9393 (defun gnus-summary-search-forward (&optional unread subject backward)
9394   "Search forward for an article.
9395 If UNREAD, look for unread articles.  If SUBJECT, look for
9396 articles with that subject.  If BACKWARD, search backward instead."
9397   (cond (subject (gnus-summary-find-subject subject unread backward))
9398         (backward (gnus-summary-find-prev unread))
9399         (t (gnus-summary-find-next unread))))
9400
9401 (defun gnus-recenter (&optional n)
9402   "Center point in window and redisplay frame.
9403 Also do horizontal recentering."
9404   (interactive "P")
9405   (when (and gnus-auto-center-summary
9406              (not (eq gnus-auto-center-summary 'vertical)))
9407     (gnus-horizontal-recenter))
9408   (recenter n))
9409
9410 (defun gnus-summary-recenter ()
9411   "Center point in the summary window.
9412 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9413 displayed, no centering will be performed."
9414   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9415   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9416   (let* ((top (cond ((< (window-height) 4) 0)
9417                     ((< (window-height) 7) 1)
9418                     (t 2)))
9419          (height (1- (window-height)))
9420          (bottom (save-excursion (goto-char (point-max))
9421                                  (forward-line (- height))
9422                                  (point)))
9423          (window (get-buffer-window (current-buffer))))
9424     ;; The user has to want it.
9425     (when gnus-auto-center-summary
9426       (when (get-buffer-window gnus-article-buffer)
9427        ;; Only do recentering when the article buffer is displayed,
9428        ;; Set the window start to either `bottom', which is the biggest
9429        ;; possible valid number, or the second line from the top,
9430        ;; whichever is the least.
9431        (set-window-start
9432         window (min bottom (save-excursion 
9433                              (forward-line (- top)) (point)))))
9434       ;; Do horizontal recentering while we're at it.
9435       (when (and (get-buffer-window (current-buffer) t)
9436                  (not (eq gnus-auto-center-summary 'vertical)))
9437         (let ((selected (selected-window)))
9438           (select-window (get-buffer-window (current-buffer) t))
9439           (gnus-summary-position-point)
9440           (gnus-horizontal-recenter)
9441           (select-window selected))))))
9442
9443 (defun gnus-horizontal-recenter ()
9444   "Recenter the current buffer horizontally."
9445   (if (< (current-column) (/ (window-width) 2))
9446       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9447     (let* ((orig (point))
9448            (end (window-end (get-buffer-window (current-buffer) t)))
9449            (max 0))
9450       ;; Find the longest line currently displayed in the window.
9451       (goto-char (window-start))
9452       (while (and (not (eobp)) 
9453                   (< (point) end))
9454         (end-of-line)
9455         (setq max (max max (current-column)))
9456         (forward-line 1))
9457       (goto-char orig)
9458       ;; Scroll horizontally to center (sort of) the point.
9459       (if (> max (window-width))
9460           (set-window-hscroll 
9461            (get-buffer-window (current-buffer) t)
9462            (min (- (current-column) (/ (window-width) 3))
9463                 (+ 2 (- max (window-width)))))
9464         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9465       max)))
9466
9467 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9468 (defun gnus-short-group-name (group &optional levels)
9469   "Collapse GROUP name LEVELS."
9470   (let* ((name "") 
9471          (foreign "")
9472          (depth 0) 
9473          (skip 1)
9474          (levels (or levels
9475                      (progn
9476                        (while (string-match "\\." group skip)
9477                          (setq skip (match-end 0)
9478                                depth (+ depth 1)))
9479                        depth))))
9480     (if (string-match ":" group)
9481         (setq foreign (substring group 0 (match-end 0))
9482               group (substring group (match-end 0))))
9483     (while group
9484       (if (and (string-match "\\." group)
9485                (> levels (- gnus-group-uncollapsed-levels 1)))
9486           (setq name (concat name (substring group 0 1))
9487                 group (substring group (match-end 0))
9488                 levels (- levels 1)
9489                 name (concat name "."))
9490         (setq name (concat foreign name group)
9491               group nil)))
9492     name))
9493
9494 (defun gnus-summary-jump-to-group (newsgroup)
9495   "Move point to NEWSGROUP in group mode buffer."
9496   ;; Keep update point of group mode buffer if visible.
9497   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9498       (save-window-excursion
9499         ;; Take care of tree window mode.
9500         (if (get-buffer-window gnus-group-buffer)
9501             (pop-to-buffer gnus-group-buffer))
9502         (gnus-group-jump-to-group newsgroup))
9503     (save-excursion
9504       ;; Take care of tree window mode.
9505       (if (get-buffer-window gnus-group-buffer)
9506           (pop-to-buffer gnus-group-buffer)
9507         (set-buffer gnus-group-buffer))
9508       (gnus-group-jump-to-group newsgroup))))
9509
9510 ;; This function returns a list of article numbers based on the
9511 ;; difference between the ranges of read articles in this group and
9512 ;; the range of active articles.
9513 (defun gnus-list-of-unread-articles (group)
9514   (let* ((read (gnus-info-read (gnus-get-info group)))
9515          (active (gnus-active group))
9516          (last (cdr active))
9517          first nlast unread)
9518     ;; If none are read, then all are unread.
9519     (if (not read)
9520         (setq first (car active))
9521       ;; If the range of read articles is a single range, then the
9522       ;; first unread article is the article after the last read
9523       ;; article.  Sounds logical, doesn't it?
9524       (if (not (listp (cdr read)))
9525           (setq first (1+ (cdr read)))
9526         ;; `read' is a list of ranges.
9527         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9528                                 (caar read))) 1)
9529             (setq first 1))
9530         (while read
9531           (if first
9532               (while (< first nlast)
9533                 (setq unread (cons first unread))
9534                 (setq first (1+ first))))
9535           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9536           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9537           (setq read (cdr read)))))
9538     ;; And add the last unread articles.
9539     (while (<= first last)
9540       (setq unread (cons first unread))
9541       (setq first (1+ first)))
9542     ;; Return the list of unread articles.
9543     (nreverse unread)))
9544
9545 (defun gnus-list-of-read-articles (group)
9546   "Return a list of unread, unticked and non-dormant articles."
9547   (let* ((info (gnus-get-info group))
9548          (marked (gnus-info-marks info))
9549          (active (gnus-active group)))
9550     (and info active
9551          (gnus-set-difference
9552           (gnus-sorted-complement
9553            (gnus-uncompress-range active)
9554            (gnus-list-of-unread-articles group))
9555           (append
9556            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9557            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9558
9559 ;; Various summary commands
9560
9561 (defun gnus-summary-universal-argument (arg)
9562   "Perform any operation on all articles that are process/prefixed."
9563   (interactive "P")
9564   (gnus-set-global-variables)
9565   (let ((articles (gnus-summary-work-articles arg))
9566         func article)
9567     (if (eq
9568          (setq
9569           func
9570           (key-binding
9571            (read-key-sequence
9572             (substitute-command-keys
9573              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9574              ))))
9575          'undefined)
9576         (progn
9577           (message "Undefined key")
9578           (ding))
9579       (save-excursion
9580         (while articles
9581           (gnus-summary-goto-subject (setq article (pop articles)))
9582           (command-execute func)
9583           (gnus-summary-remove-process-mark article)))))
9584   (gnus-summary-position-point))
9585
9586 (defun gnus-summary-toggle-truncation (&optional arg)
9587   "Toggle truncation of summary lines.
9588 With arg, turn line truncation on iff arg is positive."
9589   (interactive "P")
9590   (setq truncate-lines
9591         (if (null arg) (not truncate-lines)
9592           (> (prefix-numeric-value arg) 0)))
9593   (redraw-display))
9594
9595 (defun gnus-summary-reselect-current-group (&optional all rescan)
9596   "Exit and then reselect the current newsgroup.
9597 The prefix argument ALL means to select all articles."
9598   (interactive "P")
9599   (gnus-set-global-variables)
9600   (let ((current-subject (gnus-summary-article-number))
9601         (group gnus-newsgroup-name))
9602     (setq gnus-newsgroup-begin nil)
9603     (gnus-summary-exit)
9604     ;; We have to adjust the point of group mode buffer because the
9605     ;; current point was moved to the next unread newsgroup by
9606     ;; exiting.
9607     (gnus-summary-jump-to-group group)
9608     (when rescan
9609       (save-excursion
9610         (gnus-group-get-new-news-this-group 1)))
9611     (gnus-group-read-group all t)
9612     (gnus-summary-goto-subject current-subject)))
9613
9614 (defun gnus-summary-rescan-group (&optional all)
9615   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9616   (interactive "P")
9617   (gnus-summary-reselect-current-group all t))
9618
9619 (defun gnus-summary-update-info ()
9620   (let* ((group gnus-newsgroup-name))
9621     (when gnus-newsgroup-kill-headers
9622       (setq gnus-newsgroup-killed
9623             (gnus-compress-sequence
9624              (nconc
9625               (gnus-set-sorted-intersection
9626                (gnus-uncompress-range gnus-newsgroup-killed)
9627                (setq gnus-newsgroup-unselected
9628                      (sort gnus-newsgroup-unselected '<)))
9629               (setq gnus-newsgroup-unreads
9630                     (sort gnus-newsgroup-unreads '<))) t)))
9631     (unless (listp (cdr gnus-newsgroup-killed))
9632       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9633     (let ((headers gnus-newsgroup-headers))
9634       (run-hooks 'gnus-exit-group-hook)
9635       (unless gnus-save-score
9636         (setq gnus-newsgroup-scored nil))
9637       ;; Set the new ranges of read articles.
9638       (gnus-update-read-articles
9639        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9640       ;; Set the current article marks.
9641       (gnus-update-marks)
9642       ;; Do the cross-ref thing.
9643       (when gnus-use-cross-reference
9644         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9645       ;; Do adaptive scoring, and possibly save score files.
9646       (when gnus-newsgroup-adaptive
9647         (gnus-score-adaptive))
9648       (when gnus-use-scoring
9649         (gnus-score-save))
9650       ;; Do not switch windows but change the buffer to work.
9651       (set-buffer gnus-group-buffer)
9652       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9653           (gnus-group-update-group group)))))
9654
9655 (defun gnus-summary-exit (&optional temporary)
9656   "Exit reading current newsgroup, and then return to group selection mode.
9657 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9658   (interactive)
9659   (gnus-set-global-variables)
9660   (gnus-kill-save-kill-buffer)
9661   (let* ((group gnus-newsgroup-name)
9662          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9663          (mode major-mode)
9664          (buf (current-buffer)))
9665     (unless temporary
9666       (run-hooks 'gnus-summary-prepare-exit-hook))
9667     ;; If we have several article buffers, we kill them at exit.
9668     (unless gnus-single-article-buffer
9669       (gnus-kill-buffer gnus-article-buffer)
9670       (gnus-kill-buffer gnus-original-article-buffer)
9671       (setq gnus-article-current nil))
9672     (when gnus-use-cache
9673       (gnus-cache-possibly-remove-articles)
9674       (gnus-cache-save-buffers))
9675     (when gnus-use-trees
9676       (gnus-tree-close group))
9677     ;; Make all changes in this group permanent.
9678     (unless quit-config
9679       (gnus-summary-update-info))
9680     (gnus-close-group group)
9681     ;; Make sure where I was, and go to next newsgroup.
9682     (set-buffer gnus-group-buffer)
9683     (unless quit-config
9684       (gnus-group-jump-to-group group)
9685       (gnus-group-next-unread-group 1))
9686     (run-hooks 'gnus-summary-exit-hook)
9687     (unless gnus-single-article-buffer
9688       (setq gnus-article-current nil))
9689     (if temporary
9690         nil                             ;Nothing to do.
9691       ;; If we have several article buffers, we kill them at exit.
9692       (unless gnus-single-article-buffer
9693         (gnus-kill-buffer gnus-article-buffer)
9694         (gnus-kill-buffer gnus-original-article-buffer)
9695         (setq gnus-article-current nil))
9696       (set-buffer buf)
9697       (if (not gnus-kill-summary-on-exit)
9698           (gnus-deaden-summary)
9699         ;; We set all buffer-local variables to nil.  It is unclear why
9700         ;; this is needed, but if we don't, buffer-local variables are
9701         ;; not garbage-collected, it seems.  This would the lead to en
9702         ;; ever-growing Emacs.
9703         (gnus-summary-clear-local-variables)
9704         (when (get-buffer gnus-article-buffer)
9705           (bury-buffer gnus-article-buffer))
9706         ;; We clear the global counterparts of the buffer-local
9707         ;; variables as well, just to be on the safe side.
9708         (gnus-configure-windows 'group 'force)
9709         (gnus-summary-clear-local-variables)
9710         ;; Return to group mode buffer.
9711         (if (eq mode 'gnus-summary-mode)
9712             (gnus-kill-buffer buf)))
9713       (setq gnus-current-select-method gnus-select-method)
9714       (pop-to-buffer gnus-group-buffer)
9715       ;; Clear the current group name.
9716       (if (not quit-config)
9717           (progn
9718             (gnus-group-jump-to-group group)
9719             (gnus-group-next-unread-group 1)
9720             (gnus-configure-windows 'group 'force))
9721         (if (not (buffer-name (car quit-config)))
9722             (gnus-configure-windows 'group 'force)
9723           (set-buffer (car quit-config))
9724           (and (eq major-mode 'gnus-summary-mode)
9725                (gnus-set-global-variables))
9726           (gnus-configure-windows (cdr quit-config))))
9727       (unless quit-config
9728         (setq gnus-newsgroup-name nil)))))
9729
9730 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9731 (defun gnus-summary-exit-no-update (&optional no-questions)
9732   "Quit reading current newsgroup without updating read article info."
9733   (interactive)
9734   (gnus-set-global-variables)
9735   (let* ((group gnus-newsgroup-name)
9736          (quit-config (gnus-group-quit-config group)))
9737     (when (or no-questions
9738               gnus-expert-user
9739               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9740       ;; If we have several article buffers, we kill them at exit.
9741       (unless gnus-single-article-buffer
9742         (gnus-kill-buffer gnus-article-buffer)
9743         (gnus-kill-buffer gnus-original-article-buffer)
9744         (setq gnus-article-current nil))
9745       (if (not gnus-kill-summary-on-exit)
9746           (gnus-deaden-summary)
9747         (gnus-close-group group)
9748         (gnus-summary-clear-local-variables)
9749         (set-buffer gnus-group-buffer)
9750         (gnus-summary-clear-local-variables)
9751         (when (get-buffer gnus-summary-buffer)
9752           (kill-buffer gnus-summary-buffer)))
9753       (unless gnus-single-article-buffer
9754         (setq gnus-article-current nil))
9755       (when gnus-use-trees
9756         (gnus-tree-close group))
9757       (when (get-buffer gnus-article-buffer)
9758         (bury-buffer gnus-article-buffer))
9759       ;; Return to the group buffer.
9760       (gnus-configure-windows 'group 'force)
9761       ;; Clear the current group name.
9762       (setq gnus-newsgroup-name nil)
9763       (when (equal (gnus-group-group-name) group)
9764         (gnus-group-next-unread-group 1))
9765       (when quit-config
9766         (if (not (buffer-name (car quit-config)))
9767             (gnus-configure-windows 'group 'force)
9768           (set-buffer (car quit-config))
9769           (when (eq major-mode 'gnus-summary-mode)
9770             (gnus-set-global-variables))
9771           (gnus-configure-windows (cdr quit-config)))))))
9772
9773 ;;; Dead summaries.
9774
9775 (defvar gnus-dead-summary-mode-map nil)
9776
9777 (if gnus-dead-summary-mode-map
9778     nil
9779   (setq gnus-dead-summary-mode-map (make-keymap))
9780   (suppress-keymap gnus-dead-summary-mode-map)
9781   (substitute-key-definition
9782    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9783   (let ((keys '("\C-d" "\r" "\177")))
9784     (while keys
9785       (define-key gnus-dead-summary-mode-map
9786         (pop keys) 'gnus-summary-wake-up-the-dead))))
9787
9788 (defvar gnus-dead-summary-mode nil
9789   "Minor mode for Gnus summary buffers.")
9790
9791 (defun gnus-dead-summary-mode (&optional arg)
9792   "Minor mode for Gnus summary buffers."
9793   (interactive "P")
9794   (when (eq major-mode 'gnus-summary-mode)
9795     (make-local-variable 'gnus-dead-summary-mode)
9796     (setq gnus-dead-summary-mode
9797           (if (null arg) (not gnus-dead-summary-mode)
9798             (> (prefix-numeric-value arg) 0)))
9799     (when gnus-dead-summary-mode
9800       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9801         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9802       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9803         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9804               minor-mode-map-alist)))))
9805
9806 (defun gnus-deaden-summary ()
9807   "Make the current summary buffer into a dead summary buffer."
9808   ;; Kill any previous dead summary buffer.
9809   (when (and gnus-dead-summary
9810              (buffer-name gnus-dead-summary))
9811     (save-excursion
9812       (set-buffer gnus-dead-summary)
9813       (when gnus-dead-summary-mode
9814         (kill-buffer (current-buffer)))))
9815   ;; Make this the current dead summary.
9816   (setq gnus-dead-summary (current-buffer))
9817   (gnus-dead-summary-mode 1)
9818   (let ((name (buffer-name)))
9819     (when (string-match "Summary" name)
9820       (rename-buffer
9821        (concat (substring name 0 (match-beginning 0)) "Dead "
9822                (substring name (match-beginning 0))) t))))
9823
9824 (defun gnus-kill-or-deaden-summary (buffer)
9825   "Kill or deaden the summary BUFFER."
9826   (cond (gnus-kill-summary-on-exit
9827          (when (and gnus-use-trees
9828                     (and (get-buffer buffer)
9829                          (buffer-name (get-buffer buffer))))
9830            (save-excursion
9831              (set-buffer (get-buffer buffer))
9832              (gnus-tree-close gnus-newsgroup-name)))
9833          (gnus-kill-buffer buffer))
9834         ((and (get-buffer buffer)
9835               (buffer-name (get-buffer buffer)))
9836          (save-excursion
9837            (set-buffer buffer)
9838            (gnus-deaden-summary)))))
9839
9840 (defun gnus-summary-wake-up-the-dead (&rest args)
9841   "Wake up the dead summary buffer."
9842   (interactive)
9843   (gnus-dead-summary-mode -1)
9844   (let ((name (buffer-name)))
9845     (when (string-match "Dead " name)
9846       (rename-buffer
9847        (concat (substring name 0 (match-beginning 0))
9848                (substring name (match-end 0))) t)))
9849   (gnus-message 3 "This dead summary is now alive again"))
9850
9851 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9852 (defun gnus-summary-fetch-faq (&optional faq-dir)
9853   "Fetch the FAQ for the current group.
9854 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9855 in."
9856   (interactive
9857    (list
9858     (if current-prefix-arg
9859         (completing-read
9860          "Faq dir: " (and (listp gnus-group-faq-directory)
9861                           gnus-group-faq-directory)))))
9862   (let (gnus-faq-buffer)
9863     (and (setq gnus-faq-buffer
9864                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9865          (gnus-configure-windows 'summary-faq))))
9866
9867 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9868 (defun gnus-summary-describe-group (&optional force)
9869   "Describe the current newsgroup."
9870   (interactive "P")
9871   (gnus-group-describe-group force gnus-newsgroup-name))
9872
9873 (defun gnus-summary-describe-briefly ()
9874   "Describe summary mode commands briefly."
9875   (interactive)
9876   (gnus-message 6
9877                 (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")))
9878
9879 ;; Walking around group mode buffer from summary mode.
9880
9881 (defun gnus-summary-next-group (&optional no-article target-group backward)
9882   "Exit current newsgroup and then select next unread newsgroup.
9883 If prefix argument NO-ARTICLE is non-nil, no article is selected
9884 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9885 previous group instead."
9886   (interactive "P")
9887   (gnus-set-global-variables)
9888   (let ((current-group gnus-newsgroup-name)
9889         (current-buffer (current-buffer))
9890         entered)
9891     ;; First we semi-exit this group to update Xrefs and all variables.
9892     ;; We can't do a real exit, because the window conf must remain
9893     ;; the same in case the user is prompted for info, and we don't
9894     ;; want the window conf to change before that...
9895     (gnus-summary-exit t)
9896     (while (not entered)
9897       ;; Then we find what group we are supposed to enter.
9898       (set-buffer gnus-group-buffer)
9899       (gnus-group-jump-to-group current-group)
9900       (setq target-group
9901             (or target-group
9902                 (if (eq gnus-keep-same-level 'best)
9903                     (gnus-summary-best-group gnus-newsgroup-name)
9904                   (gnus-summary-search-group backward gnus-keep-same-level))))
9905       (if (not target-group)
9906           ;; There are no further groups, so we return to the group
9907           ;; buffer.
9908           (progn
9909             (gnus-message 5 "Returning to the group buffer")
9910             (setq entered t)
9911             (set-buffer current-buffer)
9912             (gnus-summary-exit))
9913         ;; We try to enter the target group.
9914         (gnus-group-jump-to-group target-group)
9915         (let ((unreads (gnus-group-group-unread)))
9916           (if (and (or (eq t unreads)
9917                        (and unreads (not (zerop unreads))))
9918                    (gnus-summary-read-group
9919                     target-group nil no-article current-buffer))
9920               (setq entered t)
9921             (setq current-group target-group
9922                   target-group nil)))))))
9923
9924 (defun gnus-summary-prev-group (&optional no-article)
9925   "Exit current newsgroup and then select previous unread newsgroup.
9926 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9927   (interactive "P")
9928   (gnus-summary-next-group no-article nil t))
9929
9930 ;; Walking around summary lines.
9931
9932 (defun gnus-summary-first-subject (&optional unread)
9933   "Go to the first unread subject.
9934 If UNREAD is non-nil, go to the first unread article.
9935 Returns the article selected or nil if there are no unread articles."
9936   (interactive "P")
9937   (prog1
9938       (cond
9939        ;; Empty summary.
9940        ((null gnus-newsgroup-data)
9941         (gnus-message 3 "No articles in the group")
9942         nil)
9943        ;; Pick the first article.
9944        ((not unread)
9945         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9946         (gnus-data-number (car gnus-newsgroup-data)))
9947        ;; No unread articles.
9948        ((null gnus-newsgroup-unreads)
9949         (gnus-message 3 "No more unread articles")
9950         nil)
9951        ;; Find the first unread article.
9952        (t
9953         (let ((data gnus-newsgroup-data))
9954           (while (and data
9955                       (not (gnus-data-unread-p (car data))))
9956             (setq data (cdr data)))
9957           (if data
9958               (progn
9959                 (goto-char (gnus-data-pos (car data)))
9960                 (gnus-data-number (car data)))))))
9961     (gnus-summary-position-point)))
9962
9963 (defun gnus-summary-next-subject (n &optional unread dont-display)
9964   "Go to next N'th summary line.
9965 If N is negative, go to the previous N'th subject line.
9966 If UNREAD is non-nil, only unread articles are selected.
9967 The difference between N and the actual number of steps taken is
9968 returned."
9969   (interactive "p")
9970   (let ((backward (< n 0))
9971         (n (abs n)))
9972     (while (and (> n 0)
9973                 (if backward
9974                     (gnus-summary-find-prev unread)
9975                   (gnus-summary-find-next unread)))
9976       (setq n (1- n)))
9977     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9978                                (if unread " unread" "")))
9979     (unless dont-display
9980       (gnus-summary-recenter)
9981       (gnus-summary-position-point))
9982     n))
9983
9984 (defun gnus-summary-next-unread-subject (n)
9985   "Go to next N'th unread summary line."
9986   (interactive "p")
9987   (gnus-summary-next-subject n t))
9988
9989 (defun gnus-summary-prev-subject (n &optional unread)
9990   "Go to previous N'th summary line.
9991 If optional argument UNREAD is non-nil, only unread article is selected."
9992   (interactive "p")
9993   (gnus-summary-next-subject (- n) unread))
9994
9995 (defun gnus-summary-prev-unread-subject (n)
9996   "Go to previous N'th unread summary line."
9997   (interactive "p")
9998   (gnus-summary-next-subject (- n) t))
9999
10000 (defun gnus-summary-goto-subject (article &optional force silent)
10001   "Go the subject line of ARTICLE.
10002 If FORCE, also allow jumping to articles not currently shown."
10003   (let ((b (point))
10004         (data (gnus-data-find article)))
10005     ;; We read in the article if we have to.
10006     (and (not data)
10007          force
10008          (gnus-summary-insert-subject article)
10009          (setq data (gnus-data-find article)))
10010     (goto-char b)
10011     (if (not data)
10012         (progn
10013           (unless silent
10014             (gnus-message 3 "Can't find article %d" article))
10015           nil)
10016       (goto-char (gnus-data-pos data))
10017       article)))
10018
10019 ;; Walking around summary lines with displaying articles.
10020
10021 (defun gnus-summary-expand-window (&optional arg)
10022   "Make the summary buffer take up the entire Emacs frame.
10023 Given a prefix, will force an `article' buffer configuration."
10024   (interactive "P")
10025   (gnus-set-global-variables)
10026   (if arg
10027       (gnus-configure-windows 'article 'force)
10028     (gnus-configure-windows 'summary 'force)))
10029
10030 (defun gnus-summary-display-article (article &optional all-header)
10031   "Display ARTICLE in article buffer."
10032   (gnus-set-global-variables)
10033   (if (null article)
10034       nil
10035     (prog1
10036         (if gnus-summary-display-article-function
10037             (funcall gnus-summary-display-article-function article all-header)
10038           (gnus-article-prepare article all-header))
10039       (run-hooks 'gnus-select-article-hook)
10040       (gnus-summary-recenter)
10041       (gnus-summary-goto-subject article)
10042       (when gnus-use-trees
10043         (gnus-possibly-generate-tree article)
10044         (gnus-highlight-selected-tree article))
10045       ;; Successfully display article.
10046       (gnus-article-set-window-start
10047        (cdr (assq article gnus-newsgroup-bookmarks)))
10048       t)))
10049
10050 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10051   "Select the current article.
10052 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10053 non-nil, the article will be re-fetched even if it already present in
10054 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10055 be displayed."
10056   (let ((article (or article (gnus-summary-article-number)))
10057         (all-headers (not (not all-headers))) ;Must be T or NIL.
10058         gnus-summary-display-article-function
10059         did)
10060     (and (not pseudo)
10061          (gnus-summary-article-pseudo-p article)
10062          (error "This is a pseudo-article."))
10063     (prog1
10064         (save-excursion
10065           (set-buffer gnus-summary-buffer)
10066           (if (or (and gnus-single-article-buffer
10067                        (or (null gnus-current-article)
10068                            (null gnus-article-current)
10069                            (null (get-buffer gnus-article-buffer))
10070                            (not (eq article (cdr gnus-article-current)))
10071                            (not (equal (car gnus-article-current)
10072                                        gnus-newsgroup-name))))
10073                   (and (not gnus-single-article-buffer)
10074                        (or (null gnus-current-article)
10075                            (not (eq gnus-current-article article))))
10076                   force)
10077               ;; The requested article is different from the current article.
10078               (prog1
10079                   (gnus-summary-display-article article all-headers)
10080                 (setq did article))
10081             (if (or all-headers gnus-show-all-headers)
10082                 (gnus-article-show-all-headers))
10083             'old))
10084       (if did
10085           (gnus-article-set-window-start
10086            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10087
10088 (defun gnus-summary-set-current-mark (&optional current-mark)
10089   "Obsolete function."
10090   nil)
10091
10092 (defun gnus-summary-next-article (&optional unread subject backward push)
10093   "Select the next article.
10094 If UNREAD, only unread articles are selected.
10095 If SUBJECT, only articles with SUBJECT are selected.
10096 If BACKWARD, the previous article is selected instead of the next."
10097   (interactive "P")
10098   (gnus-set-global-variables)
10099   (cond
10100    ;; Is there such an article?
10101    ((and (gnus-summary-search-forward unread subject backward)
10102          (or (gnus-summary-display-article (gnus-summary-article-number))
10103              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10104     (gnus-summary-position-point))
10105    ;; If not, we try the first unread, if that is wanted.
10106    ((and subject
10107          gnus-auto-select-same
10108          (or (gnus-summary-first-unread-article)
10109              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10110     (gnus-summary-position-point)
10111     (gnus-message 6 "Wrapped"))
10112    ;; Try to get next/previous article not displayed in this group.
10113    ((and gnus-auto-extend-newsgroup
10114          (not unread) (not subject))
10115     (gnus-summary-goto-article
10116      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10117      nil t))
10118    ;; Go to next/previous group.
10119    (t
10120     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10121         (gnus-summary-jump-to-group gnus-newsgroup-name))
10122     (let ((cmd last-command-char)
10123           (group
10124            (if (eq gnus-keep-same-level 'best)
10125                (gnus-summary-best-group gnus-newsgroup-name)
10126              (gnus-summary-search-group backward gnus-keep-same-level))))
10127       ;; For some reason, the group window gets selected.  We change
10128       ;; it back.
10129       (select-window (get-buffer-window (current-buffer)))
10130       ;; Select next unread newsgroup automagically.
10131       (cond
10132        ((not gnus-auto-select-next)
10133         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10134        ((or (eq gnus-auto-select-next 'quietly)
10135             (and (eq gnus-auto-select-next 'slightly-quietly)
10136                  push)
10137             (and (eq gnus-auto-select-next 'almost-quietly)
10138                  (gnus-summary-last-article-p)))
10139         ;; Select quietly.
10140         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10141             (gnus-summary-exit)
10142           (gnus-message 7 "No more%s articles (%s)..."
10143                         (if unread " unread" "")
10144                         (if group (concat "selecting " group)
10145                           "exiting"))
10146           (gnus-summary-next-group nil group backward)))
10147        (t
10148         (gnus-summary-walk-group-buffer
10149          gnus-newsgroup-name cmd unread backward)))))))
10150
10151 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10152   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10153                       (?\C-p (gnus-group-prev-unread-group 1))))
10154         keve key group ended)
10155     (save-excursion
10156       (set-buffer gnus-group-buffer)
10157       (gnus-summary-jump-to-group from-group)
10158       (setq group
10159             (if (eq gnus-keep-same-level 'best)
10160                 (gnus-summary-best-group gnus-newsgroup-name)
10161               (gnus-summary-search-group backward gnus-keep-same-level))))
10162     (while (not ended)
10163       (gnus-message
10164        5 "No more%s articles%s" (if unread " unread" "")
10165        (if (and group
10166                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10167            (format " (Type %s for %s [%s])"
10168                    (single-key-description cmd) group
10169                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10170          (format " (Type %s to exit %s)"
10171                  (single-key-description cmd)
10172                  gnus-newsgroup-name)))
10173       ;; Confirm auto selection.
10174       (setq key (car (setq keve (gnus-read-event-char))))
10175       (setq ended t)
10176       (cond
10177        ((assq key keystrokes)
10178         (let ((obuf (current-buffer)))
10179           (switch-to-buffer gnus-group-buffer)
10180           (and group
10181                (gnus-group-jump-to-group group))
10182           (eval (cadr (assq key keystrokes)))
10183           (setq group (gnus-group-group-name))
10184           (switch-to-buffer obuf))
10185         (setq ended nil))
10186        ((equal key cmd)
10187         (if (or (not group)
10188                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10189             (gnus-summary-exit)
10190           (gnus-summary-next-group nil group backward)))
10191        (t
10192         (push (cdr keve) unread-command-events))))))
10193
10194 (defun gnus-read-event-char ()
10195   "Get the next event."
10196   (let ((event (read-event)))
10197     (cons (and (numberp event) event) event)))
10198
10199 (defun gnus-summary-next-unread-article ()
10200   "Select unread article after current one."
10201   (interactive)
10202   (gnus-summary-next-article t (and gnus-auto-select-same
10203                                     (gnus-summary-article-subject))))
10204
10205 (defun gnus-summary-prev-article (&optional unread subject)
10206   "Select the article after the current one.
10207 If UNREAD is non-nil, only unread articles are selected."
10208   (interactive "P")
10209   (gnus-summary-next-article unread subject t))
10210
10211 (defun gnus-summary-prev-unread-article ()
10212   "Select unred article before current one."
10213   (interactive)
10214   (gnus-summary-prev-article t (and gnus-auto-select-same
10215                                     (gnus-summary-article-subject))))
10216
10217 (defun gnus-summary-next-page (&optional lines circular)
10218   "Show next page of the selected article.
10219 If at the end of the current article, select the next article.
10220 LINES says how many lines should be scrolled up.
10221
10222 If CIRCULAR is non-nil, go to the start of the article instead of
10223 selecting the next article when reaching the end of the current
10224 article."
10225   (interactive "P")
10226   (setq gnus-summary-buffer (current-buffer))
10227   (gnus-set-global-variables)
10228   (let ((article (gnus-summary-article-number))
10229         (endp nil))
10230     (gnus-configure-windows 'article)
10231     (if (or (null gnus-current-article)
10232             (null gnus-article-current)
10233             (/= article (cdr gnus-article-current))
10234             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10235         ;; Selected subject is different from current article's.
10236         (gnus-summary-display-article article)
10237       (gnus-eval-in-buffer-window
10238        gnus-article-buffer
10239        (setq endp (gnus-article-next-page lines)))
10240       (if endp
10241           (cond (circular
10242                  (gnus-summary-beginning-of-article))
10243                 (lines
10244                  (gnus-message 3 "End of message"))
10245                 ((null lines)
10246                  (if (and (eq gnus-summary-goto-unread 'never)
10247                           (not (gnus-summary-last-article-p article)))
10248                      (gnus-summary-next-article)
10249                    (gnus-summary-next-unread-article))))))
10250     (gnus-summary-recenter)
10251     (gnus-summary-position-point)))
10252
10253 (defun gnus-summary-prev-page (&optional lines)
10254   "Show previous page of selected article.
10255 Argument LINES specifies lines to be scrolled down."
10256   (interactive "P")
10257   (gnus-set-global-variables)
10258   (let ((article (gnus-summary-article-number)))
10259     (gnus-configure-windows 'article)
10260     (if (or (null gnus-current-article)
10261             (null gnus-article-current)
10262             (/= article (cdr gnus-article-current))
10263             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10264         ;; Selected subject is different from current article's.
10265         (gnus-summary-display-article article)
10266       (gnus-summary-recenter)
10267       (gnus-eval-in-buffer-window gnus-article-buffer
10268                                   (gnus-article-prev-page lines))))
10269   (gnus-summary-position-point))
10270
10271 (defun gnus-summary-scroll-up (lines)
10272   "Scroll up (or down) one line current article.
10273 Argument LINES specifies lines to be scrolled up (or down if negative)."
10274   (interactive "p")
10275   (gnus-set-global-variables)
10276   (gnus-configure-windows 'article)
10277   (gnus-summary-show-thread)
10278   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10279     (gnus-eval-in-buffer-window
10280      gnus-article-buffer
10281      (cond ((> lines 0)
10282             (if (gnus-article-next-page lines)
10283                 (gnus-message 3 "End of message")))
10284            ((< lines 0)
10285             (gnus-article-prev-page (- lines))))))
10286   (gnus-summary-recenter)
10287   (gnus-summary-position-point))
10288
10289 (defun gnus-summary-next-same-subject ()
10290   "Select next article which has the same subject as current one."
10291   (interactive)
10292   (gnus-set-global-variables)
10293   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10294
10295 (defun gnus-summary-prev-same-subject ()
10296   "Select previous article which has the same subject as current one."
10297   (interactive)
10298   (gnus-set-global-variables)
10299   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10300
10301 (defun gnus-summary-next-unread-same-subject ()
10302   "Select next unread article which has the same subject as current one."
10303   (interactive)
10304   (gnus-set-global-variables)
10305   (gnus-summary-next-article t (gnus-summary-article-subject)))
10306
10307 (defun gnus-summary-prev-unread-same-subject ()
10308   "Select previous unread article which has the same subject as current one."
10309   (interactive)
10310   (gnus-set-global-variables)
10311   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10312
10313 (defun gnus-summary-first-unread-article ()
10314   "Select the first unread article.
10315 Return nil if there are no unread articles."
10316   (interactive)
10317   (gnus-set-global-variables)
10318   (prog1
10319       (if (gnus-summary-first-subject t)
10320           (progn
10321             (gnus-summary-show-thread)
10322             (gnus-summary-first-subject t)
10323             (gnus-summary-display-article (gnus-summary-article-number))))
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-best-unread-article ()
10327   "Select the unread article with the highest score."
10328   (interactive)
10329   (gnus-set-global-variables)
10330   (let ((best -1000000)
10331         (data gnus-newsgroup-data)
10332         article score)
10333     (while data
10334       (and (gnus-data-unread-p (car data))
10335            (> (setq score
10336                     (gnus-summary-article-score (gnus-data-number (car data))))
10337               best)
10338            (setq best score
10339                  article (gnus-data-number (car data))))
10340       (setq data (cdr data)))
10341     (if article
10342         (gnus-summary-goto-article article)
10343       (error "No unread articles"))
10344     (gnus-summary-position-point)))
10345
10346 (defun gnus-summary-last-subject ()
10347   "Go to the last displayed subject line in the group."
10348   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10349     (when article
10350       (gnus-summary-goto-subject article))))
10351
10352 (defun gnus-summary-goto-article (article &optional all-headers force)
10353   "Fetch ARTICLE and display it if it exists.
10354 If ALL-HEADERS is non-nil, no header lines are hidden."
10355   (interactive
10356    (list
10357     (string-to-int
10358      (completing-read
10359       "Article number: "
10360       (mapcar (lambda (number) (list (int-to-string number)))
10361               gnus-newsgroup-limit)))
10362     current-prefix-arg
10363     t))
10364   (prog1
10365       (if (gnus-summary-goto-subject article force)
10366           (gnus-summary-display-article article all-headers)
10367         (gnus-message 4 "Couldn't go to article %s" article) nil)
10368     (gnus-summary-position-point)))
10369
10370 (defun gnus-summary-goto-last-article ()
10371   "Go to the previously read article."
10372   (interactive)
10373   (prog1
10374       (and gnus-last-article
10375            (gnus-summary-goto-article gnus-last-article))
10376     (gnus-summary-position-point)))
10377
10378 (defun gnus-summary-pop-article (number)
10379   "Pop one article off the history and go to the previous.
10380 NUMBER articles will be popped off."
10381   (interactive "p")
10382   (let (to)
10383     (setq gnus-newsgroup-history
10384           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10385     (if to
10386         (gnus-summary-goto-article (car to))
10387       (error "Article history empty")))
10388   (gnus-summary-position-point))
10389
10390 ;; Summary commands and functions for limiting the summary buffer.
10391
10392 (defun gnus-summary-limit-to-articles (n)
10393   "Limit the summary buffer to the next N articles.
10394 If not given a prefix, use the process marked articles instead."
10395   (interactive "P")
10396   (gnus-set-global-variables)
10397   (prog1
10398       (let ((articles (gnus-summary-work-articles n)))
10399         (setq gnus-newsgroup-processable nil)
10400         (gnus-summary-limit articles))
10401     (gnus-summary-position-point)))
10402
10403 (defun gnus-summary-pop-limit (&optional total)
10404   "Restore the previous limit.
10405 If given a prefix, remove all limits."
10406   (interactive "P")
10407   (gnus-set-global-variables)
10408   (when total 
10409     (setq gnus-newsgroup-limits
10410           (list (mapcar (lambda (h) (mail-header-number h))
10411                         gnus-newsgroup-headers))))
10412   (unless gnus-newsgroup-limits
10413     (error "No limit to pop"))
10414   (prog1
10415       (gnus-summary-limit nil 'pop)
10416     (gnus-summary-position-point)))
10417
10418 (defun gnus-summary-limit-to-subject (subject &optional header)
10419   "Limit the summary buffer to articles that have subjects that match a regexp."
10420   (interactive "sRegexp: ")
10421   (unless header
10422     (setq header "subject"))
10423   (when (not (equal "" subject))
10424     (prog1
10425         (let ((articles (gnus-summary-find-matching
10426                          (or header "subject") subject 'all)))
10427           (or articles (error "Found no matches for \"%s\"" subject))
10428           (gnus-summary-limit articles))
10429       (gnus-summary-position-point))))
10430
10431 (defun gnus-summary-limit-to-author (from)
10432   "Limit the summary buffer to articles that have authors that match a regexp."
10433   (interactive "sRegexp: ")
10434   (gnus-summary-limit-to-subject from "from"))
10435
10436 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10437 (make-obsolete
10438  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10439
10440 (defun gnus-summary-limit-to-unread (&optional all)
10441   "Limit the summary buffer to articles that are not marked as read.
10442 If ALL is non-nil, limit strictly to unread articles."
10443   (interactive "P")
10444   (if all
10445       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10446     (gnus-summary-limit-to-marks
10447      ;; Concat all the marks that say that an article is read and have
10448      ;; those removed.
10449      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10450            gnus-killed-mark gnus-kill-file-mark
10451            gnus-low-score-mark gnus-expirable-mark
10452            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10453      'reverse)))
10454
10455 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10456 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10457
10458 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10459   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10460 If REVERSE, limit the summary buffer to articles that are not marked
10461 with MARKS.  MARKS can either be a string of marks or a list of marks.
10462 Returns how many articles were removed."
10463   (interactive "sMarks: ")
10464   (gnus-set-global-variables)
10465   (prog1
10466       (let ((data gnus-newsgroup-data)
10467             (marks (if (listp marks) marks
10468                      (append marks nil))) ; Transform to list.
10469             articles)
10470         (while data
10471           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10472                  (memq (gnus-data-mark (car data)) marks))
10473                (setq articles (cons (gnus-data-number (car data)) articles)))
10474           (setq data (cdr data)))
10475         (gnus-summary-limit articles))
10476     (gnus-summary-position-point)))
10477
10478 (defun gnus-summary-limit-to-score (&optional score)
10479   "Limit to articles with score at or above SCORE."
10480   (interactive "P")
10481   (gnus-set-global-variables)
10482   (setq score (if score
10483                   (prefix-numeric-value score)
10484                 (or gnus-summary-default-score 0)))
10485   (let ((data gnus-newsgroup-data)
10486         articles)
10487     (while data
10488       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10489                 score)
10490         (push (gnus-data-number (car data)) articles))
10491       (setq data (cdr data)))
10492     (prog1
10493         (gnus-summary-limit articles)
10494       (gnus-summary-position-point))))
10495
10496 (defun gnus-summary-limit-include-dormant ()
10497   "Display all the hidden articles that are marked as dormant."
10498   (interactive)
10499   (gnus-set-global-variables)
10500   (or gnus-newsgroup-dormant
10501       (error "There are no dormant articles in this group"))
10502   (prog1
10503       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10504     (gnus-summary-position-point)))
10505
10506 (defun gnus-summary-limit-exclude-dormant ()
10507   "Hide all dormant articles."
10508   (interactive)
10509   (gnus-set-global-variables)
10510   (prog1
10511       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10512     (gnus-summary-position-point)))
10513
10514 (defun gnus-summary-limit-exclude-childless-dormant ()
10515   "Hide all dormant articles that have no children."
10516   (interactive)
10517   (gnus-set-global-variables)
10518   (let ((data (gnus-data-list t))
10519         articles d children)
10520     ;; Find all articles that are either not dormant or have
10521     ;; children.
10522     (while (setq d (pop data))
10523       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10524                 (and (setq children 
10525                            (gnus-article-children (gnus-data-number d)))
10526                      (let (found)
10527                        (while children
10528                          (when (memq (car children) articles)
10529                            (setq children nil
10530                                  found t))
10531                          (pop children))
10532                        found)))
10533         (push (gnus-data-number d) articles)))
10534     ;; Do the limiting.
10535     (prog1
10536         (gnus-summary-limit articles)
10537       (gnus-summary-position-point))))
10538
10539 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10540   "Mark all unread excluded articles as read.
10541 If ALL, mark even excluded ticked and dormants as read."
10542   (interactive "P")
10543   (let ((articles (gnus-sorted-complement
10544                    (sort
10545                     (mapcar (lambda (h) (mail-header-number h))
10546                             gnus-newsgroup-headers)
10547                     '<)
10548                    (sort gnus-newsgroup-limit '<)))
10549         article)
10550     (setq gnus-newsgroup-unreads nil)
10551     (if all
10552         (setq gnus-newsgroup-dormant nil
10553               gnus-newsgroup-marked nil
10554               gnus-newsgroup-reads
10555               (nconc
10556                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10557                gnus-newsgroup-reads))
10558       (while (setq article (pop articles))
10559         (unless (or (memq article gnus-newsgroup-dormant)
10560                     (memq article gnus-newsgroup-marked))
10561           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10562
10563 (defun gnus-summary-limit (articles &optional pop)
10564   (if pop
10565       ;; We pop the previous limit off the stack and use that.
10566       (setq articles (car gnus-newsgroup-limits)
10567             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10568     ;; We use the new limit, so we push the old limit on the stack.
10569     (setq gnus-newsgroup-limits
10570           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10571   ;; Set the limit.
10572   (setq gnus-newsgroup-limit articles)
10573   (let ((total (length gnus-newsgroup-data))
10574         (data (gnus-data-find-list (gnus-summary-article-number)))
10575         found)
10576     ;; This will do all the work of generating the new summary buffer
10577     ;; according to the new limit.
10578     (gnus-summary-prepare)
10579     ;; Hide any threads, possibly.
10580     (and gnus-show-threads
10581          gnus-thread-hide-subtree
10582          (gnus-summary-hide-all-threads))
10583     ;; Try to return to the article you were at, or one in the
10584     ;; neighborhood.
10585     (if data
10586         ;; We try to find some article after the current one.
10587         (while data
10588           (and (gnus-summary-goto-subject
10589                 (gnus-data-number (car data)) nil t)
10590                (setq data nil
10591                      found t))
10592           (setq data (cdr data))))
10593     (or found
10594         ;; If there is no data, that means that we were after the last
10595         ;; article.  The same goes when we can't find any articles
10596         ;; after the current one.
10597         (progn
10598           (goto-char (point-max))
10599           (gnus-summary-find-prev)))
10600     ;; We return how many articles were removed from the summary
10601     ;; buffer as a result of the new limit.
10602     (- total (length gnus-newsgroup-data))))
10603
10604 (defsubst gnus-cut-thread (thread)
10605   "Go forwards in the thread until we find an article that we want to display."
10606   (when (eq gnus-fetch-old-headers 'some)
10607     ;; Deal with old-fetched headers.
10608     (while (and thread
10609                 (memq (mail-header-number (car thread)) 
10610                       gnus-newsgroup-ancient)
10611                 (<= (length (cdr thread)) 1))
10612       (setq thread (cadr thread))))
10613   ;; Deal with sparse threads.
10614   (when (or (eq gnus-build-sparse-threads 'some)
10615             (eq gnus-build-sparse-threads 'more))
10616     (while (and thread
10617                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10618                 (= (length (cdr thread)) 1))
10619       (setq thread (cadr thread))))
10620   thread)
10621
10622 (defun gnus-cut-threads (threads)
10623   "Cut off all uninteresting articles from the beginning of threads."
10624   (when (or (eq gnus-fetch-old-headers 'some)
10625             (eq gnus-build-sparse-threads 'some)
10626             (eq gnus-build-sparse-threads 'more))
10627     (let ((th threads))
10628       (while th
10629         (setcar th (gnus-cut-thread (car th)))
10630         (setq th (cdr th)))))
10631   ;; Remove nixed out threads.
10632   (delq nil threads))
10633
10634 (defun gnus-summary-initial-limit (&optional show-if-empty)
10635   "Figure out what the initial limit is supposed to be on group entry.
10636 This entails weeding out unwanted dormants, low-scored articles,
10637 fetch-old-headers verbiage, and so on."
10638   ;; Most groups have nothing to remove.
10639   (if (or gnus-inhibit-limiting
10640           (and (null gnus-newsgroup-dormant)
10641                (not (eq gnus-fetch-old-headers 'some))
10642                (null gnus-summary-expunge-below)
10643                (not (eq gnus-build-sparse-threads 'some))
10644                (not (eq gnus-build-sparse-threads 'more))
10645                (null gnus-thread-expunge-below)
10646                (not gnus-use-nocem)))
10647       () ; Do nothing.
10648     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10649     (setq gnus-newsgroup-limit nil)
10650     (mapatoms
10651      (lambda (node)
10652        (unless (car (symbol-value node))
10653          ;; These threads have no parents -- they are roots.
10654          (let ((nodes (cdr (symbol-value node)))
10655                thread)
10656            (while nodes
10657              (if (and gnus-thread-expunge-below
10658                       (< (gnus-thread-total-score (car nodes))
10659                          gnus-thread-expunge-below))
10660                  (gnus-expunge-thread (pop nodes))
10661                (setq thread (pop nodes))
10662                (gnus-summary-limit-children thread))))))
10663      gnus-newsgroup-dependencies)
10664     ;; If this limitation resulted in an empty group, we might
10665     ;; pop the previous limit and use it instead.
10666     (when (and (not gnus-newsgroup-limit)
10667                show-if-empty)
10668       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10669     gnus-newsgroup-limit))
10670
10671 (defun gnus-summary-limit-children (thread)
10672   "Return 1 if this subthread is visible and 0 if it is not."
10673   ;; First we get the number of visible children to this thread.  This
10674   ;; is done by recursing down the thread using this function, so this
10675   ;; will really go down to a leaf article first, before slowly
10676   ;; working its way up towards the root.
10677   (when thread
10678     (let ((children
10679            (if (cdr thread)
10680                (apply '+ (mapcar 'gnus-summary-limit-children
10681                                  (cdr thread)))
10682              0))
10683           (number (mail-header-number (car thread)))
10684           score)
10685       (if (or
10686            ;; If this article is dormant and has absolutely no visible
10687            ;; children, then this article isn't visible.
10688            (and (memq number gnus-newsgroup-dormant)
10689                 (= children 0))
10690            ;; If this is a "fetch-old-headered" and there is only one
10691            ;; visible child (or less), then we don't want this article.
10692            (and (eq gnus-fetch-old-headers 'some)
10693                 (memq number gnus-newsgroup-ancient)
10694                 (zerop children))
10695            ;; If this is a sparsely inserted article with no children,
10696            ;; we don't want it.
10697            (and (eq gnus-build-sparse-threads 'some)
10698                 (memq number gnus-newsgroup-sparse)
10699                 (zerop children))
10700            ;; If we use expunging, and this article is really
10701            ;; low-scored, then we don't want this article.
10702            (when (and gnus-summary-expunge-below
10703                       (< (setq score
10704                                (or (cdr (assq number gnus-newsgroup-scored))
10705                                    gnus-summary-default-score))
10706                          gnus-summary-expunge-below))
10707              ;; We increase the expunge-tally here, but that has
10708              ;; nothing to do with the limits, really.
10709              (incf gnus-newsgroup-expunged-tally)
10710              ;; We also mark as read here, if that's wanted.
10711              (when (and gnus-summary-mark-below
10712                         (< score gnus-summary-mark-below))
10713                (setq gnus-newsgroup-unreads
10714                      (delq number gnus-newsgroup-unreads))
10715                (if gnus-newsgroup-auto-expire
10716                    (push number gnus-newsgroup-expirable)
10717                  (push (cons number gnus-low-score-mark)
10718                        gnus-newsgroup-reads)))
10719              t)
10720            (and gnus-use-nocem
10721                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10722           ;; Nope, invisible article.
10723           0
10724         ;; Ok, this article is to be visible, so we add it to the limit
10725         ;; and return 1.
10726         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10727         1))))
10728
10729 (defun gnus-expunge-thread (thread)
10730   "Mark all articles in THREAD as read."
10731   (let* ((number (mail-header-number (car thread))))
10732     (incf gnus-newsgroup-expunged-tally)
10733     ;; We also mark as read here, if that's wanted.
10734     (setq gnus-newsgroup-unreads
10735           (delq number gnus-newsgroup-unreads))
10736     (if gnus-newsgroup-auto-expire
10737         (push number gnus-newsgroup-expirable)
10738       (push (cons number gnus-low-score-mark)
10739             gnus-newsgroup-reads)))
10740   ;; Go recursively through all subthreads.
10741   (mapcar 'gnus-expunge-thread (cdr thread)))
10742
10743 ;; Summary article oriented commands
10744
10745 (defun gnus-summary-refer-parent-article (n)
10746   "Refer parent article N times.
10747 The difference between N and the number of articles fetched is returned."
10748   (interactive "p")
10749   (gnus-set-global-variables)
10750   (while
10751       (and
10752        (> n 0)
10753        (let* ((header (gnus-summary-article-header))
10754               (ref
10755                ;; If we try to find the parent of the currently
10756                ;; displayed article, then we take a look at the actual
10757                ;; References header, since this is slightly more
10758                ;; reliable than the References field we got from the
10759                ;; server.
10760                (if (and (eq (mail-header-number header)
10761                             (cdr gnus-article-current))
10762                         (equal gnus-newsgroup-name
10763                                (car gnus-article-current)))
10764                    (save-excursion
10765                      (set-buffer gnus-original-article-buffer)
10766                      (nnheader-narrow-to-headers)
10767                      (prog1
10768                          (mail-fetch-field "references")
10769                        (widen)))
10770                  ;; It's not the current article, so we take a bet on
10771                  ;; the value we got from the server.
10772                  (mail-header-references header))))
10773          (if (setq ref (or ref (mail-header-references header)))
10774              (or (gnus-summary-refer-article (gnus-parent-id ref))
10775                  (gnus-message 1 "Couldn't find parent"))
10776            (gnus-message 1 "No references in article %d"
10777                          (gnus-summary-article-number))
10778            nil)))
10779     (setq n (1- n)))
10780   (gnus-summary-position-point)
10781   n)
10782
10783 (defun gnus-summary-refer-references ()
10784   "Fetch all articles mentioned in the References header.
10785 Return how many articles were fetched."
10786   (interactive)
10787   (gnus-set-global-variables)
10788   (let ((ref (mail-header-references (gnus-summary-article-header)))
10789         (current (gnus-summary-article-number))
10790         (n 0))
10791     ;; For each Message-ID in the References header...
10792     (while (string-match "<[^>]*>" ref)
10793       (incf n)
10794       ;; ... fetch that article.
10795       (gnus-summary-refer-article
10796        (prog1 (match-string 0 ref)
10797          (setq ref (substring ref (match-end 0))))))
10798     (gnus-summary-goto-subject current)
10799     (gnus-summary-position-point)
10800     n))
10801
10802 (defun gnus-summary-refer-article (message-id)
10803   "Fetch an article specified by MESSAGE-ID."
10804   (interactive "sMessage-ID: ")
10805   (when (and (stringp message-id)
10806              (not (zerop (length message-id))))
10807     ;; Construct the correct Message-ID if necessary.
10808     ;; Suggested by tale@pawl.rpi.edu.
10809     (unless (string-match "^<" message-id)
10810       (setq message-id (concat "<" message-id)))
10811     (unless (string-match ">$" message-id)
10812       (setq message-id (concat message-id ">")))
10813     (let ((header (car (gnus-gethash message-id
10814                                      gnus-newsgroup-dependencies))))
10815       (if header
10816           ;; The article is present in the buffer, to we just go to it.
10817           (gnus-summary-goto-article (mail-header-number header) nil t)
10818         ;; We fetch the article
10819         (let ((gnus-override-method gnus-refer-article-method)
10820               number)
10821           ;; Start the special refer-article method, if necessary.
10822           (when gnus-refer-article-method
10823             (gnus-check-server gnus-refer-article-method))
10824           ;; Fetch the header, and display the article.
10825           (if (setq number (gnus-summary-insert-subject message-id))
10826               (gnus-summary-select-article nil nil nil number)
10827             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10828
10829 (defun gnus-summary-enter-digest-group (&optional force)
10830   "Enter a digest group based on the current article."
10831   (interactive "P")
10832   (gnus-set-global-variables)
10833   (gnus-summary-select-article)
10834   (let ((name (format "%s-%d"
10835                       (gnus-group-prefixed-name
10836                        gnus-newsgroup-name (list 'nndoc ""))
10837                       gnus-current-article))
10838         (ogroup gnus-newsgroup-name)
10839         (case-fold-search t)
10840         (buf (current-buffer))
10841         dig)
10842     (save-excursion
10843       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10844       (insert-buffer-substring gnus-original-article-buffer)
10845       (narrow-to-region
10846        (goto-char (point-min))
10847        (or (search-forward "\n\n" nil t) (point)))
10848       (goto-char (point-min))
10849       (delete-matching-lines "^\\(Path\\):\\|^From ")
10850       (widen))
10851     (unwind-protect
10852         (if (gnus-group-read-ephemeral-group
10853              name `(nndoc ,name (nndoc-address
10854                                  ,(get-buffer dig))
10855                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10856             ;; Make all postings to this group go to the parent group.
10857             (nconc (gnus-info-params (gnus-get-info name))
10858                    (list (cons 'to-group ogroup)))
10859           ;; Couldn't select this doc group.
10860           (switch-to-buffer buf)
10861           (gnus-set-global-variables)
10862           (gnus-configure-windows 'summary)
10863           (gnus-message 3 "Article couldn't be entered?"))
10864       (kill-buffer dig))))
10865
10866 (defun gnus-summary-isearch-article (&optional regexp-p)
10867   "Do incremental search forward on the current article.
10868 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10869   (interactive "P")
10870   (gnus-set-global-variables)
10871   (gnus-summary-select-article)
10872   (gnus-configure-windows 'article)
10873   (gnus-eval-in-buffer-window
10874    gnus-article-buffer
10875    (goto-char (point-min))
10876    (isearch-forward regexp-p)))
10877
10878 (defun gnus-summary-search-article-forward (regexp &optional backward)
10879   "Search for an article containing REGEXP forward.
10880 If BACKWARD, search backward instead."
10881   (interactive
10882    (list (read-string
10883           (format "Search article %s (regexp%s): "
10884                   (if current-prefix-arg "backward" "forward")
10885                   (if gnus-last-search-regexp
10886                       (concat ", default " gnus-last-search-regexp)
10887                     "")))
10888          current-prefix-arg))
10889   (gnus-set-global-variables)
10890   (if (string-equal regexp "")
10891       (setq regexp (or gnus-last-search-regexp ""))
10892     (setq gnus-last-search-regexp regexp))
10893   (if (gnus-summary-search-article regexp backward)
10894       (gnus-article-set-window-start
10895        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10896     (error "Search failed: \"%s\"" regexp)))
10897
10898 (defun gnus-summary-search-article-backward (regexp)
10899   "Search for an article containing REGEXP backward."
10900   (interactive
10901    (list (read-string
10902           (format "Search article backward (regexp%s): "
10903                   (if gnus-last-search-regexp
10904                       (concat ", default " gnus-last-search-regexp)
10905                     "")))))
10906   (gnus-summary-search-article-forward regexp 'backward))
10907
10908 (defun gnus-summary-search-article (regexp &optional backward)
10909   "Search for an article containing REGEXP.
10910 Optional argument BACKWARD means do search for backward.
10911 gnus-select-article-hook is not called during the search."
10912   (let ((gnus-select-article-hook nil)  ;Disable hook.
10913         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10914         (re-search
10915          (if backward
10916              (function re-search-backward) (function re-search-forward)))
10917         (found nil)
10918         (last nil))
10919     ;; Hidden thread subtrees must be searched for ,too.
10920     (gnus-summary-show-all-threads)
10921     ;; First of all, search current article.
10922     ;; We don't want to read article again from NNTP server nor reset
10923     ;; current point.
10924     (gnus-summary-select-article)
10925     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10926     (setq last gnus-current-article)
10927     (gnus-eval-in-buffer-window
10928      gnus-article-buffer
10929      (save-restriction
10930        (widen)
10931        ;; Begin search from current point.
10932        (setq found (funcall re-search regexp nil t))))
10933     ;; Then search next articles.
10934     (while (and (not found)
10935                 (gnus-summary-display-article
10936                  (if backward (gnus-summary-find-prev)
10937                    (gnus-summary-find-next))))
10938       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10939       (gnus-eval-in-buffer-window
10940        gnus-article-buffer
10941        (save-restriction
10942          (widen)
10943          (goto-char (if backward (point-max) (point-min)))
10944          (setq found (funcall re-search regexp nil t)))))
10945     (message "")
10946     ;; Adjust article pointer.
10947     (or (eq last gnus-current-article)
10948         (setq gnus-last-article last))
10949     ;; Return T if found such article.
10950     found))
10951
10952 (defun gnus-summary-find-matching (header regexp &optional backward unread
10953                                           not-case-fold)
10954   "Return a list of all articles that match REGEXP on HEADER.
10955 The search stars on the current article and goes forwards unless
10956 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10957 If UNREAD is non-nil, only unread articles will
10958 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10959 in the comparisons."
10960   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10961                 (gnus-data-find-list
10962                  (gnus-summary-article-number) (gnus-data-list backward))))
10963         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10964         (case-fold-search (not not-case-fold))
10965         articles d)
10966     (or (fboundp (intern (concat "mail-header-" header)))
10967         (error "%s is not a valid header" header))
10968     (while data
10969       (setq d (car data))
10970       (and (or (not unread)             ; We want all articles...
10971                (gnus-data-unread-p d))  ; Or just unreads.
10972            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10973            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10974            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10975       (setq data (cdr data)))
10976     (nreverse articles)))
10977
10978 (defun gnus-summary-execute-command (header regexp command &optional backward)
10979   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10980 If HEADER is an empty string (or nil), the match is done on the entire
10981 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10982   (interactive
10983    (list (let ((completion-ignore-case t))
10984            (completing-read
10985             "Header name: "
10986             (mapcar (lambda (string) (list string))
10987                     '("Number" "Subject" "From" "Lines" "Date"
10988                       "Message-ID" "Xref" "References" "Body"))
10989             nil 'require-match))
10990          (read-string "Regexp: ")
10991          (read-key-sequence "Command: ")
10992          current-prefix-arg))
10993   (when (equal header "Body")
10994     (setq header ""))
10995   (gnus-set-global-variables)
10996   ;; Hidden thread subtrees must be searched as well.
10997   (gnus-summary-show-all-threads)
10998   ;; We don't want to change current point nor window configuration.
10999   (save-excursion
11000     (save-window-excursion
11001       (gnus-message 6 "Executing %s..." (key-description command))
11002       ;; We'd like to execute COMMAND interactively so as to give arguments.
11003       (gnus-execute header regexp
11004                     `(lambda () (call-interactively ',(key-binding command)))
11005                     backward)
11006       (gnus-message 6 "Executing %s...done" (key-description command)))))
11007
11008 (defun gnus-summary-beginning-of-article ()
11009   "Scroll the article back to the beginning."
11010   (interactive)
11011   (gnus-set-global-variables)
11012   (gnus-summary-select-article)
11013   (gnus-configure-windows 'article)
11014   (gnus-eval-in-buffer-window
11015    gnus-article-buffer
11016    (widen)
11017    (goto-char (point-min))
11018    (and gnus-break-pages (gnus-narrow-to-page))))
11019
11020 (defun gnus-summary-end-of-article ()
11021   "Scroll to the end of the article."
11022   (interactive)
11023   (gnus-set-global-variables)
11024   (gnus-summary-select-article)
11025   (gnus-configure-windows 'article)
11026   (gnus-eval-in-buffer-window
11027    gnus-article-buffer
11028    (widen)
11029    (goto-char (point-max))
11030    (recenter -3)
11031    (and gnus-break-pages (gnus-narrow-to-page))))
11032
11033 (defun gnus-summary-show-article (&optional arg)
11034   "Force re-fetching of the current article.
11035 If ARG (the prefix) is non-nil, show the raw article without any
11036 article massaging functions being run."
11037   (interactive "P")
11038   (gnus-set-global-variables)
11039   (if (not arg)
11040       ;; Select the article the normal way.
11041       (gnus-summary-select-article nil 'force)
11042     ;; Bind the article treatment functions to nil.
11043     (let ((gnus-have-all-headers t)
11044           gnus-article-display-hook
11045           gnus-article-prepare-hook
11046           gnus-visual)
11047       (gnus-summary-select-article nil 'force)))
11048 ;  (gnus-configure-windows 'article)
11049   (gnus-summary-position-point))
11050
11051 (defun gnus-summary-verbose-headers (&optional arg)
11052   "Toggle permanent full header display.
11053 If ARG is a positive number, turn header display on.
11054 If ARG is a negative number, turn header display off."
11055   (interactive "P")
11056   (gnus-set-global-variables)
11057   (gnus-summary-toggle-header arg)
11058   (setq gnus-show-all-headers
11059         (cond ((or (not (numberp arg))
11060                    (zerop arg))
11061                (not gnus-show-all-headers))
11062               ((natnump arg)
11063                t))))
11064
11065 (defun gnus-summary-toggle-header (&optional arg)
11066   "Show the headers if they are hidden, or hide them if they are shown.
11067 If ARG is a positive number, show the entire header.
11068 If ARG is a negative number, hide the unwanted header lines."
11069   (interactive "P")
11070   (gnus-set-global-variables)
11071   (save-excursion
11072     (set-buffer gnus-article-buffer)
11073     (let* ((buffer-read-only nil)
11074            (inhibit-point-motion-hooks t)
11075            (hidden (text-property-any
11076                     (goto-char (point-min)) (search-forward "\n\n")
11077                     'invisible t))
11078            e)
11079       (goto-char (point-min))
11080       (when (search-forward "\n\n" nil t)
11081         (delete-region (point-min) (1- (point))))
11082       (goto-char (point-min))
11083       (save-excursion
11084         (set-buffer gnus-original-article-buffer)
11085         (goto-char (point-min))
11086         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11087       (insert-buffer-substring gnus-original-article-buffer 1 e)
11088       (let ((gnus-inhibit-hiding t))
11089         (run-hooks 'gnus-article-display-hook))
11090       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11091           (gnus-article-hide-headers)))))
11092
11093 (defun gnus-summary-show-all-headers ()
11094   "Make all header lines visible."
11095   (interactive)
11096   (gnus-set-global-variables)
11097   (gnus-article-show-all-headers))
11098
11099 (defun gnus-summary-toggle-mime (&optional arg)
11100   "Toggle MIME processing.
11101 If ARG is a positive number, turn MIME processing on."
11102   (interactive "P")
11103   (gnus-set-global-variables)
11104   (setq gnus-show-mime
11105         (if (null arg) (not gnus-show-mime)
11106           (> (prefix-numeric-value arg) 0)))
11107   (gnus-summary-select-article t 'force))
11108
11109 (defun gnus-summary-caesar-message (&optional arg)
11110   "Caesar rotate the current article by 13.
11111 The numerical prefix specifies how manu places to rotate each letter
11112 forward."
11113   (interactive "P")
11114   (gnus-set-global-variables)
11115   (gnus-summary-select-article)
11116   (let ((mail-header-separator ""))
11117     (gnus-eval-in-buffer-window
11118      gnus-article-buffer
11119      (save-restriction
11120        (widen)
11121        (let ((start (window-start)))
11122          (news-caesar-buffer-body arg)
11123          (set-window-start (get-buffer-window (current-buffer)) start))))))
11124
11125 (defun gnus-summary-stop-page-breaking ()
11126   "Stop page breaking in the current article."
11127   (interactive)
11128   (gnus-set-global-variables)
11129   (gnus-summary-select-article)
11130   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11131
11132 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11133   "Move the current article to a different newsgroup.
11134 If N is a positive number, move the N next articles.
11135 If N is a negative number, move the N previous articles.
11136 If N is nil and any articles have been marked with the process mark,
11137 move those articles instead.
11138 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11139 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11140 re-spool using this method.
11141
11142 For this function to work, both the current newsgroup and the
11143 newsgroup that you want to move to have to support the `request-move'
11144 and `request-accept' functions."
11145   (interactive "P")
11146   (unless action (setq action 'move))
11147   (gnus-set-global-variables)
11148   ;; Check whether the source group supports the required functions.
11149   (cond ((and (eq action 'move)
11150               (not (gnus-check-backend-function
11151                     'request-move-article gnus-newsgroup-name)))
11152          (error "The current group does not support article moving"))
11153         ((and (eq action 'crosspost)
11154               (not (gnus-check-backend-function
11155                     'request-replace-article gnus-newsgroup-name)))
11156          (error "The current group does not support article editing")))
11157   (let ((articles (gnus-summary-work-articles n))
11158         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11159         (names '((move "move" "Moving")
11160                  (copy "copy" "Copying")
11161                  (crosspost "crosspost" "Crossposting")))
11162         (copy-buf (save-excursion
11163                     (nnheader-set-temp-buffer " *copy article*")))
11164         art-group to-method new-xref article to-groups)
11165     (unless (assq action names)
11166       (error "Unknown action %s" action))
11167     ;; Read the newsgroup name.
11168     (when (and (not to-newsgroup)
11169                (not select-method))
11170       (setq to-newsgroup
11171             (gnus-read-move-group-name
11172              (cadr (assq action names))
11173              gnus-current-move-group articles prefix))
11174       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11175     (setq to-method (if select-method (list select-method "")
11176                       (gnus-find-method-for-group to-newsgroup)))
11177     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11178     ;;(error "Can't %s to the same group you're already in" action))
11179     ;; Check the method we are to move this article to...
11180     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11181         (error "%s does not support article copying" (car to-method)))
11182     (or (gnus-check-server to-method)
11183         (error "Can't open server %s" (car to-method)))
11184     (gnus-message 6 "%s to %s: %s..."
11185                   (caddr (assq action names))
11186                   (or select-method to-newsgroup) articles)
11187     (while articles
11188       (setq article (pop articles))
11189       (setq
11190        art-group
11191        (cond
11192         ;; Move the article.
11193         ((eq action 'move)
11194          (gnus-request-move-article
11195           article                       ; Article to move
11196           gnus-newsgroup-name           ; From newsgrouo
11197           (nth 1 (gnus-find-method-for-group
11198                   gnus-newsgroup-name)) ; Server
11199           (list 'gnus-request-accept-article
11200                 (if select-method
11201                     (list 'quote select-method)
11202                   to-newsgroup)
11203                 (not articles))         ; Accept form
11204           (not articles)))              ; Only save nov last time
11205         ;; Copy the article.
11206         ((eq action 'copy)
11207          (save-excursion
11208            (set-buffer copy-buf)
11209            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11210            (gnus-request-accept-article
11211             (if select-method select-method to-newsgroup)
11212             (not articles))))
11213         ;; Crosspost the article.
11214         ((eq action 'crosspost)
11215          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11216            (setq new-xref (concat gnus-newsgroup-name ":" article))
11217            (if (and xref (not (string= xref "")))
11218                (progn
11219                  (when (string-match "^Xref: " xref)
11220                    (setq xref (substring xref (match-end 0))))
11221                  (setq new-xref (concat xref " " new-xref)))
11222              (setq new-xref (concat (system-name) " " new-xref)))
11223            (save-excursion
11224              (set-buffer copy-buf)
11225              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11226              (nnheader-replace-header "xref" new-xref)
11227              (gnus-request-accept-article
11228               (if select-method select-method to-newsgroup)
11229               (not articles)))))))
11230       (if (not art-group)
11231           (gnus-message 1 "Couldn't %s article %s"
11232                         (cadr (assq action names)) article)
11233         (let* ((entry
11234                 (or
11235                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11236                  (gnus-gethash
11237                   (gnus-group-prefixed-name
11238                    (car art-group)
11239                    (if select-method (list select-method "")
11240                      (gnus-find-method-for-group to-newsgroup)))
11241                   gnus-newsrc-hashtb)))
11242                (info (nth 2 entry))
11243                (to-group (gnus-info-group info)))
11244           ;; Update the group that has been moved to.
11245           (when (and info
11246                      (memq action '(move copy)))
11247             (unless (member to-group to-groups)
11248               (push to-group to-groups))
11249
11250             (unless (memq article gnus-newsgroup-unreads)
11251               (gnus-info-set-read
11252                info (gnus-add-to-range (gnus-info-read info)
11253                                        (list (cdr art-group)))))
11254
11255             ;; Copy any marks over to the new group.
11256             (let ((marks gnus-article-mark-lists)
11257                   (to-article (cdr art-group)))
11258
11259               ;; See whether the article is to be put in the cache.
11260               (when gnus-use-cache
11261                 (gnus-cache-possibly-enter-article
11262                  to-group to-article
11263                  (let ((header (copy-sequence
11264                                 (gnus-summary-article-header article))))
11265                    (mail-header-set-number header to-article)
11266                    header)
11267                  (memq article gnus-newsgroup-marked)
11268                  (memq article gnus-newsgroup-dormant)
11269                  (memq article gnus-newsgroup-unreads)))
11270
11271               (while marks
11272                 (when (memq article (symbol-value
11273                                      (intern (format "gnus-newsgroup-%s"
11274                                                      (caar marks)))))
11275                   ;; If the other group is the same as this group,
11276                   ;; then we have to add the mark to the list.
11277                   (when (equal to-group gnus-newsgroup-name)
11278                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11279                          (cons to-article
11280                                (symbol-value
11281                                 (intern (format "gnus-newsgroup-%s"
11282                                                 (caar marks)))))))
11283                   ;; Copy mark to other group.
11284                   (gnus-add-marked-articles
11285                    to-group (cdar marks) (list to-article) info))
11286                 (setq marks (cdr marks)))))
11287
11288           ;; Update the Xref header in this article to point to
11289           ;; the new crossposted article we have just created.
11290           (when (eq action 'crosspost)
11291             (save-excursion
11292               (set-buffer copy-buf)
11293               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11294               (nnheader-replace-header
11295                "xref" (concat new-xref " " (gnus-group-prefixed-name
11296                                             (car art-group) to-method)
11297                               ":" (cdr art-group)))
11298               (gnus-request-replace-article
11299                article gnus-newsgroup-name (current-buffer)))))
11300
11301         (gnus-summary-goto-subject article)
11302         (when (eq action 'move)
11303           (gnus-summary-mark-article article gnus-canceled-mark)))
11304       (gnus-summary-remove-process-mark article))
11305     ;; Re-activate all groups that have been moved to.
11306     (while to-groups
11307       (gnus-activate-group (pop to-groups)))
11308     
11309     (gnus-kill-buffer copy-buf)
11310     (gnus-summary-position-point)
11311     (gnus-set-mode-line 'summary)))
11312
11313 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11314   "Move the current article to a different newsgroup.
11315 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11316 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11317 re-spool using this method."
11318   (interactive "P")
11319   (gnus-summary-move-article n nil select-method 'copy))
11320
11321 (defun gnus-summary-crosspost-article (&optional n)
11322   "Crosspost the current article to some other group."
11323   (interactive "P")
11324   (gnus-summary-move-article n nil nil 'crosspost))
11325
11326 (defun gnus-summary-respool-article (&optional n respool-method)
11327   "Respool the current article.
11328 The article will be squeezed through the mail spooling process again,
11329 which means that it will be put in some mail newsgroup or other
11330 depending on `nnmail-split-methods'.
11331 If N is a positive number, respool the N next articles.
11332 If N is a negative number, respool the N previous articles.
11333 If N is nil and any articles have been marked with the process mark,
11334 respool those articles instead.
11335
11336 Respooling can be done both from mail groups and \"real\" newsgroups.
11337 In the former case, the articles in question will be moved from the
11338 current group into whatever groups they are destined to.  In the
11339 latter case, they will be copied into the relevant groups."
11340   (interactive "P")
11341   (gnus-set-global-variables)
11342   (let ((respool-methods (gnus-methods-using 'respool))
11343         (methname
11344          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11345     (unless respool-method
11346       (setq respool-method
11347             (completing-read
11348              "What method do you want to use when respooling? "
11349              respool-methods nil t (cons methname 0))))
11350     (unless (string= respool-method "")
11351       (if (assoc (symbol-name
11352                   (car (gnus-find-method-for-group gnus-newsgroup-name)))
11353                  respool-methods)
11354           (gnus-summary-move-article n nil (intern respool-method))
11355         (gnus-summary-copy-article n nil (intern respool-method))))))
11356
11357 (defun gnus-summary-import-article (file)
11358   "Import a random file into a mail newsgroup."
11359   (interactive "fImport file: ")
11360   (gnus-set-global-variables)
11361   (let ((group gnus-newsgroup-name)
11362         (now (current-time))
11363         atts lines)
11364     (or (gnus-check-backend-function 'request-accept-article group)
11365         (error "%s does not support article importing" group))
11366     (or (file-readable-p file)
11367         (not (file-regular-p file))
11368         (error "Can't read %s" file))
11369     (save-excursion
11370       (set-buffer (get-buffer-create " *import file*"))
11371       (buffer-disable-undo (current-buffer))
11372       (erase-buffer)
11373       (insert-file-contents file)
11374       (goto-char (point-min))
11375       (unless (nnheader-article-p)
11376         ;; This doesn't look like an article, so we fudge some headers.
11377         (setq atts (file-attributes file)
11378               lines (count-lines (point-min) (point-max)))
11379         (insert "From: " (read-string "From: ") "\n"
11380                 "Subject: " (read-string "Subject: ") "\n"
11381                 "Date: " (timezone-make-date-arpa-standard
11382                           (current-time-string (nth 5 atts))
11383                           (current-time-zone now)
11384                           (current-time-zone now)) "\n"
11385                 "Message-ID: " (gnus-inews-message-id) "\n"
11386                 "Lines: " (int-to-string lines) "\n"
11387                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11388       (gnus-request-accept-article group t)
11389       (kill-buffer (current-buffer)))))
11390
11391 (defun gnus-summary-expire-articles ()
11392   "Expire all articles that are marked as expirable in the current group."
11393   (interactive)
11394   (gnus-set-global-variables)
11395   (when (gnus-check-backend-function
11396          'request-expire-articles gnus-newsgroup-name)
11397     ;; This backend supports expiry.
11398     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11399            (expirable (if total
11400                           (gnus-list-of-read-articles gnus-newsgroup-name)
11401                         (setq gnus-newsgroup-expirable
11402                               (sort gnus-newsgroup-expirable '<))))
11403            (expiry-wait (gnus-group-get-parameter
11404                          gnus-newsgroup-name 'expiry-wait))
11405            es)
11406       (when expirable
11407         ;; There are expirable articles in this group, so we run them
11408         ;; through the expiry process.
11409         (gnus-message 6 "Expiring articles...")
11410         ;; The list of articles that weren't expired is returned.
11411         (if expiry-wait
11412             (let ((nnmail-expiry-wait-function nil)
11413                   (nnmail-expiry-wait expiry-wait))
11414               (setq es (gnus-request-expire-articles
11415                         expirable gnus-newsgroup-name)))
11416           (setq es (gnus-request-expire-articles
11417                     expirable gnus-newsgroup-name)))
11418         (or total (setq gnus-newsgroup-expirable es))
11419         ;; We go through the old list of expirable, and mark all
11420         ;; really expired articles as nonexistent.
11421         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11422           (let ((gnus-use-cache nil))
11423             (while expirable
11424               (unless (memq (car expirable) es)
11425                 (when (gnus-data-find (car expirable))
11426                   (gnus-summary-mark-article
11427                    (car expirable) gnus-canceled-mark)))
11428               (setq expirable (cdr expirable)))))
11429         (gnus-message 6 "Expiring articles...done")))))
11430
11431 (defun gnus-summary-expire-articles-now ()
11432   "Expunge all expirable articles in the current group.
11433 This means that *all* articles that are marked as expirable will be
11434 deleted forever, right now."
11435   (interactive)
11436   (gnus-set-global-variables)
11437   (or gnus-expert-user
11438       (gnus-y-or-n-p
11439        "Are you really, really, really sure you want to expunge? ")
11440       (error "Phew!"))
11441   (let ((nnmail-expiry-wait 'immediate)
11442         (nnmail-expiry-wait-function nil))
11443     (gnus-summary-expire-articles)))
11444
11445 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11446 (defun gnus-summary-delete-article (&optional n)
11447   "Delete the N next (mail) articles.
11448 This command actually deletes articles.  This is not a marking
11449 command.  The article will disappear forever from your life, never to
11450 return.
11451 If N is negative, delete backwards.
11452 If N is nil and articles have been marked with the process mark,
11453 delete these instead."
11454   (interactive "P")
11455   (gnus-set-global-variables)
11456   (or (gnus-check-backend-function 'request-expire-articles
11457                                    gnus-newsgroup-name)
11458       (error "The current newsgroup does not support article deletion."))
11459   ;; Compute the list of articles to delete.
11460   (let ((articles (gnus-summary-work-articles n))
11461         not-deleted)
11462     (if (and gnus-novice-user
11463              (not (gnus-y-or-n-p
11464                    (format "Do you really want to delete %s forever? "
11465                            (if (> (length articles) 1) "these articles"
11466                              "this article")))))
11467         ()
11468       ;; Delete the articles.
11469       (setq not-deleted (gnus-request-expire-articles
11470                          articles gnus-newsgroup-name 'force))
11471       (while articles
11472         (gnus-summary-remove-process-mark (car articles))
11473         ;; The backend might not have been able to delete the article
11474         ;; after all.
11475         (or (memq (car articles) not-deleted)
11476             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11477         (setq articles (cdr articles))))
11478     (gnus-summary-position-point)
11479     (gnus-set-mode-line 'summary)
11480     not-deleted))
11481
11482 (defun gnus-summary-edit-article (&optional force)
11483   "Enter into a buffer and edit the current article.
11484 This will have permanent effect only in mail groups.
11485 If FORCE is non-nil, allow editing of articles even in read-only
11486 groups."
11487   (interactive "P")
11488   (save-excursion
11489     (set-buffer gnus-summary-buffer)
11490     (gnus-set-global-variables)
11491     (when (and (not force)
11492                (gnus-group-read-only-p))
11493       (error "The current newsgroup does not support article editing."))
11494     (gnus-summary-select-article t nil t)
11495     (gnus-configure-windows 'article)
11496     (select-window (get-buffer-window gnus-article-buffer))
11497     (gnus-message 6 "C-c C-c to end edits")
11498     (setq buffer-read-only nil)
11499     (text-mode)
11500     (use-local-map (copy-keymap (current-local-map)))
11501     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11502     (buffer-enable-undo)
11503     (widen)
11504     (goto-char (point-min))
11505     (search-forward "\n\n" nil t)))
11506
11507 (defun gnus-summary-edit-article-done ()
11508   "Make edits to the current article permanent."
11509   (interactive)
11510   (if (gnus-group-read-only-p)
11511       (progn
11512         (gnus-summary-edit-article-postpone)
11513         (gnus-message
11514          1 "The current newsgroup does not support article editing.")
11515         (ding))
11516     (let ((buf (format "%s" (buffer-string))))
11517       (erase-buffer)
11518       (insert buf)
11519       (if (not (gnus-request-replace-article
11520                 (cdr gnus-article-current) (car gnus-article-current)
11521                 (current-buffer)))
11522           (error "Couldn't replace article.")
11523         (gnus-article-mode)
11524         (use-local-map gnus-article-mode-map)
11525         (setq buffer-read-only t)
11526         (buffer-disable-undo (current-buffer))
11527         (gnus-configure-windows 'summary)
11528         (gnus-summary-update-article (cdr gnus-article-current))
11529         (when gnus-use-cache
11530           (gnus-cache-update-article 
11531            (cdr gnus-article-current) (car gnus-article-current))))
11532       (run-hooks 'gnus-article-display-hook)
11533       (and (gnus-visual-p 'summary-highlight 'highlight)
11534            (run-hooks 'gnus-visual-mark-article-hook)))))
11535
11536 (defun gnus-summary-edit-article-postpone ()
11537   "Postpone changes to the current article."
11538   (interactive)
11539   (gnus-article-mode)
11540   (use-local-map gnus-article-mode-map)
11541   (setq buffer-read-only t)
11542   (buffer-disable-undo (current-buffer))
11543   (gnus-configure-windows 'summary)
11544   (and (gnus-visual-p 'summary-highlight 'highlight)
11545        (run-hooks 'gnus-visual-mark-article-hook)))
11546
11547 (defun gnus-summary-respool-query ()
11548   "Query where the respool algorithm would put this article."
11549   (interactive)
11550   (gnus-set-global-variables)
11551   (gnus-summary-select-article)
11552   (save-excursion
11553     (set-buffer gnus-article-buffer)
11554     (save-restriction
11555       (goto-char (point-min))
11556       (search-forward "\n\n")
11557       (narrow-to-region (point-min) (point))
11558       (pp-eval-expression
11559        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11560
11561 ;; Summary score commands.
11562
11563 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11564
11565 (defun gnus-summary-raise-score (n)
11566   "Raise the score of the current article by N."
11567   (interactive "p")
11568   (gnus-set-global-variables)
11569   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11570
11571 (defun gnus-summary-set-score (n)
11572   "Set the score of the current article to N."
11573   (interactive "p")
11574   (gnus-set-global-variables)
11575   (save-excursion
11576     (gnus-summary-show-thread)
11577     (let ((buffer-read-only nil))
11578       ;; Set score.
11579       (gnus-summary-update-mark
11580        (if (= n (or gnus-summary-default-score 0)) ? 
11581          (if (< n (or gnus-summary-default-score 0))
11582              gnus-score-below-mark gnus-score-over-mark)) 'score))
11583     (let* ((article (gnus-summary-article-number))
11584            (score (assq article gnus-newsgroup-scored)))
11585       (if score (setcdr score n)
11586         (setq gnus-newsgroup-scored
11587               (cons (cons article n) gnus-newsgroup-scored))))
11588     (gnus-summary-update-line)))
11589
11590 (defun gnus-summary-current-score ()
11591   "Return the score of the current article."
11592   (interactive)
11593   (gnus-set-global-variables)
11594   (message "%s" (gnus-summary-article-score)))
11595
11596 ;; Summary marking commands.
11597
11598 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11599   "Mark articles which has the same subject as read, and then select the next.
11600 If UNMARK is positive, remove any kind of mark.
11601 If UNMARK is negative, tick articles."
11602   (interactive "P")
11603   (gnus-set-global-variables)
11604   (if unmark
11605       (setq unmark (prefix-numeric-value unmark)))
11606   (let ((count
11607          (gnus-summary-mark-same-subject
11608           (gnus-summary-article-subject) unmark)))
11609     ;; Select next unread article.  If auto-select-same mode, should
11610     ;; select the first unread article.
11611     (gnus-summary-next-article t (and gnus-auto-select-same
11612                                       (gnus-summary-article-subject)))
11613     (gnus-message 7 "%d article%s marked as %s"
11614                   count (if (= count 1) " is" "s are")
11615                   (if unmark "unread" "read"))))
11616
11617 (defun gnus-summary-kill-same-subject (&optional unmark)
11618   "Mark articles which has the same subject as read.
11619 If UNMARK is positive, remove any kind of mark.
11620 If UNMARK is negative, tick articles."
11621   (interactive "P")
11622   (gnus-set-global-variables)
11623   (if unmark
11624       (setq unmark (prefix-numeric-value unmark)))
11625   (let ((count
11626          (gnus-summary-mark-same-subject
11627           (gnus-summary-article-subject) unmark)))
11628     ;; If marked as read, go to next unread subject.
11629     (if (null unmark)
11630         ;; Go to next unread subject.
11631         (gnus-summary-next-subject 1 t))
11632     (gnus-message 7 "%d articles are marked as %s"
11633                   count (if unmark "unread" "read"))))
11634
11635 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11636   "Mark articles with same SUBJECT as read, and return marked number.
11637 If optional argument UNMARK is positive, remove any kinds of marks.
11638 If optional argument UNMARK is negative, mark articles as unread instead."
11639   (let ((count 1))
11640     (save-excursion
11641       (cond
11642        ((null unmark)                   ; Mark as read.
11643         (while (and
11644                 (progn
11645                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11646                   (gnus-summary-show-thread) t)
11647                 (gnus-summary-find-subject subject))
11648           (setq count (1+ count))))
11649        ((> unmark 0)                    ; Tick.
11650         (while (and
11651                 (progn
11652                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11653                   (gnus-summary-show-thread) t)
11654                 (gnus-summary-find-subject subject))
11655           (setq count (1+ count))))
11656        (t                               ; Mark as unread.
11657         (while (and
11658                 (progn
11659                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11660                   (gnus-summary-show-thread) t)
11661                 (gnus-summary-find-subject subject))
11662           (setq count (1+ count)))))
11663       (gnus-set-mode-line 'summary)
11664       ;; Return the number of marked articles.
11665       count)))
11666
11667 (defun gnus-summary-mark-as-processable (n &optional unmark)
11668   "Set the process mark on the next N articles.
11669 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11670 the process mark instead.  The difference between N and the actual
11671 number of articles marked is returned."
11672   (interactive "p")
11673   (gnus-set-global-variables)
11674   (let ((backward (< n 0))
11675         (n (abs n)))
11676     (while (and
11677             (> n 0)
11678             (if unmark
11679                 (gnus-summary-remove-process-mark
11680                  (gnus-summary-article-number))
11681               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11682             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11683       (setq n (1- n)))
11684     (if (/= 0 n) (gnus-message 7 "No more articles"))
11685     (gnus-summary-recenter)
11686     (gnus-summary-position-point)
11687     n))
11688
11689 (defun gnus-summary-unmark-as-processable (n)
11690   "Remove the process mark from the next N articles.
11691 If N is negative, mark backward instead.  The difference between N and
11692 the actual number of articles marked is returned."
11693   (interactive "p")
11694   (gnus-set-global-variables)
11695   (gnus-summary-mark-as-processable n t))
11696
11697 (defun gnus-summary-unmark-all-processable ()
11698   "Remove the process mark from all articles."
11699   (interactive)
11700   (gnus-set-global-variables)
11701   (save-excursion
11702     (while gnus-newsgroup-processable
11703       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11704   (gnus-summary-position-point))
11705
11706 (defun gnus-summary-mark-as-expirable (n)
11707   "Mark N articles forward as expirable.
11708 If N is negative, mark backward instead.  The difference between N and
11709 the actual number of articles marked is returned."
11710   (interactive "p")
11711   (gnus-set-global-variables)
11712   (gnus-summary-mark-forward n gnus-expirable-mark))
11713
11714 (defun gnus-summary-mark-article-as-replied (article)
11715   "Mark ARTICLE replied and update the summary line."
11716   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11717   (let ((buffer-read-only nil))
11718     (when (gnus-summary-goto-subject article)
11719       (gnus-summary-update-secondary-mark article))))
11720
11721 (defun gnus-summary-set-bookmark (article)
11722   "Set a bookmark in current article."
11723   (interactive (list (gnus-summary-article-number)))
11724   (gnus-set-global-variables)
11725   (if (or (not (get-buffer gnus-article-buffer))
11726           (not gnus-current-article)
11727           (not gnus-article-current)
11728           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11729       (error "No current article selected"))
11730   ;; Remove old bookmark, if one exists.
11731   (let ((old (assq article gnus-newsgroup-bookmarks)))
11732     (if old (setq gnus-newsgroup-bookmarks
11733                   (delq old gnus-newsgroup-bookmarks))))
11734   ;; Set the new bookmark, which is on the form
11735   ;; (article-number . line-number-in-body).
11736   (setq gnus-newsgroup-bookmarks
11737         (cons
11738          (cons article
11739                (save-excursion
11740                  (set-buffer gnus-article-buffer)
11741                  (count-lines
11742                   (min (point)
11743                        (save-excursion
11744                          (goto-char (point-min))
11745                          (search-forward "\n\n" nil t)
11746                          (point)))
11747                   (point))))
11748          gnus-newsgroup-bookmarks))
11749   (gnus-message 6 "A bookmark has been added to the current article."))
11750
11751 (defun gnus-summary-remove-bookmark (article)
11752   "Remove the bookmark from the current article."
11753   (interactive (list (gnus-summary-article-number)))
11754   (gnus-set-global-variables)
11755   ;; Remove old bookmark, if one exists.
11756   (let ((old (assq article gnus-newsgroup-bookmarks)))
11757     (if old
11758         (progn
11759           (setq gnus-newsgroup-bookmarks
11760                 (delq old gnus-newsgroup-bookmarks))
11761           (gnus-message 6 "Removed bookmark."))
11762       (gnus-message 6 "No bookmark in current article."))))
11763
11764 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11765 (defun gnus-summary-mark-as-dormant (n)
11766   "Mark N articles forward as dormant.
11767 If N is negative, mark backward instead.  The difference between N and
11768 the actual number of articles marked is returned."
11769   (interactive "p")
11770   (gnus-set-global-variables)
11771   (gnus-summary-mark-forward n gnus-dormant-mark))
11772
11773 (defun gnus-summary-set-process-mark (article)
11774   "Set the process mark on ARTICLE and update the summary line."
11775   (setq gnus-newsgroup-processable
11776         (cons article
11777               (delq article gnus-newsgroup-processable)))
11778   (when (gnus-summary-goto-subject article)
11779     (gnus-summary-show-thread)
11780     (gnus-summary-update-secondary-mark article)))
11781
11782 (defun gnus-summary-remove-process-mark (article)
11783   "Remove the process mark from ARTICLE and update the summary line."
11784   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11785   (when (gnus-summary-goto-subject article)
11786     (gnus-summary-show-thread)
11787     (gnus-summary-update-secondary-mark article)))
11788
11789 (defun gnus-summary-set-saved-mark (article)
11790   "Set the process mark on ARTICLE and update the summary line."
11791   (push article gnus-newsgroup-saved)
11792   (when (gnus-summary-goto-subject article)
11793     (gnus-summary-update-secondary-mark article)))
11794
11795 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11796   "Mark N articles as read forwards.
11797 If N is negative, mark backwards instead.
11798 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11799 marked as unread.
11800 The difference between N and the actual number of articles marked is
11801 returned."
11802   (interactive "p")
11803   (gnus-set-global-variables)
11804   (let ((backward (< n 0))
11805         (gnus-summary-goto-unread
11806          (and gnus-summary-goto-unread
11807               (not (eq gnus-summary-goto-unread 'never))
11808               (not (memq mark (list gnus-unread-mark
11809                                     gnus-ticked-mark gnus-dormant-mark)))))
11810         (n (abs n))
11811         (mark (or mark gnus-del-mark)))
11812     (while (and (> n 0)
11813                 (gnus-summary-mark-article nil mark no-expire)
11814                 (zerop (gnus-summary-next-subject
11815                         (if backward -1 1)
11816                         (and gnus-summary-goto-unread
11817                              (not (eq gnus-summary-goto-unread 'never)))
11818                         t)))
11819       (setq n (1- n)))
11820     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11821     (gnus-summary-recenter)
11822     (gnus-summary-position-point)
11823     (gnus-set-mode-line 'summary)
11824     n))
11825
11826 (defun gnus-summary-mark-article-as-read (mark)
11827   "Mark the current article quickly as read with MARK."
11828   (let ((article (gnus-summary-article-number)))
11829     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11830     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11831     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11832     (setq gnus-newsgroup-reads
11833           (cons (cons article mark) gnus-newsgroup-reads))
11834     ;; Possibly remove from cache, if that is used.
11835     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11836     ;; Allow the backend to change the mark.
11837     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11838     ;; Check for auto-expiry.
11839     (when (and gnus-newsgroup-auto-expire
11840                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11841                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11842                    (= mark gnus-ancient-mark)
11843                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11844       (setq mark gnus-expirable-mark)
11845       (push article gnus-newsgroup-expirable))
11846     ;; Set the mark in the buffer.
11847     (gnus-summary-update-mark mark 'unread)
11848     t))
11849
11850 (defun gnus-summary-mark-article-as-unread (mark)
11851   "Mark the current article quickly as unread with MARK."
11852   (let ((article (gnus-summary-article-number)))
11853     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11854     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11855     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11856     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11857     (cond ((= mark gnus-ticked-mark)
11858            (push article gnus-newsgroup-marked))
11859           ((= mark gnus-dormant-mark)
11860            (push article gnus-newsgroup-dormant))
11861           (t
11862            (push article gnus-newsgroup-unreads)))
11863     (setq gnus-newsgroup-reads
11864           (delq (assq article gnus-newsgroup-reads)
11865                 gnus-newsgroup-reads))
11866
11867     ;; See whether the article is to be put in the cache.
11868     (and gnus-use-cache
11869          (vectorp (gnus-summary-article-header article))
11870          (save-excursion
11871            (gnus-cache-possibly-enter-article
11872             gnus-newsgroup-name article
11873             (gnus-summary-article-header article)
11874             (= mark gnus-ticked-mark)
11875             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11876
11877     ;; Fix the mark.
11878     (gnus-summary-update-mark mark 'unread)
11879     t))
11880
11881 (defun gnus-summary-mark-article (&optional article mark no-expire)
11882   "Mark ARTICLE with MARK.  MARK can be any character.
11883 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11884 `??' (dormant) and `?E' (expirable).
11885 If MARK is nil, then the default character `?D' is used.
11886 If ARTICLE is nil, then the article on the current line will be
11887 marked."
11888   ;; The mark might be a string.
11889   (and (stringp mark)
11890        (setq mark (aref mark 0)))
11891   ;; If no mark is given, then we check auto-expiring.
11892   (and (not no-expire)
11893        gnus-newsgroup-auto-expire
11894        (or (not mark)
11895            (and (numberp mark)
11896                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11897                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11898                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11899        (setq mark gnus-expirable-mark))
11900   (let* ((mark (or mark gnus-del-mark))
11901          (article (or article (gnus-summary-article-number))))
11902     (or article (error "No article on current line"))
11903     (if (or (= mark gnus-unread-mark)
11904             (= mark gnus-ticked-mark)
11905             (= mark gnus-dormant-mark))
11906         (gnus-mark-article-as-unread article mark)
11907       (gnus-mark-article-as-read article mark))
11908
11909     ;; See whether the article is to be put in the cache.
11910     (and gnus-use-cache
11911          (not (= mark gnus-canceled-mark))
11912          (vectorp (gnus-summary-article-header article))
11913          (save-excursion
11914            (gnus-cache-possibly-enter-article
11915             gnus-newsgroup-name article
11916             (gnus-summary-article-header article)
11917             (= mark gnus-ticked-mark)
11918             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11919
11920     (if (gnus-summary-goto-subject article nil t)
11921         (let ((buffer-read-only nil))
11922           (gnus-summary-show-thread)
11923           ;; Fix the mark.
11924           (gnus-summary-update-mark mark 'unread)
11925           t))))
11926
11927 (defun gnus-summary-update-secondary-mark (article)
11928   "Update the secondary (read, process, cache) mark."
11929   (gnus-summary-update-mark
11930    (cond ((memq article gnus-newsgroup-processable)
11931           gnus-process-mark)
11932          ((memq article gnus-newsgroup-cached)
11933           gnus-cached-mark)
11934          ((memq article gnus-newsgroup-replied)
11935           gnus-replied-mark)
11936          ((memq article gnus-newsgroup-saved)
11937           gnus-saved-mark)
11938          (t gnus-unread-mark))
11939    'replied)
11940   (when (gnus-visual-p 'summary-highlight 'highlight)
11941     (run-hooks 'gnus-summary-update-hook))
11942   t)
11943
11944 (defun gnus-summary-update-mark (mark type)
11945   (beginning-of-line)
11946   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11947         (buffer-read-only nil))
11948     (when forward
11949       ;; Go to the right position on the line.
11950       (forward-char forward)
11951       ;; Replace the old mark with the new mark.
11952       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11953       ;; Optionally update the marks by some user rule.
11954       (when (eq type 'unread)
11955         (gnus-data-set-mark
11956          (gnus-data-find (gnus-summary-article-number)) mark)
11957         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11958
11959 (defun gnus-mark-article-as-read (article &optional mark)
11960   "Enter ARTICLE in the pertinent lists and remove it from others."
11961   ;; Make the article expirable.
11962   (let ((mark (or mark gnus-del-mark)))
11963     (if (= mark gnus-expirable-mark)
11964         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11965       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11966     ;; Remove from unread and marked lists.
11967     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11968     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11969     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11970     (push (cons article mark) gnus-newsgroup-reads)
11971     ;; Possibly remove from cache, if that is used.
11972     (when gnus-use-cache
11973       (gnus-cache-enter-remove-article article))))
11974
11975 (defun gnus-mark-article-as-unread (article &optional mark)
11976   "Enter ARTICLE in the pertinent lists and remove it from others."
11977   (let ((mark (or mark gnus-ticked-mark)))
11978     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11979     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11980     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11981     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11982     (cond ((= mark gnus-ticked-mark)
11983            (push article gnus-newsgroup-marked))
11984           ((= mark gnus-dormant-mark)
11985            (push article gnus-newsgroup-dormant))
11986           (t
11987            (push article gnus-newsgroup-unreads)))
11988     (setq gnus-newsgroup-reads
11989           (delq (assq article gnus-newsgroup-reads)
11990                 gnus-newsgroup-reads))))
11991
11992 (defalias 'gnus-summary-mark-as-unread-forward
11993   'gnus-summary-tick-article-forward)
11994 (make-obsolete 'gnus-summary-mark-as-unread-forward
11995                'gnus-summary-tick-article-forward)
11996 (defun gnus-summary-tick-article-forward (n)
11997   "Tick N articles forwards.
11998 If N is negative, tick backwards instead.
11999 The difference between N and the number of articles ticked is returned."
12000   (interactive "p")
12001   (gnus-summary-mark-forward n gnus-ticked-mark))
12002
12003 (defalias 'gnus-summary-mark-as-unread-backward
12004   'gnus-summary-tick-article-backward)
12005 (make-obsolete 'gnus-summary-mark-as-unread-backward
12006                'gnus-summary-tick-article-backward)
12007 (defun gnus-summary-tick-article-backward (n)
12008   "Tick N articles backwards.
12009 The difference between N and the number of articles ticked is returned."
12010   (interactive "p")
12011   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12012
12013 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12014 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12015 (defun gnus-summary-tick-article (&optional article clear-mark)
12016   "Mark current article as unread.
12017 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12018 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12019   (interactive)
12020   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12021                                        gnus-ticked-mark)))
12022
12023 (defun gnus-summary-mark-as-read-forward (n)
12024   "Mark N articles as read forwards.
12025 If N is negative, mark backwards instead.
12026 The difference between N and the actual number of articles marked is
12027 returned."
12028   (interactive "p")
12029   (gnus-summary-mark-forward n gnus-del-mark t))
12030
12031 (defun gnus-summary-mark-as-read-backward (n)
12032   "Mark the N articles as read backwards.
12033 The difference between N and the actual number of articles marked is
12034 returned."
12035   (interactive "p")
12036   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12037
12038 (defun gnus-summary-mark-as-read (&optional article mark)
12039   "Mark current article as read.
12040 ARTICLE specifies the article to be marked as read.
12041 MARK specifies a string to be inserted at the beginning of the line."
12042   (gnus-summary-mark-article article mark))
12043
12044 (defun gnus-summary-clear-mark-forward (n)
12045   "Clear marks from N articles forward.
12046 If N is negative, clear backward instead.
12047 The difference between N and the number of marks cleared is returned."
12048   (interactive "p")
12049   (gnus-summary-mark-forward n gnus-unread-mark))
12050
12051 (defun gnus-summary-clear-mark-backward (n)
12052   "Clear marks from N articles backward.
12053 The difference between N and the number of marks cleared is returned."
12054   (interactive "p")
12055   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12056
12057 (defun gnus-summary-mark-unread-as-read ()
12058   "Intended to be used by `gnus-summary-mark-article-hook'."
12059   (when (memq gnus-current-article gnus-newsgroup-unreads)
12060     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12061
12062 (defun gnus-summary-mark-read-and-unread-as-read ()
12063   "Intended to be used by `gnus-summary-mark-article-hook'."
12064   (let ((mark (gnus-summary-article-mark)))
12065     (when (or (gnus-unread-mark-p mark)
12066               (gnus-read-mark-p mark))
12067       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12068
12069 (defun gnus-summary-mark-region-as-read (point mark all)
12070   "Mark all unread articles between point and mark as read.
12071 If given a prefix, mark all articles between point and mark as read,
12072 even ticked and dormant ones."
12073   (interactive "r\nP")
12074   (save-excursion
12075     (let (article)
12076       (goto-char point)
12077       (beginning-of-line)
12078       (while (and
12079               (< (point) mark)
12080               (progn
12081                 (when (or all
12082                           (memq (setq article (gnus-summary-article-number))
12083                                 gnus-newsgroup-unreads))
12084                   (gnus-summary-mark-article article gnus-del-mark))
12085                 t)
12086               (gnus-summary-find-next))))))
12087
12088 (defun gnus-summary-mark-below (score mark)
12089   "Mark articles with score less than SCORE with MARK."
12090   (interactive "P\ncMark: ")
12091   (gnus-set-global-variables)
12092   (setq score (if score
12093                   (prefix-numeric-value score)
12094                 (or gnus-summary-default-score 0)))
12095   (save-excursion
12096     (set-buffer gnus-summary-buffer)
12097     (goto-char (point-min))
12098     (while 
12099         (progn
12100           (and (< (gnus-summary-article-score) score)
12101                (gnus-summary-mark-article nil mark))
12102           (gnus-summary-find-next)))))
12103
12104 (defun gnus-summary-kill-below (&optional score)
12105   "Mark articles with score below SCORE as read."
12106   (interactive "P")
12107   (gnus-set-global-variables)
12108   (gnus-summary-mark-below score gnus-killed-mark))
12109
12110 (defun gnus-summary-clear-above (&optional score)
12111   "Clear all marks from articles with score above SCORE."
12112   (interactive "P")
12113   (gnus-set-global-variables)
12114   (gnus-summary-mark-above score gnus-unread-mark))
12115
12116 (defun gnus-summary-tick-above (&optional score)
12117   "Tick all articles with score above SCORE."
12118   (interactive "P")
12119   (gnus-set-global-variables)
12120   (gnus-summary-mark-above score gnus-ticked-mark))
12121
12122 (defun gnus-summary-mark-above (score mark)
12123   "Mark articles with score over SCORE with MARK."
12124   (interactive "P\ncMark: ")
12125   (gnus-set-global-variables)
12126   (setq score (if score
12127                   (prefix-numeric-value score)
12128                 (or gnus-summary-default-score 0)))
12129   (save-excursion
12130     (set-buffer gnus-summary-buffer)
12131     (goto-char (point-min))
12132     (while (and (progn
12133                   (if (> (gnus-summary-article-score) score)
12134                       (gnus-summary-mark-article nil mark))
12135                   t)
12136                 (gnus-summary-find-next)))))
12137
12138 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12139 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12140 (defun gnus-summary-limit-include-expunged ()
12141   "Display all the hidden articles that were expunged for low scores."
12142   (interactive)
12143   (gnus-set-global-variables)
12144   (let ((buffer-read-only nil))
12145     (let ((scored gnus-newsgroup-scored)
12146           headers h)
12147       (while scored
12148         (or (gnus-summary-goto-subject (caar scored))
12149             (and (setq h (gnus-summary-article-header (caar scored)))
12150                  (< (cdar scored) gnus-summary-expunge-below)
12151                  (setq headers (cons h headers))))
12152         (setq scored (cdr scored)))
12153       (or headers (error "No expunged articles hidden."))
12154       (goto-char (point-min))
12155       (gnus-summary-prepare-unthreaded (nreverse headers)))
12156     (goto-char (point-min))
12157     (gnus-summary-position-point)))
12158
12159 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12160   "Mark all articles not marked as unread in this newsgroup as read.
12161 If prefix argument ALL is non-nil, all articles are marked as read.
12162 If QUIETLY is non-nil, no questions will be asked.
12163 If TO-HERE is non-nil, it should be a point in the buffer.  All
12164 articles before this point will be marked as read.
12165 The number of articles marked as read is returned."
12166   (interactive "P")
12167   (gnus-set-global-variables)
12168   (prog1
12169       (if (or quietly
12170               (not gnus-interactive-catchup) ;Without confirmation?
12171               gnus-expert-user
12172               (gnus-y-or-n-p
12173                (if all
12174                    "Mark absolutely all articles as read? "
12175                  "Mark all unread articles as read? ")))
12176           (if (and not-mark
12177                    (not gnus-newsgroup-adaptive)
12178                    (not gnus-newsgroup-auto-expire))
12179               (progn
12180                 (when all
12181                   (setq gnus-newsgroup-marked nil
12182                         gnus-newsgroup-dormant nil))
12183                 (setq gnus-newsgroup-unreads nil))
12184             ;; We actually mark all articles as canceled, which we
12185             ;; have to do when using auto-expiry or adaptive scoring.
12186             (gnus-summary-show-all-threads)
12187             (if (gnus-summary-first-subject (not all))
12188                 (while (and
12189                         (if to-here (< (point) to-here) t)
12190                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12191                         (gnus-summary-find-next (not all)))))
12192             (unless to-here
12193               (setq gnus-newsgroup-unreads nil))
12194             (gnus-set-mode-line 'summary)))
12195     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12196       (if (and (not to-here) (eq 'nnvirtual (car method)))
12197           (nnvirtual-catchup-group
12198            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12199     (gnus-summary-position-point)))
12200
12201 (defun gnus-summary-catchup-to-here (&optional all)
12202   "Mark all unticked articles before the current one as read.
12203 If ALL is non-nil, also mark ticked and dormant articles as read."
12204   (interactive "P")
12205   (gnus-set-global-variables)
12206   (save-excursion
12207     (let ((beg (point)))
12208       ;; We check that there are unread articles.
12209       (when (or all (gnus-summary-find-prev))
12210         (gnus-summary-catchup all t beg))))
12211   (gnus-summary-position-point))
12212
12213 (defun gnus-summary-catchup-all (&optional quietly)
12214   "Mark all articles in this newsgroup as read."
12215   (interactive "P")
12216   (gnus-set-global-variables)
12217   (gnus-summary-catchup t quietly))
12218
12219 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12220   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12221 If prefix argument ALL is non-nil, all articles are marked as read."
12222   (interactive "P")
12223   (gnus-set-global-variables)
12224   (gnus-summary-catchup all quietly nil 'fast)
12225   ;; Select next newsgroup or exit.
12226   (if (eq gnus-auto-select-next 'quietly)
12227       (gnus-summary-next-group nil)
12228     (gnus-summary-exit)))
12229
12230 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12231   "Mark all articles in this newsgroup as read, and then exit."
12232   (interactive "P")
12233   (gnus-set-global-variables)
12234   (gnus-summary-catchup-and-exit t quietly))
12235
12236 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12237 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12238   "Mark all articles in this group as read and select the next group.
12239 If given a prefix, mark all articles, unread as well as ticked, as
12240 read."
12241   (interactive "P")
12242   (gnus-set-global-variables)
12243   (save-excursion
12244     (gnus-summary-catchup all))
12245   (gnus-summary-next-article t nil nil t))
12246
12247 ;; Thread-based commands.
12248
12249 (defun gnus-summary-articles-in-thread (&optional article)
12250   "Return a list of all articles in the current thread.
12251 If ARTICLE is non-nil, return all articles in the thread that starts
12252 with that article."
12253   (let* ((article (or article (gnus-summary-article-number)))
12254          (data (gnus-data-find-list article))
12255          (top-level (gnus-data-level (car data)))
12256          (top-subject
12257           (cond ((null gnus-thread-operation-ignore-subject)
12258                  (gnus-simplify-subject-re
12259                   (mail-header-subject (gnus-data-header (car data)))))
12260                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12261                  (gnus-simplify-subject-fuzzy
12262                   (mail-header-subject (gnus-data-header (car data)))))
12263                 (t nil)))
12264          articles)
12265     (if (not data)
12266         ()                              ; This article doesn't exist.
12267       (while data
12268         (and (or (not top-subject)
12269                  (string= top-subject
12270                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12271                               (gnus-simplify-subject-fuzzy
12272                                (mail-header-subject
12273                                 (gnus-data-header (car data))))
12274                             (gnus-simplify-subject-re
12275                              (mail-header-subject
12276                               (gnus-data-header (car data)))))))
12277              (setq articles (cons (gnus-data-number (car data)) articles)))
12278         (if (and (setq data (cdr data))
12279                  (> (gnus-data-level (car data)) top-level))
12280             ()
12281           (setq data nil)))
12282       ;; Return the list of articles.
12283       (nreverse articles))))
12284
12285 (defun gnus-summary-rethread-current ()
12286   "Rethread the thread the current article is part of."
12287   (interactive)
12288   (gnus-set-global-variables)
12289   (let* ((gnus-show-threads t)
12290          (article (gnus-summary-article-number))
12291          (id (mail-header-id (gnus-summary-article-header)))
12292          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12293     (unless id
12294       (error "No article on the current line"))
12295     (gnus-rebuild-thread id)
12296     (gnus-summary-goto-subject article)))
12297
12298 (defun gnus-summary-reparent-thread ()
12299   "Make current article child of the marked (or previous) article.
12300
12301 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12302 is non-nil or the Subject: of both articles are the same."
12303   (interactive)
12304   (or (not (gnus-group-read-only-p))
12305       (error "The current newsgroup does not support article editing."))
12306   (or (<= (length gnus-newsgroup-processable) 1)
12307       (error "No more than one article may be marked."))
12308   (save-window-excursion
12309     (let ((gnus-article-buffer " *reparent*")
12310           (current-article (gnus-summary-article-number))
12311           ; first grab the marked article, otherwise one line up.
12312           (parent-article (if (not (null gnus-newsgroup-processable))
12313                               (car gnus-newsgroup-processable)
12314                             (save-excursion
12315                               (if (eq (forward-line -1) 0)
12316                                   (gnus-summary-article-number)
12317                                 (error "Beginning of summary buffer."))))))
12318       (or (not (eq current-article parent-article))
12319           (error "An article may not be self-referential."))
12320       (let ((message-id (mail-header-id 
12321                          (gnus-summary-article-header parent-article))))
12322         (or (and message-id (not (equal message-id "")))
12323             (error "No message-id in desired parent."))
12324         (gnus-summary-select-article t t nil current-article)
12325         (set-buffer gnus-article-buffer)
12326         (setq buffer-read-only nil)
12327         (let ((buf (format "%s" (buffer-string))))
12328           (erase-buffer)
12329           (insert buf))
12330         (goto-char (point-min))
12331         (if (search-forward-regexp "^References: " nil t)
12332             (insert message-id " " )
12333           (insert "References: " message-id "\n"))
12334         (or (gnus-request-replace-article current-article
12335                                           (car gnus-article-current)
12336                                           gnus-article-buffer)
12337             (error "Couldn't replace article."))
12338         (set-buffer gnus-summary-buffer)
12339         (gnus-summary-unmark-all-processable)
12340         (gnus-summary-rethread-current)
12341         (message "Article %d is now the child of article %d."
12342                  current-article parent-article)))))
12343
12344 (defun gnus-summary-toggle-threads (&optional arg)
12345   "Toggle showing conversation threads.
12346 If ARG is positive number, turn showing conversation threads on."
12347   (interactive "P")
12348   (gnus-set-global-variables)
12349   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12350     (setq gnus-show-threads
12351           (if (null arg) (not gnus-show-threads)
12352             (> (prefix-numeric-value arg) 0)))
12353     (gnus-summary-prepare)
12354     (gnus-summary-goto-subject current)
12355     (gnus-summary-position-point)))
12356
12357 (defun gnus-summary-show-all-threads ()
12358   "Show all threads."
12359   (interactive)
12360   (gnus-set-global-variables)
12361   (save-excursion
12362     (let ((buffer-read-only nil))
12363       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12364   (gnus-summary-position-point))
12365
12366 (defun gnus-summary-show-thread ()
12367   "Show thread subtrees.
12368 Returns nil if no thread was there to be shown."
12369   (interactive)
12370   (gnus-set-global-variables)
12371   (let ((buffer-read-only nil)
12372         (orig (point))
12373         ;; first goto end then to beg, to have point at beg after let
12374         (end (progn (end-of-line) (point)))
12375         (beg (progn (beginning-of-line) (point))))
12376     (prog1
12377         ;; Any hidden lines here?
12378         (search-forward "\r" end t)
12379       (subst-char-in-region beg end ?\^M ?\n t)
12380       (goto-char orig)
12381       (gnus-summary-position-point))))
12382
12383 (defun gnus-summary-hide-all-threads ()
12384   "Hide all thread subtrees."
12385   (interactive)
12386   (gnus-set-global-variables)
12387   (save-excursion
12388     (goto-char (point-min))
12389     (gnus-summary-hide-thread)
12390     (while (zerop (gnus-summary-next-thread 1 t))
12391       (gnus-summary-hide-thread)))
12392   (gnus-summary-position-point))
12393
12394 (defun gnus-summary-hide-thread ()
12395   "Hide thread subtrees.
12396 Returns nil if no threads were there to be hidden."
12397   (interactive)
12398   (gnus-set-global-variables)
12399   (let ((buffer-read-only nil)
12400         (start (point))
12401         (article (gnus-summary-article-number)))
12402     (goto-char start)
12403     ;; Go forward until either the buffer ends or the subthread
12404     ;; ends.
12405     (when (and (not (eobp))
12406                (or (zerop (gnus-summary-next-thread 1 t))
12407                    (goto-char (point-max))))
12408       (prog1
12409           (if (and (> (point) start)
12410                    (search-backward "\n" start t))
12411               (progn
12412                 (subst-char-in-region start (point) ?\n ?\^M)
12413                 (gnus-summary-goto-subject article))
12414             (goto-char start)
12415             nil)
12416         ;;(gnus-summary-position-point)
12417         ))))
12418
12419 (defun gnus-summary-go-to-next-thread (&optional previous)
12420   "Go to the same level (or less) next thread.
12421 If PREVIOUS is non-nil, go to previous thread instead.
12422 Return the article number moved to, or nil if moving was impossible."
12423   (let ((level (gnus-summary-thread-level))
12424         (way (if previous -1 1))
12425         (beg (point)))
12426     (forward-line way)
12427     (while (and (not (eobp))
12428                 (< level (gnus-summary-thread-level)))
12429       (forward-line way))
12430     (if (eobp)
12431         (progn
12432           (goto-char beg)
12433           nil)
12434       (setq beg (point))
12435       (prog1
12436           (gnus-summary-article-number)
12437         (goto-char beg)))))
12438
12439 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12440   "Go to the same level (or less) next thread.
12441 If PREVIOUS is non-nil, go to previous thread instead.
12442 Return the article number moved to, or nil if moving was impossible."
12443   (if (and (eq gnus-summary-make-false-root 'dummy)
12444            (gnus-summary-article-intangible-p))
12445       (let ((beg (point)))
12446         (while (and (zerop (forward-line 1))
12447                     (not (gnus-summary-article-intangible-p))
12448                     (not (zerop (save-excursion 
12449                                   (gnus-summary-thread-level))))))
12450         (if (eobp)
12451             (progn
12452               (goto-char beg)
12453               nil)
12454           (point)))
12455     (let* ((level (gnus-summary-thread-level))
12456            (article (gnus-summary-article-number))
12457            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12458            oart)
12459       (while data
12460         (if (<= (gnus-data-level (car data)) level)
12461             (setq oart (gnus-data-number (car data))
12462                   data nil)
12463           (setq data (cdr data))))
12464       (and oart
12465            (gnus-summary-goto-subject oart)))))
12466
12467 (defun gnus-summary-next-thread (n &optional silent)
12468   "Go to the same level next N'th thread.
12469 If N is negative, search backward instead.
12470 Returns the difference between N and the number of skips actually
12471 done.
12472
12473 If SILENT, don't output messages."
12474   (interactive "p")
12475   (gnus-set-global-variables)
12476   (let ((backward (< n 0))
12477         (n (abs n))
12478         old dum int)
12479     (while (and (> n 0)
12480                 (gnus-summary-go-to-next-thread backward))
12481       (decf n))
12482     (unless silent 
12483       (gnus-summary-position-point))
12484     (when (and (not silent) (/= 0 n))
12485       (gnus-message 7 "No more threads"))
12486     n))
12487
12488 (defun gnus-summary-prev-thread (n)
12489   "Go to the same level previous N'th thread.
12490 Returns the difference between N and the number of skips actually
12491 done."
12492   (interactive "p")
12493   (gnus-set-global-variables)
12494   (gnus-summary-next-thread (- n)))
12495
12496 (defun gnus-summary-go-down-thread ()
12497   "Go down one level in the current thread."
12498   (let ((children (gnus-summary-article-children)))
12499     (and children
12500          (gnus-summary-goto-subject (car children)))))
12501
12502 (defun gnus-summary-go-up-thread ()
12503   "Go up one level in the current thread."
12504   (let ((parent (gnus-summary-article-parent)))
12505     (and parent
12506          (gnus-summary-goto-subject parent))))
12507
12508 (defun gnus-summary-down-thread (n)
12509   "Go down thread N steps.
12510 If N is negative, go up instead.
12511 Returns the difference between N and how many steps down that were
12512 taken."
12513   (interactive "p")
12514   (gnus-set-global-variables)
12515   (let ((up (< n 0))
12516         (n (abs n)))
12517     (while (and (> n 0)
12518                 (if up (gnus-summary-go-up-thread)
12519                   (gnus-summary-go-down-thread)))
12520       (setq n (1- n)))
12521     (gnus-summary-position-point)
12522     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12523     n))
12524
12525 (defun gnus-summary-up-thread (n)
12526   "Go up thread N steps.
12527 If N is negative, go up instead.
12528 Returns the difference between N and how many steps down that were
12529 taken."
12530   (interactive "p")
12531   (gnus-set-global-variables)
12532   (gnus-summary-down-thread (- n)))
12533
12534 (defun gnus-summary-top-thread ()
12535   "Go to the top of the thread."
12536   (interactive)
12537   (gnus-set-global-variables)
12538   (while (gnus-summary-go-up-thread))
12539   (gnus-summary-article-number))
12540
12541 (defun gnus-summary-kill-thread (&optional unmark)
12542   "Mark articles under current thread as read.
12543 If the prefix argument is positive, remove any kinds of marks.
12544 If the prefix argument is negative, tick articles instead."
12545   (interactive "P")
12546   (gnus-set-global-variables)
12547   (if unmark
12548       (setq unmark (prefix-numeric-value unmark)))
12549   (let ((articles (gnus-summary-articles-in-thread)))
12550     (save-excursion
12551       ;; Expand the thread.
12552       (gnus-summary-show-thread)
12553       ;; Mark all the articles.
12554       (while articles
12555         (gnus-summary-goto-subject (car articles))
12556         (cond ((null unmark)
12557                (gnus-summary-mark-article-as-read gnus-killed-mark))
12558               ((> unmark 0)
12559                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12560               (t
12561                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12562         (setq articles (cdr articles))))
12563     ;; Hide killed subtrees.
12564     (and (null unmark)
12565          gnus-thread-hide-killed
12566          (gnus-summary-hide-thread))
12567     ;; If marked as read, go to next unread subject.
12568     (if (null unmark)
12569         ;; Go to next unread subject.
12570         (gnus-summary-next-subject 1 t)))
12571   (gnus-set-mode-line 'summary))
12572
12573 ;; Summary sorting commands
12574
12575 (defun gnus-summary-sort-by-number (&optional reverse)
12576   "Sort summary buffer by article number.
12577 Argument REVERSE means reverse order."
12578   (interactive "P")
12579   (gnus-summary-sort 'number reverse))
12580
12581 (defun gnus-summary-sort-by-author (&optional reverse)
12582   "Sort summary buffer by author name alphabetically.
12583 If case-fold-search is non-nil, case of letters is ignored.
12584 Argument REVERSE means reverse order."
12585   (interactive "P")
12586   (gnus-summary-sort 'author reverse))
12587
12588 (defun gnus-summary-sort-by-subject (&optional reverse)
12589   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12590 If case-fold-search is non-nil, case of letters is ignored.
12591 Argument REVERSE means reverse order."
12592   (interactive "P")
12593   (gnus-summary-sort 'subject reverse))
12594
12595 (defun gnus-summary-sort-by-date (&optional reverse)
12596   "Sort summary buffer by date.
12597 Argument REVERSE means reverse order."
12598   (interactive "P")
12599   (gnus-summary-sort 'date reverse))
12600
12601 (defun gnus-summary-sort-by-score (&optional reverse)
12602   "Sort summary buffer by score.
12603 Argument REVERSE means reverse order."
12604   (interactive "P")
12605   (gnus-summary-sort 'score reverse))
12606
12607 (defun gnus-summary-sort (predicate reverse)
12608   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12609   (gnus-set-global-variables)
12610   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12611          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12612          (gnus-thread-sort-functions
12613           (list
12614            (if (not reverse)
12615                thread
12616              `(lambda (t1 t2)
12617                 (,thread t2 t1)))))
12618          (gnus-article-sort-functions
12619           (list
12620            (if (not reverse)
12621                article
12622              `(lambda (t1 t2)
12623                 (,article t2 t1)))))
12624          (buffer-read-only)
12625          (gnus-summary-prepare-hook nil))
12626     ;; We do the sorting by regenerating the threads.
12627     (gnus-summary-prepare)
12628     ;; Hide subthreads if needed.
12629     (when (and gnus-show-threads gnus-thread-hide-subtree)
12630       (gnus-summary-hide-all-threads)))
12631   ;; If in async mode, we send some info to the backend.
12632   (when gnus-newsgroup-async
12633     (gnus-request-asynchronous
12634      gnus-newsgroup-name gnus-newsgroup-data)))
12635
12636 (defun gnus-sortable-date (date)
12637   "Make sortable string by string-lessp from DATE.
12638 Timezone package is used."
12639   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12640          (year (aref date 0))
12641          (month (aref date 1))
12642          (day (aref date 2)))
12643     (timezone-make-sortable-date
12644      year month day
12645      (timezone-make-time-string
12646       (aref date 3) (aref date 4) (aref date 5)))))
12647
12648 ;; Summary saving commands.
12649
12650 (defun gnus-summary-save-article (&optional n not-saved)
12651   "Save the current article using the default saver function.
12652 If N is a positive number, save the N next articles.
12653 If N is a negative number, save the N previous articles.
12654 If N is nil and any articles have been marked with the process mark,
12655 save those articles instead.
12656 The variable `gnus-default-article-saver' specifies the saver function."
12657   (interactive "P")
12658   (gnus-set-global-variables)
12659   (let ((articles (gnus-summary-work-articles n))
12660         file header article)
12661     (while articles
12662       (setq header (gnus-summary-article-header
12663                     (setq article (pop articles))))
12664       (if (not (vectorp header))
12665           ;; This is a pseudo-article.
12666           (if (assq 'name header)
12667               (gnus-copy-file (cdr (assq 'name header)))
12668             (gnus-message 1 "Article %d is unsaveable" article))
12669         ;; This is a real article.
12670         (save-window-excursion
12671           (gnus-summary-select-article t nil nil article))
12672         (unless gnus-save-all-headers
12673           ;; Remove headers accoring to `gnus-saved-headers'.
12674           (let ((gnus-visible-headers
12675                  (or gnus-saved-headers gnus-visible-headers)))
12676             (gnus-article-hide-headers nil t)))
12677         ;; Remove any X-Gnus lines.
12678         (save-excursion
12679           (set-buffer gnus-article-buffer)
12680           (save-restriction
12681             (let ((buffer-read-only nil))
12682               (nnheader-narrow-to-headers)
12683               (while (re-search-forward "^X-Gnus" nil t)
12684                 (gnus-delete-line)))))
12685         (save-window-excursion
12686           (if (not gnus-default-article-saver)
12687               (error "No default saver is defined.")
12688             (setq file (funcall
12689                         gnus-default-article-saver
12690                         (cond
12691                          ((not gnus-prompt-before-saving)
12692                           'default)
12693                          ((eq gnus-prompt-before-saving 'always)
12694                           nil)
12695                          (t file))))))
12696         (gnus-summary-remove-process-mark article)
12697         (unless not-saved
12698           (gnus-summary-set-saved-mark article))))
12699     (gnus-summary-position-point)
12700     n))
12701
12702 (defun gnus-summary-pipe-output (&optional arg)
12703   "Pipe the current article to a subprocess.
12704 If N is a positive number, pipe the N next articles.
12705 If N is a negative number, pipe the N previous articles.
12706 If N is nil and any articles have been marked with the process mark,
12707 pipe those articles instead."
12708   (interactive "P")
12709   (gnus-set-global-variables)
12710   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12711     (gnus-summary-save-article arg t))
12712   (gnus-configure-windows 'pipe))
12713
12714 (defun gnus-summary-save-article-mail (&optional arg)
12715   "Append the current article to an mail file.
12716 If N is a positive number, save the N next articles.
12717 If N is a negative number, save the N previous articles.
12718 If N is nil and any articles have been marked with the process mark,
12719 save those articles instead."
12720   (interactive "P")
12721   (gnus-set-global-variables)
12722   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12723     (gnus-summary-save-article arg)))
12724
12725 (defun gnus-summary-save-article-rmail (&optional arg)
12726   "Append the current article to an rmail file.
12727 If N is a positive number, save the N next articles.
12728 If N is a negative number, save the N previous articles.
12729 If N is nil and any articles have been marked with the process mark,
12730 save those articles instead."
12731   (interactive "P")
12732   (gnus-set-global-variables)
12733   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12734     (gnus-summary-save-article arg)))
12735
12736 (defun gnus-summary-save-article-file (&optional arg)
12737   "Append the current article to a file.
12738 If N is a positive number, save the N next articles.
12739 If N is a negative number, save the N previous articles.
12740 If N is nil and any articles have been marked with the process mark,
12741 save those articles instead."
12742   (interactive "P")
12743   (gnus-set-global-variables)
12744   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12745     (gnus-summary-save-article arg)))
12746
12747 (defun gnus-summary-save-article-body-file (&optional arg)
12748   "Append the current article body to a file.
12749 If N is a positive number, save the N next articles.
12750 If N is a negative number, save the N previous articles.
12751 If N is nil and any articles have been marked with the process mark,
12752 save those articles instead."
12753   (interactive "P")
12754   (gnus-set-global-variables)
12755   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12756     (gnus-summary-save-article arg)))
12757
12758 (defun gnus-get-split-value (methods)
12759   "Return a value based on the split METHODS."
12760   (let (split-name method result match)
12761     (when methods
12762       (save-excursion
12763         (set-buffer gnus-original-article-buffer)
12764         (save-restriction
12765           (nnheader-narrow-to-headers)
12766           (while methods
12767             (goto-char (point-min))
12768             (setq method (pop methods))
12769             (setq match (car method))
12770             (when (cond
12771                    ((stringp match)
12772                     ;; Regular expression.
12773                     (condition-case ()
12774                         (re-search-forward match nil t)
12775                       (error nil)))
12776                    ((gnus-functionp match)
12777                     ;; Function.
12778                     (save-restriction
12779                       (widen)
12780                       (setq result (funcall match gnus-newsgroup-name))))
12781                    ((consp match)
12782                     ;; Form.
12783                     (save-restriction
12784                       (widen)
12785                       (setq result (eval match)))))
12786               (setq split-name (append (cdr method) split-name))
12787               (cond ((stringp result)
12788                      (push result split-name))
12789                     ((consp result)
12790                      (setq split-name (append result split-name)))))))))
12791     split-name))
12792
12793 (defun gnus-read-move-group-name (prompt default articles prefix)
12794   "Read a group name."
12795   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12796          (prom
12797           (format "Where do you want to %s %s? "
12798                   prompt
12799                   (if (> (length articles) 1)
12800                       (format "these %d articles" (length articles))
12801                     "this article")))
12802          (to-newsgroup
12803           (cond
12804            ((null split-name)
12805             (completing-read
12806              (concat prom
12807                      (if default
12808                          (format "(default %s) " default)
12809                        ""))
12810              gnus-active-hashtb nil nil prefix))
12811            ((= 1 (length split-name))
12812             (completing-read prom gnus-active-hashtb
12813                              nil nil (cons (car split-name) 0)))
12814            (t
12815             (completing-read
12816              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12817
12818     (when to-newsgroup
12819       (if (or (string= to-newsgroup "")
12820               (string= to-newsgroup prefix))
12821           (setq to-newsgroup (or default "")))
12822       (or (gnus-active to-newsgroup)
12823           (gnus-activate-group to-newsgroup)
12824           (error "No such group: %s" to-newsgroup)))
12825     to-newsgroup))
12826
12827 (defun gnus-read-save-file-name (prompt default-name)
12828   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12829          (file
12830           ;; Let the split methods have their say.
12831           (cond
12832            ;; No split name was found.
12833            ((null split-name)
12834             (read-file-name
12835              (concat prompt " (default "
12836                      (file-name-nondirectory default-name) ") ")
12837              (file-name-directory default-name)
12838              default-name))
12839            ;; A single split name was found
12840            ((= 1 (length split-name))
12841             (read-file-name
12842              (concat prompt " (default " (car split-name) ") ")
12843              gnus-article-save-directory
12844              (concat gnus-article-save-directory (car split-name))))
12845            ;; A list of splits was found.
12846            (t
12847             (setq split-name (mapcar (lambda (el) (list el))
12848                                      (nreverse split-name)))
12849             (let ((result (completing-read
12850                            (concat prompt " ") split-name nil nil)))
12851               (concat gnus-article-save-directory
12852                       (if (string= result "")
12853                           (caar split-name)
12854                         result)))))))
12855     ;; If we have read a directory, we append the default file name.
12856     (when (file-directory-p file)
12857       (setq file (concat (file-name-as-directory file)
12858                          (file-name-nondirectory default-name))))
12859     ;; Possibly translate some charaters.
12860     (nnheader-translate-file-chars file)))
12861
12862 (defun gnus-article-archive-name (group)
12863   "Return the first instance of an \"Archive-name\" in the current buffer."
12864   (let ((case-fold-search t))
12865     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12866       (match-string 1))))
12867
12868 (defun gnus-summary-save-in-rmail (&optional filename)
12869   "Append this article to Rmail file.
12870 Optional argument FILENAME specifies file name.
12871 Directory to save to is default to `gnus-article-save-directory' which
12872 is initialized from the SAVEDIR environment variable."
12873   (interactive)
12874   (gnus-set-global-variables)
12875   (let ((default-name
12876           (funcall gnus-rmail-save-name gnus-newsgroup-name
12877                    gnus-current-headers gnus-newsgroup-last-rmail)))
12878     (setq filename
12879           (cond ((eq filename 'default)
12880                  default-name)
12881                 (filename filename)
12882                 (t (gnus-read-save-file-name
12883                     "Save in rmail file:" default-name))))
12884     (gnus-make-directory (file-name-directory filename))
12885     (gnus-eval-in-buffer-window
12886      gnus-original-article-buffer
12887      (save-excursion
12888        (save-restriction
12889          (widen)
12890          (gnus-output-to-rmail filename))))
12891     ;; Remember the directory name to save articles
12892     (setq gnus-newsgroup-last-rmail filename)))
12893
12894 (defun gnus-summary-save-in-mail (&optional filename)
12895   "Append this article to Unix mail file.
12896 Optional argument FILENAME specifies file name.
12897 Directory to save to is default to `gnus-article-save-directory' which
12898 is initialized from the SAVEDIR environment variable."
12899   (interactive)
12900   (gnus-set-global-variables)
12901   (let ((default-name
12902           (funcall gnus-mail-save-name gnus-newsgroup-name
12903                    gnus-current-headers gnus-newsgroup-last-mail)))
12904     (setq filename
12905           (cond ((eq filename 'default)
12906                  default-name)
12907                 (filename filename)
12908                 (t (gnus-read-save-file-name
12909                     "Save in Unix mail file:" default-name))))
12910     (setq filename
12911           (expand-file-name filename
12912                             (and default-name
12913                                  (file-name-directory default-name))))
12914     (gnus-make-directory (file-name-directory filename))
12915     (gnus-eval-in-buffer-window
12916      gnus-original-article-buffer
12917      (save-excursion
12918        (save-restriction
12919          (widen)
12920          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12921              (gnus-output-to-rmail filename)
12922            (let ((mail-use-rfc822 t))
12923              (rmail-output filename 1 t t))))))
12924     ;; Remember the directory name to save articles.
12925     (setq gnus-newsgroup-last-mail filename)))
12926
12927 (defun gnus-summary-save-in-file (&optional filename)
12928   "Append this article to file.
12929 Optional argument FILENAME specifies file name.
12930 Directory to save to is default to `gnus-article-save-directory' which
12931 is initialized from the SAVEDIR environment variable."
12932   (interactive)
12933   (gnus-set-global-variables)
12934   (let ((default-name
12935           (funcall gnus-file-save-name gnus-newsgroup-name
12936                    gnus-current-headers gnus-newsgroup-last-file)))
12937     (setq filename
12938           (cond ((eq filename 'default)
12939                  default-name)
12940                 (filename filename)
12941                 (t (gnus-read-save-file-name
12942                     "Save in file:" default-name))))
12943     (gnus-make-directory (file-name-directory filename))
12944     (gnus-eval-in-buffer-window
12945      gnus-original-article-buffer
12946      (save-excursion
12947        (save-restriction
12948          (widen)
12949          (gnus-output-to-file filename))))
12950     ;; Remember the directory name to save articles.
12951     (setq gnus-newsgroup-last-file filename)))
12952
12953 (defun gnus-summary-save-body-in-file (&optional filename)
12954   "Append this article body to a file.
12955 Optional argument FILENAME specifies file name.
12956 The directory to save in defaults to `gnus-article-save-directory' which
12957 is initialized from the SAVEDIR environment variable."
12958   (interactive)
12959   (gnus-set-global-variables)
12960   (let ((default-name
12961           (funcall gnus-file-save-name gnus-newsgroup-name
12962                    gnus-current-headers gnus-newsgroup-last-file)))
12963     (setq filename
12964           (cond ((eq filename 'default)
12965                  default-name)
12966                 (filename filename)
12967                 (t (gnus-read-save-file-name
12968                     "Save body in file:" default-name))))
12969     (gnus-make-directory (file-name-directory filename))
12970     (gnus-eval-in-buffer-window
12971      gnus-article-buffer
12972      (save-excursion
12973        (save-restriction
12974          (widen)
12975          (goto-char (point-min))
12976          (and (search-forward "\n\n" nil t)
12977               (narrow-to-region (point) (point-max)))
12978          (gnus-output-to-file filename))))
12979     ;; Remember the directory name to save articles.
12980     (setq gnus-newsgroup-last-file filename)))
12981
12982 (defun gnus-summary-save-in-pipe (&optional command)
12983   "Pipe this article to subprocess."
12984   (interactive)
12985   (gnus-set-global-variables)
12986   (setq command
12987         (cond ((eq command 'default)
12988                gnus-last-shell-command)
12989               (command command)
12990               (t (read-string "Shell command on article: "
12991                               gnus-last-shell-command))))
12992   (if (string-equal command "")
12993       (setq command gnus-last-shell-command))
12994   (gnus-eval-in-buffer-window
12995    gnus-article-buffer
12996    (save-restriction
12997      (widen)
12998      (shell-command-on-region (point-min) (point-max) command nil)))
12999   (setq gnus-last-shell-command command))
13000
13001 ;; Summary extract commands
13002
13003 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13004   (let ((buffer-read-only nil)
13005         (article (gnus-summary-article-number))
13006         after-article b e)
13007     (or (gnus-summary-goto-subject article)
13008         (error (format "No such article: %d" article)))
13009     (gnus-summary-position-point)
13010     ;; If all commands are to be bunched up on one line, we collect
13011     ;; them here.
13012     (if gnus-view-pseudos-separately
13013         ()
13014       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13015             files action)
13016         (while ps
13017           (setq action (cdr (assq 'action (car ps))))
13018           (setq files (list (cdr (assq 'name (car ps)))))
13019           (while (and ps (cdr ps)
13020                       (string= (or action "1")
13021                                (or (cdr (assq 'action (cadr ps))) "2")))
13022             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13023             (setcdr ps (cddr ps)))
13024           (if (not files)
13025               ()
13026             (if (not (string-match "%s" action))
13027                 (setq files (cons " " files)))
13028             (setq files (cons " " files))
13029             (and (assq 'execute (car ps))
13030                  (setcdr (assq 'execute (car ps))
13031                          (funcall (if (string-match "%s" action)
13032                                       'format 'concat)
13033                                   action
13034                                   (mapconcat (lambda (f) f) files " ")))))
13035           (setq ps (cdr ps)))))
13036     (if (and gnus-view-pseudos (not not-view))
13037         (while pslist
13038           (and (assq 'execute (car pslist))
13039                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13040                                      (eq gnus-view-pseudos 'not-confirm)))
13041           (setq pslist (cdr pslist)))
13042       (save-excursion
13043         (while pslist
13044           (setq after-article (or (cdr (assq 'article (car pslist)))
13045                                   (gnus-summary-article-number)))
13046           (gnus-summary-goto-subject after-article)
13047           (forward-line 1)
13048           (setq b (point))
13049           (insert "          " (file-name-nondirectory
13050                                 (cdr (assq 'name (car pslist))))
13051                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13052           (setq e (point))
13053           (forward-line -1)             ; back to `b'
13054           (add-text-properties
13055            b e (list 'gnus-number gnus-reffed-article-number
13056                      gnus-mouse-face-prop gnus-mouse-face))
13057           (gnus-data-enter
13058            after-article gnus-reffed-article-number
13059            gnus-unread-mark b (car pslist) 0 (- e b))
13060           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13061           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13062           (setq pslist (cdr pslist)))))))
13063
13064 (defun gnus-pseudos< (p1 p2)
13065   (let ((c1 (cdr (assq 'action p1)))
13066         (c2 (cdr (assq 'action p2))))
13067     (and c1 c2 (string< c1 c2))))
13068
13069 (defun gnus-request-pseudo-article (props)
13070   (cond ((assq 'execute props)
13071          (gnus-execute-command (cdr (assq 'execute props)))))
13072   (let ((gnus-current-article (gnus-summary-article-number)))
13073     (run-hooks 'gnus-mark-article-hook)))
13074
13075 (defun gnus-execute-command (command &optional automatic)
13076   (save-excursion
13077     (gnus-article-setup-buffer)
13078     (set-buffer gnus-article-buffer)
13079     (let ((command (if automatic command (read-string "Command: " command)))
13080           (buffer-read-only nil))
13081       (erase-buffer)
13082       (insert "$ " command "\n\n")
13083       (if gnus-view-pseudo-asynchronously
13084           (start-process "gnus-execute" nil "sh" "-c" command)
13085         (call-process "sh" nil t nil "-c" command)))))
13086
13087 (defun gnus-copy-file (file &optional to)
13088   "Copy FILE to TO."
13089   (interactive
13090    (list (read-file-name "Copy file: " default-directory)
13091          (read-file-name "Copy file to: " default-directory)))
13092   (gnus-set-global-variables)
13093   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13094   (and (file-directory-p to)
13095        (setq to (concat (file-name-as-directory to)
13096                         (file-name-nondirectory file))))
13097   (copy-file file to))
13098
13099 ;; Summary kill commands.
13100
13101 (defun gnus-summary-edit-global-kill (article)
13102   "Edit the \"global\" kill file."
13103   (interactive (list (gnus-summary-article-number)))
13104   (gnus-set-global-variables)
13105   (gnus-group-edit-global-kill article))
13106
13107 (defun gnus-summary-edit-local-kill ()
13108   "Edit a local kill file applied to the current newsgroup."
13109   (interactive)
13110   (gnus-set-global-variables)
13111   (setq gnus-current-headers (gnus-summary-article-header))
13112   (gnus-set-global-variables)
13113   (gnus-group-edit-local-kill
13114    (gnus-summary-article-number) gnus-newsgroup-name))
13115
13116 \f
13117 ;;;
13118 ;;; Gnus article mode
13119 ;;;
13120
13121 (put 'gnus-article-mode 'mode-class 'special)
13122
13123 (if gnus-article-mode-map
13124     nil
13125   (setq gnus-article-mode-map (make-keymap))
13126   (suppress-keymap gnus-article-mode-map)
13127
13128   (gnus-define-keys gnus-article-mode-map
13129     " " gnus-article-goto-next-page
13130     "\177" gnus-article-goto-prev-page
13131     [delete] gnus-article-goto-prev-page
13132     "\C-c^" gnus-article-refer-article
13133     "h" gnus-article-show-summary
13134     "s" gnus-article-show-summary
13135     "\C-c\C-m" gnus-article-mail
13136     "?" gnus-article-describe-briefly
13137     gnus-mouse-2 gnus-article-push-button
13138     "\r" gnus-article-press-button
13139     "\t" gnus-article-next-button
13140     "\M-\t" gnus-article-prev-button
13141     "\C-c\C-b" gnus-bug)
13142
13143   (substitute-key-definition
13144    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13145
13146 (defun gnus-article-mode ()
13147   "Major mode for displaying an article.
13148
13149 All normal editing commands are switched off.
13150
13151 The following commands are available:
13152
13153 \\<gnus-article-mode-map>
13154 \\[gnus-article-next-page]\t Scroll the article one page forwards
13155 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13156 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13157 \\[gnus-article-show-summary]\t Display the summary buffer
13158 \\[gnus-article-mail]\t Send a reply to the address near point
13159 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13160 \\[gnus-info-find-node]\t Go to the Gnus info node"
13161   (interactive)
13162   (when (and menu-bar-mode
13163              (gnus-visual-p 'article-menu 'menu))
13164     (gnus-article-make-menu-bar))
13165   (kill-all-local-variables)
13166   (gnus-simplify-mode-line)
13167   (setq mode-name "Article")
13168   (setq major-mode 'gnus-article-mode)
13169   (make-local-variable 'minor-mode-alist)
13170   (or (assq 'gnus-show-mime minor-mode-alist)
13171       (setq minor-mode-alist
13172             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13173   (use-local-map gnus-article-mode-map)
13174   (make-local-variable 'page-delimiter)
13175   (setq page-delimiter gnus-page-delimiter)
13176   (buffer-disable-undo (current-buffer))
13177   (setq buffer-read-only t)             ;Disable modification
13178   (run-hooks 'gnus-article-mode-hook))
13179
13180 (defun gnus-article-setup-buffer ()
13181   "Initialize the article buffer."
13182   (let* ((name (if gnus-single-article-buffer "*Article*"
13183                  (concat "*Article " gnus-newsgroup-name "*")))
13184          (original
13185           (progn (string-match "\\*Article" name)
13186                  (concat " *Original Article"
13187                          (substring name (match-end 0))))))
13188     (setq gnus-article-buffer name)
13189     (setq gnus-original-article-buffer original)
13190     ;; This might be a variable local to the summary buffer.
13191     (unless gnus-single-article-buffer
13192       (save-excursion
13193         (set-buffer gnus-summary-buffer)
13194         (setq gnus-article-buffer name)
13195         (setq gnus-original-article-buffer original)
13196         (gnus-set-global-variables))
13197       (make-local-variable 'gnus-summary-buffer))
13198     ;; Init original article buffer.
13199     (save-excursion
13200       (set-buffer (get-buffer-create gnus-original-article-buffer))
13201       (buffer-disable-undo (current-buffer))
13202       (setq major-mode 'gnus-original-article-mode)
13203       (make-local-variable 'gnus-original-article))
13204     (if (get-buffer name)
13205         (save-excursion
13206           (set-buffer name)
13207           (buffer-disable-undo (current-buffer))
13208           (setq buffer-read-only t)
13209           (gnus-add-current-to-buffer-list)
13210           (or (eq major-mode 'gnus-article-mode)
13211               (gnus-article-mode))
13212           (current-buffer))
13213       (save-excursion
13214         (set-buffer (get-buffer-create name))
13215         (gnus-add-current-to-buffer-list)
13216         (gnus-article-mode)
13217         (current-buffer)))))
13218
13219 ;; Set article window start at LINE, where LINE is the number of lines
13220 ;; from the head of the article.
13221 (defun gnus-article-set-window-start (&optional line)
13222   (set-window-start
13223    (get-buffer-window gnus-article-buffer)
13224    (save-excursion
13225      (set-buffer gnus-article-buffer)
13226      (goto-char (point-min))
13227      (if (not line)
13228          (point-min)
13229        (gnus-message 6 "Moved to bookmark")
13230        (search-forward "\n\n" nil t)
13231        (forward-line line)
13232        (point)))))
13233
13234 (defun gnus-kill-all-overlays ()
13235   "Delete all overlays in the current buffer."
13236   (when (fboundp 'overlay-lists)
13237     (let* ((overlayss (overlay-lists))
13238            (buffer-read-only nil)
13239            (overlays (nconc (car overlayss) (cdr overlayss))))
13240       (while overlays
13241         (delete-overlay (pop overlays))))))
13242
13243 (defun gnus-request-article-this-buffer (article group)
13244   "Get an article and insert it into this buffer."
13245   (let (do-update-line)
13246     (prog1
13247         (save-excursion
13248           (erase-buffer)
13249           (gnus-kill-all-overlays)
13250           (setq group (or group gnus-newsgroup-name))
13251
13252           ;; Open server if it has closed.
13253           (gnus-check-server (gnus-find-method-for-group group))
13254
13255           ;; Using `gnus-request-article' directly will insert the article into
13256           ;; `nntp-server-buffer' - so we'll save some time by not having to
13257           ;; copy it from the server buffer into the article buffer.
13258
13259           ;; We only request an article by message-id when we do not have the
13260           ;; headers for it, so we'll have to get those.
13261           (when (stringp article)
13262             (let ((gnus-override-method gnus-refer-article-method))
13263               (gnus-read-header article)))
13264
13265           ;; If the article number is negative, that means that this article
13266           ;; doesn't belong in this newsgroup (possibly), so we find its
13267           ;; message-id and request it by id instead of number.
13268           (when (and (numberp article)
13269                      gnus-summary-buffer
13270                      (get-buffer gnus-summary-buffer)
13271                      (buffer-name (get-buffer gnus-summary-buffer)))
13272             (save-excursion
13273               (set-buffer gnus-summary-buffer)
13274               (let ((header (gnus-summary-article-header article)))
13275                 (if (< article 0)
13276                     (cond 
13277                      ((memq article gnus-newsgroup-sparse)
13278                       ;; This is a sparse gap article.
13279                       (setq do-update-line article)
13280                       (setq article (mail-header-id header))
13281                       (let ((gnus-override-method gnus-refer-article-method))
13282                         (gnus-read-header article)))
13283                      ((vectorp header)
13284                       ;; It's a real article.
13285                       (setq article (mail-header-id header)))
13286                      (t
13287                       ;; It is an extracted pseudo-article.
13288                       (setq article 'pseudo)
13289                       (gnus-request-pseudo-article header))))
13290                 
13291                 (let ((method (gnus-find-method-for-group 
13292                                gnus-newsgroup-name)))
13293                   (if (not (eq (car method) 'nneething))
13294                       ()
13295                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13296                                        (mail-header-subject header))))
13297                       (if (file-directory-p dir)
13298                           (progn
13299                             (setq article 'nneething)
13300                             (gnus-group-enter-directory dir)))))))))
13301
13302           (cond
13303            ;; We first check `gnus-original-article-buffer'.
13304            ((and (get-buffer gnus-original-article-buffer)
13305                  (save-excursion
13306                    (set-buffer gnus-original-article-buffer)
13307                    (and (equal (car gnus-original-article) group)
13308                         (eq (cdr gnus-original-article) article))))
13309             (insert-buffer-substring gnus-original-article-buffer)
13310             'article)
13311            ;; Check the backlog.
13312            ((and gnus-keep-backlog
13313                  (gnus-backlog-request-article group article (current-buffer)))
13314             'article)
13315            ;; Check the cache.
13316            ((and gnus-use-cache
13317                  (numberp article)
13318                  (gnus-cache-request-article article group))
13319             'article)
13320            ;; Get the article and put into the article buffer.
13321            ((or (stringp article) (numberp article))
13322             (let ((gnus-override-method
13323                    (and (stringp article) gnus-refer-article-method))
13324                   (buffer-read-only nil))
13325               (erase-buffer)
13326               (gnus-kill-all-overlays)
13327               (if (gnus-request-article article group (current-buffer))
13328                   (progn
13329                     (and gnus-keep-backlog
13330                          (gnus-backlog-enter-article
13331                           group article (current-buffer)))
13332                     'article))))
13333            ;; It was a pseudo.
13334            (t article)))
13335
13336       ;; Take the article from the original article buffer
13337       ;; and place it in the buffer it's supposed to be in.
13338       (when (and (get-buffer gnus-article-buffer)
13339                  (equal (buffer-name (current-buffer))
13340                         (buffer-name (get-buffer gnus-article-buffer))))
13341         (save-excursion
13342           (if (get-buffer gnus-original-article-buffer)
13343               (set-buffer (get-buffer gnus-original-article-buffer))
13344             (set-buffer (get-buffer-create gnus-original-article-buffer))
13345             (buffer-disable-undo (current-buffer))
13346             (setq major-mode 'gnus-original-article-mode)
13347             (setq buffer-read-only t)
13348             (gnus-add-current-to-buffer-list))
13349           (setq gnus-original-article (cons group article))
13350           (let (buffer-read-only)
13351             (erase-buffer)
13352             (insert-buffer-substring gnus-article-buffer))))
13353     
13354       ;; Update sparse articles.
13355       (when do-update-line
13356         (save-excursion
13357           (set-buffer gnus-summary-buffer)
13358           (gnus-summary-update-article do-update-line)
13359           (gnus-summary-goto-subject do-update-line)
13360           (set-window-point (get-buffer-window (current-buffer) t)
13361                             (point)))))))
13362
13363 (defun gnus-read-header (id &optional header)
13364   "Read the headers of article ID and enter them into the Gnus system."
13365   (let ((group gnus-newsgroup-name)
13366         where)
13367     ;; First we check to see whether the header in question is already
13368     ;; fetched.
13369     (if (stringp id)
13370         ;; This is a Message-ID.
13371         (setq header (or header (gnus-id-to-header id)))
13372       ;; This is an article number.
13373       (setq header (or header (gnus-summary-article-header id))))
13374     (if (and header
13375              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13376         ;; We have found the header.
13377         header
13378       ;; We have to really fetch the header to this article.
13379       (when (setq where
13380                   (if (gnus-check-backend-function 'request-head group)
13381                       (gnus-request-head id group)
13382                     (gnus-request-article id group)))
13383         (save-excursion
13384           (set-buffer nntp-server-buffer)
13385           (and (search-forward "\n\n" nil t)
13386                (delete-region (1- (point)) (point-max)))
13387           (goto-char (point-max))
13388           (insert ".\n")
13389           (goto-char (point-min))
13390           (insert "211 ")
13391           (princ (cond
13392                   ((numberp id) id)
13393                   ((cdr where) (cdr where))
13394                   (header (mail-header-number header))
13395                   (t gnus-reffed-article-number))
13396                  (current-buffer))
13397           (insert " Article retrieved.\n"))
13398         ;(when (and header
13399         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13400         ;  (setcar (gnus-id-to-thread id) nil))
13401         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13402             ()                          ; Malformed head.
13403           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13404             (if (and (stringp id)
13405                      (not (string= (gnus-group-real-name group)
13406                                    (car where))))
13407                 ;; If we fetched by Message-ID and the article came
13408                 ;; from a different group, we fudge some bogus article
13409                 ;; numbers for this article.
13410                 (mail-header-set-number header gnus-reffed-article-number))
13411             (decf gnus-reffed-article-number)
13412             (push header gnus-newsgroup-headers)
13413             (setq gnus-current-headers header)
13414             (push (mail-header-number header) gnus-newsgroup-limit))
13415           header)))))
13416
13417 (defun gnus-article-prepare (article &optional all-headers header)
13418   "Prepare ARTICLE in article mode buffer.
13419 ARTICLE should either be an article number or a Message-ID.
13420 If ARTICLE is an id, HEADER should be the article headers.
13421 If ALL-HEADERS is non-nil, no headers are hidden."
13422   (save-excursion
13423     ;; Make sure we start in a summary buffer.
13424     (unless (eq major-mode 'gnus-summary-mode)
13425       (set-buffer gnus-summary-buffer))
13426     (setq gnus-summary-buffer (current-buffer))
13427     ;; Make sure the connection to the server is alive.
13428     (unless (gnus-server-opened
13429              (gnus-find-method-for-group gnus-newsgroup-name))
13430       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13431       (gnus-request-group gnus-newsgroup-name t))
13432     (let* ((article (if header (mail-header-number header) article))
13433            (summary-buffer (current-buffer))
13434            (internal-hook gnus-article-internal-prepare-hook)
13435            (group gnus-newsgroup-name)
13436            result)
13437       (save-excursion
13438         (gnus-article-setup-buffer)
13439         (set-buffer gnus-article-buffer)
13440         ;; Deactivate active regions.
13441         (when (and (boundp 'transient-mark-mode)
13442                    transient-mark-mode)
13443           (setq mark-active nil))
13444         (if (not (setq result (let ((buffer-read-only nil))
13445                                 (gnus-request-article-this-buffer
13446                                  article group))))
13447             ;; There is no such article.
13448             (save-excursion
13449               (when (and (numberp article)
13450                          (not (memq article gnus-newsgroup-sparse)))
13451                 (setq gnus-article-current
13452                       (cons gnus-newsgroup-name article))
13453                 (set-buffer gnus-summary-buffer)
13454                 (setq gnus-current-article article)
13455                 (gnus-summary-mark-article article gnus-canceled-mark))
13456               (unless (memq article gnus-newsgroup-sparse)
13457                 (gnus-message
13458                  1 "No such article (may have expired or been canceled)")
13459                 (ding)
13460                 nil))
13461           (if (or (eq result 'pseudo) (eq result 'nneething))
13462               (progn
13463                 (save-excursion
13464                   (set-buffer summary-buffer)
13465                   (setq gnus-last-article gnus-current-article
13466                         gnus-newsgroup-history (cons gnus-current-article
13467                                                      gnus-newsgroup-history)
13468                         gnus-current-article 0
13469                         gnus-current-headers nil
13470                         gnus-article-current nil)
13471                   (if (eq result 'nneething)
13472                       (gnus-configure-windows 'summary)
13473                     (gnus-configure-windows 'article))
13474                   (gnus-set-global-variables))
13475                 (gnus-set-mode-line 'article))
13476             ;; The result from the `request' was an actual article -
13477             ;; or at least some text that is now displayed in the
13478             ;; article buffer.
13479             (if (and (numberp article)
13480                      (not (eq article gnus-current-article)))
13481                 ;; Seems like a new article has been selected.
13482                 ;; `gnus-current-article' must be an article number.
13483                 (save-excursion
13484                   (set-buffer summary-buffer)
13485                   (setq gnus-last-article gnus-current-article
13486                         gnus-newsgroup-history (cons gnus-current-article
13487                                                      gnus-newsgroup-history)
13488                         gnus-current-article article
13489                         gnus-current-headers
13490                         (gnus-summary-article-header gnus-current-article)
13491                         gnus-article-current
13492                         (cons gnus-newsgroup-name gnus-current-article))
13493                   (unless (vectorp gnus-current-headers)
13494                     (setq gnus-current-headers nil))
13495                   (gnus-summary-show-thread)
13496                   (run-hooks 'gnus-mark-article-hook)
13497                   (gnus-set-mode-line 'summary)
13498                   (and (gnus-visual-p 'article-highlight 'highlight)
13499                        (run-hooks 'gnus-visual-mark-article-hook))
13500                   ;; Set the global newsgroup variables here.
13501                   ;; Suggested by Jim Sisolak
13502                   ;; <sisolak@trans4.neep.wisc.edu>.
13503                   (gnus-set-global-variables)
13504                   (setq gnus-have-all-headers
13505                         (or all-headers gnus-show-all-headers))
13506                   (and gnus-use-cache
13507                        (vectorp (gnus-summary-article-header article))
13508                        (gnus-cache-possibly-enter-article
13509                         group article
13510                         (gnus-summary-article-header article)
13511                         (memq article gnus-newsgroup-marked)
13512                         (memq article gnus-newsgroup-dormant)
13513                         (memq article gnus-newsgroup-unreads)))))
13514             ;; Hooks for getting information from the article.
13515             ;; This hook must be called before being narrowed.
13516             (let (buffer-read-only)
13517               (run-hooks 'internal-hook)
13518               (run-hooks 'gnus-article-prepare-hook)
13519               ;; Decode MIME message.
13520               (if gnus-show-mime
13521                   (if (or (not gnus-strict-mime)
13522                           (gnus-fetch-field "Mime-Version"))
13523                       (funcall gnus-show-mime-method)
13524                     (funcall gnus-decode-encoded-word-method)))
13525               ;; Perform the article display hooks.
13526               (run-hooks 'gnus-article-display-hook))
13527             ;; Do page break.
13528             (goto-char (point-min))
13529             (and gnus-break-pages (gnus-narrow-to-page))
13530             (gnus-set-mode-line 'article)
13531             (gnus-configure-windows 'article)
13532             (goto-char (point-min))
13533             t))))))
13534
13535 (defun gnus-article-show-all-headers ()
13536   "Show all article headers in article mode buffer."
13537   (save-excursion
13538     (gnus-article-setup-buffer)
13539     (set-buffer gnus-article-buffer)
13540     (let ((buffer-read-only nil))
13541       (gnus-unhide-text (point-min) (point-max)))))
13542
13543 (defun gnus-article-hide-headers-if-wanted ()
13544   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13545 Provided for backwards compatibility."
13546   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13547       gnus-inhibit-hiding
13548       (gnus-article-hide-headers)))
13549
13550 (defun gnus-article-hide-headers (&optional arg delete)
13551   "Toggle whether to hide unwanted headers and possibly sort them as well.
13552 If given a negative prefix, always show; if given a positive prefix,
13553 always hide."
13554   (interactive "P")
13555   (unless (gnus-article-check-hidden-text 'headers arg)
13556     ;; This function might be inhibited.
13557     (unless gnus-inhibit-hiding
13558       (save-excursion
13559         (set-buffer gnus-article-buffer)
13560         (save-restriction
13561           (let ((buffer-read-only nil)
13562                 (props (nconc (list 'gnus-type 'headers)
13563                               gnus-hidden-properties))
13564                 (ignored (when (not (stringp gnus-visible-headers))
13565                            (cond ((stringp gnus-ignored-headers)
13566                                   gnus-ignored-headers)
13567                                  ((listp gnus-ignored-headers)
13568                                   (mapconcat 'identity gnus-ignored-headers
13569                                              "\\|")))))
13570                 (visible
13571                  (cond ((stringp gnus-visible-headers)
13572                         gnus-visible-headers)
13573                        ((and gnus-visible-headers
13574                              (listp gnus-visible-headers))
13575                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13576                 want-list beg)
13577             ;; First we narrow to just the headers.
13578             (widen)
13579             (goto-char (point-min))
13580             ;; Hide any "From " lines at the beginning of (mail) articles.
13581             (while (looking-at "From ")
13582               (forward-line 1))
13583             (unless (bobp)
13584               (gnus-hide-text (point-min) (point) props))
13585             ;; Then treat the rest of the header lines.
13586             (narrow-to-region
13587              (point)
13588              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13589             ;; Then we use the two regular expressions
13590             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13591             ;; select which header lines is to remain visible in the
13592             ;; article buffer.
13593             (goto-char (point-min))
13594             (while (re-search-forward "^[^ \t]*:" nil t)
13595               (beginning-of-line)
13596               ;; We add the headers we want to keep to a list and delete
13597               ;; them from the buffer.
13598               (if (or (and visible (looking-at visible))
13599                       (and ignored (not (looking-at ignored))))
13600                   (progn
13601                     (push (buffer-substring
13602                            (setq beg (point))
13603                            (progn
13604                              (forward-line 1)
13605                              ;; Be sure to get multi-line headers...
13606                              (re-search-forward "^[^ \t]*:" nil t)
13607                              (beginning-of-line)
13608                              (point)))
13609                           want-list)
13610                     (delete-region beg (point)))
13611                 (forward-line 1)))
13612             ;; Sort the headers that we want to display.
13613             (setq want-list (sort want-list 'gnus-article-header-less))
13614             (goto-char (point-min))
13615             (while want-list
13616               (insert (pop want-list)))
13617             ;; We make the unwanted headers invisible.
13618             (if delete
13619                 (delete-region (point-min) (point-max))
13620               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13621               (gnus-hide-text-type (point) (point-max) 'headers))))))))
13622
13623 (defsubst gnus-article-header-rank (header)
13624   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13625   (let ((list gnus-sorted-header-list)
13626         (i 0))
13627     (while list
13628       (when (string-match (car list) header)
13629         (setq list nil))
13630       (setq list (cdr list))
13631       (incf i))
13632     i))
13633
13634 (defun gnus-article-header-less (h1 h2)
13635   "Say whether string H1 is \"less\" than string H2."
13636   (< (gnus-article-header-rank h1)
13637      (gnus-article-header-rank h2)))
13638
13639 (defun gnus-article-hide-boring-headers (&optional arg)
13640   "Toggle hiding of headers that aren't very interesting.
13641 If given a negative prefix, always show; if given a positive prefix,
13642 always hide."
13643   (interactive "P")
13644   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13645     (save-excursion
13646       (set-buffer gnus-article-buffer)
13647       (save-restriction
13648         (let ((buffer-read-only nil)
13649               (list gnus-boring-article-headers)
13650               (inhibit-point-motion-hooks t)
13651               elem)
13652           (nnheader-narrow-to-headers)
13653           (while list
13654             (setq elem (pop list))
13655             (goto-char (point-min))
13656             (cond
13657              ;; Hide empty headers.
13658              ((eq elem 'empty)
13659               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13660                 (forward-line -1)
13661                 (gnus-hide-text-type
13662                  (progn (beginning-of-line) (point))
13663                  (progn 
13664                    (end-of-line)
13665                    (if (re-search-forward "^[^ \t]" nil t)
13666                        (match-beginning 0)
13667                      (point-max)))
13668                  'boring-headers)))
13669              ;; Hide boring Newsgroups header.
13670              ((eq elem 'newsgroups)
13671               (when (equal (mail-fetch-field "newsgroups")
13672                            (gnus-group-real-name gnus-newsgroup-name))
13673                 (gnus-article-hide-header "newsgroups")))
13674              ((eq elem 'followup-to)
13675               (when (equal (mail-fetch-field "followup-to")
13676                            (mail-fetch-field "newsgroups"))
13677                 (gnus-article-hide-header "followup-to")))
13678              ((eq elem 'reply-to)
13679               (let ((from (mail-fetch-field "from"))
13680                     (reply-to (mail-fetch-field "reply-to")))
13681                 (when (and
13682                        from reply-to
13683                        (equal 
13684                         (nth 1 (mail-extract-address-components from))
13685                         (nth 1 (mail-extract-address-components reply-to))))
13686                   (gnus-article-hide-header "reply-to"))))
13687              ((eq elem 'date)
13688               (let ((date (mail-fetch-field "date")))
13689                 (when (and date
13690                            (< (gnus-days-between date (current-time-string))
13691                               4))
13692                   (gnus-article-hide-header "date")))))))))))
13693
13694 (defun gnus-article-hide-header (header)
13695   (save-excursion
13696     (goto-char (point-min))
13697     (when (re-search-forward (concat "^" header ":") nil t)
13698       (gnus-hide-text-type
13699        (progn (beginning-of-line) (point))
13700        (progn 
13701          (end-of-line)
13702          (if (re-search-forward "^[^ \t]" nil t)
13703              (match-beginning 0)
13704            (point-max)))
13705        'boring-headers))))
13706
13707 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13708 (defun gnus-article-treat-overstrike ()
13709   "Translate overstrikes into bold text."
13710   (interactive)
13711   (save-excursion
13712     (set-buffer gnus-article-buffer)
13713     (let ((buffer-read-only nil))
13714       (while (search-forward "\b" nil t)
13715         (let ((next (following-char))
13716               (previous (char-after (- (point) 2))))
13717           (cond ((eq next previous)
13718                  (put-text-property (- (point) 2) (point) 'invisible t)
13719                  (put-text-property (point) (1+ (point)) 'face 'bold))
13720                 ((eq next ?_)
13721                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13722                  (put-text-property
13723                   (- (point) 2) (1- (point)) 'face 'underline))
13724                 ((eq previous ?_)
13725                  (put-text-property (- (point) 2) (point) 'invisible t)
13726                  (put-text-property
13727                   (point) (1+ (point))  'face 'underline))))))))
13728
13729 (defun gnus-article-word-wrap ()
13730   "Format too long lines."
13731   (interactive)
13732   (save-excursion
13733     (set-buffer gnus-article-buffer)
13734     (let ((buffer-read-only nil))
13735       (widen)
13736       (goto-char (point-min))
13737       (search-forward "\n\n" nil t)
13738       (end-of-line 1)
13739       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13740             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13741             (adaptive-fill-mode t))
13742         (while (not (eobp))
13743           (and (>= (current-column) (min fill-column (window-width)))
13744                (/= (preceding-char) ?:)
13745                (fill-paragraph nil))
13746           (end-of-line 2))))))
13747
13748 (defun gnus-article-remove-cr ()
13749   "Remove carriage returns from an article."
13750   (interactive)
13751   (save-excursion
13752     (set-buffer gnus-article-buffer)
13753     (let ((buffer-read-only nil))
13754       (goto-char (point-min))
13755       (while (search-forward "\r" nil t)
13756         (replace-match "" t t)))))
13757
13758 (defun gnus-article-remove-trailing-blank-lines ()
13759   "Remove all trailing blank lines from the article."
13760   (interactive)
13761   (save-excursion
13762     (set-buffer gnus-article-buffer)
13763     (let ((buffer-read-only nil))
13764       (goto-char (point-max))
13765       (delete-region
13766        (point)
13767        (progn
13768          (while (looking-at "^[ \t]*$")
13769            (forward-line -1))
13770          (forward-line 1)
13771          (point))))))
13772
13773 (defun gnus-article-display-x-face (&optional force)
13774   "Look for an X-Face header and display it if present."
13775   (interactive (list 'force))
13776   (save-excursion
13777     (set-buffer gnus-article-buffer)
13778     ;; Delete the old process, if any.
13779     (when (process-status "gnus-x-face")
13780       (delete-process "gnus-x-face"))
13781     (let ((inhibit-point-motion-hooks t)
13782           (case-fold-search nil)
13783           from)
13784       (save-restriction
13785         (nnheader-narrow-to-headers)
13786         (setq from (mail-fetch-field "from"))
13787         (goto-char (point-min))
13788         (when (and gnus-article-x-face-command
13789                    (or force
13790                        ;; Check whether this face is censored.
13791                        (not gnus-article-x-face-too-ugly)
13792                        (and gnus-article-x-face-too-ugly from
13793                             (not (string-match gnus-article-x-face-too-ugly
13794                                                from))))
13795                    ;; Has to be present.
13796                    (re-search-forward "^X-Face: " nil t))
13797           ;; We now have the area of the buffer where the X-Face is stored.
13798           (let ((beg (point))
13799                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13800             ;; We display the face.
13801             (if (symbolp gnus-article-x-face-command)
13802                 ;; The command is a lisp function, so we call it.
13803                 (if (gnus-functionp gnus-article-x-face-command)
13804                     (funcall gnus-article-x-face-command beg end)
13805                   (error "%s is not a function" gnus-article-x-face-command))
13806               ;; The command is a string, so we interpret the command
13807               ;; as a, well, command, and fork it off.
13808               (let ((process-connection-type nil))
13809                 (process-kill-without-query
13810                  (start-process
13811                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13812                 (process-send-region "gnus-x-face" beg end)
13813                 (process-send-eof "gnus-x-face")))))))))
13814
13815 (defun gnus-headers-decode-quoted-printable ()
13816   "Hack to remove QP encoding from headers."
13817   (let ((case-fold-search t)
13818         (inhibit-point-motion-hooks t)
13819         string)
13820     (goto-char (point-min))
13821     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13822       (setq string (match-string 1))
13823       (narrow-to-region (match-beginning 0) (match-end 0))
13824       (delete-region (point-min) (point-max))
13825       (insert string)
13826       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13827       (subst-char-in-region (point-min) (point-max) ?_ ? )
13828       (widen)
13829       (goto-char (point-min)))))
13830
13831 (defun gnus-article-de-quoted-unreadable (&optional force)
13832   "Do a naive translation of a quoted-printable-encoded article.
13833 This is in no way, shape or form meant as a replacement for real MIME
13834 processing, but is simply a stop-gap measure until MIME support is
13835 written.
13836 If FORCE, decode the article whether it is marked as quoted-printable
13837 or not."
13838   (interactive (list 'force))
13839   (save-excursion
13840     (set-buffer gnus-article-buffer)
13841     (let ((case-fold-search t)
13842           (buffer-read-only nil)
13843           (type (gnus-fetch-field "content-transfer-encoding")))
13844       (when (or force
13845                 (and type (string-match "quoted-printable" type)))
13846         (gnus-headers-decode-quoted-printable)
13847         (goto-char (point-min))
13848         (search-forward "\n\n" nil 'move)
13849         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13850
13851 (defun gnus-mime-decode-quoted-printable (from to)
13852   "Decode Quoted-Printable in the region between FROM and TO."
13853   (goto-char from)
13854   (while (search-forward "=" to t)
13855     (cond ((eq (following-char) ?\n)
13856            (delete-char -1)
13857            (delete-char 1))
13858           ((looking-at "[0-9A-F][0-9A-F]")
13859            (delete-char -1)
13860            (insert (hexl-hex-string-to-integer
13861                     (buffer-substring (point) (+ 2 (point)))))
13862            (delete-char 2))
13863           ((looking-at "=")
13864            (delete-char 1))
13865           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13866
13867 (defun gnus-article-hide-pgp (&optional arg)
13868   "Toggle hiding of any PGP headers and signatures in the current article.
13869 If given a negative prefix, always show; if given a positive prefix,
13870 always hide."
13871   (interactive "P")
13872   (unless (gnus-article-check-hidden-text 'pgp arg)
13873     (save-excursion
13874       (set-buffer gnus-article-buffer)
13875       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13876             buffer-read-only beg end)
13877         (widen)
13878         (goto-char (point-min))
13879         ;; Hide the "header".
13880         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13881              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13882         (setq beg (point))
13883         ;; Hide the actual signature.
13884         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13885              (setq end (1+ (match-beginning 0)))
13886              (gnus-hide-text
13887               end
13888               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13889                   (match-end 0)
13890                 ;; Perhaps we shouldn't hide to the end of the buffer
13891                 ;; if there is no end to the signature?
13892                 (point-max))
13893               props))
13894         ;; Hide "- " PGP quotation markers.
13895         (when (and beg end)
13896           (narrow-to-region beg end)
13897           (goto-char (point-min))
13898           (while (re-search-forward "^- " nil t)
13899             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13900           (widen))))))
13901
13902 (defun gnus-article-hide-signature (&optional arg)
13903   "Hide the signature in the current article.
13904 If given a negative prefix, always show; if given a positive prefix,
13905 always hide."
13906   (interactive "P")
13907   (unless (gnus-article-check-hidden-text 'signature arg)
13908     (save-excursion
13909       (set-buffer gnus-article-buffer)
13910       (save-restriction
13911         (let ((buffer-read-only nil))
13912           (when (gnus-narrow-to-signature)
13913             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13914
13915 (defun gnus-article-strip-leading-blank-lines ()
13916   "Remove all blank lines from the beginning of the article."
13917   (interactive)
13918   (save-excursion
13919     (set-buffer gnus-article-buffer)
13920     (let (buffer-read-only)
13921       (goto-char (point-min))
13922       (when (search-forward "\n\n" nil t)
13923         (while (looking-at "[ \t]$")
13924           (gnus-delete-line))))))
13925
13926 (defun gnus-narrow-to-signature ()
13927   "Narrow to the signature."
13928   (widen)
13929   (goto-char (point-max))
13930   (when (re-search-backward gnus-signature-separator nil t)
13931     (forward-line 1)
13932     (when (or (null gnus-signature-limit)
13933               (and (numberp gnus-signature-limit)
13934                    (< (- (point-max) (point)) gnus-signature-limit))
13935               (and (gnus-functionp gnus-signature-limit)
13936                    (funcall gnus-signature-limit))
13937               (and (stringp gnus-signature-limit)
13938                    (not (re-search-forward gnus-signature-limit nil t))))
13939       (narrow-to-region (point) (point-max))
13940       t)))
13941
13942 (defun gnus-article-check-hidden-text (type arg)
13943   "Return nil if hiding is necessary."
13944   (save-excursion
13945     (set-buffer gnus-article-buffer)
13946     (let ((hide (gnus-article-hidden-text-p type)))
13947       (cond ((or (and (null arg) (eq hide 'hidden))
13948                  (and arg (< 0 (prefix-numeric-value arg))))
13949              (gnus-article-show-hidden-text type))
13950             ((eq hide 'shown)
13951              (gnus-article-show-hidden-text type t))
13952             (t nil)))))
13953
13954 (defun gnus-article-hidden-text-p (type)
13955   "Say whether the current buffer contains hidden text of type TYPE."
13956   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13957     (when pos
13958       (if (get-text-property pos 'invisible)
13959           'hidden
13960         'shown))))
13961
13962 (defun gnus-article-hide (&optional arg force)
13963   "Hide all the gruft in the current article.
13964 This means that PGP stuff, signatures, cited text and (some)
13965 headers will be hidden.
13966 If given a prefix, show the hidden text instead."
13967   (interactive (list current-prefix-arg 'force))
13968   (gnus-article-hide-headers arg)
13969   (gnus-article-hide-pgp arg)
13970   (gnus-article-hide-citation-maybe arg force)
13971   (gnus-article-hide-signature arg))
13972
13973 (defun gnus-article-show-hidden-text (type &optional hide)
13974   "Show all hidden text of type TYPE.
13975 If HIDE, hide the text instead."
13976   (save-excursion
13977     (set-buffer gnus-article-buffer)
13978     (let ((buffer-read-only nil)
13979           (inhibit-point-motion-hooks t)
13980           (beg (point-min)))
13981       (while (gnus-goto-char (text-property-any
13982                               beg (point-max) 'gnus-type type))
13983         (setq beg (point))
13984         (forward-char)
13985         (if hide
13986             (gnus-hide-text beg (point) gnus-hidden-properties)
13987           (gnus-unhide-text beg (point)))
13988         (setq beg (point)))
13989       t)))
13990
13991 (defvar gnus-article-time-units
13992   `((year . ,(* 365.25 24 60 60))
13993     (week . ,(* 7 24 60 60))
13994     (day . ,(* 24 60 60))
13995     (hour . ,(* 60 60))
13996     (minute . 60)
13997     (second . 1))
13998   "Mapping from time units to seconds.")
13999
14000 (defun gnus-article-date-ut (&optional type highlight)
14001   "Convert DATE date to universal time in the current article.
14002 If TYPE is `local', convert to local time; if it is `lapsed', output
14003 how much time has lapsed since DATE."
14004   (interactive (list 'ut t))
14005   (let* ((header (or gnus-current-headers
14006                      (gnus-summary-article-header) ""))
14007          (date (and (vectorp header) (mail-header-date header)))
14008          (date-regexp "^Date: \\|^X-Sent: ")
14009          (now (current-time))
14010          (inhibit-point-motion-hooks t))
14011     (when (and date (not (string= date "")))
14012       (save-excursion
14013         (set-buffer gnus-article-buffer)
14014         (save-restriction
14015           (nnheader-narrow-to-headers)
14016           (let ((buffer-read-only nil))
14017             ;; Delete any old Date headers.
14018             (if (zerop (nnheader-remove-header date-regexp t))
14019                 (beginning-of-line)
14020               (goto-char (point-max)))
14021             (insert
14022              (cond
14023               ;; Convert to the local timezone.  We have to slap a
14024               ;; `condition-case' round the calls to the timezone
14025               ;; functions since they aren't particularly resistant to
14026               ;; buggy dates.
14027               ((eq type 'local)
14028                (concat "Date: " (condition-case ()
14029                                     (timezone-make-date-arpa-standard date)
14030                                   (error date))
14031                        "\n"))
14032               ;; Convert to Universal Time.
14033               ((eq type 'ut)
14034                (concat "Date: "
14035                        (condition-case ()
14036                            (timezone-make-date-arpa-standard date nil "UT")
14037                          (error date))
14038                        "\n"))
14039               ;; Get the original date from the article.
14040               ((eq type 'original)
14041                (concat "Date: " date "\n"))
14042               ;; Do an X-Sent lapsed format.
14043               ((eq type 'lapsed)
14044                ;; If the date is seriously mangled, the timezone
14045                ;; functions are liable to bug out, so we condition-case
14046                ;; the entire thing.
14047                (let* ((real-time
14048                        (condition-case ()
14049                            (gnus-time-minus
14050                             (gnus-encode-date
14051                              (timezone-make-date-arpa-standard
14052                               (current-time-string now)
14053                               (current-time-zone now) "UT"))
14054                             (gnus-encode-date
14055                              (timezone-make-date-arpa-standard
14056                               date nil "UT")))
14057                          (error '(0 0))))
14058                       (real-sec (+ (* (float (car real-time)) 65536)
14059                                    (cadr real-time)))
14060                       (sec (abs real-sec))
14061                       num prev)
14062                  (if (zerop sec)
14063                      "X-Sent: Now\n"
14064                    (concat
14065                     "X-Sent: "
14066                     ;; This is a bit convoluted, but basically we go
14067                     ;; through the time units for years, weeks, etc,
14068                     ;; and divide things to see whether that results
14069                     ;; in positive answers.
14070                     (mapconcat
14071                      (lambda (unit)
14072                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14073                            ;; The (remaining) seconds are too few to
14074                            ;; be divided into this time unit.
14075                            ""
14076                          ;; It's big enough, so we output it.
14077                          (setq sec (- sec (* num (cdr unit))))
14078                          (prog1
14079                              (concat (if prev ", " "") (int-to-string
14080                                                         (floor num))
14081                                      " " (symbol-name (car unit))
14082                                      (if (> num 1) "s" ""))
14083                            (setq prev t))))
14084                      gnus-article-time-units "")
14085                     ;; If dates are odd, then it might appear like the
14086                     ;; article was sent in the future.
14087                     (if (> real-sec 0)
14088                         " ago\n"
14089                       " in the future\n")))))
14090               (t
14091                (error "Unknown conversion type: %s" type)))))
14092           ;; Do highlighting.
14093           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
14094             (gnus-article-highlight-headers)))))))
14095
14096 (defun gnus-article-date-local (&optional highlight)
14097   "Convert the current article date to the local timezone."
14098   (interactive (list t))
14099   (gnus-article-date-ut 'local highlight))
14100
14101 (defun gnus-article-date-original (&optional highlight)
14102   "Convert the current article date to what it was originally.
14103 This is only useful if you have used some other date conversion
14104 function and want to see what the date was before converting."
14105   (interactive (list t))
14106   (gnus-article-date-ut 'original highlight))
14107
14108 (defun gnus-article-date-lapsed (&optional highlight)
14109   "Convert the current article date to time lapsed since it was sent."
14110   (interactive (list t))
14111   (gnus-article-date-ut 'lapsed highlight))
14112
14113 (defun gnus-article-maybe-highlight ()
14114   "Do some article highlighting if `gnus-visual' is non-nil."
14115   (if (gnus-visual-p 'article-highlight 'highlight)
14116       (gnus-article-highlight-some)))
14117
14118 ;; Article savers.
14119
14120 (defun gnus-output-to-rmail (file-name)
14121   "Append the current article to an Rmail file named FILE-NAME."
14122   (require 'rmail)
14123   ;; Most of these codes are borrowed from rmailout.el.
14124   (setq file-name (expand-file-name file-name))
14125   (setq rmail-default-rmail-file file-name)
14126   (let ((artbuf (current-buffer))
14127         (tmpbuf (get-buffer-create " *Gnus-output*")))
14128     (save-excursion
14129       (or (get-file-buffer file-name)
14130           (file-exists-p file-name)
14131           (if (gnus-yes-or-no-p
14132                (concat "\"" file-name "\" does not exist, create it? "))
14133               (let ((file-buffer (create-file-buffer file-name)))
14134                 (save-excursion
14135                   (set-buffer file-buffer)
14136                   (rmail-insert-rmail-file-header)
14137                   (let ((require-final-newline nil))
14138                     (write-region (point-min) (point-max) file-name t 1)))
14139                 (kill-buffer file-buffer))
14140             (error "Output file does not exist")))
14141       (set-buffer tmpbuf)
14142       (buffer-disable-undo (current-buffer))
14143       (erase-buffer)
14144       (insert-buffer-substring artbuf)
14145       (gnus-convert-article-to-rmail)
14146       ;; Decide whether to append to a file or to an Emacs buffer.
14147       (let ((outbuf (get-file-buffer file-name)))
14148         (if (not outbuf)
14149             (append-to-file (point-min) (point-max) file-name)
14150           ;; File has been visited, in buffer OUTBUF.
14151           (set-buffer outbuf)
14152           (let ((buffer-read-only nil)
14153                 (msg (and (boundp 'rmail-current-message)
14154                           (symbol-value 'rmail-current-message))))
14155             ;; If MSG is non-nil, buffer is in RMAIL mode.
14156             (if msg
14157                 (progn (widen)
14158                        (narrow-to-region (point-max) (point-max))))
14159             (insert-buffer-substring tmpbuf)
14160             (if msg
14161                 (progn
14162                   (goto-char (point-min))
14163                   (widen)
14164                   (search-backward "\^_")
14165                   (narrow-to-region (point) (point-max))
14166                   (goto-char (1+ (point-min)))
14167                   (rmail-count-new-messages t)
14168                   (rmail-show-message msg)))))))
14169     (kill-buffer tmpbuf)))
14170
14171 (defun gnus-output-to-file (file-name)
14172   "Append the current article to a file named FILE-NAME."
14173   (setq file-name (expand-file-name file-name))
14174   (let ((artbuf (current-buffer))
14175         (tmpbuf (get-buffer-create " *Gnus-output*")))
14176     (save-excursion
14177       (set-buffer tmpbuf)
14178       (buffer-disable-undo (current-buffer))
14179       (erase-buffer)
14180       (insert-buffer-substring artbuf)
14181       ;; Append newline at end of the buffer as separator, and then
14182       ;; save it to file.
14183       (goto-char (point-max))
14184       (insert "\n")
14185       (append-to-file (point-min) (point-max) file-name))
14186     (kill-buffer tmpbuf)))
14187
14188 (defun gnus-convert-article-to-rmail ()
14189   "Convert article in current buffer to Rmail message format."
14190   (let ((buffer-read-only nil))
14191     ;; Convert article directly into Babyl format.
14192     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14193     (goto-char (point-min))
14194     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14195     (while (search-forward "\n\^_" nil t) ;single char
14196       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14197     (goto-char (point-max))
14198     (insert "\^_")))
14199
14200 (defun gnus-narrow-to-page (&optional arg)
14201   "Narrow the article buffer to a page.
14202 If given a numerical ARG, move forward ARG pages."
14203   (interactive "P")
14204   (setq arg (if arg (prefix-numeric-value arg) 0))
14205   (save-excursion
14206     (set-buffer gnus-article-buffer)
14207     (goto-char (point-min))
14208     (widen)
14209     (when (gnus-visual-p 'page-marker)
14210       (let ((buffer-read-only nil))
14211         (gnus-remove-text-with-property 'gnus-prev)
14212         (gnus-remove-text-with-property 'gnus-next)))
14213     (when
14214         (cond ((< arg 0)
14215                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14216               ((> arg 0)
14217                (re-search-forward page-delimiter nil 'move arg)))
14218       (goto-char (match-end 0)))
14219     (narrow-to-region
14220      (point)
14221      (if (re-search-forward page-delimiter nil 'move)
14222          (match-beginning 0)
14223        (point)))
14224     (when (and (gnus-visual-p 'page-marker)
14225                (not (= (point-min) 1)))
14226       (save-excursion
14227         (goto-char (point-min))
14228         (gnus-insert-prev-page-button)))
14229     (when (and (gnus-visual-p 'page-marker)
14230                (not (= (1- (point-max)) (buffer-size))))
14231       (save-excursion
14232         (goto-char (point-max))
14233         (gnus-insert-next-page-button)))))
14234
14235 ;; Article mode commands
14236
14237 (defun gnus-article-goto-next-page ()
14238   "Show the next page of the article."
14239   (interactive)
14240   (when (gnus-article-next-page)
14241     (gnus-article-read-summary-keys nil ?n)))
14242
14243 (defun gnus-article-goto-prev-page ()
14244   "Show the next page of the article."
14245   (interactive)
14246   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14247     (gnus-article-prev-page nil)))
14248
14249 (defun gnus-article-next-page (&optional lines)
14250   "Show the next page of the current article.
14251 If end of article, return non-nil.  Otherwise return nil.
14252 Argument LINES specifies lines to be scrolled up."
14253   (interactive "p")
14254   (move-to-window-line -1)
14255   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14256   (if (save-excursion
14257         (end-of-line)
14258         (and (pos-visible-in-window-p)  ;Not continuation line.
14259              (eobp)))
14260       ;; Nothing in this page.
14261       (if (or (not gnus-break-pages)
14262               (save-excursion
14263                 (save-restriction
14264                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14265           t                             ;Nothing more.
14266         (gnus-narrow-to-page 1)         ;Go to next page.
14267         nil)
14268     ;; More in this page.
14269     (condition-case ()
14270         (scroll-up lines)
14271       (end-of-buffer
14272        ;; Long lines may cause an end-of-buffer error.
14273        (goto-char (point-max))))
14274     (move-to-window-line 0)
14275     nil))
14276
14277 (defun gnus-article-prev-page (&optional lines)
14278   "Show previous page of current article.
14279 Argument LINES specifies lines to be scrolled down."
14280   (interactive "p")
14281   (move-to-window-line 0)
14282   (if (and gnus-break-pages
14283            (bobp)
14284            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14285       (progn
14286         (gnus-narrow-to-page -1)        ;Go to previous page.
14287         (goto-char (point-max))
14288         (recenter -1))
14289     (prog1
14290         (condition-case ()
14291             (scroll-down lines)
14292           (error nil))
14293       (move-to-window-line 0))))
14294
14295 (defun gnus-article-refer-article ()
14296   "Read article specified by message-id around point."
14297   (interactive)
14298   (let ((point (point)))
14299     (search-forward ">" nil t)          ;Move point to end of "<....>".
14300     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14301         (let ((message-id (match-string 1)))
14302           (goto-char point)
14303           (set-buffer gnus-summary-buffer)
14304           (gnus-summary-refer-article message-id))
14305       (goto-char (point))
14306       (error "No references around point"))))
14307
14308 (defun gnus-article-show-summary ()
14309   "Reconfigure windows to show summary buffer."
14310   (interactive)
14311   (gnus-configure-windows 'article)
14312   (gnus-summary-goto-subject gnus-current-article))
14313
14314 (defun gnus-article-describe-briefly ()
14315   "Describe article mode commands briefly."
14316   (interactive)
14317   (gnus-message 6
14318                 (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")))
14319
14320 (defun gnus-article-summary-command ()
14321   "Execute the last keystroke in the summary buffer."
14322   (interactive)
14323   (let ((obuf (current-buffer))
14324         (owin (current-window-configuration))
14325         func)
14326     (switch-to-buffer gnus-summary-buffer 'norecord)
14327     (setq func (lookup-key (current-local-map) (this-command-keys)))
14328     (call-interactively func)
14329     (set-buffer obuf)
14330     (set-window-configuration owin)
14331     (set-window-point (get-buffer-window (current-buffer)) (point))))
14332
14333 (defun gnus-article-summary-command-nosave ()
14334   "Execute the last keystroke in the summary buffer."
14335   (interactive)
14336   (let (func)
14337     (pop-to-buffer gnus-summary-buffer 'norecord)
14338     (setq func (lookup-key (current-local-map) (this-command-keys)))
14339     (call-interactively func)))
14340
14341 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14342   "Read a summary buffer key sequence and execute it from the article buffer."
14343   (interactive "P")
14344   (let ((nosaves
14345          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14346            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14347            "=" "^" "\M-^"))
14348         keys)
14349     (save-excursion
14350       (set-buffer gnus-summary-buffer)
14351       (push (or key last-command-event) unread-command-events)
14352       (setq keys (read-key-sequence nil)))
14353     (message "")
14354
14355     (if (member keys nosaves)
14356         (let (func)
14357           (pop-to-buffer gnus-summary-buffer 'norecord)
14358           (if (setq func (lookup-key (current-local-map) keys))
14359               (call-interactively func)
14360             (ding)))
14361       (let ((obuf (current-buffer))
14362             (owin (current-window-configuration))
14363             (opoint (point))
14364             func in-buffer)
14365         (if not-restore-window
14366             (pop-to-buffer gnus-summary-buffer 'norecord)
14367           (switch-to-buffer gnus-summary-buffer 'norecord))
14368         (setq in-buffer (current-buffer))
14369         (if (setq func (lookup-key (current-local-map) keys))
14370             (call-interactively func)
14371           (ding))
14372         (when (eq in-buffer (current-buffer))
14373           (set-buffer obuf)
14374           (unless not-restore-window
14375             (set-window-configuration owin))
14376           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14377
14378 \f
14379 ;;;
14380 ;;; Kill file handling.
14381 ;;;
14382
14383 ;;;###autoload
14384 (defalias 'gnus-batch-kill 'gnus-batch-score)
14385 ;;;###autoload
14386 (defun gnus-batch-score ()
14387   "Run batched scoring.
14388 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14389 Newsgroups is a list of strings in Bnews format.  If you want to score
14390 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14391 score the alt hierarchy, you'd say \"!alt.all\"."
14392   (interactive)
14393   (let* ((yes-and-no
14394           (gnus-newsrc-parse-options
14395            (apply (function concat)
14396                   (mapcar (lambda (g) (concat g " "))
14397                           command-line-args-left))))
14398          (gnus-expert-user t)
14399          (nnmail-spool-file nil)
14400          (gnus-use-dribble-file nil)
14401          (yes (car yes-and-no))
14402          (no (cdr yes-and-no))
14403          group newsrc entry
14404          ;; Disable verbose message.
14405          gnus-novice-user gnus-large-newsgroup)
14406     ;; Eat all arguments.
14407     (setq command-line-args-left nil)
14408     ;; Start Gnus.
14409     (gnus)
14410     ;; Apply kills to specified newsgroups in command line arguments.
14411     (setq newsrc (cdr gnus-newsrc-alist))
14412     (while newsrc
14413       (setq group (caar newsrc))
14414       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14415       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14416                (and (car entry)
14417                     (or (eq (car entry) t)
14418                         (not (zerop (car entry)))))
14419                (if yes (string-match yes group) t)
14420                (or (null no) (not (string-match no group))))
14421           (progn
14422             (gnus-summary-read-group group nil t nil t)
14423             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14424                  (gnus-summary-exit))))
14425       (setq newsrc (cdr newsrc)))
14426     ;; Exit Emacs.
14427     (switch-to-buffer gnus-group-buffer)
14428     (gnus-group-save-newsrc)))
14429
14430 (defun gnus-apply-kill-file ()
14431   "Apply a kill file to the current newsgroup.
14432 Returns the number of articles marked as read."
14433   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14434           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14435       (gnus-apply-kill-file-internal)
14436     0))
14437
14438 (defun gnus-kill-save-kill-buffer ()
14439   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14440     (when (get-file-buffer file)
14441       (save-excursion
14442         (set-buffer (get-file-buffer file))
14443         (and (buffer-modified-p) (save-buffer))
14444         (kill-buffer (current-buffer))))))
14445
14446 (defvar gnus-kill-file-name "KILL"
14447   "Suffix of the kill files.")
14448
14449 (defun gnus-newsgroup-kill-file (newsgroup)
14450   "Return the name of a kill file name for NEWSGROUP.
14451 If NEWSGROUP is nil, return the global kill file name instead."
14452   (cond 
14453    ;; The global KILL file is placed at top of the directory.
14454    ((or (null newsgroup)
14455         (string-equal newsgroup ""))
14456     (expand-file-name gnus-kill-file-name
14457                       (or gnus-kill-files-directory "~/News")))
14458    ;; Append ".KILL" to newsgroup name.
14459    ((gnus-use-long-file-name 'not-kill)
14460     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14461                               "." gnus-kill-file-name)
14462                       (or gnus-kill-files-directory "~/News")))
14463    ;; Place "KILL" under the hierarchical directory.
14464    (t
14465     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14466                               "/" gnus-kill-file-name)
14467                       (or gnus-kill-files-directory "~/News")))))
14468
14469 \f
14470 ;;;
14471 ;;; Dribble file
14472 ;;;
14473
14474 (defvar gnus-dribble-ignore nil)
14475 (defvar gnus-dribble-eval-file nil)
14476
14477 (defun gnus-dribble-file-name ()
14478   "Return the dribble file for the current .newsrc."
14479   (concat
14480    (if gnus-dribble-directory
14481        (concat (file-name-as-directory gnus-dribble-directory)
14482                (file-name-nondirectory gnus-current-startup-file))
14483      gnus-current-startup-file)
14484    "-dribble"))
14485
14486 (defun gnus-dribble-enter (string)
14487   "Enter STRING into the dribble buffer."
14488   (if (and (not gnus-dribble-ignore)
14489            gnus-dribble-buffer
14490            (buffer-name gnus-dribble-buffer))
14491       (let ((obuf (current-buffer)))
14492         (set-buffer gnus-dribble-buffer)
14493         (insert string "\n")
14494         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14495         (set-buffer obuf))))
14496
14497 (defun gnus-dribble-read-file ()
14498   "Read the dribble file from disk."
14499   (let ((dribble-file (gnus-dribble-file-name)))
14500     (save-excursion
14501       (set-buffer (setq gnus-dribble-buffer
14502                         (get-buffer-create
14503                          (file-name-nondirectory dribble-file))))
14504       (gnus-add-current-to-buffer-list)
14505       (erase-buffer)
14506       (setq buffer-file-name dribble-file)
14507       (auto-save-mode t)
14508       (buffer-disable-undo (current-buffer))
14509       (bury-buffer (current-buffer))
14510       (set-buffer-modified-p nil)
14511       (let ((auto (make-auto-save-file-name))
14512             (gnus-dribble-ignore t))
14513         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14514           ;; Load whichever file is newest -- the auto save file
14515           ;; or the "real" file.
14516           (if (file-newer-than-file-p auto dribble-file)
14517               (insert-file-contents auto)
14518             (insert-file-contents dribble-file))
14519           (unless (zerop (buffer-size))
14520             (set-buffer-modified-p t))
14521           ;; Set the file modes to reflect the .newsrc file modes.
14522           (save-buffer)
14523           (when (file-exists-p gnus-current-startup-file)
14524             (set-file-modes dribble-file
14525                             (file-modes gnus-current-startup-file)))
14526           ;; Possibly eval the file later.
14527           (when (gnus-y-or-n-p
14528                  "Auto-save file exists.  Do you want to read it? ")
14529             (setq gnus-dribble-eval-file t)))))))
14530
14531 (defun gnus-dribble-eval-file ()
14532   (if (not gnus-dribble-eval-file)
14533       ()
14534     (setq gnus-dribble-eval-file nil)
14535     (save-excursion
14536       (let ((gnus-dribble-ignore t))
14537         (set-buffer gnus-dribble-buffer)
14538         (eval-buffer (current-buffer))))))
14539
14540 (defun gnus-dribble-delete-file ()
14541   (if (file-exists-p (gnus-dribble-file-name))
14542       (delete-file (gnus-dribble-file-name)))
14543   (if gnus-dribble-buffer
14544       (save-excursion
14545         (set-buffer gnus-dribble-buffer)
14546         (let ((auto (make-auto-save-file-name)))
14547           (if (file-exists-p auto)
14548               (delete-file auto))
14549           (erase-buffer)
14550           (set-buffer-modified-p nil)))))
14551
14552 (defun gnus-dribble-save ()
14553   (if (and gnus-dribble-buffer
14554            (buffer-name gnus-dribble-buffer))
14555       (save-excursion
14556         (set-buffer gnus-dribble-buffer)
14557         (save-buffer))))
14558
14559 (defun gnus-dribble-clear ()
14560   (save-excursion
14561     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14562         (progn
14563           (set-buffer gnus-dribble-buffer)
14564           (erase-buffer)
14565           (set-buffer-modified-p nil)
14566           (setq buffer-saved-size (buffer-size))))))
14567
14568 \f
14569 ;;;
14570 ;;; Server Communication
14571 ;;;
14572
14573 (defun gnus-start-news-server (&optional confirm)
14574   "Open a method for getting news.
14575 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14576   (let (how)
14577     (if gnus-current-select-method
14578         ;; Stream is already opened.
14579         nil
14580       ;; Open NNTP server.
14581       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14582       (if confirm
14583           (progn
14584             ;; Read server name with completion.
14585             (setq gnus-nntp-server
14586                   (completing-read "NNTP server: "
14587                                    (mapcar (lambda (server) (list server))
14588                                            (cons (list gnus-nntp-server)
14589                                                  gnus-secondary-servers))
14590                                    nil nil gnus-nntp-server))))
14591
14592       (if (and gnus-nntp-server
14593                (stringp gnus-nntp-server)
14594                (not (string= gnus-nntp-server "")))
14595           (setq gnus-select-method
14596                 (cond ((or (string= gnus-nntp-server "")
14597                            (string= gnus-nntp-server "::"))
14598                        (list 'nnspool (system-name)))
14599                       ((string-match "^:" gnus-nntp-server)
14600                        (list 'nnmh gnus-nntp-server
14601                              (list 'nnmh-directory
14602                                    (file-name-as-directory
14603                                     (expand-file-name
14604                                      (concat "~/" (substring
14605                                                    gnus-nntp-server 1)))))
14606                              (list 'nnmh-get-new-mail nil)))
14607                       (t
14608                        (list 'nntp gnus-nntp-server)))))
14609
14610       (setq how (car gnus-select-method))
14611       (cond ((eq how 'nnspool)
14612              (require 'nnspool)
14613              (gnus-message 5 "Looking up local news spool..."))
14614             ((eq how 'nnmh)
14615              (require 'nnmh)
14616              (gnus-message 5 "Looking up mh spool..."))
14617             (t
14618              (require 'nntp)))
14619       (setq gnus-current-select-method gnus-select-method)
14620       (run-hooks 'gnus-open-server-hook)
14621       (or
14622        ;; gnus-open-server-hook might have opened it
14623        (gnus-server-opened gnus-select-method)
14624        (gnus-open-server gnus-select-method)
14625        (gnus-y-or-n-p
14626         (format
14627          "%s (%s) open error: '%s'.     Continue? "
14628          (car gnus-select-method) (cadr gnus-select-method)
14629          (gnus-status-message gnus-select-method)))
14630        (progn
14631          (gnus-message 1 "Couldn't open server on %s"
14632                        (nth 1 gnus-select-method))
14633          (ding)
14634          nil)))))
14635
14636 (defun gnus-check-group (group)
14637   "Try to make sure that the server where GROUP exists is alive."
14638   (let ((method (gnus-find-method-for-group group)))
14639     (or (gnus-server-opened method)
14640         (gnus-open-server method))))
14641
14642 (defun gnus-check-server (&optional method silent)
14643   "Check whether the connection to METHOD is down.
14644 If METHOD is nil, use `gnus-select-method'.
14645 If it is down, start it up (again)."
14646   (let ((method (or method gnus-select-method)))
14647     ;; Transform virtual server names into select methods.
14648     (when (stringp method)
14649       (setq method (gnus-server-to-method method)))
14650     (if (gnus-server-opened method)
14651         ;; The stream is already opened.
14652         t
14653       ;; Open the server.
14654       (unless silent
14655         (gnus-message 5 "Opening %s server%s..." (car method)
14656                       (if (equal (nth 1 method) "") ""
14657                         (format " on %s" (nth 1 method)))))
14658       (run-hooks 'gnus-open-server-hook)
14659       (prog1
14660           (gnus-open-server method)
14661         (unless silent
14662           (message ""))))))
14663
14664 (defun gnus-get-function (method function)
14665   "Return a function symbol based on METHOD and FUNCTION."
14666   ;; Translate server names into methods.
14667   (unless method
14668     (error "Attempted use of a nil select method"))
14669   (when (stringp method)
14670     (setq method (gnus-server-to-method method)))
14671   (let ((func (intern (format "%s-%s" (car method) function))))
14672     ;; If the functions isn't bound, we require the backend in
14673     ;; question.
14674     (unless (fboundp func)
14675       (require (car method))
14676       (unless (fboundp func)
14677         ;; This backend doesn't implement this function.
14678         (error "No such function: %s" func)))
14679     func))
14680
14681 \f
14682 ;;;
14683 ;;; Interface functions to the backends.
14684 ;;;
14685
14686 (defun gnus-open-server (method)
14687   "Open a connection to METHOD."
14688   (when (stringp method)
14689     (setq method (gnus-server-to-method method)))
14690   (let ((elem (assoc method gnus-opened-servers)))
14691     ;; If this method was previously denied, we just return nil.
14692     (if (eq (nth 1 elem) 'denied)
14693         (progn
14694           (gnus-message 1 "Denied server")
14695           nil)
14696       ;; Open the server.
14697       (let ((result
14698              (funcall (gnus-get-function method 'open-server)
14699                       (nth 1 method) (nthcdr 2 method))))
14700         ;; If this hasn't been opened before, we add it to the list.
14701         (unless elem
14702           (setq elem (list method nil)
14703                 gnus-opened-servers (cons elem gnus-opened-servers)))
14704         ;; Set the status of this server.
14705         (setcar (cdr elem) (if result 'ok 'denied))
14706         ;; Return the result from the "open" call.
14707         result))))
14708
14709 (defun gnus-close-server (method)
14710   "Close the connection to METHOD."
14711   (when (stringp method)
14712     (setq method (gnus-server-to-method method)))
14713   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14714
14715 (defun gnus-request-list (method)
14716   "Request the active file from METHOD."
14717   (when (stringp method)
14718     (setq method (gnus-server-to-method method)))
14719   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14720
14721 (defun gnus-request-list-newsgroups (method)
14722   "Request the newsgroups file from METHOD."
14723   (when (stringp method)
14724     (setq method (gnus-server-to-method method)))
14725   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14726
14727 (defun gnus-request-newgroups (date method)
14728   "Request all new groups since DATE from METHOD."
14729   (when (stringp method)
14730     (setq method (gnus-server-to-method method)))
14731   (funcall (gnus-get-function method 'request-newgroups)
14732            date (nth 1 method)))
14733
14734 (defun gnus-server-opened (method)
14735   "Check whether a connection to METHOD has been opened."
14736   (when (stringp method)
14737     (setq method (gnus-server-to-method method)))
14738   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14739
14740 (defun gnus-status-message (method)
14741   "Return the status message from METHOD.
14742 If METHOD is a string, it is interpreted as a group name.   The method
14743 this group uses will be queried."
14744   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14745                   method)))
14746     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14747
14748 (defun gnus-request-group (group &optional dont-check method)
14749   "Request GROUP.  If DONT-CHECK, no information is required."
14750   (let ((method (or method (gnus-find-method-for-group group))))
14751     (when (stringp method)
14752       (setq method (gnus-server-to-method method)))
14753     (funcall (gnus-get-function method 'request-group)
14754              (gnus-group-real-name group) (nth 1 method) dont-check)))
14755
14756 (defun gnus-request-asynchronous (group &optional articles)
14757   "Request that GROUP behave asynchronously.
14758 ARTICLES is the `data' of the group."
14759   (let ((method (gnus-find-method-for-group group)))
14760     (funcall (gnus-get-function method 'request-asynchronous)
14761              (gnus-group-real-name group) (nth 1 method) articles)))
14762
14763 (defun gnus-list-active-group (group)
14764   "Request active information on GROUP."
14765   (let ((method (gnus-find-method-for-group group))
14766         (func 'list-active-group))
14767     (when (gnus-check-backend-function func group)
14768       (funcall (gnus-get-function method func)
14769                (gnus-group-real-name group) (nth 1 method)))))
14770
14771 (defun gnus-request-group-description (group)
14772   "Request a description of GROUP."
14773   (let ((method (gnus-find-method-for-group group))
14774         (func 'request-group-description))
14775     (when (gnus-check-backend-function func group)
14776       (funcall (gnus-get-function method func)
14777                (gnus-group-real-name group) (nth 1 method)))))
14778
14779 (defun gnus-close-group (group)
14780   "Request the GROUP be closed."
14781   (let ((method (gnus-find-method-for-group group)))
14782     (funcall (gnus-get-function method 'close-group)
14783              (gnus-group-real-name group) (nth 1 method))))
14784
14785 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14786   "Request headers for ARTICLES in GROUP.
14787 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14788   (let ((method (gnus-find-method-for-group group)))
14789     (if (and gnus-use-cache (numberp (car articles)))
14790         (gnus-cache-retrieve-headers articles group fetch-old)
14791       (funcall (gnus-get-function method 'retrieve-headers)
14792                articles (gnus-group-real-name group) (nth 1 method)
14793                fetch-old))))
14794
14795 (defun gnus-retrieve-groups (groups method)
14796   "Request active information on GROUPS from METHOD."
14797   (when (stringp method)
14798     (setq method (gnus-server-to-method method)))
14799   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14800
14801 (defun gnus-request-type (group &optional article)
14802   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14803   (let ((method (gnus-find-method-for-group group)))
14804     (if (not (gnus-check-backend-function 'request-type (car method)))
14805         'unknown
14806       (funcall (gnus-get-function method 'request-type)
14807                (gnus-group-real-name group) article))))
14808
14809 (defun gnus-request-update-mark (group article mark)
14810   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14811   (let ((method (gnus-find-method-for-group group)))
14812     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14813         mark
14814       (funcall (gnus-get-function method 'request-update-mark)
14815                (gnus-group-real-name group) article mark))))
14816
14817 (defun gnus-request-article (article group &optional buffer)
14818   "Request the ARTICLE in GROUP.
14819 ARTICLE can either be an article number or an article Message-ID.
14820 If BUFFER, insert the article in that group."
14821   (let ((method (gnus-find-method-for-group group)))
14822     (funcall (gnus-get-function method 'request-article)
14823              article (gnus-group-real-name group) (nth 1 method) buffer)))
14824
14825 (defun gnus-request-head (article group)
14826   "Request the head of ARTICLE in GROUP."
14827   (let ((method (gnus-find-method-for-group group)))
14828     (funcall (gnus-get-function method 'request-head)
14829              article (gnus-group-real-name group) (nth 1 method))))
14830
14831 (defun gnus-request-body (article group)
14832   "Request the body of ARTICLE in GROUP."
14833   (let ((method (gnus-find-method-for-group group)))
14834     (funcall (gnus-get-function method 'request-body)
14835              article (gnus-group-real-name group) (nth 1 method))))
14836
14837 (defun gnus-request-post (method)
14838   "Post the current buffer using METHOD."
14839   (when (stringp method)
14840     (setq method (gnus-server-to-method method)))
14841   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14842
14843 (defun gnus-request-scan (group method)
14844   "Request a SCAN being performed in GROUP from METHOD.
14845 If GROUP is nil, all groups on METHOD are scanned."
14846   (let ((method (if group (gnus-find-method-for-group group) method)))
14847     (funcall (gnus-get-function method 'request-scan)
14848              (and group (gnus-group-real-name group)) (nth 1 method))))
14849
14850 (defsubst gnus-request-update-info (info method)
14851   "Request that METHOD update INFO."
14852   (when (stringp method)
14853     (setq method (gnus-server-to-method method)))
14854   (when (gnus-check-backend-function 'request-update-info (car method))
14855     (funcall (gnus-get-function method 'request-update-info)
14856              (gnus-group-real-name (gnus-info-group info))
14857              info (nth 1 method))))
14858
14859 (defun gnus-request-expire-articles (articles group &optional force)
14860   (let ((method (gnus-find-method-for-group group)))
14861     (funcall (gnus-get-function method 'request-expire-articles)
14862              articles (gnus-group-real-name group) (nth 1 method)
14863              force)))
14864
14865 (defun gnus-request-move-article
14866   (article group server accept-function &optional last)
14867   (let ((method (gnus-find-method-for-group group)))
14868     (funcall (gnus-get-function method 'request-move-article)
14869              article (gnus-group-real-name group)
14870              (nth 1 method) accept-function last)))
14871
14872 (defun gnus-request-accept-article (group &optional last method)
14873   ;; Make sure there's a newline at the end of the article.
14874   (when (stringp method)
14875     (setq method (gnus-server-to-method method)))
14876   (goto-char (point-max))
14877   (unless (bolp)
14878     (insert "\n"))
14879   (let ((func (if (symbolp group) group
14880                 (car (or method (gnus-find-method-for-group group))))))
14881     (funcall (intern (format "%s-request-accept-article" func))
14882              (if (stringp group) (gnus-group-real-name group) group)
14883              last)))
14884
14885 (defun gnus-request-replace-article (article group buffer)
14886   (let ((func (car (gnus-find-method-for-group group))))
14887     (funcall (intern (format "%s-request-replace-article" func))
14888              article (gnus-group-real-name group) buffer)))
14889
14890 (defun gnus-request-associate-buffer (group)
14891   (let ((method (gnus-find-method-for-group group)))
14892     (funcall (gnus-get-function method 'request-associate-buffer)
14893              (gnus-group-real-name group))))
14894
14895 (defun gnus-request-restore-buffer (article group)
14896   "Request a new buffer restored to the state of ARTICLE."
14897   (let ((method (gnus-find-method-for-group group)))
14898     (funcall (gnus-get-function method 'request-restore-buffer)
14899              article (gnus-group-real-name group) (nth 1 method))))
14900
14901 (defun gnus-request-create-group (group &optional method)
14902   (when (stringp method)
14903     (setq method (gnus-server-to-method method)))
14904   (let ((method (or method (gnus-find-method-for-group group))))
14905     (funcall (gnus-get-function method 'request-create-group)
14906              (gnus-group-real-name group) (nth 1 method))))
14907
14908 (defun gnus-request-delete-group (group &optional force)
14909   (let ((method (gnus-find-method-for-group group)))
14910     (funcall (gnus-get-function method 'request-delete-group)
14911              (gnus-group-real-name group) force (nth 1 method))))
14912
14913 (defun gnus-request-rename-group (group new-name)
14914   (let ((method (gnus-find-method-for-group group)))
14915     (funcall (gnus-get-function method 'request-rename-group)
14916              (gnus-group-real-name group)
14917              (gnus-group-real-name new-name) (nth 1 method))))
14918
14919 (defun gnus-member-of-valid (symbol group)
14920   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14921   (memq symbol (assoc
14922                 (symbol-name (car (gnus-find-method-for-group group)))
14923                 gnus-valid-select-methods)))
14924
14925 (defun gnus-method-option-p (method option)
14926   "Return non-nil if select METHOD has OPTION as a parameter."
14927   (when (stringp method)
14928     (setq method (gnus-server-to-method method)))
14929   (memq option (assoc (format "%s" (car method))
14930                       gnus-valid-select-methods)))
14931
14932 (defun gnus-server-extend-method (group method)
14933   ;; This function "extends" a virtual server.  If the server is
14934   ;; "hello", and the select method is ("hello" (my-var "something"))
14935   ;; in the group "alt.alt", this will result in a new virtual server
14936   ;; called "hello+alt.alt".
14937   (let ((entry
14938          (gnus-copy-sequence
14939           (if (equal (car method) "native") gnus-select-method
14940             (cdr (assoc (car method) gnus-server-alist))))))
14941     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14942     (nconc entry (cdr method))))
14943
14944 (defun gnus-find-method-for-group (group &optional info)
14945   "Find the select method that GROUP uses."
14946   (or gnus-override-method
14947       (and (not group)
14948            gnus-select-method)
14949       (let ((info (or info (gnus-get-info group)))
14950             method)
14951         (if (or (not info)
14952                 (not (setq method (gnus-info-method info)))
14953                 (equal method "native"))
14954             gnus-select-method
14955           (setq method
14956                 (cond ((stringp method)
14957                        (gnus-server-to-method method))
14958                       ((stringp (car method))
14959                        (gnus-server-extend-method group method))
14960                       (t
14961                        method)))
14962           (if (equal (cadr method) "")
14963               method
14964             (gnus-server-add-address method))))))
14965
14966 (defun gnus-check-backend-function (func group)
14967   "Check whether GROUP supports function FUNC."
14968   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14969                   group)))
14970     (fboundp (intern (format "%s-%s" method func)))))
14971
14972 (defun gnus-methods-using (feature)
14973   "Find all methods that have FEATURE."
14974   (let ((valids gnus-valid-select-methods)
14975         outs)
14976     (while valids
14977       (if (memq feature (car valids))
14978           (setq outs (cons (car valids) outs)))
14979       (setq valids (cdr valids)))
14980     outs))
14981
14982 \f
14983 ;;;
14984 ;;; Active & Newsrc File Handling
14985 ;;;
14986
14987 (defun gnus-setup-news (&optional rawfile level dont-connect)
14988   "Setup news information.
14989 If RAWFILE is non-nil, the .newsrc file will also be read.
14990 If LEVEL is non-nil, the news will be set up at level LEVEL."
14991   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14992
14993     (when init 
14994       ;; Clear some variables to re-initialize news information.
14995       (setq gnus-newsrc-alist nil
14996             gnus-active-hashtb nil)
14997       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14998       (gnus-read-newsrc-file rawfile))
14999
15000     (when (and (not (assoc "archive" gnus-server-alist))
15001                gnus-message-archive-method)
15002       (push (cons "archive" gnus-message-archive-method)
15003             gnus-server-alist))
15004
15005     ;; If we don't read the complete active file, we fill in the
15006     ;; hashtb here.
15007     (if (or (null gnus-read-active-file)
15008             (eq gnus-read-active-file 'some))
15009         (gnus-update-active-hashtb-from-killed))
15010
15011     ;; Read the active file and create `gnus-active-hashtb'.
15012     ;; If `gnus-read-active-file' is nil, then we just create an empty
15013     ;; hash table.  The partial filling out of the hash table will be
15014     ;; done in `gnus-get-unread-articles'.
15015     (and gnus-read-active-file
15016          (not level)
15017          (gnus-read-active-file))
15018
15019     (or gnus-active-hashtb
15020         (setq gnus-active-hashtb (make-vector 4095 0)))
15021
15022     ;; Initialize the cache.
15023     (when gnus-use-cache
15024       (gnus-cache-open))
15025
15026     ;; Possibly eval the dribble file.
15027     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15028
15029     (gnus-update-format-specifications)
15030
15031     ;; See whether we need to read the description file.
15032     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15033              (not gnus-description-hashtb)
15034              (not dont-connect)
15035              gnus-read-active-file)
15036         (gnus-read-all-descriptions-files))
15037
15038     ;; Find new newsgroups and treat them.
15039     (if (and init gnus-check-new-newsgroups (not level)
15040              (gnus-check-server gnus-select-method))
15041         (gnus-find-new-newsgroups))
15042
15043     ;; We might read in new NoCeM messages here.
15044     (when gnus-use-nocem 
15045       (gnus-nocem-scan-groups))
15046
15047     ;; Find the number of unread articles in each non-dead group.
15048     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15049       (gnus-get-unread-articles level))
15050
15051     (if (and init gnus-check-bogus-newsgroups
15052              gnus-read-active-file (not level)
15053              (gnus-server-opened gnus-select-method))
15054         (gnus-check-bogus-newsgroups))))
15055
15056 (defun gnus-find-new-newsgroups (&optional arg)
15057   "Search for new newsgroups and add them.
15058 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15059 The `-n' option line from .newsrc is respected.
15060 If ARG (the prefix), use the `ask-server' method to query
15061 the server for new groups."
15062   (interactive "P")
15063   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15064                        (null gnus-read-active-file)
15065                        (eq gnus-read-active-file 'some))
15066                    'ask-server gnus-check-new-newsgroups)))
15067     (unless (gnus-check-first-time-used)
15068       (if (or (consp check)
15069               (eq check 'ask-server))
15070           ;; Ask the server for new groups.
15071           (gnus-ask-server-for-new-groups)
15072         ;; Go through the active hashtb and look for new groups.
15073         (let ((groups 0)
15074               group new-newsgroups)
15075           (gnus-message 5 "Looking for new newsgroups...")
15076           (unless gnus-have-read-active-file
15077             (gnus-read-active-file))
15078           (setq gnus-newsrc-last-checked-date (current-time-string))
15079           (unless gnus-killed-hashtb
15080             (gnus-make-hashtable-from-killed))
15081           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15082           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15083           (mapatoms
15084            (lambda (sym)
15085              (if (or (null (setq group (symbol-name sym)))
15086                      (not (boundp sym))
15087                      (null (symbol-value sym))
15088                      (gnus-gethash group gnus-killed-hashtb)
15089                      (gnus-gethash group gnus-newsrc-hashtb))
15090                  ()
15091                (let ((do-sub (gnus-matches-options-n group)))
15092                  (cond
15093                   ((eq do-sub 'subscribe)
15094                    (setq groups (1+ groups))
15095                    (gnus-sethash group group gnus-killed-hashtb)
15096                    (funcall gnus-subscribe-options-newsgroup-method group))
15097                   ((eq do-sub 'ignore)
15098                    nil)
15099                   (t
15100                    (setq groups (1+ groups))
15101                    (gnus-sethash group group gnus-killed-hashtb)
15102                    (if gnus-subscribe-hierarchical-interactive
15103                        (setq new-newsgroups (cons group new-newsgroups))
15104                      (funcall gnus-subscribe-newsgroup-method group)))))))
15105            gnus-active-hashtb)
15106           (when new-newsgroups
15107             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15108           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15109           (if (> groups 0)
15110               (gnus-message 6 "%d new newsgroup%s arrived."
15111                             groups (if (> groups 1) "s have" " has"))
15112             (gnus-message 6 "No new newsgroups.")))))))
15113
15114 (defun gnus-matches-options-n (group)
15115   ;; Returns `subscribe' if the group is to be unconditionally
15116   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15117   ;; no match for the group.
15118
15119   ;; First we check the two user variables.
15120   (cond
15121    ((and gnus-options-subscribe
15122          (string-match gnus-options-subscribe group))
15123     'subscribe)
15124    ((and gnus-auto-subscribed-groups
15125          (string-match gnus-auto-subscribed-groups group))
15126     'subscribe)
15127    ((and gnus-options-not-subscribe
15128          (string-match gnus-options-not-subscribe group))
15129     'ignore)
15130    ;; Then we go through the list that was retrieved from the .newsrc
15131    ;; file.  This list has elements on the form
15132    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15133    ;; is in the reverse order of the options line) is returned.
15134    (t
15135     (let ((regs gnus-newsrc-options-n))
15136       (while (and regs
15137                   (not (string-match (caar regs) group)))
15138         (setq regs (cdr regs)))
15139       (and regs (cdar regs))))))
15140
15141 (defun gnus-ask-server-for-new-groups ()
15142   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15143          (methods (cons gnus-select-method
15144                         (nconc
15145                          (when gnus-message-archive-method
15146                            (list "archive"))
15147                          (append
15148                           (and (consp gnus-check-new-newsgroups)
15149                                gnus-check-new-newsgroups)
15150                           gnus-secondary-select-methods))))
15151          (groups 0)
15152          (new-date (current-time-string))
15153          group new-newsgroups got-new method hashtb
15154          gnus-override-subscribe-method)
15155     ;; Go through both primary and secondary select methods and
15156     ;; request new newsgroups.
15157     (while (setq method (gnus-server-get-method nil (pop methods)))
15158       (setq new-newsgroups nil)
15159       (setq gnus-override-subscribe-method method)
15160       (when (and (gnus-check-server method)
15161                  (gnus-request-newgroups date method))
15162         (save-excursion
15163           (setq got-new t)
15164           (setq hashtb (gnus-make-hashtable 100))
15165           (set-buffer nntp-server-buffer)
15166           ;; Enter all the new groups into a hashtable.
15167           (gnus-active-to-gnus-format method hashtb 'ignore)))
15168       ;; Now all new groups from `method' are in `hashtb'.
15169       (mapatoms
15170        (lambda (group-sym)
15171          (if (or (null (setq group (symbol-name group-sym)))
15172                  (null (symbol-value group-sym))
15173                  (gnus-gethash group gnus-newsrc-hashtb)
15174                  (member group gnus-zombie-list)
15175                  (member group gnus-killed-list))
15176              ;; The group is already known.
15177              ()
15178            ;; Make this group active.
15179            (when (symbol-value group-sym)
15180              (gnus-set-active group (symbol-value group-sym)))
15181            ;; Check whether we want it or not.
15182            (let ((do-sub (gnus-matches-options-n group)))
15183              (cond
15184               ((eq do-sub 'subscribe)
15185                (incf groups)
15186                (gnus-sethash group group gnus-killed-hashtb)
15187                (funcall gnus-subscribe-options-newsgroup-method group))
15188               ((eq do-sub 'ignore)
15189                nil)
15190               (t
15191                (incf groups)
15192                (gnus-sethash group group gnus-killed-hashtb)
15193                (if gnus-subscribe-hierarchical-interactive
15194                    (push group new-newsgroups)
15195                  (funcall gnus-subscribe-newsgroup-method group)))))))
15196        hashtb)
15197       (when new-newsgroups
15198         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15199     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15200     (when (> groups 0)
15201       (gnus-message 6 "%d new newsgroup%s arrived."
15202                     groups (if (> groups 1) "s have" " has")))
15203     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15204     got-new))
15205
15206 (defun gnus-check-first-time-used ()
15207   (if (or (> (length gnus-newsrc-alist) 1)
15208           (file-exists-p gnus-startup-file)
15209           (file-exists-p (concat gnus-startup-file ".el"))
15210           (file-exists-p (concat gnus-startup-file ".eld")))
15211       nil
15212     (gnus-message 6 "First time user; subscribing you to default groups")
15213     (unless gnus-have-read-active-file
15214       (gnus-read-active-file))
15215     (setq gnus-newsrc-last-checked-date (current-time-string))
15216     (let ((groups gnus-default-subscribed-newsgroups)
15217           group)
15218       (if (eq groups t)
15219           nil
15220         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15221         (mapatoms
15222          (lambda (sym)
15223            (if (null (setq group (symbol-name sym)))
15224                ()
15225              (let ((do-sub (gnus-matches-options-n group)))
15226                (cond
15227                 ((eq do-sub 'subscribe)
15228                  (gnus-sethash group group gnus-killed-hashtb)
15229                  (funcall gnus-subscribe-options-newsgroup-method group))
15230                 ((eq do-sub 'ignore)
15231                  nil)
15232                 (t
15233                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15234          gnus-active-hashtb)
15235         (while groups
15236           (if (gnus-active (car groups))
15237               (gnus-group-change-level
15238                (car groups) gnus-level-default-subscribed gnus-level-killed))
15239           (setq groups (cdr groups)))
15240         (gnus-group-make-help-group)
15241         (and gnus-novice-user
15242              (gnus-message 7 "`A k' to list killed groups"))))))
15243
15244 (defun gnus-subscribe-group (group previous &optional method)
15245   (gnus-group-change-level
15246    (if method
15247        (list t group gnus-level-default-subscribed nil nil method)
15248      group)
15249    gnus-level-default-subscribed gnus-level-killed previous t))
15250
15251 ;; `gnus-group-change-level' is the fundamental function for changing
15252 ;; subscription levels of newsgroups.  This might mean just changing
15253 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15254 ;; again, which subscribes/unsubscribes a group, which is equally
15255 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15256 ;; from 8-9 to 1-7 means that you remove the group from the list of
15257 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15258 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15259 ;; which is trivial.
15260 ;; ENTRY can either be a string (newsgroup name) or a list (if
15261 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15262 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15263 ;; entries.
15264 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15265 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15266 ;; after.
15267 (defun gnus-group-change-level (entry level &optional oldlevel
15268                                       previous fromkilled)
15269   (let (group info active num)
15270     ;; Glean what info we can from the arguments
15271     (if (consp entry)
15272         (if fromkilled (setq group (nth 1 entry))
15273           (setq group (car (nth 2 entry))))
15274       (setq group entry))
15275     (if (and (stringp entry)
15276              oldlevel
15277              (< oldlevel gnus-level-zombie))
15278         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15279     (if (and (not oldlevel)
15280              (consp entry))
15281         (setq oldlevel (gnus-info-level (nth 2 entry)))
15282       (setq oldlevel (or oldlevel 9)))
15283     (if (stringp previous)
15284         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15285
15286     (if (and (>= oldlevel gnus-level-zombie)
15287              (gnus-gethash group gnus-newsrc-hashtb))
15288         ;; We are trying to subscribe a group that is already
15289         ;; subscribed.
15290         ()                              ; Do nothing.
15291
15292       (or (gnus-ephemeral-group-p group)
15293           (gnus-dribble-enter
15294            (format "(gnus-group-change-level %S %S %S %S %S)"
15295                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15296
15297       ;; Then we remove the newgroup from any old structures, if needed.
15298       ;; If the group was killed, we remove it from the killed or zombie
15299       ;; list.  If not, and it is in fact going to be killed, we remove
15300       ;; it from the newsrc hash table and assoc.
15301       (cond
15302        ((>= oldlevel gnus-level-zombie)
15303         (if (= oldlevel gnus-level-zombie)
15304             (setq gnus-zombie-list (delete group gnus-zombie-list))
15305           (setq gnus-killed-list (delete group gnus-killed-list))))
15306        (t
15307         (if (and (>= level gnus-level-zombie)
15308                  entry)
15309             (progn
15310               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15311               (if (nth 3 entry)
15312                   (setcdr (gnus-gethash (car (nth 3 entry))
15313                                         gnus-newsrc-hashtb)
15314                           (cdr entry)))
15315               (setcdr (cdr entry) (cdddr entry))))))
15316
15317       ;; Finally we enter (if needed) the list where it is supposed to
15318       ;; go, and change the subscription level.  If it is to be killed,
15319       ;; we enter it into the killed or zombie list.
15320       (cond 
15321        ((>= level gnus-level-zombie)
15322         ;; Remove from the hash table.
15323         (gnus-sethash group nil gnus-newsrc-hashtb)
15324         ;; We do not enter foreign groups into the list of dead
15325         ;; groups.
15326         (unless (gnus-group-foreign-p group)
15327           (if (= level gnus-level-zombie)
15328               (setq gnus-zombie-list (cons group gnus-zombie-list))
15329             (setq gnus-killed-list (cons group gnus-killed-list)))))
15330        (t
15331         ;; If the list is to be entered into the newsrc assoc, and
15332         ;; it was killed, we have to create an entry in the newsrc
15333         ;; hashtb format and fix the pointers in the newsrc assoc.
15334         (if (< oldlevel gnus-level-zombie)
15335             ;; It was alive, and it is going to stay alive, so we
15336             ;; just change the level and don't change any pointers or
15337             ;; hash table entries.
15338             (setcar (cdaddr entry) level)
15339           (if (listp entry)
15340               (setq info (cdr entry)
15341                     num (car entry))
15342             (setq active (gnus-active group))
15343             (setq num
15344                   (if active (- (1+ (cdr active)) (car active)) t))
15345             ;; Check whether the group is foreign.  If so, the
15346             ;; foreign select method has to be entered into the
15347             ;; info.
15348             (let ((method (or gnus-override-subscribe-method
15349                               (gnus-group-method group))))
15350               (if (eq method gnus-select-method)
15351                   (setq info (list group level nil))
15352                 (setq info (list group level nil nil method)))))
15353           (unless previous
15354             (setq previous
15355                   (let ((p gnus-newsrc-alist))
15356                     (while (cddr p)
15357                       (setq p (cdr p)))
15358                     p)))
15359           (setq entry (cons info (cddr previous)))
15360           (if (cdr previous)
15361               (progn
15362                 (setcdr (cdr previous) entry)
15363                 (gnus-sethash group (cons num (cdr previous))
15364                               gnus-newsrc-hashtb))
15365             (setcdr previous entry)
15366             (gnus-sethash group (cons num previous)
15367                           gnus-newsrc-hashtb))
15368           (when (cdr entry)
15369             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15370       (when gnus-group-change-level-function
15371         (funcall gnus-group-change-level-function group level oldlevel)))))
15372
15373 (defun gnus-kill-newsgroup (newsgroup)
15374   "Obsolete function.  Kills a newsgroup."
15375   (gnus-group-change-level
15376    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15377
15378 (defun gnus-check-bogus-newsgroups (&optional confirm)
15379   "Remove bogus newsgroups.
15380 If CONFIRM is non-nil, the user has to confirm the deletion of every
15381 newsgroup."
15382   (let ((newsrc (cdr gnus-newsrc-alist))
15383         bogus group entry info)
15384     (gnus-message 5 "Checking bogus newsgroups...")
15385     (unless gnus-have-read-active-file
15386       (gnus-read-active-file))
15387     (when (member gnus-select-method gnus-have-read-active-file)
15388       ;; Find all bogus newsgroup that are subscribed.
15389       (while newsrc
15390         (setq info (pop newsrc)
15391               group (gnus-info-group info))
15392         (unless (or (gnus-active group) ; Active
15393                     (gnus-info-method info) ; Foreign
15394                     (and confirm
15395                          (not (gnus-y-or-n-p
15396                                (format "Remove bogus newsgroup: %s " group)))))
15397           ;; Found a bogus newsgroup.
15398           (push group bogus)))
15399       ;; Remove all bogus subscribed groups by first killing them, and
15400       ;; then removing them from the list of killed groups.
15401       (while bogus
15402         (when (setq entry (gnus-gethash (setq group (pop bogus))
15403                                         gnus-newsrc-hashtb))
15404           (gnus-group-change-level entry gnus-level-killed)
15405           (setq gnus-killed-list (delete group gnus-killed-list))))
15406       ;; Then we remove all bogus groups from the list of killed and
15407       ;; zombie groups.  They are are removed without confirmation.
15408       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15409             killed)
15410         (while dead-lists
15411           (setq killed (symbol-value (car dead-lists)))
15412           (while killed
15413             (unless (gnus-active (setq group (pop killed)))
15414               ;; The group is bogus.
15415               ;; !!!Slow as hell.
15416               (set (car dead-lists)
15417                    (delete group (symbol-value (car dead-lists))))))
15418           (setq dead-lists (cdr dead-lists))))
15419       (gnus-message 5 "Checking bogus newsgroups...done"))))
15420
15421 (defun gnus-check-duplicate-killed-groups ()
15422   "Remove duplicates from the list of killed groups."
15423   (interactive)
15424   (let ((killed gnus-killed-list))
15425     (while killed
15426       (gnus-message 9 "%d" (length killed))
15427       (setcdr killed (delete (car killed) (cdr killed)))
15428       (setq killed (cdr killed)))))
15429
15430 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15431 ;; and compute how many unread articles there are in each group.
15432 (defun gnus-get-unread-articles (&optional level)
15433   (let* ((newsrc (cdr gnus-newsrc-alist))
15434          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15435          (foreign-level
15436           (min
15437            (cond ((and gnus-activate-foreign-newsgroups
15438                        (not (numberp gnus-activate-foreign-newsgroups)))
15439                   (1+ gnus-level-subscribed))
15440                  ((numberp gnus-activate-foreign-newsgroups)
15441                   gnus-activate-foreign-newsgroups)
15442                  (t 0))
15443            level))
15444          info group active method)
15445     (gnus-message 5 "Checking new news...")
15446
15447     (while newsrc
15448       (setq active (gnus-active (setq group (gnus-info-group
15449                                              (setq info (pop newsrc))))))
15450
15451       ;; Check newsgroups.  If the user doesn't want to check them, or
15452       ;; they can't be checked (for instance, if the news server can't
15453       ;; be reached) we just set the number of unread articles in this
15454       ;; newsgroup to t.  This means that Gnus thinks that there are
15455       ;; unread articles, but it has no idea how many.
15456       (if (and (setq method (gnus-info-method info))
15457                (not (gnus-server-equal
15458                      gnus-select-method
15459                      (gnus-server-get-method nil method)))
15460                (not (gnus-secondary-method-p method)))
15461           ;; These groups are foreign.  Check the level.
15462           (when (<= (gnus-info-level info) foreign-level)
15463             (setq active (gnus-activate-group group 'scan))
15464             (gnus-close-group group))
15465
15466         ;; These groups are native or secondary.
15467         (when (and (<= (gnus-info-level info) level)
15468                    (not gnus-read-active-file))
15469           (setq active (gnus-activate-group group 'scan))
15470           (gnus-close-group group)))
15471
15472       (if active
15473           (gnus-get-unread-articles-in-group info active t)
15474         ;; The group couldn't be reached, so we nix out the number of
15475         ;; unread articles and stuff.
15476         (gnus-set-active group nil)
15477         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15478
15479     (gnus-message 5 "Checking new news...done")))
15480
15481 ;; Create a hash table out of the newsrc alist.  The `car's of the
15482 ;; alist elements are used as keys.
15483 (defun gnus-make-hashtable-from-newsrc-alist ()
15484   (let ((alist gnus-newsrc-alist)
15485         (ohashtb gnus-newsrc-hashtb)
15486         prev)
15487     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15488     (setq alist
15489           (setq prev (setq gnus-newsrc-alist
15490                            (if (equal (caar gnus-newsrc-alist)
15491                                       "dummy.group")
15492                                gnus-newsrc-alist
15493                              (cons (list "dummy.group" 0 nil) alist)))))
15494     (while alist
15495       (gnus-sethash
15496        (caar alist)
15497        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15498              prev)
15499        gnus-newsrc-hashtb)
15500       (setq prev alist
15501             alist (cdr alist)))))
15502
15503 (defun gnus-make-hashtable-from-killed ()
15504   "Create a hash table from the killed and zombie lists."
15505   (let ((lists '(gnus-killed-list gnus-zombie-list))
15506         list)
15507     (setq gnus-killed-hashtb
15508           (gnus-make-hashtable
15509            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15510     (while (setq list (symbol-value (pop lists)))
15511       (while list
15512         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15513
15514 (defun gnus-get-unread-articles-in-group (info active &optional update)
15515   (when active
15516     ;; Allow the backend to update the info in the group.
15517     (when (and update 
15518                (gnus-request-update-info
15519                 info (gnus-find-method-for-group (gnus-info-group info))))
15520       (gnus-activate-group (gnus-info-group info)))
15521     (let* ((range (gnus-info-read info))
15522            (num 0))
15523       ;; If a cache is present, we may have to alter the active info.
15524       (when (and gnus-use-cache info)
15525         (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15526       ;; Modify the list of read articles according to what articles
15527       ;; are available; then tally the unread articles and add the
15528       ;; number to the group hash table entry.
15529       (cond
15530        ((zerop (cdr active))
15531         (setq num 0))
15532        ((not range)
15533         (setq num (- (1+ (cdr active)) (car active))))
15534        ((not (listp (cdr range)))
15535         ;; Fix a single (num . num) range according to the
15536         ;; active hash table.
15537         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15538         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15539         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15540         ;; Compute number of unread articles.
15541         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15542        (t
15543         ;; The read list is a list of ranges.  Fix them according to
15544         ;; the active hash table.
15545         ;; First peel off any elements that are below the lower
15546         ;; active limit.
15547         (while (and (cdr range)
15548                     (>= (car active)
15549                         (or (and (atom (cadr range)) (cadr range))
15550                             (caadr range))))
15551           (if (numberp (car range))
15552               (setcar range
15553                       (cons (car range)
15554                             (or (and (numberp (cadr range))
15555                                      (cadr range))
15556                                 (cdadr range))))
15557             (setcdr (car range)
15558                     (or (and (numberp (nth 1 range)) (nth 1 range))
15559                         (cdadr range))))
15560           (setcdr range (cddr range)))
15561         ;; Adjust the first element to be the same as the lower limit.
15562         (if (and (not (atom (car range)))
15563                  (< (cdar range) (car active)))
15564             (setcdr (car range) (1- (car active))))
15565         ;; Then we want to peel off any elements that are higher
15566         ;; than the upper active limit.
15567         (let ((srange range))
15568           ;; Go past all legal elements.
15569           (while (and (cdr srange)
15570                       (<= (or (and (atom (cadr srange))
15571                                    (cadr srange))
15572                               (caadr srange)) (cdr active)))
15573             (setq srange (cdr srange)))
15574           (if (cdr srange)
15575               ;; Nuke all remaining illegal elements.
15576               (setcdr srange nil))
15577
15578           ;; Adjust the final element.
15579           (if (and (not (atom (car srange)))
15580                    (> (cdar srange) (cdr active)))
15581               (setcdr (car srange) (cdr active))))
15582         ;; Compute the number of unread articles.
15583         (while range
15584           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15585                                       (cdar range)))
15586                               (or (and (atom (car range)) (car range))
15587                                   (caar range)))))
15588           (setq range (cdr range)))
15589         (setq num (max 0 (- (cdr active) num)))))
15590       ;; Set the number of unread articles.
15591       (when info
15592         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15593       num)))
15594
15595 (defun gnus-activate-group (group &optional scan)
15596   ;; Check whether a group has been activated or not.
15597   ;; If SCAN, request a scan of that group as well.
15598   (let ((method (gnus-find-method-for-group group))
15599         active)
15600     (and (gnus-check-server method)
15601          ;; We escape all bugs and quit here to make it possible to
15602          ;; continue if a group is so out-there that it reports bugs
15603          ;; and stuff.
15604          (progn
15605            (and scan
15606                 (gnus-check-backend-function 'request-scan (car method))
15607                 (gnus-request-scan group method))
15608            t)
15609          (condition-case ()
15610              (gnus-request-group group)
15611         ;   (error nil)
15612            (quit nil))
15613          (save-excursion
15614            (set-buffer nntp-server-buffer)
15615            (goto-char (point-min))
15616            ;; Parse the result we got from `gnus-request-group'.
15617            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15618                 (progn
15619                   (goto-char (match-beginning 1))
15620                   (gnus-set-active
15621                    group (setq active (cons (read (current-buffer))
15622                                             (read (current-buffer)))))
15623                   ;; Return the new active info.
15624                   active))))))
15625
15626 (defun gnus-update-read-articles (group unread)
15627   "Update the list of read and ticked articles in GROUP using the
15628 UNREAD and TICKED lists.
15629 Note: UNSELECTED has to be sorted over `<'.
15630 Returns whether the updating was successful."
15631   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15632          (entry (gnus-gethash group gnus-newsrc-hashtb))
15633          (info (nth 2 entry))
15634          (prev 1)
15635          (unread (sort (copy-sequence unread) '<))
15636          read)
15637     (if (or (not info) (not active))
15638         ;; There is no info on this group if it was, in fact,
15639         ;; killed.  Gnus stores no information on killed groups, so
15640         ;; there's nothing to be done.
15641         ;; One could store the information somewhere temporarily,
15642         ;; perhaps...  Hmmm...
15643         ()
15644       ;; Remove any negative articles numbers.
15645       (while (and unread (< (car unread) 0))
15646         (setq unread (cdr unread)))
15647       ;; Remove any expired article numbers
15648       (while (and unread (< (car unread) (car active)))
15649         (setq unread (cdr unread)))
15650       ;; Compute the ranges of read articles by looking at the list of
15651       ;; unread articles.
15652       (while unread
15653         (if (/= (car unread) prev)
15654             (setq read (cons (if (= prev (1- (car unread))) prev
15655                                (cons prev (1- (car unread)))) read)))
15656         (setq prev (1+ (car unread)))
15657         (setq unread (cdr unread)))
15658       (when (<= prev (cdr active))
15659         (setq read (cons (cons prev (cdr active)) read)))
15660       ;; Enter this list into the group info.
15661       (gnus-info-set-read
15662        info (if (> (length read) 1) (nreverse read) read))
15663       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15664       (gnus-get-unread-articles-in-group info (gnus-active group))
15665       t)))
15666
15667 (defun gnus-make-articles-unread (group articles)
15668   "Mark ARTICLES in GROUP as unread."
15669   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15670                           (gnus-gethash (gnus-group-real-name group)
15671                                         gnus-newsrc-hashtb))))
15672          (ranges (gnus-info-read info))
15673          news article)
15674     (while articles
15675       (when (gnus-member-of-range
15676              (setq article (pop articles)) ranges)
15677         (setq news (cons article news))))
15678     (when news
15679       (gnus-info-set-read
15680        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15681       (gnus-group-update-group group t))))
15682
15683 ;; Enter all dead groups into the hashtb.
15684 (defun gnus-update-active-hashtb-from-killed ()
15685   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15686         (lists (list gnus-killed-list gnus-zombie-list))
15687         killed)
15688     (while lists
15689       (setq killed (car lists))
15690       (while killed
15691         (gnus-sethash (car killed) nil hashtb)
15692         (setq killed (cdr killed)))
15693       (setq lists (cdr lists)))))
15694
15695 ;; Get the active file(s) from the backend(s).
15696 (defun gnus-read-active-file ()
15697   (gnus-group-set-mode-line)
15698   (let ((methods 
15699          (append
15700           (if (gnus-check-server gnus-select-method)
15701               ;; The native server is available.
15702               (cons gnus-select-method gnus-secondary-select-methods)
15703             ;; The native server is down, so we just do the
15704             ;; secondary ones.
15705             gnus-secondary-select-methods)
15706           ;; Also read from the archive server.
15707           (when gnus-message-archive-method
15708             (list "archive"))))
15709         list-type)
15710     (setq gnus-have-read-active-file nil)
15711     (save-excursion
15712       (set-buffer nntp-server-buffer)
15713       (while methods
15714         (let* ((method (if (stringp (car methods))
15715                            (gnus-server-get-method nil (car methods))
15716                          (car methods)))
15717                (where (nth 1 method))
15718                (mesg (format "Reading active file%s via %s..."
15719                              (if (and where (not (zerop (length where))))
15720                                  (concat " from " where) "")
15721                              (car method))))
15722           (gnus-message 5 mesg)
15723           (when (gnus-check-server method)
15724             ;; Request that the backend scan its incoming messages.
15725             (and (gnus-check-backend-function 'request-scan (car method))
15726                  (gnus-request-scan nil method))
15727             (cond
15728              ((and (eq gnus-read-active-file 'some)
15729                    (gnus-check-backend-function 'retrieve-groups (car method)))
15730               (let ((newsrc (cdr gnus-newsrc-alist))
15731                     (gmethod (if (stringp method)
15732                                  (gnus-server-get-method nil method)
15733                                method))
15734                     groups info)
15735                 (while (setq info (pop newsrc))
15736                   (when (gnus-server-equal
15737                          (gnus-find-method-for-group 
15738                           (gnus-info-group info) info)
15739                          gmethod)
15740                     (push (gnus-group-real-name (gnus-info-group info)) 
15741                           groups)))
15742                 (when groups
15743                   (gnus-check-server method)
15744                   (setq list-type (gnus-retrieve-groups groups method))
15745                   (cond
15746                    ((not list-type)
15747                     (gnus-message
15748                      1 "Cannot read partial active file from %s server."
15749                      (car method))
15750                     (ding)
15751                     (sit-for 2))
15752                    ((eq list-type 'active)
15753                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15754                    (t
15755                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15756              (t
15757               (if (not (gnus-request-list method))
15758                   (progn
15759                     (unless (equal method gnus-message-archive-method)
15760                       (gnus-message 1 "Cannot read active file from %s server."
15761                                     (car method))
15762                       (ding)))
15763                 (gnus-active-to-gnus-format method)
15764                 ;; We mark this active file as read.
15765                 (push method gnus-have-read-active-file)
15766                 (gnus-message 5 "%sdone" mesg))))))
15767         (setq methods (cdr methods))))))
15768
15769 ;; Read an active file and place the results in `gnus-active-hashtb'.
15770 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15771   (unless method
15772     (setq method gnus-select-method))
15773   (let ((cur (current-buffer))
15774         (hashtb (or hashtb
15775                     (if (and gnus-active-hashtb
15776                              (not (equal method gnus-select-method)))
15777                         gnus-active-hashtb
15778                       (setq gnus-active-hashtb
15779                             (if (equal method gnus-select-method)
15780                                 (gnus-make-hashtable
15781                                  (count-lines (point-min) (point-max)))
15782                               (gnus-make-hashtable 4096)))))))
15783     ;; Delete unnecessary lines.
15784     (goto-char (point-min))
15785     (while (search-forward "\nto." nil t)
15786       (delete-region (1+ (match-beginning 0))
15787                      (progn (forward-line 1) (point))))
15788     (or (string= gnus-ignored-newsgroups "")
15789         (progn
15790           (goto-char (point-min))
15791           (delete-matching-lines gnus-ignored-newsgroups)))
15792     ;; Make the group names readable as a lisp expression even if they
15793     ;; contain special characters.
15794     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15795     (goto-char (point-max))
15796     (while (re-search-backward "[][';?()#]" nil t)
15797       (insert ?\\))
15798     ;; If these are groups from a foreign select method, we insert the
15799     ;; group prefix in front of the group names.
15800     (and method (not (gnus-server-equal
15801                       (gnus-server-get-method nil method)
15802                       (gnus-server-get-method nil gnus-select-method)))
15803          (let ((prefix (gnus-group-prefixed-name "" method)))
15804            (goto-char (point-min))
15805            (while (and (not (eobp))
15806                        (progn (insert prefix)
15807                               (zerop (forward-line 1)))))))
15808     ;; Store the active file in a hash table.
15809     (goto-char (point-min))
15810     (if (string-match "%[oO]" gnus-group-line-format)
15811         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15812         ;; If we want information on moderated groups, we use this
15813         ;; loop...
15814         (let* ((mod-hashtb (make-vector 7 0))
15815                (m (intern "m" mod-hashtb))
15816                group max min)
15817           (while (not (eobp))
15818             (condition-case nil
15819                 (progn
15820                   (narrow-to-region (point) (gnus-point-at-eol))
15821                   (setq group (let ((obarray hashtb)) (read cur)))
15822                   (if (and (numberp (setq max (read cur)))
15823                            (numberp (setq min (read cur)))
15824                            (progn
15825                              (skip-chars-forward " \t")
15826                              (not
15827                               (or (= (following-char) ?=)
15828                                   (= (following-char) ?x)
15829                                   (= (following-char) ?j)))))
15830                       (set group (cons min max))
15831                     (set group nil))
15832                   ;; Enter moderated groups into a list.
15833                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15834                       (setq gnus-moderated-list
15835                             (cons (symbol-name group) gnus-moderated-list))))
15836               (error
15837                (and group
15838                     (symbolp group)
15839                     (set group nil))))
15840             (widen)
15841             (forward-line 1)))
15842       ;; And if we do not care about moderation, we use this loop,
15843       ;; which is faster.
15844       (let (group max min)
15845         (while (not (eobp))
15846           (condition-case ()
15847               (progn
15848                 (narrow-to-region (point) (gnus-point-at-eol))
15849                 ;; group gets set to a symbol interned in the hash table
15850                 ;; (what a hack!!) - jwz
15851                 (setq group (let ((obarray hashtb)) (read cur)))
15852                 (if (and (numberp (setq max (read cur)))
15853                          (numberp (setq min (read cur)))
15854                          (progn
15855                            (skip-chars-forward " \t")
15856                            (not
15857                             (or (= (following-char) ?=)
15858                                 (= (following-char) ?x)
15859                                 (= (following-char) ?j)))))
15860                     (set group (cons min max))
15861                   (set group nil)))
15862             (error
15863              (progn
15864                (and group
15865                     (symbolp group)
15866                     (set group nil))
15867                (or ignore-errors
15868                    (gnus-message 3 "Warning - illegal active: %s"
15869                                  (buffer-substring
15870                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15871           (widen)
15872           (forward-line 1))))))
15873
15874 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15875   ;; Parse a "groups" active file.
15876   (let ((cur (current-buffer))
15877         (hashtb (or hashtb
15878                     (if (and method gnus-active-hashtb)
15879                         gnus-active-hashtb
15880                       (setq gnus-active-hashtb
15881                             (gnus-make-hashtable
15882                              (count-lines (point-min) (point-max)))))))
15883         (prefix (and method
15884                      (not (gnus-server-equal
15885                            (gnus-server-get-method nil method)
15886                            (gnus-server-get-method nil gnus-select-method)))
15887                      (gnus-group-prefixed-name "" method))))
15888
15889     (goto-char (point-min))
15890     ;; We split this into to separate loops, one with the prefix
15891     ;; and one without to speed the reading up somewhat.
15892     (if prefix
15893         (let (min max opoint group)
15894           (while (not (eobp))
15895             (condition-case ()
15896                 (progn
15897                   (read cur) (read cur)
15898                   (setq min (read cur)
15899                         max (read cur)
15900                         opoint (point))
15901                   (skip-chars-forward " \t")
15902                   (insert prefix)
15903                   (goto-char opoint)
15904                   (set (let ((obarray hashtb)) (read cur))
15905                        (cons min max)))
15906               (error (and group (symbolp group) (set group nil))))
15907             (forward-line 1)))
15908       (let (min max group)
15909         (while (not (eobp))
15910           (condition-case ()
15911               (if (= (following-char) ?2)
15912                   (progn
15913                     (read cur) (read cur)
15914                     (setq min (read cur)
15915                           max (read cur))
15916                     (set (setq group (let ((obarray hashtb)) (read cur)))
15917                          (cons min max))))
15918             (error (and group (symbolp group) (set group nil))))
15919           (forward-line 1))))))
15920
15921 (defun gnus-read-newsrc-file (&optional force)
15922   "Read startup file.
15923 If FORCE is non-nil, the .newsrc file is read."
15924   ;; Reset variables that might be defined in the .newsrc.eld file.
15925   (let ((variables gnus-variable-list))
15926     (while variables
15927       (set (car variables) nil)
15928       (setq variables (cdr variables))))
15929   (let* ((newsrc-file gnus-current-startup-file)
15930          (quick-file (concat newsrc-file ".el")))
15931     (save-excursion
15932       ;; We always load the .newsrc.eld file.  If always contains
15933       ;; much information that can not be gotten from the .newsrc
15934       ;; file (ticked articles, killed groups, foreign methods, etc.)
15935       (gnus-read-newsrc-el-file quick-file)
15936
15937       (if (and (file-exists-p gnus-current-startup-file)
15938                (or force
15939                    (and (file-newer-than-file-p newsrc-file quick-file)
15940                         (file-newer-than-file-p newsrc-file
15941                                                 (concat quick-file "d")))
15942                    (not gnus-newsrc-alist)))
15943           ;; We read the .newsrc file.  Note that if there if a
15944           ;; .newsrc.eld file exists, it has already been read, and
15945           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15946           ;; the .newsrc file, Gnus will only use the information it
15947           ;; can find there for changing the data already read -
15948           ;; ie. reading the .newsrc file will not trash the data
15949           ;; already read (except for read articles).
15950           (save-excursion
15951             (gnus-message 5 "Reading %s..." newsrc-file)
15952             (set-buffer (find-file-noselect newsrc-file))
15953             (buffer-disable-undo (current-buffer))
15954             (gnus-newsrc-to-gnus-format)
15955             (kill-buffer (current-buffer))
15956             (gnus-message 5 "Reading %s...done" newsrc-file)))
15957
15958       ;; Read any slave files.
15959       (or gnus-slave
15960           (gnus-master-read-slave-newsrc)))))
15961
15962 (defun gnus-read-newsrc-el-file (file)
15963   (let ((ding-file (concat file "d")))
15964     ;; We always, always read the .eld file.
15965     (gnus-message 5 "Reading %s..." ding-file)
15966     (let (gnus-newsrc-assoc)
15967       (condition-case nil
15968           (load ding-file t t t)
15969         (error
15970          (gnus-message 1 "Error in %s" ding-file)
15971          (ding)))
15972       (when gnus-newsrc-assoc
15973         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15974     (gnus-make-hashtable-from-newsrc-alist)
15975     (when (file-newer-than-file-p file ding-file)
15976       ;; Old format quick file
15977       (gnus-message 5 "Reading %s..." file)
15978       ;; The .el file is newer than the .eld file, so we read that one
15979       ;; as well.
15980       (gnus-read-old-newsrc-el-file file))))
15981
15982 ;; Parse the old-style quick startup file
15983 (defun gnus-read-old-newsrc-el-file (file)
15984   (let (newsrc killed marked group m info)
15985     (prog1
15986         (let ((gnus-killed-assoc nil)
15987               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15988           (prog1
15989               (condition-case nil
15990                   (load file t t t)
15991                 (error nil))
15992             (setq newsrc gnus-newsrc-assoc
15993                   killed gnus-killed-assoc
15994                   marked gnus-marked-assoc)))
15995       (setq gnus-newsrc-alist nil)
15996       (while (setq info (gnus-get-info (setq group (pop newsrc))))
15997         (if info
15998             (progn
15999               (gnus-info-set-read info (cddr group))
16000               (gnus-info-set-level
16001                info (if (nth 1 group) gnus-level-default-subscribed
16002                       gnus-level-default-unsubscribed))
16003               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16004           (push (setq info
16005                       (list (car group)
16006                             (if (nth 1 group) gnus-level-default-subscribed
16007                               gnus-level-default-unsubscribed)
16008                             (cddr group)))
16009                 gnus-newsrc-alist))
16010         ;; Copy marks into info.
16011         (when (setq m (assoc (car group) marked))
16012           (unless (nthcdr 3 info)
16013             (nconc info (list nil)))
16014           (gnus-info-set-marks
16015            info (list (cons 'tick (gnus-compress-sequence 
16016                                    (sort (cdr m) '<) t))))))
16017       (setq newsrc killed)
16018       (while newsrc
16019         (setcar newsrc (caar newsrc))
16020         (setq newsrc (cdr newsrc)))
16021       (setq gnus-killed-list killed))
16022     ;; The .el file version of this variable does not begin with
16023     ;; "options", while the .eld version does, so we just add it if it
16024     ;; isn't there.
16025     (and
16026      gnus-newsrc-options
16027      (progn
16028        (and (not (string-match "^ *options" gnus-newsrc-options))
16029             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16030        (and (not (string-match "\n$" gnus-newsrc-options))
16031             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16032        ;; Finally, if we read some options lines, we parse them.
16033        (or (string= gnus-newsrc-options "")
16034            (gnus-newsrc-parse-options gnus-newsrc-options))))
16035
16036     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16037     (gnus-make-hashtable-from-newsrc-alist)))
16038
16039 (defun gnus-make-newsrc-file (file)
16040   "Make server dependent file name by catenating FILE and server host name."
16041   (let* ((file (expand-file-name file nil))
16042          (real-file (concat file "-" (nth 1 gnus-select-method))))
16043     (if (or (file-exists-p real-file)
16044             (file-exists-p (concat real-file ".el"))
16045             (file-exists-p (concat real-file ".eld")))
16046         real-file file)))
16047
16048 (defun gnus-newsrc-to-gnus-format ()
16049   (setq gnus-newsrc-options "")
16050   (setq gnus-newsrc-options-n nil)
16051
16052   (or gnus-active-hashtb
16053       (setq gnus-active-hashtb (make-vector 4095 0)))
16054   (let ((buf (current-buffer))
16055         (already-read (> (length gnus-newsrc-alist) 1))
16056         group subscribed options-symbol newsrc Options-symbol
16057         symbol reads num1)
16058     (goto-char (point-min))
16059     ;; We intern the symbol `options' in the active hashtb so that we
16060     ;; can `eq' against it later.
16061     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16062     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16063
16064     (while (not (eobp))
16065       ;; We first read the first word on the line by narrowing and
16066       ;; then reading into `gnus-active-hashtb'.  Most groups will
16067       ;; already exist in that hashtb, so this will save some string
16068       ;; space.
16069       (narrow-to-region
16070        (point)
16071        (progn (skip-chars-forward "^ \t!:\n") (point)))
16072       (goto-char (point-min))
16073       (setq symbol
16074             (and (/= (point-min) (point-max))
16075                  (let ((obarray gnus-active-hashtb)) (read buf))))
16076       (widen)
16077       ;; Now, the symbol we have read is either `options' or a group
16078       ;; name.  If it is an options line, we just add it to a string.
16079       (cond
16080        ((or (eq symbol options-symbol)
16081             (eq symbol Options-symbol))
16082         (setq gnus-newsrc-options
16083               ;; This concating is quite inefficient, but since our
16084               ;; thorough studies show that approx 99.37% of all
16085               ;; .newsrc files only contain a single options line, we
16086               ;; don't give a damn, frankly, my dear.
16087               (concat gnus-newsrc-options
16088                       (buffer-substring
16089                        (gnus-point-at-bol)
16090                        ;; Options may continue on the next line.
16091                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16092                                 (progn (beginning-of-line) (point)))
16093                            (point)))))
16094         (forward-line -1))
16095        (symbol
16096         ;; Group names can be just numbers.  
16097         (when (numberp symbol) 
16098           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16099         (or (boundp symbol) (set symbol nil))
16100         ;; It was a group name.
16101         (setq subscribed (= (following-char) ?:)
16102               group (symbol-name symbol)
16103               reads nil)
16104         (if (eolp)
16105             ;; If the line ends here, this is clearly a buggy line, so
16106             ;; we put point a the beginning of line and let the cond
16107             ;; below do the error handling.
16108             (beginning-of-line)
16109           ;; We skip to the beginning of the ranges.
16110           (skip-chars-forward "!: \t"))
16111         ;; We are now at the beginning of the list of read articles.
16112         ;; We read them range by range.
16113         (while
16114             (cond
16115              ((looking-at "[0-9]+")
16116               ;; We narrow and read a number instead of buffer-substring/
16117               ;; string-to-int because it's faster.  narrow/widen is
16118               ;; faster than save-restriction/narrow, and save-restriction
16119               ;; produces a garbage object.
16120               (setq num1 (progn
16121                            (narrow-to-region (match-beginning 0) (match-end 0))
16122                            (read buf)))
16123               (widen)
16124               ;; If the next character is a dash, then this is a range.
16125               (if (= (following-char) ?-)
16126                   (progn
16127                     ;; We read the upper bound of the range.
16128                     (forward-char 1)
16129                     (if (not (looking-at "[0-9]+"))
16130                         ;; This is a buggy line, by we pretend that
16131                         ;; it's kinda OK.  Perhaps the user should be
16132                         ;; dinged?
16133                         (setq reads (cons num1 reads))
16134                       (setq reads
16135                             (cons
16136                              (cons num1
16137                                    (progn
16138                                      (narrow-to-region (match-beginning 0)
16139                                                        (match-end 0))
16140                                      (read buf)))
16141                              reads))
16142                       (widen)))
16143                 ;; It was just a simple number, so we add it to the
16144                 ;; list of ranges.
16145                 (setq reads (cons num1 reads)))
16146               ;; If the next char in ?\n, then we have reached the end
16147               ;; of the line and return nil.
16148               (/= (following-char) ?\n))
16149              ((= (following-char) ?\n)
16150               ;; End of line, so we end.
16151               nil)
16152              (t
16153               ;; Not numbers and not eol, so this might be a buggy
16154               ;; line...
16155               (or (eobp)
16156                   ;; If it was eob instead of ?\n, we allow it.
16157                   (progn
16158                     ;; The line was buggy.
16159                     (setq group nil)
16160                     (gnus-message 3 "Mangled line: %s"
16161                                   (buffer-substring (gnus-point-at-bol)
16162                                                     (gnus-point-at-eol)))
16163                     (ding)
16164                     (sit-for 1)))
16165               nil))
16166           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16167           ;; we allow them, because it's a common mistake to put a
16168           ;; space after the comma.
16169           (skip-chars-forward ", "))
16170
16171         ;; We have already read .newsrc.eld, so we gently update the
16172         ;; data in the hash table with the information we have just
16173         ;; read.
16174         (when group
16175           (let ((info (gnus-get-info group))
16176                 level)
16177             (if info
16178                 ;; There is an entry for this file in the alist.
16179                 (progn
16180                   (gnus-info-set-read info (nreverse reads))
16181                   ;; We update the level very gently.  In fact, we
16182                   ;; only change it if there's been a status change
16183                   ;; from subscribed to unsubscribed, or vice versa.
16184                   (setq level (gnus-info-level info))
16185                   (cond ((and (<= level gnus-level-subscribed)
16186                               (not subscribed))
16187                          (setq level (if reads
16188                                          gnus-level-default-unsubscribed
16189                                        (1+ gnus-level-default-unsubscribed))))
16190                         ((and (> level gnus-level-subscribed) subscribed)
16191                          (setq level gnus-level-default-subscribed)))
16192                   (gnus-info-set-level info level))
16193               ;; This is a new group.
16194               (setq info (list group
16195                                (if subscribed
16196                                    gnus-level-default-subscribed
16197                                  (if reads
16198                                      (1+ gnus-level-subscribed)
16199                                    gnus-level-default-unsubscribed))
16200                                (nreverse reads))))
16201             (setq newsrc (cons info newsrc))))))
16202       (forward-line 1))
16203
16204     (setq newsrc (nreverse newsrc))
16205
16206     (if (not already-read)
16207         ()
16208       ;; We now have two newsrc lists - `newsrc', which is what we
16209       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16210       ;; what we've read from .newsrc.eld.  We have to merge these
16211       ;; lists.  We do this by "attaching" any (foreign) groups in the
16212       ;; gnus-newsrc-alist to the (native) group that precedes them.
16213       (let ((rc (cdr gnus-newsrc-alist))
16214             (prev gnus-newsrc-alist)
16215             entry mentry)
16216         (while rc
16217           (or (null (nth 4 (car rc)))   ; It's a native group.
16218               (assoc (caar rc) newsrc) ; It's already in the alist.
16219               (if (setq entry (assoc (caar prev) newsrc))
16220                   (setcdr (setq mentry (memq entry newsrc))
16221                           (cons (car rc) (cdr mentry)))
16222                 (setq newsrc (cons (car rc) newsrc))))
16223           (setq prev rc
16224                 rc (cdr rc)))))
16225
16226     (setq gnus-newsrc-alist newsrc)
16227     ;; We make the newsrc hashtb.
16228     (gnus-make-hashtable-from-newsrc-alist)
16229
16230     ;; Finally, if we read some options lines, we parse them.
16231     (or (string= gnus-newsrc-options "")
16232         (gnus-newsrc-parse-options gnus-newsrc-options))))
16233
16234 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16235 ;; The return value will be a list on the form
16236 ;; ((regexp1 . ignore)
16237 ;;  (regexp2 . subscribe)...)
16238 ;; When handling new newsgroups, groups that match a `ignore' regexp
16239 ;; will be ignored, and groups that match a `subscribe' regexp will be
16240 ;; subscribed.  A line like
16241 ;; options -n !all rec.all
16242 ;; will lead to a list that looks like
16243 ;; (("^rec\\..+" . subscribe)
16244 ;;  ("^.+" . ignore))
16245 ;; So all "rec.*" groups will be subscribed, while all the other
16246 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16247 ;; different from "options -n rec.all !all".
16248 (defun gnus-newsrc-parse-options (options)
16249   (let (out eol)
16250     (save-excursion
16251       (gnus-set-work-buffer)
16252       (insert (regexp-quote options))
16253       ;; First we treat all continuation lines.
16254       (goto-char (point-min))
16255       (while (re-search-forward "\n[ \t]+" nil t)
16256         (replace-match " " t t))
16257       ;; Then we transform all "all"s into ".+"s.
16258       (goto-char (point-min))
16259       (while (re-search-forward "\\ball\\b" nil t)
16260         (replace-match ".+" t t))
16261       (goto-char (point-min))
16262       ;; We remove all other options than the "-n" ones.
16263       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16264         (replace-match " ")
16265         (forward-char -1))
16266       (goto-char (point-min))
16267
16268       ;; We are only interested in "options -n" lines - we
16269       ;; ignore the other option lines.
16270       (while (re-search-forward "[ \t]-n" nil t)
16271         (setq eol
16272               (or (save-excursion
16273                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16274                          (- (point) 2)))
16275                   (gnus-point-at-eol)))
16276         ;; Search for all "words"...
16277         (while (re-search-forward "[^ \t,\n]+" eol t)
16278           (if (= (char-after (match-beginning 0)) ?!)
16279               ;; If the word begins with a bang (!), this is a "not"
16280               ;; spec.  We put this spec (minus the bang) and the
16281               ;; symbol `ignore' into the list.
16282               (setq out (cons (cons (concat
16283                                      "^" (buffer-substring
16284                                           (1+ (match-beginning 0))
16285                                           (match-end 0)))
16286                                     'ignore) out))
16287             ;; There was no bang, so this is a "yes" spec.
16288             (setq out (cons (cons (concat "^" (match-string 0))
16289                                   'subscribe) out)))))
16290
16291       (setq gnus-newsrc-options-n out))))
16292
16293 (defun gnus-save-newsrc-file (&optional force)
16294   "Save .newsrc file."
16295   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16296   ;; from the variable gnus-newsrc-alist.
16297   (when (and (or gnus-newsrc-alist gnus-killed-list)
16298              gnus-current-startup-file)
16299     (save-excursion
16300       (if (and (or gnus-use-dribble-file gnus-slave)
16301                (not force)
16302                (or (not gnus-dribble-buffer)
16303                    (not (buffer-name gnus-dribble-buffer))
16304                    (zerop (save-excursion
16305                             (set-buffer gnus-dribble-buffer)
16306                             (buffer-size)))))
16307           (gnus-message 4 "(No changes need to be saved)")
16308         (run-hooks 'gnus-save-newsrc-hook)
16309         (if gnus-slave
16310             (gnus-slave-save-newsrc)
16311           ;; Save .newsrc.
16312           (when gnus-save-newsrc-file
16313             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16314             (gnus-gnus-to-newsrc-format)
16315             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16316           ;; Save .newsrc.eld.
16317           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16318           (make-local-variable 'version-control)
16319           (setq version-control 'never)
16320           (setq buffer-file-name
16321                 (concat gnus-current-startup-file ".eld"))
16322           (gnus-add-current-to-buffer-list)
16323           (buffer-disable-undo (current-buffer))
16324           (erase-buffer)
16325           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16326           (gnus-gnus-to-quick-newsrc-format)
16327           (run-hooks 'gnus-save-quick-newsrc-hook)
16328           (save-buffer)
16329           (kill-buffer (current-buffer))
16330           (gnus-message
16331            5 "Saving %s.eld...done" gnus-current-startup-file))
16332         (gnus-dribble-delete-file)))))
16333
16334 (defun gnus-gnus-to-quick-newsrc-format ()
16335   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16336   (insert ";; Gnus startup file.\n")
16337   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16338   (insert ";; to read .newsrc.\n")
16339   (insert "(setq gnus-newsrc-file-version "
16340           (prin1-to-string gnus-version) ")\n")
16341   (let ((variables
16342          (if gnus-save-killed-list gnus-variable-list
16343            ;; Remove the `gnus-killed-list' from the list of variables
16344            ;; to be saved, if required.
16345            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16346         ;; Peel off the "dummy" group.
16347         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16348         variable)
16349     ;; Insert the variables into the file.
16350     (while variables
16351       (when (and (boundp (setq variable (pop variables)))
16352                  (symbol-value variable))
16353         (insert "(setq " (symbol-name variable) " '")
16354         (prin1 (symbol-value variable) (current-buffer))
16355         (insert ")\n")))))
16356
16357 (defun gnus-gnus-to-newsrc-format ()
16358   ;; Generate and save the .newsrc file.
16359   (save-excursion
16360     (set-buffer (create-file-buffer gnus-current-startup-file))
16361     (let ((newsrc (cdr gnus-newsrc-alist))
16362           (standard-output (current-buffer))
16363           info ranges range method)
16364       (setq buffer-file-name gnus-current-startup-file)
16365       (buffer-disable-undo (current-buffer))
16366       (erase-buffer)
16367       ;; Write options.
16368       (if gnus-newsrc-options (insert gnus-newsrc-options))
16369       ;; Write subscribed and unsubscribed.
16370       (while (setq info (pop newsrc))
16371         ;; Don't write foreign groups to .newsrc.
16372         (when (or (null (setq method (gnus-info-method info)))
16373                   (equal method "native")
16374                   (gnus-server-equal method gnus-select-method))
16375           (insert (gnus-info-group info)
16376                   (if (> (gnus-info-level info) gnus-level-subscribed)
16377                       "!" ":"))
16378           (when (setq ranges (gnus-info-read info))
16379             (insert " ")
16380             (if (not (listp (cdr ranges)))
16381                 (if (= (car ranges) (cdr ranges))
16382                     (princ (car ranges))
16383                   (princ (car ranges))
16384                   (insert "-")
16385                   (princ (cdr ranges)))
16386               (while (setq range (pop ranges))
16387                 (if (or (atom range) (= (car range) (cdr range)))
16388                     (princ (or (and (atom range) range) (car range)))
16389                   (princ (car range))
16390                   (insert "-")
16391                   (princ (cdr range)))
16392                 (if ranges (insert ",")))))
16393           (insert "\n")))
16394       (make-local-variable 'version-control)
16395       (setq version-control 'never)
16396       ;; It has been reported that sometime the modtime on the .newsrc
16397       ;; file seems to be off.  We really do want to overwrite it, so
16398       ;; we clear the modtime here before saving.  It's a bit odd,
16399       ;; though...
16400       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16401       ;; delete the silly thing entirely first.  but this fails to provide
16402       ;; such niceties as .newsrc~ creation.
16403       (if gnus-modtime-botch
16404           (delete-file gnus-startup-file)
16405         (clear-visited-file-modtime))
16406       (run-hooks 'gnus-save-standard-newsrc-hook)
16407       (save-buffer)
16408       (kill-buffer (current-buffer)))))
16409
16410 \f
16411 ;;;
16412 ;;; Slave functions.
16413 ;;;
16414
16415 (defun gnus-slave-save-newsrc ()
16416   (save-excursion
16417     (set-buffer gnus-dribble-buffer)
16418     (let ((slave-name
16419            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16420       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16421
16422 (defun gnus-master-read-slave-newsrc ()
16423   (let ((slave-files
16424          (directory-files
16425           (file-name-directory gnus-current-startup-file)
16426           t (concat
16427              "^" (regexp-quote
16428                   (concat
16429                    (file-name-nondirectory gnus-current-startup-file)
16430                    "-slave-")))
16431           t))
16432         file)
16433     (if (not slave-files)
16434         ()                              ; There are no slave files to read.
16435       (gnus-message 7 "Reading slave newsrcs...")
16436       (save-excursion
16437         (set-buffer (get-buffer-create " *gnus slave*"))
16438         (buffer-disable-undo (current-buffer))
16439         (setq slave-files
16440               (sort (mapcar (lambda (file)
16441                               (list (nth 5 (file-attributes file)) file))
16442                             slave-files)
16443                     (lambda (f1 f2)
16444                       (or (< (caar f1) (caar f2))
16445                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16446         (while slave-files
16447           (erase-buffer)
16448           (setq file (nth 1 (car slave-files)))
16449           (insert-file-contents file)
16450           (if (condition-case ()
16451                   (progn
16452                     (eval-buffer (current-buffer))
16453                     t)
16454                 (error
16455                  (gnus-message 3 "Possible error in %s" file)
16456                  (ding)
16457                  (sit-for 2)
16458                  nil))
16459               (or gnus-slave ; Slaves shouldn't delete these files.
16460                   (condition-case ()
16461                       (delete-file file)
16462                     (error nil))))
16463           (setq slave-files (cdr slave-files))))
16464       (gnus-message 7 "Reading slave newsrcs...done"))))
16465
16466 \f
16467 ;;;
16468 ;;; Group description.
16469 ;;;
16470
16471 (defun gnus-read-all-descriptions-files ()
16472   (let ((methods (cons gnus-select-method 
16473                        (nconc
16474                         (when gnus-message-archive-method
16475                           (list "archive"))
16476                         gnus-secondary-select-methods))))
16477     (while methods
16478       (gnus-read-descriptions-file (car methods))
16479       (setq methods (cdr methods)))
16480     t))
16481
16482 (defun gnus-read-descriptions-file (&optional method)
16483   (let ((method (or method gnus-select-method)))
16484     (when (stringp method)
16485       (setq method (gnus-server-to-method method)))
16486     ;; We create the hashtable whether we manage to read the desc file
16487     ;; to avoid trying to re-read after a failed read.
16488     (or gnus-description-hashtb
16489         (setq gnus-description-hashtb
16490               (gnus-make-hashtable (length gnus-active-hashtb))))
16491     ;; Mark this method's desc file as read.
16492     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16493                   gnus-description-hashtb)
16494
16495     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16496     (cond
16497      ((not (gnus-check-server method))
16498       (gnus-message 1 "Couldn't open server")
16499       nil)
16500      ((not (gnus-request-list-newsgroups method))
16501       (gnus-message 1 "Couldn't read newsgroups descriptions")
16502       nil)
16503      (t
16504       (let (group)
16505         (save-excursion
16506           (save-restriction
16507             (set-buffer nntp-server-buffer)
16508             (goto-char (point-min))
16509             (if (or (search-forward "\n.\n" nil t)
16510                     (goto-char (point-max)))
16511                 (progn
16512                   (beginning-of-line)
16513                   (narrow-to-region (point-min) (point))))
16514             (goto-char (point-min))
16515             (while (not (eobp))
16516               ;; If we get an error, we set group to 0, which is not a
16517               ;; symbol...
16518               (setq group
16519                     (condition-case ()
16520                         (let ((obarray gnus-description-hashtb))
16521                           ;; Group is set to a symbol interned in this
16522                           ;; hash table.
16523                           (read nntp-server-buffer))
16524                       (error 0)))
16525               (skip-chars-forward " \t")
16526               ;; ...  which leads to this line being effectively ignored.
16527               (and (symbolp group)
16528                    (set group (buffer-substring
16529                                (point) (progn (end-of-line) (point)))))
16530               (forward-line 1))))
16531         (gnus-message 5 "Reading descriptions file...done")
16532         t)))))
16533
16534 (defun gnus-group-get-description (group)
16535   "Get the description of a group by sending XGTITLE to the server."
16536   (when (gnus-request-group-description group)
16537     (save-excursion
16538       (set-buffer nntp-server-buffer)
16539       (goto-char (point-min))
16540       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16541         (match-string 1)))))
16542
16543 \f
16544 ;;;
16545 ;;; Shutdown
16546 ;;;
16547
16548 (defvar gnus-shutdown-alist nil)
16549
16550 (defun gnus-add-shutdown (function &rest symbols)
16551   "Run FUNCTION whenever one of SYMBOLS is shut down."
16552   (push (cons function symbols) gnus-shutdown-alist))
16553
16554 (defun gnus-shutdown (symbol)
16555   "Shut down everything that waits for SYMBOL."
16556   (let ((alist gnus-shutdown-alist)
16557         entry)
16558     (while (setq entry (pop alist))
16559       (when (memq symbol (cdr entry))
16560         (funcall (car entry))))))
16561
16562 \f
16563 ;;;
16564 ;;; Buffering of read articles.
16565 ;;;
16566
16567 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16568 (defvar gnus-backlog-articles nil)
16569 (defvar gnus-backlog-hashtb nil)
16570
16571 (defun gnus-backlog-buffer ()
16572   "Return the backlog buffer."
16573   (or (get-buffer gnus-backlog-buffer)
16574       (save-excursion
16575         (set-buffer (get-buffer-create gnus-backlog-buffer))
16576         (buffer-disable-undo (current-buffer))
16577         (setq buffer-read-only t)
16578         (gnus-add-current-to-buffer-list)
16579         (get-buffer gnus-backlog-buffer))))
16580
16581 (defun gnus-backlog-setup ()
16582   "Initialize backlog variables."
16583   (unless gnus-backlog-hashtb
16584     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16585
16586 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16587
16588 (defun gnus-backlog-shutdown ()
16589   "Clear all backlog variables and buffers."
16590   (when (get-buffer gnus-backlog-buffer)
16591     (kill-buffer gnus-backlog-buffer))
16592   (setq gnus-backlog-hashtb nil
16593         gnus-backlog-articles nil))
16594
16595 (defun gnus-backlog-enter-article (group number buffer)
16596   (gnus-backlog-setup)
16597   (let ((ident (intern (concat group ":" (int-to-string number))
16598                        gnus-backlog-hashtb))
16599         b)
16600     (if (memq ident gnus-backlog-articles)
16601         () ; It's already kept.
16602       ;; Remove the oldest article, if necessary.
16603       (and (numberp gnus-keep-backlog)
16604            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16605            (gnus-backlog-remove-oldest-article))
16606       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16607       ;; Insert the new article.
16608       (save-excursion
16609         (set-buffer (gnus-backlog-buffer))
16610         (let (buffer-read-only)
16611           (goto-char (point-max))
16612           (or (bolp) (insert "\n"))
16613           (setq b (point))
16614           (insert-buffer-substring buffer)
16615           ;; Tag the beginning of the article with the ident.
16616           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16617
16618 (defun gnus-backlog-remove-oldest-article ()
16619   (save-excursion
16620     (set-buffer (gnus-backlog-buffer))
16621     (goto-char (point-min))
16622     (if (zerop (buffer-size))
16623         () ; The buffer is empty.
16624       (let ((ident (get-text-property (point) 'gnus-backlog))
16625             buffer-read-only)
16626         ;; Remove the ident from the list of articles.
16627         (when ident
16628           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16629         ;; Delete the article itself.
16630         (delete-region
16631          (point) (next-single-property-change
16632                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16633
16634 (defun gnus-backlog-request-article (group number buffer)
16635   (when (numberp number)
16636     (gnus-backlog-setup)
16637     (let ((ident (intern (concat group ":" (int-to-string number))
16638                          gnus-backlog-hashtb))
16639           beg end)
16640       (when (memq ident gnus-backlog-articles)
16641         ;; It was in the backlog.
16642         (save-excursion
16643           (set-buffer (gnus-backlog-buffer))
16644           (if (not (setq beg (text-property-any
16645                               (point-min) (point-max) 'gnus-backlog
16646                               ident)))
16647               ;; It wasn't in the backlog after all.
16648               (ignore
16649                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16650             ;; Find the end (i. e., the beginning of the next article).
16651             (setq end
16652                   (next-single-property-change
16653                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16654         (let ((buffer-read-only nil))
16655           (erase-buffer)
16656           (insert-buffer-substring gnus-backlog-buffer beg end)
16657           t)))))
16658
16659 ;; Allow redefinition of Gnus functions.
16660
16661 (gnus-ems-redefine)
16662
16663 (provide 'gnus)
16664
16665 ;;; gnus.el ends here